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