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 {
51  std::optional<std::size_t> optnr;
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 }
std::optional< std::size_t > getoptnr(char option) const
Definition: cmdline.cpp:145
std::vector< optiont > options
Definition: cmdline.h:193
parsed_argvt parsed_argv
void add_infile_arg(const std::string &arg)
std::size_t get_optnr(const std::string &option)
std::string stdin_file
bool have_infile_arg() const
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
Command line interpretation for goto-cc.
int __CPROVER_ID java::java io InputStream read
Definition: java.io.c:5
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:525
int fclose(FILE *stream)
Definition: stdio.c:230
void perror(const char *s)
Definition: stdio.c:946
FILE * fopen(const char *filename, const char *mode)
Definition: stdio.c:77
int strcmp(const char *s1, const char *s2)
Definition: string.c:363
std::string optstring
Definition: cmdline.h:170
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