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