]> mj.ucw.cz Git - libucw.git/blob - ucw/opt.c
Opt: Cosmetics
[libucw.git] / ucw / opt.c
1 /*
2  *      UCW Library -- Parsing of command line options
3  *
4  *      (c) 2013 Jan Moskyto Matejka <mq@ucw.cz>
5  *      (c) 2014 Martin Mares <mj@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #include <ucw/lib.h>
12 #include <ucw/opt.h>
13 #include <ucw/conf.h>
14 #include <ucw/conf-internal.h>
15 #include <ucw/fastbuf.h>
16 #include <ucw/stkstring.h>
17 #include <ucw/strtonum.h>
18
19 #include <alloca.h>
20 #include <math.h>
21
22 /***
23  * Value flags defaults
24  * ~~~~~~~~~~~~~~~~~~~~
25  *
26  * OPT_NO_VALUE for OPT_BOOL, OPT_SWITCH and OPT_INC
27  * OPT_MAYBE_VALUE for OPT_STRING, OPT_UNS, OPT_INT
28  * Some of the value flags (OPT_NO_VALUE, OPT_MAYBE_VALUE, OPT_REQUIRED_VALUE)
29  * must be specified for OPT_CALL and OPT_USER.
30  ***/
31 static uns opt_default_value_flags[] = {
32     [OPT_CL_BOOL] = OPT_NO_VALUE,
33     [OPT_CL_STATIC] = OPT_MAYBE_VALUE,
34     [OPT_CL_SWITCH] = OPT_NO_VALUE,
35     [OPT_CL_INC] = OPT_NO_VALUE,
36     [OPT_CL_CALL] = 0,
37     [OPT_CL_USER] = 0,
38     [OPT_CL_SECTION] = 0,
39     [OPT_CL_HELP] = 0
40 };
41
42 struct opt_precomputed {
43   struct opt_item * item;
44   const char * name;
45   short flags;
46   short count;
47 };
48
49 struct opt_context {
50   struct opt_precomputed * opts;
51   struct opt_precomputed ** shortopt;
52   struct opt_item ** hooks_before_arg;
53   struct opt_item ** hooks_before_value;
54   struct opt_item ** hooks_after_value;
55   short opt_count;
56   short hooks_before_arg_count;
57   short hooks_before_value_count;
58   short hooks_after_value_count;
59   int positional_max;
60   int positional_count;
61 };
62
63 static void opt_failure(const char * mesg, ...) FORMAT_CHECK(printf,1,2) NONRET;
64 static void opt_failure(const char * mesg, ...) {
65   va_list args;
66   va_start(args, mesg);
67   vfprintf(stderr, mesg, args);
68   fprintf(stderr, "\n");
69   opt_usage();
70   exit(OPT_EXIT_BAD_ARGS);
71   va_end(args);         // FIXME: Does this make a sense after exit()?
72 }
73
74 #define FOREACHLINE(text) for (const char * begin = (text), * end = (text); (*end) && (end = strchrnul(begin, '\n')); begin = end+1)
75
76 static inline uns uns_min(uns x, uns y)
77 {
78   return MIN(x, y);
79 }
80
81 void opt_help(const struct opt_section * help) {
82   int sections_cnt = 0;
83   int lines_cnt = 0;
84
85   for (struct opt_item * item = help->opt; item->cls != OPT_CL_END; item++) {
86     if (item->flags & OPT_NO_HELP) continue;
87     if (item->cls == OPT_CL_SECTION) {
88       sections_cnt++;
89       continue;
90     }
91     if (!*(item->help)) {
92       lines_cnt++;
93       continue;
94     }
95     FOREACHLINE(item->help)
96       lines_cnt++;
97   }
98
99   struct opt_sectlist {
100     int pos;
101     struct opt_section * sect;
102   } sections[sections_cnt];
103   int s = 0;
104
105   const char *lines[lines_cnt][3];
106   memset(lines, 0, sizeof(lines));
107   int line = 0;
108
109   int linelengths[3] = { -1, -1, -1 };
110
111   for (struct opt_item * item = help->opt; item->cls != OPT_CL_END; item++) {
112     if (item->flags & OPT_NO_HELP) continue;
113
114     if (item->cls == OPT_CL_HELP) {
115       if (!*(item->help)) {
116         line++;
117         continue;
118       }
119 #define SPLITLINES(text) do { \
120       FOREACHLINE(text) { \
121         int cell = 0; \
122         for (const char * b = begin, * e = begin; (e < end) && (e = strchrnul(b, '\t')) && (e > end ? (e = end) : end); b = e+1) { \
123           lines[line][cell] = b; \
124           if (cell >= 2) \
125             break; \
126           else \
127             if (*e == '\t' && linelengths[cell] < (e - b)) \
128               linelengths[cell] = e-b; \
129           cell++; \
130         } \
131         line++; \
132       } } while (0)
133       SPLITLINES(item->help);
134       continue;
135     }
136
137     if (item->cls == OPT_CL_SECTION) {
138       sections[s++] = (struct opt_sectlist) { .pos = line, .sect = item->u.section };
139       continue;
140     }
141
142     uns valoff = strchrnul(item->help, '\t') - item->help;
143     uns eol = strchrnul(item->help, '\n') - item->help;
144     if (valoff > eol)
145       valoff = eol;
146 #define VAL(it) ((it->flags & OPT_REQUIRED_VALUE) ? stk_printf("=%.*s", valoff, item->help)  : ((it->flags & OPT_NO_VALUE) ? "" : stk_printf("(=%.*s)", valoff, item->help)))
147     if (item->name) {
148       lines[line][1] = stk_printf("--%s%s", item->name, VAL(item));
149       if (linelengths[1] < (int) strlen(lines[line][1]))
150         linelengths[1] = strlen(lines[line][1]);
151       lines[line][0] = "";
152       if (linelengths[0] < 0)
153         linelengths[0] = 0;
154     }
155     if (item->letter) {
156       lines[line][0] = stk_printf("-%c,", item->letter);
157       if (linelengths[0] < (int) strlen(lines[line][0]))
158         linelengths[0] = strlen(lines[line][0]);
159     }
160 #undef VAL
161
162     if (eol > valoff) {
163       lines[line][2] = item->help + valoff + 1;
164     }
165
166     line++;
167
168     if (*(item->help + eol))
169       SPLITLINES(item->help + eol + 1);
170   }
171 #undef SPLITLINES
172
173   s = 0;
174 #define FIELD(k) linelengths[k], uns_min(strchrnul(lines[i][k], '\t') - lines[i][k], strchrnul(lines[i][k], '\n') - lines[i][k]), lines[i][k]
175 #define LASTFIELD(k) uns_min(strchrnul(lines[i][k], '\t') - lines[i][k], strchrnul(lines[i][k], '\n') - lines[i][k]), lines[i][k]
176   for (int i=0;i<line;i++) {
177     while (s < sections_cnt && sections[s].pos == i) {
178       opt_help(sections[s].sect);
179       s++;
180     }
181     if (lines[i][0] == NULL)
182       printf("\n");
183     else if (linelengths[0] == -1 || lines[i][1] == NULL)
184       printf("%.*s\n", LASTFIELD(0));
185     else if (linelengths[1] == -1 || lines[i][2] == NULL)
186       printf("%-*.*s  %.*s\n", FIELD(0), LASTFIELD(1));
187     else
188       printf("%-*.*s  %-*.*s  %.*s\n", FIELD(0), FIELD(1), LASTFIELD(2));
189   }
190   while (s < sections_cnt && sections[s].pos == line) {
191     opt_help(sections[s].sect);
192     s++;
193   }
194 }
195
196 static struct opt_precomputed * opt_find_item_shortopt(struct opt_context * oc, int chr) {
197   struct opt_precomputed * candidate = oc->shortopt[chr];
198   if (!candidate)
199     opt_failure("Invalid option -%c", chr);
200   if (candidate->count++ && (candidate->flags & OPT_SINGLE))
201     opt_failure("Option -%c appeared the second time.", candidate->item->letter);
202   return candidate;
203 }
204
205 static struct opt_precomputed * opt_find_item_longopt(struct opt_context * oc, char * str) {
206   uns len = strlen(str);
207   struct opt_precomputed * candidate = NULL;
208
209   for (int i=0; i<oc->opt_count; i++) {
210     if (!oc->opts[i].name)
211       continue;
212     if (!strncmp(oc->opts[i].name, str, len)) {
213       if (strlen(oc->opts[i].name) == len) {
214         if (oc->opts[i].count++ && (oc->opts[i].flags & OPT_SINGLE))
215           opt_failure("Option %s appeared the second time.", oc->opts[i].name);
216
217         return &oc->opts[i];
218       }
219       if (candidate)
220         opt_failure("Ambiguous prefix %s: Found matching %s and %s.", str, candidate->name, oc->opts[i].name);
221       else
222         candidate = &oc->opts[i];
223     }
224     if (!strncmp("no-", str, 3) && !strncmp(oc->opts[i].name, str+3, len-3)) {
225       if (strlen(oc->opts[i].name) == len-3) {
226         if (oc->opts[i].count++ && (oc->opts[i].flags & OPT_SINGLE))
227           opt_failure("Option %s appeared the second time.", oc->opts[i].name);
228
229         return &oc->opts[i];
230       }
231       if (candidate)
232         opt_failure("Ambiguous prefix %s: Found matching %s and %s.", str, candidate->name, oc->opts[i].name);
233       else
234         candidate = &oc->opts[i];
235     }
236   }
237
238   if (candidate)
239     return candidate;
240
241   opt_failure("Invalid option %s.", str);
242 }
243
244 #define OPT_PTR(type) ({ \
245   type * ptr; \
246   if (item->flags & OPT_MULTIPLE) { \
247     struct { \
248       cnode n; \
249       type v; \
250     } * n = xmalloc(sizeof(*n)); \
251     clist_add_tail(item->ptr, &(n->n)); \
252     ptr = &(n->v); \
253   } else \
254     ptr = item->ptr; \
255   ptr; })
256
257 #define OPT_NAME (longopt == 2 ? stk_printf("positional arg #%d", oc->positional_count) : (longopt == 1 ? stk_printf("--%s", opt->name) : stk_printf("-%c", item->letter)))
258 static void opt_parse_value(struct opt_context * oc, struct opt_precomputed * opt, char * value, int longopt) {
259   struct opt_item * item = opt->item;
260   for (int i=0;i<oc->hooks_before_value_count;i++)
261     oc->hooks_before_value[i]->u.call(item, value, oc->hooks_before_value[i]->ptr);
262
263   switch (item->cls) {
264     case OPT_CL_BOOL:
265       if (!value || !strcasecmp(value, "y") || !strcasecmp(value, "yes") || !strcasecmp(value, "true") || !strcasecmp(value, "1"))
266         *((int *) item->ptr) = 1 ^ (!!(opt->flags & OPT_NEGATIVE));
267       else if (!strcasecmp(value, "n") || !strcasecmp(value, "no") || !strcasecmp(value, "false") || !strcasecmp(value, "0"))
268         *((int *) item->ptr) = 0 ^ (!!(opt->flags & OPT_NEGATIVE));
269       else
270         opt_failure("Boolean argument for %s has a strange value. Supported (case insensitive): y/n, yes/no, true/false.", OPT_NAME);
271       break;
272     case OPT_CL_STATIC:
273       {
274         char * e = NULL;
275         switch (item->type) {
276           case CT_INT:
277             if (!value)
278               *OPT_PTR(int) = 0;
279             else
280               e = cf_parse_int(value, OPT_PTR(int));
281             if (e)
282               opt_failure("Integer value parsing failed for %s: %s", OPT_NAME, e);
283             break;
284           case CT_U64:
285             if (!value)
286               *OPT_PTR(u64) = 0;
287             else
288               e = cf_parse_u64(value, OPT_PTR(u64));
289             if (e)
290               opt_failure("Unsigned 64-bit value parsing failed for %s: %s", OPT_NAME, e);
291             break;
292           case CT_DOUBLE:
293             if (!value)
294               *OPT_PTR(double) = NAN;
295             else
296               e = cf_parse_double(value, OPT_PTR(double));
297             if (e)
298               opt_failure("Double value parsing failed for %s: %s", OPT_NAME, e);
299             break;
300           case CT_IP:
301             if (!value)
302               e = cf_parse_ip("0.0.0.0", OPT_PTR(u32));
303             else
304               e = cf_parse_ip(value, OPT_PTR(u32));
305             if (e)
306               opt_failure("IP parsing failed for %s: %s", OPT_NAME, e);
307             break;
308           case CT_STRING:
309             if (!value)
310               *OPT_PTR(const char *) = NULL;
311             else
312               *OPT_PTR(const char *) = xstrdup(value);
313             break;
314           default:
315             ASSERT(0);
316         }
317         break;
318       }
319     case OPT_CL_SWITCH:
320       if (*((int *)item->ptr) != -1)
321         opt_failure("Multiple switches: %s", OPT_NAME);
322       else
323         *((int *)item->ptr) = item->u.value;
324       break;
325     case OPT_CL_INC:
326       if (opt->flags & OPT_NEGATIVE)
327         (*((int *)item->ptr))--;
328       else
329         (*((int *)item->ptr))++;
330       break;
331     case OPT_CL_CALL:
332       item->u.call(item, value, item->ptr);
333       break;
334     case OPT_CL_USER:
335       {
336         char * e = NULL;
337         e = item->u.utype->parser(value, OPT_PTR(void*));
338         if (e)
339           opt_failure("User defined type value parsing failed for %s: %s", OPT_NAME, e);
340         break;
341       }
342     default:
343       ASSERT(0);
344   }
345
346   for (int i=0;i<oc->hooks_after_value_count;i++)
347     oc->hooks_after_value[i]->u.call(item, value, oc->hooks_after_value[i]->ptr);
348 }
349 #undef OPT_NAME
350
351 static int opt_longopt(struct opt_context * oc, char ** argv, int index) {
352   int eaten = 0;
353   char * name_in = argv[index] + 2; // skipping the -- on the beginning
354   uns pos = strchrnul(name_in, '=') - name_in;
355   struct opt_precomputed * opt = opt_find_item_longopt(oc, strndupa(name_in, pos));
356   char * value = NULL;
357
358   if (opt->item->cls == OPT_CL_BOOL && !strncmp(name_in, "no-", 3) && !strncmp(name_in+3, opt->item->name, pos-3))
359     value = "n";
360   else if (opt->flags & OPT_REQUIRED_VALUE) {
361     if (pos < strlen(name_in))
362       value = name_in + pos + 1;
363     else {
364       value = argv[index+1];
365       if (!value)
366         opt_failure("Argument --%s must have a value but nothing supplied.", opt->name);
367       eaten++;
368     }
369   }
370   else if (opt->flags & OPT_MAYBE_VALUE) {
371     if (pos < strlen(name_in))
372       value = name_in + pos + 1;
373   }
374   else {
375     if (pos < strlen(name_in))
376       opt_failure("Argument --%s must not have any value.", opt->name);
377   }
378   opt_parse_value(oc, opt, value, 1);
379   return eaten;
380 }
381
382 static int opt_shortopt(struct opt_context * oc, char ** argv, int index) {
383   int chr = 0;
384   struct opt_precomputed * opt;
385   while (argv[index][++chr] && (opt = opt_find_item_shortopt(oc, argv[index][chr]))) {
386     if (opt->flags & OPT_NO_VALUE) {
387       opt_parse_value(oc, opt, NULL, 0);
388     }
389     else if (opt->flags & OPT_REQUIRED_VALUE) {
390       if (chr == 1 && argv[index][2]) {
391         opt_parse_value(oc, opt, argv[index] + 2, 0);
392         return 0;
393       }
394       else if (argv[index][chr+1])
395         opt_failure("Option -%c must have a value but found inside a bunch of short opts.", opt->item->letter);
396       else if (!argv[index+1])
397         opt_failure("Option -%c must have a value but nothing supplied.", opt->item->letter);
398       else {
399         opt_parse_value(oc, opt, argv[index+1], 0);
400         return 1;
401       }
402     }
403     else if (opt->flags & OPT_MAYBE_VALUE) {
404       if (chr == 1 && argv[index][2]) {
405         opt_parse_value(oc, opt, argv[index] + 2, 0);
406         return 0;
407       }
408       else
409         opt_parse_value(oc, opt, NULL, 0);
410     }
411     else {
412       ASSERT(0);
413     }
414   }
415
416   if (argv[index][chr])
417     opt_failure("Unknown option -%c.", argv[index][chr]);
418
419   return 0;
420 }
421
422 static void opt_positional(struct opt_context * oc, char * value) {
423   oc->positional_count++;
424   struct opt_precomputed * opt = opt_find_item_shortopt(oc, (oc->positional_count > oc->positional_max ? 256 : oc->positional_count + 256));
425   if (!opt) {
426     ASSERT(oc->positional_count > oc->positional_max);
427     opt_failure("Too many positional args.");
428   }
429
430   opt_parse_value(oc, opt, value, 2);
431 }
432
433 static void opt_count_items(struct opt_context *oc, const struct opt_section *sec)
434 {
435   for (const struct opt_item *item = sec->opt; item->cls != OPT_CL_END; item++) {
436     if (item->cls == OPT_CL_SECTION)
437       opt_count_items(oc, item->u.section);
438     else if (item->cls == OPT_CL_HOOK) {
439       if (item->flags & OPT_HOOK_BEFORE_ARG)
440         oc->hooks_before_arg_count++;
441       else if (item->flags & OPT_HOOK_BEFORE_VALUE)
442         oc->hooks_before_value_count++;
443       else if (item->flags & OPT_HOOK_AFTER_VALUE)
444         oc->hooks_after_value_count++;
445       else
446         ASSERT(0);
447     } else if (item->letter || item->name) {
448       oc->opt_count++;
449       if (item->letter > OPT_POSITIONAL_TAIL)
450         oc->positional_max++;
451     }
452   }
453 }
454
455 static void opt_add_default_flags(struct opt_precomputed *opt)
456 {
457   struct opt_item *item = opt->item;
458   uns flags = opt->flags;
459
460   if (item->letter >= 256) {
461     flags &= ~OPT_VALUE_FLAGS;
462     flags |= OPT_REQUIRED_VALUE;
463   }
464   if (!(flags & OPT_VALUE_FLAGS)) {
465     ASSERT(item->cls != OPT_CL_CALL && item->cls != OPT_CL_USER);
466     flags |= opt_default_value_flags[item->cls];
467   }
468
469   opt->flags = flags;
470 }
471
472 static void opt_prepare_items(struct opt_context *oc, const struct opt_section *sec)
473 {
474   for (struct opt_item *item = sec->opt; item->cls != OPT_CL_END; item++) {
475     if (item->cls == OPT_CL_SECTION)
476       opt_prepare_items(oc, item->u.section);
477     else if (item->cls == OPT_CL_HOOK) {
478       if (item->flags & OPT_HOOK_BEFORE_ARG)
479         oc->hooks_before_arg[oc->hooks_before_arg_count++] = item;
480       else if (item->flags & OPT_HOOK_BEFORE_VALUE)
481         oc->hooks_before_value[oc->hooks_before_value_count++] = item;
482       else if (item->flags & OPT_HOOK_AFTER_VALUE)
483         oc->hooks_after_value[oc->hooks_after_value_count++] = item;
484       else
485         ASSERT(0);
486     } else if (item->letter || item->name) {
487       struct opt_precomputed * opt = &oc->opts[oc->opt_count++];
488       opt->item = item;
489       opt->flags = item->flags;
490       opt->count = 0;
491       opt->name = item->name;
492       if (item->letter)
493         oc->shortopt[(int) item->letter] = opt;
494       opt_add_default_flags(opt);
495     }
496   }
497 }
498
499 void opt_parse(const struct opt_section * options, char ** argv) {
500   struct opt_context * oc = alloca(sizeof(*oc));
501   memset(oc, 0, sizeof (*oc));
502
503   opt_count_items(oc, options);
504   oc->opts = alloca(sizeof(*oc->opts) * oc->opt_count);
505   oc->shortopt = alloca(sizeof(*oc->shortopt) * (oc->positional_max + 257));
506   memset(oc->shortopt, 0, sizeof(*oc->shortopt) * (oc->positional_max + 257));
507   oc->hooks_before_arg = alloca(sizeof (*oc->hooks_before_arg) * oc->hooks_before_arg_count);
508   oc->hooks_before_value = alloca(sizeof (*oc->hooks_before_value) * oc->hooks_before_value_count);
509   oc->hooks_after_value = alloca(sizeof (*oc->hooks_after_value) * oc->hooks_after_value_count);
510
511   oc->opt_count = 0;
512   oc->hooks_before_arg_count = 0;
513   oc->hooks_before_value_count = 0;
514   oc->hooks_after_value_count = 0;
515   opt_prepare_items(oc, options);
516
517   int force_positional = 0;
518   for (int i=0; argv[i]; i++) {
519     char *arg = argv[i];
520     for (int j=0; j<oc->hooks_before_arg_count; j++)
521       oc->hooks_before_arg[j]->u.call(NULL, NULL, oc->hooks_before_arg[j]->ptr);
522     if (arg[0] != '-' || force_positional)
523       opt_positional(oc, arg);
524     else {
525       if (arg[1] == '-') {
526         if (arg[2] == '\0')
527           force_positional++;
528         else
529           i += opt_longopt(oc, argv, i);
530       } else if (arg[1])
531         i += opt_shortopt(oc, argv, i);
532       else
533         opt_positional(oc, arg);
534     }
535   }
536
537   for (int i=0;i<oc->positional_max+257;i++) {
538     if (!oc->shortopt[i])
539       continue;
540     if (!oc->shortopt[i]->count && (oc->shortopt[i]->flags & OPT_REQUIRED))
541       if (i < 256)
542         if (oc->shortopt[i]->item->name)
543           opt_failure("Required option -%c/--%s not found.", oc->shortopt[i]->item->letter, oc->shortopt[i]->item->name);
544         else
545           opt_failure("Required option -%c not found.", oc->shortopt[i]->item->letter);
546       else
547         opt_failure("Required positional argument #%d not found.", (i > 256) ? oc->shortopt[i]->item->letter-256 : oc->positional_max+1);
548   }
549
550   for (int i=0;i<oc->opt_count;i++) {
551     if (!oc->opts[i].count && (oc->opts[i].flags & OPT_REQUIRED))
552       opt_failure("Required option --%s not found.", oc->opts[i].item->name);
553   }
554 }
555
556 static void opt_conf_end_of_options(struct cf_context *cc) {
557   cf_load_default(cc);
558   if (cc->postpone_commit && cf_close_group())
559     opt_failure("Loading of configuration failed");
560 }
561
562 void opt_conf_internal(struct opt_item * opt, const char * value, void * data UNUSED) {
563   struct cf_context *cc = cf_get_context();
564   switch (opt->letter) {
565     case 'S':
566       cf_load_default(cc);
567       if (cf_set(value))
568         opt_failure("Cannot set %s", value);
569       break;
570     case 'C':
571       if (cf_load(value))
572         opt_failure("Cannot load config file %s", value);
573       break;
574 #ifdef CONFIG_UCW_DEBUG
575     case '0':
576       opt_conf_end_of_options(cc);
577       struct fastbuf *b = bfdopen(1, 4096);
578       cf_dump_sections(b);
579       bclose(b);
580       exit(0);
581       break;
582 #endif
583   }
584 }
585
586 void opt_conf_hook_internal(struct opt_item * opt, const char * value UNUSED, void * data UNUSED) {
587   static enum {
588     OPT_CONF_HOOK_BEGIN,
589     OPT_CONF_HOOK_CONFIG,
590     OPT_CONF_HOOK_OTHERS
591   } state = OPT_CONF_HOOK_BEGIN;
592
593   int confopt = 0;
594
595   if (opt->letter == 'S' || opt->letter == 'C' || (opt->name && !strcmp(opt->name, "dumpconfig")))
596     confopt = 1;
597
598   switch (state) {
599     case OPT_CONF_HOOK_BEGIN:
600       if (confopt)
601         state = OPT_CONF_HOOK_CONFIG;
602       else {
603         opt_conf_end_of_options(cf_get_context());
604         state = OPT_CONF_HOOK_OTHERS;
605       }
606       break;
607     case OPT_CONF_HOOK_CONFIG:
608       if (!confopt) {
609         opt_conf_end_of_options(cf_get_context());
610         state = OPT_CONF_HOOK_OTHERS;
611       }
612       break;
613     case OPT_CONF_HOOK_OTHERS:
614       if (confopt)
615         opt_failure("Config options (-C, -S) must stand before other options.");
616       break;
617     default:
618       ASSERT(0);
619   }
620 }