parse-options.con commit Merge branch 'js/maint-pretty-mailmap' (fab600c)
   1#include "git-compat-util.h"
   2#include "parse-options.h"
   3#include "cache.h"
   4
   5#define OPT_SHORT 1
   6#define OPT_UNSET 2
   7
   8static inline const char *skip_prefix(const char *str, const char *prefix)
   9{
  10        size_t len = strlen(prefix);
  11        return strncmp(str, prefix, len) ? NULL : str + len;
  12}
  13
  14static int opterror(const struct option *opt, const char *reason, int flags)
  15{
  16        if (flags & OPT_SHORT)
  17                return error("switch `%c' %s", opt->short_name, reason);
  18        if (flags & OPT_UNSET)
  19                return error("option `no-%s' %s", opt->long_name, reason);
  20        return error("option `%s' %s", opt->long_name, reason);
  21}
  22
  23static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
  24                   int flags, const char **arg)
  25{
  26        if (p->opt) {
  27                *arg = p->opt;
  28                p->opt = NULL;
  29        } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
  30                *arg = (const char *)opt->defval;
  31        } else if (p->argc) {
  32                p->argc--;
  33                *arg = *++p->argv;
  34        } else
  35                return opterror(opt, "requires a value", flags);
  36        return 0;
  37}
  38
  39static int get_value(struct parse_opt_ctx_t *p,
  40                     const struct option *opt, int flags)
  41{
  42        const char *s, *arg;
  43        const int unset = flags & OPT_UNSET;
  44
  45        if (unset && p->opt)
  46                return opterror(opt, "takes no value", flags);
  47        if (unset && (opt->flags & PARSE_OPT_NONEG))
  48                return opterror(opt, "isn't available", flags);
  49
  50        if (!(flags & OPT_SHORT) && p->opt) {
  51                switch (opt->type) {
  52                case OPTION_CALLBACK:
  53                        if (!(opt->flags & PARSE_OPT_NOARG))
  54                                break;
  55                        /* FALLTHROUGH */
  56                case OPTION_BOOLEAN:
  57                case OPTION_BIT:
  58                case OPTION_SET_INT:
  59                case OPTION_SET_PTR:
  60                        return opterror(opt, "takes no value", flags);
  61                default:
  62                        break;
  63                }
  64        }
  65
  66        switch (opt->type) {
  67        case OPTION_BIT:
  68                if (unset)
  69                        *(int *)opt->value &= ~opt->defval;
  70                else
  71                        *(int *)opt->value |= opt->defval;
  72                return 0;
  73
  74        case OPTION_BOOLEAN:
  75                *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
  76                return 0;
  77
  78        case OPTION_SET_INT:
  79                *(int *)opt->value = unset ? 0 : opt->defval;
  80                return 0;
  81
  82        case OPTION_SET_PTR:
  83                *(void **)opt->value = unset ? NULL : (void *)opt->defval;
  84                return 0;
  85
  86        case OPTION_STRING:
  87                if (unset)
  88                        *(const char **)opt->value = NULL;
  89                else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
  90                        *(const char **)opt->value = (const char *)opt->defval;
  91                else
  92                        return get_arg(p, opt, flags, (const char **)opt->value);
  93                return 0;
  94
  95        case OPTION_CALLBACK:
  96                if (unset)
  97                        return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
  98                if (opt->flags & PARSE_OPT_NOARG)
  99                        return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
 100                if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
 101                        return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
 102                if (get_arg(p, opt, flags, &arg))
 103                        return -1;
 104                return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
 105
 106        case OPTION_INTEGER:
 107                if (unset) {
 108                        *(int *)opt->value = 0;
 109                        return 0;
 110                }
 111                if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
 112                        *(int *)opt->value = opt->defval;
 113                        return 0;
 114                }
 115                if (get_arg(p, opt, flags, &arg))
 116                        return -1;
 117                *(int *)opt->value = strtol(arg, (char **)&s, 10);
 118                if (*s)
 119                        return opterror(opt, "expects a numerical value", flags);
 120                return 0;
 121
 122        default:
 123                die("should not happen, someone must be hit on the forehead");
 124        }
 125}
 126
 127static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
 128{
 129        for (; options->type != OPTION_END; options++) {
 130                if (options->short_name == *p->opt) {
 131                        p->opt = p->opt[1] ? p->opt + 1 : NULL;
 132                        return get_value(p, options, OPT_SHORT);
 133                }
 134        }
 135        return -2;
 136}
 137
 138static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
 139                          const struct option *options)
 140{
 141        const char *arg_end = strchr(arg, '=');
 142        const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
 143        int abbrev_flags = 0, ambiguous_flags = 0;
 144
 145        if (!arg_end)
 146                arg_end = arg + strlen(arg);
 147
 148        for (; options->type != OPTION_END; options++) {
 149                const char *rest;
 150                int flags = 0;
 151
 152                if (!options->long_name)
 153                        continue;
 154
 155                rest = skip_prefix(arg, options->long_name);
 156                if (options->type == OPTION_ARGUMENT) {
 157                        if (!rest)
 158                                continue;
 159                        if (*rest == '=')
 160                                return opterror(options, "takes no value", flags);
 161                        if (*rest)
 162                                continue;
 163                        p->out[p->cpidx++] = arg - 2;
 164                        return 0;
 165                }
 166                if (!rest) {
 167                        /* abbreviated? */
 168                        if (!strncmp(options->long_name, arg, arg_end - arg)) {
 169is_abbreviated:
 170                                if (abbrev_option) {
 171                                        /*
 172                                         * If this is abbreviated, it is
 173                                         * ambiguous. So when there is no
 174                                         * exact match later, we need to
 175                                         * error out.
 176                                         */
 177                                        ambiguous_option = abbrev_option;
 178                                        ambiguous_flags = abbrev_flags;
 179                                }
 180                                if (!(flags & OPT_UNSET) && *arg_end)
 181                                        p->opt = arg_end + 1;
 182                                abbrev_option = options;
 183                                abbrev_flags = flags;
 184                                continue;
 185                        }
 186                        /* negated and abbreviated very much? */
 187                        if (!prefixcmp("no-", arg)) {
 188                                flags |= OPT_UNSET;
 189                                goto is_abbreviated;
 190                        }
 191                        /* negated? */
 192                        if (strncmp(arg, "no-", 3))
 193                                continue;
 194                        flags |= OPT_UNSET;
 195                        rest = skip_prefix(arg + 3, options->long_name);
 196                        /* abbreviated and negated? */
 197                        if (!rest && !prefixcmp(options->long_name, arg + 3))
 198                                goto is_abbreviated;
 199                        if (!rest)
 200                                continue;
 201                }
 202                if (*rest) {
 203                        if (*rest != '=')
 204                                continue;
 205                        p->opt = rest + 1;
 206                }
 207                return get_value(p, options, flags);
 208        }
 209
 210        if (ambiguous_option)
 211                return error("Ambiguous option: %s "
 212                        "(could be --%s%s or --%s%s)",
 213                        arg,
 214                        (ambiguous_flags & OPT_UNSET) ?  "no-" : "",
 215                        ambiguous_option->long_name,
 216                        (abbrev_flags & OPT_UNSET) ?  "no-" : "",
 217                        abbrev_option->long_name);
 218        if (abbrev_option)
 219                return get_value(p, abbrev_option, abbrev_flags);
 220        return -2;
 221}
 222
 223void check_typos(const char *arg, const struct option *options)
 224{
 225        if (strlen(arg) < 3)
 226                return;
 227
 228        if (!prefixcmp(arg, "no-")) {
 229                error ("did you mean `--%s` (with two dashes ?)", arg);
 230                exit(129);
 231        }
 232
 233        for (; options->type != OPTION_END; options++) {
 234                if (!options->long_name)
 235                        continue;
 236                if (!prefixcmp(options->long_name, arg)) {
 237                        error ("did you mean `--%s` (with two dashes ?)", arg);
 238                        exit(129);
 239                }
 240        }
 241}
 242
 243void parse_options_start(struct parse_opt_ctx_t *ctx,
 244                         int argc, const char **argv, int flags)
 245{
 246        memset(ctx, 0, sizeof(*ctx));
 247        ctx->argc = argc - 1;
 248        ctx->argv = argv + 1;
 249        ctx->out  = argv;
 250        ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
 251        ctx->flags = flags;
 252}
 253
 254static int usage_with_options_internal(const char * const *,
 255                                       const struct option *, int);
 256
 257int parse_options_step(struct parse_opt_ctx_t *ctx,
 258                       const struct option *options,
 259                       const char * const usagestr[])
 260{
 261        /* we must reset ->opt, unknown short option leave it dangling */
 262        ctx->opt = NULL;
 263
 264        for (; ctx->argc; ctx->argc--, ctx->argv++) {
 265                const char *arg = ctx->argv[0];
 266
 267                if (*arg != '-' || !arg[1]) {
 268                        if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
 269                                break;
 270                        ctx->out[ctx->cpidx++] = ctx->argv[0];
 271                        continue;
 272                }
 273
 274                if (arg[1] != '-') {
 275                        ctx->opt = arg + 1;
 276                        if (*ctx->opt == 'h')
 277                                return parse_options_usage(usagestr, options);
 278                        switch (parse_short_opt(ctx, options)) {
 279                        case -1:
 280                                return parse_options_usage(usagestr, options);
 281                        case -2:
 282                                return PARSE_OPT_UNKNOWN;
 283                        }
 284                        if (ctx->opt)
 285                                check_typos(arg + 1, options);
 286                        while (ctx->opt) {
 287                                if (*ctx->opt == 'h')
 288                                        return parse_options_usage(usagestr, options);
 289                                switch (parse_short_opt(ctx, options)) {
 290                                case -1:
 291                                        return parse_options_usage(usagestr, options);
 292                                case -2:
 293                                        /* fake a short option thing to hide the fact that we may have
 294                                         * started to parse aggregated stuff
 295                                         *
 296                                         * This is leaky, too bad.
 297                                         */
 298                                        ctx->argv[0] = xstrdup(ctx->opt - 1);
 299                                        *(char *)ctx->argv[0] = '-';
 300                                        return PARSE_OPT_UNKNOWN;
 301                                }
 302                        }
 303                        continue;
 304                }
 305
 306                if (!arg[2]) { /* "--" */
 307                        if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
 308                                ctx->argc--;
 309                                ctx->argv++;
 310                        }
 311                        break;
 312                }
 313
 314                if (!strcmp(arg + 2, "help-all"))
 315                        return usage_with_options_internal(usagestr, options, 1);
 316                if (!strcmp(arg + 2, "help"))
 317                        return parse_options_usage(usagestr, options);
 318                switch (parse_long_opt(ctx, arg + 2, options)) {
 319                case -1:
 320                        return parse_options_usage(usagestr, options);
 321                case -2:
 322                        return PARSE_OPT_UNKNOWN;
 323                }
 324        }
 325        return PARSE_OPT_DONE;
 326}
 327
 328int parse_options_end(struct parse_opt_ctx_t *ctx)
 329{
 330        memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
 331        ctx->out[ctx->cpidx + ctx->argc] = NULL;
 332        return ctx->cpidx + ctx->argc;
 333}
 334
 335int parse_options(int argc, const char **argv, const struct option *options,
 336                  const char * const usagestr[], int flags)
 337{
 338        struct parse_opt_ctx_t ctx;
 339
 340        parse_options_start(&ctx, argc, argv, flags);
 341        switch (parse_options_step(&ctx, options, usagestr)) {
 342        case PARSE_OPT_HELP:
 343                exit(129);
 344        case PARSE_OPT_DONE:
 345                break;
 346        default: /* PARSE_OPT_UNKNOWN */
 347                if (ctx.argv[0][1] == '-') {
 348                        error("unknown option `%s'", ctx.argv[0] + 2);
 349                } else {
 350                        error("unknown switch `%c'", *ctx.opt);
 351                }
 352                usage_with_options(usagestr, options);
 353        }
 354
 355        return parse_options_end(&ctx);
 356}
 357
 358#define USAGE_OPTS_WIDTH 24
 359#define USAGE_GAP         2
 360
 361int usage_with_options_internal(const char * const *usagestr,
 362                                const struct option *opts, int full)
 363{
 364        fprintf(stderr, "usage: %s\n", *usagestr++);
 365        while (*usagestr && **usagestr)
 366                fprintf(stderr, "   or: %s\n", *usagestr++);
 367        while (*usagestr) {
 368                fprintf(stderr, "%s%s\n",
 369                                **usagestr ? "    " : "",
 370                                *usagestr);
 371                usagestr++;
 372        }
 373
 374        if (opts->type != OPTION_GROUP)
 375                fputc('\n', stderr);
 376
 377        for (; opts->type != OPTION_END; opts++) {
 378                size_t pos;
 379                int pad;
 380
 381                if (opts->type == OPTION_GROUP) {
 382                        fputc('\n', stderr);
 383                        if (*opts->help)
 384                                fprintf(stderr, "%s\n", opts->help);
 385                        continue;
 386                }
 387                if (!full && (opts->flags & PARSE_OPT_HIDDEN))
 388                        continue;
 389
 390                pos = fprintf(stderr, "    ");
 391                if (opts->short_name)
 392                        pos += fprintf(stderr, "-%c", opts->short_name);
 393                if (opts->long_name && opts->short_name)
 394                        pos += fprintf(stderr, ", ");
 395                if (opts->long_name)
 396                        pos += fprintf(stderr, "--%s", opts->long_name);
 397
 398                switch (opts->type) {
 399                case OPTION_ARGUMENT:
 400                        break;
 401                case OPTION_INTEGER:
 402                        if (opts->flags & PARSE_OPT_OPTARG)
 403                                if (opts->long_name)
 404                                        pos += fprintf(stderr, "[=<n>]");
 405                                else
 406                                        pos += fprintf(stderr, "[<n>]");
 407                        else
 408                                pos += fprintf(stderr, " <n>");
 409                        break;
 410                case OPTION_CALLBACK:
 411                        if (opts->flags & PARSE_OPT_NOARG)
 412                                break;
 413                        /* FALLTHROUGH */
 414                case OPTION_STRING:
 415                        if (opts->argh) {
 416                                if (opts->flags & PARSE_OPT_OPTARG)
 417                                        if (opts->long_name)
 418                                                pos += fprintf(stderr, "[=<%s>]", opts->argh);
 419                                        else
 420                                                pos += fprintf(stderr, "[<%s>]", opts->argh);
 421                                else
 422                                        pos += fprintf(stderr, " <%s>", opts->argh);
 423                        } else {
 424                                if (opts->flags & PARSE_OPT_OPTARG)
 425                                        if (opts->long_name)
 426                                                pos += fprintf(stderr, "[=...]");
 427                                        else
 428                                                pos += fprintf(stderr, "[...]");
 429                                else
 430                                        pos += fprintf(stderr, " ...");
 431                        }
 432                        break;
 433                default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */
 434                        break;
 435                }
 436
 437                if (pos <= USAGE_OPTS_WIDTH)
 438                        pad = USAGE_OPTS_WIDTH - pos;
 439                else {
 440                        fputc('\n', stderr);
 441                        pad = USAGE_OPTS_WIDTH;
 442                }
 443                fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
 444        }
 445        fputc('\n', stderr);
 446
 447        return PARSE_OPT_HELP;
 448}
 449
 450void usage_with_options(const char * const *usagestr,
 451                        const struct option *opts)
 452{
 453        usage_with_options_internal(usagestr, opts, 0);
 454        exit(129);
 455}
 456
 457int parse_options_usage(const char * const *usagestr,
 458                        const struct option *opts)
 459{
 460        return usage_with_options_internal(usagestr, opts, 0);
 461}
 462
 463
 464/*----- some often used options -----*/
 465#include "cache.h"
 466
 467int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
 468{
 469        int v;
 470
 471        if (!arg) {
 472                v = unset ? 0 : DEFAULT_ABBREV;
 473        } else {
 474                v = strtol(arg, (char **)&arg, 10);
 475                if (*arg)
 476                        return opterror(opt, "expects a numerical value", 0);
 477                if (v && v < MINIMUM_ABBREV)
 478                        v = MINIMUM_ABBREV;
 479                else if (v > 40)
 480                        v = 40;
 481        }
 482        *(int *)(opt->value) = v;
 483        return 0;
 484}
 485
 486int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
 487                             int unset)
 488{
 489        *(unsigned long *)(opt->value) = approxidate(arg);
 490        return 0;
 491}