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