CBMC
as86_cmdline.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: A special command line object for as86 (of Bruce's C Compiler)
4 
5 Author: Michael Tautschnig
6 
7 \*******************************************************************/
8 
11 
12 #include "as86_cmdline.h"
13 
14 #include <util/invariant.h>
15 #include <util/prefix.h>
16 
17 #include <iostream>
18 
19 // non-as86 options
21 {
22  "--verbosity",
23  "--function",
24  "--native-assembler",
25  "--print-rejected-preprocessed-source",
26  nullptr
27 };
28 
30 {
31  "-0",
32  "-1",
33  "-2",
34  "-3",
35  "-a",
36  "-g",
37  "-j",
38  "-O",
39  "-u",
40  "-u-", // both -u and -u- seem to be accepted
41  "-v",
42  "-w-",
43  nullptr
44 };
45 
47 {
48  "-lm",
49  "-l",
50  "-n",
51  "-o",
52  "-b",
53  "-s",
54  "-t",
55  nullptr
56 };
57 
58 bool as86_cmdlinet::parse(int argc, const char **argv)
59 {
60  PRECONDITION(argc > 0);
61  add_arg(argv[0]);
62 
63  for(int i=1; i<argc; i++)
64  {
65  std::string argv_i=argv[i];
66 
67  // file?
68  if(argv_i=="-" || !has_prefix(argv_i, "-"))
69  {
70  add_infile_arg(argv_i);
71  continue;
72  }
73 
74  bool found=false;
75 
76  // separated only, and also allow concatenation with "="
77  for(const char **o=goto_as86_options_with_argument;
78  *o!=nullptr && !found;
79  ++o)
80  {
81  std::string os(*o);
82 
83  if(argv_i==os) // separated
84  {
85  found=true;
86  if(i!=argc-1)
87  {
88  set(argv_i, argv[i+1]);
89  ++i;
90  }
91  else
92  set(argv_i, "");
93  }
94  else if(has_prefix(argv_i, os+"=")) // concatenated with "="
95  {
96  found=true;
97  set(os, argv_i.substr(os.size()+1));
98  }
99  }
100 
101  // goto-as86-only command line argument found
102  if(found)
103  continue;
104 
105  // add to new_argv
106  add_arg(argv_i);
107 
108  // without argument; also store in cmdlinet
109  if(in_list(argv_i.c_str(), as86_options_without_argument))
110  {
111  set(argv_i);
112  continue;
113  }
114 
115  for(const char **o=as86_options_with_argument;
116  *o!=nullptr && !found;
117  ++o)
118  {
119  std::string os(*o);
120 
121  if(argv_i==os) // separated
122  {
123  found=true;
124  if(i!=argc-1)
125  {
126  set(argv_i, argv[i+1]);
127  add_arg(argv[i+1]);
128  ++i;
129  }
130  else
131  set(argv_i, "");
132  }
133  else if(has_prefix(argv_i, os))
134  {
135  found=true;
136  set(os, argv[i]+os.size());
137  }
138  }
139 
140  if(!found)
141  {
142  // unrecognized option
143  std::cerr << "Warning: uninterpreted as86 option '" << argv_i
144  << "'\n";
145  }
146  }
147 
148  return false;
149 }
const char * as86_options_without_argument[]
const char * goto_as86_options_with_argument[]
const char * as86_options_with_argument[]
A special command line object for as86 (of Bruce's C Compiler) Author: Michael Tautschnig Date: July ...
void set(const std::string &opt, const char *value) override
Set option option to value.
void add_infile_arg(const std::string &arg)
static bool in_list(const char *option, const char **list)
virtual bool parse(int argc, const char **argv, const char *optstring)
Parses a commandline according to a specification given in optstring.
Definition: cmdline.cpp:153
void add_arg(const std::string &arg)
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
#define PRECONDITION(CONDITION)
Definition: invariant.h:463