This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetLongOpt.cpp
executable file
·280 lines (245 loc) · 6.69 KB
/
GetLongOpt.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* S Manoharan. Advanced Computer Research Institute. Lyon. France */
#include "GetLongOpt.hpp"
#include "VerdeMessage.hpp"
GetLongOpt::GetLongOpt(const char optmark)
{
table = last = 0;
ustring = "[valid options and arguments]";
enroll_done = 0;
optmarker = optmark;
}
GetLongOpt::~GetLongOpt()
{
Cell *t = table;
while ( t ) {
Cell *tmp = t;
t = t->next;
delete tmp;
}
}
char *
GetLongOpt::basename(char * const prog_name) const
{
char *s;
s = strrchr(prog_name, '/');
if ( s == 0 ) s = prog_name;
else ++s;
return s;
}
int
GetLongOpt::enroll(const char * const opt, const OptType t,
const char * const desc, const char * const val)
{
if ( enroll_done ) return 0;
Cell *c = new Cell;
c->option = opt;
c->type = t;
c->description = desc ? desc : "no description available";
c->value = val;
c->wasSet = 0;
c->next = 0;
if ( last == 0 ) {
table = last = c;
}
else {
last->next = c;
last = c;
}
return 1;
}
const char *
GetLongOpt::retrieve(const char * const opt) const
{
Cell *t;
for ( t = table; t != 0; t = t->next ) {
if ( strcmp(opt, t->option) == 0 )
return t->value;
}
PRINT_ERROR("GetLongOpt::retrieve - unenrolled option %c%s\n",
optmarker, opt );
return 0;
}
int
GetLongOpt::parse(int argc, char * const *argv)
{
int optind = 1;
pname = basename(*argv);
enroll_done = 1;
if ( argc-- <= 1 ) return optind;
while ( argc >= 1 ) {
char *token = *++argv; --argc;
if ( token[0] != optmarker || token[1] == optmarker )
break; /* end of options */
++optind;
char *tmptoken = ++token;
while ( *tmptoken && *tmptoken != '=' )
++tmptoken;
/* (tmptoken - token) is now equal to the command line option
length. */
Cell *t;
enum { NoMatch, ExactMatch, PartialMatch } matchStatus = NoMatch;
Cell *pc = 0; // pointer to the partially-matched cell
for ( t = table; t != 0; t = t->next ) {
if ( strncmp(t->option, token, (tmptoken - token)) == 0 ) {
if ( strlen(t->option) == (unsigned int)(tmptoken - token) ) {
/* an exact match found */
int stat = setcell(t, tmptoken, *(argv+1), pname);
if ( stat == -1 ) return -1;
else if ( stat == 1 ) {
++argv; --argc; ++optind;
}
matchStatus = ExactMatch;
break;
}
else {
/* partial match found */
matchStatus = PartialMatch;
pc = t;
}
} /* end if */
} /* end for */
if ( matchStatus == PartialMatch ) {
int stat = setcell(pc, tmptoken, *(argv+1), pname);
if ( stat == -1 ) return -1;
else if ( stat == 1 ) {
++argv; --argc; ++optind;
}
}
else if ( matchStatus == NoMatch ) {
PRINT_ERROR("%s: unrecognized option %c%s\n",
pname, optmarker, strtok(token,"= \n") );
return -1; /* no match */
}
} /* end while */
return optind;
}
int
GetLongOpt::parse(char * const str, char * const p)
{
enroll_done = 1;
char *token = strtok(str, " \t");
const char *name = p ? p : "GetLongOpt";
while ( token ) {
if ( token[0] != optmarker || token[1] == optmarker ) {
PRINT_ERROR("%s: nonoptions not allowed\n", name );
return -1; /* end of options */
}
char *ladtoken = 0; /* lookahead token */
char *tmptoken = ++token;
while ( *tmptoken && *tmptoken != '=' )
++tmptoken;
/* (tmptoken - token) is now equal to the command line option
length. */
Cell *t;
enum { NoMatch, ExactMatch, PartialMatch } matchStatus = NoMatch;
Cell *pc =0; // pointer to the partially-matched cell
for ( t = table; t != 0; t = t->next ) {
if ( strncmp(t->option, token, (tmptoken - token)) == 0 ) {
if ( strlen(t->option) == (unsigned int)(tmptoken - token) ) {
/* an exact match found */
ladtoken = strtok(0, " \t");
int stat = setcell(t, tmptoken, ladtoken, name);
if ( stat == -1 ) return -1;
else if ( stat == 1 ) {
ladtoken = 0;
}
matchStatus = ExactMatch;
break;
}
else {
/* partial match found */
matchStatus = PartialMatch;
pc = t;
}
} /* end if */
} /* end for */
if ( matchStatus == PartialMatch ) {
ladtoken = strtok(0, " \t");
int stat = setcell(pc, tmptoken, ladtoken, name);
if ( stat == -1 ) return -1;
else if ( stat == 1 ) {
ladtoken = 0;
}
}
else if ( matchStatus == NoMatch ) {
PRINT_ERROR("%s: unrecognized option %c%s\n",
name, optmarker, strtok(token,"= \n"));
return -1; /* no match */
}
token = ladtoken ? ladtoken : strtok(0, " \t");
} /* end while */
return 1;
}
/* ----------------------------------------------------------------
GetLongOpt::setcell returns
-1 if there was an error
0 if the nexttoken was not consumed
1 if the nexttoken was consumed
------------------------------------------------------------------- */
int
GetLongOpt::setcell(Cell *c, char *valtoken, char *nexttoken, const char *name)
{
if ( c == 0 ) return -1;
switch ( c->type ) {
case GetLongOpt::Valueless :
if ( *valtoken == '=' ) {
PRINT_ERROR("%s: unsolicited value for flag %c%s\n",
name, optmarker,c->option );
return -1; /* unsolicited value specification */
}
if (!c->wasSet) {
c->value = (c->value) ? 0 : (char *) ~0;
c->wasSet = 1;
}
return 0;
case GetLongOpt::OptionalValue :
if ( *valtoken == '=' ) {
c->value = ++valtoken;
}
else {
if ( nexttoken != 0 && nexttoken[0] != optmarker ) {
c->value = nexttoken;
return 1;
}
}
// explicit return here, just to make sure another if-case isn't
// put in which falls through to the next case (in the absence of
// a break statement)
return 0;
case GetLongOpt::MandatoryValue :
int return_val;
if ( *valtoken == '=' ) {
c->value = ++valtoken;
return_val = 0;
}
else {
if ( nexttoken != 0 && nexttoken[0] != optmarker ) {
c->value = nexttoken;
return_val = 1;
}
else {
PRINT_ERROR("%s: mandatory value for %c%s\n",
name, optmarker, c->option );
return_val = -1; /* mandatory value not specified */
}
}
return return_val;
default :
break;
}
return -1;
}
void GetLongOpt::usage(std::ostream &outfile) const
{
Cell *t;
outfile << "usage: " << pname << " " << ustring << "\n";
for ( t = table; t != 0; t = t->next ) {
outfile << "\t" << optmarker << t->option;
if ( t->type == GetLongOpt::MandatoryValue )
outfile << " <$val>";
else if ( t->type == GetLongOpt::OptionalValue )
outfile << " [$val]";
outfile << " (" << t->description << ")\n";
}
outfile.flush();
}