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