-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline-arg.c
717 lines (601 loc) · 16.9 KB
/
line-arg.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
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
#include "line-arg.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdarg.h>
#include <stdio.h>
typedef struct lnA_Usage {
char* usage;
struct lnA_Usage* next;
} lnA_Usage;
typedef struct lnA_Param {
char* name;
lnA_ParamCb callback;
struct lnA_Param* next;
} lnA_Param;
typedef struct lnA_Option {
char* sForm; // Short form (i.e -h)
char* lForm; // Long form (i.e --help)
char* desc; // Description text
lnA_OptionCb callback;
struct lnA_Option* next;
} lnA_Option;
typedef struct lnA_Queued {
void
(*callback)( char* str, void* udata );
char* str;
struct lnA_Queued* next;
} lnA_Queued;
typedef struct lnA_Queue {
lnA_Queued* first;
lnA_Queued* last;
} lnA_Queue;
typedef struct lnA_Parser {
lnA_Usage* uList; // List of usage altenratives
lnA_Param* pList; // List of parameter callbacks
lnA_Option* oList; // List of option descriptions
char* eText; // Error message
char* pName; // Program name
char* hText; // Header text provided by user
char* fText; // Footer text provided by user
lnA_Queue* qNow; // Current callback queue
lnA_Usage* uNow; // Current usage being parsed
unsigned uIdx; // Index into usage string
char** argv; // Current argument list
unsigned aIdx; // Index into argument list
void* udata; // User data passed to callbacks.
} lnA_Parser;
lnA_Parser*
lnA_makeParser( char* name, void* udata ) {
lnA_Parser* par = malloc( sizeof(lnA_Parser) );
*par = (lnA_Parser){ 0 };
par->pName = name;
par->udata = udata;
return par;
}
void
lnA_freeParser( lnA_Parser* par ) {
lnA_Usage* uIt = par->uList;
while( uIt ) {
lnA_Usage* tmp = uIt;
uIt = uIt->next;
free( tmp );
}
lnA_Option* oIt = par->oList;
while( oIt ) {
lnA_Option* tmp = oIt;
oIt = oIt->next;
free( tmp );
}
lnA_Param* pIt = par->pList;
while( pIt ) {
lnA_Param* tmp = pIt;
pIt = pIt->next;
free( tmp );
}
if( par->eText )
free( par->eText );
free( par );
}
lnA_Usage*
lnA_addUsage( lnA_Parser* par, char* usage ) {
lnA_Usage* usg = malloc(sizeof(lnA_Usage));
usg->usage = usage;
usg->next = par->uList;
par->uList = usg;
return usg;
}
void
lnA_addParam( lnA_Parser* par, char* name, lnA_ParamCb cb ) {
lnA_Param* prm = malloc(sizeof(lnA_Param));
prm->name = name;
prm->callback = cb;
prm->next = par->pList;
par->pList = prm;
}
void
lnA_addOption( lnA_Parser* par, char* sf, char* lf, char* desc, lnA_OptionCb cb ) {
lnA_Option* opt = malloc(sizeof(lnA_Option));
opt->sForm = sf;
opt->lForm = lf;
opt->desc = desc;
opt->callback = cb;
opt->next = par->oList;
par->oList = opt;
}
void
lnA_setHeader( lnA_Parser* par, char* header ) {
par->hText = header;
}
void
lnA_setFooter( lnA_Parser* par, char* footer ) {
par->fText = footer;
}
void
lnA_printUsage( lnA_Parser* par ) {
printf( "Usage:\n" );
lnA_Usage* uIt = par->uList;
while( uIt ) {
printf( " %s %s\n", par->pName, uIt->usage );
uIt = uIt->next;
}
putchar( '\n' );
if( par->hText )
printf( "%s\n\n", par->hText );
printf( "Options:\n" );
lnA_Option* oIt = par->oList;
while( oIt ) {
if( oIt->sForm && oIt->lForm )
printf( "-%s, --%s\n", oIt->sForm, oIt->lForm );
else
if( oIt->sForm )
printf( "-%s\n", oIt->sForm );
else
if( oIt->lForm )
printf( "--%s\n", oIt->lForm );
unsigned len = strlen( oIt->desc );
unsigned idx = 0;
while( idx < len ) {
printf( " %.*s", lnA_MAX_DESC_WIDTH, &oIt->desc[idx] );
idx += lnA_MAX_DESC_WIDTH;
}
printf( "\n\n" );
oIt = oIt->next;
}
if( par->fText )
printf( "%s\n\n", par->fText );
}
static char*
parseUsage( lnA_Parser* par );
char*
lnA_tryUsage( lnA_Parser* par, lnA_Usage* usg, char** argv ) {
par->uNow = usg;
par->uIdx = 0;
par->argv = argv;
par->aIdx = 0;
return parseUsage( par );
}
#define uPeek( p ) ((p)->uNow->usage[(p)->uIdx])
#define uNext( p ) ((p)->uNow->usage[(p)->uIdx++])
#define uAdv( p ) ((p)->uIdx++)
#define aPeek( p ) ((p)->argv[(p)->aIdx])
#define aAdv( p ) ((p)->aIdx++)
static char*
parseThing( lnA_Parser* par );
static void
queueCallback(
lnA_Parser* par,
void (*cb)( char* str, void* udata ),
char* str
);
static void
queueCallbacks( lnA_Parser* par, lnA_Queue* src );
static void
invokeCallbacks( lnA_Parser* par );
static void
freeCallbacks( lnA_Parser* par );
static lnA_Param*
findParam( lnA_Parser* par, char* name, unsigned len );
static lnA_Option*
findOptionLong( lnA_Parser* par, char* name, unsigned len );
static lnA_Option*
findOptionShort( lnA_Parser* par, char name );
static char*
error( lnA_Parser* par, char* fmt, ... );
static char*
validate( lnA_Parser* par );
static char*
parseUsage( lnA_Parser* par ) {
char* err = validate( par );
if( err )
return err;
lnA_Queue q = { 0 };
par->qNow = &q;
while( uPeek( par ) != '\0' ) {
err = parseThing( par );
if( err )
return err;
}
if( aPeek( par ) ) {
return error( par, "Extra or unmatched word '%s'", aPeek( par ) );
}
invokeCallbacks( par );
freeCallbacks( par );
return NULL;
}
static bool
isOptChr( char* c ) {
if( c[0] == '.' && c[1] == '.' && c[2] == '.' )
return false;
else
return isgraph( *c ) &&
*c != '[' && *c != ']' &&
*c != '{' && *c != '}';
}
static char*
parseLong( lnA_Parser* par ) {
// Find option name end in usage string
int uBrk = 0;
char* uStr = &uPeek( par );
while( isOptChr( &uStr[uBrk] ) && uStr[uBrk] != '=' )
uBrk++;
int uLen = uBrk;
while( isOptChr( &uStr[uLen] ) )
uLen++;
par->uIdx += uLen;
// Find matching option
lnA_Option* opt = findOptionLong( par, uStr, uBrk );
if( !opt )
return error( par, "Missing option info" );
// Make sure the argument is provided and is an option
char* arg = aPeek( par );
if( !arg || arg[0] != '-' || arg[1] != '-' )
return error( par, "Missing --%s option", opt->lForm );
// Find option name end in argument string
int aBrk = 0;
char* aStr = &arg[2];
while( isgraph( aStr[aBrk] ) && aStr[aBrk] != '=' )
aBrk++;
// Make sure the two names match
if( aBrk != uBrk || strncmp( aStr, uStr, aBrk ) )
return error( par, "Missing --%s option", opt->lForm );
// If usage string doesn't show option parameter
// then the argument string shouldn't provide one
if( uStr[uBrk] != '=' && aStr[aBrk] == '=' )
return error( par, "Unexpected argument for --%s option", opt->lForm );
// If an option argument is expected then the
// argument string should provide one
if( uStr[uBrk] == '=' && aStr[aBrk] != '=' )
return error( par, "Missing argument for --%s option", opt->lForm );
// Queue callbacks, will be called only if
// unit completes without errors
if( opt->callback )
queueCallback( par, opt->callback, opt->lForm );
lnA_Param* prm = findParam( par, &uStr[uBrk+1], uLen - uBrk - 1 );
if( prm && prm->callback )
queueCallback( par, prm->callback, &aStr[aBrk+1] );
aAdv( par );
return NULL;
}
static bool
contains( char* str, unsigned len, char chr ) {
for( unsigned i = 0 ; i < len ; i++ ) {
if( str[i] == chr )
return true;
}
return false;
}
static char*
parseShort( lnA_Parser* par ) {
// Allowed flags
char* fStr = &uPeek( par );
int fLen = 0;
while( isOptChr( &fStr[fLen] ) )
fLen++;
par->uIdx += fLen;
// Make sure the argument is provided and is an option
char* arg = aPeek( par );
if( !arg || arg[0] != '-' || arg[1] == '-' || !isgraph( arg[1] ) )
return error( par, "Missing -%.*s flag(s)", fLen, fStr );
char* aChr = &arg[1];
while( *aChr ) {
if( !contains( fStr, fLen, *aChr ) )
return error( par, "Invalid flag '%c' for -%.*s flag(s)", *aChr, fLen, fStr );
aChr++;
}
aChr = &arg[1];
while( *aChr ) {
// Queue option callback if provided
lnA_Option* opt = findOptionShort( par, *aChr );
if( !opt )
return error( par, "Missing option info" );
if( opt->callback )
queueCallback( par, opt->callback, opt->sForm );
aChr++;
}
aAdv( par );
return NULL;
}
static char*
parseParam( lnA_Parser* par ) {
char* uStr = &uPeek( par );
unsigned uLen = 0;
while( isOptChr( &uStr[uLen] ) )
uLen++;
par->uIdx += uLen;
char* arg = aPeek( par );
if( !arg || arg[0] == '-' )
return error( par, "Missing %.*s parameter", uLen, uStr );
lnA_Param* prm = findParam( par, uStr, uLen );
if( prm && prm->callback )
queueCallback( par, prm->callback, arg );
aAdv( par );
return NULL;
}
static void
exitGroup( lnA_Parser* par, char open, char close ) {
while( uPeek( par ) != close ) {
char c = uNext( par );
if( c == open )
exitGroup( par, open, close );
}
// Skip terminating bracket
uAdv( par );
}
static char*
parseGroup( lnA_Parser* par, char open, char close ) {
char* uStart = &uPeek( par ) - 1;
// Replace the current callback queue with
// one local to the current group, this allows
// us to discard local callbacks for a failed
// match while maintaining those for previous
// successful matches
lnA_Queue* oldQ = par->qNow;
lnA_Queue newQ = { 0 };
par->qNow = &newQ;
char* err;
again:
// Parse all words/things in the current alternative
err = NULL;
while( !err && uPeek( par ) != '|' && uPeek( par ) != close ) {
if( uPeek( par ) == '\0' )
return error( par, "Unterminated group" );
err = parseThing( par );
}
// If the match was successful then merge the
// parent and local queues
if( !err ) {
// Skip everything until (and including) the closing bracket
exitGroup( par, open, close );
// Restore the old queue and merge it with the new one
par->qNow = oldQ;
queueCallbacks( par, &newQ );
return NULL;
}
// If match fails then clear the queued callbacks
freeCallbacks( par );
// Skip until '[' or '|'
while( uPeek( par ) != ']' && uPeek( par ) != '|' )
uAdv( par );
// If match fails but a '|' indicates a following
// alternative then try again with the new form
if( uPeek( par ) == '|' ) {
uAdv( par );
goto again;
}
// Restore old callback queue
par->qNow = oldQ;
// Skip the closing bracket
uAdv( par );
// Return error
{
char* uEnd = &uPeek( par ) + 1;
int uLen = uEnd - uStart;
return error( par, "Missing group %.*s", uLen, uStart );
}
}
static char*
parseThing( lnA_Parser* par ) {
// Skip whitespace
while( isspace( uPeek( par ) ) )
uAdv( par );
// Parse unit
unsigned uStart = par->uIdx;
bool parsedOne = false;
char* err = NULL;
again:
switch( uPeek( par ) ) {
case '-':
uAdv( par );
if( uPeek( par ) == '-' ) {
uAdv( par );
err = parseLong( par );
}
else {
err = parseShort( par );
}
break;
case '[':
uAdv( par );
err = parseGroup( par, '[', ']' );
parsedOne = true;
break;
case '{':
uAdv( par );
err = parseGroup( par, '{', '}' );
break;
default:
err = parseParam( par );
break;
}
char* end = &uPeek( par );
if( end[0] == '.' && end[1] == '.' && end[2] == '.' ) {
if( !err ) {
par->uIdx = uStart;
parsedOne = true;
goto again;
}
par->uIdx += 3;
}
// Skip whitespace
while( isspace( uPeek( par ) ) )
uAdv( par );
if( parsedOne )
return NULL;
else
return err;
}
static void
queueCallback(
lnA_Parser* par,
void (*cb)( char* str, void* udata ),
char* str
) {
lnA_Queued* c = malloc(sizeof(lnA_Queued));
c->callback = cb;
c->str = str;
c->next = NULL;
if( par->qNow->last ) {
par->qNow->last->next = c;
par->qNow->last = c;
}
else {
par->qNow->first = c;
par->qNow->last = c;
}
}
static void
queueCallbacks( lnA_Parser* par, lnA_Queue* src ) {
if( par->qNow->last ) {
par->qNow->last->next = src->first;
par->qNow->last = src->last;
}
else {
par->qNow->first = src->first;
par->qNow->last = src->last;
}
}
static void
invokeCallbacks( lnA_Parser* par ) {
lnA_Queued* qIt = par->qNow->first;
while( qIt ) {
qIt->callback( qIt->str, par->udata );
qIt = qIt->next;
}
}
static void
freeCallbacks( lnA_Parser* par ) {
lnA_Queued* qIt = par->qNow->first;
while( qIt ) {
lnA_Queued* tmp = qIt;
qIt = qIt->next;
free( tmp );
}
*par->qNow = (lnA_Queue){ 0 };
}
static lnA_Param*
findParam( lnA_Parser* par, char* name, unsigned len ) {
lnA_Param* pIt = par->pList;
while( pIt ) {
unsigned pLen = strlen( pIt->name );
if( len == pLen && !strncmp( name, pIt->name, len ) )
return pIt;
pIt = pIt->next;
}
return NULL;
}
static lnA_Option*
findOptionLong( lnA_Parser* par, char* name, unsigned len ) {
lnA_Option* oIt = par->oList;
while( oIt ) {
lnA_Option* opt = oIt;
oIt = oIt->next;
if( !opt->lForm )
continue;
unsigned oLen = strlen( opt->lForm );
if( len == oLen && !strncmp( name, opt->lForm, len ) )
return opt;
}
return NULL;
}
static lnA_Option*
findOptionShort( lnA_Parser* par, char name ) {
lnA_Option* oIt = par->oList;
while( oIt ) {
lnA_Option* opt = oIt;
oIt = oIt->next;
if( !opt->sForm )
continue;
if( contains( opt->sForm, strlen(opt->sForm), name ) )
return opt;
opt = opt->next;
}
return NULL;
}
static char*
error( lnA_Parser* par, char* fmt, ... ) {
va_list args;
va_start( args, fmt );
unsigned len = vsnprintf( NULL, 0, fmt, args ) + 1;
va_end( args );
par->eText = realloc( par->eText, len );
va_start( args, fmt );
vsnprintf( par->eText, len, fmt, args );
va_end( args );
return par->eText;
}
static char*
validateRequired( lnA_Parser* par );
static char*
validateOptional( lnA_Parser* par ) {
// Skip whitespace
while( isspace( uPeek( par ) ) )
uAdv( par );
while( uPeek( par ) != ']' ) {
char c = uNext( par );
if( c == '}' || c == '\0' )
return error( par, "Unterminated optional group" );
if( c == '[' ) {
char* err = validateOptional( par );
if( err )
return err;
}
if( c == '{' ) {
char* err = validateRequired( par );
if( err )
return err;
}
}
// Skip terminating bracket
uAdv( par );
return NULL;
}
static char*
validateRequired( lnA_Parser* par ) {
// Skip whitespace
while( isspace( uPeek( par ) ) )
uAdv( par );
while( uPeek( par ) != '}' ) {
char c = uNext( par );
if( c == ']' || c == '\0' )
return error( par, "Unterminated optional group" );
if( c == '[' ) {
char* err = validateOptional( par );
if( err )
return err;
}
if( c == '{' ) {
char* err = validateRequired( par );
if( err )
return err;
}
}
// Skip terminating bracket
uAdv( par );
return NULL;
}
static char*
validate( lnA_Parser* par ) {
unsigned uIdx = par->uIdx;
// Skip whitespace
while( isspace( uPeek( par ) ) )
uAdv( par );
while( uPeek( par ) != '\0' ) {
char c = uNext( par );
if( c == ']' || c == '}' )
return error( par, "Stray bracket" );
if( c == '[' ) {
char* err = validateOptional( par );
if( err )
return err;
}
if( c == '{' ) {
char* err = validateRequired( par );
if( err )
return err;
}
}
par->uIdx = uIdx;
return NULL;
}