CBMC
goto_cc_cmdline.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Command line interpretation for goto-cc
4 
5 Author: Daniel Kroening
6 
7 Date: April 2010
8 
9 \*******************************************************************/
10 
13 
14 #include "goto_cc_cmdline.h"
15 
16 #include <algorithm>
17 #include <cstdio>
18 #include <cstring>
19 #include <iostream>
20 
21 #include <util/invariant.h>
22 #include <util/prefix.h>
23 #include <util/tempfile.h>
24 
26 {
27  if(!stdin_file.empty())
28  {
29  int result=remove(stdin_file.c_str());
30  if(result!=0)
31  {
32  // Let's print the error to stderr instead of ignoring it completely
33  std::perror("Remove failed");
34  }
35  }
36 }
37 
38 bool goto_cc_cmdlinet::in_list(const char *option, const char **list)
39 {
40  for(std::size_t i=0; list[i]!=nullptr; i++)
41  {
42  if(strcmp(option, list[i])==0)
43  return true;
44  }
45 
46  return false;
47 }
48 
49 std::size_t goto_cc_cmdlinet::get_optnr(const std::string &opt_string)
50 {
52  cmdlinet::optiont option;
53 
54  if(has_prefix(opt_string, "--")) // starts with -- ?
55  {
56  if(opt_string.size()==3) // still "short"
57  {
58  option.islong=false;
59  option.optchar=opt_string[2];
60  optnr=getoptnr(option.optchar);
61  }
62  else
63  {
64  option.islong=true;
65  option.optstring=std::string(opt_string, 2, std::string::npos);
66  option.optchar=0;
67  optnr=getoptnr(option.optstring);
68  }
69  }
70  else if(has_prefix(opt_string, "-")) // starts with - ?
71  {
72  if(opt_string.size()==2)
73  {
74  option.islong=false;
75  option.optchar=opt_string[1];
76  optnr=getoptnr(option.optchar);
77  }
78  else
79  {
80  option.islong=true;
81  option.optstring=std::string(opt_string, 1, std::string::npos);
82  option.optchar=0;
83  optnr=getoptnr(option.optstring);
84  }
85  }
86  else
87  {
89  return 0;
90  }
91 
92  // new?
93  if(!optnr.has_value())
94  {
95  options.push_back(option);
96  return options.size()-1;
97  }
98 
99  return *optnr;
100 }
101 
102 void goto_cc_cmdlinet::add_infile_arg(const std::string &arg)
103 {
104  parsed_argv.push_back(argt(arg));
105  parsed_argv.back().is_infile_name=true;
106 
107  if(arg=="-")
108  {
109  stdin_file=get_temporary_file("goto-cc", "stdin");
110 
111  FILE *tmp=fopen(stdin_file.c_str(), "wt");
112 
113  char ch;
114  while(std::cin.read(&ch, 1))
115  fputc(ch, tmp);
116 
117  fclose(tmp);
118  }
119 }
120 
122 {
123  return std::any_of(
124  parsed_argv.cbegin(), parsed_argv.cend(), [](const argt &arg) {
125  return arg.is_infile_name;
126  });
127 }
UNREACHABLE
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:503
get_temporary_file
std::string get_temporary_file(const std::string &prefix, const std::string &suffix)
Substitute for mkstemps (OpenBSD standard) for Windows, where it is unavailable.
Definition: tempfile.cpp:99
tempfile.h
goto_cc_cmdlinet::parsed_argv
parsed_argvt parsed_argv
Definition: goto_cc_cmdline.h:64
goto_cc_cmdlinet::stdin_file
std::string stdin_file
Definition: goto_cc_cmdline.h:68
prefix.h
invariant.h
cmdlinet::options
std::vector< optiont > options
Definition: cmdline.h:184
goto_cc_cmdlinet::~goto_cc_cmdlinet
~goto_cc_cmdlinet()
Definition: goto_cc_cmdline.cpp:25
cmdlinet::optiont::islong
bool islong
Definition: cmdline.h:159
goto_cc_cmdline.h
cmdlinet::optiont::optstring
std::string optstring
Definition: cmdline.h:161
has_prefix
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
optionalt
nonstd::optional< T > optionalt
Definition: optional.h:35
goto_cc_cmdlinet::have_infile_arg
bool have_infile_arg() const
Definition: goto_cc_cmdline.cpp:121
goto_cc_cmdlinet::get_optnr
std::size_t get_optnr(const std::string &option)
Definition: goto_cc_cmdline.cpp:49
goto_cc_cmdlinet::argt
Definition: goto_cc_cmdline.h:54
cmdlinet::getoptnr
optionalt< std::size_t > getoptnr(char option) const
Definition: cmdline.cpp:135
cmdlinet::optiont::optchar
char optchar
Definition: cmdline.h:160
cmdlinet::optiont
Definition: cmdline.h:155
goto_cc_cmdlinet::add_infile_arg
void add_infile_arg(const std::string &arg)
Definition: goto_cc_cmdline.cpp:102
goto_cc_cmdlinet::in_list
static bool in_list(const char *option, const char **list)
Definition: goto_cc_cmdline.cpp:38