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/prefix.h>
22 #include <util/tempfile.h>
23 
25 {
26  if(!stdin_file.empty())
27  remove(stdin_file.c_str());
28 }
29 
30 bool goto_cc_cmdlinet::in_list(const char *option, const char **list)
31 {
32  for(std::size_t i=0; list[i]!=nullptr; i++)
33  {
34  if(strcmp(option, list[i])==0)
35  return true;
36  }
37 
38  return false;
39 }
40 
42  const char *option,
43  const char **list,
44  std::string &prefix)
45 {
46  for(std::size_t i=0; list[i]!=nullptr; i++)
47  {
48  if(strncmp(option, list[i], strlen(list[i]))==0)
49  {
50  prefix=std::string(list[i]);
51  return true;
52  }
53  }
54 
55  return false;
56 }
57 
58 std::size_t goto_cc_cmdlinet::get_optnr(const std::string &opt_string)
59 {
60  int optnr;
61  cmdlinet::optiont option;
62 
63  if(has_prefix(opt_string, "--")) // starts with -- ?
64  {
65  if(opt_string.size()==3) // still "short"
66  {
67  option.islong=false;
68  option.optchar=opt_string[2];
69  optnr=getoptnr(option.optchar);
70  }
71  else
72  {
73  option.islong=true;
74  option.optstring=std::string(opt_string, 2, std::string::npos);
75  option.optchar=0;
76  optnr=getoptnr(option.optstring);
77  }
78  }
79  else if(has_prefix(opt_string, "-")) // starts with - ?
80  {
81  if(opt_string.size()==2)
82  {
83  option.islong=false;
84  option.optchar=opt_string[1];
85  optnr=getoptnr(option.optchar);
86  }
87  else
88  {
89  option.islong=true;
90  option.optstring=std::string(opt_string, 1, std::string::npos);
91  option.optchar=0;
92  optnr=getoptnr(option.optstring);
93  }
94  }
95  else
96  {
97  assert(false);
98  return -1;
99  }
100 
101  // new?
102  if(optnr==-1)
103  {
104  options.push_back(option);
105  return options.size()-1;
106  }
107 
108  return optnr;
109 }
110 
111 void goto_cc_cmdlinet::add_infile_arg(const std::string &arg)
112 {
113  parsed_argv.push_back(argt(arg));
114  parsed_argv.back().is_infile_name=true;
115 
116  if(arg=="-")
117  {
118  stdin_file=get_temporary_file("goto-cc", "stdin");
119 
120  FILE *tmp=fopen(stdin_file.c_str(), "wt");
121 
122  char ch;
123  while(std::cin.read(&ch, 1))
124  fputc(ch, tmp);
125 
126  fclose(tmp);
127  }
128 }
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)
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
parsed_argvt parsed_argv
int getoptnr(char option) const
Definition: cmdline.cpp:98
std::vector< optiont > options
Definition: cmdline.h:58
void add_infile_arg(const std::string &arg)
std::string optstring
Definition: cmdline.h:47