-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparse_command_options.c
More file actions
597 lines (521 loc) · 18.6 KB
/
parse_command_options.c
File metadata and controls
597 lines (521 loc) · 18.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
590
591
592
593
594
595
596
597
#include <string.h>
#include <stdio.h>
#ifndef __APPLE__
#include <malloc.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include "cli-sub.h"
#include "err_ref.h"
/*
* Revision history
* ----------------
* 1/21/13 -jj
* -- Allocation an extra string pointer, which is set to '0', when making
* -- the "struct word_list" for a OP_TYPE_LAST flag.
* 1/19/13 -jj
* -- Add a new option type OP_TYPE_LAST for options that mean everything
* -- else on the command line should be aggregated together.
* 12/15/12 -jj
* -- The original code didn't require a "-" prefix on flags, but that's too
* -- loose for this application.
* -- Make clean-up code parse the given value for "OP_TYPE_FLAG" options
* -- and set the "parsed" field. The value stored is an "int" that's
* -- set by the value of the "flags" field if the option was seen. And
* -- if not, the default string is converted from char to int.
* -- Also, change the code to allow for "--" style options and "--no-" too.
* 12/11/12 -jj
* -- Pulled from the "junction-ac" code with minor changes made.
*
* Todo
* ----
*/
/* --- */
struct word_chain *parse_cgi_options( int *rc, struct option_set *plist,
int nopt, char *cgi_data)
{
int op, match, opseq = 0;
char *st = 0, *en = 0, *sep = 0, *vname = 0, *vvalue = 0, *source = 0;
struct option_set *curr = 0;
struct word_chain *invlist = 0, *endlist = 0;
struct value_chain *chain = 0;
/* --- */
if( cgi_data)
{
source = strdup( cgi_data);
if( source) *rc = RC_NORMAL;
else *rc = ERR_MALLOC_FAILED;
}
else *rc = ERR_BAD_PARMS;
for( op = 0; op < nopt; op++)
{
curr = plist + op;
curr->val = 0;
curr->flags &= ~(OP_FL_FOUND | OP_FL_SET | OP_FL_INVALID);
if( curr->flags & OP_FL_REPEATS && (curr->type == OP_TYPE_FLAG || curr->type == OP_TYPE_LAST))
curr->flags &= ~OP_FL_REPEATS;
curr->parsed = 0;
curr->opt_num = 0;
}
/* --- */
if( *rc == RC_NORMAL) for( st = source; *st && *rc == RC_NORMAL; )
{
vname = st;
en = index( st, CGI_OPT_DELIM);
if( en)
{
*en = EOS_CH;
st = en + 1;
}
else for( ; *st; st++) ;
if( *vname)
{
vvalue = 0;
opseq++;
sep = index( vname, CGI_OPT_EQUAL);
/* If there's just a variable but no value, there's nothing to
* do. For a CGI form just "name=" doesn't mean the user had a
* particular preference for the value. It just means it was
* defined as a form field.
*/
if( sep)
{
*sep = EOS_CH;
vvalue = decode_url_str( sep + 1);
if( !vvalue) *rc = ERR_MALLOC_FAILED;
else for( match = 0, op = 0; op < nopt && !match; op++)
{
if( !strcmp( vname, plist[ op].name))
{
match = 1;
plist[ op].flags |= OP_FL_FOUND;
if( is_option_set( vvalue)) plist[ op].flags |= OP_FL_SET;
else plist[ op].flags &= ~OP_FL_SET;
if( plist[ op].type != OP_TYPE_FLAG)
{
plist[ op].val = vvalue;
if( plist[ op].flags & OP_FL_REPEATS)
{
curr = plist + op;
chain = add_option_to_chain( (struct value_chain *) curr->parsed, opseq,
curr->flags, curr->val, 0);
if( !chain) *rc = ERR_MALLOC_FAILED;
else curr->parsed = (void *) chain;
}
}
else free( vvalue);
plist[ op].opt_num = opseq;
}
}
}
if( *rc == RC_NORMAL) if( !sep || !match)
{
endlist = add_to_word_chain( endlist, vname);
if( !endlist) *rc = ERR_MALLOC_FAILED;
else if( !invlist) invlist = endlist;
if( vvalue) if( *vvalue)
{
endlist = add_to_word_chain( endlist, vvalue);
if( !endlist) *rc = ERR_MALLOC_FAILED;
}
}
}
}
/* --- */
*rc = process_parsed_command_options( plist, nopt, 0, 0);
/* --- */
return( invlist);
}
/* --- */
struct word_chain *parse_command_options( int *rc, struct option_set *plist,
int nopt, int narg, char **opts)
{
int match, ii, op, efp_pr_len, efp_ne_len, is_flag;
char *st = 0, *first = 0, *dup_opt = 0;
struct value_chain *chain = 0;
struct option_set *curr = 0, *co = 0;
struct word_chain *invlist = 0, *inv = 0, *endlist = 0;
*rc = RC_NORMAL;
efp_pr_len = strlen( EXT_FLAG_PREFIX);
efp_ne_len = strlen( EXT_FLAG_NEGATE);
/* --- */
for( op = 0; op < nopt; op++)
{
curr = plist + op;
curr->val = 0;
curr->flags &= ~(OP_FL_FOUND | OP_FL_SET | OP_FL_INVALID);
if( curr->flags & OP_FL_REPEATS && (curr->type == OP_TYPE_FLAG || curr->type == OP_TYPE_LAST))
curr->flags &= ~OP_FL_REPEATS;
curr->parsed = 0;
curr->opt_num = 0;
}
/* ---
* Options, options, options...
*/
for( ii = 1; ii < narg && *rc == RC_NORMAL; ii++)
{
is_flag = 1;
match = 0;
st = first = opts[ ii];
if( !strncmp( st, EXT_FLAG_NEGATE, efp_ne_len)) st += efp_ne_len;
else if( !strncmp( st, EXT_FLAG_PREFIX, efp_pr_len)) st += efp_pr_len;
else if( *st == '-') st++;
else if( *st == '+') st++;
else is_flag = 0;
/* fprintf( stderr, "++DBG: first(%c) opt(%s) nopt(%d)\n", *first, PSSP( st), nopt); */
if( is_flag) for( match = 0, op = 0; op < nopt && !match && *rc == RC_NORMAL; op++)
{
/* fprintf( stderr, "++DBG: ii=%d, op=%d, compare (%s) (%s)\n", ii, op, PSSP( st),
PSSP( plist[ op].name)); */
if( !strcmp( st, plist[ op].name))
{
/* fprintf( stderr, "++DBG: ii=%d, op=%d, matched.\n", ii, op); */
match = 1;
plist[ op].flags |= OP_FL_FOUND;
if( *first == '+' || !strncmp(first, EXT_FLAG_NEGATE, efp_ne_len)) plist[ op].flags &= ~OP_FL_SET;
else plist[ op].flags |= OP_FL_SET;
if( plist[ op].type != OP_TYPE_FLAG) if( ++ii < narg)
{
plist[ op].val = strdup( opts[ ii]);
if( !plist[ op].val) *rc = ERR_MALLOC_FAILED;
else if( plist[ op].flags & OP_FL_REPEATS)
{
co = plist + op;
chain = add_option_to_chain( (struct value_chain *) co->parsed, ii, co->flags, co->val, 0);
if( !chain) *rc = ERR_MALLOC_FAILED;
else co->parsed = (void *) chain;
}
}
plist[ op].opt_num = ii;
if( plist[ op].type == OP_TYPE_LAST) ii = narg;
}
}
if( !match && *rc == RC_NORMAL)
{
inv = (struct word_chain *) malloc( (sizeof *inv));
dup_opt = strdup( opts[ ii]);
if( !inv || !dup_opt) *rc = ERR_MALLOC_FAILED;
else
{
if( !endlist) invlist = endlist = inv;
else endlist->next = inv;
inv->next = 0;
inv->opt = dup_opt;
endlist = inv;
}
}
}
*rc = process_parsed_command_options( plist, nopt, narg, opts);
/* --- */
return invlist;
}
/* --- */
int process_parsed_command_options( struct option_set *plist, int nopt,
int narg, char **opts)
{
int op, *int_val = 0, nconv, ii, nwords, off, rc = RC_NORMAL;
char *invalid = 0, *copy = 0;
float *fl_val = 0;
struct word_list *dup_opts = 0, *wlist;
struct option_set *curr = 0;
struct value_chain *chain = 0;
for( op = 0; op < nopt && rc == RC_NORMAL; op++)
{
curr = plist + op;
if( !curr->val) curr->val = strdup( curr->def);
if( !curr->val) rc = ERR_MALLOC_FAILED;
else if( curr->type == OP_TYPE_FLAG)
{
if( int_val) free( int_val);
int_val = (int *) malloc( sizeof *int_val);
if( !int_val) rc = ERR_MALLOC_FAILED;
else
{
if( curr->opt_num) *int_val = curr->flags & OP_FL_SET;
else
{
*int_val = strtol( curr->val, &invalid, 10);
if( *invalid)
{
curr->flags |= OP_FL_INVALID;
*int_val = 0;
}
else if( *int_val) curr->flags |= OP_FL_SET;
}
curr->parsed = (void *) int_val;
int_val = 0;
}
}
else if( curr->flags & OP_FL_REPEATS)
{
chain = (struct value_chain *) curr->parsed;
for( ; chain && rc == RC_NORMAL; chain = chain->next)
{
if( !curr->val) curr->val = strdup( curr->def);
if( !curr->val) rc = ERR_MALLOC_FAILED;
else if( curr->type == OP_TYPE_INT)
{
if( int_val) free( int_val);
int_val = (int *) malloc( sizeof *int_val);
if( !int_val) rc = ERR_MALLOC_FAILED;
else
{
*int_val = strtol( chain->val, &invalid, 10);
if( *invalid)
{
curr->flags |= OP_FL_INVALID;
chain->flags |= OP_FL_INVALID;
*int_val = 0;
}
chain->parsed = (void *) int_val;
int_val = 0;
if( !curr->parsed)
{
if( int_val) free( int_val);
int_val = (int *) malloc( sizeof *int_val);
if( !int_val) rc = ERR_MALLOC_FAILED;
else
{
curr->parsed = (void *) int_val;
int_val = 0;
}
}
}
}
else if( curr->type == OP_TYPE_FLOAT)
{
if( fl_val) free( fl_val);
fl_val = (float *) malloc( sizeof *fl_val);
if( !fl_val) rc = ERR_MALLOC_FAILED;
else
{
nconv = sscanf( chain->val, "%f", fl_val);
if( nconv != 1)
{
curr->flags |= OP_FL_INVALID;
chain->flags |= OP_FL_INVALID;
*fl_val = 0.0;
}
if( !curr->parsed) curr->parsed = (void *) fl_val;
chain->parsed = (void *) fl_val;
fl_val = 0;
if( !curr->parsed)
{
if( fl_val) free( fl_val);
fl_val = (float *) malloc( sizeof *fl_val);
if( !fl_val) rc = ERR_MALLOC_FAILED;
else
{
curr->parsed = (void *) fl_val;
fl_val = 0;
}
}
}
}
else if( curr->type == OP_TYPE_CHAR || curr->type == OP_TYPE_VOID)
{
chain->parsed = (void *) strdup( chain->val);
if( !chain->parsed) rc = ERR_MALLOC_FAILED;
else if( !curr->parsed)
{
curr->parsed = (void *) strdup( chain->val);
if( !curr->parsed) rc = ERR_MALLOC_FAILED;
}
}
}
}
else if( curr->type == OP_TYPE_INT)
{
if( int_val) free( int_val);
int_val = (int *) malloc( sizeof *int_val);
if( !int_val) rc = ERR_MALLOC_FAILED;
else
{
*int_val = strtol( curr->val, &invalid, 10);
if( *invalid)
{
curr->flags |= OP_FL_INVALID;
*int_val = 0;
}
curr->parsed = (void *) int_val;
int_val = 0;
}
}
else if( curr->type == OP_TYPE_FLOAT)
{
if( fl_val) free( fl_val);
fl_val = (float *) malloc( sizeof *fl_val);
if( !fl_val) rc = ERR_MALLOC_FAILED;
else
{
nconv = sscanf( curr->val, "%f", fl_val);
if( nconv != 1)
{
curr->flags |= OP_FL_INVALID;
*fl_val = 0.0;
}
curr->parsed = (void *) fl_val;
fl_val = 0;
}
}
else if( curr->type == OP_TYPE_CHAR || curr->type == OP_TYPE_VOID)
{
if( curr->parsed) free( curr->parsed);
curr->parsed = (void *) strdup( curr->val);
if( !curr->parsed) rc = ERR_MALLOC_FAILED;
}
/* The OPT_TYPE_LAST options don't make sense for CGI, so skip if
* "opts" is null. That's how the CGI parser calls this routine.
*/
else if( curr->type == OP_TYPE_LAST && opts)
{
if( dup_opts)
{
if( dup_opts->words) free( dup_opts->words);
dup_opts->words = 0;
free( dup_opts);
}
dup_opts = (struct word_list *) malloc( sizeof *dup_opts);
if( !dup_opts) rc = ERR_MALLOC_FAILED;
else
{
if( curr->flags & OP_FL_FOUND)
{
ii = curr->opt_num;
nwords = narg - ii;
}
else nwords = 1;
dup_opts->count = nwords;
dup_opts->words = (char **) malloc( (nwords + 1) * (sizeof *dup_opts->words));
if( !dup_opts->words) rc = ERR_MALLOC_FAILED;
else if( curr->flags & OP_FL_FOUND)
{
for( off = 0; ii < narg && rc == RC_NORMAL; ii++, off++)
{
copy = strdup( opts[ ii]);
if( copy) dup_opts->words[ off] = copy;
else rc = ERR_MALLOC_FAILED;
}
if( rc == RC_NORMAL) dup_opts->words[ nwords] = 0;
}
else
{
copy = strdup( curr->def);
if( copy)
{
dup_opts->words[ 0] = copy;
dup_opts->words[ 1] = 0;
}
else rc = ERR_MALLOC_FAILED;
}
}
if( rc == RC_NORMAL)
{
if( curr->parsed)
{
wlist = (struct word_list *) curr->parsed;
if( wlist->words)
{
free( wlist->words);
wlist->words = 0;
}
free( wlist);
wlist = 0;
}
curr->parsed = (void *) dup_opts;
dup_opts = 0;
}
}
}
/* --- */
if( int_val) free( int_val);
if( fl_val) free( fl_val);
return( rc);
}
/* --- */
struct option_set *get_matching_option( int flag_num, struct option_set *opset, int nflags)
{
struct option_set *walk, *match = 0;
int off;
for( off= 0, walk = opset; off < nflags; off++, walk++)
{
if( walk->num == flag_num)
{
if( !match) match = walk;
else if( walk->opt_num > match->opt_num) match = walk;
}
}
return( match);
}
/* --- */
void print_parse_summary( struct word_chain *extras, struct option_set *opset, int nflags)
{
int off, word_off, *int_p = 0;
struct option_set *co = 0;
struct word_chain *walk = 0;
struct word_list *wlist = 0;
if( extras)
{
fprintf( stderr, "Extras '%s", extras->opt);
for( walk = extras; walk; walk = walk->next)
fprintf( stderr, " %s", walk->opt);
fprintf( stderr, "'\n");
}
else fprintf( stderr, "No extraneous options.\n");
/* Print out all options settings, includes defaults for unspecified options */
fprintf( stderr, "Seq Num Typ Fl Opt\n");
for( off= 0; off < nflags; off++)
{
co = opset + off;
fprintf( stderr, "%2d. %3d %3d %2x ", off + 1, co->num, co->type, co->flags);
fprintf( stderr, "%3d ", co->opt_num);
if( co->type == OP_TYPE_INT || co->type == OP_TYPE_FLAG)
{
int_p = (int *) co->parsed;
fprintf( stderr, "%d ", *int_p);
}
else if( co->type == OP_TYPE_CHAR) fprintf( stderr, "(%s) ", (char *) co->parsed);
else if( co->type == OP_TYPE_FLOAT) fprintf( stderr, "%f ", *((float *) co->parsed));
else if( co->type == OP_TYPE_LAST)
{
wlist = (struct word_list *) co->parsed;
fprintf( stderr, "[ %d ", wlist->count);
for( word_off = 0; word_off < wlist->count; word_off++)
fprintf( stderr, " '%s'", wlist->words[ word_off]);
fprintf( stderr, " ] ");
}
else fprintf( stderr, "/?/ ");
fprintf( stderr, "(%s) (%s) ", co->name, co->val);
fprintf( stderr, "\n");
}
return;
}
/* --- */
struct value_chain *add_option_to_chain( struct value_chain *spot, int opt_num, unsigned int flags, char *val, void *parsed)
{
struct value_chain *link = 0;
if( val)
{
link = (struct value_chain *) malloc( sizeof *link);
if( link)
{
link->opt_num = opt_num;
link->flags = flags;
link->val = val;
link->parsed = parsed;
link->next = spot;
}
}
return( link);
}
/* --- */
struct option_set *cond_get_matching_option( int *rc, int which_flag, struct option_set *opset, int nflags)
{
struct option_set *co = 0;
if( *rc == RC_NORMAL)
{
co = get_matching_option( which_flag, opset, nflags);
if( !co) *rc = ERR_OPT_CONFIG;
}
return( co);
}