]> git.ipfire.org Git - thirdparty/git.git/blame - parse-options.c
t5318: avoid unnecessary command substitutions
[thirdparty/git.git] / parse-options.c
CommitLineData
4a59fd13
PH
1#include "git-compat-util.h"
2#include "parse-options.h"
26141b5b 3#include "cache.h"
b2141fc1 4#include "config.h"
269defdf 5#include "commit.h"
73e9da01 6#include "color.h"
c0821965 7#include "utf8.h"
4a59fd13
PH
8
9#define OPT_SHORT 1
10#define OPT_UNSET 2
11
1f275b7c 12int optbug(const struct option *opt, const char *reason)
1e5ce570 13{
af465af8
JH
14 if (opt->long_name) {
15 if (opt->short_name)
16 return error("BUG: switch '%c' (--%s) %s",
17 opt->short_name, opt->long_name, reason);
1e5ce570 18 return error("BUG: option '%s' %s", opt->long_name, reason);
af465af8 19 }
1e5ce570
JN
20 return error("BUG: switch '%c' %s", opt->short_name, reason);
21}
22
1cc6985c
PH
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;
d5d745f9 31 } else if (p->argc > 1) {
1cc6985c
PH
32 p->argc--;
33 *arg = *++p->argv;
34 } else
35 return opterror(opt, "requires a value", flags);
36 return 0;
37}
38
df217ed6
SB
39static void fix_filename(const char *prefix, const char **file)
40{
41 if (!file || !*file || !prefix || is_absolute_path(*file)
42 || !strcmp("-", *file))
43 return;
e4da43b1 44 *file = prefix_filename(prefix, *file);
df217ed6
SB
45}
46
11588263
JH
47static int opt_command_mode_error(const struct option *opt,
48 const struct option *all_opts,
49 int flags)
50{
51 const struct option *that;
52 struct strbuf message = STRBUF_INIT;
53 struct strbuf that_name = STRBUF_INIT;
54
55 /*
56 * Find the other option that was used to set the variable
57 * already, and report that this is not compatible with it.
58 */
59 for (that = all_opts; that->type != OPTION_END; that++) {
60 if (that == opt ||
61 that->type != OPTION_CMDMODE ||
62 that->value != opt->value ||
63 that->defval != *(int *)opt->value)
64 continue;
65
66 if (that->long_name)
67 strbuf_addf(&that_name, "--%s", that->long_name);
68 else
69 strbuf_addf(&that_name, "-%c", that->short_name);
70 strbuf_addf(&message, ": incompatible with %s", that_name.buf);
71 strbuf_release(&that_name);
72 opterror(opt, message.buf, flags);
73 strbuf_release(&message);
74 return -1;
75 }
76 return opterror(opt, ": incompatible with something else", flags);
77}
78
7e7bbcb4 79static int get_value(struct parse_opt_ctx_t *p,
11588263
JH
80 const struct option *opt,
81 const struct option *all_opts,
82 int flags)
4a59fd13 83{
ffe659f9 84 const char *s, *arg;
db7244bd 85 const int unset = flags & OPT_UNSET;
df217ed6 86 int err;
4a59fd13 87
db7244bd 88 if (unset && p->opt)
4a59fd13 89 return opterror(opt, "takes no value", flags);
db7244bd
PH
90 if (unset && (opt->flags & PARSE_OPT_NONEG))
91 return opterror(opt, "isn't available", flags);
c1f4ec9e
SB
92 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
93 return opterror(opt, "takes no value", flags);
db7244bd 94
db7244bd 95 switch (opt->type) {
b0b3a8b6
JN
96 case OPTION_LOWLEVEL_CALLBACK:
97 return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
98
db7244bd
PH
99 case OPTION_BIT:
100 if (unset)
101 *(int *)opt->value &= ~opt->defval;
4a59fd13 102 else
db7244bd
PH
103 *(int *)opt->value |= opt->defval;
104 return 0;
105
2f4b97f9
RS
106 case OPTION_NEGBIT:
107 if (unset)
108 *(int *)opt->value |= opt->defval;
109 else
110 *(int *)opt->value &= ~opt->defval;
111 return 0;
112
b04ba2bb 113 case OPTION_COUNTUP:
e0070e8b
PB
114 if (*(int *)opt->value < 0)
115 *(int *)opt->value = 0;
db7244bd
PH
116 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
117 return 0;
118
119 case OPTION_SET_INT:
120 *(int *)opt->value = unset ? 0 : opt->defval;
121 return 0;
122
11588263
JH
123 case OPTION_CMDMODE:
124 /*
125 * Giving the same mode option twice, although is unnecessary,
126 * is not a grave error, so let it pass.
127 */
128 if (*(int *)opt->value && *(int *)opt->value != opt->defval)
129 return opt_command_mode_error(opt, all_opts, flags);
130 *(int *)opt->value = opt->defval;
131 return 0;
132
4a59fd13 133 case OPTION_STRING:
1cc6985c 134 if (unset)
db7244bd 135 *(const char **)opt->value = NULL;
1cc6985c 136 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
ffe659f9 137 *(const char **)opt->value = (const char *)opt->defval;
1cc6985c
PH
138 else
139 return get_arg(p, opt, flags, (const char **)opt->value);
4a59fd13
PH
140 return 0;
141
df217ed6
SB
142 case OPTION_FILENAME:
143 err = 0;
144 if (unset)
145 *(const char **)opt->value = NULL;
146 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
147 *(const char **)opt->value = (const char *)opt->defval;
148 else
149 err = get_arg(p, opt, flags, (const char **)opt->value);
150
151 if (!err)
152 fix_filename(p->prefix, (const char **)opt->value);
153 return err;
154
ffe659f9 155 case OPTION_CALLBACK:
db7244bd 156 if (unset)
07fe54db 157 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
db7244bd 158 if (opt->flags & PARSE_OPT_NOARG)
07fe54db 159 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
c43a2483 160 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
07fe54db 161 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
1cc6985c
PH
162 if (get_arg(p, opt, flags, &arg))
163 return -1;
164 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
ffe659f9 165
4a59fd13 166 case OPTION_INTEGER:
db7244bd 167 if (unset) {
4a59fd13
PH
168 *(int *)opt->value = 0;
169 return 0;
170 }
c43a2483 171 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
ffe659f9
PH
172 *(int *)opt->value = opt->defval;
173 return 0;
174 }
1cc6985c
PH
175 if (get_arg(p, opt, flags, &arg))
176 return -1;
177 *(int *)opt->value = strtol(arg, (char **)&s, 10);
4a59fd13
PH
178 if (*s)
179 return opterror(opt, "expects a numerical value", flags);
180 return 0;
181
2a514ed8
CB
182 case OPTION_MAGNITUDE:
183 if (unset) {
184 *(unsigned long *)opt->value = 0;
185 return 0;
186 }
187 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
188 *(unsigned long *)opt->value = opt->defval;
189 return 0;
190 }
191 if (get_arg(p, opt, flags, &arg))
192 return -1;
193 if (!git_parse_ulong(arg, opt->value))
194 return opterror(opt,
195 "expects a non-negative integer value with an optional k/m/g suffix",
196 flags);
197 return 0;
198
4a59fd13
PH
199 default:
200 die("should not happen, someone must be hit on the forehead");
201 }
202}
203
7e7bbcb4 204static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
4a59fd13 205{
11588263 206 const struct option *all_opts = options;
e0319ff5
RS
207 const struct option *numopt = NULL;
208
4a59fd13
PH
209 for (; options->type != OPTION_END; options++) {
210 if (options->short_name == *p->opt) {
211 p->opt = p->opt[1] ? p->opt + 1 : NULL;
11588263 212 return get_value(p, options, all_opts, OPT_SHORT);
4a59fd13 213 }
e0319ff5
RS
214
215 /*
216 * Handle the numerical option later, explicit one-digit
217 * options take precedence over it.
218 */
219 if (options->type == OPTION_NUMBER)
220 numopt = options;
221 }
222 if (numopt && isdigit(*p->opt)) {
223 size_t len = 1;
224 char *arg;
225 int rc;
226
227 while (isdigit(p->opt[len]))
228 len++;
229 arg = xmemdupz(p->opt, len);
230 p->opt = p->opt[len] ? p->opt + len : NULL;
231 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
232 free(arg);
233 return rc;
4a59fd13 234 }
07fe54db 235 return -2;
4a59fd13
PH
236}
237
7e7bbcb4 238static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
4a59fd13
PH
239 const struct option *options)
240{
11588263 241 const struct option *all_opts = options;
2c5495f7 242 const char *arg_end = strchrnul(arg, '=');
243e0614
JS
243 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
244 int abbrev_flags = 0, ambiguous_flags = 0;
7f275b91 245
4a59fd13 246 for (; options->type != OPTION_END; options++) {
0f1930c5
RS
247 const char *rest, *long_name = options->long_name;
248 int flags = 0, opt_flags = 0;
4a59fd13 249
0f1930c5 250 if (!long_name)
4a59fd13
PH
251 continue;
252
0f1930c5 253again:
cf4fff57
JK
254 if (!skip_prefix(arg, long_name, &rest))
255 rest = NULL;
580d5bff
PH
256 if (options->type == OPTION_ARGUMENT) {
257 if (!rest)
258 continue;
259 if (*rest == '=')
260 return opterror(options, "takes no value", flags);
261 if (*rest)
262 continue;
263 p->out[p->cpidx++] = arg - 2;
264 return 0;
265 }
4a59fd13 266 if (!rest) {
7f275b91 267 /* abbreviated? */
0f1930c5 268 if (!strncmp(long_name, arg, arg_end - arg)) {
7f275b91 269is_abbreviated:
243e0614
JS
270 if (abbrev_option) {
271 /*
272 * If this is abbreviated, it is
273 * ambiguous. So when there is no
274 * exact match later, we need to
275 * error out.
276 */
277 ambiguous_option = abbrev_option;
278 ambiguous_flags = abbrev_flags;
279 }
7f275b91
JS
280 if (!(flags & OPT_UNSET) && *arg_end)
281 p->opt = arg_end + 1;
282 abbrev_option = options;
0f1930c5 283 abbrev_flags = flags ^ opt_flags;
7f275b91
JS
284 continue;
285 }
6bbfd1fa
AS
286 /* negation allowed? */
287 if (options->flags & PARSE_OPT_NONEG)
288 continue;
7f275b91 289 /* negated and abbreviated very much? */
59556548 290 if (starts_with("no-", arg)) {
7f275b91
JS
291 flags |= OPT_UNSET;
292 goto is_abbreviated;
293 }
294 /* negated? */
59556548
CC
295 if (!starts_with(arg, "no-")) {
296 if (starts_with(long_name, "no-")) {
0f1930c5
RS
297 long_name += 3;
298 opt_flags |= OPT_UNSET;
299 goto again;
300 }
4a59fd13 301 continue;
0f1930c5 302 }
4a59fd13 303 flags |= OPT_UNSET;
cf4fff57
JK
304 if (!skip_prefix(arg + 3, long_name, &rest)) {
305 /* abbreviated and negated? */
306 if (starts_with(long_name, arg + 3))
307 goto is_abbreviated;
308 else
309 continue;
310 }
4a59fd13
PH
311 }
312 if (*rest) {
313 if (*rest != '=')
314 continue;
315 p->opt = rest + 1;
316 }
11588263 317 return get_value(p, options, all_opts, flags ^ opt_flags);
4a59fd13 318 }
243e0614 319
3bb0923f
PSU
320 if (ambiguous_option) {
321 error("Ambiguous option: %s "
243e0614
JS
322 "(could be --%s%s or --%s%s)",
323 arg,
324 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
325 ambiguous_option->long_name,
326 (abbrev_flags & OPT_UNSET) ? "no-" : "",
327 abbrev_option->long_name);
3bb0923f
PSU
328 return -3;
329 }
7f275b91 330 if (abbrev_option)
11588263 331 return get_value(p, abbrev_option, all_opts, abbrev_flags);
07fe54db 332 return -2;
4a59fd13
PH
333}
334
51a9949e
RS
335static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
336 const struct option *options)
337{
11588263
JH
338 const struct option *all_opts = options;
339
51a9949e
RS
340 for (; options->type != OPTION_END; options++) {
341 if (!(options->flags & PARSE_OPT_NODASH))
342 continue;
51a9949e 343 if (options->short_name == arg[0] && arg[1] == '\0')
11588263 344 return get_value(p, options, all_opts, OPT_SHORT);
51a9949e
RS
345 }
346 return -2;
347}
348
1121a878 349static void check_typos(const char *arg, const struct option *options)
3a9f0f41
PH
350{
351 if (strlen(arg) < 3)
352 return;
353
59556548 354 if (starts_with(arg, "no-")) {
3a9f0f41
PH
355 error ("did you mean `--%s` (with two dashes ?)", arg);
356 exit(129);
357 }
358
359 for (; options->type != OPTION_END; options++) {
360 if (!options->long_name)
361 continue;
59556548 362 if (starts_with(options->long_name, arg)) {
3a9f0f41
PH
363 error ("did you mean `--%s` (with two dashes ?)", arg);
364 exit(129);
365 }
366 }
367}
368
cb9d398c
PH
369static void parse_options_check(const struct option *opts)
370{
371 int err = 0;
af465af8 372 char short_opts[128];
cb9d398c 373
af465af8 374 memset(short_opts, '\0', sizeof(short_opts));
cb9d398c
PH
375 for (; opts->type != OPTION_END; opts++) {
376 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
1e5ce570
JN
377 (opts->flags & PARSE_OPT_OPTARG))
378 err |= optbug(opts, "uses incompatible flags "
379 "LASTARG_DEFAULT and OPTARG");
af465af8
JH
380 if (opts->short_name) {
381 if (0x7F <= opts->short_name)
382 err |= optbug(opts, "invalid short name");
383 else if (short_opts[opts->short_name]++)
384 err |= optbug(opts, "short name already used");
385 }
a02dd4ff
JN
386 if (opts->flags & PARSE_OPT_NODASH &&
387 ((opts->flags & PARSE_OPT_OPTARG) ||
388 !(opts->flags & PARSE_OPT_NOARG) ||
389 !(opts->flags & PARSE_OPT_NONEG) ||
390 opts->long_name))
391 err |= optbug(opts, "uses feature "
392 "not supported for dashless options");
5c400ed2 393 switch (opts->type) {
b04ba2bb 394 case OPTION_COUNTUP:
5c400ed2
JN
395 case OPTION_BIT:
396 case OPTION_NEGBIT:
397 case OPTION_SET_INT:
5c400ed2
JN
398 case OPTION_NUMBER:
399 if ((opts->flags & PARSE_OPT_OPTARG) ||
400 !(opts->flags & PARSE_OPT_NOARG))
401 err |= optbug(opts, "should not accept an argument");
402 default:
403 ; /* ok. (usually accepts an argument) */
404 }
b6c2a0d4
JH
405 if (opts->argh &&
406 strcspn(opts->argh, " _") != strlen(opts->argh))
407 err |= optbug(opts, "multi-word argh should use dash to separate words");
cb9d398c 408 }
cb9d398c 409 if (err)
1e5ce570 410 exit(128);
cb9d398c
PH
411}
412
7e7bbcb4 413void parse_options_start(struct parse_opt_ctx_t *ctx,
37782920 414 int argc, const char **argv, const char *prefix,
9ca1169f 415 const struct option *options, int flags)
7e7bbcb4
PH
416{
417 memset(ctx, 0, sizeof(*ctx));
5ad0d3d5 418 ctx->argc = ctx->total = argc - 1;
7e7bbcb4
PH
419 ctx->argv = argv + 1;
420 ctx->out = argv;
37782920 421 ctx->prefix = prefix;
a32a4eaa 422 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
7e7bbcb4 423 ctx->flags = flags;
0d260f9a
RS
424 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
425 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
426 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
9ca1169f 427 parse_options_check(options);
7e7bbcb4
PH
428}
429
b221b5ab
NTND
430static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
431{
432 int printed_dashdash = 0;
433
434 for (; opts->type != OPTION_END; opts++) {
435 int has_unset_form = 0;
436 const char *name;
437
438 if (!opts->long_name)
439 continue;
440 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
441 continue;
442 if (opts->flags & PARSE_OPT_NONEG)
443 continue;
444
445 switch (opts->type) {
446 case OPTION_STRING:
447 case OPTION_FILENAME:
448 case OPTION_INTEGER:
449 case OPTION_MAGNITUDE:
450 case OPTION_CALLBACK:
451 case OPTION_BIT:
452 case OPTION_NEGBIT:
453 case OPTION_COUNTUP:
454 case OPTION_SET_INT:
455 has_unset_form = 1;
456 break;
457 default:
458 break;
459 }
460 if (!has_unset_form)
461 continue;
462
463 if (skip_prefix(opts->long_name, "no-", &name)) {
464 if (nr_noopts < 0)
465 printf(" --%s", name);
466 } else if (nr_noopts >= 0) {
467 if (nr_noopts && !printed_dashdash) {
468 printf(" --");
469 printed_dashdash = 1;
470 }
471 printf(" --no-%s", opts->long_name);
472 nr_noopts++;
473 }
474 }
475}
476
b9d7f4b4
NTND
477static int show_gitcomp(struct parse_opt_ctx_t *ctx,
478 const struct option *opts)
479{
b221b5ab
NTND
480 const struct option *original_opts = opts;
481 int nr_noopts = 0;
482
b9d7f4b4
NTND
483 for (; opts->type != OPTION_END; opts++) {
484 const char *suffix = "";
485
486 if (!opts->long_name)
487 continue;
488 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
489 continue;
490
491 switch (opts->type) {
492 case OPTION_GROUP:
493 continue;
494 case OPTION_STRING:
495 case OPTION_FILENAME:
496 case OPTION_INTEGER:
497 case OPTION_MAGNITUDE:
498 case OPTION_CALLBACK:
499 if (opts->flags & PARSE_OPT_NOARG)
500 break;
501 if (opts->flags & PARSE_OPT_OPTARG)
502 break;
503 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
504 break;
505 suffix = "=";
506 break;
507 default:
508 break;
509 }
ebc4a04e
NTND
510 if (opts->flags & PARSE_OPT_COMP_ARG)
511 suffix = "=";
b221b5ab
NTND
512 if (starts_with(opts->long_name, "no-"))
513 nr_noopts++;
b9d7f4b4
NTND
514 printf(" --%s%s", opts->long_name, suffix);
515 }
b221b5ab
NTND
516 show_negated_gitcomp(original_opts, -1);
517 show_negated_gitcomp(original_opts, nr_noopts);
b9d7f4b4
NTND
518 fputc('\n', stdout);
519 exit(0);
520}
521
47e9cd28
TR
522static int usage_with_options_internal(struct parse_opt_ctx_t *,
523 const char * const *,
9c7304e3 524 const struct option *, int, int);
dd3bf0f4 525
ff43ec3e
PH
526int parse_options_step(struct parse_opt_ctx_t *ctx,
527 const struct option *options,
528 const char * const usagestr[])
4a59fd13 529{
b92891f9
RS
530 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
531
26141b5b
PH
532 /* we must reset ->opt, unknown short option leave it dangling */
533 ctx->opt = NULL;
534
ff43ec3e
PH
535 for (; ctx->argc; ctx->argc--, ctx->argv++) {
536 const char *arg = ctx->argv[0];
4a59fd13
PH
537
538 if (*arg != '-' || !arg[1]) {
51a9949e
RS
539 if (parse_nodash_opt(ctx, arg, options) == 0)
540 continue;
ff43ec3e 541 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
979240fe 542 return PARSE_OPT_NON_OPTION;
ff43ec3e 543 ctx->out[ctx->cpidx++] = ctx->argv[0];
4a59fd13
PH
544 continue;
545 }
546
5ad0d3d5
RS
547 /* lone -h asks for help */
548 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
549 goto show_usage;
550
b9d7f4b4
NTND
551 /* lone --git-completion-helper is asked by git-completion.bash */
552 if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
553 return show_gitcomp(ctx, options);
554
4a59fd13 555 if (arg[1] != '-') {
ff43ec3e 556 ctx->opt = arg + 1;
07fe54db
PH
557 switch (parse_short_opt(ctx, options)) {
558 case -1:
3bb0923f 559 return PARSE_OPT_ERROR;
07fe54db 560 case -2:
38916c5b
RS
561 if (ctx->opt)
562 check_typos(arg + 1, options);
5ad0d3d5
RS
563 if (internal_help && *ctx->opt == 'h')
564 goto show_usage;
b5ce3a54 565 goto unknown;
07fe54db 566 }
ff43ec3e 567 if (ctx->opt)
3a9f0f41 568 check_typos(arg + 1, options);
ff43ec3e 569 while (ctx->opt) {
07fe54db
PH
570 switch (parse_short_opt(ctx, options)) {
571 case -1:
3bb0923f 572 return PARSE_OPT_ERROR;
07fe54db 573 case -2:
5ad0d3d5
RS
574 if (internal_help && *ctx->opt == 'h')
575 goto show_usage;
576
26141b5b
PH
577 /* fake a short option thing to hide the fact that we may have
578 * started to parse aggregated stuff
579 *
580 * This is leaky, too bad.
581 */
582 ctx->argv[0] = xstrdup(ctx->opt - 1);
583 *(char *)ctx->argv[0] = '-';
b5ce3a54 584 goto unknown;
07fe54db 585 }
3a9f0f41 586 }
4a59fd13
PH
587 continue;
588 }
589
590 if (!arg[2]) { /* "--" */
ff43ec3e
PH
591 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
592 ctx->argc--;
593 ctx->argv++;
4a59fd13
PH
594 }
595 break;
596 }
597
b92891f9 598 if (internal_help && !strcmp(arg + 2, "help-all"))
47e9cd28 599 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
b92891f9 600 if (internal_help && !strcmp(arg + 2, "help"))
ac20ff6d 601 goto show_usage;
07fe54db
PH
602 switch (parse_long_opt(ctx, arg + 2, options)) {
603 case -1:
3bb0923f 604 return PARSE_OPT_ERROR;
07fe54db 605 case -2:
b5ce3a54 606 goto unknown;
3bb0923f
PSU
607 case -3:
608 goto show_usage;
07fe54db 609 }
b5ce3a54
RS
610 continue;
611unknown:
612 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
613 return PARSE_OPT_UNKNOWN;
614 ctx->out[ctx->cpidx++] = ctx->argv[0];
615 ctx->opt = NULL;
ff43ec3e
PH
616 }
617 return PARSE_OPT_DONE;
ac20ff6d 618
ac20ff6d 619 show_usage:
3bb0923f 620 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
ff43ec3e
PH
621}
622
623int parse_options_end(struct parse_opt_ctx_t *ctx)
624{
f919ffeb 625 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
ff43ec3e
PH
626 ctx->out[ctx->cpidx + ctx->argc] = NULL;
627 return ctx->cpidx + ctx->argc;
628}
629
37782920
SB
630int parse_options(int argc, const char **argv, const char *prefix,
631 const struct option *options, const char * const usagestr[],
632 int flags)
ff43ec3e
PH
633{
634 struct parse_opt_ctx_t ctx;
635
9ca1169f 636 parse_options_start(&ctx, argc, argv, prefix, options, flags);
ff43ec3e
PH
637 switch (parse_options_step(&ctx, options, usagestr)) {
638 case PARSE_OPT_HELP:
3bb0923f 639 case PARSE_OPT_ERROR:
ff43ec3e 640 exit(129);
979240fe 641 case PARSE_OPT_NON_OPTION:
ff43ec3e
PH
642 case PARSE_OPT_DONE:
643 break;
644 default: /* PARSE_OPT_UNKNOWN */
07fe54db
PH
645 if (ctx.argv[0][1] == '-') {
646 error("unknown option `%s'", ctx.argv[0] + 2);
b141a478 647 } else if (isascii(*ctx.opt)) {
07fe54db 648 error("unknown switch `%c'", *ctx.opt);
b141a478
EFL
649 } else {
650 error("unknown non-ascii option in string: `%s'",
651 ctx.argv[0]);
07fe54db
PH
652 }
653 usage_with_options(usagestr, options);
4a59fd13
PH
654 }
655
76759c7d 656 precompose_argv(argc, argv);
7e7bbcb4 657 return parse_options_end(&ctx);
4a59fd13 658}
d7a38c54 659
9c7304e3 660static int usage_argh(const struct option *opts, FILE *outfile)
29f25d49
SB
661{
662 const char *s;
34aec9f5 663 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
29f25d49
SB
664 if (opts->flags & PARSE_OPT_OPTARG)
665 if (opts->long_name)
666 s = literal ? "[=%s]" : "[=<%s>]";
667 else
668 s = literal ? "[%s]" : "[<%s>]";
669 else
670 s = literal ? " %s" : " <%s>";
c0821965 671 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
29f25d49
SB
672}
673
d7a38c54
PH
674#define USAGE_OPTS_WIDTH 24
675#define USAGE_GAP 2
676
47e9cd28
TR
677static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
678 const char * const *usagestr,
679 const struct option *opts, int full, int err)
d7a38c54 680{
9c7304e3 681 FILE *outfile = err ? stderr : stdout;
a6304fa4 682 int need_newline;
9c7304e3 683
49b61802
RS
684 if (!usagestr)
685 return PARSE_OPT_HELP;
686
47e9cd28
TR
687 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
688 fprintf(outfile, "cat <<\\EOF\n");
689
54e6dc7d 690 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
f389c808 691 while (*usagestr && **usagestr)
66f5f6dc
ÆAB
692 /*
693 * TRANSLATORS: the colon here should align with the
694 * one in "usage: %s" translation.
695 */
54e6dc7d 696 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
44d86e91 697 while (*usagestr) {
54e6dc7d
NTND
698 if (**usagestr)
699 fprintf_ln(outfile, _(" %s"), _(*usagestr));
700 else
1a9bf1e1 701 fputc('\n', outfile);
44d86e91
JK
702 usagestr++;
703 }
d7a38c54 704
a6304fa4 705 need_newline = 1;
d7a38c54
PH
706
707 for (; opts->type != OPTION_END; opts++) {
708 size_t pos;
709 int pad;
710
711 if (opts->type == OPTION_GROUP) {
9c7304e3 712 fputc('\n', outfile);
a6304fa4 713 need_newline = 0;
d7a38c54 714 if (*opts->help)
54e6dc7d 715 fprintf(outfile, "%s\n", _(opts->help));
d7a38c54
PH
716 continue;
717 }
dd3bf0f4
PH
718 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
719 continue;
d7a38c54 720
a6304fa4
BC
721 if (need_newline) {
722 fputc('\n', outfile);
723 need_newline = 0;
724 }
725
9c7304e3 726 pos = fprintf(outfile, " ");
cbb08c2e 727 if (opts->short_name) {
51a9949e 728 if (opts->flags & PARSE_OPT_NODASH)
9c7304e3 729 pos += fprintf(outfile, "%c", opts->short_name);
51a9949e 730 else
9c7304e3 731 pos += fprintf(outfile, "-%c", opts->short_name);
51a9949e 732 }
d7a38c54 733 if (opts->long_name && opts->short_name)
9c7304e3 734 pos += fprintf(outfile, ", ");
d7a38c54 735 if (opts->long_name)
cbb08c2e 736 pos += fprintf(outfile, "--%s", opts->long_name);
e0319ff5 737 if (opts->type == OPTION_NUMBER)
c0821965 738 pos += utf8_fprintf(outfile, _("-NUM"));
d7a38c54 739
b57c68a6
JN
740 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
741 !(opts->flags & PARSE_OPT_NOARG))
9c7304e3 742 pos += usage_argh(opts, outfile);
d7a38c54 743
f389c808
AR
744 if (pos <= USAGE_OPTS_WIDTH)
745 pad = USAGE_OPTS_WIDTH - pos;
d7a38c54 746 else {
9c7304e3 747 fputc('\n', outfile);
d7a38c54
PH
748 pad = USAGE_OPTS_WIDTH;
749 }
54e6dc7d 750 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
d7a38c54 751 }
9c7304e3 752 fputc('\n', outfile);
f389c808 753
47e9cd28
TR
754 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
755 fputs("EOF\n", outfile);
756
ee68b87a 757 return PARSE_OPT_HELP;
d7a38c54 758}
0ce865b1 759
c2e86add 760void NORETURN usage_with_options(const char * const *usagestr,
ff43ec3e 761 const struct option *opts)
dd3bf0f4 762{
47e9cd28 763 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
ff43ec3e 764 exit(129);
dd3bf0f4
PH
765}
766
c2e86add 767void NORETURN usage_msg_opt(const char *msg,
451bb210
CC
768 const char * const *usagestr,
769 const struct option *options)
770{
87433261 771 fprintf(stderr, "fatal: %s\n\n", msg);
451bb210
CC
772 usage_with_options(usagestr, options);
773}
774
a469a101
JK
775#undef opterror
776int opterror(const struct option *opt, const char *reason, int flags)
777{
778 if (flags & OPT_SHORT)
779 return error("switch `%c' %s", opt->short_name, reason);
780 if (flags & OPT_UNSET)
781 return error("option `no-%s' %s", opt->long_name, reason);
782 return error("option `%s' %s", opt->long_name, reason);
783}