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