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