parse-options.con commit git-daemon: produce output when ready (f6a34cf)
   1#include "git-compat-util.h"
   2#include "parse-options.h"
   3#include "cache.h"
   4#include "commit.h"
   5#include "color.h"
   6
   7static int parse_options_usage(struct parse_opt_ctx_t *ctx,
   8                               const char * const *usagestr,
   9                               const struct option *opts, int err);
  10
  11#define OPT_SHORT 1
  12#define OPT_UNSET 2
  13
  14int optbug(const struct option *opt, const char *reason)
  15{
  16        if (opt->long_name)
  17                return error("BUG: option '%s' %s", opt->long_name, reason);
  18        return error("BUG: switch '%c' %s", opt->short_name, reason);
  19}
  20
  21int opterror(const struct option *opt, const char *reason, int flags)
  22{
  23        if (flags & OPT_SHORT)
  24                return error("switch `%c' %s", opt->short_name, reason);
  25        if (flags & OPT_UNSET)
  26                return error("option `no-%s' %s", opt->long_name, reason);
  27        return error("option `%s' %s", opt->long_name, reason);
  28}
  29
  30static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
  31                   int flags, const char **arg)
  32{
  33        if (p->opt) {
  34                *arg = p->opt;
  35                p->opt = NULL;
  36        } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
  37                *arg = (const char *)opt->defval;
  38        } else if (p->argc > 1) {
  39                p->argc--;
  40                *arg = *++p->argv;
  41        } else
  42                return opterror(opt, "requires a value", flags);
  43        return 0;
  44}
  45
  46static void fix_filename(const char *prefix, const char **file)
  47{
  48        if (!file || !*file || !prefix || is_absolute_path(*file)
  49            || !strcmp("-", *file))
  50                return;
  51        *file = xstrdup(prefix_filename(prefix, strlen(prefix), *file));
  52}
  53
  54static int get_value(struct parse_opt_ctx_t *p,
  55                     const struct option *opt, int flags)
  56{
  57        const char *s, *arg;
  58        const int unset = flags & OPT_UNSET;
  59        int err;
  60
  61        if (unset && p->opt)
  62                return opterror(opt, "takes no value", flags);
  63        if (unset && (opt->flags & PARSE_OPT_NONEG))
  64                return opterror(opt, "isn't available", flags);
  65        if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
  66                return opterror(opt, "takes no value", flags);
  67
  68        switch (opt->type) {
  69        case OPTION_LOWLEVEL_CALLBACK:
  70                return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
  71
  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_COUNTUP:
  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                        /* negation allowed? */
 234                        if (options->flags & PARSE_OPT_NONEG)
 235                                continue;
 236                        /* negated and abbreviated very much? */
 237                        if (!prefixcmp("no-", arg)) {
 238                                flags |= OPT_UNSET;
 239                                goto is_abbreviated;
 240                        }
 241                        /* negated? */
 242                        if (strncmp(arg, "no-", 3))
 243                                continue;
 244                        flags |= OPT_UNSET;
 245                        rest = skip_prefix(arg + 3, options->long_name);
 246                        /* abbreviated and negated? */
 247                        if (!rest && !prefixcmp(options->long_name, arg + 3))
 248                                goto is_abbreviated;
 249                        if (!rest)
 250                                continue;
 251                }
 252                if (*rest) {
 253                        if (*rest != '=')
 254                                continue;
 255                        p->opt = rest + 1;
 256                }
 257                return get_value(p, options, flags);
 258        }
 259
 260        if (ambiguous_option)
 261                return error("Ambiguous option: %s "
 262                        "(could be --%s%s or --%s%s)",
 263                        arg,
 264                        (ambiguous_flags & OPT_UNSET) ?  "no-" : "",
 265                        ambiguous_option->long_name,
 266                        (abbrev_flags & OPT_UNSET) ?  "no-" : "",
 267                        abbrev_option->long_name);
 268        if (abbrev_option)
 269                return get_value(p, abbrev_option, abbrev_flags);
 270        return -2;
 271}
 272
 273static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
 274                            const struct option *options)
 275{
 276        for (; options->type != OPTION_END; options++) {
 277                if (!(options->flags & PARSE_OPT_NODASH))
 278                        continue;
 279                if (options->short_name == arg[0] && arg[1] == '\0')
 280                        return get_value(p, options, OPT_SHORT);
 281        }
 282        return -2;
 283}
 284
 285static void check_typos(const char *arg, const struct option *options)
 286{
 287        if (strlen(arg) < 3)
 288                return;
 289
 290        if (!prefixcmp(arg, "no-")) {
 291                error ("did you mean `--%s` (with two dashes ?)", arg);
 292                exit(129);
 293        }
 294
 295        for (; options->type != OPTION_END; options++) {
 296                if (!options->long_name)
 297                        continue;
 298                if (!prefixcmp(options->long_name, arg)) {
 299                        error ("did you mean `--%s` (with two dashes ?)", arg);
 300                        exit(129);
 301                }
 302        }
 303}
 304
 305static void parse_options_check(const struct option *opts)
 306{
 307        int err = 0;
 308
 309        for (; opts->type != OPTION_END; opts++) {
 310                if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
 311                    (opts->flags & PARSE_OPT_OPTARG))
 312                        err |= optbug(opts, "uses incompatible flags "
 313                                        "LASTARG_DEFAULT and OPTARG");
 314                if (opts->flags & PARSE_OPT_NODASH &&
 315                    ((opts->flags & PARSE_OPT_OPTARG) ||
 316                     !(opts->flags & PARSE_OPT_NOARG) ||
 317                     !(opts->flags & PARSE_OPT_NONEG) ||
 318                     opts->long_name))
 319                        err |= optbug(opts, "uses feature "
 320                                        "not supported for dashless options");
 321                switch (opts->type) {
 322                case OPTION_COUNTUP:
 323                case OPTION_BIT:
 324                case OPTION_NEGBIT:
 325                case OPTION_SET_INT:
 326                case OPTION_SET_PTR:
 327                case OPTION_NUMBER:
 328                        if ((opts->flags & PARSE_OPT_OPTARG) ||
 329                            !(opts->flags & PARSE_OPT_NOARG))
 330                                err |= optbug(opts, "should not accept an argument");
 331                default:
 332                        ; /* ok. (usually accepts an argument) */
 333                }
 334        }
 335        if (err)
 336                exit(128);
 337}
 338
 339void parse_options_start(struct parse_opt_ctx_t *ctx,
 340                         int argc, const char **argv, const char *prefix,
 341                         const struct option *options, int flags)
 342{
 343        memset(ctx, 0, sizeof(*ctx));
 344        ctx->argc = argc - 1;
 345        ctx->argv = argv + 1;
 346        ctx->out  = argv;
 347        ctx->prefix = prefix;
 348        ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
 349        ctx->flags = flags;
 350        if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
 351            (flags & PARSE_OPT_STOP_AT_NON_OPTION))
 352                die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
 353        parse_options_check(options);
 354}
 355
 356static int usage_with_options_internal(struct parse_opt_ctx_t *,
 357                                       const char * const *,
 358                                       const struct option *, int, int);
 359
 360int parse_options_step(struct parse_opt_ctx_t *ctx,
 361                       const struct option *options,
 362                       const char * const usagestr[])
 363{
 364        int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
 365
 366        /* we must reset ->opt, unknown short option leave it dangling */
 367        ctx->opt = NULL;
 368
 369        for (; ctx->argc; ctx->argc--, ctx->argv++) {
 370                const char *arg = ctx->argv[0];
 371
 372                if (*arg != '-' || !arg[1]) {
 373                        if (parse_nodash_opt(ctx, arg, options) == 0)
 374                                continue;
 375                        if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
 376                                return PARSE_OPT_NON_OPTION;
 377                        ctx->out[ctx->cpidx++] = ctx->argv[0];
 378                        continue;
 379                }
 380
 381                if (arg[1] != '-') {
 382                        ctx->opt = arg + 1;
 383                        if (internal_help && *ctx->opt == 'h')
 384                                return parse_options_usage(ctx, usagestr, options, 0);
 385                        switch (parse_short_opt(ctx, options)) {
 386                        case -1:
 387                                return parse_options_usage(ctx, usagestr, options, 1);
 388                        case -2:
 389                                goto unknown;
 390                        }
 391                        if (ctx->opt)
 392                                check_typos(arg + 1, options);
 393                        while (ctx->opt) {
 394                                if (internal_help && *ctx->opt == 'h')
 395                                        return parse_options_usage(ctx, usagestr, options, 0);
 396                                switch (parse_short_opt(ctx, options)) {
 397                                case -1:
 398                                        return parse_options_usage(ctx, usagestr, options, 1);
 399                                case -2:
 400                                        /* fake a short option thing to hide the fact that we may have
 401                                         * started to parse aggregated stuff
 402                                         *
 403                                         * This is leaky, too bad.
 404                                         */
 405                                        ctx->argv[0] = xstrdup(ctx->opt - 1);
 406                                        *(char *)ctx->argv[0] = '-';
 407                                        goto unknown;
 408                                }
 409                        }
 410                        continue;
 411                }
 412
 413                if (!arg[2]) { /* "--" */
 414                        if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
 415                                ctx->argc--;
 416                                ctx->argv++;
 417                        }
 418                        break;
 419                }
 420
 421                if (internal_help && !strcmp(arg + 2, "help-all"))
 422                        return usage_with_options_internal(ctx, usagestr, options, 1, 0);
 423                if (internal_help && !strcmp(arg + 2, "help"))
 424                        return parse_options_usage(ctx, usagestr, options, 0);
 425                switch (parse_long_opt(ctx, arg + 2, options)) {
 426                case -1:
 427                        return parse_options_usage(ctx, usagestr, options, 1);
 428                case -2:
 429                        goto unknown;
 430                }
 431                continue;
 432unknown:
 433                if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
 434                        return PARSE_OPT_UNKNOWN;
 435                ctx->out[ctx->cpidx++] = ctx->argv[0];
 436                ctx->opt = NULL;
 437        }
 438        return PARSE_OPT_DONE;
 439}
 440
 441int parse_options_end(struct parse_opt_ctx_t *ctx)
 442{
 443        memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
 444        ctx->out[ctx->cpidx + ctx->argc] = NULL;
 445        return ctx->cpidx + ctx->argc;
 446}
 447
 448int parse_options(int argc, const char **argv, const char *prefix,
 449                  const struct option *options, const char * const usagestr[],
 450                  int flags)
 451{
 452        struct parse_opt_ctx_t ctx;
 453
 454        parse_options_start(&ctx, argc, argv, prefix, options, flags);
 455        switch (parse_options_step(&ctx, options, usagestr)) {
 456        case PARSE_OPT_HELP:
 457                exit(129);
 458        case PARSE_OPT_NON_OPTION:
 459        case PARSE_OPT_DONE:
 460                break;
 461        default: /* PARSE_OPT_UNKNOWN */
 462                if (ctx.argv[0][1] == '-') {
 463                        error("unknown option `%s'", ctx.argv[0] + 2);
 464                } else {
 465                        error("unknown switch `%c'", *ctx.opt);
 466                }
 467                usage_with_options(usagestr, options);
 468        }
 469
 470        return parse_options_end(&ctx);
 471}
 472
 473static int usage_argh(const struct option *opts, FILE *outfile)
 474{
 475        const char *s;
 476        int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
 477        if (opts->flags & PARSE_OPT_OPTARG)
 478                if (opts->long_name)
 479                        s = literal ? "[=%s]" : "[=<%s>]";
 480                else
 481                        s = literal ? "[%s]" : "[<%s>]";
 482        else
 483                s = literal ? " %s" : " <%s>";
 484        return fprintf(outfile, s, opts->argh ? opts->argh : "...");
 485}
 486
 487#define USAGE_OPTS_WIDTH 24
 488#define USAGE_GAP         2
 489
 490static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
 491                                       const char * const *usagestr,
 492                                       const struct option *opts, int full, int err)
 493{
 494        FILE *outfile = err ? stderr : stdout;
 495
 496        if (!usagestr)
 497                return PARSE_OPT_HELP;
 498
 499        if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
 500                fprintf(outfile, "cat <<\\EOF\n");
 501
 502        fprintf(outfile, "usage: %s\n", *usagestr++);
 503        while (*usagestr && **usagestr)
 504                fprintf(outfile, "   or: %s\n", *usagestr++);
 505        while (*usagestr) {
 506                fprintf(outfile, "%s%s\n",
 507                                **usagestr ? "    " : "",
 508                                *usagestr);
 509                usagestr++;
 510        }
 511
 512        if (opts->type != OPTION_GROUP)
 513                fputc('\n', outfile);
 514
 515        for (; opts->type != OPTION_END; opts++) {
 516                size_t pos;
 517                int pad;
 518
 519                if (opts->type == OPTION_GROUP) {
 520                        fputc('\n', outfile);
 521                        if (*opts->help)
 522                                fprintf(outfile, "%s\n", opts->help);
 523                        continue;
 524                }
 525                if (!full && (opts->flags & PARSE_OPT_HIDDEN))
 526                        continue;
 527
 528                pos = fprintf(outfile, "    ");
 529                if (opts->short_name && !(opts->flags & PARSE_OPT_NEGHELP)) {
 530                        if (opts->flags & PARSE_OPT_NODASH)
 531                                pos += fprintf(outfile, "%c", opts->short_name);
 532                        else
 533                                pos += fprintf(outfile, "-%c", opts->short_name);
 534                }
 535                if (opts->long_name && opts->short_name)
 536                        pos += fprintf(outfile, ", ");
 537                if (opts->long_name)
 538                        pos += fprintf(outfile, "--%s%s",
 539                                (opts->flags & PARSE_OPT_NEGHELP) ?  "no-" : "",
 540                                opts->long_name);
 541                if (opts->type == OPTION_NUMBER)
 542                        pos += fprintf(outfile, "-NUM");
 543
 544                if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
 545                    !(opts->flags & PARSE_OPT_NOARG))
 546                        pos += usage_argh(opts, outfile);
 547
 548                if (pos <= USAGE_OPTS_WIDTH)
 549                        pad = USAGE_OPTS_WIDTH - pos;
 550                else {
 551                        fputc('\n', outfile);
 552                        pad = USAGE_OPTS_WIDTH;
 553                }
 554                fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
 555        }
 556        fputc('\n', outfile);
 557
 558        if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
 559                fputs("EOF\n", outfile);
 560
 561        return PARSE_OPT_HELP;
 562}
 563
 564void NORETURN usage_with_options(const char * const *usagestr,
 565                        const struct option *opts)
 566{
 567        usage_with_options_internal(NULL, usagestr, opts, 0, 1);
 568        exit(129);
 569}
 570
 571void NORETURN usage_msg_opt(const char *msg,
 572                   const char * const *usagestr,
 573                   const struct option *options)
 574{
 575        fprintf(stderr, "%s\n\n", msg);
 576        usage_with_options(usagestr, options);
 577}
 578
 579static int parse_options_usage(struct parse_opt_ctx_t *ctx,
 580                               const char * const *usagestr,
 581                               const struct option *opts, int err)
 582{
 583        return usage_with_options_internal(ctx, usagestr, opts, 0, err);
 584}
 585