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