forked from fristed/BEaST
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ParseArgv.c
488 lines (447 loc) · 13.8 KB
/
ParseArgv.c
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
* ParseArgv.c --
*
* This file contains a procedure that handles table-based
* argv-argc parsing.
*
* Copyright 1990 Regents of the University of California
* Permission to use, copy, modify, and distribute this
* software and its documentation for any purpose and without
* fee is hereby granted, provided that the above copyright
* notice appear in all copies. The University of California
* makes no representations about the suitability of this
* software for any purpose. It is provided "as is" without
* express or implied warranty.
*
*
* This file has been modified to not rely on tcl, tk or X11.
* Based on tkArgv.c from tk2.3 :
*
* Modifications by Peter Neelin (November 27, 1992)
*
* Further modifications has been done by Simon Fristed Eskildsen
* for the implementation of BEaST (2011).
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "basic.h"
#include "ParseArgv.h"
/*
* Default table of argument descriptors. These are normally available
* in every application.
*/
static ArgvInfo defaultTable[] = {
{"-help", ARGV_HELP, (char *) NULL, (char *) NULL,
"Print summary of command-line options and abort"},
{"-version", ARGV_VERSION, (char *) NULL, (char *) NULL,
"Print version number of program and exit"},
{NULL, ARGV_END, (char *) NULL, (char *) NULL,
(char *) NULL}
};
/*
* Forward declarations for procedures defined in this file:
*/
static void PrintUsage _ANSI_ARGS_((ArgvInfo *argTable, int flags));
static void PrintVersion(ArgvInfo *argTable);
/*
* ParseLong
*
* Quick replacement for strtol which eliminates the undesirable property
* of interpreting numbers with leading '0' characters as octal, while
* retaining "0x" as indicating a hexidecimal number.
*/
long int
ParseLong(const char *argPtr, char **endPtr)
{
const char *tmpPtr = argPtr;
/* Skip sign if present.
*/
if (tmpPtr[0] == '+' || tmpPtr[0] == '-')
tmpPtr++;
/* If '0x' or '0X', treat this as hex.
*/
if (tmpPtr[0] == '0' && (tmpPtr[1] == 'x' || tmpPtr[1] == 'X'))
return strtol(argPtr, endPtr, 16);
/* Otherwise, treat it as decimal. Octal is excluded.
*/
return strtol(argPtr, endPtr, 10);
}
/*
*----------------------------------------------------------------------
*
* ParseArgv --
*
* Process an argv array according to a table of expected
* command-line options. See the manual page for more details.
*
* Results:
* The return value is a Boolean value with non-zero indicating an
* error.
* If an error occurs then an error message is printed on stderr.
* Under normal conditions, both *argcPtr and *argv are modified
* to return the arguments that couldn't be processed here (they
* didn't match the option table, or followed an ARGV_REST
* argument).
*
* Side effects:
*
*----------------------------------------------------------------------
*/
int
ParseArgv(argcPtr, argv, argTable, flags)
int *argcPtr; /* Number of arguments in argv. Modified
* to hold # args left in argv at end. */
char **argv; /* Array of arguments. Modified to hold
* those that couldn't be processed here. */
ArgvInfo *argTable; /* Array of option descriptions */
int flags; /* Or'ed combination of various flag bits,
* such as ARGV_NO_DEFAULTS. */
{
register ArgvInfo *infoPtr;
/* Pointer to the current entry in the
* table of argument descriptions. */
ArgvInfo *matchPtr; /* Descriptor that matches current argument. */
char *curArg; /* Current argument */
register char c; /* Second character of current arg (used for
* quick check for matching; use 2nd char.
* because first char. will almost always
* be '-'). */
int srcIndex; /* Location from which to read next argument
* from argv. */
int dstIndex; /* Index into argv to which next unused
* argument should be copied (never greater
* than srcIndex). */
int argc; /* # arguments in argv still to process. */
int length; /* Number of characters in current argument. */
int nargs; /* Number of following arguments to get. */
int i;
/* Macro to optionally print errors */
#define FPRINTF if (!(flags&ARGV_NO_PRINT)) (void) fprintf
if (flags & ARGV_DONT_SKIP_FIRST_ARG) {
srcIndex = dstIndex = 0;
argc = *argcPtr;
} else {
srcIndex = dstIndex = 1;
argc = *argcPtr-1;
}
while (argc > 0) {
curArg = argv[srcIndex];
srcIndex++;
argc--;
c = curArg[1];
length = strlen(curArg);
/*
* Loop throught the argument descriptors searching for one with
* the matching key string. If found, leave a pointer to it in
* matchPtr.
*/
matchPtr = NULL;
for (i = 0; i < 2; i++) {
if (i == 0) {
infoPtr = argTable;
} else {
infoPtr = defaultTable;
}
for (; infoPtr->type != ARGV_END; infoPtr++) {
if (infoPtr->key == NULL) {
continue;
}
if ((infoPtr->key[1] != c)
|| (strncmp(infoPtr->key, curArg, length) != 0)) {
continue;
}
if (infoPtr->key[length] == 0) {
matchPtr = infoPtr;
goto gotMatch;
}
if (flags & ARGV_NO_ABBREV) {
continue;
}
if (matchPtr != NULL) {
FPRINTF(stderr, "ambiguous option \"%s\"\n", curArg);
return TRUE;
}
matchPtr = infoPtr;
}
}
if (matchPtr == NULL) {
/*
* Unrecognized argument. Just copy it down, unless the caller
* prefers an error to be registered.
*/
if (flags & ARGV_NO_LEFTOVERS) {
FPRINTF(stderr, "unrecognized argument \"%s\"\n", curArg);
}
argv[dstIndex] = curArg;
dstIndex++;
continue;
}
/*
* Take the appropriate action based on the option type
*/
gotMatch:
infoPtr = matchPtr;
switch (infoPtr->type) {
case ARGV_CONSTANT:
*((int *) infoPtr->dst) = (intptr_t) infoPtr->src;
break;
case ARGV_INT:
nargs = (intptr_t) infoPtr->src;
if (nargs<1) nargs=1;
for (i=0; i<nargs; i++) {
if (argc == 0) {
goto missingArg;
} else {
char *endPtr;
*(((int *) infoPtr->dst)+i) =
ParseLong(argv[srcIndex], &endPtr);
if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) {
FPRINTF(stderr,
"expected integer argument for \"%s\" but got \"%s\"",
infoPtr->key, argv[srcIndex]);
return TRUE;
}
srcIndex++;
argc--;
}
}
break;
case ARGV_LONG:
nargs = (intptr_t) infoPtr->src;
if (nargs<1) nargs=1;
for (i=0; i<nargs; i++) {
if (argc == 0) {
goto missingArg;
} else {
char *endPtr;
*(((long *) infoPtr->dst)+i) =
ParseLong(argv[srcIndex], &endPtr);
if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) {
FPRINTF(stderr,
"expected integer argument for \"%s\" but got \"%s\"",
infoPtr->key, argv[srcIndex]);
return TRUE;
}
srcIndex++;
argc--;
}
}
break;
case ARGV_STRING:
nargs = (intptr_t) infoPtr->src;
if (nargs<1) nargs=1;
for (i=0; i<nargs; i++) {
if (argc == 0) {
goto missingArg;
} else {
*(((char **)infoPtr->dst)+i) = argv[srcIndex];
srcIndex++;
argc--;
}
}
break;
case ARGV_REST:
*((int *) infoPtr->dst) = dstIndex;
goto argsDone;
case ARGV_FLOAT:
nargs = (intptr_t) infoPtr->src;
if (nargs<1) nargs=1;
for (i=0; i<nargs; i++) {
if (argc == 0) {
goto missingArg;
} else {
char *endPtr;
*(((double *) infoPtr->dst)+i) =
strtod(argv[srcIndex], &endPtr);
if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) {
FPRINTF(stderr,
"expected floating-point argument for \"%s\" but got\"%s\"\n",
infoPtr->key, argv[srcIndex]);
return TRUE;
}
srcIndex++;
argc--;
}
}
break;
case ARGV_FUNC: {
int (*handlerProc)();
handlerProc = (int (*)())infoPtr->src;
if ((*handlerProc)(infoPtr->dst, infoPtr->key,
argv[srcIndex])) {
srcIndex += 1;
argc -= 1;
}
break;
}
case ARGV_GENFUNC: {
int (*handlerProc)();
handlerProc = (int (*)())infoPtr->src;
argc = (*handlerProc)(infoPtr->dst, infoPtr->key,
argc, argv+srcIndex);
if (argc < 0) {
return TRUE;
}
break;
}
case ARGV_HELP:
PrintUsage (argTable, flags);
return TRUE;
case ARGV_VERSION:
PrintVersion(argTable);
return FALSE;
default:
FPRINTF(stderr, "bad argument type %d in ArgvInfo",
infoPtr->type);
return TRUE;
}
}
/*
* If we broke out of the loop because of an OPT_REST argument,
* copy the remaining arguments down.
*/
argsDone:
while (argc) {
argv[dstIndex] = argv[srcIndex];
srcIndex++;
dstIndex++;
argc--;
}
argv[dstIndex] = (char *) NULL;
*argcPtr = dstIndex;
return FALSE;
missingArg:
FPRINTF(stderr, "\"%s\" option requires an additional argument\n", curArg);
return TRUE;
}
/*
*----------------------------------------------------------------------
*
* PrintUsage --
*
* Generate a help string describing command-line options.
*
* Results:
* Prints on stderr (unless ARGV_NO_PRINT is specified in flags)
* a help string describing all the options in argTable, plus all those
* in the default table unless ARGV_NO_DEFAULTS is
* specified in flags.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
PrintUsage(argTable, flags)
ArgvInfo *argTable; /* Array of command-specific argument
* descriptions. */
int flags; /* If the ARGV_NO_DEFAULTS bit is set
* in this word, then don't generate
* information for default options. */
{
register ArgvInfo *infoPtr;
int width, i, j, numSpaces;
#define NUM_SPACES 20
static char spaces[] = " ";
/* char tmp[30]; */
int nargs;
/* Macro to optionally print errors */
#define FPRINTF if (!(flags&ARGV_NO_PRINT)) (void) fprintf
/*
* First, compute the width of the widest option key, so that we
* can make everything line up.
*/
width = 4;
for (i = 0; i < 2; i++) {
for (infoPtr = i ? defaultTable : argTable;
infoPtr->type != ARGV_END; infoPtr++) {
int length;
if (infoPtr->key == NULL) {
continue;
}
length = strlen(infoPtr->key);
if (length > width) {
width = length;
}
}
}
FPRINTF(stderr, "Command-specific options:");
for (i = 0; ; i++) {
for (infoPtr = i ? defaultTable : argTable;
infoPtr->type != ARGV_END; infoPtr++) {
if (infoPtr->type == ARGV_VERINFO) {
continue;
}
if ((infoPtr->type == ARGV_HELP) && (infoPtr->key == NULL)) {
FPRINTF(stderr, "\n%s", infoPtr->help);
continue;
}
FPRINTF(stderr, "\n %s:", infoPtr->key);
numSpaces = width + 1 - strlen(infoPtr->key);
while (numSpaces > 0) {
if (numSpaces >= NUM_SPACES) {
FPRINTF(stderr, "%s",spaces);
} else {
FPRINTF(stderr, "%s",spaces+NUM_SPACES-numSpaces);
}
numSpaces -= NUM_SPACES;
}
FPRINTF(stderr, "%s",infoPtr->help);
switch (infoPtr->type) {
case ARGV_INT: {
FPRINTF(stderr, "\n\t\tDefault value:");
nargs = (intptr_t) infoPtr->src;
if (nargs<1) nargs=1;
for (j=0; j<nargs; j++) {
FPRINTF(stderr, " %d", *(((int *) infoPtr->dst)+j));
}
break;
}
case ARGV_FLOAT: {
FPRINTF(stderr, "\n\t\tDefault value:");
nargs = (intptr_t) infoPtr->src;
if (nargs<1) nargs=1;
for (j=0; j<nargs; j++) {
FPRINTF(stderr, " %g", *(((double *) infoPtr->dst)+j));
}
break;
}
case ARGV_STRING: {
char *string;
nargs = (intptr_t) infoPtr->src;
if (nargs<1) nargs=1;
string = *((char **) infoPtr->dst);
if ((nargs==1) && (string == NULL)) break;
for (j=0; j<nargs; j++) {
string = *(((char **) infoPtr->dst)+j);
if (string != NULL) {
FPRINTF(stderr, " \"%s\"", string);
}
else {
FPRINTF(stderr, " \"%s\"", string);
}
}
break;
}
default: {
break;
}
}
}
if ((flags & ARGV_NO_DEFAULTS) || (i > 0)) {
break;
}
FPRINTF(stderr, "\nGeneric options for all commands:");
}
FPRINTF(stderr, "\n");
}
static void PrintVersion(ArgvInfo *argTable)
{
printf("PrintVersion not implemented\n");
exit(0);
}