-
Notifications
You must be signed in to change notification settings - Fork 0
/
opt_parser.cpp
208 lines (160 loc) · 5.78 KB
/
opt_parser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <cstring>
#include <cassert>
#include "opt_parser.h"
#include "debug.h"
#define TEST 1
#define VAL_SHIFT 150
opt::opt()
: short_opt('\0'),
long_opt(NULL),
type(NO_ARGUMENT),
count(0),
arg(NULL),
arg_name(NULL),
description(NULL) {}
static int make_options(struct opt *&options) {
const int n = 12;
options = new opt[n];
options[0].short_opt = 'a';
options[0].long_opt = "all";
options[1].short_opt = 'b';
options[1].long_opt = "build";
options[2].short_opt = 'f';
options[2].long_opt = "force";
options[3].short_opt = 'g';
options[4].short_opt = 'h';
options[4].long_opt = "help";
options[5].short_opt = 'o';
options[5].long_opt = "output";
options[5].type = REQUIRE_ARGUMENT;
options[6].short_opt = 'v';
options[6].long_opt = "ver";
options[7].long_opt = "debug";
options[7].type = REQUIRE_ARGUMENT;
options[8].long_opt = "cc";
options[8].type = REQUIRE_ARGUMENT;
options[9].long_opt = "cxx";
options[9].type = REQUIRE_ARGUMENT;
options[10].long_opt = "cflags";
options[10].type = REQUIRE_ARGUMENT;
options[11].long_opt = "cxxflags";
options[11].type = REQUIRE_ARGUMENT;
return n;
}
// const options::as_option options::s_as_opts[] = {
// {OT_ALL_SECS, 'a', "all", NULL, true,
// "Overwrite all sections, ascan will only overwrite the "
// "'Dependencies' section on default"},
// {OT_BUILD, 'b', "build", NULL, true,
// "Put binaries to 'build' subdirectory, Option '-b' is not implemented "
// "yet!"},
// {OT_FORCE, 'f', "force", NULL, true, "Force overwrite"},
// {OT_G, 'g', NULL, NULL, true, "add '-g' flag to cflags or cxxflags"},
// {OT_HELP, 'h', "help", NULL, true, "Print help information"},
// {OT_OUTPUT, 'o', "output", "OUTPUT_FILE", false,
// "Output to the specified file rather than 'Makefile'"},
// {OT_VER, 'v', "ver", NULL, true, "Print ascan version"},
// {OT_DEBUG, '\0', "debug", "DEBUG_LEVEL", false,
// "Set debug level: \n\t\t\t- 0: ERROR\n\t\t\t- "
// "1: WARNING\n\t\t\t- 2: INFO\n\t\t\t- 3: DEBUG\n\t\t\t- 4: "
// "MSGDUMP\n\t\t\t- 5: EXCESSIVE"},
// {OT_CC, '\0', "cc", "CC", false,
// "Set c compiler, default: '" CONFIG_DEFAULT_V_CC "'"},
// {OT_CXX, '\0', "cxx", "CXX", false,
// "Set c++ compiler, default: '" CONFIG_DEFAULT_V_CXX "'"},
// {OT_CFLAGS, '\0', "cflags", "CFLAGS", false,
// "Set c compile flags, default: '" CONFIG_DEFAULT_V_CFLAG "'"},
// {OT_CXXFLAGS, '\0', "cxxflags", "CXXFLAGS", false,
// "Set c++ compile flags, default: '" CONFIG_DEFAULT_V_CXXFLAG "'"},
// };
opt_parser::opt_parser() : short_opts(NULL), long_opts(NULL) {}
opt_parser::~opt_parser() {
delete[] short_opts;
delete[] long_opts;
}
int opt_parser::parse(opt *opts, int n, int argc, char *const *argv) {
make_short_opt(opts, n);
make_long_opt(opts, n);
opt **opts_ref = make_ref(opts, n, n + VAL_SHIFT);
opterr = 0; // do NOT print error message
int opt, long_ind;
while ((opt = getopt_long(argc, argv, short_opts, long_opts, &long_ind)) !=
-1) {
if (opt == '?') {
// '?' is returned if there are unrecognized/missing-arguments
// options. Unrecognized options is set to optopt.
// Note: don't need to concern about no-argument options having
// arguments.
} else {
assert(opt <= n + VAL_SHIFT && opts_ref[opt]);
if (opts_ref[opt]->type != NO_ARGUMENT) {
opts_ref[opt]->arg = optarg;
}
++opts_ref[opt]->count;
}
}
delete[] opts_ref;
return optind;
}
void opt_parser::make_short_opt(const opt *opts, int n) {
short_opts = new char[n * 3 + 1];
int los = 0;
for (int i = 0; i < n; ++i) {
if (opts[i].short_opt != '\0') {
short_opts[los++] = opts[i].short_opt;
if (opts[i].type == REQUIRE_ARGUMENT) {
short_opts[los++] = ':';
} else if (opts[i].type == OPTIONAL_ARGUMENT) {
short_opts[los++] = ':';
short_opts[los++] = ':';
}
}
}
short_opts[los] = '\0';
dbg_assert(los < n * 3 + 1);
print_debug("short options: |%s|\n", short_opts);
}
void opt_parser::make_long_opt(const opt *opts, int n) {
long_opts = new struct option[n + 1];
int los = 0;
for (int i = 0; i < n; ++i) {
if (opts[i].long_opt) {
long_opts[los].name = opts[i].long_opt;
long_opts[los].has_arg =
opts[i].type == REQUIRE_ARGUMENT
? required_argument
: (opts[i].type == OPTIONAL_ARGUMENT ? optional_argument
: no_argument);
long_opts[los].flag = NULL;
long_opts[los].val =
opts[i].short_opt ? opts[i].short_opt : VAL_SHIFT + i;
++los;
}
}
long_opts[los] = {NULL, 0, NULL, 0};
print_msgdump("long options size: %d\n", los);
}
opt **opt_parser::make_ref(opt *opts, int n, int max_val) const {
opt **opts_ref = new opt *[max_val + 1];
memset(opts_ref, 0, sizeof(opt *) * (max_val + 1));
for (int i = 0; i < n; ++i) {
if (opts[i].short_opt) {
opts_ref[(int)opts[i].short_opt] = &opts[i];
} else if (opts[i].long_opt) {
opts_ref[i + VAL_SHIFT] = &opts[i];
}
}
return opts_ref;
}
/*==========================================================================*/
#if (TEST)
int debug_level = 5;
int main(int argc, char **argv) {
struct opt *options;
int n = make_options(options);
opt_parser opt;
opt.parse(options, n, argc, argv);
delete[] options;
return 0;
}
#endif