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