cprover
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 <cstring>
17 #include <cassert>
18 #include <iostream>
19 #include <cstdio>
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 
50  const char *option,
51  const char **list,
52  std::string &prefix)
53 {
54  for(std::size_t i=0; list[i]!=nullptr; i++)
55  {
56  if(strncmp(option, list[i], strlen(list[i]))==0)
57  {
58  prefix=std::string(list[i]);
59  return true;
60  }
61  }
62 
63  return false;
64 }
65 
66 std::size_t goto_cc_cmdlinet::get_optnr(const std::string &opt_string)
67 {
69  cmdlinet::optiont option;
70 
71  if(has_prefix(opt_string, "--")) // starts with -- ?
72  {
73  if(opt_string.size()==3) // still "short"
74  {
75  option.islong=false;
76  option.optchar=opt_string[2];
77  optnr=getoptnr(option.optchar);
78  }
79  else
80  {
81  option.islong=true;
82  option.optstring=std::string(opt_string, 2, std::string::npos);
83  option.optchar=0;
84  optnr=getoptnr(option.optstring);
85  }
86  }
87  else if(has_prefix(opt_string, "-")) // starts with - ?
88  {
89  if(opt_string.size()==2)
90  {
91  option.islong=false;
92  option.optchar=opt_string[1];
93  optnr=getoptnr(option.optchar);
94  }
95  else
96  {
97  option.islong=true;
98  option.optstring=std::string(opt_string, 1, std::string::npos);
99  option.optchar=0;
100  optnr=getoptnr(option.optstring);
101  }
102  }
103  else
104  {
105  UNREACHABLE;
106  return 0;
107  }
108 
109  // new?
110  if(!optnr.has_value())
111  {
112  options.push_back(option);
113  return options.size()-1;
114  }
115 
116  return *optnr;
117 }
118 
119 void goto_cc_cmdlinet::add_infile_arg(const std::string &arg)
120 {
121  parsed_argv.push_back(argt(arg));
122  parsed_argv.back().is_infile_name=true;
123 
124  if(arg=="-")
125  {
126  stdin_file=get_temporary_file("goto-cc", "stdin");
127 
128  FILE *tmp=fopen(stdin_file.c_str(), "wt");
129 
130  char ch;
131  while(std::cin.read(&ch, 1))
132  fputc(ch, tmp);
133 
134  fclose(tmp);
135  }
136 }
std::string stdin_file
std::size_t get_optnr(const std::string &option)
Command line interpretation for goto-cc.
static bool prefix_in_list(const char *option, const char **list, std::string &prefix)
static bool in_list(const char *option, const char **list)
nonstd::optional< T > optionalt
Definition: optional.h:35
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
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:87
#define UNREACHABLE
Definition: invariant.h:271
parsed_argvt parsed_argv
optionalt< std::size_t > getoptnr(char option) const
Definition: cmdline.cpp:121
std::vector< optiont > options
Definition: cmdline.h:61
void add_infile_arg(const std::string &arg)
std::string optstring
Definition: cmdline.h:50