Loading...
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/compiler.h>
3#include <linux/string.h>
4#include <linux/types.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <stdint.h>
8#include <string.h>
9#include <ctype.h>
10#include "subcmd-util.h"
11#include "parse-options.h"
12#include "subcmd-config.h"
13#include "pager.h"
14
15#define OPT_SHORT 1
16#define OPT_UNSET 2
17
18char *error_buf;
19
20static int opterror(const struct option *opt, const char *reason, int flags)
21{
22 if (flags & OPT_SHORT)
23 fprintf(stderr, " Error: switch `%c' %s", opt->short_name, reason);
24 else if (flags & OPT_UNSET)
25 fprintf(stderr, " Error: option `no-%s' %s", opt->long_name, reason);
26 else
27 fprintf(stderr, " Error: option `%s' %s", opt->long_name, reason);
28
29 return -1;
30}
31
32static const char *skip_prefix(const char *str, const char *prefix)
33{
34 size_t len = strlen(prefix);
35 return strncmp(str, prefix, len) ? NULL : str + len;
36}
37
38static void optwarning(const struct option *opt, const char *reason, int flags)
39{
40 if (flags & OPT_SHORT)
41 fprintf(stderr, " Warning: switch `%c' %s", opt->short_name, reason);
42 else if (flags & OPT_UNSET)
43 fprintf(stderr, " Warning: option `no-%s' %s", opt->long_name, reason);
44 else
45 fprintf(stderr, " Warning: option `%s' %s", opt->long_name, reason);
46}
47
48static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
49 int flags, const char **arg)
50{
51 const char *res;
52
53 if (p->opt) {
54 res = p->opt;
55 p->opt = NULL;
56 } else if ((opt->flags & PARSE_OPT_LASTARG_DEFAULT) && (p->argc == 1 ||
57 **(p->argv + 1) == '-')) {
58 res = (const char *)opt->defval;
59 } else if (p->argc > 1) {
60 p->argc--;
61 res = *++p->argv;
62 } else
63 return opterror(opt, "requires a value", flags);
64 if (arg)
65 *arg = res;
66 return 0;
67}
68
69static int get_value(struct parse_opt_ctx_t *p,
70 const struct option *opt, int flags)
71{
72 const char *s, *arg = NULL;
73 const int unset = flags & OPT_UNSET;
74 int err;
75
76 if (unset && p->opt)
77 return opterror(opt, "takes no value", flags);
78 if (unset && (opt->flags & PARSE_OPT_NONEG))
79 return opterror(opt, "isn't available", flags);
80 if (opt->flags & PARSE_OPT_DISABLED)
81 return opterror(opt, "is not usable", flags);
82
83 if (opt->flags & PARSE_OPT_EXCLUSIVE) {
84 if (p->excl_opt && p->excl_opt != opt) {
85 char msg[128];
86
87 if (((flags & OPT_SHORT) && p->excl_opt->short_name) ||
88 p->excl_opt->long_name == NULL) {
89 snprintf(msg, sizeof(msg), "cannot be used with switch `%c'",
90 p->excl_opt->short_name);
91 } else {
92 snprintf(msg, sizeof(msg), "cannot be used with %s",
93 p->excl_opt->long_name);
94 }
95 opterror(opt, msg, flags);
96 return -3;
97 }
98 p->excl_opt = opt;
99 }
100 if (!(flags & OPT_SHORT) && p->opt) {
101 switch (opt->type) {
102 case OPTION_CALLBACK:
103 if (!(opt->flags & PARSE_OPT_NOARG))
104 break;
105 /* FALLTHROUGH */
106 case OPTION_BOOLEAN:
107 case OPTION_INCR:
108 case OPTION_BIT:
109 case OPTION_SET_UINT:
110 case OPTION_SET_PTR:
111 return opterror(opt, "takes no value", flags);
112 case OPTION_END:
113 case OPTION_ARGUMENT:
114 case OPTION_GROUP:
115 case OPTION_STRING:
116 case OPTION_INTEGER:
117 case OPTION_UINTEGER:
118 case OPTION_LONG:
119 case OPTION_ULONG:
120 case OPTION_U64:
121 default:
122 break;
123 }
124 }
125
126 if (opt->flags & PARSE_OPT_NOBUILD) {
127 char reason[128];
128 bool noarg = false;
129
130 err = snprintf(reason, sizeof(reason),
131 opt->flags & PARSE_OPT_CANSKIP ?
132 "is being ignored because %s " :
133 "is not available because %s",
134 opt->build_opt);
135 reason[sizeof(reason) - 1] = '\0';
136
137 if (err < 0)
138 strncpy(reason, opt->flags & PARSE_OPT_CANSKIP ?
139 "is being ignored" :
140 "is not available",
141 sizeof(reason));
142
143 if (!(opt->flags & PARSE_OPT_CANSKIP))
144 return opterror(opt, reason, flags);
145
146 err = 0;
147 if (unset)
148 noarg = true;
149 if (opt->flags & PARSE_OPT_NOARG)
150 noarg = true;
151 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
152 noarg = true;
153
154 switch (opt->type) {
155 case OPTION_BOOLEAN:
156 case OPTION_INCR:
157 case OPTION_BIT:
158 case OPTION_SET_UINT:
159 case OPTION_SET_PTR:
160 case OPTION_END:
161 case OPTION_ARGUMENT:
162 case OPTION_GROUP:
163 noarg = true;
164 break;
165 case OPTION_CALLBACK:
166 case OPTION_STRING:
167 case OPTION_INTEGER:
168 case OPTION_UINTEGER:
169 case OPTION_LONG:
170 case OPTION_ULONG:
171 case OPTION_U64:
172 default:
173 break;
174 }
175
176 if (!noarg)
177 err = get_arg(p, opt, flags, NULL);
178 if (err)
179 return err;
180
181 optwarning(opt, reason, flags);
182 return 0;
183 }
184
185 switch (opt->type) {
186 case OPTION_BIT:
187 if (unset)
188 *(int *)opt->value &= ~opt->defval;
189 else
190 *(int *)opt->value |= opt->defval;
191 return 0;
192
193 case OPTION_BOOLEAN:
194 *(bool *)opt->value = unset ? false : true;
195 if (opt->set)
196 *(bool *)opt->set = true;
197 return 0;
198
199 case OPTION_INCR:
200 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
201 return 0;
202
203 case OPTION_SET_UINT:
204 *(unsigned int *)opt->value = unset ? 0 : opt->defval;
205 return 0;
206
207 case OPTION_SET_PTR:
208 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
209 return 0;
210
211 case OPTION_STRING:
212 err = 0;
213 if (unset)
214 *(const char **)opt->value = NULL;
215 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
216 *(const char **)opt->value = (const char *)opt->defval;
217 else
218 err = get_arg(p, opt, flags, (const char **)opt->value);
219
220 if (opt->set)
221 *(bool *)opt->set = true;
222
223 /* PARSE_OPT_NOEMPTY: Allow NULL but disallow empty string. */
224 if (opt->flags & PARSE_OPT_NOEMPTY) {
225 const char *val = *(const char **)opt->value;
226
227 if (!val)
228 return err;
229
230 /* Similar to unset if we are given an empty string. */
231 if (val[0] == '\0') {
232 *(const char **)opt->value = NULL;
233 return 0;
234 }
235 }
236
237 return err;
238
239 case OPTION_CALLBACK:
240 if (opt->set)
241 *(bool *)opt->set = true;
242
243 if (unset)
244 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
245 if (opt->flags & PARSE_OPT_NOARG)
246 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
247 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
248 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
249 if (get_arg(p, opt, flags, &arg))
250 return -1;
251 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
252
253 case OPTION_INTEGER:
254 if (unset) {
255 *(int *)opt->value = 0;
256 return 0;
257 }
258 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
259 *(int *)opt->value = opt->defval;
260 return 0;
261 }
262 if (get_arg(p, opt, flags, &arg))
263 return -1;
264 *(int *)opt->value = strtol(arg, (char **)&s, 10);
265 if (*s)
266 return opterror(opt, "expects a numerical value", flags);
267 return 0;
268
269 case OPTION_UINTEGER:
270 if (unset) {
271 *(unsigned int *)opt->value = 0;
272 return 0;
273 }
274 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
275 *(unsigned int *)opt->value = opt->defval;
276 return 0;
277 }
278 if (get_arg(p, opt, flags, &arg))
279 return -1;
280 if (arg[0] == '-')
281 return opterror(opt, "expects an unsigned numerical value", flags);
282 *(unsigned int *)opt->value = strtol(arg, (char **)&s, 10);
283 if (*s)
284 return opterror(opt, "expects a numerical value", flags);
285 return 0;
286
287 case OPTION_LONG:
288 if (unset) {
289 *(long *)opt->value = 0;
290 return 0;
291 }
292 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
293 *(long *)opt->value = opt->defval;
294 return 0;
295 }
296 if (get_arg(p, opt, flags, &arg))
297 return -1;
298 *(long *)opt->value = strtol(arg, (char **)&s, 10);
299 if (*s)
300 return opterror(opt, "expects a numerical value", flags);
301 return 0;
302
303 case OPTION_ULONG:
304 if (unset) {
305 *(unsigned long *)opt->value = 0;
306 return 0;
307 }
308 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
309 *(unsigned long *)opt->value = opt->defval;
310 return 0;
311 }
312 if (get_arg(p, opt, flags, &arg))
313 return -1;
314 *(unsigned long *)opt->value = strtoul(arg, (char **)&s, 10);
315 if (*s)
316 return opterror(opt, "expects a numerical value", flags);
317 return 0;
318
319 case OPTION_U64:
320 if (unset) {
321 *(u64 *)opt->value = 0;
322 return 0;
323 }
324 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
325 *(u64 *)opt->value = opt->defval;
326 return 0;
327 }
328 if (get_arg(p, opt, flags, &arg))
329 return -1;
330 if (arg[0] == '-')
331 return opterror(opt, "expects an unsigned numerical value", flags);
332 *(u64 *)opt->value = strtoull(arg, (char **)&s, 10);
333 if (*s)
334 return opterror(opt, "expects a numerical value", flags);
335 return 0;
336
337 case OPTION_END:
338 case OPTION_ARGUMENT:
339 case OPTION_GROUP:
340 default:
341 die("should not happen, someone must be hit on the forehead");
342 }
343}
344
345static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
346{
347retry:
348 for (; options->type != OPTION_END; options++) {
349 if (options->short_name == *p->opt) {
350 p->opt = p->opt[1] ? p->opt + 1 : NULL;
351 return get_value(p, options, OPT_SHORT);
352 }
353 }
354
355 if (options->parent) {
356 options = options->parent;
357 goto retry;
358 }
359
360 return -2;
361}
362
363static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
364 const struct option *options)
365{
366 const char *arg_end = strchr(arg, '=');
367 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
368 int abbrev_flags = 0, ambiguous_flags = 0;
369
370 if (!arg_end)
371 arg_end = arg + strlen(arg);
372
373retry:
374 for (; options->type != OPTION_END; options++) {
375 const char *rest;
376 int flags = 0;
377
378 if (!options->long_name)
379 continue;
380
381 rest = skip_prefix(arg, options->long_name);
382 if (options->type == OPTION_ARGUMENT) {
383 if (!rest)
384 continue;
385 if (*rest == '=')
386 return opterror(options, "takes no value", flags);
387 if (*rest)
388 continue;
389 p->out[p->cpidx++] = arg - 2;
390 return 0;
391 }
392 if (!rest) {
393 if (strstarts(options->long_name, "no-")) {
394 /*
395 * The long name itself starts with "no-", so
396 * accept the option without "no-" so that users
397 * do not have to enter "no-no-" to get the
398 * negation.
399 */
400 rest = skip_prefix(arg, options->long_name + 3);
401 if (rest) {
402 flags |= OPT_UNSET;
403 goto match;
404 }
405 /* Abbreviated case */
406 if (strstarts(options->long_name + 3, arg)) {
407 flags |= OPT_UNSET;
408 goto is_abbreviated;
409 }
410 }
411 /* abbreviated? */
412 if (!strncmp(options->long_name, arg, arg_end - arg)) {
413is_abbreviated:
414 if (abbrev_option) {
415 /*
416 * If this is abbreviated, it is
417 * ambiguous. So when there is no
418 * exact match later, we need to
419 * error out.
420 */
421 ambiguous_option = abbrev_option;
422 ambiguous_flags = abbrev_flags;
423 }
424 if (!(flags & OPT_UNSET) && *arg_end)
425 p->opt = arg_end + 1;
426 abbrev_option = options;
427 abbrev_flags = flags;
428 continue;
429 }
430 /* negated and abbreviated very much? */
431 if (strstarts("no-", arg)) {
432 flags |= OPT_UNSET;
433 goto is_abbreviated;
434 }
435 /* negated? */
436 if (strncmp(arg, "no-", 3))
437 continue;
438 flags |= OPT_UNSET;
439 rest = skip_prefix(arg + 3, options->long_name);
440 /* abbreviated and negated? */
441 if (!rest && strstarts(options->long_name, arg + 3))
442 goto is_abbreviated;
443 if (!rest)
444 continue;
445 }
446match:
447 if (*rest) {
448 if (*rest != '=')
449 continue;
450 p->opt = rest + 1;
451 }
452 return get_value(p, options, flags);
453 }
454
455 if (ambiguous_option) {
456 fprintf(stderr,
457 " Error: Ambiguous option: %s (could be --%s%s or --%s%s)\n",
458 arg,
459 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
460 ambiguous_option->long_name,
461 (abbrev_flags & OPT_UNSET) ? "no-" : "",
462 abbrev_option->long_name);
463 return -1;
464 }
465 if (abbrev_option)
466 return get_value(p, abbrev_option, abbrev_flags);
467
468 if (options->parent) {
469 options = options->parent;
470 goto retry;
471 }
472
473 return -2;
474}
475
476static void check_typos(const char *arg, const struct option *options)
477{
478 if (strlen(arg) < 3)
479 return;
480
481 if (strstarts(arg, "no-")) {
482 fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)\n", arg);
483 exit(129);
484 }
485
486 for (; options->type != OPTION_END; options++) {
487 if (!options->long_name)
488 continue;
489 if (strstarts(options->long_name, arg)) {
490 fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)\n", arg);
491 exit(129);
492 }
493 }
494}
495
496static void parse_options_start(struct parse_opt_ctx_t *ctx,
497 int argc, const char **argv, int flags)
498{
499 memset(ctx, 0, sizeof(*ctx));
500 ctx->argc = argc - 1;
501 ctx->argv = argv + 1;
502 ctx->out = argv;
503 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
504 ctx->flags = flags;
505 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
506 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
507 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
508}
509
510static int usage_with_options_internal(const char * const *,
511 const struct option *, int,
512 struct parse_opt_ctx_t *);
513
514static int parse_options_step(struct parse_opt_ctx_t *ctx,
515 const struct option *options,
516 const char * const usagestr[])
517{
518 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
519 int excl_short_opt = 1;
520 const char *arg;
521
522 /* we must reset ->opt, unknown short option leave it dangling */
523 ctx->opt = NULL;
524
525 for (; ctx->argc; ctx->argc--, ctx->argv++) {
526 arg = ctx->argv[0];
527 if (*arg != '-' || !arg[1]) {
528 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
529 break;
530 ctx->out[ctx->cpidx++] = ctx->argv[0];
531 continue;
532 }
533
534 if (arg[1] != '-') {
535 ctx->opt = ++arg;
536 if (internal_help && *ctx->opt == 'h') {
537 return usage_with_options_internal(usagestr, options, 0, ctx);
538 }
539 switch (parse_short_opt(ctx, options)) {
540 case -1:
541 return parse_options_usage(usagestr, options, arg, 1);
542 case -2:
543 goto unknown;
544 case -3:
545 goto exclusive;
546 default:
547 break;
548 }
549 if (ctx->opt)
550 check_typos(arg, options);
551 while (ctx->opt) {
552 if (internal_help && *ctx->opt == 'h')
553 return usage_with_options_internal(usagestr, options, 0, ctx);
554 arg = ctx->opt;
555 switch (parse_short_opt(ctx, options)) {
556 case -1:
557 return parse_options_usage(usagestr, options, arg, 1);
558 case -2:
559 /* fake a short option thing to hide the fact that we may have
560 * started to parse aggregated stuff
561 *
562 * This is leaky, too bad.
563 */
564 ctx->argv[0] = strdup(ctx->opt - 1);
565 *(char *)ctx->argv[0] = '-';
566 goto unknown;
567 case -3:
568 goto exclusive;
569 default:
570 break;
571 }
572 }
573 continue;
574 }
575
576 if (!arg[2]) { /* "--" */
577 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
578 ctx->argc--;
579 ctx->argv++;
580 }
581 break;
582 }
583
584 arg += 2;
585 if (internal_help && !strcmp(arg, "help-all"))
586 return usage_with_options_internal(usagestr, options, 1, ctx);
587 if (internal_help && !strcmp(arg, "help"))
588 return usage_with_options_internal(usagestr, options, 0, ctx);
589 if (!strcmp(arg, "list-opts"))
590 return PARSE_OPT_LIST_OPTS;
591 if (!strcmp(arg, "list-cmds"))
592 return PARSE_OPT_LIST_SUBCMDS;
593 switch (parse_long_opt(ctx, arg, options)) {
594 case -1:
595 return parse_options_usage(usagestr, options, arg, 0);
596 case -2:
597 goto unknown;
598 case -3:
599 excl_short_opt = 0;
600 goto exclusive;
601 default:
602 break;
603 }
604 continue;
605unknown:
606 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
607 return PARSE_OPT_UNKNOWN;
608 ctx->out[ctx->cpidx++] = ctx->argv[0];
609 ctx->opt = NULL;
610 }
611 return PARSE_OPT_DONE;
612
613exclusive:
614 parse_options_usage(usagestr, options, arg, excl_short_opt);
615 if ((excl_short_opt && ctx->excl_opt->short_name) ||
616 ctx->excl_opt->long_name == NULL) {
617 char opt = ctx->excl_opt->short_name;
618 parse_options_usage(NULL, options, &opt, 1);
619 } else {
620 parse_options_usage(NULL, options, ctx->excl_opt->long_name, 0);
621 }
622 return PARSE_OPT_HELP;
623}
624
625static int parse_options_end(struct parse_opt_ctx_t *ctx)
626{
627 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
628 ctx->out[ctx->cpidx + ctx->argc] = NULL;
629 return ctx->cpidx + ctx->argc;
630}
631
632int parse_options_subcommand(int argc, const char **argv, const struct option *options,
633 const char *const subcommands[], const char *usagestr[], int flags)
634{
635 struct parse_opt_ctx_t ctx;
636
637 /* build usage string if it's not provided */
638 if (subcommands && !usagestr[0]) {
639 char *buf = NULL;
640
641 astrcatf(&buf, "%s %s [<options>] {", subcmd_config.exec_name, argv[0]);
642
643 for (int i = 0; subcommands[i]; i++) {
644 if (i)
645 astrcat(&buf, "|");
646 astrcat(&buf, subcommands[i]);
647 }
648 astrcat(&buf, "}");
649
650 usagestr[0] = buf;
651 }
652
653 parse_options_start(&ctx, argc, argv, flags);
654 switch (parse_options_step(&ctx, options, usagestr)) {
655 case PARSE_OPT_HELP:
656 exit(129);
657 case PARSE_OPT_DONE:
658 break;
659 case PARSE_OPT_LIST_OPTS:
660 while (options->type != OPTION_END) {
661 if (options->long_name)
662 printf("--%s ", options->long_name);
663 options++;
664 }
665 putchar('\n');
666 exit(130);
667 case PARSE_OPT_LIST_SUBCMDS:
668 if (subcommands) {
669 for (int i = 0; subcommands[i]; i++)
670 printf("%s ", subcommands[i]);
671 }
672 putchar('\n');
673 exit(130);
674 default: /* PARSE_OPT_UNKNOWN */
675 if (ctx.argv[0][1] == '-')
676 astrcatf(&error_buf, "unknown option `%s'",
677 ctx.argv[0] + 2);
678 else
679 astrcatf(&error_buf, "unknown switch `%c'", *ctx.opt);
680 usage_with_options(usagestr, options);
681 }
682
683 return parse_options_end(&ctx);
684}
685
686int parse_options(int argc, const char **argv, const struct option *options,
687 const char * const usagestr[], int flags)
688{
689 return parse_options_subcommand(argc, argv, options, NULL,
690 (const char **) usagestr, flags);
691}
692
693#define USAGE_OPTS_WIDTH 24
694#define USAGE_GAP 2
695
696static void print_option_help(const struct option *opts, int full)
697{
698 size_t pos;
699 int pad;
700
701 if (opts->type == OPTION_GROUP) {
702 fputc('\n', stderr);
703 if (*opts->help)
704 fprintf(stderr, "%s\n", opts->help);
705 return;
706 }
707 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
708 return;
709 if (opts->flags & PARSE_OPT_DISABLED)
710 return;
711
712 pos = fprintf(stderr, " ");
713 if (opts->short_name)
714 pos += fprintf(stderr, "-%c", opts->short_name);
715 else
716 pos += fprintf(stderr, " ");
717
718 if (opts->long_name && opts->short_name)
719 pos += fprintf(stderr, ", ");
720 if (opts->long_name)
721 pos += fprintf(stderr, "--%s", opts->long_name);
722
723 switch (opts->type) {
724 case OPTION_ARGUMENT:
725 break;
726 case OPTION_LONG:
727 case OPTION_ULONG:
728 case OPTION_U64:
729 case OPTION_INTEGER:
730 case OPTION_UINTEGER:
731 if (opts->flags & PARSE_OPT_OPTARG)
732 if (opts->long_name)
733 pos += fprintf(stderr, "[=<n>]");
734 else
735 pos += fprintf(stderr, "[<n>]");
736 else
737 pos += fprintf(stderr, " <n>");
738 break;
739 case OPTION_CALLBACK:
740 if (opts->flags & PARSE_OPT_NOARG)
741 break;
742 /* FALLTHROUGH */
743 case OPTION_STRING:
744 if (opts->argh) {
745 if (opts->flags & PARSE_OPT_OPTARG)
746 if (opts->long_name)
747 pos += fprintf(stderr, "[=<%s>]", opts->argh);
748 else
749 pos += fprintf(stderr, "[<%s>]", opts->argh);
750 else
751 pos += fprintf(stderr, " <%s>", opts->argh);
752 } else {
753 if (opts->flags & PARSE_OPT_OPTARG)
754 if (opts->long_name)
755 pos += fprintf(stderr, "[=...]");
756 else
757 pos += fprintf(stderr, "[...]");
758 else
759 pos += fprintf(stderr, " ...");
760 }
761 break;
762 default: /* OPTION_{BIT,BOOLEAN,SET_UINT,SET_PTR} */
763 case OPTION_END:
764 case OPTION_GROUP:
765 case OPTION_BIT:
766 case OPTION_BOOLEAN:
767 case OPTION_INCR:
768 case OPTION_SET_UINT:
769 case OPTION_SET_PTR:
770 break;
771 }
772
773 if (pos <= USAGE_OPTS_WIDTH)
774 pad = USAGE_OPTS_WIDTH - pos;
775 else {
776 fputc('\n', stderr);
777 pad = USAGE_OPTS_WIDTH;
778 }
779 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
780 if (opts->flags & PARSE_OPT_NOBUILD)
781 fprintf(stderr, "%*s(not built-in because %s)\n",
782 USAGE_OPTS_WIDTH + USAGE_GAP, "",
783 opts->build_opt);
784}
785
786static int option__cmp(const void *va, const void *vb)
787{
788 const struct option *a = va, *b = vb;
789 int sa = tolower(a->short_name), sb = tolower(b->short_name), ret;
790
791 if (sa == 0)
792 sa = 'z' + 1;
793 if (sb == 0)
794 sb = 'z' + 1;
795
796 ret = sa - sb;
797
798 if (ret == 0) {
799 const char *la = a->long_name ?: "",
800 *lb = b->long_name ?: "";
801 ret = strcmp(la, lb);
802 }
803
804 return ret;
805}
806
807static struct option *options__order(const struct option *opts)
808{
809 int nr_opts = 0, nr_group = 0, len;
810 const struct option *o = opts;
811 struct option *opt, *ordered, *group;
812
813 for (o = opts; o->type != OPTION_END; o++)
814 ++nr_opts;
815
816 len = sizeof(*o) * (nr_opts + 1);
817 ordered = malloc(len);
818 if (!ordered)
819 goto out;
820 memcpy(ordered, opts, len);
821
822 /* sort each option group individually */
823 for (opt = group = ordered; opt->type != OPTION_END; opt++) {
824 if (opt->type == OPTION_GROUP) {
825 qsort(group, nr_group, sizeof(*opt), option__cmp);
826 group = opt + 1;
827 nr_group = 0;
828 continue;
829 }
830 nr_group++;
831 }
832 qsort(group, nr_group, sizeof(*opt), option__cmp);
833
834out:
835 return ordered;
836}
837
838static bool option__in_argv(const struct option *opt, const struct parse_opt_ctx_t *ctx)
839{
840 int i;
841
842 for (i = 1; i < ctx->argc; ++i) {
843 const char *arg = ctx->argv[i];
844
845 if (arg[0] != '-') {
846 if (arg[1] == '\0') {
847 if (arg[0] == opt->short_name)
848 return true;
849 continue;
850 }
851
852 if (opt->long_name && strcmp(opt->long_name, arg) == 0)
853 return true;
854
855 if (opt->help && strcasestr(opt->help, arg) != NULL)
856 return true;
857
858 continue;
859 }
860
861 if (arg[1] == opt->short_name ||
862 (arg[1] == '-' && opt->long_name && strcmp(opt->long_name, arg + 2) == 0))
863 return true;
864 }
865
866 return false;
867}
868
869static int usage_with_options_internal(const char * const *usagestr,
870 const struct option *opts, int full,
871 struct parse_opt_ctx_t *ctx)
872{
873 struct option *ordered;
874
875 if (!usagestr)
876 return PARSE_OPT_HELP;
877
878 setup_pager();
879
880 if (error_buf) {
881 fprintf(stderr, " Error: %s\n", error_buf);
882 zfree(&error_buf);
883 }
884
885 fprintf(stderr, "\n Usage: %s\n", *usagestr++);
886 while (*usagestr && **usagestr)
887 fprintf(stderr, " or: %s\n", *usagestr++);
888 while (*usagestr) {
889 fprintf(stderr, "%s%s\n",
890 **usagestr ? " " : "",
891 *usagestr);
892 usagestr++;
893 }
894
895 if (opts->type != OPTION_GROUP)
896 fputc('\n', stderr);
897
898 ordered = options__order(opts);
899 if (ordered)
900 opts = ordered;
901
902 for ( ; opts->type != OPTION_END; opts++) {
903 if (ctx && ctx->argc > 1 && !option__in_argv(opts, ctx))
904 continue;
905 print_option_help(opts, full);
906 }
907
908 fputc('\n', stderr);
909
910 free(ordered);
911
912 return PARSE_OPT_HELP;
913}
914
915void usage_with_options(const char * const *usagestr,
916 const struct option *opts)
917{
918 usage_with_options_internal(usagestr, opts, 0, NULL);
919 exit(129);
920}
921
922void usage_with_options_msg(const char * const *usagestr,
923 const struct option *opts, const char *fmt, ...)
924{
925 va_list ap;
926 char *tmp = error_buf;
927
928 va_start(ap, fmt);
929 if (vasprintf(&error_buf, fmt, ap) == -1)
930 die("vasprintf failed");
931 va_end(ap);
932
933 free(tmp);
934
935 usage_with_options_internal(usagestr, opts, 0, NULL);
936 exit(129);
937}
938
939int parse_options_usage(const char * const *usagestr,
940 const struct option *opts,
941 const char *optstr, bool short_opt)
942{
943 if (!usagestr)
944 goto opt;
945
946 fprintf(stderr, "\n Usage: %s\n", *usagestr++);
947 while (*usagestr && **usagestr)
948 fprintf(stderr, " or: %s\n", *usagestr++);
949 while (*usagestr) {
950 fprintf(stderr, "%s%s\n",
951 **usagestr ? " " : "",
952 *usagestr);
953 usagestr++;
954 }
955 fputc('\n', stderr);
956
957opt:
958 for ( ; opts->type != OPTION_END; opts++) {
959 if (short_opt) {
960 if (opts->short_name == *optstr) {
961 print_option_help(opts, 0);
962 break;
963 }
964 continue;
965 }
966
967 if (opts->long_name == NULL)
968 continue;
969
970 if (strstarts(opts->long_name, optstr))
971 print_option_help(opts, 0);
972 if (strstarts("no-", optstr) &&
973 strstarts(opts->long_name, optstr + 3))
974 print_option_help(opts, 0);
975 }
976
977 return PARSE_OPT_HELP;
978}
979
980
981int parse_opt_verbosity_cb(const struct option *opt,
982 const char *arg __maybe_unused,
983 int unset)
984{
985 int *target = opt->value;
986
987 if (unset)
988 /* --no-quiet, --no-verbose */
989 *target = 0;
990 else if (opt->short_name == 'v') {
991 if (*target >= 0)
992 (*target)++;
993 else
994 *target = 1;
995 } else {
996 if (*target <= 0)
997 (*target)--;
998 else
999 *target = -1;
1000 }
1001 return 0;
1002}
1003
1004static struct option *
1005find_option(struct option *opts, int shortopt, const char *longopt)
1006{
1007 for (; opts->type != OPTION_END; opts++) {
1008 if ((shortopt && opts->short_name == shortopt) ||
1009 (opts->long_name && longopt &&
1010 !strcmp(opts->long_name, longopt)))
1011 return opts;
1012 }
1013 return NULL;
1014}
1015
1016void set_option_flag(struct option *opts, int shortopt, const char *longopt,
1017 int flag)
1018{
1019 struct option *opt = find_option(opts, shortopt, longopt);
1020
1021 if (opt)
1022 opt->flags |= flag;
1023 return;
1024}
1025
1026void set_option_nobuild(struct option *opts, int shortopt,
1027 const char *longopt,
1028 const char *build_opt,
1029 bool can_skip)
1030{
1031 struct option *opt = find_option(opts, shortopt, longopt);
1032
1033 if (!opt)
1034 return;
1035
1036 opt->flags |= PARSE_OPT_NOBUILD;
1037 opt->flags |= can_skip ? PARSE_OPT_CANSKIP : 0;
1038 opt->build_opt = build_opt;
1039}
1#include <linux/compiler.h>
2#include <linux/types.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <stdint.h>
6#include <string.h>
7#include <ctype.h>
8#include "subcmd-util.h"
9#include "parse-options.h"
10#include "subcmd-config.h"
11#include "pager.h"
12
13#define OPT_SHORT 1
14#define OPT_UNSET 2
15
16char *error_buf;
17
18static int opterror(const struct option *opt, const char *reason, int flags)
19{
20 if (flags & OPT_SHORT)
21 fprintf(stderr, " Error: switch `%c' %s", opt->short_name, reason);
22 else if (flags & OPT_UNSET)
23 fprintf(stderr, " Error: option `no-%s' %s", opt->long_name, reason);
24 else
25 fprintf(stderr, " Error: option `%s' %s", opt->long_name, reason);
26
27 return -1;
28}
29
30static const char *skip_prefix(const char *str, const char *prefix)
31{
32 size_t len = strlen(prefix);
33 return strncmp(str, prefix, len) ? NULL : str + len;
34}
35
36static void optwarning(const struct option *opt, const char *reason, int flags)
37{
38 if (flags & OPT_SHORT)
39 fprintf(stderr, " Warning: switch `%c' %s", opt->short_name, reason);
40 else if (flags & OPT_UNSET)
41 fprintf(stderr, " Warning: option `no-%s' %s", opt->long_name, reason);
42 else
43 fprintf(stderr, " Warning: option `%s' %s", opt->long_name, reason);
44}
45
46static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
47 int flags, const char **arg)
48{
49 const char *res;
50
51 if (p->opt) {
52 res = p->opt;
53 p->opt = NULL;
54 } else if ((opt->flags & PARSE_OPT_LASTARG_DEFAULT) && (p->argc == 1 ||
55 **(p->argv + 1) == '-')) {
56 res = (const char *)opt->defval;
57 } else if (p->argc > 1) {
58 p->argc--;
59 res = *++p->argv;
60 } else
61 return opterror(opt, "requires a value", flags);
62 if (arg)
63 *arg = res;
64 return 0;
65}
66
67static int get_value(struct parse_opt_ctx_t *p,
68 const struct option *opt, int flags)
69{
70 const char *s, *arg = NULL;
71 const int unset = flags & OPT_UNSET;
72 int err;
73
74 if (unset && p->opt)
75 return opterror(opt, "takes no value", flags);
76 if (unset && (opt->flags & PARSE_OPT_NONEG))
77 return opterror(opt, "isn't available", flags);
78 if (opt->flags & PARSE_OPT_DISABLED)
79 return opterror(opt, "is not usable", flags);
80
81 if (opt->flags & PARSE_OPT_EXCLUSIVE) {
82 if (p->excl_opt && p->excl_opt != opt) {
83 char msg[128];
84
85 if (((flags & OPT_SHORT) && p->excl_opt->short_name) ||
86 p->excl_opt->long_name == NULL) {
87 snprintf(msg, sizeof(msg), "cannot be used with switch `%c'",
88 p->excl_opt->short_name);
89 } else {
90 snprintf(msg, sizeof(msg), "cannot be used with %s",
91 p->excl_opt->long_name);
92 }
93 opterror(opt, msg, flags);
94 return -3;
95 }
96 p->excl_opt = opt;
97 }
98 if (!(flags & OPT_SHORT) && p->opt) {
99 switch (opt->type) {
100 case OPTION_CALLBACK:
101 if (!(opt->flags & PARSE_OPT_NOARG))
102 break;
103 /* FALLTHROUGH */
104 case OPTION_BOOLEAN:
105 case OPTION_INCR:
106 case OPTION_BIT:
107 case OPTION_SET_UINT:
108 case OPTION_SET_PTR:
109 return opterror(opt, "takes no value", flags);
110 case OPTION_END:
111 case OPTION_ARGUMENT:
112 case OPTION_GROUP:
113 case OPTION_STRING:
114 case OPTION_INTEGER:
115 case OPTION_UINTEGER:
116 case OPTION_LONG:
117 case OPTION_U64:
118 default:
119 break;
120 }
121 }
122
123 if (opt->flags & PARSE_OPT_NOBUILD) {
124 char reason[128];
125 bool noarg = false;
126
127 err = snprintf(reason, sizeof(reason),
128 opt->flags & PARSE_OPT_CANSKIP ?
129 "is being ignored because %s " :
130 "is not available because %s",
131 opt->build_opt);
132 reason[sizeof(reason) - 1] = '\0';
133
134 if (err < 0)
135 strncpy(reason, opt->flags & PARSE_OPT_CANSKIP ?
136 "is being ignored" :
137 "is not available",
138 sizeof(reason));
139
140 if (!(opt->flags & PARSE_OPT_CANSKIP))
141 return opterror(opt, reason, flags);
142
143 err = 0;
144 if (unset)
145 noarg = true;
146 if (opt->flags & PARSE_OPT_NOARG)
147 noarg = true;
148 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
149 noarg = true;
150
151 switch (opt->type) {
152 case OPTION_BOOLEAN:
153 case OPTION_INCR:
154 case OPTION_BIT:
155 case OPTION_SET_UINT:
156 case OPTION_SET_PTR:
157 case OPTION_END:
158 case OPTION_ARGUMENT:
159 case OPTION_GROUP:
160 noarg = true;
161 break;
162 case OPTION_CALLBACK:
163 case OPTION_STRING:
164 case OPTION_INTEGER:
165 case OPTION_UINTEGER:
166 case OPTION_LONG:
167 case OPTION_U64:
168 default:
169 break;
170 }
171
172 if (!noarg)
173 err = get_arg(p, opt, flags, NULL);
174 if (err)
175 return err;
176
177 optwarning(opt, reason, flags);
178 return 0;
179 }
180
181 switch (opt->type) {
182 case OPTION_BIT:
183 if (unset)
184 *(int *)opt->value &= ~opt->defval;
185 else
186 *(int *)opt->value |= opt->defval;
187 return 0;
188
189 case OPTION_BOOLEAN:
190 *(bool *)opt->value = unset ? false : true;
191 if (opt->set)
192 *(bool *)opt->set = true;
193 return 0;
194
195 case OPTION_INCR:
196 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
197 return 0;
198
199 case OPTION_SET_UINT:
200 *(unsigned int *)opt->value = unset ? 0 : opt->defval;
201 return 0;
202
203 case OPTION_SET_PTR:
204 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
205 return 0;
206
207 case OPTION_STRING:
208 err = 0;
209 if (unset)
210 *(const char **)opt->value = NULL;
211 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
212 *(const char **)opt->value = (const char *)opt->defval;
213 else
214 err = get_arg(p, opt, flags, (const char **)opt->value);
215
216 /* PARSE_OPT_NOEMPTY: Allow NULL but disallow empty string. */
217 if (opt->flags & PARSE_OPT_NOEMPTY) {
218 const char *val = *(const char **)opt->value;
219
220 if (!val)
221 return err;
222
223 /* Similar to unset if we are given an empty string. */
224 if (val[0] == '\0') {
225 *(const char **)opt->value = NULL;
226 return 0;
227 }
228 }
229
230 return err;
231
232 case OPTION_CALLBACK:
233 if (unset)
234 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
235 if (opt->flags & PARSE_OPT_NOARG)
236 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
237 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
238 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
239 if (get_arg(p, opt, flags, &arg))
240 return -1;
241 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
242
243 case OPTION_INTEGER:
244 if (unset) {
245 *(int *)opt->value = 0;
246 return 0;
247 }
248 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
249 *(int *)opt->value = opt->defval;
250 return 0;
251 }
252 if (get_arg(p, opt, flags, &arg))
253 return -1;
254 *(int *)opt->value = strtol(arg, (char **)&s, 10);
255 if (*s)
256 return opterror(opt, "expects a numerical value", flags);
257 return 0;
258
259 case OPTION_UINTEGER:
260 if (unset) {
261 *(unsigned int *)opt->value = 0;
262 return 0;
263 }
264 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
265 *(unsigned int *)opt->value = opt->defval;
266 return 0;
267 }
268 if (get_arg(p, opt, flags, &arg))
269 return -1;
270 *(unsigned int *)opt->value = strtol(arg, (char **)&s, 10);
271 if (*s)
272 return opterror(opt, "expects a numerical value", flags);
273 return 0;
274
275 case OPTION_LONG:
276 if (unset) {
277 *(long *)opt->value = 0;
278 return 0;
279 }
280 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
281 *(long *)opt->value = opt->defval;
282 return 0;
283 }
284 if (get_arg(p, opt, flags, &arg))
285 return -1;
286 *(long *)opt->value = strtol(arg, (char **)&s, 10);
287 if (*s)
288 return opterror(opt, "expects a numerical value", flags);
289 return 0;
290
291 case OPTION_U64:
292 if (unset) {
293 *(u64 *)opt->value = 0;
294 return 0;
295 }
296 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
297 *(u64 *)opt->value = opt->defval;
298 return 0;
299 }
300 if (get_arg(p, opt, flags, &arg))
301 return -1;
302 *(u64 *)opt->value = strtoull(arg, (char **)&s, 10);
303 if (*s)
304 return opterror(opt, "expects a numerical value", flags);
305 return 0;
306
307 case OPTION_END:
308 case OPTION_ARGUMENT:
309 case OPTION_GROUP:
310 default:
311 die("should not happen, someone must be hit on the forehead");
312 }
313}
314
315static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
316{
317 for (; options->type != OPTION_END; options++) {
318 if (options->short_name == *p->opt) {
319 p->opt = p->opt[1] ? p->opt + 1 : NULL;
320 return get_value(p, options, OPT_SHORT);
321 }
322 }
323 return -2;
324}
325
326static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
327 const struct option *options)
328{
329 const char *arg_end = strchr(arg, '=');
330 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
331 int abbrev_flags = 0, ambiguous_flags = 0;
332
333 if (!arg_end)
334 arg_end = arg + strlen(arg);
335
336 for (; options->type != OPTION_END; options++) {
337 const char *rest;
338 int flags = 0;
339
340 if (!options->long_name)
341 continue;
342
343 rest = skip_prefix(arg, options->long_name);
344 if (options->type == OPTION_ARGUMENT) {
345 if (!rest)
346 continue;
347 if (*rest == '=')
348 return opterror(options, "takes no value", flags);
349 if (*rest)
350 continue;
351 p->out[p->cpidx++] = arg - 2;
352 return 0;
353 }
354 if (!rest) {
355 if (!prefixcmp(options->long_name, "no-")) {
356 /*
357 * The long name itself starts with "no-", so
358 * accept the option without "no-" so that users
359 * do not have to enter "no-no-" to get the
360 * negation.
361 */
362 rest = skip_prefix(arg, options->long_name + 3);
363 if (rest) {
364 flags |= OPT_UNSET;
365 goto match;
366 }
367 /* Abbreviated case */
368 if (!prefixcmp(options->long_name + 3, arg)) {
369 flags |= OPT_UNSET;
370 goto is_abbreviated;
371 }
372 }
373 /* abbreviated? */
374 if (!strncmp(options->long_name, arg, arg_end - arg)) {
375is_abbreviated:
376 if (abbrev_option) {
377 /*
378 * If this is abbreviated, it is
379 * ambiguous. So when there is no
380 * exact match later, we need to
381 * error out.
382 */
383 ambiguous_option = abbrev_option;
384 ambiguous_flags = abbrev_flags;
385 }
386 if (!(flags & OPT_UNSET) && *arg_end)
387 p->opt = arg_end + 1;
388 abbrev_option = options;
389 abbrev_flags = flags;
390 continue;
391 }
392 /* negated and abbreviated very much? */
393 if (!prefixcmp("no-", arg)) {
394 flags |= OPT_UNSET;
395 goto is_abbreviated;
396 }
397 /* negated? */
398 if (strncmp(arg, "no-", 3))
399 continue;
400 flags |= OPT_UNSET;
401 rest = skip_prefix(arg + 3, options->long_name);
402 /* abbreviated and negated? */
403 if (!rest && !prefixcmp(options->long_name, arg + 3))
404 goto is_abbreviated;
405 if (!rest)
406 continue;
407 }
408match:
409 if (*rest) {
410 if (*rest != '=')
411 continue;
412 p->opt = rest + 1;
413 }
414 return get_value(p, options, flags);
415 }
416
417 if (ambiguous_option) {
418 fprintf(stderr,
419 " Error: Ambiguous option: %s (could be --%s%s or --%s%s)",
420 arg,
421 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
422 ambiguous_option->long_name,
423 (abbrev_flags & OPT_UNSET) ? "no-" : "",
424 abbrev_option->long_name);
425 return -1;
426 }
427 if (abbrev_option)
428 return get_value(p, abbrev_option, abbrev_flags);
429 return -2;
430}
431
432static void check_typos(const char *arg, const struct option *options)
433{
434 if (strlen(arg) < 3)
435 return;
436
437 if (!prefixcmp(arg, "no-")) {
438 fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)", arg);
439 exit(129);
440 }
441
442 for (; options->type != OPTION_END; options++) {
443 if (!options->long_name)
444 continue;
445 if (!prefixcmp(options->long_name, arg)) {
446 fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)", arg);
447 exit(129);
448 }
449 }
450}
451
452static void parse_options_start(struct parse_opt_ctx_t *ctx,
453 int argc, const char **argv, int flags)
454{
455 memset(ctx, 0, sizeof(*ctx));
456 ctx->argc = argc - 1;
457 ctx->argv = argv + 1;
458 ctx->out = argv;
459 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
460 ctx->flags = flags;
461 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
462 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
463 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
464}
465
466static int usage_with_options_internal(const char * const *,
467 const struct option *, int,
468 struct parse_opt_ctx_t *);
469
470static int parse_options_step(struct parse_opt_ctx_t *ctx,
471 const struct option *options,
472 const char * const usagestr[])
473{
474 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
475 int excl_short_opt = 1;
476 const char *arg;
477
478 /* we must reset ->opt, unknown short option leave it dangling */
479 ctx->opt = NULL;
480
481 for (; ctx->argc; ctx->argc--, ctx->argv++) {
482 arg = ctx->argv[0];
483 if (*arg != '-' || !arg[1]) {
484 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
485 break;
486 ctx->out[ctx->cpidx++] = ctx->argv[0];
487 continue;
488 }
489
490 if (arg[1] != '-') {
491 ctx->opt = ++arg;
492 if (internal_help && *ctx->opt == 'h') {
493 return usage_with_options_internal(usagestr, options, 0, ctx);
494 }
495 switch (parse_short_opt(ctx, options)) {
496 case -1:
497 return parse_options_usage(usagestr, options, arg, 1);
498 case -2:
499 goto unknown;
500 case -3:
501 goto exclusive;
502 default:
503 break;
504 }
505 if (ctx->opt)
506 check_typos(arg, options);
507 while (ctx->opt) {
508 if (internal_help && *ctx->opt == 'h')
509 return usage_with_options_internal(usagestr, options, 0, ctx);
510 arg = ctx->opt;
511 switch (parse_short_opt(ctx, options)) {
512 case -1:
513 return parse_options_usage(usagestr, options, arg, 1);
514 case -2:
515 /* fake a short option thing to hide the fact that we may have
516 * started to parse aggregated stuff
517 *
518 * This is leaky, too bad.
519 */
520 ctx->argv[0] = strdup(ctx->opt - 1);
521 *(char *)ctx->argv[0] = '-';
522 goto unknown;
523 case -3:
524 goto exclusive;
525 default:
526 break;
527 }
528 }
529 continue;
530 }
531
532 if (!arg[2]) { /* "--" */
533 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
534 ctx->argc--;
535 ctx->argv++;
536 }
537 break;
538 }
539
540 arg += 2;
541 if (internal_help && !strcmp(arg, "help-all"))
542 return usage_with_options_internal(usagestr, options, 1, ctx);
543 if (internal_help && !strcmp(arg, "help"))
544 return usage_with_options_internal(usagestr, options, 0, ctx);
545 if (!strcmp(arg, "list-opts"))
546 return PARSE_OPT_LIST_OPTS;
547 if (!strcmp(arg, "list-cmds"))
548 return PARSE_OPT_LIST_SUBCMDS;
549 switch (parse_long_opt(ctx, arg, options)) {
550 case -1:
551 return parse_options_usage(usagestr, options, arg, 0);
552 case -2:
553 goto unknown;
554 case -3:
555 excl_short_opt = 0;
556 goto exclusive;
557 default:
558 break;
559 }
560 continue;
561unknown:
562 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
563 return PARSE_OPT_UNKNOWN;
564 ctx->out[ctx->cpidx++] = ctx->argv[0];
565 ctx->opt = NULL;
566 }
567 return PARSE_OPT_DONE;
568
569exclusive:
570 parse_options_usage(usagestr, options, arg, excl_short_opt);
571 if ((excl_short_opt && ctx->excl_opt->short_name) ||
572 ctx->excl_opt->long_name == NULL) {
573 char opt = ctx->excl_opt->short_name;
574 parse_options_usage(NULL, options, &opt, 1);
575 } else {
576 parse_options_usage(NULL, options, ctx->excl_opt->long_name, 0);
577 }
578 return PARSE_OPT_HELP;
579}
580
581static int parse_options_end(struct parse_opt_ctx_t *ctx)
582{
583 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
584 ctx->out[ctx->cpidx + ctx->argc] = NULL;
585 return ctx->cpidx + ctx->argc;
586}
587
588int parse_options_subcommand(int argc, const char **argv, const struct option *options,
589 const char *const subcommands[], const char *usagestr[], int flags)
590{
591 struct parse_opt_ctx_t ctx;
592
593 /* build usage string if it's not provided */
594 if (subcommands && !usagestr[0]) {
595 char *buf = NULL;
596
597 astrcatf(&buf, "%s %s [<options>] {", subcmd_config.exec_name, argv[0]);
598
599 for (int i = 0; subcommands[i]; i++) {
600 if (i)
601 astrcat(&buf, "|");
602 astrcat(&buf, subcommands[i]);
603 }
604 astrcat(&buf, "}");
605
606 usagestr[0] = buf;
607 }
608
609 parse_options_start(&ctx, argc, argv, flags);
610 switch (parse_options_step(&ctx, options, usagestr)) {
611 case PARSE_OPT_HELP:
612 exit(129);
613 case PARSE_OPT_DONE:
614 break;
615 case PARSE_OPT_LIST_OPTS:
616 while (options->type != OPTION_END) {
617 if (options->long_name)
618 printf("--%s ", options->long_name);
619 options++;
620 }
621 putchar('\n');
622 exit(130);
623 case PARSE_OPT_LIST_SUBCMDS:
624 if (subcommands) {
625 for (int i = 0; subcommands[i]; i++)
626 printf("%s ", subcommands[i]);
627 }
628 putchar('\n');
629 exit(130);
630 default: /* PARSE_OPT_UNKNOWN */
631 if (ctx.argv[0][1] == '-')
632 astrcatf(&error_buf, "unknown option `%s'",
633 ctx.argv[0] + 2);
634 else
635 astrcatf(&error_buf, "unknown switch `%c'", *ctx.opt);
636 usage_with_options(usagestr, options);
637 }
638
639 return parse_options_end(&ctx);
640}
641
642int parse_options(int argc, const char **argv, const struct option *options,
643 const char * const usagestr[], int flags)
644{
645 return parse_options_subcommand(argc, argv, options, NULL,
646 (const char **) usagestr, flags);
647}
648
649#define USAGE_OPTS_WIDTH 24
650#define USAGE_GAP 2
651
652static void print_option_help(const struct option *opts, int full)
653{
654 size_t pos;
655 int pad;
656
657 if (opts->type == OPTION_GROUP) {
658 fputc('\n', stderr);
659 if (*opts->help)
660 fprintf(stderr, "%s\n", opts->help);
661 return;
662 }
663 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
664 return;
665 if (opts->flags & PARSE_OPT_DISABLED)
666 return;
667
668 pos = fprintf(stderr, " ");
669 if (opts->short_name)
670 pos += fprintf(stderr, "-%c", opts->short_name);
671 else
672 pos += fprintf(stderr, " ");
673
674 if (opts->long_name && opts->short_name)
675 pos += fprintf(stderr, ", ");
676 if (opts->long_name)
677 pos += fprintf(stderr, "--%s", opts->long_name);
678
679 switch (opts->type) {
680 case OPTION_ARGUMENT:
681 break;
682 case OPTION_LONG:
683 case OPTION_U64:
684 case OPTION_INTEGER:
685 case OPTION_UINTEGER:
686 if (opts->flags & PARSE_OPT_OPTARG)
687 if (opts->long_name)
688 pos += fprintf(stderr, "[=<n>]");
689 else
690 pos += fprintf(stderr, "[<n>]");
691 else
692 pos += fprintf(stderr, " <n>");
693 break;
694 case OPTION_CALLBACK:
695 if (opts->flags & PARSE_OPT_NOARG)
696 break;
697 /* FALLTHROUGH */
698 case OPTION_STRING:
699 if (opts->argh) {
700 if (opts->flags & PARSE_OPT_OPTARG)
701 if (opts->long_name)
702 pos += fprintf(stderr, "[=<%s>]", opts->argh);
703 else
704 pos += fprintf(stderr, "[<%s>]", opts->argh);
705 else
706 pos += fprintf(stderr, " <%s>", opts->argh);
707 } else {
708 if (opts->flags & PARSE_OPT_OPTARG)
709 if (opts->long_name)
710 pos += fprintf(stderr, "[=...]");
711 else
712 pos += fprintf(stderr, "[...]");
713 else
714 pos += fprintf(stderr, " ...");
715 }
716 break;
717 default: /* OPTION_{BIT,BOOLEAN,SET_UINT,SET_PTR} */
718 case OPTION_END:
719 case OPTION_GROUP:
720 case OPTION_BIT:
721 case OPTION_BOOLEAN:
722 case OPTION_INCR:
723 case OPTION_SET_UINT:
724 case OPTION_SET_PTR:
725 break;
726 }
727
728 if (pos <= USAGE_OPTS_WIDTH)
729 pad = USAGE_OPTS_WIDTH - pos;
730 else {
731 fputc('\n', stderr);
732 pad = USAGE_OPTS_WIDTH;
733 }
734 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
735 if (opts->flags & PARSE_OPT_NOBUILD)
736 fprintf(stderr, "%*s(not built-in because %s)\n",
737 USAGE_OPTS_WIDTH + USAGE_GAP, "",
738 opts->build_opt);
739}
740
741static int option__cmp(const void *va, const void *vb)
742{
743 const struct option *a = va, *b = vb;
744 int sa = tolower(a->short_name), sb = tolower(b->short_name), ret;
745
746 if (sa == 0)
747 sa = 'z' + 1;
748 if (sb == 0)
749 sb = 'z' + 1;
750
751 ret = sa - sb;
752
753 if (ret == 0) {
754 const char *la = a->long_name ?: "",
755 *lb = b->long_name ?: "";
756 ret = strcmp(la, lb);
757 }
758
759 return ret;
760}
761
762static struct option *options__order(const struct option *opts)
763{
764 int nr_opts = 0, len;
765 const struct option *o = opts;
766 struct option *ordered;
767
768 for (o = opts; o->type != OPTION_END; o++)
769 ++nr_opts;
770
771 len = sizeof(*o) * (nr_opts + 1);
772 ordered = malloc(len);
773 if (!ordered)
774 goto out;
775 memcpy(ordered, opts, len);
776
777 qsort(ordered, nr_opts, sizeof(*o), option__cmp);
778out:
779 return ordered;
780}
781
782static bool option__in_argv(const struct option *opt, const struct parse_opt_ctx_t *ctx)
783{
784 int i;
785
786 for (i = 1; i < ctx->argc; ++i) {
787 const char *arg = ctx->argv[i];
788
789 if (arg[0] != '-') {
790 if (arg[1] == '\0') {
791 if (arg[0] == opt->short_name)
792 return true;
793 continue;
794 }
795
796 if (opt->long_name && strcmp(opt->long_name, arg) == 0)
797 return true;
798
799 if (opt->help && strcasestr(opt->help, arg) != NULL)
800 return true;
801
802 continue;
803 }
804
805 if (arg[1] == opt->short_name ||
806 (arg[1] == '-' && opt->long_name && strcmp(opt->long_name, arg + 2) == 0))
807 return true;
808 }
809
810 return false;
811}
812
813static int usage_with_options_internal(const char * const *usagestr,
814 const struct option *opts, int full,
815 struct parse_opt_ctx_t *ctx)
816{
817 struct option *ordered;
818
819 if (!usagestr)
820 return PARSE_OPT_HELP;
821
822 setup_pager();
823
824 if (error_buf) {
825 fprintf(stderr, " Error: %s\n", error_buf);
826 zfree(&error_buf);
827 }
828
829 fprintf(stderr, "\n Usage: %s\n", *usagestr++);
830 while (*usagestr && **usagestr)
831 fprintf(stderr, " or: %s\n", *usagestr++);
832 while (*usagestr) {
833 fprintf(stderr, "%s%s\n",
834 **usagestr ? " " : "",
835 *usagestr);
836 usagestr++;
837 }
838
839 if (opts->type != OPTION_GROUP)
840 fputc('\n', stderr);
841
842 ordered = options__order(opts);
843 if (ordered)
844 opts = ordered;
845
846 for ( ; opts->type != OPTION_END; opts++) {
847 if (ctx && ctx->argc > 1 && !option__in_argv(opts, ctx))
848 continue;
849 print_option_help(opts, full);
850 }
851
852 fputc('\n', stderr);
853
854 free(ordered);
855
856 return PARSE_OPT_HELP;
857}
858
859void usage_with_options(const char * const *usagestr,
860 const struct option *opts)
861{
862 usage_with_options_internal(usagestr, opts, 0, NULL);
863 exit(129);
864}
865
866void usage_with_options_msg(const char * const *usagestr,
867 const struct option *opts, const char *fmt, ...)
868{
869 va_list ap;
870 char *tmp = error_buf;
871
872 va_start(ap, fmt);
873 if (vasprintf(&error_buf, fmt, ap) == -1)
874 die("vasprintf failed");
875 va_end(ap);
876
877 free(tmp);
878
879 usage_with_options_internal(usagestr, opts, 0, NULL);
880 exit(129);
881}
882
883int parse_options_usage(const char * const *usagestr,
884 const struct option *opts,
885 const char *optstr, bool short_opt)
886{
887 if (!usagestr)
888 goto opt;
889
890 fprintf(stderr, "\n Usage: %s\n", *usagestr++);
891 while (*usagestr && **usagestr)
892 fprintf(stderr, " or: %s\n", *usagestr++);
893 while (*usagestr) {
894 fprintf(stderr, "%s%s\n",
895 **usagestr ? " " : "",
896 *usagestr);
897 usagestr++;
898 }
899 fputc('\n', stderr);
900
901opt:
902 for ( ; opts->type != OPTION_END; opts++) {
903 if (short_opt) {
904 if (opts->short_name == *optstr) {
905 print_option_help(opts, 0);
906 break;
907 }
908 continue;
909 }
910
911 if (opts->long_name == NULL)
912 continue;
913
914 if (!prefixcmp(opts->long_name, optstr))
915 print_option_help(opts, 0);
916 if (!prefixcmp("no-", optstr) &&
917 !prefixcmp(opts->long_name, optstr + 3))
918 print_option_help(opts, 0);
919 }
920
921 return PARSE_OPT_HELP;
922}
923
924
925int parse_opt_verbosity_cb(const struct option *opt,
926 const char *arg __maybe_unused,
927 int unset)
928{
929 int *target = opt->value;
930
931 if (unset)
932 /* --no-quiet, --no-verbose */
933 *target = 0;
934 else if (opt->short_name == 'v') {
935 if (*target >= 0)
936 (*target)++;
937 else
938 *target = 1;
939 } else {
940 if (*target <= 0)
941 (*target)--;
942 else
943 *target = -1;
944 }
945 return 0;
946}
947
948static struct option *
949find_option(struct option *opts, int shortopt, const char *longopt)
950{
951 for (; opts->type != OPTION_END; opts++) {
952 if ((shortopt && opts->short_name == shortopt) ||
953 (opts->long_name && longopt &&
954 !strcmp(opts->long_name, longopt)))
955 return opts;
956 }
957 return NULL;
958}
959
960void set_option_flag(struct option *opts, int shortopt, const char *longopt,
961 int flag)
962{
963 struct option *opt = find_option(opts, shortopt, longopt);
964
965 if (opt)
966 opt->flags |= flag;
967 return;
968}
969
970void set_option_nobuild(struct option *opts, int shortopt,
971 const char *longopt,
972 const char *build_opt,
973 bool can_skip)
974{
975 struct option *opt = find_option(opts, shortopt, longopt);
976
977 if (!opt)
978 return;
979
980 opt->flags |= PARSE_OPT_NOBUILD;
981 opt->flags |= can_skip ? PARSE_OPT_CANSKIP : 0;
982 opt->build_opt = build_opt;
983}