Repository navigation
Expand file tree
/
Copy pathoptparse.h
More file actions
560 lines (530 loc) · 13.8 KB
/
Copy pathoptparse.h
File metadata and controls
560 lines (530 loc) · 13.8 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
#if !defined(__NNOPTPARSEHEADERFILE_H__)
#define __NNOPTPARSEHEADERFILE_H__
/* Optparse --- portable, reentrant, embeddable, getopt-like option parser
*
* This is free and unencumbered software released into the public domain.
*
* The POSIX getopt() option parser has three fatal flaws. These flaws
* are solved by Optparse.
*
* 1) Parser state is stored entirely in global variables, some of
* which are static and inaccessible. This means only one thread can
* use getopt(). It also means it's not possible to recursively parse
* nested sub-arguments while in the middle of argument parsing.
* Optparse fixes this by storing all state on a local struct.
*
* 2) The POSIX standard provides no way to properly reset the parser.
* This means for portable code that getopt() is only good for one
* run, over one argv with one option string. It also means subcommand
* options cannot be processed with getopt(). Most implementations
* provide a method to reset the parser, but it's not portable.
* Optparse provides an optprs_nextpositional() function for stepping over
* subcommands and continuing parsing of options with another option
* string. The Optparse struct itself can be passed around to
* subcommand handlers for additional subcommand option parsing. A
* full reset can be achieved by with an additional optprs_init().
*
* 3) Error messages are printed to stderr. This can be disabled with
* opterr, but the messages themselves are still inaccessible.
* Optparse solves this by writing an error message in its errmsg
* field. The downside to Optparse is that this error message will
* always be in English rather than the current locale.
*
* Optparse should be familiar with anyone accustomed to getopt(), and
* it could be a nearly drop-in replacement. The option string is the
* same and the fields have the same names as the getopt() global
* variables (optarg, optind, optopt).
*
* Optparse also supports GNU-style long options with optprs_nextlongflag().
* The interface is slightly different and simpler than getopt_long().
*
* By default, argv is permuted as it is parsed, moving non-option
* arguments to the end. This can be disabled by setting the `permute`
* field to 0 after initialization.
*/
#include <stdbool.h>
#define OPTPARSE_MSG_INVALID "invalid option"
#define OPTPARSE_MSG_MISSING "option requires an argument"
#define OPTPARSE_MSG_TOOMANY "option takes no arguments"
enum optargtype_t
{
OPTPARSE_NONE,
OPTPARSE_REQUIRED,
OPTPARSE_OPTIONAL
};
typedef struct optcontext_t optcontext_t;
typedef struct optflag_t optflag_t;
typedef enum optargtype_t optargtype_t;
typedef bool(*optcallback_t)(optcontext_t* ox, optflag_t*, void* userptr);
struct optflag_t
{
const char* longname;
int shortname;
optargtype_t argtype;
const char* helptext;
};
struct optcontext_t
{
char** argv;
int argc;
void* userptr;
int permute;
int optind;
int optopt;
char* optarg;
char errmsg[64];
int subopt;
int restargc;
int knowncount;
optcallback_t callback;
char* restargv[1024];
optflag_t knownflags[1024];
};
/**
* Initializes the parser state.
*/
static void optprs_init(optcontext_t* ox, int argc, char** argv, void* userptr, optcallback_t callback)
{
memset(ox, 0, sizeof(optcontext_t));
ox->userptr = userptr;
ox->argv = argv;
ox->argc = argc;
ox->permute = 1;
ox->optind = argv[0] != 0;
ox->subopt = 0;
ox->optarg = 0;
ox->errmsg[0] = '\0';
ox->restargc = 0;
ox->knowncount = 0;
ox->callback = callback;
}
static int optprs_nextshortflag(optcontext_t* ox, const char* optstring);
static int optprs_makeerror(optcontext_t* ox, const char* msg, const char* data)
{
unsigned p;
const char* sep;
p = 0;
sep = " -- '";
while(*msg)
{
ox->errmsg[p++] = *msg++;
}
while(*sep)
{
ox->errmsg[p++] = *sep++;
}
while(p < sizeof(ox->errmsg) - 2 && *data)
{
ox->errmsg[p++] = *data++;
}
ox->errmsg[p++] = '\'';
ox->errmsg[p++] = '\0';
return '?';
}
static bool optbits_isdashdash(const char* arg)
{
if(arg != NULL)
{
if((arg[0] == '-') && (arg[1] == '-') && (arg[2] == '\0'))
{
return true;
}
}
return false;
}
static bool optbits_isshortopt(const char* arg)
{
if(arg != NULL)
{
if((arg[0] == '-') && (arg[1] != '-') && (arg[1] != '\0'))
{
return true;
}
}
return false;
}
static bool optbits_islongopt(const char* arg)
{
if(arg != NULL)
{
if((arg[0] == '-') && (arg[1] == '-') && (arg[2] != '\0'))
{
return true;
}
}
return false;
}
static void optbits_permute(optcontext_t* ox, int index)
{
int i;
char* nonoption;
nonoption = ox->argv[index];
for(i = index; i < ox->optind - 1; i++)
{
ox->argv[i] = ox->argv[i + 1];
}
ox->argv[ox->optind - 1] = nonoption;
}
static int optbits_getargtype(const char* optstring, char c)
{
int count;
count = OPTPARSE_NONE;
if(c == ':')
{
return -1;
}
for(; *optstring && c != *optstring; optstring++)
{
}
if(!*optstring)
{
return -1;
}
if(optstring[1] == ':')
{
count += optstring[2] == ':' ? 2 : 1;
}
return count;
}
static bool optbits_islongoptsend(const optflag_t* longopts, int i)
{
if(!longopts[i].longname && !longopts[i].shortname)
{
return true;
}
return false;
}
static void optbits_fromlong(const optflag_t* longopts, char* optstring)
{
int i;
int a;
char* p;
p = optstring;
for(i = 0; !optbits_islongoptsend(longopts, i); i++)
{
if(longopts[i].shortname && longopts[i].shortname < 127)
{
*p++ = longopts[i].shortname;
for(a = 0; a < (int)longopts[i].argtype; a++)
{
*p++ = ':';
}
}
}
*p = '\0';
}
/* Unlike strcmp(), handles options containing "=". */
static bool optbits_matchlongopts(const char* longname, const char* option)
{
const char *a;
const char* n;
a = option;
n = longname;
if(longname == 0)
{
return 0;
}
for(; *a && *n && *a != '='; a++, n++)
{
if(*a != *n)
{
return 0;
}
}
return *n == '\0' && (*a == '\0' || *a == '=');
}
/* Return the part after "=", or NULL. */
static char* optbits_getlongoptsarg(char* option)
{
for(; *option && *option != '='; option++)
{
}
if(*option == '=')
{
return option + 1;
}
return NULL;
}
static int optbits_longfallback(optcontext_t* ox, const optflag_t* longopts, int* longindex)
{
int i;
int result;
/* 96 ASCII printable characters */
char optstring[96 * 3 + 1];
optbits_fromlong(longopts, optstring);
result = optprs_nextshortflag(ox, optstring);
if(longindex != 0)
{
*longindex = -1;
if(result != -1)
{
for(i = 0; !optbits_islongoptsend(longopts, i); i++)
{
if(longopts[i].shortname == ox->optopt)
{
*longindex = i;
}
}
}
}
return result;
}
/**
* Read the next option in the argv array.
* @param optstring a getopt()-formatted option string.
* @return the next option character, -1 for done, or '?' for error
*
* Just like getopt(), a character followed by no colons means no
* argument. One colon means the option has a required argument. Two
* colons means the option takes an optional argument.
*/
static int optprs_nextshortflag(optcontext_t* ox, const char* optstring)
{
int r;
int type;
int index;
char* next;
char* option;
char str[2] = { 0, 0 };
option = ox->argv[ox->optind];
ox->errmsg[0] = '\0';
ox->optopt = 0;
ox->optarg = 0;
if(option == 0)
{
return -1;
}
else if(optbits_isdashdash(option))
{
/* consume "--" */
ox->optind++;
return -1;
}
else if(!optbits_isshortopt(option))
{
if(ox->permute)
{
index = ox->optind++;
r = optprs_nextshortflag(ox, optstring);
optbits_permute(ox, index);
ox->optind--;
return r;
}
else
{
return -1;
}
}
option += ox->subopt + 1;
ox->optopt = option[0];
type = optbits_getargtype(optstring, option[0]);
next = ox->argv[ox->optind + 1];
switch(type)
{
case -1:
{
str[1] = 0;
str[0] = option[0];
ox->optind++;
return optprs_makeerror(ox, OPTPARSE_MSG_INVALID, str);
}
break;
case OPTPARSE_NONE:
{
if(option[1])
{
ox->subopt++;
}
else
{
ox->subopt = 0;
ox->optind++;
}
return option[0];
}
break;
case OPTPARSE_REQUIRED:
{
ox->subopt = 0;
ox->optind++;
if(option[1])
{
ox->optarg = option + 1;
}
else if(next != 0)
{
ox->optarg = next;
ox->optind++;
}
else
{
str[1] = 0;
str[0] = option[0];
ox->optarg = 0;
return optprs_makeerror(ox, OPTPARSE_MSG_MISSING, str);
}
return option[0];
}
break;
case OPTPARSE_OPTIONAL:
{
ox->subopt = 0;
ox->optind++;
if(option[1])
{
ox->optarg = option + 1;
}
else
{
ox->optarg = 0;
}
return option[0];
}
break;
}
return 0;
}
/**
* Handles GNU-style long options in addition to getopt() options.
* This works a lot like GNU's getopt_long(). The last option in
* longopts must be all zeros, marking the end of the array. The
* longindex argument may be NULL.
*/
static int optprs_nextlongflag(optcontext_t* ox, const optflag_t* longopts, int* longindex)
{
int i;
int r;
int index;
char* arg;
char* option;
const char* name;
*longindex = 0;
option = ox->argv[ox->optind];
if(option == 0)
{
return -1;
}
else if(optbits_isdashdash(option))
{
ox->optind++; /* consume "--" */
return -1;
}
else if(optbits_isshortopt(option))
{
return optbits_longfallback(ox, longopts, longindex);
}
else if(!optbits_islongopt(option))
{
if(ox->permute)
{
index = ox->optind++;
r = optprs_nextlongflag(ox, longopts, longindex);
optbits_permute(ox, index);
ox->optind--;
return r;
}
else
{
return -1;
}
}
/* Parse as long option. */
ox->errmsg[0] = '\0';
ox->optopt = 0;
ox->optarg = 0;
option += 2; /* skip "--" */
ox->optind++;
for(i = 0; !optbits_islongoptsend(longopts, i); i++)
{
name = longopts[i].longname;
if(optbits_matchlongopts(name, option))
{
if(longindex)
{
*longindex = i;
}
ox->optopt = longopts[i].shortname;
arg = optbits_getlongoptsarg(option);
if(longopts[i].argtype == OPTPARSE_NONE && arg != 0)
{
return optprs_makeerror(ox, OPTPARSE_MSG_TOOMANY, name);
}
if(arg != 0)
{
ox->optarg = arg;
}
else if(longopts[i].argtype == OPTPARSE_REQUIRED)
{
ox->optarg = ox->argv[ox->optind];
if(ox->optarg == 0)
{
return optprs_makeerror(ox, OPTPARSE_MSG_MISSING, name);
}
else
{
ox->optind++;
}
}
return ox->optopt;
}
}
return optprs_makeerror(ox, OPTPARSE_MSG_INVALID, option);
}
/**
* Used for stepping over non-option arguments.
* @return the next non-option argument, or NULL for no more arguments
*
* Argument parsing can continue with optparse() after using this
* function. That would be used to parse the options for the
* subcommand returned by optprs_nextpositional(). This function allows you to
* ignore the value of optind.
*/
static char* optprs_nextpositional(optcontext_t* ox)
{
char* option;
option = ox->argv[ox->optind];
ox->subopt = 0;
if(option != 0)
{
ox->optind++;
}
return option;
}
static bool optprs_add(optcontext_t* ox, const char* longname, int shortname, optargtype_t argtype, const char* helptext)
{
optflag_t flag;
flag.shortname = shortname;
flag.longname = longname;
flag.argtype = argtype;
flag.helptext = helptext,
ox->knownflags[ox->knowncount] = flag;
ox->knowncount++;
return true;
}
static bool optprs_run(optcontext_t* ox)
{
int opt;
int longindex;
char* arg;
optflag_t* flag;
while ((opt = optprs_nextlongflag(ox, ox->knownflags, &longindex)) != -1)
{
flag = &ox->knownflags[longindex];
if(!ox->callback(ox, flag, ox->userptr))
{
break;
}
}
while(true)
{
arg = optprs_nextpositional(ox);
if(arg == NULL)
{
break;
}
ox->restargv[ox->restargc] = arg;
ox->restargc++;
}
return true;
}
#endif