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