1 | /* Hierarchial argument parsing |
---|
2 | Copyright (C) 1995, 96, 97, 98, 99, 2000,2003 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 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 | #ifndef _GNU_SOURCE |
---|
22 | # define _GNU_SOURCE 1 |
---|
23 | #endif |
---|
24 | |
---|
25 | #ifdef HAVE_CONFIG_H |
---|
26 | #include <config.h> |
---|
27 | #endif |
---|
28 | |
---|
29 | /* AIX requires this to be the first thing in the file. */ |
---|
30 | #ifndef __GNUC__ |
---|
31 | # if HAVE_ALLOCA_H |
---|
32 | # include <alloca.h> |
---|
33 | # else |
---|
34 | # ifdef _AIX |
---|
35 | #pragma alloca |
---|
36 | # else |
---|
37 | # ifndef alloca /* predefined by HP cc +Olibcalls */ |
---|
38 | char *alloca (); |
---|
39 | # endif |
---|
40 | # endif |
---|
41 | # endif |
---|
42 | #endif |
---|
43 | |
---|
44 | #include <stdlib.h> |
---|
45 | #include <string.h> |
---|
46 | #include <unistd.h> |
---|
47 | #include <limits.h> |
---|
48 | #include <assert.h> |
---|
49 | |
---|
50 | #ifndef _ |
---|
51 | /* This is for other GNU distributions with internationalized messages. |
---|
52 | When compiling libc, the _ macro is predefined. */ |
---|
53 | # if defined HAVE_LIBINTL_H || defined _LIBC |
---|
54 | # include <libintl.h> |
---|
55 | # ifdef _LIBC |
---|
56 | # undef dgettext |
---|
57 | # define dgettext(domain, msgid) __dcgettext (domain, msgid, LC_MESSAGES) |
---|
58 | # endif |
---|
59 | # else |
---|
60 | # define dgettext(domain, msgid) (msgid) |
---|
61 | # define gettext(msgid) (msgid) |
---|
62 | # endif |
---|
63 | #endif |
---|
64 | #ifndef N_ |
---|
65 | # define N_(msgid) (msgid) |
---|
66 | #endif |
---|
67 | |
---|
68 | #if _LIBC - 0 |
---|
69 | #include <bits/libc-lock.h> |
---|
70 | #else |
---|
71 | #ifdef HAVE_CTHREADS_H |
---|
72 | #include <cthreads.h> |
---|
73 | #endif |
---|
74 | #endif /* _LIBC */ |
---|
75 | |
---|
76 | #include "src/lib/argp/argp.h" |
---|
77 | #include "src/lib/argp/argp-namefrob.h" |
---|
78 | |
---|
79 | |
---|
80 | /* The meta-argument used to prevent any further arguments being interpreted |
---|
81 | as options. */ |
---|
82 | #define QUOTE "--" |
---|
83 | |
---|
84 | /* EZ alias for ARGP_ERR_UNKNOWN. */ |
---|
85 | #define EBADKEY ARGP_ERR_UNKNOWN |
---|
86 | |
---|
87 | |
---|
88 | /* Default options. */ |
---|
89 | |
---|
90 | /* When argp is given the --HANG switch, _ARGP_HANG is set and argp will sleep |
---|
91 | for one second intervals, decrementing _ARGP_HANG until it's zero. Thus |
---|
92 | you can force the program to continue by attaching a debugger and setting |
---|
93 | it to 0 yourself. */ |
---|
94 | volatile int _argp_hang; |
---|
95 | |
---|
96 | #define OPT_PROGNAME -2 |
---|
97 | #define OPT_USAGE -3 |
---|
98 | #define OPT_HANG -4 |
---|
99 | |
---|
100 | static const struct argp_option argp_default_options[] = |
---|
101 | { |
---|
102 | {"help", '?', 0, 0, N_("Give this help list"), -1}, |
---|
103 | {"usage", OPT_USAGE, 0, 0, N_("Give a short usage message"), 0 }, |
---|
104 | {"program-name",OPT_PROGNAME,"NAME", OPTION_HIDDEN, |
---|
105 | N_("Set the program name"), 0}, |
---|
106 | {"HANG", OPT_HANG, "SECS", OPTION_ARG_OPTIONAL | OPTION_HIDDEN, |
---|
107 | N_("Hang for SECS seconds (default 3600)"), 0 }, |
---|
108 | {0, 0, 0, 0, 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 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 | |
---|
134 | state->name = __argp_basename(arg); |
---|
135 | |
---|
136 | #if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME |
---|
137 | program_invocation_short_name = state->name; |
---|
138 | #endif |
---|
139 | |
---|
140 | if ((state->flags & (ARGP_PARSE_ARGV0 | ARGP_NO_ERRS)) |
---|
141 | == ARGP_PARSE_ARGV0) |
---|
142 | /* Update what getopt uses too. */ |
---|
143 | state->argv[0] = arg; |
---|
144 | |
---|
145 | break; |
---|
146 | |
---|
147 | case OPT_HANG: |
---|
148 | _argp_hang = atoi (arg ? arg : "3600"); |
---|
149 | fprintf(state->err_stream, "%s: pid = %ld\n", |
---|
150 | state->name, (long) getpid()); |
---|
151 | while (_argp_hang-- > 0) |
---|
152 | __sleep (1); |
---|
153 | break; |
---|
154 | |
---|
155 | default: |
---|
156 | return EBADKEY; |
---|
157 | } |
---|
158 | return 0; |
---|
159 | } |
---|
160 | |
---|
161 | static const struct argp argp_default_argp = |
---|
162 | {argp_default_options, &argp_default_parser, NULL, NULL, NULL, NULL, "libc"}; |
---|
163 | |
---|
164 | |
---|
165 | static const struct argp_option argp_version_options[] = |
---|
166 | { |
---|
167 | {"version", 'V', 0, 0, N_("Print program version"), -1}, |
---|
168 | {0, 0, 0, 0, 0, 0 } |
---|
169 | }; |
---|
170 | |
---|
171 | static error_t |
---|
172 | argp_version_parser (int key, char *arg , struct argp_state *state) |
---|
173 | { |
---|
174 | switch (key) |
---|
175 | { |
---|
176 | case 'V': |
---|
177 | if (argp_program_version_hook) |
---|
178 | (*argp_program_version_hook) (state->out_stream, state); |
---|
179 | else if (argp_program_version) |
---|
180 | fprintf (state->out_stream, "%s\n", argp_program_version); |
---|
181 | else |
---|
182 | __argp_error (state, dgettext (state->root_argp->argp_domain, |
---|
183 | "(PROGRAM ERROR) No version known!?")); |
---|
184 | if (! (state->flags & ARGP_NO_EXIT)) |
---|
185 | exit (0); |
---|
186 | break; |
---|
187 | default: |
---|
188 | return EBADKEY; |
---|
189 | } |
---|
190 | return 0; |
---|
191 | } |
---|
192 | |
---|
193 | static const struct argp argp_version_argp = |
---|
194 | {argp_version_options, &argp_version_parser, NULL, NULL, NULL, NULL, "libc"}; |
---|
195 | |
---|
196 | |
---|
197 | |
---|
198 | /* The state of a `group' during parsing. Each group corresponds to a |
---|
199 | particular argp structure from the tree of such descending from the top |
---|
200 | level argp passed to argp_parse. */ |
---|
201 | struct group |
---|
202 | { |
---|
203 | /* This group's parsing function. */ |
---|
204 | argp_parser_t parser; |
---|
205 | |
---|
206 | /* Which argp this group is from. */ |
---|
207 | const struct argp *argp; |
---|
208 | |
---|
209 | /* The number of non-option args sucessfully handled by this parser. */ |
---|
210 | unsigned args_processed; |
---|
211 | |
---|
212 | /* This group's parser's parent's group. */ |
---|
213 | struct group *parent; |
---|
214 | unsigned parent_index; /* And the our position in the parent. */ |
---|
215 | |
---|
216 | /* These fields are swapped into and out of the state structure when |
---|
217 | calling this group's parser. */ |
---|
218 | void *input, **child_inputs; |
---|
219 | void *hook; |
---|
220 | }; |
---|
221 | |
---|
222 | /* Call GROUP's parser with KEY and ARG, swapping any group-specific info |
---|
223 | from STATE before calling, and back into state afterwards. If GROUP has |
---|
224 | no parser, EBADKEY is returned. */ |
---|
225 | static error_t |
---|
226 | group_parse (struct group *group, struct argp_state *state, int key, char *arg) |
---|
227 | { |
---|
228 | if (group->parser) |
---|
229 | { |
---|
230 | error_t err; |
---|
231 | state->hook = group->hook; |
---|
232 | state->input = group->input; |
---|
233 | state->child_inputs = group->child_inputs; |
---|
234 | state->arg_num = group->args_processed; |
---|
235 | err = (*group->parser)(key, arg, state); |
---|
236 | group->hook = state->hook; |
---|
237 | return err; |
---|
238 | } |
---|
239 | else |
---|
240 | return EBADKEY; |
---|
241 | } |
---|
242 | |
---|
243 | struct parser |
---|
244 | { |
---|
245 | const struct argp *argp; |
---|
246 | |
---|
247 | const char *posixly_correct; |
---|
248 | |
---|
249 | /* True if there are only no-option arguments left, which are just |
---|
250 | passed verbatim with ARGP_KEY_ARG. This is set if we encounter a |
---|
251 | quote, or the end of the proper options, but may be cleared again |
---|
252 | if the user moves the next argument pointer backwards. */ |
---|
253 | int args_only; |
---|
254 | |
---|
255 | /* Describe how to deal with options that follow non-option ARGV-elements. |
---|
256 | |
---|
257 | If the caller did not specify anything, the default is |
---|
258 | REQUIRE_ORDER if the environment variable POSIXLY_CORRECT is |
---|
259 | defined, PERMUTE otherwise. |
---|
260 | |
---|
261 | REQUIRE_ORDER means don't recognize them as options; stop option |
---|
262 | processing when the first non-option is seen. This is what Unix |
---|
263 | does. This mode of operation is selected by either setting the |
---|
264 | environment variable POSIXLY_CORRECT, or using `+' as the first |
---|
265 | character of the list of option characters. |
---|
266 | |
---|
267 | PERMUTE is the default. We permute the contents of ARGV as we |
---|
268 | scan, so that eventually all the non-options are at the end. This |
---|
269 | allows options to be given in any order, even with programs that |
---|
270 | were not written to expect this. |
---|
271 | |
---|
272 | RETURN_IN_ORDER is an option available to programs that were |
---|
273 | written to expect options and other ARGV-elements in any order |
---|
274 | and that care about the ordering of the two. We describe each |
---|
275 | non-option ARGV-element as if it were the argument of an option |
---|
276 | with character code 1. Using `-' as the first character of the |
---|
277 | list of option characters selects this mode of operation. |
---|
278 | |
---|
279 | */ |
---|
280 | enum { REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER } ordering; |
---|
281 | |
---|
282 | /* A segment of non-option arguments that have been skipped for |
---|
283 | later processing, after all options. `first_nonopt' is the index |
---|
284 | in ARGV of the first of them; `last_nonopt' is the index after |
---|
285 | the last of them. |
---|
286 | |
---|
287 | If quoted or args_only is non-zero, this segment should be empty. */ |
---|
288 | |
---|
289 | /* FIXME: I'd prefer to use unsigned, but it's more consistent to |
---|
290 | use the same type as for state.next. */ |
---|
291 | int first_nonopt; |
---|
292 | int last_nonopt; |
---|
293 | |
---|
294 | /* String of all recognized short options. Needed for ARGP_LONG_ONLY. */ |
---|
295 | /* FIXME: Perhaps change to a pointer to a suitable bitmap instead? */ |
---|
296 | char *short_opts; |
---|
297 | |
---|
298 | /* For parsing combined short options. */ |
---|
299 | char *nextchar; |
---|
300 | |
---|
301 | /* States of the various parsing groups. */ |
---|
302 | struct group *groups; |
---|
303 | /* The end of the GROUPS array. */ |
---|
304 | struct group *egroup; |
---|
305 | /* An vector containing storage for the CHILD_INPUTS field in all groups. */ |
---|
306 | void **child_inputs; |
---|
307 | |
---|
308 | /* State block supplied to parsing routines. */ |
---|
309 | struct argp_state state; |
---|
310 | |
---|
311 | /* Memory used by this parser. */ |
---|
312 | void *storage; |
---|
313 | }; |
---|
314 | |
---|
315 | /* Search for a group defining a short option. */ |
---|
316 | static const struct argp_option * |
---|
317 | find_short_option(struct parser *parser, int key, struct group **p) |
---|
318 | { |
---|
319 | struct group *group; |
---|
320 | |
---|
321 | assert(key >= 0); |
---|
322 | assert(isascii(key)); |
---|
323 | |
---|
324 | for (group = parser->groups; group < parser->egroup; group++) |
---|
325 | { |
---|
326 | const struct argp_option *opts; |
---|
327 | |
---|
328 | for (opts = group->argp->options; !__option_is_end(opts); opts++) |
---|
329 | if (opts->key == key) |
---|
330 | { |
---|
331 | *p = group; |
---|
332 | return opts; |
---|
333 | } |
---|
334 | } |
---|
335 | return NULL; |
---|
336 | } |
---|
337 | |
---|
338 | enum match_result { MATCH_EXACT, MATCH_PARTIAL, MATCH_NO }; |
---|
339 | |
---|
340 | /* If defined, allow complete.el-like abbreviations of long options. */ |
---|
341 | #ifndef ARGP_COMPLETE |
---|
342 | #define ARGP_COMPLETE 0 |
---|
343 | #endif |
---|
344 | |
---|
345 | /* Matches an encountern long-option argument ARG against an option NAME. |
---|
346 | * ARG is terminated by NUL or '='. */ |
---|
347 | static enum match_result |
---|
348 | match_option(const char *arg, const char *name) |
---|
349 | { |
---|
350 | unsigned i, j; |
---|
351 | for (i = j = 0;; i++, j++) |
---|
352 | { |
---|
353 | switch(arg[i]) |
---|
354 | { |
---|
355 | case '\0': |
---|
356 | case '=': |
---|
357 | return name[j] ? MATCH_PARTIAL : MATCH_EXACT; |
---|
358 | #if ARGP_COMPLETE |
---|
359 | case '-': |
---|
360 | while (name[j] != '-') |
---|
361 | if (!name[j++]) |
---|
362 | return MATCH_NO; |
---|
363 | break; |
---|
364 | #endif |
---|
365 | default: |
---|
366 | if (arg[i] != name[j]) |
---|
367 | return MATCH_NO; |
---|
368 | } |
---|
369 | } |
---|
370 | } |
---|
371 | |
---|
372 | static const struct argp_option * |
---|
373 | find_long_option(struct parser *parser, |
---|
374 | const char *arg, |
---|
375 | struct group **p) |
---|
376 | { |
---|
377 | struct group *group; |
---|
378 | |
---|
379 | /* Partial match found so far. */ |
---|
380 | struct group *matched_group = NULL; |
---|
381 | const struct argp_option *matched_option = NULL; |
---|
382 | |
---|
383 | /* Number of partial matches. */ |
---|
384 | int num_partial = 0; |
---|
385 | |
---|
386 | for (group = parser->groups; group < parser->egroup; group++) |
---|
387 | { |
---|
388 | const struct argp_option *opts; |
---|
389 | |
---|
390 | for (opts = group->argp->options; !__option_is_end(opts); opts++) |
---|
391 | { |
---|
392 | if (!opts->name) |
---|
393 | continue; |
---|
394 | switch (match_option(arg, opts->name)) |
---|
395 | { |
---|
396 | case MATCH_NO: |
---|
397 | break; |
---|
398 | case MATCH_PARTIAL: |
---|
399 | num_partial++; |
---|
400 | |
---|
401 | matched_group = group; |
---|
402 | matched_option = opts; |
---|
403 | |
---|
404 | break; |
---|
405 | case MATCH_EXACT: |
---|
406 | /* Exact match. */ |
---|
407 | *p = group; |
---|
408 | return opts; |
---|
409 | } |
---|
410 | } |
---|
411 | } |
---|
412 | if (num_partial == 1) |
---|
413 | { |
---|
414 | *p = matched_group; |
---|
415 | return matched_option; |
---|
416 | } |
---|
417 | |
---|
418 | return NULL; |
---|
419 | } |
---|
420 | |
---|
421 | |
---|
422 | /* The next usable entries in the various parser tables being filled in by |
---|
423 | convert_options. */ |
---|
424 | struct parser_convert_state |
---|
425 | { |
---|
426 | struct parser *parser; |
---|
427 | char *short_end; |
---|
428 | void **child_inputs_end; |
---|
429 | }; |
---|
430 | |
---|
431 | /* Initialize GROUP from ARGP. If CVT->SHORT_END is non-NULL, short |
---|
432 | options are recorded in the short options string. Returns the next |
---|
433 | unused group entry. CVT holds state used during the conversion. */ |
---|
434 | static struct group * |
---|
435 | convert_options (const struct argp *argp, |
---|
436 | struct group *parent, unsigned parent_index, |
---|
437 | struct group *group, struct parser_convert_state *cvt) |
---|
438 | { |
---|
439 | const struct argp_option *opt = argp->options; |
---|
440 | const struct argp_child *children = argp->children; |
---|
441 | |
---|
442 | if (opt || argp->parser) |
---|
443 | { |
---|
444 | /* This parser needs a group. */ |
---|
445 | if (cvt->short_end) |
---|
446 | { |
---|
447 | /* Record any short options. */ |
---|
448 | for ( ; !__option_is_end (opt); opt++) |
---|
449 | if (__option_is_short(opt)) |
---|
450 | *cvt->short_end++ = opt->key; |
---|
451 | } |
---|
452 | |
---|
453 | group->parser = argp->parser; |
---|
454 | group->argp = argp; |
---|
455 | group->args_processed = 0; |
---|
456 | group->parent = parent; |
---|
457 | group->parent_index = parent_index; |
---|
458 | group->input = 0; |
---|
459 | group->hook = 0; |
---|
460 | group->child_inputs = 0; |
---|
461 | |
---|
462 | if (children) |
---|
463 | /* Assign GROUP's CHILD_INPUTS field some space from |
---|
464 | CVT->child_inputs_end.*/ |
---|
465 | { |
---|
466 | unsigned num_children = 0; |
---|
467 | while (children[num_children].argp) |
---|
468 | num_children++; |
---|
469 | group->child_inputs = cvt->child_inputs_end; |
---|
470 | cvt->child_inputs_end += num_children; |
---|
471 | } |
---|
472 | parent = group++; |
---|
473 | } |
---|
474 | else |
---|
475 | parent = 0; |
---|
476 | |
---|
477 | if (children) |
---|
478 | { |
---|
479 | unsigned index = 0; |
---|
480 | while (children->argp) |
---|
481 | group = |
---|
482 | convert_options (children++->argp, parent, index++, group, cvt); |
---|
483 | } |
---|
484 | |
---|
485 | return group; |
---|
486 | } |
---|
487 | /* Allocate and initialize the group structures, so that they are |
---|
488 | ordered as if by traversing the corresponding argp parser tree in |
---|
489 | pre-order. Also build the list of short options, if that is needed. */ |
---|
490 | static void |
---|
491 | parser_convert (struct parser *parser, const struct argp *argp) |
---|
492 | { |
---|
493 | struct parser_convert_state cvt; |
---|
494 | |
---|
495 | cvt.parser = parser; |
---|
496 | cvt.short_end = parser->short_opts; |
---|
497 | cvt.child_inputs_end = parser->child_inputs; |
---|
498 | |
---|
499 | parser->argp = argp; |
---|
500 | |
---|
501 | if (argp) |
---|
502 | parser->egroup = convert_options (argp, 0, 0, parser->groups, &cvt); |
---|
503 | else |
---|
504 | parser->egroup = parser->groups; /* No parsers at all! */ |
---|
505 | |
---|
506 | if (parser->short_opts) |
---|
507 | *cvt.short_end ='\0'; |
---|
508 | } |
---|
509 | |
---|
510 | /* Lengths of various parser fields which we will allocated. */ |
---|
511 | struct parser_sizes |
---|
512 | { |
---|
513 | /* Needed only ARGP_LONG_ONLY */ |
---|
514 | size_t short_len; /* Number of short options. */ |
---|
515 | |
---|
516 | size_t num_groups; /* Group structures we allocate. */ |
---|
517 | size_t num_child_inputs; /* Child input slots. */ |
---|
518 | }; |
---|
519 | |
---|
520 | /* For ARGP, increments the NUM_GROUPS field in SZS by the total |
---|
521 | number of argp structures descended from it, and the SHORT_LEN by |
---|
522 | the total number of short options. */ |
---|
523 | static void |
---|
524 | calc_sizes (const struct argp *argp, struct parser_sizes *szs) |
---|
525 | { |
---|
526 | const struct argp_child *child = argp->children; |
---|
527 | const struct argp_option *opt = argp->options; |
---|
528 | |
---|
529 | if (opt || argp->parser) |
---|
530 | { |
---|
531 | /* This parser needs a group. */ |
---|
532 | szs->num_groups++; |
---|
533 | if (opt) |
---|
534 | { |
---|
535 | while (__option_is_short (opt++)) |
---|
536 | szs->short_len++; |
---|
537 | } |
---|
538 | } |
---|
539 | |
---|
540 | if (child) |
---|
541 | while (child->argp) |
---|
542 | { |
---|
543 | calc_sizes ((child++)->argp, szs); |
---|
544 | szs->num_child_inputs++; |
---|
545 | } |
---|
546 | } |
---|
547 | |
---|
548 | /* Initializes PARSER to parse ARGP in a manner described by FLAGS. */ |
---|
549 | static error_t |
---|
550 | parser_init (struct parser *parser, const struct argp *argp, |
---|
551 | int argc, char **argv, int flags, void *input) |
---|
552 | { |
---|
553 | error_t err = 0; |
---|
554 | struct group *group; |
---|
555 | struct parser_sizes szs; |
---|
556 | |
---|
557 | parser->posixly_correct = getenv ("POSIXLY_CORRECT"); |
---|
558 | |
---|
559 | if (flags & ARGP_IN_ORDER) |
---|
560 | parser->ordering = RETURN_IN_ORDER; |
---|
561 | else if (flags & ARGP_NO_ARGS) |
---|
562 | parser->ordering = REQUIRE_ORDER; |
---|
563 | else if (parser->posixly_correct) |
---|
564 | parser->ordering = REQUIRE_ORDER; |
---|
565 | else |
---|
566 | parser->ordering = PERMUTE; |
---|
567 | |
---|
568 | szs.short_len = 0; |
---|
569 | szs.num_groups = 0; |
---|
570 | szs.num_child_inputs = 0; |
---|
571 | |
---|
572 | if (argp) |
---|
573 | calc_sizes (argp, &szs); |
---|
574 | |
---|
575 | if (!(flags & ARGP_LONG_ONLY)) |
---|
576 | /* We have no use for the short option array. */ |
---|
577 | szs.short_len = 0; |
---|
578 | |
---|
579 | /* Lengths of the various bits of storage used by PARSER. */ |
---|
580 | #define GLEN (szs.num_groups + 1) * sizeof (struct group) |
---|
581 | #define CLEN (szs.num_child_inputs * sizeof (void *)) |
---|
582 | #define SLEN (szs.short_len + 1) |
---|
583 | #define STORAGE(offset) ((void *) (((char *) parser->storage) + (offset))) |
---|
584 | |
---|
585 | parser->storage = malloc (GLEN + CLEN + SLEN); |
---|
586 | if (! parser->storage) |
---|
587 | return ENOMEM; |
---|
588 | |
---|
589 | parser->groups = parser->storage; |
---|
590 | |
---|
591 | parser->child_inputs = STORAGE(GLEN); |
---|
592 | memset (parser->child_inputs, 0, szs.num_child_inputs * sizeof (void *)); |
---|
593 | |
---|
594 | if (flags & ARGP_LONG_ONLY) |
---|
595 | parser->short_opts = STORAGE(GLEN + CLEN); |
---|
596 | else |
---|
597 | parser->short_opts = NULL; |
---|
598 | |
---|
599 | parser_convert (parser, argp); |
---|
600 | |
---|
601 | memset (&parser->state, 0, sizeof (struct argp_state)); |
---|
602 | |
---|
603 | parser->state.root_argp = parser->argp; |
---|
604 | parser->state.argc = argc; |
---|
605 | parser->state.argv = argv; |
---|
606 | parser->state.flags = flags; |
---|
607 | parser->state.err_stream = stderr; |
---|
608 | parser->state.out_stream = stdout; |
---|
609 | parser->state.pstate = parser; |
---|
610 | |
---|
611 | parser->args_only = 0; |
---|
612 | parser->nextchar = NULL; |
---|
613 | parser->first_nonopt = parser->last_nonopt = 0; |
---|
614 | |
---|
615 | /* Call each parser for the first time, giving it a chance to propagate |
---|
616 | values to child parsers. */ |
---|
617 | if (parser->groups < parser->egroup) |
---|
618 | parser->groups->input = input; |
---|
619 | for (group = parser->groups; |
---|
620 | group < parser->egroup && (!err || err == EBADKEY); |
---|
621 | group++) |
---|
622 | { |
---|
623 | if (group->parent) |
---|
624 | /* If a child parser, get the initial input value from the parent. */ |
---|
625 | group->input = group->parent->child_inputs[group->parent_index]; |
---|
626 | |
---|
627 | if (!group->parser |
---|
628 | && group->argp->children && group->argp->children->argp) |
---|
629 | /* For the special case where no parsing function is supplied for an |
---|
630 | argp, propagate its input to its first child, if any (this just |
---|
631 | makes very simple wrapper argps more convenient). */ |
---|
632 | group->child_inputs[0] = group->input; |
---|
633 | |
---|
634 | err = group_parse (group, &parser->state, ARGP_KEY_INIT, 0); |
---|
635 | } |
---|
636 | if (err == EBADKEY) |
---|
637 | err = 0; /* Some parser didn't understand. */ |
---|
638 | |
---|
639 | if (err) |
---|
640 | return err; |
---|
641 | |
---|
642 | if (argv[0] && !(parser->state.flags & ARGP_PARSE_ARGV0)) |
---|
643 | /* There's an argv[0]; use it for messages. */ |
---|
644 | { |
---|
645 | parser->state.name = __argp_basename(argv[0]); |
---|
646 | |
---|
647 | /* Don't parse it as an argument. */ |
---|
648 | parser->state.next = 1; |
---|
649 | } |
---|
650 | else |
---|
651 | parser->state.name = __argp_short_program_name(NULL); |
---|
652 | |
---|
653 | return 0; |
---|
654 | } |
---|
655 | |
---|
656 | /* Free any storage consumed by PARSER (but not PARSER itself). */ |
---|
657 | static error_t |
---|
658 | parser_finalize (struct parser *parser, |
---|
659 | error_t err, int arg_ebadkey, int *end_index) |
---|
660 | { |
---|
661 | struct group *group; |
---|
662 | |
---|
663 | if (err == EBADKEY && arg_ebadkey) |
---|
664 | /* Suppress errors generated by unparsed arguments. */ |
---|
665 | err = 0; |
---|
666 | |
---|
667 | if (! err) |
---|
668 | { |
---|
669 | if (parser->state.next == parser->state.argc) |
---|
670 | /* We successfully parsed all arguments! Call all the parsers again, |
---|
671 | just a few more times... */ |
---|
672 | { |
---|
673 | for (group = parser->groups; |
---|
674 | group < parser->egroup && (!err || err==EBADKEY); |
---|
675 | group++) |
---|
676 | if (group->args_processed == 0) |
---|
677 | err = group_parse (group, &parser->state, ARGP_KEY_NO_ARGS, 0); |
---|
678 | for (group = parser->egroup - 1; |
---|
679 | group >= parser->groups && (!err || err==EBADKEY); |
---|
680 | group--) |
---|
681 | err = group_parse (group, &parser->state, ARGP_KEY_END, 0); |
---|
682 | |
---|
683 | if (err == EBADKEY) |
---|
684 | err = 0; /* Some parser didn't understand. */ |
---|
685 | |
---|
686 | /* Tell the user that all arguments are parsed. */ |
---|
687 | if (end_index) |
---|
688 | *end_index = parser->state.next; |
---|
689 | } |
---|
690 | else if (end_index) |
---|
691 | /* Return any remaining arguments to the user. */ |
---|
692 | *end_index = parser->state.next; |
---|
693 | else |
---|
694 | /* No way to return the remaining arguments, they must be bogus. */ |
---|
695 | { |
---|
696 | if (!(parser->state.flags & ARGP_NO_ERRS) |
---|
697 | && parser->state.err_stream) |
---|
698 | fprintf (parser->state.err_stream, |
---|
699 | dgettext (parser->argp->argp_domain, |
---|
700 | "%s: Too many arguments\n"), |
---|
701 | parser->state.name); |
---|
702 | err = EBADKEY; |
---|
703 | } |
---|
704 | } |
---|
705 | |
---|
706 | /* Okay, we're all done, with either an error or success; call the parsers |
---|
707 | to indicate which one. */ |
---|
708 | |
---|
709 | if (err) |
---|
710 | { |
---|
711 | /* Maybe print an error message. */ |
---|
712 | if (err == EBADKEY) |
---|
713 | /* An appropriate message describing what the error was should have |
---|
714 | been printed earlier. */ |
---|
715 | __argp_state_help (&parser->state, parser->state.err_stream, |
---|
716 | ARGP_HELP_STD_ERR); |
---|
717 | |
---|
718 | /* Since we didn't exit, give each parser an error indication. */ |
---|
719 | for (group = parser->groups; group < parser->egroup; group++) |
---|
720 | group_parse (group, &parser->state, ARGP_KEY_ERROR, 0); |
---|
721 | } |
---|
722 | else |
---|
723 | /* Notify parsers of success, and propagate back values from parsers. */ |
---|
724 | { |
---|
725 | /* We pass over the groups in reverse order so that child groups are |
---|
726 | given a chance to do there processing before passing back a value to |
---|
727 | the parent. */ |
---|
728 | for (group = parser->egroup - 1 |
---|
729 | ; group >= parser->groups && (!err || err == EBADKEY) |
---|
730 | ; group--) |
---|
731 | err = group_parse (group, &parser->state, ARGP_KEY_SUCCESS, 0); |
---|
732 | if (err == EBADKEY) |
---|
733 | err = 0; /* Some parser didn't understand. */ |
---|
734 | } |
---|
735 | |
---|
736 | /* Call parsers once more, to do any final cleanup. Errors are ignored. */ |
---|
737 | for (group = parser->egroup - 1; group >= parser->groups; group--) |
---|
738 | group_parse (group, &parser->state, ARGP_KEY_FINI, 0); |
---|
739 | |
---|
740 | if (err == EBADKEY) |
---|
741 | err = EINVAL; |
---|
742 | |
---|
743 | free (parser->storage); |
---|
744 | |
---|
745 | return err; |
---|
746 | } |
---|
747 | |
---|
748 | /* Call the user parsers to parse the non-option argument VAL, at the |
---|
749 | current position, returning any error. The state NEXT pointer |
---|
750 | should point to the argument; this function will adjust it |
---|
751 | correctly to reflect however many args actually end up being |
---|
752 | consumed. */ |
---|
753 | static error_t |
---|
754 | parser_parse_arg (struct parser *parser, char *val) |
---|
755 | { |
---|
756 | /* Save the starting value of NEXT */ |
---|
757 | int index = parser->state.next; |
---|
758 | error_t err = EBADKEY; |
---|
759 | struct group *group; |
---|
760 | int key = 0; /* Which of ARGP_KEY_ARG[S] we used. */ |
---|
761 | |
---|
762 | /* Try to parse the argument in each parser. */ |
---|
763 | for (group = parser->groups |
---|
764 | ; group < parser->egroup && err == EBADKEY |
---|
765 | ; group++) |
---|
766 | { |
---|
767 | parser->state.next++; /* For ARGP_KEY_ARG, consume the arg. */ |
---|
768 | key = ARGP_KEY_ARG; |
---|
769 | err = group_parse (group, &parser->state, key, val); |
---|
770 | |
---|
771 | if (err == EBADKEY) |
---|
772 | /* This parser doesn't like ARGP_KEY_ARG; try ARGP_KEY_ARGS instead. */ |
---|
773 | { |
---|
774 | parser->state.next--; /* For ARGP_KEY_ARGS, put back the arg. */ |
---|
775 | key = ARGP_KEY_ARGS; |
---|
776 | err = group_parse (group, &parser->state, key, 0); |
---|
777 | } |
---|
778 | } |
---|
779 | |
---|
780 | if (! err) |
---|
781 | { |
---|
782 | if (key == ARGP_KEY_ARGS) |
---|
783 | /* The default for ARGP_KEY_ARGS is to assume that if NEXT isn't |
---|
784 | changed by the user, *all* arguments should be considered |
---|
785 | consumed. */ |
---|
786 | parser->state.next = parser->state.argc; |
---|
787 | |
---|
788 | if (parser->state.next > index) |
---|
789 | /* Remember that we successfully processed a non-option |
---|
790 | argument -- but only if the user hasn't gotten tricky and set |
---|
791 | the clock back. */ |
---|
792 | (--group)->args_processed += (parser->state.next - index); |
---|
793 | else |
---|
794 | /* The user wants to reparse some args, so try looking for options again. */ |
---|
795 | parser->args_only = 0; |
---|
796 | } |
---|
797 | |
---|
798 | return err; |
---|
799 | } |
---|
800 | |
---|
801 | /* Exchange two adjacent subsequences of ARGV. |
---|
802 | One subsequence is elements [first_nonopt,last_nonopt) |
---|
803 | which contains all the non-options that have been skipped so far. |
---|
804 | The other is elements [last_nonopt,next), which contains all |
---|
805 | the options processed since those non-options were skipped. |
---|
806 | |
---|
807 | `first_nonopt' and `last_nonopt' are relocated so that they describe |
---|
808 | the new indices of the non-options in ARGV after they are moved. */ |
---|
809 | |
---|
810 | static void |
---|
811 | exchange (struct parser *parser) |
---|
812 | { |
---|
813 | int bottom = parser->first_nonopt; |
---|
814 | int middle = parser->last_nonopt; |
---|
815 | int top = parser->state.next; |
---|
816 | char **argv = parser->state.argv; |
---|
817 | |
---|
818 | char *tem; |
---|
819 | |
---|
820 | /* Exchange the shorter segment with the far end of the longer segment. |
---|
821 | That puts the shorter segment into the right place. |
---|
822 | It leaves the longer segment in the right place overall, |
---|
823 | but it consists of two parts that need to be swapped next. */ |
---|
824 | |
---|
825 | while (top > middle && middle > bottom) |
---|
826 | { |
---|
827 | if (top - middle > middle - bottom) |
---|
828 | { |
---|
829 | /* Bottom segment is the short one. */ |
---|
830 | int len = middle - bottom; |
---|
831 | register int i; |
---|
832 | |
---|
833 | /* Swap it with the top part of the top segment. */ |
---|
834 | for (i = 0; i < len; i++) |
---|
835 | { |
---|
836 | tem = argv[bottom + i]; |
---|
837 | argv[bottom + i] = argv[top - (middle - bottom) + i]; |
---|
838 | argv[top - (middle - bottom) + i] = tem; |
---|
839 | } |
---|
840 | /* Exclude the moved bottom segment from further swapping. */ |
---|
841 | top -= len; |
---|
842 | } |
---|
843 | else |
---|
844 | { |
---|
845 | /* Top segment is the short one. */ |
---|
846 | int len = top - middle; |
---|
847 | register int i; |
---|
848 | |
---|
849 | /* Swap it with the bottom part of the bottom segment. */ |
---|
850 | for (i = 0; i < len; i++) |
---|
851 | { |
---|
852 | tem = argv[bottom + i]; |
---|
853 | argv[bottom + i] = argv[middle + i]; |
---|
854 | argv[middle + i] = tem; |
---|
855 | } |
---|
856 | /* Exclude the moved top segment from further swapping. */ |
---|
857 | bottom += len; |
---|
858 | } |
---|
859 | } |
---|
860 | |
---|
861 | /* Update records for the slots the non-options now occupy. */ |
---|
862 | |
---|
863 | parser->first_nonopt += (parser->state.next - parser->last_nonopt); |
---|
864 | parser->last_nonopt = parser->state.next; |
---|
865 | } |
---|
866 | |
---|
867 | |
---|
868 | |
---|
869 | enum arg_type { ARG_ARG, ARG_SHORT_OPTION, |
---|
870 | ARG_LONG_OPTION, ARG_LONG_ONLY_OPTION, |
---|
871 | ARG_QUOTE }; |
---|
872 | |
---|
873 | static enum arg_type |
---|
874 | classify_arg(struct parser *parser, char *arg, char **opt) |
---|
875 | { |
---|
876 | if (arg[0] == '-') |
---|
877 | /* Looks like an option... */ |
---|
878 | switch (arg[1]) |
---|
879 | { |
---|
880 | case '\0': |
---|
881 | /* "-" is not an option. */ |
---|
882 | return ARG_ARG; |
---|
883 | case '-': |
---|
884 | /* Long option, or quote. */ |
---|
885 | if (!arg[2]) |
---|
886 | return ARG_QUOTE; |
---|
887 | |
---|
888 | /* A long option. */ |
---|
889 | if (opt) |
---|
890 | *opt = arg + 2; |
---|
891 | return ARG_LONG_OPTION; |
---|
892 | |
---|
893 | default: |
---|
894 | /* Short option. But if ARGP_LONG_ONLY, it can also be a long option. */ |
---|
895 | |
---|
896 | if (opt) |
---|
897 | *opt = arg + 1; |
---|
898 | |
---|
899 | if (parser->state.flags & ARGP_LONG_ONLY) |
---|
900 | { |
---|
901 | /* Rules from getopt.c: |
---|
902 | |
---|
903 | If long_only and the ARGV-element has the form "-f", |
---|
904 | where f is a valid short option, don't consider it an |
---|
905 | abbreviated form of a long option that starts with f. |
---|
906 | Otherwise there would be no way to give the -f short |
---|
907 | option. |
---|
908 | |
---|
909 | On the other hand, if there's a long option "fubar" and |
---|
910 | the ARGV-element is "-fu", do consider that an |
---|
911 | abbreviation of the long option, just like "--fu", and |
---|
912 | not "-f" with arg "u". |
---|
913 | |
---|
914 | This distinction seems to be the most useful approach. */ |
---|
915 | |
---|
916 | assert(parser->short_opts); |
---|
917 | |
---|
918 | if (arg[2] || !strchr(parser->short_opts, arg[1])) |
---|
919 | return ARG_LONG_ONLY_OPTION; |
---|
920 | } |
---|
921 | |
---|
922 | return ARG_SHORT_OPTION; |
---|
923 | } |
---|
924 | |
---|
925 | else |
---|
926 | return ARG_ARG; |
---|
927 | } |
---|
928 | |
---|
929 | /* Parse the next argument in PARSER (as indicated by PARSER->state.next). |
---|
930 | Any error from the parsers is returned, and *ARGP_EBADKEY indicates |
---|
931 | whether a value of EBADKEY is due to an unrecognized argument (which is |
---|
932 | generally not fatal). */ |
---|
933 | static error_t |
---|
934 | parser_parse_next (struct parser *parser, int *arg_ebadkey) |
---|
935 | { |
---|
936 | if (parser->state.quoted && parser->state.next < parser->state.quoted) |
---|
937 | /* The next argument pointer has been moved to before the quoted |
---|
938 | region, so pretend we never saw the quoting `--', and start |
---|
939 | looking for options again. If the `--' is still there we'll just |
---|
940 | process it one more time. */ |
---|
941 | parser->state.quoted = parser->args_only = 0; |
---|
942 | |
---|
943 | /* Give FIRST_NONOPT & LAST_NONOPT rational values if NEXT has been |
---|
944 | moved back by the user (who may also have changed the arguments). */ |
---|
945 | if (parser->last_nonopt > parser->state.next) |
---|
946 | parser->last_nonopt = parser->state.next; |
---|
947 | if (parser->first_nonopt > parser->state.next) |
---|
948 | parser->first_nonopt = parser->state.next; |
---|
949 | |
---|
950 | if (parser->nextchar) |
---|
951 | /* Deal with short options. */ |
---|
952 | { |
---|
953 | struct group *group; |
---|
954 | char c; |
---|
955 | const struct argp_option *option; |
---|
956 | char *value = NULL;; |
---|
957 | |
---|
958 | assert(!parser->args_only); |
---|
959 | |
---|
960 | c = *parser->nextchar++; |
---|
961 | |
---|
962 | option = find_short_option(parser, c, &group); |
---|
963 | if (!option) |
---|
964 | { |
---|
965 | if (parser->posixly_correct) |
---|
966 | /* 1003.2 specifies the format of this message. */ |
---|
967 | fprintf (parser->state.err_stream, |
---|
968 | dgettext(parser->state.root_argp->argp_domain, |
---|
969 | "%s: illegal option -- %c\n"), |
---|
970 | parser->state.name, c); |
---|
971 | else |
---|
972 | fprintf (parser->state.err_stream, |
---|
973 | dgettext(parser->state.root_argp->argp_domain, |
---|
974 | "%s: invalid option -- %c\n"), |
---|
975 | parser->state.name, c); |
---|
976 | |
---|
977 | *arg_ebadkey = 0; |
---|
978 | return EBADKEY; |
---|
979 | } |
---|
980 | |
---|
981 | if (!*parser->nextchar) |
---|
982 | parser->nextchar = NULL; |
---|
983 | |
---|
984 | if (option->arg) |
---|
985 | { |
---|
986 | value = parser->nextchar; |
---|
987 | parser->nextchar = NULL; |
---|
988 | |
---|
989 | if (!value |
---|
990 | && !(option->flags & OPTION_ARG_OPTIONAL)) |
---|
991 | /* We need an mandatory argument. */ |
---|
992 | { |
---|
993 | if (parser->state.next == parser->state.argc) |
---|
994 | /* Missing argument */ |
---|
995 | { |
---|
996 | /* 1003.2 specifies the format of this message. */ |
---|
997 | fprintf (parser->state.err_stream, |
---|
998 | dgettext(parser->state.root_argp->argp_domain, |
---|
999 | "%s: option requires an argument -- %c\n"), |
---|
1000 | parser->state.name, c); |
---|
1001 | |
---|
1002 | *arg_ebadkey = 0; |
---|
1003 | return EBADKEY; |
---|
1004 | } |
---|
1005 | value = parser->state.argv[parser->state.next++]; |
---|
1006 | } |
---|
1007 | } |
---|
1008 | return group_parse(group, &parser->state, |
---|
1009 | option->key, value); |
---|
1010 | } |
---|
1011 | else |
---|
1012 | /* Advance to the next ARGV-element. */ |
---|
1013 | { |
---|
1014 | if (parser->args_only) |
---|
1015 | { |
---|
1016 | *arg_ebadkey = 1; |
---|
1017 | if (parser->state.next >= parser->state.argc) |
---|
1018 | /* We're done. */ |
---|
1019 | return EBADKEY; |
---|
1020 | else |
---|
1021 | return parser_parse_arg(parser, |
---|
1022 | parser->state.argv[parser->state.next]); |
---|
1023 | } |
---|
1024 | |
---|
1025 | if (parser->state.next >= parser->state.argc) |
---|
1026 | /* Almost done. If there are non-options that we skipped |
---|
1027 | previously, we should process them now. */ |
---|
1028 | { |
---|
1029 | *arg_ebadkey = 1; |
---|
1030 | if (parser->first_nonopt != parser->last_nonopt) |
---|
1031 | { |
---|
1032 | exchange(parser); |
---|
1033 | |
---|
1034 | /* Start processing the arguments we skipped previously. */ |
---|
1035 | parser->state.next = parser->first_nonopt; |
---|
1036 | |
---|
1037 | parser->first_nonopt = parser->last_nonopt = 0; |
---|
1038 | |
---|
1039 | parser->args_only = 1; |
---|
1040 | return 0; |
---|
1041 | } |
---|
1042 | else |
---|
1043 | /* Indicate that we're really done. */ |
---|
1044 | return EBADKEY; |
---|
1045 | } |
---|
1046 | else |
---|
1047 | /* Look for options. */ |
---|
1048 | { |
---|
1049 | char *arg = parser->state.argv[parser->state.next]; |
---|
1050 | |
---|
1051 | char *optstart; |
---|
1052 | enum arg_type token = classify_arg(parser, arg, &optstart); |
---|
1053 | |
---|
1054 | switch (token) |
---|
1055 | { |
---|
1056 | case ARG_ARG: |
---|
1057 | switch (parser->ordering) |
---|
1058 | { |
---|
1059 | case PERMUTE: |
---|
1060 | if (parser->first_nonopt == parser->last_nonopt) |
---|
1061 | /* Skipped sequence is empty; start a new one. */ |
---|
1062 | parser->first_nonopt = parser->last_nonopt = parser->state.next; |
---|
1063 | |
---|
1064 | else if (parser->last_nonopt != parser->state.next) |
---|
1065 | /* We have a non-empty skipped sequence, and |
---|
1066 | we're not at the end-point, so move it. */ |
---|
1067 | exchange(parser); |
---|
1068 | |
---|
1069 | assert(parser->last_nonopt == parser->state.next); |
---|
1070 | |
---|
1071 | /* Skip this argument for now. */ |
---|
1072 | parser->state.next++; |
---|
1073 | parser->last_nonopt = parser->state.next; |
---|
1074 | |
---|
1075 | return 0; |
---|
1076 | |
---|
1077 | case REQUIRE_ORDER: |
---|
1078 | /* Implicit quote before the first argument. */ |
---|
1079 | parser->args_only = 1; |
---|
1080 | return 0; |
---|
1081 | |
---|
1082 | case RETURN_IN_ORDER: |
---|
1083 | *arg_ebadkey = 1; |
---|
1084 | return parser_parse_arg(parser, arg); |
---|
1085 | |
---|
1086 | default: |
---|
1087 | abort(); |
---|
1088 | } |
---|
1089 | case ARG_QUOTE: |
---|
1090 | /* Skip it, then exchange with any previous non-options. */ |
---|
1091 | parser->state.next++; |
---|
1092 | assert (parser->last_nonopt != parser->state.next); |
---|
1093 | |
---|
1094 | if (parser->first_nonopt != parser->last_nonopt) |
---|
1095 | { |
---|
1096 | exchange(parser); |
---|
1097 | |
---|
1098 | /* Start processing the skipped and the quoted |
---|
1099 | arguments. */ |
---|
1100 | |
---|
1101 | parser->state.quoted = parser->state.next = parser->first_nonopt; |
---|
1102 | |
---|
1103 | /* Also empty the skipped-list, to avoid confusion |
---|
1104 | if the user resets the next pointer. */ |
---|
1105 | parser->first_nonopt = parser->last_nonopt = 0; |
---|
1106 | } |
---|
1107 | else |
---|
1108 | parser->state.quoted = parser->state.next; |
---|
1109 | |
---|
1110 | parser->args_only = 1; |
---|
1111 | return 0; |
---|
1112 | |
---|
1113 | case ARG_LONG_ONLY_OPTION: |
---|
1114 | case ARG_LONG_OPTION: |
---|
1115 | { |
---|
1116 | struct group *group; |
---|
1117 | const struct argp_option *option; |
---|
1118 | char *value; |
---|
1119 | |
---|
1120 | parser->state.next++; |
---|
1121 | option = find_long_option(parser, optstart, &group); |
---|
1122 | |
---|
1123 | if (!option) |
---|
1124 | { |
---|
1125 | /* NOTE: This includes any "=something" in the output. */ |
---|
1126 | fprintf (parser->state.err_stream, |
---|
1127 | dgettext(parser->state.root_argp->argp_domain, |
---|
1128 | "%s: unrecognized option `%s'\n"), |
---|
1129 | parser->state.name, arg); |
---|
1130 | *arg_ebadkey = 0; |
---|
1131 | return EBADKEY; |
---|
1132 | } |
---|
1133 | |
---|
1134 | value = strchr(optstart, '='); |
---|
1135 | if (value) |
---|
1136 | value++; |
---|
1137 | |
---|
1138 | if (value && !option->arg) |
---|
1139 | /* Unexpected argument. */ |
---|
1140 | { |
---|
1141 | if (token == ARG_LONG_OPTION) |
---|
1142 | /* --option */ |
---|
1143 | fprintf (parser->state.err_stream, |
---|
1144 | dgettext(parser->state.root_argp->argp_domain, |
---|
1145 | "%s: option `--%s' doesn't allow an argument\n"), |
---|
1146 | parser->state.name, option->name); |
---|
1147 | else |
---|
1148 | /* +option or -option */ |
---|
1149 | fprintf (parser->state.err_stream, |
---|
1150 | dgettext(parser->state.root_argp->argp_domain, |
---|
1151 | "%s: option `%c%s' doesn't allow an argument\n"), |
---|
1152 | parser->state.name, arg[0], option->name); |
---|
1153 | |
---|
1154 | *arg_ebadkey = 0; |
---|
1155 | return EBADKEY; |
---|
1156 | } |
---|
1157 | |
---|
1158 | if (option->arg && !value |
---|
1159 | && !(option->flags & OPTION_ARG_OPTIONAL)) |
---|
1160 | /* We need an mandatory argument. */ |
---|
1161 | { |
---|
1162 | if (parser->state.next == parser->state.argc) |
---|
1163 | /* Missing argument */ |
---|
1164 | { |
---|
1165 | if (token == ARG_LONG_OPTION) |
---|
1166 | /* --option */ |
---|
1167 | fprintf (parser->state.err_stream, |
---|
1168 | dgettext(parser->state.root_argp->argp_domain, |
---|
1169 | "%s: option `--%s' requires an argument\n"), |
---|
1170 | parser->state.name, option->name); |
---|
1171 | else |
---|
1172 | /* +option or -option */ |
---|
1173 | fprintf (parser->state.err_stream, |
---|
1174 | dgettext(parser->state.root_argp->argp_domain, |
---|
1175 | "%s: option `%c%s' requires an argument\n"), |
---|
1176 | parser->state.name, arg[0], option->name); |
---|
1177 | |
---|
1178 | *arg_ebadkey = 0; |
---|
1179 | return EBADKEY; |
---|
1180 | } |
---|
1181 | |
---|
1182 | value = parser->state.argv[parser->state.next++]; |
---|
1183 | } |
---|
1184 | *arg_ebadkey = 0; |
---|
1185 | return group_parse(group, &parser->state, |
---|
1186 | option->key, value); |
---|
1187 | } |
---|
1188 | case ARG_SHORT_OPTION: |
---|
1189 | parser->state.next++; |
---|
1190 | parser->nextchar = optstart; |
---|
1191 | return 0; |
---|
1192 | |
---|
1193 | default: |
---|
1194 | abort(); |
---|
1195 | } |
---|
1196 | } |
---|
1197 | } |
---|
1198 | } |
---|
1199 | |
---|
1200 | /* Parse the options strings in ARGC & ARGV according to the argp in ARGP. |
---|
1201 | FLAGS is one of the ARGP_ flags above. If END_INDEX is non-NULL, the |
---|
1202 | index in ARGV of the first unparsed option is returned in it. If an |
---|
1203 | unknown option is present, EINVAL is returned; if some parser routine |
---|
1204 | returned a non-zero value, it is returned; otherwise 0 is returned. */ |
---|
1205 | error_t |
---|
1206 | __argp_parse (const struct argp *argp, int argc, char **argv, unsigned flags, |
---|
1207 | int *end_index, void *input) |
---|
1208 | { |
---|
1209 | error_t err; |
---|
1210 | struct parser parser; |
---|
1211 | |
---|
1212 | /* If true, then err == EBADKEY is a result of a non-option argument failing |
---|
1213 | to be parsed (which in some cases isn't actually an error). */ |
---|
1214 | int arg_ebadkey = 0; |
---|
1215 | |
---|
1216 | if (! (flags & ARGP_NO_HELP)) |
---|
1217 | /* Add our own options. */ |
---|
1218 | { |
---|
1219 | struct argp_child *child = alloca (4 * sizeof (struct argp_child)); |
---|
1220 | struct argp *top_argp = alloca (sizeof (struct argp)); |
---|
1221 | |
---|
1222 | /* TOP_ARGP has no options, it just serves to group the user & default |
---|
1223 | argps. */ |
---|
1224 | memset (top_argp, 0, sizeof (*top_argp)); |
---|
1225 | top_argp->children = child; |
---|
1226 | |
---|
1227 | memset (child, 0, 4 * sizeof (struct argp_child)); |
---|
1228 | |
---|
1229 | if (argp) |
---|
1230 | (child++)->argp = argp; |
---|
1231 | (child++)->argp = &argp_default_argp; |
---|
1232 | if (argp_program_version || argp_program_version_hook) |
---|
1233 | (child++)->argp = &argp_version_argp; |
---|
1234 | child->argp = 0; |
---|
1235 | |
---|
1236 | argp = top_argp; |
---|
1237 | } |
---|
1238 | |
---|
1239 | /* Construct a parser for these arguments. */ |
---|
1240 | err = parser_init (&parser, argp, argc, argv, flags, input); |
---|
1241 | |
---|
1242 | if (! err) |
---|
1243 | /* Parse! */ |
---|
1244 | { |
---|
1245 | while (! err) |
---|
1246 | err = parser_parse_next (&parser, &arg_ebadkey); |
---|
1247 | err = parser_finalize (&parser, err, arg_ebadkey, end_index); |
---|
1248 | } |
---|
1249 | |
---|
1250 | return err; |
---|
1251 | } |
---|
1252 | #ifdef weak_alias |
---|
1253 | weak_alias (__argp_parse, argp_parse) |
---|
1254 | #endif |
---|
1255 | |
---|
1256 | /* Return the input field for ARGP in the parser corresponding to STATE; used |
---|
1257 | by the help routines. */ |
---|
1258 | void * |
---|
1259 | __argp_input (const struct argp *argp, const struct argp_state *state) |
---|
1260 | { |
---|
1261 | if (state) |
---|
1262 | { |
---|
1263 | struct group *group; |
---|
1264 | struct parser *parser = state->pstate; |
---|
1265 | |
---|
1266 | for (group = parser->groups; group < parser->egroup; group++) |
---|
1267 | if (group->argp == argp) |
---|
1268 | return group->input; |
---|
1269 | } |
---|
1270 | |
---|
1271 | return 0; |
---|
1272 | } |
---|
1273 | #ifdef weak_alias |
---|
1274 | weak_alias (__argp_input, _argp_input) |
---|
1275 | #endif |
---|
1276 | |
---|
1277 | /* Defined here, in case a user is not inlining the definitions in |
---|
1278 | * argp.h */ |
---|
1279 | void |
---|
1280 | __argp_usage (__const struct argp_state *__state) |
---|
1281 | { |
---|
1282 | __argp_state_help (__state, stderr, ARGP_HELP_STD_USAGE); |
---|
1283 | } |
---|
1284 | |
---|
1285 | int |
---|
1286 | __option_is_short (__const struct argp_option *__opt) |
---|
1287 | { |
---|
1288 | if (__opt->flags & OPTION_DOC) |
---|
1289 | return 0; |
---|
1290 | else |
---|
1291 | { |
---|
1292 | int __key = __opt->key; |
---|
1293 | /* FIXME: whether or not a particular key implies a short option |
---|
1294 | * ought not to be locale dependent. */ |
---|
1295 | return __key > 0 && isprint (__key); |
---|
1296 | } |
---|
1297 | } |
---|
1298 | |
---|
1299 | int |
---|
1300 | __option_is_end (__const struct argp_option *__opt) |
---|
1301 | { |
---|
1302 | return !__opt->key && !__opt->name && !__opt->doc && !__opt->group; |
---|
1303 | } |
---|