]> Pileus Git - ~andy/linux/blob - tools/perf/util/parse-options.c
sched: Fix __sched_setscheduler() nice test
[~andy/linux] / tools / perf / util / parse-options.c
1 #include "util.h"
2 #include "parse-options.h"
3 #include "cache.h"
4 #include "header.h"
5
6 #define OPT_SHORT 1
7 #define OPT_UNSET 2
8
9 static int opterror(const struct option *opt, const char *reason, int flags)
10 {
11         if (flags & OPT_SHORT)
12                 return error("switch `%c' %s", opt->short_name, reason);
13         if (flags & OPT_UNSET)
14                 return error("option `no-%s' %s", opt->long_name, reason);
15         return error("option `%s' %s", opt->long_name, reason);
16 }
17
18 static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
19                    int flags, const char **arg)
20 {
21         if (p->opt) {
22                 *arg = p->opt;
23                 p->opt = NULL;
24         } else if ((opt->flags & PARSE_OPT_LASTARG_DEFAULT) && (p->argc == 1 ||
25                     **(p->argv + 1) == '-')) {
26                 *arg = (const char *)opt->defval;
27         } else if (p->argc > 1) {
28                 p->argc--;
29                 *arg = *++p->argv;
30         } else
31                 return opterror(opt, "requires a value", flags);
32         return 0;
33 }
34
35 static int get_value(struct parse_opt_ctx_t *p,
36                      const struct option *opt, int flags)
37 {
38         const char *s, *arg = NULL;
39         const int unset = flags & OPT_UNSET;
40
41         if (unset && p->opt)
42                 return opterror(opt, "takes no value", flags);
43         if (unset && (opt->flags & PARSE_OPT_NONEG))
44                 return opterror(opt, "isn't available", flags);
45
46         if (!(flags & OPT_SHORT) && p->opt) {
47                 switch (opt->type) {
48                 case OPTION_CALLBACK:
49                         if (!(opt->flags & PARSE_OPT_NOARG))
50                                 break;
51                         /* FALLTHROUGH */
52                 case OPTION_BOOLEAN:
53                 case OPTION_INCR:
54                 case OPTION_BIT:
55                 case OPTION_SET_UINT:
56                 case OPTION_SET_PTR:
57                         return opterror(opt, "takes no value", flags);
58                 case OPTION_END:
59                 case OPTION_ARGUMENT:
60                 case OPTION_GROUP:
61                 case OPTION_STRING:
62                 case OPTION_INTEGER:
63                 case OPTION_UINTEGER:
64                 case OPTION_LONG:
65                 case OPTION_U64:
66                 default:
67                         break;
68                 }
69         }
70
71         switch (opt->type) {
72         case OPTION_BIT:
73                 if (unset)
74                         *(int *)opt->value &= ~opt->defval;
75                 else
76                         *(int *)opt->value |= opt->defval;
77                 return 0;
78
79         case OPTION_BOOLEAN:
80                 *(bool *)opt->value = unset ? false : true;
81                 return 0;
82
83         case OPTION_INCR:
84                 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
85                 return 0;
86
87         case OPTION_SET_UINT:
88                 *(unsigned int *)opt->value = unset ? 0 : opt->defval;
89                 return 0;
90
91         case OPTION_SET_PTR:
92                 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
93                 return 0;
94
95         case OPTION_STRING:
96                 if (unset)
97                         *(const char **)opt->value = NULL;
98                 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
99                         *(const char **)opt->value = (const char *)opt->defval;
100                 else
101                         return get_arg(p, opt, flags, (const char **)opt->value);
102                 return 0;
103
104         case OPTION_CALLBACK:
105                 if (unset)
106                         return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
107                 if (opt->flags & PARSE_OPT_NOARG)
108                         return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
109                 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
110                         return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
111                 if (get_arg(p, opt, flags, &arg))
112                         return -1;
113                 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
114
115         case OPTION_INTEGER:
116                 if (unset) {
117                         *(int *)opt->value = 0;
118                         return 0;
119                 }
120                 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
121                         *(int *)opt->value = opt->defval;
122                         return 0;
123                 }
124                 if (get_arg(p, opt, flags, &arg))
125                         return -1;
126                 *(int *)opt->value = strtol(arg, (char **)&s, 10);
127                 if (*s)
128                         return opterror(opt, "expects a numerical value", flags);
129                 return 0;
130
131         case OPTION_UINTEGER:
132                 if (unset) {
133                         *(unsigned int *)opt->value = 0;
134                         return 0;
135                 }
136                 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
137                         *(unsigned int *)opt->value = opt->defval;
138                         return 0;
139                 }
140                 if (get_arg(p, opt, flags, &arg))
141                         return -1;
142                 *(unsigned int *)opt->value = strtol(arg, (char **)&s, 10);
143                 if (*s)
144                         return opterror(opt, "expects a numerical value", flags);
145                 return 0;
146
147         case OPTION_LONG:
148                 if (unset) {
149                         *(long *)opt->value = 0;
150                         return 0;
151                 }
152                 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
153                         *(long *)opt->value = opt->defval;
154                         return 0;
155                 }
156                 if (get_arg(p, opt, flags, &arg))
157                         return -1;
158                 *(long *)opt->value = strtol(arg, (char **)&s, 10);
159                 if (*s)
160                         return opterror(opt, "expects a numerical value", flags);
161                 return 0;
162
163         case OPTION_U64:
164                 if (unset) {
165                         *(u64 *)opt->value = 0;
166                         return 0;
167                 }
168                 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
169                         *(u64 *)opt->value = opt->defval;
170                         return 0;
171                 }
172                 if (get_arg(p, opt, flags, &arg))
173                         return -1;
174                 *(u64 *)opt->value = strtoull(arg, (char **)&s, 10);
175                 if (*s)
176                         return opterror(opt, "expects a numerical value", flags);
177                 return 0;
178
179         case OPTION_END:
180         case OPTION_ARGUMENT:
181         case OPTION_GROUP:
182         default:
183                 die("should not happen, someone must be hit on the forehead");
184         }
185 }
186
187 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
188 {
189         for (; options->type != OPTION_END; options++) {
190                 if (options->short_name == *p->opt) {
191                         p->opt = p->opt[1] ? p->opt + 1 : NULL;
192                         return get_value(p, options, OPT_SHORT);
193                 }
194         }
195         return -2;
196 }
197
198 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
199                           const struct option *options)
200 {
201         const char *arg_end = strchr(arg, '=');
202         const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
203         int abbrev_flags = 0, ambiguous_flags = 0;
204
205         if (!arg_end)
206                 arg_end = arg + strlen(arg);
207
208         for (; options->type != OPTION_END; options++) {
209                 const char *rest;
210                 int flags = 0;
211
212                 if (!options->long_name)
213                         continue;
214
215                 rest = skip_prefix(arg, options->long_name);
216                 if (options->type == OPTION_ARGUMENT) {
217                         if (!rest)
218                                 continue;
219                         if (*rest == '=')
220                                 return opterror(options, "takes no value", flags);
221                         if (*rest)
222                                 continue;
223                         p->out[p->cpidx++] = arg - 2;
224                         return 0;
225                 }
226                 if (!rest) {
227                         /* abbreviated? */
228                         if (!strncmp(options->long_name, arg, arg_end - arg)) {
229 is_abbreviated:
230                                 if (abbrev_option) {
231                                         /*
232                                          * If this is abbreviated, it is
233                                          * ambiguous. So when there is no
234                                          * exact match later, we need to
235                                          * error out.
236                                          */
237                                         ambiguous_option = abbrev_option;
238                                         ambiguous_flags = abbrev_flags;
239                                 }
240                                 if (!(flags & OPT_UNSET) && *arg_end)
241                                         p->opt = arg_end + 1;
242                                 abbrev_option = options;
243                                 abbrev_flags = flags;
244                                 continue;
245                         }
246                         /* negated and abbreviated very much? */
247                         if (!prefixcmp("no-", arg)) {
248                                 flags |= OPT_UNSET;
249                                 goto is_abbreviated;
250                         }
251                         /* negated? */
252                         if (strncmp(arg, "no-", 3))
253                                 continue;
254                         flags |= OPT_UNSET;
255                         rest = skip_prefix(arg + 3, options->long_name);
256                         /* abbreviated and negated? */
257                         if (!rest && !prefixcmp(options->long_name, arg + 3))
258                                 goto is_abbreviated;
259                         if (!rest)
260                                 continue;
261                 }
262                 if (*rest) {
263                         if (*rest != '=')
264                                 continue;
265                         p->opt = rest + 1;
266                 }
267                 return get_value(p, options, flags);
268         }
269
270         if (ambiguous_option)
271                 return error("Ambiguous option: %s "
272                         "(could be --%s%s or --%s%s)",
273                         arg,
274                         (ambiguous_flags & OPT_UNSET) ?  "no-" : "",
275                         ambiguous_option->long_name,
276                         (abbrev_flags & OPT_UNSET) ?  "no-" : "",
277                         abbrev_option->long_name);
278         if (abbrev_option)
279                 return get_value(p, abbrev_option, abbrev_flags);
280         return -2;
281 }
282
283 static void check_typos(const char *arg, const struct option *options)
284 {
285         if (strlen(arg) < 3)
286                 return;
287
288         if (!prefixcmp(arg, "no-")) {
289                 error ("did you mean `--%s` (with two dashes ?)", arg);
290                 exit(129);
291         }
292
293         for (; options->type != OPTION_END; options++) {
294                 if (!options->long_name)
295                         continue;
296                 if (!prefixcmp(options->long_name, arg)) {
297                         error ("did you mean `--%s` (with two dashes ?)", arg);
298                         exit(129);
299                 }
300         }
301 }
302
303 void parse_options_start(struct parse_opt_ctx_t *ctx,
304                          int argc, const char **argv, int flags)
305 {
306         memset(ctx, 0, sizeof(*ctx));
307         ctx->argc = argc - 1;
308         ctx->argv = argv + 1;
309         ctx->out  = argv;
310         ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
311         ctx->flags = flags;
312         if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
313             (flags & PARSE_OPT_STOP_AT_NON_OPTION))
314                 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
315 }
316
317 static int usage_with_options_internal(const char * const *,
318                                        const struct option *, int);
319
320 int parse_options_step(struct parse_opt_ctx_t *ctx,
321                        const struct option *options,
322                        const char * const usagestr[])
323 {
324         int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
325
326         /* we must reset ->opt, unknown short option leave it dangling */
327         ctx->opt = NULL;
328
329         for (; ctx->argc; ctx->argc--, ctx->argv++) {
330                 const char *arg = ctx->argv[0];
331
332                 if (*arg != '-' || !arg[1]) {
333                         if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
334                                 break;
335                         ctx->out[ctx->cpidx++] = ctx->argv[0];
336                         continue;
337                 }
338
339                 if (arg[1] != '-') {
340                         ctx->opt = arg + 1;
341                         if (internal_help && *ctx->opt == 'h')
342                                 return usage_with_options_internal(usagestr, options, 0);
343                         switch (parse_short_opt(ctx, options)) {
344                         case -1:
345                                 return parse_options_usage(usagestr, options, arg + 1, 1);
346                         case -2:
347                                 goto unknown;
348                         default:
349                                 break;
350                         }
351                         if (ctx->opt)
352                                 check_typos(arg + 1, options);
353                         while (ctx->opt) {
354                                 if (internal_help && *ctx->opt == 'h')
355                                         return usage_with_options_internal(usagestr, options, 0);
356                                 arg = ctx->opt;
357                                 switch (parse_short_opt(ctx, options)) {
358                                 case -1:
359                                         return parse_options_usage(usagestr, options, arg, 1);
360                                 case -2:
361                                         /* fake a short option thing to hide the fact that we may have
362                                          * started to parse aggregated stuff
363                                          *
364                                          * This is leaky, too bad.
365                                          */
366                                         ctx->argv[0] = strdup(ctx->opt - 1);
367                                         *(char *)ctx->argv[0] = '-';
368                                         goto unknown;
369                                 default:
370                                         break;
371                                 }
372                         }
373                         continue;
374                 }
375
376                 if (!arg[2]) { /* "--" */
377                         if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
378                                 ctx->argc--;
379                                 ctx->argv++;
380                         }
381                         break;
382                 }
383
384                 if (internal_help && !strcmp(arg + 2, "help-all"))
385                         return usage_with_options_internal(usagestr, options, 1);
386                 if (internal_help && !strcmp(arg + 2, "help"))
387                         return usage_with_options_internal(usagestr, options, 0);
388                 if (!strcmp(arg + 2, "list-opts"))
389                         return PARSE_OPT_LIST;
390                 switch (parse_long_opt(ctx, arg + 2, options)) {
391                 case -1:
392                         return parse_options_usage(usagestr, options, arg + 2, 0);
393                 case -2:
394                         goto unknown;
395                 default:
396                         break;
397                 }
398                 continue;
399 unknown:
400                 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
401                         return PARSE_OPT_UNKNOWN;
402                 ctx->out[ctx->cpidx++] = ctx->argv[0];
403                 ctx->opt = NULL;
404         }
405         return PARSE_OPT_DONE;
406 }
407
408 int parse_options_end(struct parse_opt_ctx_t *ctx)
409 {
410         memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
411         ctx->out[ctx->cpidx + ctx->argc] = NULL;
412         return ctx->cpidx + ctx->argc;
413 }
414
415 int parse_options(int argc, const char **argv, const struct option *options,
416                   const char * const usagestr[], int flags)
417 {
418         struct parse_opt_ctx_t ctx;
419
420         perf_header__set_cmdline(argc, argv);
421
422         parse_options_start(&ctx, argc, argv, flags);
423         switch (parse_options_step(&ctx, options, usagestr)) {
424         case PARSE_OPT_HELP:
425                 exit(129);
426         case PARSE_OPT_DONE:
427                 break;
428         case PARSE_OPT_LIST:
429                 while (options->type != OPTION_END) {
430                         printf("--%s ", options->long_name);
431                         options++;
432                 }
433                 exit(130);
434         default: /* PARSE_OPT_UNKNOWN */
435                 if (ctx.argv[0][1] == '-') {
436                         error("unknown option `%s'", ctx.argv[0] + 2);
437                 } else {
438                         error("unknown switch `%c'", *ctx.opt);
439                 }
440                 usage_with_options(usagestr, options);
441         }
442
443         return parse_options_end(&ctx);
444 }
445
446 #define USAGE_OPTS_WIDTH 24
447 #define USAGE_GAP         2
448
449 static void print_option_help(const struct option *opts, int full)
450 {
451         size_t pos;
452         int pad;
453
454         if (opts->type == OPTION_GROUP) {
455                 fputc('\n', stderr);
456                 if (*opts->help)
457                         fprintf(stderr, "%s\n", opts->help);
458                 return;
459         }
460         if (!full && (opts->flags & PARSE_OPT_HIDDEN))
461                 return;
462
463         pos = fprintf(stderr, "    ");
464         if (opts->short_name)
465                 pos += fprintf(stderr, "-%c", opts->short_name);
466         else
467                 pos += fprintf(stderr, "    ");
468
469         if (opts->long_name && opts->short_name)
470                 pos += fprintf(stderr, ", ");
471         if (opts->long_name)
472                 pos += fprintf(stderr, "--%s", opts->long_name);
473
474         switch (opts->type) {
475         case OPTION_ARGUMENT:
476                 break;
477         case OPTION_LONG:
478         case OPTION_U64:
479         case OPTION_INTEGER:
480         case OPTION_UINTEGER:
481                 if (opts->flags & PARSE_OPT_OPTARG)
482                         if (opts->long_name)
483                                 pos += fprintf(stderr, "[=<n>]");
484                         else
485                                 pos += fprintf(stderr, "[<n>]");
486                 else
487                         pos += fprintf(stderr, " <n>");
488                 break;
489         case OPTION_CALLBACK:
490                 if (opts->flags & PARSE_OPT_NOARG)
491                         break;
492                 /* FALLTHROUGH */
493         case OPTION_STRING:
494                 if (opts->argh) {
495                         if (opts->flags & PARSE_OPT_OPTARG)
496                                 if (opts->long_name)
497                                         pos += fprintf(stderr, "[=<%s>]", opts->argh);
498                                 else
499                                         pos += fprintf(stderr, "[<%s>]", opts->argh);
500                         else
501                                 pos += fprintf(stderr, " <%s>", opts->argh);
502                 } else {
503                         if (opts->flags & PARSE_OPT_OPTARG)
504                                 if (opts->long_name)
505                                         pos += fprintf(stderr, "[=...]");
506                                 else
507                                         pos += fprintf(stderr, "[...]");
508                         else
509                                 pos += fprintf(stderr, " ...");
510                 }
511                 break;
512         default: /* OPTION_{BIT,BOOLEAN,SET_UINT,SET_PTR} */
513         case OPTION_END:
514         case OPTION_GROUP:
515         case OPTION_BIT:
516         case OPTION_BOOLEAN:
517         case OPTION_INCR:
518         case OPTION_SET_UINT:
519         case OPTION_SET_PTR:
520                 break;
521         }
522
523         if (pos <= USAGE_OPTS_WIDTH)
524                 pad = USAGE_OPTS_WIDTH - pos;
525         else {
526                 fputc('\n', stderr);
527                 pad = USAGE_OPTS_WIDTH;
528         }
529         fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
530 }
531
532 int usage_with_options_internal(const char * const *usagestr,
533                                 const struct option *opts, int full)
534 {
535         if (!usagestr)
536                 return PARSE_OPT_HELP;
537
538         fprintf(stderr, "\n usage: %s\n", *usagestr++);
539         while (*usagestr && **usagestr)
540                 fprintf(stderr, "    or: %s\n", *usagestr++);
541         while (*usagestr) {
542                 fprintf(stderr, "%s%s\n",
543                                 **usagestr ? "    " : "",
544                                 *usagestr);
545                 usagestr++;
546         }
547
548         if (opts->type != OPTION_GROUP)
549                 fputc('\n', stderr);
550
551         for (  ; opts->type != OPTION_END; opts++)
552                 print_option_help(opts, full);
553
554         fputc('\n', stderr);
555
556         return PARSE_OPT_HELP;
557 }
558
559 void usage_with_options(const char * const *usagestr,
560                         const struct option *opts)
561 {
562         exit_browser(false);
563         usage_with_options_internal(usagestr, opts, 0);
564         exit(129);
565 }
566
567 int parse_options_usage(const char * const *usagestr,
568                         const struct option *opts,
569                         const char *optstr, bool short_opt)
570 {
571         if (!usagestr)
572                 goto opt;
573
574         fprintf(stderr, "\n usage: %s\n", *usagestr++);
575         while (*usagestr && **usagestr)
576                 fprintf(stderr, "    or: %s\n", *usagestr++);
577         while (*usagestr) {
578                 fprintf(stderr, "%s%s\n",
579                                 **usagestr ? "    " : "",
580                                 *usagestr);
581                 usagestr++;
582         }
583         fputc('\n', stderr);
584
585 opt:
586         for (  ; opts->type != OPTION_END; opts++) {
587                 if (short_opt) {
588                         if (opts->short_name == *optstr)
589                                 break;
590                         continue;
591                 }
592
593                 if (opts->long_name == NULL)
594                         continue;
595
596                 if (!prefixcmp(optstr, opts->long_name))
597                         break;
598                 if (!prefixcmp(optstr, "no-") &&
599                     !prefixcmp(optstr + 3, opts->long_name))
600                         break;
601         }
602
603         if (opts->type != OPTION_END)
604                 print_option_help(opts, 0);
605
606         return PARSE_OPT_HELP;
607 }
608
609
610 int parse_opt_verbosity_cb(const struct option *opt,
611                            const char *arg __maybe_unused,
612                            int unset)
613 {
614         int *target = opt->value;
615
616         if (unset)
617                 /* --no-quiet, --no-verbose */
618                 *target = 0;
619         else if (opt->short_name == 'v') {
620                 if (*target >= 0)
621                         (*target)++;
622                 else
623                         *target = 1;
624         } else {
625                 if (*target <= 0)
626                         (*target)--;
627                 else
628                         *target = -1;
629         }
630         return 0;
631 }