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