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