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