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