04adf219cace134e2e51440b037b0e8a2622b3f7
   1#include "git-compat-util.h"
   2#include "parse-options.h"
   3
   4#define OPT_SHORT 1
   5#define OPT_UNSET 2
   6
   7static inline const char *get_arg(struct parse_opt_ctx_t *p)
   8{
   9        if (p->opt) {
  10                const char *res = p->opt;
  11                p->opt = NULL;
  12                return res;
  13        }
  14        p->argc--;
  15        return *++p->argv;
  16}
  17
  18static inline const char *skip_prefix(const char *str, const char *prefix)
  19{
  20        size_t len = strlen(prefix);
  21        return strncmp(str, prefix, len) ? NULL : str + len;
  22}
  23
  24static int opterror(const struct option *opt, const char *reason, int flags)
  25{
  26        if (flags & OPT_SHORT)
  27                return error("switch `%c' %s", opt->short_name, reason);
  28        if (flags & OPT_UNSET)
  29                return error("option `no-%s' %s", opt->long_name, reason);
  30        return error("option `%s' %s", opt->long_name, reason);
  31}
  32
  33static int get_value(struct parse_opt_ctx_t *p,
  34                     const struct option *opt, int flags)
  35{
  36        const char *s, *arg;
  37        const int unset = flags & OPT_UNSET;
  38
  39        if (unset && p->opt)
  40                return opterror(opt, "takes no value", flags);
  41        if (unset && (opt->flags & PARSE_OPT_NONEG))
  42                return opterror(opt, "isn't available", flags);
  43
  44        if (!(flags & OPT_SHORT) && p->opt) {
  45                switch (opt->type) {
  46                case OPTION_CALLBACK:
  47                        if (!(opt->flags & PARSE_OPT_NOARG))
  48                                break;
  49                        /* FALLTHROUGH */
  50                case OPTION_BOOLEAN:
  51                case OPTION_BIT:
  52                case OPTION_SET_INT:
  53                case OPTION_SET_PTR:
  54                        return opterror(opt, "takes no value", flags);
  55                default:
  56                        break;
  57                }
  58        }
  59
  60        arg = p->opt ? p->opt : (p->argc > 1 ? p->argv[1] : NULL);
  61        switch (opt->type) {
  62        case OPTION_BIT:
  63                if (unset)
  64                        *(int *)opt->value &= ~opt->defval;
  65                else
  66                        *(int *)opt->value |= opt->defval;
  67                return 0;
  68
  69        case OPTION_BOOLEAN:
  70                *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
  71                return 0;
  72
  73        case OPTION_SET_INT:
  74                *(int *)opt->value = unset ? 0 : opt->defval;
  75                return 0;
  76
  77        case OPTION_SET_PTR:
  78                *(void **)opt->value = unset ? NULL : (void *)opt->defval;
  79                return 0;
  80
  81        case OPTION_STRING:
  82                if (unset) {
  83                        *(const char **)opt->value = NULL;
  84                        return 0;
  85                }
  86                if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
  87                        *(const char **)opt->value = (const char *)opt->defval;
  88                        return 0;
  89                }
  90                if (!arg)
  91                        return opterror(opt, "requires a value", flags);
  92                *(const char **)opt->value = get_arg(p);
  93                return 0;
  94
  95        case OPTION_CALLBACK:
  96                if (unset)
  97                        return (*opt->callback)(opt, NULL, 1);
  98                if (opt->flags & PARSE_OPT_NOARG)
  99                        return (*opt->callback)(opt, NULL, 0);
 100                if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
 101                        return (*opt->callback)(opt, NULL, 0);
 102                if (!arg)
 103                        return opterror(opt, "requires a value", flags);
 104                return (*opt->callback)(opt, get_arg(p), 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 (!arg)
 116                        return opterror(opt, "requires a value", flags);
 117                *(int *)opt->value = strtol(get_arg(p), (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 error("unknown switch `%c'", *p->opt);
 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 error("unknown option `%s'", arg);
 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->flags = flags;
 251}
 252
 253static int usage_with_options_internal(const char * const *,
 254                                       const struct option *, int);
 255
 256int parse_options_step(struct parse_opt_ctx_t *ctx,
 257                       const struct option *options,
 258                       const char * const usagestr[])
 259{
 260        for (; ctx->argc; ctx->argc--, ctx->argv++) {
 261                const char *arg = ctx->argv[0];
 262
 263                if (*arg != '-' || !arg[1]) {
 264                        if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
 265                                break;
 266                        ctx->out[ctx->cpidx++] = ctx->argv[0];
 267                        continue;
 268                }
 269
 270                if (arg[1] != '-') {
 271                        ctx->opt = arg + 1;
 272                        if (*ctx->opt == 'h')
 273                                return parse_options_usage(usagestr, options);
 274                        if (parse_short_opt(ctx, options) < 0)
 275                                usage_with_options(usagestr, options);
 276                        if (ctx->opt)
 277                                check_typos(arg + 1, options);
 278                        while (ctx->opt) {
 279                                if (*ctx->opt == 'h')
 280                                        return parse_options_usage(usagestr, options);
 281                                if (parse_short_opt(ctx, options) < 0)
 282                                        usage_with_options(usagestr, options);
 283                        }
 284                        continue;
 285                }
 286
 287                if (!arg[2]) { /* "--" */
 288                        if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
 289                                ctx->argc--;
 290                                ctx->argv++;
 291                        }
 292                        break;
 293                }
 294
 295                if (!strcmp(arg + 2, "help-all"))
 296                        return usage_with_options_internal(usagestr, options, 1);
 297                if (!strcmp(arg + 2, "help"))
 298                        return parse_options_usage(usagestr, options);
 299                if (parse_long_opt(ctx, arg + 2, options))
 300                        usage_with_options(usagestr, options);
 301        }
 302        return PARSE_OPT_DONE;
 303}
 304
 305int parse_options_end(struct parse_opt_ctx_t *ctx)
 306{
 307        memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
 308        ctx->out[ctx->cpidx + ctx->argc] = NULL;
 309        return ctx->cpidx + ctx->argc;
 310}
 311
 312int parse_options(int argc, const char **argv, const struct option *options,
 313                  const char * const usagestr[], int flags)
 314{
 315        struct parse_opt_ctx_t ctx;
 316
 317        parse_options_start(&ctx, argc, argv, flags);
 318        switch (parse_options_step(&ctx, options, usagestr)) {
 319        case PARSE_OPT_HELP:
 320                exit(129);
 321        case PARSE_OPT_DONE:
 322                break;
 323        default: /* PARSE_OPT_UNKNOWN */
 324                abort(); /* unreached yet */
 325        }
 326
 327        return parse_options_end(&ctx);
 328}
 329
 330#define USAGE_OPTS_WIDTH 24
 331#define USAGE_GAP         2
 332
 333int usage_with_options_internal(const char * const *usagestr,
 334                                const struct option *opts, int full)
 335{
 336        fprintf(stderr, "usage: %s\n", *usagestr++);
 337        while (*usagestr && **usagestr)
 338                fprintf(stderr, "   or: %s\n", *usagestr++);
 339        while (*usagestr) {
 340                fprintf(stderr, "%s%s\n",
 341                                **usagestr ? "    " : "",
 342                                *usagestr);
 343                usagestr++;
 344        }
 345
 346        if (opts->type != OPTION_GROUP)
 347                fputc('\n', stderr);
 348
 349        for (; opts->type != OPTION_END; opts++) {
 350                size_t pos;
 351                int pad;
 352
 353                if (opts->type == OPTION_GROUP) {
 354                        fputc('\n', stderr);
 355                        if (*opts->help)
 356                                fprintf(stderr, "%s\n", opts->help);
 357                        continue;
 358                }
 359                if (!full && (opts->flags & PARSE_OPT_HIDDEN))
 360                        continue;
 361
 362                pos = fprintf(stderr, "    ");
 363                if (opts->short_name)
 364                        pos += fprintf(stderr, "-%c", opts->short_name);
 365                if (opts->long_name && opts->short_name)
 366                        pos += fprintf(stderr, ", ");
 367                if (opts->long_name)
 368                        pos += fprintf(stderr, "--%s", opts->long_name);
 369
 370                switch (opts->type) {
 371                case OPTION_ARGUMENT:
 372                        break;
 373                case OPTION_INTEGER:
 374                        if (opts->flags & PARSE_OPT_OPTARG)
 375                                if (opts->long_name)
 376                                        pos += fprintf(stderr, "[=<n>]");
 377                                else
 378                                        pos += fprintf(stderr, "[<n>]");
 379                        else
 380                                pos += fprintf(stderr, " <n>");
 381                        break;
 382                case OPTION_CALLBACK:
 383                        if (opts->flags & PARSE_OPT_NOARG)
 384                                break;
 385                        /* FALLTHROUGH */
 386                case OPTION_STRING:
 387                        if (opts->argh) {
 388                                if (opts->flags & PARSE_OPT_OPTARG)
 389                                        if (opts->long_name)
 390                                                pos += fprintf(stderr, "[=<%s>]", opts->argh);
 391                                        else
 392                                                pos += fprintf(stderr, "[<%s>]", opts->argh);
 393                                else
 394                                        pos += fprintf(stderr, " <%s>", opts->argh);
 395                        } else {
 396                                if (opts->flags & PARSE_OPT_OPTARG)
 397                                        if (opts->long_name)
 398                                                pos += fprintf(stderr, "[=...]");
 399                                        else
 400                                                pos += fprintf(stderr, "[...]");
 401                                else
 402                                        pos += fprintf(stderr, " ...");
 403                        }
 404                        break;
 405                default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */
 406                        break;
 407                }
 408
 409                if (pos <= USAGE_OPTS_WIDTH)
 410                        pad = USAGE_OPTS_WIDTH - pos;
 411                else {
 412                        fputc('\n', stderr);
 413                        pad = USAGE_OPTS_WIDTH;
 414                }
 415                fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
 416        }
 417        fputc('\n', stderr);
 418
 419        return PARSE_OPT_HELP;
 420}
 421
 422void usage_with_options(const char * const *usagestr,
 423                        const struct option *opts)
 424{
 425        usage_with_options_internal(usagestr, opts, 0);
 426        exit(129);
 427}
 428
 429int parse_options_usage(const char * const *usagestr,
 430                        const struct option *opts)
 431{
 432        return usage_with_options_internal(usagestr, opts, 0);
 433}
 434
 435
 436/*----- some often used options -----*/
 437#include "cache.h"
 438
 439int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
 440{
 441        int v;
 442
 443        if (!arg) {
 444                v = unset ? 0 : DEFAULT_ABBREV;
 445        } else {
 446                v = strtol(arg, (char **)&arg, 10);
 447                if (*arg)
 448                        return opterror(opt, "expects a numerical value", 0);
 449                if (v && v < MINIMUM_ABBREV)
 450                        v = MINIMUM_ABBREV;
 451                else if (v > 40)
 452                        v = 40;
 453        }
 454        *(int *)(opt->value) = v;
 455        return 0;
 456}
 457
 458int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
 459                             int unset)
 460{
 461        *(unsigned long *)(opt->value) = approxidate(arg);
 462        return 0;
 463}