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