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