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