Menu

[r3316]: / advanced / lib / util / cmdline_parser.c  Maximize  Restore  History

Download this file

590 lines (455 with data), 15.6 kB

  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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
#define _XOPEN_SOURCE 600 /* Make sure <string.h> has strdup() */
#include "xmlrpc_config.h" /* prereq for mallocvar.h -- defines __inline__ */
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <limits.h>
#include "bool.h"
#include "int.h"
#include "mallocvar.h"
#include "casprintf.h"
#include "getoptx.h"
#include "string_parser.h"
#include "cmdline_parser.h"
#define MAXOPTS 100
struct optionDesc {
const char * name;
enum optiontype type;
bool present;
union {
unsigned int u;
int i;
const char * s;
uint64_t llu;
double d;
} value;
};
struct cmdlineParserCtl {
struct optionDesc * optionDescArray;
unsigned int numOptions;
const char ** argumentArray;
unsigned int numArguments;
};
static struct optionx *
createLongOptsArray(struct optionDesc * const optionDescArray,
unsigned int const numOptions) {
struct optionx * longopts;
MALLOCARRAY(longopts, numOptions+1);
if (longopts != NULL) {
unsigned int i;
for (i = 0; i < numOptions; ++i) {
longopts[i].name = optionDescArray[i].name;
/* If the option takes a value, we say it is optional even
though it never is. That's because if we say it is
mandatory, getopt_long_only() pretends it doesn't even
recognize the option if the user doesn't give a value.
We prefer to generate a meaningful error message when
the user omits a required option value.
*/
longopts[i].has_arg =
optionDescArray[i].type == OPTTYPE_FLAG ?
no_argument : optional_argument;
longopts[i].flag = NULL;
longopts[i].val = i;
}
longopts[numOptions].name = 0;
longopts[numOptions].has_arg = 0;
longopts[numOptions].flag = 0;
longopts[numOptions].val = 0;
}
return longopts;
}
static void
parseInt(enum optiontype const type,
const char * const optarg,
unsigned int * const valueUintP,
int * const valueIntP,
const char ** const errorP) {
if (optarg == NULL)
casprintf(errorP, "Option requires a value");
else if (strlen(optarg) == 0)
casprintf(errorP, "Numeric option value is null string");
else {
char * tailptr;
long const longvalue = strtol(optarg, &tailptr, 10);
if (*tailptr != '\0')
casprintf(errorP, "Non-numeric value "
"for numeric option value: '%s'", optarg);
else if (errno == ERANGE || longvalue > INT_MAX)
casprintf(errorP, "Numeric value out of range: %s", optarg);
else {
if (type == OPTTYPE_UINT) {
if (longvalue < 0)
casprintf(errorP, "Unsigned numeric value is "
"negative: %ld", longvalue);
else {
*errorP = NULL;
*valueUintP = (unsigned int) longvalue;
}
} else {
*errorP = NULL;
*valueIntP = (int) longvalue;
}
}
}
}
static void
parseBinUint(const char * const optarg,
uint64_t * const valueP,
const char ** const errorP) {
if (optarg == NULL)
casprintf(errorP, "Option requires a value");
else if (strlen(optarg) == 0)
casprintf(errorP, "Numeric option value is null string");
else {
const char * error;
interpretBinUint(optarg, valueP, &error);
if (error) {
casprintf(errorP, "Invalid numeric option value '%s'. %s",
optarg, error);
strfree(error);
}
}
}
static void
parseFloat(const char * const optarg,
double * const valueP,
const char ** const errorP) {
if (optarg == NULL)
casprintf(errorP, "Option requires a value");
else if (strlen(optarg) == 0)
casprintf(errorP, "Numeric option value is null string");
else {
char * tailptr;
double const doublevalue = strtod(optarg, &tailptr);
if (*tailptr != '\0')
casprintf(errorP, "Non-numeric value "
"for numeric option value: '%s'", optarg);
else if (errno == ERANGE)
casprintf(errorP, "Numeric value out of range: %s", optarg);
else {
*errorP = NULL;
*valueP = doublevalue;
}
}
}
static void
parseOptionValue(const char * const optarg,
struct optionDesc * const optionP,
const char ** const errorP) {
switch (optionP->type) {
case OPTTYPE_FLAG:
*errorP = NULL;
break;
case OPTTYPE_INT:
case OPTTYPE_UINT:
parseInt(optionP->type, optarg, &optionP->value.u, &optionP->value.i,
errorP);
break;
case OPTTYPE_STRING:
if (optarg == NULL)
casprintf(errorP, "Option requires a value");
else {
*errorP = NULL;
optionP->value.s = strdup(optarg);
}
break;
case OPTTYPE_BINUINT:
parseBinUint(optarg, &optionP->value.llu, errorP);
break;
case OPTTYPE_FLOAT:
parseFloat(optarg, &optionP->value.d, errorP);
break;
}
}
static void
processOption(struct optionDesc * const optionP,
const char * const optarg,
const char ** const errorP) {
const char * error;
parseOptionValue(optarg, optionP, &error);
if (error)
casprintf(errorP, "Error in '%s' option: %s", optionP->name, error);
else
optionP->present = true;
}
static void
extractArguments(struct cmdlineParserCtl * const cpP,
unsigned int const argc,
const char ** const argv) {
cpP->numArguments = argc - getopt_argstart();
MALLOCARRAY(cpP->argumentArray, cpP->numArguments);
if (cpP->argumentArray == NULL) {
fprintf(stderr, "Unable to allocate memory for argument array "
"(%u arguments)\n", cpP->numArguments);
abort();
} else {
unsigned int i;
for (i = 0; i < cpP->numArguments; ++i) {
cpP->argumentArray[i] = strdup(argv[getopt_argstart() + i]);
if (cpP->argumentArray[i] == NULL) {
fprintf(stderr, "Unable to allocate memory for Argument %u\n",
i);
abort();
}
}
}
}
void
cmd_processOptions(cmdlineParser const cpP,
int const argc,
const char ** const argv,
const char ** const errorP) {
struct optionx * longopts;
longopts = createLongOptsArray(cpP->optionDescArray, cpP->numOptions);
if (longopts == NULL)
casprintf(errorP, "Unable to get memory for longopts array");
else {
int endOfOptions;
unsigned int i;
*errorP = NULL;
/* Set up initial assumption: No options present */
for (i = 0; i < cpP->numOptions; ++i)
cpP->optionDescArray[i].present = false;
endOfOptions = false; /* initial value */
while (!endOfOptions && !*errorP) {
int const opterr0 = 0;
/* Don't let getopt_long_only() print an error message */
unsigned int longoptsIndex;
const char * unrecognizedOption;
const char * optarg;
getopt_long_onlyx(argc, (char**) argv, "", longopts,
&longoptsIndex, opterr0,
&endOfOptions, &optarg, &unrecognizedOption);
if (unrecognizedOption)
casprintf(errorP, "Unrecognized option: '%s'",
unrecognizedOption);
else {
if (!endOfOptions)
processOption(&cpP->optionDescArray[longoptsIndex], optarg,
errorP);
}
}
if (!*errorP)
extractArguments(cpP, argc, argv);
free(longopts);
}
}
cmdlineParser
cmd_createOptionParser(void) {
struct cmdlineParserCtl * cpP;
MALLOCVAR(cpP);
if (cpP != NULL) {
struct optionDesc * optionDescArray;
cpP->numOptions = 0;
MALLOCARRAY(optionDescArray, MAXOPTS);
if (optionDescArray == NULL) {
free(cpP);
cpP = NULL;
} else
cpP->optionDescArray = optionDescArray;
}
return cpP;
}
void
cmd_destroyOptionParser(cmdlineParser const cpP) {
unsigned int i;
for (i = 0; i < cpP->numOptions; ++i) {
struct optionDesc const option = cpP->optionDescArray[i];
if (option.type == OPTTYPE_STRING && option.present)
strfree(option.value.s);
strfree(option.name);
}
for (i = 0; i < cpP->numArguments; ++i)
strfree(cpP->argumentArray[i]);
free(cpP->optionDescArray);
free(cpP);
}
void
cmd_defineOption(cmdlineParser const cpP,
const char * const name,
enum optiontype const type) {
if (cpP->numOptions < MAXOPTS) {
cpP->optionDescArray[cpP->numOptions].name = strdup(name);
cpP->optionDescArray[cpP->numOptions].type = type;
++cpP->numOptions;
}
}
static struct optionDesc *
findOptionDesc(struct cmdlineParserCtl * const cpP,
const char * const name) {
struct optionDesc * retval;
unsigned int i;
retval = NULL;
for (i = 0; i < cpP->numOptions && !retval; ++i)
if (strcmp(cpP->optionDescArray[i].name, name) == 0)
retval = &cpP->optionDescArray[i];
return retval;
}
int
cmd_optionIsPresent(cmdlineParser const cpP,
const char * const name) {
struct optionDesc * const optionDescP = findOptionDesc(cpP, name);
bool present;
if (!optionDescP) {
fprintf(stderr, "cmdlineParser called incorrectly. "
"optionIsPresent() called for undefined option '%s'\n",
name);
abort();
} else
present = optionDescP->present;
return present;
}
unsigned int
cmd_getOptionValueUint(cmdlineParser const cpP,
const char * const name) {
struct optionDesc * const optionDescP = findOptionDesc(cpP, name);
unsigned int retval;
if (!optionDescP) {
fprintf(stderr, "cmdlineParser called incorrectly. "
"cmd_getOptionValueUint() called for undefined option '%s'\n",
name);
abort();
} else {
if (optionDescP->type != OPTTYPE_UINT) {
fprintf(stderr, "cmdlineParser called incorrectly. "
"cmd_getOptionValueUint() called for non-unsigned integer "
"option '%s'\n", optionDescP->name);
abort();
} else {
if (optionDescP->present)
retval = optionDescP->value.u;
else
retval = 0;
}
}
return retval;
}
int
cmd_getOptionValueInt(cmdlineParser const cpP,
const char * const name) {
struct optionDesc * const optionDescP = findOptionDesc(cpP, name);
int retval;
if (!optionDescP) {
fprintf(stderr, "cmdlineParser called incorrectly. "
"cmd_getOptionValueInt() called for undefined option '%s'\n",
name);
abort();
} else {
if (optionDescP->type != OPTTYPE_INT) {
fprintf(stderr, "cmdlineParser called incorrectly. "
"cmd_getOptionValueInt() called for non-integer "
"option '%s'\n", optionDescP->name);
abort();
} else {
if (optionDescP->present)
retval = optionDescP->value.i;
else
retval = 0;
}
}
return retval;
}
const char *
cmd_getOptionValueString(cmdlineParser const cpP,
const char * const name) {
struct optionDesc * const optionDescP = findOptionDesc(cpP, name);
const char * retval;
if (!optionDescP) {
fprintf(stderr, "cmdlineParser called incorrectly. "
"cmd_getOptionValueString() called for "
"undefined option '%s'\n",
name);
abort();
} else {
if (optionDescP->type != OPTTYPE_STRING) {
fprintf(stderr, "cmdlineParser called incorrectly. "
"getOptionValueString() called for non-string "
"option '%s'\n", optionDescP->name);
abort();
} else {
if (optionDescP->present) {
retval = strdup(optionDescP->value.s);
if (retval == NULL) {
fprintf(stderr,
"out of memory in cmd_getOptionValueString()\n");
abort();
}
} else
retval = NULL;
}
}
return retval;
}
uint64_t
cmd_getOptionValueBinUint(cmdlineParser const cpP,
const char * const name) {
struct optionDesc * const optionDescP = findOptionDesc(cpP, name);
uint64_t retval;
if (!optionDescP) {
fprintf(stderr, "cmdlineParser called incorrectly. "
"cmd_getOptionValueUint() called for undefined option '%s'\n",
name);
abort();
} else {
if (optionDescP->type != OPTTYPE_BINUINT) {
fprintf(stderr, "cmdlineParser called incorrectly. "
"cmd_getOptionValueBinUint() called for "
"non-OPTTYPE_BINUINT "
"option '%s'\n", optionDescP->name);
abort();
} else {
if (optionDescP->present)
retval = optionDescP->value.llu;
else
retval = 0;
}
}
return retval;
}
double
cmd_getOptionValueFloat(cmdlineParser const cpP,
const char * const name) {
struct optionDesc * const optionDescP = findOptionDesc(cpP, name);
double retval;
if (!optionDescP) {
fprintf(stderr, "cmdlineParser called incorrectly. "
"cmd_getOptionValueInt() called for undefined option '%s'\n",
name);
abort();
} else {
if (optionDescP->type != OPTTYPE_FLOAT) {
fprintf(stderr, "cmdlineParser called incorrectly. "
"cmd_getOptionValueInt() called for non-float "
"option '%s'\n", optionDescP->name);
abort();
} else {
if (optionDescP->present)
retval = optionDescP->value.d;
else
retval = 0.0;
}
}
return retval;
}
unsigned int
cmd_argumentCount(cmdlineParser const cpP) {
return cpP->numArguments;
}
const char *
cmd_getArgument(cmdlineParser const cpP,
unsigned int const argNumber) {
const char * retval;
if (argNumber >= cpP->numArguments)
retval = NULL;
else {
retval = strdup(cpP->argumentArray[argNumber]);
if (retval == NULL) {
fprintf(stderr,
"out of memory in cmd_getArgument()\n");
abort();
}
}
return retval;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.