gitweb: Add git_merge sub
[git/gsoc2010-gitweb.git] / parse-options.c
blobc8aaf95b4aad62e6d0e531c70862e540272a8e19
1 #include "git-compat-util.h"
2 #include "parse-options.h"
3 #include "cache.h"
4 #include "commit.h"
5 #include "color.h"
7 static int parse_options_usage(const char * const *usagestr,
8 const struct option *opts, int err);
10 #define OPT_SHORT 1
11 #define OPT_UNSET 2
13 static int opterror(const struct option *opt, const char *reason, int flags)
15 if (flags & OPT_SHORT)
16 return error("switch `%c' %s", opt->short_name, reason);
17 if (flags & OPT_UNSET)
18 return error("option `no-%s' %s", opt->long_name, reason);
19 return error("option `%s' %s", opt->long_name, reason);
22 static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
23 int flags, const char **arg)
25 if (p->opt) {
26 *arg = p->opt;
27 p->opt = NULL;
28 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
29 *arg = (const char *)opt->defval;
30 } else if (p->argc > 1) {
31 p->argc--;
32 *arg = *++p->argv;
33 } else
34 return opterror(opt, "requires a value", flags);
35 return 0;
38 static void fix_filename(const char *prefix, const char **file)
40 if (!file || !*file || !prefix || is_absolute_path(*file)
41 || !strcmp("-", *file))
42 return;
43 *file = xstrdup(prefix_filename(prefix, strlen(prefix), *file));
46 static int get_value(struct parse_opt_ctx_t *p,
47 const struct option *opt, int flags)
49 const char *s, *arg;
50 const int unset = flags & OPT_UNSET;
51 int err;
53 if (unset && p->opt)
54 return opterror(opt, "takes no value", flags);
55 if (unset && (opt->flags & PARSE_OPT_NONEG))
56 return opterror(opt, "isn't available", flags);
58 if (!(flags & OPT_SHORT) && p->opt) {
59 switch (opt->type) {
60 case OPTION_CALLBACK:
61 if (!(opt->flags & PARSE_OPT_NOARG))
62 break;
63 /* FALLTHROUGH */
64 case OPTION_BOOLEAN:
65 case OPTION_BIT:
66 case OPTION_NEGBIT:
67 case OPTION_SET_INT:
68 case OPTION_SET_PTR:
69 return opterror(opt, "takes no value", flags);
70 default:
71 break;
75 switch (opt->type) {
76 case OPTION_BIT:
77 if (unset)
78 *(int *)opt->value &= ~opt->defval;
79 else
80 *(int *)opt->value |= opt->defval;
81 return 0;
83 case OPTION_NEGBIT:
84 if (unset)
85 *(int *)opt->value |= opt->defval;
86 else
87 *(int *)opt->value &= ~opt->defval;
88 return 0;
90 case OPTION_BOOLEAN:
91 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
92 return 0;
94 case OPTION_SET_INT:
95 *(int *)opt->value = unset ? 0 : opt->defval;
96 return 0;
98 case OPTION_SET_PTR:
99 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
100 return 0;
102 case OPTION_STRING:
103 if (unset)
104 *(const char **)opt->value = NULL;
105 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
106 *(const char **)opt->value = (const char *)opt->defval;
107 else
108 return get_arg(p, opt, flags, (const char **)opt->value);
109 return 0;
111 case OPTION_FILENAME:
112 err = 0;
113 if (unset)
114 *(const char **)opt->value = NULL;
115 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
116 *(const char **)opt->value = (const char *)opt->defval;
117 else
118 err = get_arg(p, opt, flags, (const char **)opt->value);
120 if (!err)
121 fix_filename(p->prefix, (const char **)opt->value);
122 return err;
124 case OPTION_CALLBACK:
125 if (unset)
126 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
127 if (opt->flags & PARSE_OPT_NOARG)
128 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
129 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
130 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
131 if (get_arg(p, opt, flags, &arg))
132 return -1;
133 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
135 case OPTION_INTEGER:
136 if (unset) {
137 *(int *)opt->value = 0;
138 return 0;
140 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
141 *(int *)opt->value = opt->defval;
142 return 0;
144 if (get_arg(p, opt, flags, &arg))
145 return -1;
146 *(int *)opt->value = strtol(arg, (char **)&s, 10);
147 if (*s)
148 return opterror(opt, "expects a numerical value", flags);
149 return 0;
151 default:
152 die("should not happen, someone must be hit on the forehead");
156 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
158 const struct option *numopt = NULL;
160 for (; options->type != OPTION_END; options++) {
161 if (options->short_name == *p->opt) {
162 p->opt = p->opt[1] ? p->opt + 1 : NULL;
163 return get_value(p, options, OPT_SHORT);
167 * Handle the numerical option later, explicit one-digit
168 * options take precedence over it.
170 if (options->type == OPTION_NUMBER)
171 numopt = options;
173 if (numopt && isdigit(*p->opt)) {
174 size_t len = 1;
175 char *arg;
176 int rc;
178 while (isdigit(p->opt[len]))
179 len++;
180 arg = xmemdupz(p->opt, len);
181 p->opt = p->opt[len] ? p->opt + len : NULL;
182 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
183 free(arg);
184 return rc;
186 return -2;
189 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
190 const struct option *options)
192 const char *arg_end = strchr(arg, '=');
193 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
194 int abbrev_flags = 0, ambiguous_flags = 0;
196 if (!arg_end)
197 arg_end = arg + strlen(arg);
199 for (; options->type != OPTION_END; options++) {
200 const char *rest;
201 int flags = 0;
203 if (!options->long_name)
204 continue;
206 rest = skip_prefix(arg, options->long_name);
207 if (options->type == OPTION_ARGUMENT) {
208 if (!rest)
209 continue;
210 if (*rest == '=')
211 return opterror(options, "takes no value", flags);
212 if (*rest)
213 continue;
214 p->out[p->cpidx++] = arg - 2;
215 return 0;
217 if (!rest) {
218 /* abbreviated? */
219 if (!strncmp(options->long_name, arg, arg_end - arg)) {
220 is_abbreviated:
221 if (abbrev_option) {
223 * If this is abbreviated, it is
224 * ambiguous. So when there is no
225 * exact match later, we need to
226 * error out.
228 ambiguous_option = abbrev_option;
229 ambiguous_flags = abbrev_flags;
231 if (!(flags & OPT_UNSET) && *arg_end)
232 p->opt = arg_end + 1;
233 abbrev_option = options;
234 abbrev_flags = flags;
235 continue;
237 /* negation allowed? */
238 if (options->flags & PARSE_OPT_NONEG)
239 continue;
240 /* negated and abbreviated very much? */
241 if (!prefixcmp("no-", arg)) {
242 flags |= OPT_UNSET;
243 goto is_abbreviated;
245 /* negated? */
246 if (strncmp(arg, "no-", 3))
247 continue;
248 flags |= OPT_UNSET;
249 rest = skip_prefix(arg + 3, options->long_name);
250 /* abbreviated and negated? */
251 if (!rest && !prefixcmp(options->long_name, arg + 3))
252 goto is_abbreviated;
253 if (!rest)
254 continue;
256 if (*rest) {
257 if (*rest != '=')
258 continue;
259 p->opt = rest + 1;
261 return get_value(p, options, flags);
264 if (ambiguous_option)
265 return error("Ambiguous option: %s "
266 "(could be --%s%s or --%s%s)",
267 arg,
268 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
269 ambiguous_option->long_name,
270 (abbrev_flags & OPT_UNSET) ? "no-" : "",
271 abbrev_option->long_name);
272 if (abbrev_option)
273 return get_value(p, abbrev_option, abbrev_flags);
274 return -2;
277 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
278 const struct option *options)
280 for (; options->type != OPTION_END; options++) {
281 if (!(options->flags & PARSE_OPT_NODASH))
282 continue;
283 if ((options->flags & PARSE_OPT_OPTARG) ||
284 !(options->flags & PARSE_OPT_NOARG))
285 die("BUG: dashless options don't support arguments");
286 if (!(options->flags & PARSE_OPT_NONEG))
287 die("BUG: dashless options don't support negation");
288 if (options->long_name)
289 die("BUG: dashless options can't be long");
290 if (options->short_name == arg[0] && arg[1] == '\0')
291 return get_value(p, options, OPT_SHORT);
293 return -2;
296 static void check_typos(const char *arg, const struct option *options)
298 if (strlen(arg) < 3)
299 return;
301 if (!prefixcmp(arg, "no-")) {
302 error ("did you mean `--%s` (with two dashes ?)", arg);
303 exit(129);
306 for (; options->type != OPTION_END; options++) {
307 if (!options->long_name)
308 continue;
309 if (!prefixcmp(options->long_name, arg)) {
310 error ("did you mean `--%s` (with two dashes ?)", arg);
311 exit(129);
316 static void parse_options_check(const struct option *opts)
318 int err = 0;
320 for (; opts->type != OPTION_END; opts++) {
321 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
322 (opts->flags & PARSE_OPT_OPTARG)) {
323 if (opts->long_name) {
324 error("`--%s` uses incompatible flags "
325 "LASTARG_DEFAULT and OPTARG", opts->long_name);
326 } else {
327 error("`-%c` uses incompatible flags "
328 "LASTARG_DEFAULT and OPTARG", opts->short_name);
330 err |= 1;
334 if (err)
335 exit(129);
338 void parse_options_start(struct parse_opt_ctx_t *ctx,
339 int argc, const char **argv, const char *prefix,
340 int flags)
342 memset(ctx, 0, sizeof(*ctx));
343 ctx->argc = argc - 1;
344 ctx->argv = argv + 1;
345 ctx->out = argv;
346 ctx->prefix = prefix;
347 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
348 ctx->flags = flags;
349 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
350 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
351 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
354 static int usage_with_options_internal(const char * const *,
355 const struct option *, int, int);
357 int parse_options_step(struct parse_opt_ctx_t *ctx,
358 const struct option *options,
359 const char * const usagestr[])
361 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
363 parse_options_check(options);
365 /* we must reset ->opt, unknown short option leave it dangling */
366 ctx->opt = NULL;
368 for (; ctx->argc; ctx->argc--, ctx->argv++) {
369 const char *arg = ctx->argv[0];
371 if (*arg != '-' || !arg[1]) {
372 if (parse_nodash_opt(ctx, arg, options) == 0)
373 continue;
374 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
375 break;
376 ctx->out[ctx->cpidx++] = ctx->argv[0];
377 continue;
380 if (arg[1] != '-') {
381 ctx->opt = arg + 1;
382 if (internal_help && *ctx->opt == 'h')
383 return parse_options_usage(usagestr, options, 0);
384 switch (parse_short_opt(ctx, options)) {
385 case -1:
386 return parse_options_usage(usagestr, options, 1);
387 case -2:
388 goto unknown;
390 if (ctx->opt)
391 check_typos(arg + 1, options);
392 while (ctx->opt) {
393 if (internal_help && *ctx->opt == 'h')
394 return parse_options_usage(usagestr, options, 0);
395 switch (parse_short_opt(ctx, options)) {
396 case -1:
397 return parse_options_usage(usagestr, options, 1);
398 case -2:
399 /* fake a short option thing to hide the fact that we may have
400 * started to parse aggregated stuff
402 * This is leaky, too bad.
404 ctx->argv[0] = xstrdup(ctx->opt - 1);
405 *(char *)ctx->argv[0] = '-';
406 goto unknown;
409 continue;
412 if (!arg[2]) { /* "--" */
413 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
414 ctx->argc--;
415 ctx->argv++;
417 break;
420 if (internal_help && !strcmp(arg + 2, "help-all"))
421 return usage_with_options_internal(usagestr, options, 1, 0);
422 if (internal_help && !strcmp(arg + 2, "help"))
423 return parse_options_usage(usagestr, options, 0);
424 switch (parse_long_opt(ctx, arg + 2, options)) {
425 case -1:
426 return parse_options_usage(usagestr, options, 1);
427 case -2:
428 goto unknown;
430 continue;
431 unknown:
432 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
433 return PARSE_OPT_UNKNOWN;
434 ctx->out[ctx->cpidx++] = ctx->argv[0];
435 ctx->opt = NULL;
437 return PARSE_OPT_DONE;
440 int parse_options_end(struct parse_opt_ctx_t *ctx)
442 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
443 ctx->out[ctx->cpidx + ctx->argc] = NULL;
444 return ctx->cpidx + ctx->argc;
447 int parse_options(int argc, const char **argv, const char *prefix,
448 const struct option *options, const char * const usagestr[],
449 int flags)
451 struct parse_opt_ctx_t ctx;
453 parse_options_start(&ctx, argc, argv, prefix, flags);
454 switch (parse_options_step(&ctx, options, usagestr)) {
455 case PARSE_OPT_HELP:
456 exit(129);
457 case PARSE_OPT_DONE:
458 break;
459 default: /* PARSE_OPT_UNKNOWN */
460 if (ctx.argv[0][1] == '-') {
461 error("unknown option `%s'", ctx.argv[0] + 2);
462 } else {
463 error("unknown switch `%c'", *ctx.opt);
465 usage_with_options(usagestr, options);
468 return parse_options_end(&ctx);
471 static int usage_argh(const struct option *opts, FILE *outfile)
473 const char *s;
474 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
475 if (opts->flags & PARSE_OPT_OPTARG)
476 if (opts->long_name)
477 s = literal ? "[=%s]" : "[=<%s>]";
478 else
479 s = literal ? "[%s]" : "[<%s>]";
480 else
481 s = literal ? " %s" : " <%s>";
482 return fprintf(outfile, s, opts->argh ? opts->argh : "...");
485 #define USAGE_OPTS_WIDTH 24
486 #define USAGE_GAP 2
488 static int usage_with_options_internal(const char * const *usagestr,
489 const struct option *opts, int full, int err)
491 FILE *outfile = err ? stderr : stdout;
493 if (!usagestr)
494 return PARSE_OPT_HELP;
496 fprintf(outfile, "usage: %s\n", *usagestr++);
497 while (*usagestr && **usagestr)
498 fprintf(outfile, " or: %s\n", *usagestr++);
499 while (*usagestr) {
500 fprintf(outfile, "%s%s\n",
501 **usagestr ? " " : "",
502 *usagestr);
503 usagestr++;
506 if (opts->type != OPTION_GROUP)
507 fputc('\n', outfile);
509 for (; opts->type != OPTION_END; opts++) {
510 size_t pos;
511 int pad;
513 if (opts->type == OPTION_GROUP) {
514 fputc('\n', outfile);
515 if (*opts->help)
516 fprintf(outfile, "%s\n", opts->help);
517 continue;
519 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
520 continue;
522 pos = fprintf(outfile, " ");
523 if (opts->short_name && !(opts->flags & PARSE_OPT_NEGHELP)) {
524 if (opts->flags & PARSE_OPT_NODASH)
525 pos += fprintf(outfile, "%c", opts->short_name);
526 else
527 pos += fprintf(outfile, "-%c", opts->short_name);
529 if (opts->long_name && opts->short_name)
530 pos += fprintf(outfile, ", ");
531 if (opts->long_name)
532 pos += fprintf(outfile, "--%s%s",
533 (opts->flags & PARSE_OPT_NEGHELP) ? "no-" : "",
534 opts->long_name);
535 if (opts->type == OPTION_NUMBER)
536 pos += fprintf(outfile, "-NUM");
538 if (!(opts->flags & PARSE_OPT_NOARG))
539 pos += usage_argh(opts, outfile);
541 if (pos <= USAGE_OPTS_WIDTH)
542 pad = USAGE_OPTS_WIDTH - pos;
543 else {
544 fputc('\n', outfile);
545 pad = USAGE_OPTS_WIDTH;
547 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
549 fputc('\n', outfile);
551 return PARSE_OPT_HELP;
554 void usage_with_options(const char * const *usagestr,
555 const struct option *opts)
557 usage_with_options_internal(usagestr, opts, 0, 1);
558 exit(129);
561 void usage_msg_opt(const char *msg,
562 const char * const *usagestr,
563 const struct option *options)
565 fprintf(stderr, "%s\n\n", msg);
566 usage_with_options(usagestr, options);
569 static int parse_options_usage(const char * const *usagestr,
570 const struct option *opts, int err)
572 return usage_with_options_internal(usagestr, opts, 0, err);
576 /*----- some often used options -----*/
577 #include "cache.h"
579 int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
581 int v;
583 if (!arg) {
584 v = unset ? 0 : DEFAULT_ABBREV;
585 } else {
586 v = strtol(arg, (char **)&arg, 10);
587 if (*arg)
588 return opterror(opt, "expects a numerical value", 0);
589 if (v && v < MINIMUM_ABBREV)
590 v = MINIMUM_ABBREV;
591 else if (v > 40)
592 v = 40;
594 *(int *)(opt->value) = v;
595 return 0;
598 int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
599 int unset)
601 *(unsigned long *)(opt->value) = approxidate(arg);
602 return 0;
605 int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
606 int unset)
608 int value;
610 if (!arg)
611 arg = unset ? "never" : (const char *)opt->defval;
612 value = git_config_colorbool(NULL, arg, -1);
613 if (value < 0)
614 return opterror(opt,
615 "expects \"always\", \"auto\", or \"never\"", 0);
616 *(int *)opt->value = value;
617 return 0;
620 int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
621 int unset)
623 int *target = opt->value;
625 if (unset)
626 /* --no-quiet, --no-verbose */
627 *target = 0;
628 else if (opt->short_name == 'v') {
629 if (*target >= 0)
630 (*target)++;
631 else
632 *target = 1;
633 } else {
634 if (*target <= 0)
635 (*target)--;
636 else
637 *target = -1;
639 return 0;
642 int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
644 unsigned char sha1[20];
645 struct commit *commit;
647 if (!arg)
648 return -1;
649 if (get_sha1(arg, sha1))
650 return error("malformed object name %s", arg);
651 commit = lookup_commit_reference(sha1);
652 if (!commit)
653 return error("no such commit %s", arg);
654 commit_list_insert(commit, opt->value);
655 return 0;
658 int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
660 int *target = opt->value;
661 *target = unset ? 2 : 1;
662 return 0;
665 int parse_options_concat(struct option *dst, size_t dst_size, struct option *src)
667 int i, j;
669 for (i = 0; i < dst_size; i++)
670 if (dst[i].type == OPTION_END)
671 break;
672 for (j = 0; i < dst_size; i++, j++) {
673 dst[i] = src[j];
674 if (src[j].type == OPTION_END)
675 return 0;
677 return -1;