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