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