2 * UCW Library -- Parsing of command-line options
4 * (c) 2013 Jan Moskyto Matejka <mq@ucw.cz>
5 * (c) 2014 Martin Mares <mj@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
13 #include <ucw/opt-internal.h>
14 #include <ucw/stkstring.h>
15 #include <ucw/strtonum.h>
22 * Value flags defaults
23 * ~~~~~~~~~~~~~~~~~~~~
25 * OPT_NO_VALUE for OPT_BOOL, OPT_SWITCH and OPT_INC
26 * OPT_MAYBE_VALUE for OPT_STRING, OPT_UNS, OPT_INT
27 * Some of the value flags (OPT_NO_VALUE, OPT_MAYBE_VALUE, OPT_REQUIRED_VALUE)
28 * must be specified for OPT_CALL and OPT_USER.
30 static uns opt_default_value_flags[] = {
31 [OPT_CL_BOOL] = OPT_NO_VALUE,
32 [OPT_CL_STATIC] = OPT_MAYBE_VALUE,
33 [OPT_CL_SWITCH] = OPT_NO_VALUE,
34 [OPT_CL_INC] = OPT_NO_VALUE,
42 struct opt_precomputed * opts;
43 struct opt_precomputed ** shortopt;
44 struct opt_item ** hooks_before_arg;
45 struct opt_item ** hooks_before_value;
46 struct opt_item ** hooks_after_value;
48 short hooks_before_arg_count;
49 short hooks_before_value_count;
50 short hooks_after_value_count;
56 void opt_failure(const char * mesg, ...) {
59 vfprintf(stderr, mesg, args);
60 fprintf(stderr, "\n");
62 exit(OPT_EXIT_BAD_ARGS);
65 static char *opt_name(struct opt_context *oc, struct opt_precomputed *opt)
67 struct opt_item *item = opt->item;
69 if (item->letter >= OPT_POSITIONAL_TAIL)
70 res = stk_printf("positional argument #%d", oc->positional_count);
71 else if (opt->flags & OPT_SEEN_AS_LONG)
72 res = stk_printf("--%s", opt->name);
74 res = stk_printf("-%c", item->letter);
78 #define THIS_OPT opt_name(oc, opt)
80 void opt_precompute(struct opt_precomputed *opt, struct opt_item *item)
84 opt->name = item->name;
85 uns flags = item->flags;
87 if (item->letter >= OPT_POSITIONAL_TAIL) {
88 flags &= ~OPT_VALUE_FLAGS;
89 flags |= OPT_REQUIRED_VALUE;
91 if (!(flags & OPT_VALUE_FLAGS)) {
92 ASSERT(item->cls != OPT_CL_CALL && item->cls != OPT_CL_USER);
93 flags |= opt_default_value_flags[item->cls];
99 static struct opt_precomputed * opt_find_item_longopt(struct opt_context * oc, char * str) {
100 uns len = strlen(str);
101 struct opt_precomputed * candidate = NULL;
103 for (int i = 0; i < oc->opt_count; i++) {
104 struct opt_precomputed *opt = &oc->opts[i];
108 if (!strncmp(opt->name, str, len)) {
109 if (strlen(opt->name) == len)
111 } else if (opt->item->cls == OPT_CL_BOOL && !strncmp("no-", str, 3) && !strncmp(opt->name, str+3, len-3)) {
112 if (strlen(opt->name) == len-3)
118 opt_failure("Ambiguous option --%s: matches both --%s and --%s.", str, candidate->name, opt->name);
126 opt_failure("Invalid option --%s.", str);
129 // FIXME: Use simple-lists?
130 #define OPT_PTR(type) ({ \
132 if (item->flags & OPT_MULTIPLE) { \
136 } * n = xmalloc(sizeof(*n)); \
137 clist_add_tail(item->ptr, &(n->n)); \
143 static void opt_parse_value(struct opt_context * oc, struct opt_precomputed * opt, char * value) {
144 struct opt_item * item = opt->item;
146 if (opt->count++ && (opt->flags & OPT_SINGLE))
147 opt_failure("Option %s must be specified at most once.", THIS_OPT);
149 if (opt->flags & OPT_LAST_ARG)
150 oc->stop_parsing = 1;
152 for (int i = 0; i < oc->hooks_before_value_count; i++)
153 oc->hooks_before_value[i]->u.call(item, value, oc->hooks_before_value[i]->ptr);
157 if (!value || !strcasecmp(value, "y") || !strcasecmp(value, "yes") || !strcasecmp(value, "true") || !strcasecmp(value, "1"))
158 *((int *) item->ptr) = 1 ^ (!!(opt->flags & OPT_NEGATIVE));
159 else if (!strcasecmp(value, "n") || !strcasecmp(value, "no") || !strcasecmp(value, "false") || !strcasecmp(value, "0"))
160 *((int *) item->ptr) = 0 ^ (!!(opt->flags & OPT_NEGATIVE));
162 opt_failure("Boolean argument for %s has a strange value. Supported (case insensitive): 1/0, y/n, yes/no, true/false.", THIS_OPT);
167 switch (item->type) {
172 e = cf_parse_int(value, OPT_PTR(int));
174 opt_failure("Integer value parsing failed for %s: %s", THIS_OPT, e);
180 e = cf_parse_u64(value, OPT_PTR(u64));
182 opt_failure("Unsigned 64-bit value parsing failed for %s: %s", THIS_OPT, e);
186 *OPT_PTR(double) = NAN;
188 e = cf_parse_double(value, OPT_PTR(double));
190 opt_failure("Floating-point value parsing failed for %s: %s", THIS_OPT, e);
196 e = cf_parse_ip(value, OPT_PTR(u32));
198 opt_failure("IP address parsing failed for %s: %s", THIS_OPT, e);
202 *OPT_PTR(const char *) = NULL;
204 *OPT_PTR(const char *) = xstrdup(value);
212 if ((opt->flags & OPT_SINGLE) && *((int *)item->ptr) != -1)
213 opt_failure("Multiple switches: %s", THIS_OPT);
215 *((int *)item->ptr) = item->u.value;
218 if (opt->flags & OPT_NEGATIVE)
219 (*((int *)item->ptr))--;
221 (*((int *)item->ptr))++;
224 item->u.call(item, value, item->ptr);
229 e = item->u.utype->parser(value, OPT_PTR(void*));
231 opt_failure("Cannot parse the value of %s: %s", THIS_OPT, e);
238 for (int i = 0;i < oc->hooks_after_value_count; i++)
239 oc->hooks_after_value[i]->u.call(item, value, oc->hooks_after_value[i]->ptr);
242 static int opt_longopt(struct opt_context * oc, char ** argv, int index) {
244 char * name_in = argv[index] + 2; // skipping the -- on the beginning
245 uns pos = strchrnul(name_in, '=') - name_in;
246 struct opt_precomputed * opt = opt_find_item_longopt(oc, strndupa(name_in, pos));
249 opt->flags |= OPT_SEEN_AS_LONG;
251 if (opt->item->cls == OPT_CL_BOOL && !strncmp(name_in, "no-", 3) && !strncmp(name_in+3, opt->item->name, pos-3)) {
253 opt_failure("Option --%s must not have any value.", name_in);
255 } else if (opt->flags & OPT_REQUIRED_VALUE) {
257 value = name_in + pos + 1;
259 value = argv[index+1];
261 opt_failure("Option %s must have a value, but nothing supplied.", THIS_OPT);
264 } else if (opt->flags & OPT_MAYBE_VALUE) {
266 value = name_in + pos + 1;
269 opt_failure("Option %s must have no value.", THIS_OPT);
271 opt_parse_value(oc, opt, value);
275 static int opt_shortopt(struct opt_context * oc, char ** argv, int index) {
277 struct opt_precomputed * opt;
280 while (o = argv[index][++chr]) {
281 if (o < 0 || o >= 128)
282 opt_failure("Invalid character 0x%02x in option name. Only ASCII is allowed.", o & 0xff);
283 opt = oc->shortopt[o];
286 opt_failure("Unknown option -%c.", o);
288 opt->flags &= ~OPT_SEEN_AS_LONG;
290 if (opt->flags & OPT_NO_VALUE)
291 opt_parse_value(oc, opt, NULL);
292 else if (opt->flags & OPT_REQUIRED_VALUE) {
293 if (argv[index][chr+1]) {
294 opt_parse_value(oc, opt, argv[index] + chr + 1);
296 } else if (!argv[index+1])
297 opt_failure("Option -%c must have a value, but nothing supplied.", o);
299 opt_parse_value(oc, opt, argv[index+1]);
302 } else if (opt->flags & OPT_MAYBE_VALUE) {
303 if (argv[index][chr+1]) {
304 opt_parse_value(oc, opt, argv[index] + chr + 1);
307 opt_parse_value(oc, opt, NULL);
316 static void opt_positional(struct opt_context * oc, char * value) {
317 oc->positional_count++;
318 uns id = oc->positional_count > oc->positional_max ? OPT_POSITIONAL_TAIL : OPT_POSITIONAL(oc->positional_count);
319 struct opt_precomputed * opt = oc->shortopt[id];
321 opt_failure("Too many positional arguments.");
323 opt->flags &= OPT_SEEN_AS_LONG;
324 opt_parse_value(oc, opt, value);
328 static void opt_count_items(struct opt_context *oc, const struct opt_section *sec)
330 for (const struct opt_item *item = sec->opt; item->cls != OPT_CL_END; item++) {
331 if (item->cls == OPT_CL_SECTION)
332 opt_count_items(oc, item->u.section);
333 else if (item->cls == OPT_CL_HOOK) {
334 if (item->flags & OPT_HOOK_BEFORE_ARG)
335 oc->hooks_before_arg_count++;
336 else if (item->flags & OPT_HOOK_BEFORE_VALUE)
337 oc->hooks_before_value_count++;
338 else if (item->flags & OPT_HOOK_AFTER_VALUE)
339 oc->hooks_after_value_count++;
342 } else if (item->letter || item->name) {
344 if (item->letter > OPT_POSITIONAL_TAIL)
345 oc->positional_max++;
350 static void opt_prepare_items(struct opt_context *oc, const struct opt_section *sec)
352 for (struct opt_item *item = sec->opt; item->cls != OPT_CL_END; item++) {
353 if (item->cls == OPT_CL_SECTION)
354 opt_prepare_items(oc, item->u.section);
355 else if (item->cls == OPT_CL_HOOK) {
356 if (item->flags & OPT_HOOK_BEFORE_ARG)
357 oc->hooks_before_arg[oc->hooks_before_arg_count++] = item;
358 else if (item->flags & OPT_HOOK_BEFORE_VALUE)
359 oc->hooks_before_value[oc->hooks_before_value_count++] = item;
360 else if (item->flags & OPT_HOOK_AFTER_VALUE)
361 oc->hooks_after_value[oc->hooks_after_value_count++] = item;
364 } else if (item->letter || item->name) {
365 struct opt_precomputed * opt = &oc->opts[oc->opt_count++];
366 opt_precompute(opt, item);
368 oc->shortopt[(int) item->letter] = opt;
373 static void opt_check_required(struct opt_context *oc)
375 for (int i = 0; i < oc->opt_count; i++) {
376 struct opt_precomputed *opt = &oc->opts[i];
377 if (!opt->count && (opt->flags & OPT_REQUIRED)) {
378 struct opt_item *item = opt->item;
379 if (item->letter > OPT_POSITIONAL_TAIL)
380 opt_failure("Required positional argument #%d not found.", item->letter - OPT_POSITIONAL_TAIL);
381 else if (item->letter == OPT_POSITIONAL_TAIL)
382 opt_failure("Required positional argument not found.");
383 else if (item->letter && item->name)
384 opt_failure("Required option -%c/--%s not found.", item->letter, item->name);
385 else if (item->letter)
386 opt_failure("Required option -%c not found.", item->letter);
388 opt_failure("Required option --%s not found.", item->name);
393 int opt_parse(const struct opt_section * options, char ** argv) {
394 struct opt_context * oc = alloca(sizeof(*oc));
395 memset(oc, 0, sizeof (*oc));
397 opt_count_items(oc, options);
398 oc->opts = alloca(sizeof(*oc->opts) * oc->opt_count);
399 oc->shortopt = alloca(sizeof(*oc->shortopt) * (oc->positional_max + 257));
400 memset(oc->shortopt, 0, sizeof(*oc->shortopt) * (oc->positional_max + 257));
401 oc->hooks_before_arg = alloca(sizeof (*oc->hooks_before_arg) * oc->hooks_before_arg_count);
402 oc->hooks_before_value = alloca(sizeof (*oc->hooks_before_value) * oc->hooks_before_value_count);
403 oc->hooks_after_value = alloca(sizeof (*oc->hooks_after_value) * oc->hooks_after_value_count);
406 oc->hooks_before_arg_count = 0;
407 oc->hooks_before_value_count = 0;
408 oc->hooks_after_value_count = 0;
409 opt_prepare_items(oc, options);
411 int force_positional = 0;
413 for (i=0; argv[i] && !oc->stop_parsing; i++) {
415 for (int j = 0; j < oc->hooks_before_arg_count; j++)
416 oc->hooks_before_arg[j]->u.call(NULL, NULL, oc->hooks_before_arg[j]->ptr);
417 if (arg[0] != '-' || force_positional)
418 opt_positional(oc, arg);
424 i += opt_longopt(oc, argv, i);
426 i += opt_shortopt(oc, argv, i);
428 opt_positional(oc, arg);
432 opt_check_required(oc);