-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoptparser.c
More file actions
487 lines (458 loc) · 13 KB
/
optparser.c
File metadata and controls
487 lines (458 loc) · 13 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
#include "optparser.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
struct option
{
char const* name;
char short_name;
char const* txt;
char const* group_name;
int exist;
char const* possible_arg;
char const* help;
char* buff;
void (*help_printer)(void*);
void* context;
OptionValue value;
struct option* next;
struct option* prev;
};
void display_help(FILE* file, struct option* o)
{
static char buf[80];
if ( o->group_name )
{
fprintf(file, "\n%s:\n", o->group_name);
return ;
}
if ( o->txt )
{
fprintf(file, "\n%s\n", o->txt);
return ;
}
int n = 0;
if ( o->short_name )
{
n += snprintf(buf + n, sizeof buf - n, "-%c ", o->short_name);
}
if ( o->short_name && strlen(o->name) > 1 )
{
n += snprintf(buf + n, sizeof buf - n,"[ --%s ] ", o->name);
}
if ( !o->short_name && strlen(o->name) > 1 )
{
n += snprintf(buf + n, sizeof buf - n, "--%s ", o->name);
}
if ( o->value )
{
n += snprintf(buf + n , sizeof buf - n, "arg ");
if ( o->value->has_default )
{
n += snprintf(buf + n , sizeof buf - n, "(=%s) ",
o->value->ops->display_default?o->value->ops->display_default(o->value):"obj");
}
}
n = fprintf(file, " %-40s", buf);
if ( n > 42 )
{
fprintf(file, "\n %40s", " ");
}
fprintf(file, "%s\n", o->help?o->help:"");
}
OptionCmdChain option_add(char const* option_name, char const* help, void* pvalue);
OptionCmdChain option_group(char const* group_name);
OptionCmdChain option_text(char const* txt);
OptionCmdChain option_more_help(char const* option_name, char const* help, void* pvalue, void(*printer)(void*), void* context);
OptionCmdChain option_help(char const* help);
void option_parse_into(int argc, char const * const*argv, OptionParser* parser);
static struct option* g_options = NULL;
static struct option* next_option(struct option* o)
{
do{
o = o->next;
}while(!o->name);
return o;
}
#define foreach_option( var, head ) for(struct option* var = ((struct option*)(head))->next; var != head; var = var->next)
#define foreach_argument_option( var, head ) for(struct option* var = next_option(((struct option*)(head))); var != head; var = next_option(var))
struct option_cmd_chain g_cmd_chain = {
.add = &option_add,
.parse_into = &option_parse_into,
.text = &option_text,
.group = &option_group,
.more_help = &option_more_help,
.help = &option_help,
};
OptionCmdChain opt_init(char const* desc)
{
g_options = (struct option*)malloc(sizeof *g_options);
bzero(g_options, sizeof *g_options);
g_options->next = g_options->prev = g_options;
g_options->name = desc;
return &g_cmd_chain;
}
OptionCmdChain option_more_help(char const* option_name, char const* help, void* pvalue, void(*printer)(void*), void* context)
{
option_add(option_name, help, pvalue);
struct option* o = g_options->prev;
o->help_printer = printer;
o->context = context;
return &g_cmd_chain;
}
static void insert_option(struct option* opt)
{
opt->prev = g_options->prev;
opt->next = g_options;
g_options->prev->next = opt;
g_options->prev = opt;
}
OptionCmdChain option_help(char const* help)
{
return option_add("help,h", help, NULL);
}
OptionCmdChain option_text(char const* txt)
{
assert(txt);
struct option * opt = (struct option*)malloc(sizeof *opt);
bzero(opt, sizeof *opt);
opt->txt = txt;
insert_option(opt);
return &g_cmd_chain;
}
OptionCmdChain option_group(char const* group_name)
{
assert(group_name);
struct option * opt = (struct option*)malloc(sizeof *opt);
bzero(opt, sizeof *opt);
opt->group_name = group_name;
insert_option(opt);
return &g_cmd_chain;
}
#define PRE(s) (strlen(s) == 1?"-":"--")
OptionCmdChain option_add(char const* option_name, char const* help, void* pvalue)
{
assert(option_name);
struct option * opt = (struct option*)malloc(sizeof *opt);
bzero(opt, sizeof *opt);
if(strlen(option_name) == 1)
{
opt->name = option_name;
opt->short_name = *option_name;
}else
{
opt->buff = strdup(option_name);
char *p = strchr(opt->buff, ',');
if ( p )
{
*p = '\0';
opt->short_name = *(p+1);
}
opt->name = opt->buff;
}
foreach_argument_option(p, g_options)
{
if ( (opt->short_name == p->short_name && opt->short_name != '\0')|| strcmp(opt->name, p->name) == 0 )
{
fprintf(stderr, "%s%s redefine option name or short name\n", PRE(opt->name),opt->name);
exit(1);
}
}
opt->help = help;
if ( pvalue)
{
OptionValue ov = *(OptionValue*)(pvalue);
*(OptionValue*)pvalue = NULL;
opt->value = ov;
}
insert_option(opt);
return &g_cmd_chain;
}
static struct option* find_option(char sh)
{
foreach_argument_option(p, g_options)
{
if ( sh == p->short_name )
return p;
}
return NULL;
}
static struct option* find_option_long(char const* name)
{
foreach_argument_option(p , g_options)
{
if ( strcmp(name, p->name) == 0)
return p;
}
return NULL;
}
void check_option_repeat_or_no_def(struct option* o, char const* arg)
{
if ( o == NULL)
{
fprintf(stderr, "option %s not defined\n", arg);
exit(1);
}
if ( o->exist )
{
fprintf(stderr, "Waring:option %s repeat\n", o->name);
}
}
void check_option_repeat_or_no_def_short(struct option* o, char arg)
{
if ( o == NULL)
{
fprintf(stderr, "option -%c not defined\n", arg);
exit(1);
}
if ( o->exist )
{
fprintf(stderr, "Waring:option -%c repeat\n", arg);
}
}
static int may_be_arg(const char* arg)
{
if ( *arg != '-' )
return 1;
if (strlen(arg) > 2 && *arg == '-' && (isdigit(arg[1]) || arg[1] == '.'))
return 1;
return 0;
}
static int parse_cmd(int argc, char const * const* argv)
{
int i = 1;
while(i < argc)
{
if (may_be_arg(argv[i]))
break;
if ( strlen(argv[i]) == 2 && *argv[i] == '-' && *(argv[i]+1) != '-')
{
char short_name = *(argv[i]+1);
struct option* o = find_option(short_name);
check_option_repeat_or_no_def(o, argv[i]);
o->exist = 1;
if (o->value && i+1 < argc && may_be_arg(argv[i+1]) )
{
o->possible_arg = argv[i+1];
++i;
}
++i;
}else if ( strlen(argv[i]) > 2 && *argv[i] == '-' && *(argv[i]+1) != '-')
{
char const* last = argv[i] + strlen(argv[i]);
struct option* o = NULL;
for(char const* p = argv[i]+1; p < last; ++p)
{
o = find_option(*p);
check_option_repeat_or_no_def_short(o,*p);
o->exist = 1;
if ( o->value )
{
if ( p == last - 1 )
{
if (i + 1< argc && may_be_arg(argv[i+1]))
{
o->possible_arg = argv[i+1];
++i;
}
}else
{
o->possible_arg = p+1;
}
break;
}
}
++i;
}else if ( strlen(argv[i]) > 2 && argv[i][0] == '-' && argv[i][1] == '-' && argv[i][2] != '-')
{
char * name = strdup(argv[i] + 2);
char * p = strchr(name, '=');
char const* possible_arg = NULL;
if ( p )
{
*p = '\0';
possible_arg = argv[i] + 2 + (p - name) + 1;
}
struct option * o = find_option_long(name);
free(name);
check_option_repeat_or_no_def(o , argv[i]);
o->exist = 1;
if ( possible_arg && *possible_arg != '\0' )
{
o->possible_arg = possible_arg;
}else if ( i +1 < argc && may_be_arg(argv[i+1]) )
{
o->possible_arg = argv[i+1];
++i;
}
++i;
}else{
fprintf(stderr, "illegal format option %s\n", argv[i]);
exit(1);
}
}
return i;
}
static void notify_output(int for_helper)
{
foreach_argument_option(o, g_options)
{
if ( for_helper && !o->help_printer) continue;
if ( !for_helper && o->help_printer ) continue;
if ( ! o->value ) continue;
if ( o->exist && o->possible_arg )
{
if(o->value->ops->validate)
{
char const* err_msg = o->value->ops->validate(o->value, o->possible_arg);
if ( err_msg )
{
fprintf(stderr, "option %s%s argument illegal: %s\n", PRE(o->name),o->name, err_msg);
exit(1);
}
}
if ( o->value->pointer )
{
char const* err_msg = o->value->ops->parse(o->value, o->possible_arg);
if ( err_msg )
{
fprintf(stderr, "option %s%s argument illegal: %s\n", PRE(o->name),o->name, err_msg);
exit(1);
}
continue;
}
}
if ( o->value->required )
{
if ( !o->exist && !o->value->has_default)
{
fprintf(stderr, "option %s%s required but missing\n", PRE(o->name),o->name);
exit(1);
}
if ( o->exist && o->value->pointer && o->value->has_default)
{
o->value->ops->store_default(o->value);
continue;
}
if ( ! o->exist )
{
if ( o->value->pointer ) o->value->ops->store_default(o->value);
continue;
}
if (o->exist && !o->value->has_default)
{
fprintf(stderr, "option %s%s need argument but missing\n", PRE(o->name),o->name);
exit(1);
}
}else if ( o->exist )
{
if ( !o->value->has_default )
{
fprintf(stderr, "option %s%s need argument but missing\n", PRE(o->name),o->name);
exit(1);
}else if (o->value->pointer)
{
o->value->ops->store_default(o->value);
}
}
}
}
void option_parse_into(int argc, char const * const* argv, OptionParser* pparser)
{
*pparser = (struct optparser*)malloc(sizeof(**pparser));
(*pparser)->_private = g_options;
int i = parse_cmd(argc, argv);
if ( i == 1 && g_options->next != g_options)
{
opt_print(*pparser);
exit(0);
}
foreach_argument_option(o , g_options)
{
if ( strcmp("help", o->name) == 0 && o->exist)
{
opt_print(*pparser);
exit(0);
}
}
const int for_helper = 1;
notify_output(for_helper);
foreach_argument_option(o , g_options)
{
if( o->exist && o->help_printer )
{
o->help_printer(o->context);
exit(0);
}
}
notify_output(!for_helper);
(*pparser)->argc = argc - i;
(*pparser)->argv = argv + i;
g_options = NULL;
}
int opt_has(OptionParser parser, char const* key)
{
foreach_argument_option(o, parser->_private)
{
if ( strcmp(key, o->name) == 0 )
{
if ( o->exist || (o->value && o->value->has_default) )
return 1;
break;
}
}
return 0;
}
char const* opt_get_arg(OptionParser parser, char const* key)
{
foreach_argument_option(o, parser->_private)
{
if ( o->name && strcmp(key, o->name) == 0 )
{
if ( o->exist || ( o->value && o->value->required ))
{
if (o->possible_arg)
return o->possible_arg;
if (o->value && o->value->has_default)
return o->value->ops->display_default(o->value);
}
break;
}
}
return NULL;
}
void opt_fprint(FILE* file, OptionParser parser)
{
struct option* op = (struct option*)parser->_private;
fprintf(file, "%s:\n", op->name);
foreach_option(o, op)
{
display_help(file, o);
}
}
void opt_print(OptionParser parser)
{
opt_fprint(stdout, parser);
}
static void free_option(struct option* o)
{
if(o->buff)
free(o->buff);
if(o->value)
o->value->ops->free(o->value);
free(o);
}
void opt_free(OptionParser parser)
{
struct option * p = (struct option*)parser->_private;
p = p->next;
while( p != parser->_private )
{
struct option* free_p = p;
p = p->next;
free_option(free_p);
}
free_option((struct option*)parser->_private);
free(parser);
}