]>
Commit | Line | Data |
---|---|---|
c84142e8 | 1 | /* Hierarchial argument parsing, layered over getopt |
dff8da6b | 2 | Copyright (C) 1995-2024 Free Software Foundation, Inc. |
c84142e8 UD |
3 | This file is part of the GNU C Library. |
4 | Written by Miles Bader <miles@gnu.ai.mit.edu>. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
c84142e8 UD |
10 | |
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
c84142e8 | 15 | |
41bdb6e2 | 16 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 17 | License along with the GNU C Library; if not, see |
5a82c748 | 18 | <https://www.gnu.org/licenses/>. */ |
c84142e8 UD |
19 | |
20 | #ifdef HAVE_CONFIG_H | |
21 | #include <config.h> | |
22 | #endif | |
23 | ||
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | #include <unistd.h> | |
27 | #include <limits.h> | |
28 | #include <getopt.h> | |
511676f7 | 29 | #include <getopt_int.h> |
c84142e8 UD |
30 | |
31 | #ifndef _ | |
32 | /* This is for other GNU distributions with internationalized messages. | |
33 | When compiling libc, the _ macro is predefined. */ | |
883ba315 UD |
34 | # if defined HAVE_LIBINTL_H || defined _LIBC |
35 | # include <libintl.h> | |
0bb258e3 UD |
36 | # ifdef _LIBC |
37 | # undef dgettext | |
71319b9c | 38 | # define dgettext(domain, msgid) \ |
56d25bb8 | 39 | __dcgettext (domain, msgid, LC_MESSAGES) |
0bb258e3 | 40 | # endif |
883ba315 UD |
41 | # else |
42 | # define dgettext(domain, msgid) (msgid) | |
43 | # define gettext(msgid) (msgid) | |
44 | # endif | |
c84142e8 | 45 | #endif |
883ba315 UD |
46 | #ifndef N_ |
47 | # define N_(msgid) (msgid) | |
c84142e8 UD |
48 | #endif |
49 | ||
8345a760 | 50 | #include <argp.h> |
c84142e8 UD |
51 | #include "argp-namefrob.h" |
52 | ||
53 | /* Getopt return values. */ | |
54 | #define KEY_END (-1) /* The end of the options. */ | |
55 | #define KEY_ARG 1 /* A non-option argument. */ | |
56 | #define KEY_ERR '?' /* An error parsing the options. */ | |
57 | ||
58 | /* The meta-argument used to prevent any further arguments being interpreted | |
59 | as options. */ | |
60 | #define QUOTE "--" | |
61 | ||
62 | /* The number of bits we steal in a long-option value for our own use. */ | |
63 | #define GROUP_BITS CHAR_BIT | |
64 | ||
65 | /* The number of bits available for the user value. */ | |
66 | #define USER_BITS ((sizeof ((struct option *)0)->val * CHAR_BIT) - GROUP_BITS) | |
67 | #define USER_MASK ((1 << USER_BITS) - 1) | |
68 | ||
69 | /* EZ alias for ARGP_ERR_UNKNOWN. */ | |
70 | #define EBADKEY ARGP_ERR_UNKNOWN | |
71 | \f | |
72 | /* Default options. */ | |
73 | ||
74 | /* When argp is given the --HANG switch, _ARGP_HANG is set and argp will sleep | |
75 | for one second intervals, decrementing _ARGP_HANG until it's zero. Thus | |
76 | you can force the program to continue by attaching a debugger and setting | |
511676f7 UD |
77 | it to 0 yourself. */ |
78 | static volatile int _argp_hang; | |
c84142e8 UD |
79 | |
80 | #define OPT_PROGNAME -2 | |
81 | #define OPT_USAGE -3 | |
82 | #define OPT_HANG -4 | |
83 | ||
84 | static const struct argp_option argp_default_options[] = | |
85 | { | |
8c6de69d | 86 | {"help", '?', 0, 0, N_("Give this help list"), -1}, |
d705269e | 87 | {"usage", OPT_USAGE, 0, 0, N_("Give a short usage message")}, |
0e2b9cdd RM |
88 | {"program-name",OPT_PROGNAME, N_("NAME"), OPTION_HIDDEN, |
89 | N_("Set the program name")}, | |
90 | {"HANG", OPT_HANG, N_("SECS"), OPTION_ARG_OPTIONAL | OPTION_HIDDEN, | |
91 | N_("Hang for SECS seconds (default 3600)")}, | |
c84142e8 UD |
92 | {0, 0} |
93 | }; | |
94 | ||
95 | static error_t | |
96 | argp_default_parser (int key, char *arg, struct argp_state *state) | |
97 | { | |
98 | switch (key) | |
99 | { | |
100 | case '?': | |
101 | __argp_state_help (state, state->out_stream, ARGP_HELP_STD_HELP); | |
102 | break; | |
103 | case OPT_USAGE: | |
104 | __argp_state_help (state, state->out_stream, | |
105 | ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK); | |
106 | break; | |
107 | ||
108 | case OPT_PROGNAME: /* Set the program name. */ | |
f39941e4 | 109 | #if defined _LIBC || HAVE_DECL_PROGRAM_INVOCATION_NAME |
5a97622d | 110 | program_invocation_name = arg; |
f39941e4 | 111 | #endif |
c84142e8 UD |
112 | /* [Note that some systems only have PROGRAM_INVOCATION_SHORT_NAME (aka |
113 | __PROGNAME), in which case, PROGRAM_INVOCATION_NAME is just defined | |
114 | to be that, so we have to be a bit careful here.] */ | |
c84142e8 | 115 | |
5a97622d | 116 | /* Update what we use for messages. */ |
f39941e4 UD |
117 | state->name = strrchr (arg, '/'); |
118 | if (state->name) | |
119 | state->name++; | |
120 | else | |
121 | state->name = arg; | |
122 | ||
123 | #if defined _LIBC || HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME | |
124 | program_invocation_short_name = state->name; | |
125 | #endif | |
5a97622d | 126 | |
c84142e8 UD |
127 | if ((state->flags & (ARGP_PARSE_ARGV0 | ARGP_NO_ERRS)) |
128 | == ARGP_PARSE_ARGV0) | |
1fb05e3d | 129 | /* Update what getopt uses too. */ |
f39941e4 | 130 | state->argv[0] = arg; |
c84142e8 UD |
131 | |
132 | break; | |
133 | ||
134 | case OPT_HANG: | |
a3708cf6 | 135 | _argp_hang = arg ? strtol (arg, NULL, 10) : 3600; |
c84142e8 UD |
136 | while (_argp_hang-- > 0) |
137 | __sleep (1); | |
138 | break; | |
139 | ||
140 | default: | |
141 | return EBADKEY; | |
142 | } | |
143 | return 0; | |
144 | } | |
145 | ||
146 | static const struct argp argp_default_argp = | |
9ecb7b36 | 147 | {argp_default_options, &argp_default_parser, NULL, NULL, NULL, NULL, "libc"}; |
c84142e8 UD |
148 | |
149 | \f | |
150 | static const struct argp_option argp_version_options[] = | |
151 | { | |
8c6de69d | 152 | {"version", 'V', 0, 0, N_("Print program version"), -1}, |
c84142e8 UD |
153 | {0, 0} |
154 | }; | |
155 | ||
156 | static error_t | |
157 | argp_version_parser (int key, char *arg, struct argp_state *state) | |
158 | { | |
159 | switch (key) | |
160 | { | |
161 | case 'V': | |
162 | if (argp_program_version_hook) | |
163 | (*argp_program_version_hook) (state->out_stream, state); | |
164 | else if (argp_program_version) | |
165 | fprintf (state->out_stream, "%s\n", argp_program_version); | |
166 | else | |
9184d3db UD |
167 | __argp_error (state, dgettext (state->root_argp->argp_domain, |
168 | "(PROGRAM ERROR) No version known!?")); | |
c84142e8 UD |
169 | if (! (state->flags & ARGP_NO_EXIT)) |
170 | exit (0); | |
171 | break; | |
172 | default: | |
173 | return EBADKEY; | |
174 | } | |
175 | return 0; | |
176 | } | |
177 | ||
178 | static const struct argp argp_version_argp = | |
9ecb7b36 | 179 | {argp_version_options, &argp_version_parser, NULL, NULL, NULL, NULL, "libc"}; |
c84142e8 UD |
180 | \f |
181 | /* Returns the offset into the getopt long options array LONG_OPTIONS of a | |
182 | long option with called NAME, or -1 if none is found. Passing NULL as | |
183 | NAME will return the number of options. */ | |
184 | static int | |
185 | find_long_option (struct option *long_options, const char *name) | |
186 | { | |
187 | struct option *l = long_options; | |
188 | while (l->name != NULL) | |
189 | if (name != NULL && strcmp (l->name, name) == 0) | |
190 | return l - long_options; | |
191 | else | |
192 | l++; | |
193 | if (name == NULL) | |
194 | return l - long_options; | |
195 | else | |
196 | return -1; | |
197 | } | |
c84142e8 | 198 | |
c84142e8 UD |
199 | \f |
200 | /* The state of a `group' during parsing. Each group corresponds to a | |
201 | particular argp structure from the tree of such descending from the top | |
202 | level argp passed to argp_parse. */ | |
203 | struct group | |
204 | { | |
205 | /* This group's parsing function. */ | |
206 | argp_parser_t parser; | |
207 | ||
1fb05e3d UD |
208 | /* Which argp this group is from. */ |
209 | const struct argp *argp; | |
210 | ||
c84142e8 UD |
211 | /* Points to the point in SHORT_OPTS corresponding to the end of the short |
212 | options for this group. We use it to determine from which group a | |
213 | particular short options is from. */ | |
214 | char *short_end; | |
215 | ||
7f0d9e61 | 216 | /* The number of non-option args successfully handled by this parser. */ |
c84142e8 UD |
217 | unsigned args_processed; |
218 | ||
219 | /* This group's parser's parent's group. */ | |
220 | struct group *parent; | |
221 | unsigned parent_index; /* And the our position in the parent. */ | |
222 | ||
223 | /* These fields are swapped into and out of the state structure when | |
224 | calling this group's parser. */ | |
225 | void *input, **child_inputs; | |
226 | void *hook; | |
227 | }; | |
228 | ||
229 | /* Call GROUP's parser with KEY and ARG, swapping any group-specific info | |
230 | from STATE before calling, and back into state afterwards. If GROUP has | |
231 | no parser, EBADKEY is returned. */ | |
232 | static error_t | |
233 | group_parse (struct group *group, struct argp_state *state, int key, char *arg) | |
234 | { | |
235 | if (group->parser) | |
236 | { | |
237 | error_t err; | |
238 | state->hook = group->hook; | |
239 | state->input = group->input; | |
240 | state->child_inputs = group->child_inputs; | |
241 | state->arg_num = group->args_processed; | |
242 | err = (*group->parser)(key, arg, state); | |
243 | group->hook = state->hook; | |
244 | return err; | |
245 | } | |
246 | else | |
247 | return EBADKEY; | |
248 | } | |
249 | \f | |
250 | struct parser | |
251 | { | |
252 | const struct argp *argp; | |
253 | ||
254 | /* SHORT_OPTS is the getopt short options string for the union of all the | |
255 | groups of options. */ | |
256 | char *short_opts; | |
257 | /* LONG_OPTS is the array of getop long option structures for the union of | |
258 | all the groups of options. */ | |
259 | struct option *long_opts; | |
511676f7 UD |
260 | /* OPT_DATA is the getopt data used for the re-entrant getopt. */ |
261 | struct _getopt_data opt_data; | |
c84142e8 UD |
262 | |
263 | /* States of the various parsing groups. */ | |
264 | struct group *groups; | |
265 | /* The end of the GROUPS array. */ | |
266 | struct group *egroup; | |
267 | /* An vector containing storage for the CHILD_INPUTS field in all groups. */ | |
268 | void **child_inputs; | |
269 | ||
270 | /* True if we think using getopt is still useful; if false, then | |
271 | remaining arguments are just passed verbatim with ARGP_KEY_ARG. This is | |
272 | cleared whenever getopt returns KEY_END, but may be set again if the user | |
273 | moves the next argument pointer backwards. */ | |
274 | int try_getopt; | |
275 | ||
276 | /* State block supplied to parsing routines. */ | |
277 | struct argp_state state; | |
278 | ||
279 | /* Memory used by this parser. */ | |
280 | void *storage; | |
281 | }; | |
282 | \f | |
283 | /* The next usable entries in the various parser tables being filled in by | |
284 | convert_options. */ | |
285 | struct parser_convert_state | |
286 | { | |
287 | struct parser *parser; | |
288 | char *short_end; | |
289 | struct option *long_end; | |
290 | void **child_inputs_end; | |
291 | }; | |
292 | ||
293 | /* Converts all options in ARGP (which is put in GROUP) and ancestors | |
294 | into getopt options stored in SHORT_OPTS and LONG_OPTS; SHORT_END and | |
295 | CVT->LONG_END are the points at which new options are added. Returns the | |
296 | next unused group entry. CVT holds state used during the conversion. */ | |
297 | static struct group * | |
298 | convert_options (const struct argp *argp, | |
299 | struct group *parent, unsigned parent_index, | |
300 | struct group *group, struct parser_convert_state *cvt) | |
301 | { | |
302 | /* REAL is the most recent non-alias value of OPT. */ | |
303 | const struct argp_option *real = argp->options; | |
304 | const struct argp_child *children = argp->children; | |
305 | ||
306 | if (real || argp->parser) | |
307 | { | |
308 | const struct argp_option *opt; | |
309 | ||
310 | if (real) | |
311 | for (opt = real; !__option_is_end (opt); opt++) | |
312 | { | |
313 | if (! (opt->flags & OPTION_ALIAS)) | |
314 | /* OPT isn't an alias, so we can use values from it. */ | |
315 | real = opt; | |
316 | ||
317 | if (! (real->flags & OPTION_DOC)) | |
318 | /* A real option (not just documentation). */ | |
319 | { | |
320 | if (__option_is_short (opt)) | |
321 | /* OPT can be used as a short option. */ | |
322 | { | |
323 | *cvt->short_end++ = opt->key; | |
324 | if (real->arg) | |
325 | { | |
326 | *cvt->short_end++ = ':'; | |
327 | if (real->flags & OPTION_ARG_OPTIONAL) | |
328 | *cvt->short_end++ = ':'; | |
329 | } | |
330 | *cvt->short_end = '\0'; /* keep 0 terminated */ | |
331 | } | |
332 | ||
333 | if (opt->name | |
334 | && find_long_option (cvt->parser->long_opts, opt->name) < 0) | |
335 | /* OPT can be used as a long option. */ | |
336 | { | |
337 | cvt->long_end->name = opt->name; | |
338 | cvt->long_end->has_arg = | |
339 | (real->arg | |
340 | ? (real->flags & OPTION_ARG_OPTIONAL | |
341 | ? optional_argument | |
342 | : required_argument) | |
343 | : no_argument); | |
344 | cvt->long_end->flag = 0; | |
345 | /* we add a disambiguating code to all the user's | |
346 | values (which is removed before we actually call | |
347 | the function to parse the value); this means that | |
348 | the user loses use of the high 8 bits in all his | |
349 | values (the sign of the lower bits is preserved | |
350 | however)... */ | |
351 | cvt->long_end->val = | |
8c6de69d | 352 | ((opt->key ? opt->key : real->key) & USER_MASK) |
c84142e8 UD |
353 | + (((group - cvt->parser->groups) + 1) << USER_BITS); |
354 | ||
355 | /* Keep the LONG_OPTS list terminated. */ | |
356 | (++cvt->long_end)->name = NULL; | |
357 | } | |
358 | } | |
359 | } | |
360 | ||
361 | group->parser = argp->parser; | |
1fb05e3d | 362 | group->argp = argp; |
c84142e8 UD |
363 | group->short_end = cvt->short_end; |
364 | group->args_processed = 0; | |
365 | group->parent = parent; | |
366 | group->parent_index = parent_index; | |
367 | group->input = 0; | |
368 | group->hook = 0; | |
369 | group->child_inputs = 0; | |
370 | ||
371 | if (children) | |
372 | /* Assign GROUP's CHILD_INPUTS field some space from | |
8c6de69d | 373 | CVT->child_inputs_end.*/ |
c84142e8 UD |
374 | { |
375 | unsigned num_children = 0; | |
376 | while (children[num_children].argp) | |
377 | num_children++; | |
378 | group->child_inputs = cvt->child_inputs_end; | |
379 | cvt->child_inputs_end += num_children; | |
380 | } | |
381 | ||
382 | parent = group++; | |
383 | } | |
384 | else | |
385 | parent = 0; | |
386 | ||
387 | if (children) | |
388 | { | |
389 | unsigned index = 0; | |
390 | while (children->argp) | |
391 | group = | |
392 | convert_options (children++->argp, parent, index++, group, cvt); | |
393 | } | |
394 | ||
395 | return group; | |
396 | } | |
397 | ||
6f65e668 | 398 | /* Find the merged set of getopt options, with keys appropriately prefixed. */ |
c84142e8 UD |
399 | static void |
400 | parser_convert (struct parser *parser, const struct argp *argp, int flags) | |
401 | { | |
402 | struct parser_convert_state cvt; | |
403 | ||
404 | cvt.parser = parser; | |
405 | cvt.short_end = parser->short_opts; | |
406 | cvt.long_end = parser->long_opts; | |
407 | cvt.child_inputs_end = parser->child_inputs; | |
408 | ||
409 | if (flags & ARGP_IN_ORDER) | |
410 | *cvt.short_end++ = '-'; | |
411 | else if (flags & ARGP_NO_ARGS) | |
412 | *cvt.short_end++ = '+'; | |
413 | *cvt.short_end = '\0'; | |
414 | ||
415 | cvt.long_end->name = NULL; | |
416 | ||
417 | parser->argp = argp; | |
418 | ||
419 | if (argp) | |
420 | parser->egroup = convert_options (argp, 0, 0, parser->groups, &cvt); | |
421 | else | |
422 | parser->egroup = parser->groups; /* No parsers at all! */ | |
423 | } | |
424 | \f | |
425 | /* Lengths of various parser fields which we will allocated. */ | |
426 | struct parser_sizes | |
427 | { | |
428 | size_t short_len; /* Getopt short options string. */ | |
429 | size_t long_len; /* Getopt long options vector. */ | |
430 | size_t num_groups; /* Group structures we allocate. */ | |
431 | size_t num_child_inputs; /* Child input slots. */ | |
432 | }; | |
433 | ||
434 | /* For ARGP, increments the NUM_GROUPS field in SZS by the total number of | |
435 | argp structures descended from it, and the SHORT_LEN & LONG_LEN fields by | |
436 | the maximum lengths of the resulting merged getopt short options string and | |
437 | long-options array, respectively. */ | |
438 | static void | |
439 | calc_sizes (const struct argp *argp, struct parser_sizes *szs) | |
440 | { | |
441 | const struct argp_child *child = argp->children; | |
442 | const struct argp_option *opt = argp->options; | |
443 | ||
444 | if (opt || argp->parser) | |
445 | { | |
446 | szs->num_groups++; | |
447 | if (opt) | |
448 | { | |
449 | int num_opts = 0; | |
450 | while (!__option_is_end (opt++)) | |
451 | num_opts++; | |
452 | szs->short_len += num_opts * 3; /* opt + up to 2 `:'s */ | |
453 | szs->long_len += num_opts; | |
454 | } | |
455 | } | |
456 | ||
457 | if (child) | |
458 | while (child->argp) | |
459 | { | |
460 | calc_sizes ((child++)->argp, szs); | |
461 | szs->num_child_inputs++; | |
462 | } | |
463 | } | |
464 | ||
465 | /* Initializes PARSER to parse ARGP in a manner described by FLAGS. */ | |
466 | static error_t | |
467 | parser_init (struct parser *parser, const struct argp *argp, | |
468 | int argc, char **argv, int flags, void *input) | |
469 | { | |
470 | error_t err = 0; | |
471 | struct group *group; | |
472 | struct parser_sizes szs; | |
511676f7 | 473 | struct _getopt_data opt_data = _GETOPT_DATA_INITIALIZER; |
c84142e8 UD |
474 | |
475 | szs.short_len = (flags & ARGP_NO_ARGS) ? 0 : 1; | |
476 | szs.long_len = 0; | |
477 | szs.num_groups = 0; | |
478 | szs.num_child_inputs = 0; | |
479 | ||
480 | if (argp) | |
481 | calc_sizes (argp, &szs); | |
482 | ||
483 | /* Lengths of the various bits of storage used by PARSER. */ | |
484 | #define GLEN (szs.num_groups + 1) * sizeof (struct group) | |
485 | #define CLEN (szs.num_child_inputs * sizeof (void *)) | |
486 | #define LLEN ((szs.long_len + 1) * sizeof (struct option)) | |
487 | #define SLEN (szs.short_len + 1) | |
488 | ||
489 | parser->storage = malloc (GLEN + CLEN + LLEN + SLEN); | |
490 | if (! parser->storage) | |
491 | return ENOMEM; | |
492 | ||
493 | parser->groups = parser->storage; | |
494 | parser->child_inputs = parser->storage + GLEN; | |
495 | parser->long_opts = parser->storage + GLEN + CLEN; | |
496 | parser->short_opts = parser->storage + GLEN + CLEN + LLEN; | |
511676f7 | 497 | parser->opt_data = opt_data; |
c84142e8 UD |
498 | |
499 | memset (parser->child_inputs, 0, szs.num_child_inputs * sizeof (void *)); | |
500 | parser_convert (parser, argp, flags); | |
501 | ||
c84142e8 | 502 | memset (&parser->state, 0, sizeof (struct argp_state)); |
ebbad4cc | 503 | parser->state.root_argp = parser->argp; |
c84142e8 UD |
504 | parser->state.argc = argc; |
505 | parser->state.argv = argv; | |
5a97622d | 506 | parser->state.flags = flags; |
c84142e8 UD |
507 | parser->state.err_stream = stderr; |
508 | parser->state.out_stream = stdout; | |
509 | parser->state.next = 0; /* Tell getopt to initialize. */ | |
1fb05e3d | 510 | parser->state.pstate = parser; |
c84142e8 | 511 | |
5a97622d UD |
512 | parser->try_getopt = 1; |
513 | ||
c84142e8 UD |
514 | /* Call each parser for the first time, giving it a chance to propagate |
515 | values to child parsers. */ | |
516 | if (parser->groups < parser->egroup) | |
517 | parser->groups->input = input; | |
518 | for (group = parser->groups; | |
519 | group < parser->egroup && (!err || err == EBADKEY); | |
520 | group++) | |
521 | { | |
522 | if (group->parent) | |
523 | /* If a child parser, get the initial input value from the parent. */ | |
524 | group->input = group->parent->child_inputs[group->parent_index]; | |
ebbad4cc UD |
525 | |
526 | if (!group->parser | |
527 | && group->argp->children && group->argp->children->argp) | |
528 | /* For the special case where no parsing function is supplied for an | |
529 | argp, propagate its input to its first child, if any (this just | |
530 | makes very simple wrapper argps more convenient). */ | |
531 | group->child_inputs[0] = group->input; | |
532 | ||
c84142e8 UD |
533 | err = group_parse (group, &parser->state, ARGP_KEY_INIT, 0); |
534 | } | |
535 | if (err == EBADKEY) | |
536 | err = 0; /* Some parser didn't understand. */ | |
537 | ||
538 | if (err) | |
539 | return err; | |
540 | ||
c84142e8 UD |
541 | if (parser->state.flags & ARGP_NO_ERRS) |
542 | { | |
511676f7 | 543 | parser->opt_data.opterr = 0; |
c84142e8 UD |
544 | if (parser->state.flags & ARGP_PARSE_ARGV0) |
545 | /* getopt always skips ARGV[0], so we have to fake it out. As long | |
546 | as OPTERR is 0, then it shouldn't actually try to access it. */ | |
547 | parser->state.argv--, parser->state.argc++; | |
548 | } | |
549 | else | |
511676f7 | 550 | parser->opt_data.opterr = 1; /* Print error messages. */ |
c84142e8 | 551 | |
5a97622d UD |
552 | if (parser->state.argv == argv && argv[0]) |
553 | /* There's an argv[0]; use it for messages. */ | |
554 | { | |
555 | char *short_name = strrchr (argv[0], '/'); | |
556 | parser->state.name = short_name ? short_name + 1 : argv[0]; | |
557 | } | |
558 | else | |
f39941e4 | 559 | parser->state.name = __argp_short_program_name (); |
5a97622d | 560 | |
c84142e8 UD |
561 | return 0; |
562 | } | |
563 | \f | |
564 | /* Free any storage consumed by PARSER (but not PARSER itself). */ | |
565 | static error_t | |
566 | parser_finalize (struct parser *parser, | |
567 | error_t err, int arg_ebadkey, int *end_index) | |
568 | { | |
569 | struct group *group; | |
570 | ||
c84142e8 UD |
571 | if (err == EBADKEY && arg_ebadkey) |
572 | /* Suppress errors generated by unparsed arguments. */ | |
573 | err = 0; | |
574 | ||
575 | if (! err) | |
94b78bb2 UD |
576 | { |
577 | if (parser->state.next == parser->state.argc) | |
578 | /* We successfully parsed all arguments! Call all the parsers again, | |
579 | just a few more times... */ | |
580 | { | |
581 | for (group = parser->groups; | |
582 | group < parser->egroup && (!err || err==EBADKEY); | |
583 | group++) | |
584 | if (group->args_processed == 0) | |
585 | err = group_parse (group, &parser->state, ARGP_KEY_NO_ARGS, 0); | |
7603ea28 UD |
586 | for (group = parser->egroup - 1; |
587 | group >= parser->groups && (!err || err==EBADKEY); | |
588 | group--) | |
94b78bb2 UD |
589 | err = group_parse (group, &parser->state, ARGP_KEY_END, 0); |
590 | ||
591 | if (err == EBADKEY) | |
592 | err = 0; /* Some parser didn't understand. */ | |
593 | ||
594 | /* Tell the user that all arguments are parsed. */ | |
595 | if (end_index) | |
596 | *end_index = parser->state.next; | |
597 | } | |
598 | else if (end_index) | |
599 | /* Return any remaining arguments to the user. */ | |
600 | *end_index = parser->state.next; | |
601 | else | |
602 | /* No way to return the remaining arguments, they must be bogus. */ | |
603 | { | |
604 | if (!(parser->state.flags & ARGP_NO_ERRS) | |
605 | && parser->state.err_stream) | |
606 | fprintf (parser->state.err_stream, | |
607 | dgettext (parser->argp->argp_domain, | |
608 | "%s: Too many arguments\n"), | |
609 | parser->state.name); | |
610 | err = EBADKEY; | |
611 | } | |
612 | } | |
c84142e8 | 613 | |
d705269e UD |
614 | /* Okay, we're all done, with either an error or success; call the parsers |
615 | to indicate which one. */ | |
c84142e8 UD |
616 | |
617 | if (err) | |
618 | { | |
619 | /* Maybe print an error message. */ | |
620 | if (err == EBADKEY) | |
621 | /* An appropriate message describing what the error was should have | |
622 | been printed earlier. */ | |
623 | __argp_state_help (&parser->state, parser->state.err_stream, | |
624 | ARGP_HELP_STD_ERR); | |
625 | ||
626 | /* Since we didn't exit, give each parser an error indication. */ | |
627 | for (group = parser->groups; group < parser->egroup; group++) | |
628 | group_parse (group, &parser->state, ARGP_KEY_ERROR, 0); | |
629 | } | |
630 | else | |
d705269e | 631 | /* Notify parsers of success, and propagate back values from parsers. */ |
c84142e8 UD |
632 | { |
633 | /* We pass over the groups in reverse order so that child groups are | |
634 | given a chance to do there processing before passing back a value to | |
635 | the parent. */ | |
636 | for (group = parser->egroup - 1 | |
637 | ; group >= parser->groups && (!err || err == EBADKEY) | |
638 | ; group--) | |
639 | err = group_parse (group, &parser->state, ARGP_KEY_SUCCESS, 0); | |
640 | if (err == EBADKEY) | |
641 | err = 0; /* Some parser didn't understand. */ | |
642 | } | |
643 | ||
d705269e UD |
644 | /* Call parsers once more, to do any final cleanup. Errors are ignored. */ |
645 | for (group = parser->egroup - 1; group >= parser->groups; group--) | |
646 | group_parse (group, &parser->state, ARGP_KEY_FINI, 0); | |
647 | ||
c84142e8 UD |
648 | if (err == EBADKEY) |
649 | err = EINVAL; | |
650 | ||
651 | free (parser->storage); | |
652 | ||
653 | return err; | |
654 | } | |
655 | \f | |
656 | /* Call the user parsers to parse the non-option argument VAL, at the current | |
d705269e UD |
657 | position, returning any error. The state NEXT pointer is assumed to have |
658 | been adjusted (by getopt) to point after this argument; this function will | |
659 | adjust it correctly to reflect however many args actually end up being | |
660 | consumed. */ | |
c84142e8 UD |
661 | static error_t |
662 | parser_parse_arg (struct parser *parser, char *val) | |
663 | { | |
d705269e UD |
664 | /* Save the starting value of NEXT, first adjusting it so that the arg |
665 | we're parsing is again the front of the arg vector. */ | |
666 | int index = --parser->state.next; | |
c84142e8 UD |
667 | error_t err = EBADKEY; |
668 | struct group *group; | |
d705269e | 669 | int key = 0; /* Which of ARGP_KEY_ARG[S] we used. */ |
c84142e8 | 670 | |
d705269e | 671 | /* Try to parse the argument in each parser. */ |
c84142e8 UD |
672 | for (group = parser->groups |
673 | ; group < parser->egroup && err == EBADKEY | |
674 | ; group++) | |
d705269e UD |
675 | { |
676 | parser->state.next++; /* For ARGP_KEY_ARG, consume the arg. */ | |
677 | key = ARGP_KEY_ARG; | |
678 | err = group_parse (group, &parser->state, key, val); | |
679 | ||
680 | if (err == EBADKEY) | |
681 | /* This parser doesn't like ARGP_KEY_ARG; try ARGP_KEY_ARGS instead. */ | |
682 | { | |
683 | parser->state.next--; /* For ARGP_KEY_ARGS, put back the arg. */ | |
684 | key = ARGP_KEY_ARGS; | |
685 | err = group_parse (group, &parser->state, key, 0); | |
686 | } | |
687 | } | |
688 | ||
689 | if (! err) | |
690 | { | |
691 | if (key == ARGP_KEY_ARGS) | |
692 | /* The default for ARGP_KEY_ARGS is to assume that if NEXT isn't | |
693 | changed by the user, *all* arguments should be considered | |
694 | consumed. */ | |
695 | parser->state.next = parser->state.argc; | |
696 | ||
697 | if (parser->state.next > index) | |
698 | /* Remember that we successfully processed a non-option | |
699 | argument -- but only if the user hasn't gotten tricky and set | |
700 | the clock back. */ | |
701 | (--group)->args_processed += (parser->state.next - index); | |
702 | else | |
703 | /* The user wants to reparse some args, give getopt another try. */ | |
704 | parser->try_getopt = 1; | |
705 | } | |
c84142e8 UD |
706 | |
707 | return err; | |
708 | } | |
709 | \f | |
710 | /* Call the user parsers to parse the option OPT, with argument VAL, at the | |
711 | current position, returning any error. */ | |
712 | static error_t | |
713 | parser_parse_opt (struct parser *parser, int opt, char *val) | |
714 | { | |
715 | /* The group key encoded in the high bits; 0 for short opts or | |
716 | group_number + 1 for long opts. */ | |
717 | int group_key = opt >> USER_BITS; | |
d705269e | 718 | error_t err = EBADKEY; |
c84142e8 UD |
719 | |
720 | if (group_key == 0) | |
721 | /* A short option. By comparing OPT's position in SHORT_OPTS to the | |
722 | various starting positions in each group's SHORT_END field, we can | |
723 | determine which group OPT came from. */ | |
724 | { | |
725 | struct group *group; | |
726 | char *short_index = strchr (parser->short_opts, opt); | |
727 | ||
728 | if (short_index) | |
729 | for (group = parser->groups; group < parser->egroup; group++) | |
730 | if (group->short_end > short_index) | |
d705269e | 731 | { |
511676f7 UD |
732 | err = group_parse (group, &parser->state, opt, |
733 | parser->opt_data.optarg); | |
d705269e UD |
734 | break; |
735 | } | |
c84142e8 UD |
736 | } |
737 | else | |
738 | /* A long option. We use shifts instead of masking for extracting | |
739 | the user value in order to preserve the sign. */ | |
d705269e | 740 | err = |
c84142e8 | 741 | group_parse (&parser->groups[group_key - 1], &parser->state, |
511676f7 UD |
742 | (opt << GROUP_BITS) >> GROUP_BITS, |
743 | parser->opt_data.optarg); | |
d705269e UD |
744 | |
745 | if (err == EBADKEY) | |
746 | /* At least currently, an option not recognized is an error in the | |
747 | parser, because we pre-compute which parser is supposed to deal | |
748 | with each option. */ | |
749 | { | |
750 | static const char bad_key_err[] = | |
751 | N_("(PROGRAM ERROR) Option should have been recognized!?"); | |
752 | if (group_key == 0) | |
9184d3db UD |
753 | __argp_error (&parser->state, "-%c: %s", opt, |
754 | dgettext (parser->argp->argp_domain, bad_key_err)); | |
d705269e UD |
755 | else |
756 | { | |
757 | struct option *long_opt = parser->long_opts; | |
758 | while (long_opt->val != opt && long_opt->name) | |
759 | long_opt++; | |
760 | __argp_error (&parser->state, "--%s: %s", | |
761 | long_opt->name ? long_opt->name : "???", | |
9184d3db | 762 | dgettext (parser->argp->argp_domain, bad_key_err)); |
d705269e | 763 | } |
9184d3db | 764 | } |
d705269e UD |
765 | |
766 | return err; | |
c84142e8 UD |
767 | } |
768 | \f | |
769 | /* Parse the next argument in PARSER (as indicated by PARSER->state.next). | |
770 | Any error from the parsers is returned, and *ARGP_EBADKEY indicates | |
771 | whether a value of EBADKEY is due to an unrecognized argument (which is | |
772 | generally not fatal). */ | |
773 | static error_t | |
774 | parser_parse_next (struct parser *parser, int *arg_ebadkey) | |
775 | { | |
776 | int opt; | |
777 | error_t err = 0; | |
778 | ||
779 | if (parser->state.quoted && parser->state.next < parser->state.quoted) | |
780 | /* The next argument pointer has been moved to before the quoted | |
781 | region, so pretend we never saw the quoting `--', and give getopt | |
782 | another chance. If the user hasn't removed it, getopt will just | |
783 | process it again. */ | |
784 | parser->state.quoted = 0; | |
785 | ||
786 | if (parser->try_getopt && !parser->state.quoted) | |
787 | /* Give getopt a chance to parse this. */ | |
788 | { | |
511676f7 UD |
789 | /* Put it back in OPTIND for getopt. */ |
790 | parser->opt_data.optind = parser->state.next; | |
791 | /* Distinguish KEY_ERR from a real option. */ | |
792 | parser->opt_data.optopt = KEY_END; | |
c84142e8 | 793 | if (parser->state.flags & ARGP_LONG_ONLY) |
511676f7 UD |
794 | opt = _getopt_long_only_r (parser->state.argc, parser->state.argv, |
795 | parser->short_opts, parser->long_opts, 0, | |
796 | &parser->opt_data); | |
c84142e8 | 797 | else |
511676f7 UD |
798 | opt = _getopt_long_r (parser->state.argc, parser->state.argv, |
799 | parser->short_opts, parser->long_opts, 0, | |
800 | &parser->opt_data); | |
801 | /* And see what getopt did. */ | |
802 | parser->state.next = parser->opt_data.optind; | |
c84142e8 UD |
803 | |
804 | if (opt == KEY_END) | |
805 | /* Getopt says there are no more options, so stop using | |
806 | getopt; we'll continue if necessary on our own. */ | |
807 | { | |
808 | parser->try_getopt = 0; | |
809 | if (parser->state.next > 1 | |
810 | && strcmp (parser->state.argv[parser->state.next - 1], QUOTE) | |
8c6de69d | 811 | == 0) |
c84142e8 UD |
812 | /* Not only is this the end of the options, but it's a |
813 | `quoted' region, which may have args that *look* like | |
814 | options, so we definitely shouldn't try to use getopt past | |
815 | here, whatever happens. */ | |
816 | parser->state.quoted = parser->state.next; | |
817 | } | |
511676f7 | 818 | else if (opt == KEY_ERR && parser->opt_data.optopt != KEY_END) |
c84142e8 UD |
819 | /* KEY_ERR can have the same value as a valid user short |
820 | option, but in the case of a real error, getopt sets OPTOPT | |
821 | to the offending character, which can never be KEY_END. */ | |
822 | { | |
823 | *arg_ebadkey = 0; | |
824 | return EBADKEY; | |
825 | } | |
826 | } | |
827 | else | |
828 | opt = KEY_END; | |
829 | ||
830 | if (opt == KEY_END) | |
94b78bb2 UD |
831 | { |
832 | /* We're past what getopt considers the options. */ | |
833 | if (parser->state.next >= parser->state.argc | |
834 | || (parser->state.flags & ARGP_NO_ARGS)) | |
835 | /* Indicate that we're done. */ | |
836 | { | |
837 | *arg_ebadkey = 1; | |
838 | return EBADKEY; | |
839 | } | |
840 | else | |
841 | /* A non-option arg; simulate what getopt might have done. */ | |
842 | { | |
843 | opt = KEY_ARG; | |
511676f7 | 844 | parser->opt_data.optarg = parser->state.argv[parser->state.next++]; |
94b78bb2 UD |
845 | } |
846 | } | |
d705269e UD |
847 | |
848 | if (opt == KEY_ARG) | |
c84142e8 | 849 | /* A non-option argument; try each parser in turn. */ |
511676f7 | 850 | err = parser_parse_arg (parser, parser->opt_data.optarg); |
c84142e8 | 851 | else |
511676f7 | 852 | err = parser_parse_opt (parser, opt, parser->opt_data.optarg); |
9184d3db | 853 | |
c84142e8 | 854 | if (err == EBADKEY) |
d705269e | 855 | *arg_ebadkey = (opt == KEY_END || opt == KEY_ARG); |
c84142e8 UD |
856 | |
857 | return err; | |
858 | } | |
859 | \f | |
860 | /* Parse the options strings in ARGC & ARGV according to the argp in ARGP. | |
861 | FLAGS is one of the ARGP_ flags above. If END_INDEX is non-NULL, the | |
862 | index in ARGV of the first unparsed option is returned in it. If an | |
863 | unknown option is present, EINVAL is returned; if some parser routine | |
864 | returned a non-zero value, it is returned; otherwise 0 is returned. */ | |
865 | error_t | |
866 | __argp_parse (const struct argp *argp, int argc, char **argv, unsigned flags, | |
867 | int *end_index, void *input) | |
868 | { | |
869 | error_t err; | |
870 | struct parser parser; | |
871 | ||
46924663 JST |
872 | struct argp_child child[4]; |
873 | struct argp top_argp; | |
874 | ||
c84142e8 UD |
875 | /* If true, then err == EBADKEY is a result of a non-option argument failing |
876 | to be parsed (which in some cases isn't actually an error). */ | |
877 | int arg_ebadkey = 0; | |
878 | ||
879 | if (! (flags & ARGP_NO_HELP)) | |
880 | /* Add our own options. */ | |
881 | { | |
46924663 | 882 | int child_index = 0; |
c84142e8 UD |
883 | |
884 | /* TOP_ARGP has no options, it just serves to group the user & default | |
885 | argps. */ | |
46924663 JST |
886 | memset (&top_argp, 0, sizeof (struct argp)); |
887 | top_argp.children = child; | |
c84142e8 UD |
888 | |
889 | memset (child, 0, 4 * sizeof (struct argp_child)); | |
890 | ||
891 | if (argp) | |
46924663 JST |
892 | child[child_index++].argp = argp; |
893 | child[child_index++].argp = &argp_default_argp; | |
c84142e8 | 894 | if (argp_program_version || argp_program_version_hook) |
46924663 JST |
895 | child[child_index++].argp = &argp_version_argp; |
896 | child[child_index].argp = 0; | |
c84142e8 | 897 | |
46924663 | 898 | argp = &top_argp; |
c84142e8 UD |
899 | } |
900 | ||
901 | /* Construct a parser for these arguments. */ | |
902 | err = parser_init (&parser, argp, argc, argv, flags, input); | |
903 | ||
904 | if (! err) | |
905 | /* Parse! */ | |
906 | { | |
907 | while (! err) | |
908 | err = parser_parse_next (&parser, &arg_ebadkey); | |
909 | err = parser_finalize (&parser, err, arg_ebadkey, end_index); | |
910 | } | |
911 | ||
912 | return err; | |
913 | } | |
914 | #ifdef weak_alias | |
915 | weak_alias (__argp_parse, argp_parse) | |
916 | #endif | |
1fb05e3d UD |
917 | \f |
918 | /* Return the input field for ARGP in the parser corresponding to STATE; used | |
919 | by the help routines. */ | |
920 | void * | |
921 | __argp_input (const struct argp *argp, const struct argp_state *state) | |
922 | { | |
923 | if (state) | |
924 | { | |
925 | struct group *group; | |
926 | struct parser *parser = state->pstate; | |
927 | ||
928 | for (group = parser->groups; group < parser->egroup; group++) | |
929 | if (group->argp == argp) | |
930 | return group->input; | |
931 | } | |
932 | ||
933 | return 0; | |
934 | } | |
935 | #ifdef weak_alias | |
936 | weak_alias (__argp_input, _argp_input) | |
937 | #endif |