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>
21 * Value flags defaults
22 * ~~~~~~~~~~~~~~~~~~~~
24 * OPT_NO_VALUE for OPT_BOOL, OPT_SWITCH and OPT_INC
25 * OPT_MAYBE_VALUE for OPT_STRING, OPT_UNS, OPT_INT
26 * Some of the value flags (OPT_NO_VALUE, OPT_MAYBE_VALUE, OPT_REQUIRED_VALUE)
27 * must be specified for OPT_CALL and OPT_USER.
29 static uns opt_default_value_flags[] = {
30 [OPT_CL_BOOL] = OPT_NO_VALUE,
31 [OPT_CL_STATIC] = OPT_MAYBE_VALUE,
32 [OPT_CL_SWITCH] = OPT_NO_VALUE,
33 [OPT_CL_INC] = OPT_NO_VALUE,
41 struct opt_precomputed * opts;
42 struct opt_precomputed ** shortopt;
43 struct opt_item ** hooks;
51 void opt_failure(const char * mesg, ...) {
54 vfprintf(stderr, mesg, args);
55 fprintf(stderr, "\nRun with --help for more information.\n");
56 exit(OPT_EXIT_BAD_ARGS);
59 static char *opt_name(struct opt_context *oc, struct opt_precomputed *opt)
61 struct opt_item *item = opt->item;
63 if (item->letter >= OPT_POSITIONAL_TAIL)
64 res = stk_printf("positional argument #%d", oc->positional_count);
65 else if (opt->flags & OPT_SEEN_AS_LONG)
66 res = stk_printf("--%s", opt->name);
68 res = stk_printf("-%c", item->letter);
72 #define THIS_OPT opt_name(oc, opt)
74 void opt_precompute(struct opt_precomputed *opt, struct opt_item *item)
78 opt->name = item->name;
79 uns flags = item->flags;
81 if (item->letter >= OPT_POSITIONAL_TAIL) {
82 flags &= ~OPT_VALUE_FLAGS;
83 flags |= OPT_REQUIRED_VALUE;
85 if (!(flags & OPT_VALUE_FLAGS)) {
86 ASSERT(item->cls != OPT_CL_CALL && item->cls != OPT_CL_USER);
87 flags |= opt_default_value_flags[item->cls];
93 static void opt_invoke_hooks(struct opt_context *oc, uns event, struct opt_item *item, char *value)
95 for (int i = 0; i < oc->hook_count; i++) {
96 struct opt_item *hook = oc->hooks[i];
97 if (hook->flags & event)
98 hook->u.hook(item, event, value, hook->ptr);
102 static struct opt_precomputed * opt_find_item_longopt(struct opt_context * oc, char * str) {
103 uns len = strlen(str);
104 struct opt_precomputed * candidate = NULL;
106 for (int i = 0; i < oc->opt_count; i++) {
107 struct opt_precomputed *opt = &oc->opts[i];
111 if (!strncmp(opt->name, str, len)) {
112 if (strlen(opt->name) == len)
114 } else if (opt->item->cls == OPT_CL_BOOL && !strncmp("no-", str, 3) && !strncmp(opt->name, str+3, len-3)) {
115 if (strlen(opt->name) == len-3)
121 opt_failure("Ambiguous option --%s: matches both --%s and --%s.", str, candidate->name, opt->name);
129 opt_failure("Invalid option --%s.", str);
132 // FIXME: Use simple-lists?
133 #define OPT_PTR(type) ({ \
135 if (item->flags & OPT_MULTIPLE) { \
139 } * n = xmalloc(sizeof(*n)); \
140 clist_add_tail(item->ptr, &(n->n)); \
146 static void opt_parse_value(struct opt_context * oc, struct opt_precomputed * opt, char * value) {
147 struct opt_item * item = opt->item;
149 if (opt->count++ && (opt->flags & OPT_SINGLE))
150 opt_failure("Option %s must be specified at most once.", THIS_OPT);
152 if (opt->flags & OPT_LAST_ARG)
153 oc->stop_parsing = 1;
155 opt_invoke_hooks(oc, OPT_HOOK_BEFORE_VALUE, item, value);
159 if (!value || !strcasecmp(value, "y") || !strcasecmp(value, "yes") || !strcasecmp(value, "true") || !strcasecmp(value, "1"))
160 *((int *) item->ptr) = 1 ^ (!!(opt->flags & OPT_NEGATIVE));
161 else if (!strcasecmp(value, "n") || !strcasecmp(value, "no") || !strcasecmp(value, "false") || !strcasecmp(value, "0"))
162 *((int *) item->ptr) = 0 ^ (!!(opt->flags & OPT_NEGATIVE));
164 opt_failure("Boolean argument for %s has a strange value. Supported (case insensitive): 1/0, y/n, yes/no, true/false.", THIS_OPT);
169 switch (item->type) {
174 e = cf_parse_int(value, OPT_PTR(int));
176 opt_failure("Integer value parsing failed for %s: %s", THIS_OPT, e);
182 e = cf_parse_u64(value, OPT_PTR(u64));
184 opt_failure("Unsigned 64-bit value parsing failed for %s: %s", THIS_OPT, e);
188 *OPT_PTR(double) = NAN;
190 e = cf_parse_double(value, OPT_PTR(double));
192 opt_failure("Floating-point value parsing failed for %s: %s", THIS_OPT, e);
198 e = cf_parse_ip(value, OPT_PTR(u32));
200 opt_failure("IP address parsing failed for %s: %s", THIS_OPT, e);
204 *OPT_PTR(const char *) = NULL;
206 *OPT_PTR(const char *) = xstrdup(value);
214 if ((opt->flags & OPT_SINGLE) && *((int *)item->ptr) != -1)
215 opt_failure("Multiple switches: %s", THIS_OPT);
217 *((int *)item->ptr) = item->u.value;
220 if (opt->flags & OPT_NEGATIVE)
221 (*((int *)item->ptr))--;
223 (*((int *)item->ptr))++;
226 item->u.call(item, value, item->ptr);
231 e = item->u.utype->parser(value, OPT_PTR(void*));
233 opt_failure("Cannot parse the value of %s: %s", THIS_OPT, e);
240 opt_invoke_hooks(oc, OPT_HOOK_AFTER_VALUE, item, value);
243 static int opt_longopt(struct opt_context * oc, char ** argv, int index) {
245 char * name_in = argv[index] + 2; // skipping the -- on the beginning
246 uns pos = strchrnul(name_in, '=') - name_in;
247 struct opt_precomputed * opt = opt_find_item_longopt(oc, strndupa(name_in, pos));
250 opt->flags |= OPT_SEEN_AS_LONG;
252 if (opt->item->cls == OPT_CL_BOOL && !strncmp(name_in, "no-", 3) && !strncmp(name_in+3, opt->item->name, pos-3)) {
254 opt_failure("Option --%s must not have any value.", name_in);
256 } else if (opt->flags & OPT_REQUIRED_VALUE) {
258 value = name_in + pos + 1;
260 value = argv[index+1];
262 opt_failure("Option %s must have a value, but nothing supplied.", THIS_OPT);
265 } else if (opt->flags & OPT_MAYBE_VALUE) {
267 value = name_in + pos + 1;
270 opt_failure("Option %s must have no value.", THIS_OPT);
272 opt_parse_value(oc, opt, value);
276 static int opt_shortopt(struct opt_context * oc, char ** argv, int index) {
278 struct opt_precomputed * opt;
281 while (o = argv[index][++chr]) {
282 if (o < 0 || o >= 128)
283 opt_failure("Invalid character 0x%02x in option name. Only ASCII is allowed.", o & 0xff);
284 opt = oc->shortopt[o];
287 opt_failure("Unknown option -%c.", o);
289 opt->flags &= ~OPT_SEEN_AS_LONG;
291 if (opt->flags & OPT_NO_VALUE)
292 opt_parse_value(oc, opt, NULL);
293 else if (opt->flags & OPT_REQUIRED_VALUE) {
294 if (argv[index][chr+1]) {
295 opt_parse_value(oc, opt, argv[index] + chr + 1);
297 } else if (!argv[index+1])
298 opt_failure("Option -%c must have a value, but nothing supplied.", o);
300 opt_parse_value(oc, opt, argv[index+1]);
303 } else if (opt->flags & OPT_MAYBE_VALUE) {
304 if (argv[index][chr+1]) {
305 opt_parse_value(oc, opt, argv[index] + chr + 1);
308 opt_parse_value(oc, opt, NULL);
317 static void opt_positional(struct opt_context * oc, char * value) {
318 oc->positional_count++;
319 uns id = oc->positional_count > oc->positional_max ? OPT_POSITIONAL_TAIL : OPT_POSITIONAL(oc->positional_count);
320 struct opt_precomputed * opt = oc->shortopt[id];
322 opt_failure("Too many positional arguments.");
324 opt->flags &= OPT_SEEN_AS_LONG;
325 opt_parse_value(oc, opt, value);
329 static void opt_count_items(struct opt_context *oc, const struct opt_section *sec)
331 for (const struct opt_item *item = sec->opt; item->cls != OPT_CL_END; item++) {
332 if (item->cls == OPT_CL_SECTION)
333 opt_count_items(oc, item->u.section);
334 else if (item->cls == OPT_CL_HOOK)
336 else if (item->letter || item->name) {
338 if (item->letter > OPT_POSITIONAL_TAIL)
339 oc->positional_max++;
344 static void opt_prepare_items(struct opt_context *oc, const struct opt_section *sec)
346 for (struct opt_item *item = sec->opt; item->cls != OPT_CL_END; item++) {
347 if (item->cls == OPT_CL_SECTION)
348 opt_prepare_items(oc, item->u.section);
349 else if (item->cls == OPT_CL_HOOK)
350 oc->hooks[oc->hook_count++] = item;
351 else if (item->letter || item->name) {
352 struct opt_precomputed * opt = &oc->opts[oc->opt_count++];
353 opt_precompute(opt, item);
355 oc->shortopt[(int) item->letter] = opt;
360 static void opt_check_required(struct opt_context *oc)
362 for (int i = 0; i < oc->opt_count; i++) {
363 struct opt_precomputed *opt = &oc->opts[i];
364 if (!opt->count && (opt->flags & OPT_REQUIRED)) {
365 struct opt_item *item = opt->item;
366 if (item->letter > OPT_POSITIONAL_TAIL)
367 opt_failure("Required positional argument #%d not found.", item->letter - OPT_POSITIONAL_TAIL);
368 else if (item->letter == OPT_POSITIONAL_TAIL)
369 opt_failure("Required positional argument not found.");
370 else if (item->letter && item->name)
371 opt_failure("Required option -%c/--%s not found.", item->letter, item->name);
372 else if (item->letter)
373 opt_failure("Required option -%c not found.", item->letter);
375 opt_failure("Required option --%s not found.", item->name);
380 int opt_parse(const struct opt_section * options, char ** argv) {
381 struct opt_context * oc = alloca(sizeof(*oc));
382 memset(oc, 0, sizeof (*oc));
384 opt_count_items(oc, options);
385 oc->opts = alloca(sizeof(*oc->opts) * oc->opt_count);
386 oc->shortopt = alloca(sizeof(*oc->shortopt) * (oc->positional_max + 257));
387 memset(oc->shortopt, 0, sizeof(*oc->shortopt) * (oc->positional_max + 257));
388 oc->hooks = alloca(sizeof (*oc->hooks) * oc->hook_count);
392 opt_prepare_items(oc, options);
394 int force_positional = 0;
396 for (i=0; argv[i] && !oc->stop_parsing; i++) {
398 opt_invoke_hooks(oc, OPT_HOOK_BEFORE_ARG, NULL, NULL);
399 if (arg[0] != '-' || force_positional)
400 opt_positional(oc, arg);
406 i += opt_longopt(oc, argv, i);
408 i += opt_shortopt(oc, argv, i);
410 opt_positional(oc, arg);
414 opt_check_required(oc);
415 opt_invoke_hooks(oc, OPT_HOOK_FINAL, NULL, NULL);