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