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