fb26532d67c39837e1d8718586c265be45b61b8b
[firefly-linux-kernel-4.4.55.git] / tools / perf / util / parse-options.c
1 #include "util.h"
2 #include "parse-options.h"
3 #include "cache.h"
4 #include "header.h"
5 #include <linux/string.h>
6
7 #define OPT_SHORT 1
8 #define OPT_UNSET 2
9
10 static int opterror(const struct option *opt, const char *reason, int flags)
11 {
12         if (flags & OPT_SHORT)
13                 return error("switch `%c' %s", opt->short_name, reason);
14         if (flags & OPT_UNSET)
15                 return error("option `no-%s' %s", opt->long_name, reason);
16         return error("option `%s' %s", opt->long_name, reason);
17 }
18
19 static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
20                    int flags, const char **arg)
21 {
22         if (p->opt) {
23                 *arg = p->opt;
24                 p->opt = NULL;
25         } else if ((opt->flags & PARSE_OPT_LASTARG_DEFAULT) && (p->argc == 1 ||
26                     **(p->argv + 1) == '-')) {
27                 *arg = (const char *)opt->defval;
28         } else if (p->argc > 1) {
29                 p->argc--;
30                 *arg = *++p->argv;
31         } else
32                 return opterror(opt, "requires a value", flags);
33         return 0;
34 }
35
36 static int get_value(struct parse_opt_ctx_t *p,
37                      const struct option *opt, int flags)
38 {
39         const char *s, *arg = NULL;
40         const int unset = flags & OPT_UNSET;
41         int err;
42
43         if (unset && p->opt)
44                 return opterror(opt, "takes no value", flags);
45         if (unset && (opt->flags & PARSE_OPT_NONEG))
46                 return opterror(opt, "isn't available", flags);
47         if (opt->flags & PARSE_OPT_DISABLED)
48                 return opterror(opt, "is not usable", flags);
49
50         if (opt->flags & PARSE_OPT_EXCLUSIVE) {
51                 if (p->excl_opt && p->excl_opt != opt) {
52                         char msg[128];
53
54                         if (((flags & OPT_SHORT) && p->excl_opt->short_name) ||
55                             p->excl_opt->long_name == NULL) {
56                                 scnprintf(msg, sizeof(msg), "cannot be used with switch `%c'",
57                                           p->excl_opt->short_name);
58                         } else {
59                                 scnprintf(msg, sizeof(msg), "cannot be used with %s",
60                                           p->excl_opt->long_name);
61                         }
62                         opterror(opt, msg, flags);
63                         return -3;
64                 }
65                 p->excl_opt = opt;
66         }
67         if (!(flags & OPT_SHORT) && p->opt) {
68                 switch (opt->type) {
69                 case OPTION_CALLBACK:
70                         if (!(opt->flags & PARSE_OPT_NOARG))
71                                 break;
72                         /* FALLTHROUGH */
73                 case OPTION_BOOLEAN:
74                 case OPTION_INCR:
75                 case OPTION_BIT:
76                 case OPTION_SET_UINT:
77                 case OPTION_SET_PTR:
78                         return opterror(opt, "takes no value", flags);
79                 case OPTION_END:
80                 case OPTION_ARGUMENT:
81                 case OPTION_GROUP:
82                 case OPTION_STRING:
83                 case OPTION_INTEGER:
84                 case OPTION_UINTEGER:
85                 case OPTION_LONG:
86                 case OPTION_U64:
87                 default:
88                         break;
89                 }
90         }
91
92         switch (opt->type) {
93         case OPTION_BIT:
94                 if (unset)
95                         *(int *)opt->value &= ~opt->defval;
96                 else
97                         *(int *)opt->value |= opt->defval;
98                 return 0;
99
100         case OPTION_BOOLEAN:
101                 *(bool *)opt->value = unset ? false : true;
102                 if (opt->set)
103                         *(bool *)opt->set = true;
104                 return 0;
105
106         case OPTION_INCR:
107                 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
108                 return 0;
109
110         case OPTION_SET_UINT:
111                 *(unsigned int *)opt->value = unset ? 0 : opt->defval;
112                 return 0;
113
114         case OPTION_SET_PTR:
115                 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
116                 return 0;
117
118         case OPTION_STRING:
119                 err = 0;
120                 if (unset)
121                         *(const char **)opt->value = NULL;
122                 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
123                         *(const char **)opt->value = (const char *)opt->defval;
124                 else
125                         err = get_arg(p, opt, flags, (const char **)opt->value);
126
127                 /* PARSE_OPT_NOEMPTY: Allow NULL but disallow empty string. */
128                 if (opt->flags & PARSE_OPT_NOEMPTY) {
129                         const char *val = *(const char **)opt->value;
130
131                         if (!val)
132                                 return err;
133
134                         /* Similar to unset if we are given an empty string. */
135                         if (val[0] == '\0') {
136                                 *(const char **)opt->value = NULL;
137                                 return 0;
138                         }
139                 }
140
141                 return err;
142
143         case OPTION_CALLBACK:
144                 if (unset)
145                         return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
146                 if (opt->flags & PARSE_OPT_NOARG)
147                         return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
148                 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
149                         return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
150                 if (get_arg(p, opt, flags, &arg))
151                         return -1;
152                 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
153
154         case OPTION_INTEGER:
155                 if (unset) {
156                         *(int *)opt->value = 0;
157                         return 0;
158                 }
159                 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
160                         *(int *)opt->value = opt->defval;
161                         return 0;
162                 }
163                 if (get_arg(p, opt, flags, &arg))
164                         return -1;
165                 *(int *)opt->value = strtol(arg, (char **)&s, 10);
166                 if (*s)
167                         return opterror(opt, "expects a numerical value", flags);
168                 return 0;
169
170         case OPTION_UINTEGER:
171                 if (unset) {
172                         *(unsigned int *)opt->value = 0;
173                         return 0;
174                 }
175                 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
176                         *(unsigned int *)opt->value = opt->defval;
177                         return 0;
178                 }
179                 if (get_arg(p, opt, flags, &arg))
180                         return -1;
181                 *(unsigned int *)opt->value = strtol(arg, (char **)&s, 10);
182                 if (*s)
183                         return opterror(opt, "expects a numerical value", flags);
184                 return 0;
185
186         case OPTION_LONG:
187                 if (unset) {
188                         *(long *)opt->value = 0;
189                         return 0;
190                 }
191                 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
192                         *(long *)opt->value = opt->defval;
193                         return 0;
194                 }
195                 if (get_arg(p, opt, flags, &arg))
196                         return -1;
197                 *(long *)opt->value = strtol(arg, (char **)&s, 10);
198                 if (*s)
199                         return opterror(opt, "expects a numerical value", flags);
200                 return 0;
201
202         case OPTION_U64:
203                 if (unset) {
204                         *(u64 *)opt->value = 0;
205                         return 0;
206                 }
207                 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
208                         *(u64 *)opt->value = opt->defval;
209                         return 0;
210                 }
211                 if (get_arg(p, opt, flags, &arg))
212                         return -1;
213                 *(u64 *)opt->value = strtoull(arg, (char **)&s, 10);
214                 if (*s)
215                         return opterror(opt, "expects a numerical value", flags);
216                 return 0;
217
218         case OPTION_END:
219         case OPTION_ARGUMENT:
220         case OPTION_GROUP:
221         default:
222                 die("should not happen, someone must be hit on the forehead");
223         }
224 }
225
226 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
227 {
228         for (; options->type != OPTION_END; options++) {
229                 if (options->short_name == *p->opt) {
230                         p->opt = p->opt[1] ? p->opt + 1 : NULL;
231                         return get_value(p, options, OPT_SHORT);
232                 }
233         }
234         return -2;
235 }
236
237 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
238                           const struct option *options)
239 {
240         const char *arg_end = strchr(arg, '=');
241         const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
242         int abbrev_flags = 0, ambiguous_flags = 0;
243
244         if (!arg_end)
245                 arg_end = arg + strlen(arg);
246
247         for (; options->type != OPTION_END; options++) {
248                 const char *rest;
249                 int flags = 0;
250
251                 if (!options->long_name)
252                         continue;
253
254                 rest = skip_prefix(arg, options->long_name);
255                 if (options->type == OPTION_ARGUMENT) {
256                         if (!rest)
257                                 continue;
258                         if (*rest == '=')
259                                 return opterror(options, "takes no value", flags);
260                         if (*rest)
261                                 continue;
262                         p->out[p->cpidx++] = arg - 2;
263                         return 0;
264                 }
265                 if (!rest) {
266                         if (!prefixcmp(options->long_name, "no-")) {
267                                 /*
268                                  * The long name itself starts with "no-", so
269                                  * accept the option without "no-" so that users
270                                  * do not have to enter "no-no-" to get the
271                                  * negation.
272                                  */
273                                 rest = skip_prefix(arg, options->long_name + 3);
274                                 if (rest) {
275                                         flags |= OPT_UNSET;
276                                         goto match;
277                                 }
278                                 /* Abbreviated case */
279                                 if (!prefixcmp(options->long_name + 3, arg)) {
280                                         flags |= OPT_UNSET;
281                                         goto is_abbreviated;
282                                 }
283                         }
284                         /* abbreviated? */
285                         if (!strncmp(options->long_name, arg, arg_end - arg)) {
286 is_abbreviated:
287                                 if (abbrev_option) {
288                                         /*
289                                          * If this is abbreviated, it is
290                                          * ambiguous. So when there is no
291                                          * exact match later, we need to
292                                          * error out.
293                                          */
294                                         ambiguous_option = abbrev_option;
295                                         ambiguous_flags = abbrev_flags;
296                                 }
297                                 if (!(flags & OPT_UNSET) && *arg_end)
298                                         p->opt = arg_end + 1;
299                                 abbrev_option = options;
300                                 abbrev_flags = flags;
301                                 continue;
302                         }
303                         /* negated and abbreviated very much? */
304                         if (!prefixcmp("no-", arg)) {
305                                 flags |= OPT_UNSET;
306                                 goto is_abbreviated;
307                         }
308                         /* negated? */
309                         if (strncmp(arg, "no-", 3))
310                                 continue;
311                         flags |= OPT_UNSET;
312                         rest = skip_prefix(arg + 3, options->long_name);
313                         /* abbreviated and negated? */
314                         if (!rest && !prefixcmp(options->long_name, arg + 3))
315                                 goto is_abbreviated;
316                         if (!rest)
317                                 continue;
318                 }
319 match:
320                 if (*rest) {
321                         if (*rest != '=')
322                                 continue;
323                         p->opt = rest + 1;
324                 }
325                 return get_value(p, options, flags);
326         }
327
328         if (ambiguous_option)
329                 return error("Ambiguous option: %s "
330                         "(could be --%s%s or --%s%s)",
331                         arg,
332                         (ambiguous_flags & OPT_UNSET) ?  "no-" : "",
333                         ambiguous_option->long_name,
334                         (abbrev_flags & OPT_UNSET) ?  "no-" : "",
335                         abbrev_option->long_name);
336         if (abbrev_option)
337                 return get_value(p, abbrev_option, abbrev_flags);
338         return -2;
339 }
340
341 static void check_typos(const char *arg, const struct option *options)
342 {
343         if (strlen(arg) < 3)
344                 return;
345
346         if (!prefixcmp(arg, "no-")) {
347                 error ("did you mean `--%s` (with two dashes ?)", arg);
348                 exit(129);
349         }
350
351         for (; options->type != OPTION_END; options++) {
352                 if (!options->long_name)
353                         continue;
354                 if (!prefixcmp(options->long_name, arg)) {
355                         error ("did you mean `--%s` (with two dashes ?)", arg);
356                         exit(129);
357                 }
358         }
359 }
360
361 void parse_options_start(struct parse_opt_ctx_t *ctx,
362                          int argc, const char **argv, int flags)
363 {
364         memset(ctx, 0, sizeof(*ctx));
365         ctx->argc = argc - 1;
366         ctx->argv = argv + 1;
367         ctx->out  = argv;
368         ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
369         ctx->flags = flags;
370         if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
371             (flags & PARSE_OPT_STOP_AT_NON_OPTION))
372                 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
373 }
374
375 static int usage_with_options_internal(const char * const *,
376                                        const struct option *, int);
377
378 int parse_options_step(struct parse_opt_ctx_t *ctx,
379                        const struct option *options,
380                        const char * const usagestr[])
381 {
382         int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
383         int excl_short_opt = 1;
384         const char *arg;
385
386         /* we must reset ->opt, unknown short option leave it dangling */
387         ctx->opt = NULL;
388
389         for (; ctx->argc; ctx->argc--, ctx->argv++) {
390                 arg = ctx->argv[0];
391                 if (*arg != '-' || !arg[1]) {
392                         if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
393                                 break;
394                         ctx->out[ctx->cpidx++] = ctx->argv[0];
395                         continue;
396                 }
397
398                 if (arg[1] != '-') {
399                         ctx->opt = ++arg;
400                         if (internal_help && *ctx->opt == 'h')
401                                 return usage_with_options_internal(usagestr, options, 0);
402                         switch (parse_short_opt(ctx, options)) {
403                         case -1:
404                                 return parse_options_usage(usagestr, options, arg, 1);
405                         case -2:
406                                 goto unknown;
407                         case -3:
408                                 goto exclusive;
409                         default:
410                                 break;
411                         }
412                         if (ctx->opt)
413                                 check_typos(arg, options);
414                         while (ctx->opt) {
415                                 if (internal_help && *ctx->opt == 'h')
416                                         return usage_with_options_internal(usagestr, options, 0);
417                                 arg = ctx->opt;
418                                 switch (parse_short_opt(ctx, options)) {
419                                 case -1:
420                                         return parse_options_usage(usagestr, options, arg, 1);
421                                 case -2:
422                                         /* fake a short option thing to hide the fact that we may have
423                                          * started to parse aggregated stuff
424                                          *
425                                          * This is leaky, too bad.
426                                          */
427                                         ctx->argv[0] = strdup(ctx->opt - 1);
428                                         *(char *)ctx->argv[0] = '-';
429                                         goto unknown;
430                                 case -3:
431                                         goto exclusive;
432                                 default:
433                                         break;
434                                 }
435                         }
436                         continue;
437                 }
438
439                 if (!arg[2]) { /* "--" */
440                         if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
441                                 ctx->argc--;
442                                 ctx->argv++;
443                         }
444                         break;
445                 }
446
447                 arg += 2;
448                 if (internal_help && !strcmp(arg, "help-all"))
449                         return usage_with_options_internal(usagestr, options, 1);
450                 if (internal_help && !strcmp(arg, "help"))
451                         return usage_with_options_internal(usagestr, options, 0);
452                 if (!strcmp(arg, "list-opts"))
453                         return PARSE_OPT_LIST_OPTS;
454                 if (!strcmp(arg, "list-cmds"))
455                         return PARSE_OPT_LIST_SUBCMDS;
456                 switch (parse_long_opt(ctx, arg, options)) {
457                 case -1:
458                         return parse_options_usage(usagestr, options, arg, 0);
459                 case -2:
460                         goto unknown;
461                 case -3:
462                         excl_short_opt = 0;
463                         goto exclusive;
464                 default:
465                         break;
466                 }
467                 continue;
468 unknown:
469                 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
470                         return PARSE_OPT_UNKNOWN;
471                 ctx->out[ctx->cpidx++] = ctx->argv[0];
472                 ctx->opt = NULL;
473         }
474         return PARSE_OPT_DONE;
475
476 exclusive:
477         parse_options_usage(usagestr, options, arg, excl_short_opt);
478         if ((excl_short_opt && ctx->excl_opt->short_name) ||
479             ctx->excl_opt->long_name == NULL) {
480                 char opt = ctx->excl_opt->short_name;
481                 parse_options_usage(NULL, options, &opt, 1);
482         } else {
483                 parse_options_usage(NULL, options, ctx->excl_opt->long_name, 0);
484         }
485         return PARSE_OPT_HELP;
486 }
487
488 int parse_options_end(struct parse_opt_ctx_t *ctx)
489 {
490         memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
491         ctx->out[ctx->cpidx + ctx->argc] = NULL;
492         return ctx->cpidx + ctx->argc;
493 }
494
495 int parse_options_subcommand(int argc, const char **argv, const struct option *options,
496                         const char *const subcommands[], const char *usagestr[], int flags)
497 {
498         struct parse_opt_ctx_t ctx;
499
500         perf_env__set_cmdline(&perf_env, argc, argv);
501
502         /* build usage string if it's not provided */
503         if (subcommands && !usagestr[0]) {
504                 struct strbuf buf = STRBUF_INIT;
505
506                 strbuf_addf(&buf, "perf %s [<options>] {", argv[0]);
507                 for (int i = 0; subcommands[i]; i++) {
508                         if (i)
509                                 strbuf_addstr(&buf, "|");
510                         strbuf_addstr(&buf, subcommands[i]);
511                 }
512                 strbuf_addstr(&buf, "}");
513
514                 usagestr[0] = strdup(buf.buf);
515                 strbuf_release(&buf);
516         }
517
518         parse_options_start(&ctx, argc, argv, flags);
519         switch (parse_options_step(&ctx, options, usagestr)) {
520         case PARSE_OPT_HELP:
521                 exit(129);
522         case PARSE_OPT_DONE:
523                 break;
524         case PARSE_OPT_LIST_OPTS:
525                 while (options->type != OPTION_END) {
526                         if (options->long_name)
527                                 printf("--%s ", options->long_name);
528                         options++;
529                 }
530                 putchar('\n');
531                 exit(130);
532         case PARSE_OPT_LIST_SUBCMDS:
533                 if (subcommands) {
534                         for (int i = 0; subcommands[i]; i++)
535                                 printf("%s ", subcommands[i]);
536                 }
537                 putchar('\n');
538                 exit(130);
539         default: /* PARSE_OPT_UNKNOWN */
540                 if (ctx.argv[0][1] == '-') {
541                         error("unknown option `%s'", ctx.argv[0] + 2);
542                 } else {
543                         error("unknown switch `%c'", *ctx.opt);
544                 }
545                 usage_with_options(usagestr, options);
546         }
547
548         return parse_options_end(&ctx);
549 }
550
551 int parse_options(int argc, const char **argv, const struct option *options,
552                   const char * const usagestr[], int flags)
553 {
554         return parse_options_subcommand(argc, argv, options, NULL,
555                                         (const char **) usagestr, flags);
556 }
557
558 #define USAGE_OPTS_WIDTH 24
559 #define USAGE_GAP         2
560
561 static void print_option_help(const struct option *opts, int full)
562 {
563         size_t pos;
564         int pad;
565
566         if (opts->type == OPTION_GROUP) {
567                 fputc('\n', stderr);
568                 if (*opts->help)
569                         fprintf(stderr, "%s\n", opts->help);
570                 return;
571         }
572         if (!full && (opts->flags & PARSE_OPT_HIDDEN))
573                 return;
574         if (opts->flags & PARSE_OPT_DISABLED)
575                 return;
576
577         pos = fprintf(stderr, "    ");
578         if (opts->short_name)
579                 pos += fprintf(stderr, "-%c", opts->short_name);
580         else
581                 pos += fprintf(stderr, "    ");
582
583         if (opts->long_name && opts->short_name)
584                 pos += fprintf(stderr, ", ");
585         if (opts->long_name)
586                 pos += fprintf(stderr, "--%s", opts->long_name);
587
588         switch (opts->type) {
589         case OPTION_ARGUMENT:
590                 break;
591         case OPTION_LONG:
592         case OPTION_U64:
593         case OPTION_INTEGER:
594         case OPTION_UINTEGER:
595                 if (opts->flags & PARSE_OPT_OPTARG)
596                         if (opts->long_name)
597                                 pos += fprintf(stderr, "[=<n>]");
598                         else
599                                 pos += fprintf(stderr, "[<n>]");
600                 else
601                         pos += fprintf(stderr, " <n>");
602                 break;
603         case OPTION_CALLBACK:
604                 if (opts->flags & PARSE_OPT_NOARG)
605                         break;
606                 /* FALLTHROUGH */
607         case OPTION_STRING:
608                 if (opts->argh) {
609                         if (opts->flags & PARSE_OPT_OPTARG)
610                                 if (opts->long_name)
611                                         pos += fprintf(stderr, "[=<%s>]", opts->argh);
612                                 else
613                                         pos += fprintf(stderr, "[<%s>]", opts->argh);
614                         else
615                                 pos += fprintf(stderr, " <%s>", opts->argh);
616                 } else {
617                         if (opts->flags & PARSE_OPT_OPTARG)
618                                 if (opts->long_name)
619                                         pos += fprintf(stderr, "[=...]");
620                                 else
621                                         pos += fprintf(stderr, "[...]");
622                         else
623                                 pos += fprintf(stderr, " ...");
624                 }
625                 break;
626         default: /* OPTION_{BIT,BOOLEAN,SET_UINT,SET_PTR} */
627         case OPTION_END:
628         case OPTION_GROUP:
629         case OPTION_BIT:
630         case OPTION_BOOLEAN:
631         case OPTION_INCR:
632         case OPTION_SET_UINT:
633         case OPTION_SET_PTR:
634                 break;
635         }
636
637         if (pos <= USAGE_OPTS_WIDTH)
638                 pad = USAGE_OPTS_WIDTH - pos;
639         else {
640                 fputc('\n', stderr);
641                 pad = USAGE_OPTS_WIDTH;
642         }
643         fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
644 }
645
646 static int option__cmp(const void *va, const void *vb)
647 {
648         const struct option *a = va, *b = vb;
649         int sa = tolower(a->short_name), sb = tolower(b->short_name), ret;
650
651         if (sa == 0)
652                 sa = 'z' + 1;
653         if (sb == 0)
654                 sb = 'z' + 1;
655
656         ret = sa - sb;
657
658         if (ret == 0) {
659                 const char *la = a->long_name ?: "",
660                            *lb = b->long_name ?: "";
661                 ret = strcmp(la, lb);
662         }
663
664         return ret;
665 }
666
667 static struct option *options__order(const struct option *opts)
668 {
669         int nr_opts = 0;
670         const struct option *o = opts;
671         struct option *ordered;
672
673         for (o = opts; o->type != OPTION_END; o++)
674                 ++nr_opts;
675
676         ordered = memdup(opts, sizeof(*o) * (nr_opts + 1));
677         if (ordered == NULL)
678                 goto out;
679
680         qsort(ordered, nr_opts, sizeof(*o), option__cmp);
681 out:
682         return ordered;
683 }
684
685 int usage_with_options_internal(const char * const *usagestr,
686                                 const struct option *opts, int full)
687 {
688         struct option *ordered;
689
690         if (!usagestr)
691                 return PARSE_OPT_HELP;
692
693         fprintf(stderr, "\n Usage: %s\n", *usagestr++);
694         while (*usagestr && **usagestr)
695                 fprintf(stderr, "    or: %s\n", *usagestr++);
696         while (*usagestr) {
697                 fprintf(stderr, "%s%s\n",
698                                 **usagestr ? "    " : "",
699                                 *usagestr);
700                 usagestr++;
701         }
702
703         if (opts->type != OPTION_GROUP)
704                 fputc('\n', stderr);
705
706         ordered = options__order(opts);
707         if (ordered)
708                 opts = ordered;
709
710         for (  ; opts->type != OPTION_END; opts++)
711                 print_option_help(opts, full);
712
713         fputc('\n', stderr);
714
715         free(ordered);
716
717         return PARSE_OPT_HELP;
718 }
719
720 void usage_with_options(const char * const *usagestr,
721                         const struct option *opts)
722 {
723         exit_browser(false);
724         usage_with_options_internal(usagestr, opts, 0);
725         exit(129);
726 }
727
728 int parse_options_usage(const char * const *usagestr,
729                         const struct option *opts,
730                         const char *optstr, bool short_opt)
731 {
732         if (!usagestr)
733                 goto opt;
734
735         fprintf(stderr, "\n Usage: %s\n", *usagestr++);
736         while (*usagestr && **usagestr)
737                 fprintf(stderr, "    or: %s\n", *usagestr++);
738         while (*usagestr) {
739                 fprintf(stderr, "%s%s\n",
740                                 **usagestr ? "    " : "",
741                                 *usagestr);
742                 usagestr++;
743         }
744         fputc('\n', stderr);
745
746 opt:
747         for (  ; opts->type != OPTION_END; opts++) {
748                 if (short_opt) {
749                         if (opts->short_name == *optstr)
750                                 break;
751                         continue;
752                 }
753
754                 if (opts->long_name == NULL)
755                         continue;
756
757                 if (!prefixcmp(optstr, opts->long_name))
758                         break;
759                 if (!prefixcmp(optstr, "no-") &&
760                     !prefixcmp(optstr + 3, opts->long_name))
761                         break;
762         }
763
764         if (opts->type != OPTION_END)
765                 print_option_help(opts, 0);
766
767         return PARSE_OPT_HELP;
768 }
769
770
771 int parse_opt_verbosity_cb(const struct option *opt,
772                            const char *arg __maybe_unused,
773                            int unset)
774 {
775         int *target = opt->value;
776
777         if (unset)
778                 /* --no-quiet, --no-verbose */
779                 *target = 0;
780         else if (opt->short_name == 'v') {
781                 if (*target >= 0)
782                         (*target)++;
783                 else
784                         *target = 1;
785         } else {
786                 if (*target <= 0)
787                         (*target)--;
788                 else
789                         *target = -1;
790         }
791         return 0;
792 }
793
794 void set_option_flag(struct option *opts, int shortopt, const char *longopt,
795                      int flag)
796 {
797         for (; opts->type != OPTION_END; opts++) {
798                 if ((shortopt && opts->short_name == shortopt) ||
799                     (opts->long_name && longopt &&
800                      !strcmp(opts->long_name, longopt))) {
801                         opts->flags |= flag;
802                         break;
803                 }
804         }
805 }