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