| 1 |
/* Hierarchical argument parsing, layered over getopt |
| 2 |
Copyright (C) 1995-2026 Free Software Foundation, Inc. |
| 3 |
This file is part of the GNU C Library. |
| 4 |
Written by Miles Bader <[email protected]>. |
| 5 |
|
| 6 |
This file is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU Lesser General Public License as |
| 8 |
published by the Free Software Foundation, either version 3 of the |
| 9 |
License, or (at your option) any later version. |
| 10 |
|
| 11 |
This file 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 |
| 14 |
GNU Lesser General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU Lesser General Public License |
| 17 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 18 |
|
| 19 |
#ifdef HAVE_CONFIG_H |
| 20 |
# include <config.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
#include <alloca.h> |
| 24 |
#include <stddef.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
#include <unistd.h> |
| 28 |
#include <limits.h> |
| 29 |
#include <getopt.h> |
| 30 |
#include <getopt_int.h> |
| 31 |
|
| 32 |
#ifdef _LIBC |
| 33 |
# include <libintl.h> |
| 34 |
# undef dgettext |
| 35 |
# define dgettext(domain, msgid) \ |
| 36 |
__dcgettext (domain, msgid, LC_MESSAGES) |
| 37 |
#else |
| 38 |
# include "gettext.h" |
| 39 |
#endif |
| 40 |
#define N_(msgid) msgid |
| 41 |
|
| 42 |
#include "argp.h" |
| 43 |
#include "argp-namefrob.h" |
| 44 |
|
| 45 |
#define alignto(n, d) ((((n) + (d) - 1) / (d)) * (d)) |
| 46 |
|
| 47 |
/* Getopt return values. */ |
| 48 |
#define KEY_END (-1) /* The end of the options. */ |
| 49 |
#define KEY_ARG 1 /* A non-option argument. */ |
| 50 |
#define KEY_ERR '?' /* An error parsing the options. */ |
| 51 |
|
| 52 |
/* The meta-argument used to prevent any further arguments being interpreted |
| 53 |
as options. */ |
| 54 |
#define QUOTE "--" |
| 55 |
|
| 56 |
/* The number of bits we steal in a long-option value for our own use. */ |
| 57 |
#define GROUP_BITS CHAR_BIT |
| 58 |
|
| 59 |
/* The number of bits available for the user value. */ |
| 60 |
#define USER_BITS ((sizeof ((struct option *)0)->val * CHAR_BIT) - GROUP_BITS) |
| 61 |
#define USER_MASK ((1 << USER_BITS) - 1) |
| 62 |
|
| 63 |
/* EZ alias for ARGP_ERR_UNKNOWN. */ |
| 64 |
#define EBADKEY ARGP_ERR_UNKNOWN |
| 65 |
|
| 66 |
/* Default options. */ |
| 67 |
|
| 68 |
/* When argp is given the --HANG switch, _ARGP_HANG is set and argp will sleep |
| 69 |
for one second intervals, decrementing _ARGP_HANG until it's zero. Thus |
| 70 |
you can force the program to continue by attaching a debugger and setting |
| 71 |
it to 0 yourself. */ |
| 72 |
static volatile int _argp_hang; |
| 73 |
|
| 74 |
#define OPT_PROGNAME -2 |
| 75 |
#define OPT_USAGE -3 |
| 76 |
#define OPT_HANG -4 |
| 77 |
|
| 78 |
static const struct argp_option argp_default_options[] = |
| 79 |
{ |
| 80 |
{"help", '?', NULL, 0, N_("give this help list"), -1}, |
| 81 |
{"usage", OPT_USAGE, NULL, 0, N_("give a short usage message"), 0}, |
| 82 |
{"program-name",OPT_PROGNAME, N_("NAME"), OPTION_HIDDEN, |
| 83 |
N_("set the program name"), 0}, |
| 84 |
{"HANG", OPT_HANG, N_("SECS"), OPTION_ARG_OPTIONAL | OPTION_HIDDEN, |
| 85 |
N_("hang for SECS seconds (default 3600)"), 0}, |
| 86 |
{NULL, 0, NULL, 0, NULL, 0} |
| 87 |
}; |
| 88 |
|
| 89 |
static error_t |
| 90 |
argp_default_parser (int key, char *arg, struct argp_state *state) |
| 91 |
{ |
| 92 |
switch (key) |
| 93 |
{ |
| 94 |
case '?': |
| 95 |
__argp_state_help (state, state->out_stream, ARGP_HELP_STD_HELP); |
| 96 |
break; |
| 97 |
case OPT_USAGE: |
| 98 |
__argp_state_help (state, state->out_stream, |
| 99 |
ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK); |
| 100 |
break; |
| 101 |
|
| 102 |
case OPT_PROGNAME: /* Set the program name. */ |
| 103 |
#if defined _LIBC || HAVE_DECL_PROGRAM_INVOCATION_NAME |
| 104 |
program_invocation_name = arg; |
| 105 |
#endif |
| 106 |
/* [Note that some systems only have PROGRAM_INVOCATION_SHORT_NAME (aka |
| 107 |
__PROGNAME), in which case, PROGRAM_INVOCATION_NAME is just defined |
| 108 |
to be that, so we have to be a bit careful here.] */ |
| 109 |
|
| 110 |
/* Update what we use for messages. */ |
| 111 |
state->name = __argp_base_name (arg); |
| 112 |
|
| 113 |
#if defined _LIBC || HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME |
| 114 |
program_invocation_short_name = state->name; |
| 115 |
#endif |
| 116 |
|
| 117 |
if ((state->flags & (ARGP_PARSE_ARGV0 | ARGP_NO_ERRS)) |
| 118 |
== ARGP_PARSE_ARGV0) |
| 119 |
/* Update what getopt uses too. */ |
| 120 |
state->argv[0] = arg; |
| 121 |
|
| 122 |
break; |
| 123 |
|
| 124 |
case OPT_HANG: |
| 125 |
_argp_hang = atoi (arg ? arg : "3600"); |
| 126 |
while (_argp_hang-- > 0) |
| 127 |
__sleep (1); |
| 128 |
break; |
| 129 |
|
| 130 |
default: |
| 131 |
return EBADKEY; |
| 132 |
} |
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
static const struct argp argp_default_argp = |
| 137 |
{argp_default_options, &argp_default_parser, NULL, NULL, NULL, NULL, "libc"}; |
| 138 |
|
| 139 |
|
| 140 |
static const struct argp_option argp_version_options[] = |
| 141 |
{ |
| 142 |
{"version", 'V', NULL, 0, N_("print program version"), -1}, |
| 143 |
{NULL, 0, NULL, 0, NULL, 0} |
| 144 |
}; |
| 145 |
|
| 146 |
static error_t |
| 147 |
argp_version_parser (int key, char *arg, struct argp_state *state) |
| 148 |
{ |
| 149 |
switch (key) |
| 150 |
{ |
| 151 |
case 'V': |
| 152 |
if (argp_program_version_hook) |
| 153 |
(*argp_program_version_hook) (state->out_stream, state); |
| 154 |
else if (argp_program_version) |
| 155 |
fprintf (state->out_stream, "%s\n", argp_program_version); |
| 156 |
else |
| 157 |
__argp_error (state, "%s", |
| 158 |
dgettext (state->root_argp->argp_domain, |
| 159 |
"(PROGRAM ERROR) No version known!?")); |
| 160 |
if (! (state->flags & ARGP_NO_EXIT)) |
| 161 |
exit (0); |
| 162 |
break; |
| 163 |
default: |
| 164 |
return EBADKEY; |
| 165 |
} |
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
static const struct argp argp_version_argp = |
| 170 |
{argp_version_options, &argp_version_parser, NULL, NULL, NULL, NULL, "libc"}; |
| 171 |
|
| 172 |
/* Returns the offset into the getopt long options array LONG_OPTIONS of a |
| 173 |
long option with called NAME, or -1 if none is found. Passing NULL as |
| 174 |
NAME will return the number of options. */ |
| 175 |
static int |
| 176 |
find_long_option (struct option *long_options, const char *name) |
| 177 |
{ |
| 178 |
struct option *l = long_options; |
| 179 |
while (l->name != NULL) |
| 180 |
if (name != NULL && streq (l->name, name)) |
| 181 |
return l - long_options; |
| 182 |
else |
| 183 |
l++; |
| 184 |
if (name == NULL) |
| 185 |
return l - long_options; |
| 186 |
else |
| 187 |
return -1; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
/* The state of a "group" during parsing. Each group corresponds to a |
| 192 |
particular argp structure from the tree of such descending from the top |
| 193 |
level argp passed to argp_parse. */ |
| 194 |
struct group |
| 195 |
{ |
| 196 |
/* This group's parsing function. */ |
| 197 |
argp_parser_t parser; |
| 198 |
|
| 199 |
/* Which argp this group is from. */ |
| 200 |
const struct argp *argp; |
| 201 |
|
| 202 |
/* Points to the point in SHORT_OPTS corresponding to the end of the short |
| 203 |
options for this group. We use it to determine from which group a |
| 204 |
particular short options is from. */ |
| 205 |
char *short_end; |
| 206 |
|
| 207 |
/* The number of non-option args successfully handled by this parser. */ |
| 208 |
unsigned args_processed; |
| 209 |
|
| 210 |
/* This group's parser's parent's group. */ |
| 211 |
struct group *parent; |
| 212 |
unsigned parent_index; /* And the our position in the parent. */ |
| 213 |
|
| 214 |
/* These fields are swapped into and out of the state structure when |
| 215 |
calling this group's parser. */ |
| 216 |
void *input, **child_inputs; |
| 217 |
void *hook; |
| 218 |
}; |
| 219 |
|
| 220 |
/* Call GROUP's parser with KEY and ARG, swapping any group-specific info |
| 221 |
from STATE before calling, and back into state afterwards. If GROUP has |
| 222 |
no parser, EBADKEY is returned. */ |
| 223 |
static error_t |
| 224 |
group_parse (struct group *group, struct argp_state *state, int key, char *arg) |
| 225 |
{ |
| 226 |
if (group->parser) |
| 227 |
{ |
| 228 |
state->hook = group->hook; |
| 229 |
state->input = group->input; |
| 230 |
state->child_inputs = group->child_inputs; |
| 231 |
state->arg_num = group->args_processed; |
| 232 |
error_t err = (*group->parser)(key, arg, state); |
| 233 |
group->hook = state->hook; |
| 234 |
return err; |
| 235 |
} |
| 236 |
else |
| 237 |
return EBADKEY; |
| 238 |
} |
| 239 |
|
| 240 |
struct parser |
| 241 |
{ |
| 242 |
const struct argp *argp; |
| 243 |
|
| 244 |
/* SHORT_OPTS is the getopt short options string for the union of all the |
| 245 |
groups of options. */ |
| 246 |
char *short_opts; |
| 247 |
/* LONG_OPTS is the array of getop long option structures for the union of |
| 248 |
all the groups of options. */ |
| 249 |
struct option *long_opts; |
| 250 |
/* OPT_DATA is the getopt data used for the reentrant getopt. */ |
| 251 |
struct _getopt_data opt_data; |
| 252 |
|
| 253 |
/* States of the various parsing groups. */ |
| 254 |
struct group *groups; |
| 255 |
/* The end of the GROUPS array. */ |
| 256 |
struct group *egroup; |
| 257 |
/* A vector containing storage for the CHILD_INPUTS field in all groups. */ |
| 258 |
void **child_inputs; |
| 259 |
|
| 260 |
/* True if we think using getopt is still useful; if false, then |
| 261 |
remaining arguments are just passed verbatim with ARGP_KEY_ARG. This is |
| 262 |
cleared whenever getopt returns KEY_END, but may be set again if the user |
| 263 |
moves the next argument pointer backwards. */ |
| 264 |
int try_getopt; |
| 265 |
|
| 266 |
/* State block supplied to parsing routines. */ |
| 267 |
struct argp_state state; |
| 268 |
|
| 269 |
/* Memory used by this parser. */ |
| 270 |
void *storage; |
| 271 |
}; |
| 272 |
|
| 273 |
/* The next usable entries in the various parser tables being filled in by |
| 274 |
convert_options. */ |
| 275 |
struct parser_convert_state |
| 276 |
{ |
| 277 |
struct parser *parser; |
| 278 |
char *short_end; |
| 279 |
struct option *long_end; |
| 280 |
void **child_inputs_end; |
| 281 |
}; |
| 282 |
|
| 283 |
/* Converts all options in ARGP (which is put in GROUP) and ancestors |
| 284 |
into getopt options stored in SHORT_OPTS and LONG_OPTS; SHORT_END and |
| 285 |
CVT->LONG_END are the points at which new options are added. Returns the |
| 286 |
next unused group entry. CVT holds state used during the conversion. */ |
| 287 |
static struct group * |
| 288 |
convert_options (const struct argp *argp, |
| 289 |
struct group *parent, unsigned parent_index, |
| 290 |
struct group *group, struct parser_convert_state *cvt) |
| 291 |
{ |
| 292 |
/* REAL is the most recent non-alias value of OPT. */ |
| 293 |
const struct argp_option *real = argp->options; |
| 294 |
const struct argp_child *children = argp->children; |
| 295 |
|
| 296 |
if (real || argp->parser) |
| 297 |
{ |
| 298 |
const struct argp_option *opt; |
| 299 |
|
| 300 |
if (real) |
| 301 |
for (opt = real; !__option_is_end (opt); opt++) |
| 302 |
{ |
| 303 |
if (! (opt->flags & OPTION_ALIAS)) |
| 304 |
/* OPT isn't an alias, so we can use values from it. */ |
| 305 |
real = opt; |
| 306 |
|
| 307 |
if (! (real->flags & OPTION_DOC)) |
| 308 |
/* A real option (not just documentation). */ |
| 309 |
{ |
| 310 |
if (__option_is_short (opt)) |
| 311 |
/* OPT can be used as a short option. */ |
| 312 |
{ |
| 313 |
*cvt->short_end++ = opt->key; |
| 314 |
if (real->arg) |
| 315 |
{ |
| 316 |
*cvt->short_end++ = ':'; |
| 317 |
if (real->flags & OPTION_ARG_OPTIONAL) |
| 318 |
*cvt->short_end++ = ':'; |
| 319 |
} |
| 320 |
*cvt->short_end = '\0'; /* keep 0 terminated */ |
| 321 |
} |
| 322 |
|
| 323 |
if (opt->name |
| 324 |
&& find_long_option (cvt->parser->long_opts, opt->name) < 0) |
| 325 |
/* OPT can be used as a long option. */ |
| 326 |
{ |
| 327 |
cvt->long_end->name = opt->name; |
| 328 |
cvt->long_end->has_arg = |
| 329 |
(real->arg |
| 330 |
? (real->flags & OPTION_ARG_OPTIONAL |
| 331 |
? optional_argument |
| 332 |
: required_argument) |
| 333 |
: no_argument); |
| 334 |
cvt->long_end->flag = NULL; |
| 335 |
/* we add a disambiguating code to all the user's |
| 336 |
values (which is removed before we actually call |
| 337 |
the function to parse the value); this means that |
| 338 |
the user loses use of the high 8 bits in all his |
| 339 |
values (the sign of the lower bits is preserved |
| 340 |
however)... */ |
| 341 |
cvt->long_end->val = |
| 342 |
((opt->key ? opt->key : real->key) & USER_MASK) |
| 343 |
+ (((group - cvt->parser->groups) + 1) << USER_BITS); |
| 344 |
|
| 345 |
/* Keep the LONG_OPTS list terminated. */ |
| 346 |
(++cvt->long_end)->name = NULL; |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
group->parser = argp->parser; |
| 352 |
group->argp = argp; |
| 353 |
group->short_end = cvt->short_end; |
| 354 |
group->args_processed = 0; |
| 355 |
group->parent = parent; |
| 356 |
group->parent_index = parent_index; |
| 357 |
group->input = NULL; |
| 358 |
group->hook = NULL; |
| 359 |
group->child_inputs = NULL; |
| 360 |
|
| 361 |
if (children) |
| 362 |
/* Assign GROUP's CHILD_INPUTS field some space from |
| 363 |
CVT->child_inputs_end.*/ |
| 364 |
{ |
| 365 |
unsigned num_children = 0; |
| 366 |
while (children[num_children].argp) |
| 367 |
num_children++; |
| 368 |
group->child_inputs = cvt->child_inputs_end; |
| 369 |
cvt->child_inputs_end += num_children; |
| 370 |
} |
| 371 |
|
| 372 |
parent = group++; |
| 373 |
} |
| 374 |
else |
| 375 |
parent = NULL; |
| 376 |
|
| 377 |
if (children) |
| 378 |
{ |
| 379 |
unsigned index = 0; |
| 380 |
while (children->argp) |
| 381 |
group = |
| 382 |
convert_options (children++->argp, parent, index++, group, cvt); |
| 383 |
} |
| 384 |
|
| 385 |
return group; |
| 386 |
} |
| 387 |
|
| 388 |
/* Find the merged set of getopt options, with keys appropriately prefixed. */ |
| 389 |
static void |
| 390 |
parser_convert (struct parser *parser, const struct argp *argp, int flags) |
| 391 |
{ |
| 392 |
struct parser_convert_state cvt; |
| 393 |
|
| 394 |
cvt.parser = parser; |
| 395 |
cvt.short_end = parser->short_opts; |
| 396 |
cvt.long_end = parser->long_opts; |
| 397 |
cvt.child_inputs_end = parser->child_inputs; |
| 398 |
|
| 399 |
if (flags & ARGP_IN_ORDER) |
| 400 |
*cvt.short_end++ = '-'; |
| 401 |
else if (flags & ARGP_NO_ARGS) |
| 402 |
*cvt.short_end++ = '+'; |
| 403 |
*cvt.short_end = '\0'; |
| 404 |
|
| 405 |
cvt.long_end->name = NULL; |
| 406 |
|
| 407 |
parser->argp = argp; |
| 408 |
|
| 409 |
if (argp) |
| 410 |
parser->egroup = convert_options (argp, NULL, 0, parser->groups, &cvt); |
| 411 |
else |
| 412 |
parser->egroup = parser->groups; /* No parsers at all! */ |
| 413 |
} |
| 414 |
|
| 415 |
/* Lengths of various parser fields which we will allocated. */ |
| 416 |
struct parser_sizes |
| 417 |
{ |
| 418 |
size_t short_len; /* Getopt short options string. */ |
| 419 |
size_t long_len; /* Getopt long options vector. */ |
| 420 |
size_t num_groups; /* Group structures we allocate. */ |
| 421 |
size_t num_child_inputs; /* Child input slots. */ |
| 422 |
}; |
| 423 |
|
| 424 |
/* For ARGP, increments the NUM_GROUPS field in SZS by the total number of |
| 425 |
argp structures descended from it, and the SHORT_LEN & LONG_LEN fields by |
| 426 |
the maximum lengths of the resulting merged getopt short options string and |
| 427 |
long-options array, respectively. */ |
| 428 |
static void |
| 429 |
calc_sizes (const struct argp *argp, struct parser_sizes *szs) |
| 430 |
{ |
| 431 |
const struct argp_child *child = argp->children; |
| 432 |
const struct argp_option *opt = argp->options; |
| 433 |
|
| 434 |
if (opt || argp->parser) |
| 435 |
{ |
| 436 |
szs->num_groups++; |
| 437 |
if (opt) |
| 438 |
{ |
| 439 |
int num_opts = 0; |
| 440 |
while (!__option_is_end (opt++)) |
| 441 |
num_opts++; |
| 442 |
szs->short_len += num_opts * 3; /* opt + up to 2 ':'s */ |
| 443 |
szs->long_len += num_opts; |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
if (child) |
| 448 |
while (child->argp) |
| 449 |
{ |
| 450 |
calc_sizes ((child++)->argp, szs); |
| 451 |
szs->num_child_inputs++; |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
/* Initializes PARSER to parse ARGP in a manner described by FLAGS. */ |
| 456 |
static error_t |
| 457 |
parser_init (struct parser *parser, const struct argp *argp, |
| 458 |
int argc, char **argv, int flags, void *input) |
| 459 |
{ |
| 460 |
struct parser_sizes szs; |
| 461 |
szs.short_len = (flags & ARGP_NO_ARGS) ? 0 : 1; |
| 462 |
szs.long_len = 0; |
| 463 |
szs.num_groups = 0; |
| 464 |
szs.num_child_inputs = 0; |
| 465 |
|
| 466 |
if (argp) |
| 467 |
calc_sizes (argp, &szs); |
| 468 |
|
| 469 |
/* Lengths of the various bits of storage used by PARSER. */ |
| 470 |
size_t glen = (szs.num_groups + 1) * sizeof (struct group); |
| 471 |
size_t clen = szs.num_child_inputs * sizeof (void *); |
| 472 |
size_t llen = (szs.long_len + 1) * sizeof (struct option); |
| 473 |
size_t slen = szs.short_len + 1; |
| 474 |
|
| 475 |
/* Sums of previous lengths, properly aligned. There's no need to |
| 476 |
align gsum, since struct group is aligned at least as strictly as |
| 477 |
void * (since it contains a void * member). And there's no need |
| 478 |
to align lsum, since struct option is aligned at least as |
| 479 |
strictly as char. */ |
| 480 |
size_t gsum = glen; |
| 481 |
size_t csum = alignto (gsum + clen, alignof (struct option)); |
| 482 |
size_t lsum = csum + llen; |
| 483 |
size_t ssum = lsum + slen; |
| 484 |
|
| 485 |
parser->storage = malloc (ssum); |
| 486 |
if (! parser->storage) |
| 487 |
return ENOMEM; |
| 488 |
|
| 489 |
char *storage = parser->storage; |
| 490 |
parser->groups = parser->storage; |
| 491 |
parser->child_inputs = (void **) (storage + gsum); |
| 492 |
parser->long_opts = (struct option *) (storage + csum); |
| 493 |
parser->short_opts = storage + lsum; |
| 494 |
{ |
| 495 |
struct _getopt_data opt_data = _GETOPT_DATA_INITIALIZER; |
| 496 |
parser->opt_data = opt_data; |
| 497 |
} |
| 498 |
|
| 499 |
memset (parser->child_inputs, 0, clen); |
| 500 |
parser_convert (parser, argp, flags); |
| 501 |
|
| 502 |
memset (&parser->state, 0, sizeof (struct argp_state)); |
| 503 |
parser->state.root_argp = parser->argp; |
| 504 |
parser->state.argc = argc; |
| 505 |
parser->state.argv = argv; |
| 506 |
parser->state.flags = flags; |
| 507 |
parser->state.err_stream = stderr; |
| 508 |
parser->state.out_stream = stdout; |
| 509 |
parser->state.next = 0; /* Tell getopt to initialize. */ |
| 510 |
parser->state.pstate = parser; |
| 511 |
|
| 512 |
parser->try_getopt = 1; |
| 513 |
|
| 514 |
/* Call each parser for the first time, giving it a chance to propagate |
| 515 |
values to child parsers. */ |
| 516 |
if (parser->groups < parser->egroup) |
| 517 |
parser->groups->input = input; |
| 518 |
{ |
| 519 |
error_t err = 0; |
| 520 |
|
| 521 |
for (struct group *group = parser->groups; |
| 522 |
group < parser->egroup && (!err || err == EBADKEY); |
| 523 |
group++) |
| 524 |
{ |
| 525 |
if (group->parent) |
| 526 |
/* If a child parser, get the initial input value from the parent. */ |
| 527 |
group->input = group->parent->child_inputs[group->parent_index]; |
| 528 |
|
| 529 |
if (!group->parser |
| 530 |
&& group->argp->children && group->argp->children->argp) |
| 531 |
/* For the special case where no parsing function is supplied for an |
| 532 |
argp, propagate its input to its first child, if any (this just |
| 533 |
makes very simple wrapper argps more convenient). */ |
| 534 |
group->child_inputs[0] = group->input; |
| 535 |
|
| 536 |
err = group_parse (group, &parser->state, ARGP_KEY_INIT, NULL); |
| 537 |
} |
| 538 |
if (err == EBADKEY) |
| 539 |
err = 0; /* Some parser didn't understand. */ |
| 540 |
|
| 541 |
if (err) |
| 542 |
return err; |
| 543 |
} |
| 544 |
|
| 545 |
if (parser->state.flags & ARGP_NO_ERRS) |
| 546 |
{ |
| 547 |
parser->opt_data.opterr = 0; |
| 548 |
if (parser->state.flags & ARGP_PARSE_ARGV0) |
| 549 |
/* getopt always skips ARGV[0], so we have to fake it out. As long |
| 550 |
as OPTERR is 0, then it shouldn't actually try to access it. */ |
| 551 |
parser->state.argv--, parser->state.argc++; |
| 552 |
} |
| 553 |
else |
| 554 |
parser->opt_data.opterr = 1; /* Print error messages. */ |
| 555 |
|
| 556 |
if (parser->state.argv == argv && argv[0]) |
| 557 |
/* There's an argv[0]; use it for messages. */ |
| 558 |
parser->state.name = __argp_base_name (argv[0]); |
| 559 |
else |
| 560 |
parser->state.name = __argp_short_program_name (); |
| 561 |
|
| 562 |
return 0; |
| 563 |
} |
| 564 |
|
| 565 |
/* Free any storage consumed by PARSER (but not PARSER itself). */ |
| 566 |
static error_t |
| 567 |
parser_finalize (struct parser *parser, |
| 568 |
error_t err, int arg_ebadkey, int *end_index) |
| 569 |
{ |
| 570 |
if (err == EBADKEY && arg_ebadkey) |
| 571 |
/* Suppress errors generated by unparsed arguments. */ |
| 572 |
err = 0; |
| 573 |
|
| 574 |
if (! err) |
| 575 |
{ |
| 576 |
if (parser->state.next == parser->state.argc) |
| 577 |
/* We successfully parsed all arguments! Call all the parsers again, |
| 578 |
just a few more times... */ |
| 579 |
{ |
| 580 |
for (struct group *group = parser->groups; |
| 581 |
group < parser->egroup && (!err || err==EBADKEY); |
| 582 |
group++) |
| 583 |
if (group->args_processed == 0) |
| 584 |
err = group_parse (group, &parser->state, ARGP_KEY_NO_ARGS, NULL); |
| 585 |
for (struct group *group = parser->egroup - 1; |
| 586 |
group >= parser->groups && (!err || err==EBADKEY); |
| 587 |
group--) |
| 588 |
err = group_parse (group, &parser->state, ARGP_KEY_END, NULL); |
| 589 |
|
| 590 |
if (err == EBADKEY) |
| 591 |
err = 0; /* Some parser didn't understand. */ |
| 592 |
|
| 593 |
/* Tell the user that all arguments are parsed. */ |
| 594 |
if (end_index) |
| 595 |
*end_index = parser->state.next; |
| 596 |
} |
| 597 |
else if (end_index) |
| 598 |
/* Return any remaining arguments to the user. */ |
| 599 |
*end_index = parser->state.next; |
| 600 |
else |
| 601 |
/* No way to return the remaining arguments, they must be bogus. */ |
| 602 |
{ |
| 603 |
if (!(parser->state.flags & ARGP_NO_ERRS) |
| 604 |
&& parser->state.err_stream) |
| 605 |
fprintf (parser->state.err_stream, |
| 606 |
dgettext (parser->argp->argp_domain, |
| 607 |
"%s: Too many arguments\n"), |
| 608 |
parser->state.name); |
| 609 |
err = EBADKEY; |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
/* Okay, we're all done, with either an error or success; call the parsers |
| 614 |
to indicate which one. */ |
| 615 |
|
| 616 |
if (err) |
| 617 |
{ |
| 618 |
/* Maybe print an error message. */ |
| 619 |
if (err == EBADKEY) |
| 620 |
/* An appropriate message describing what the error was should have |
| 621 |
been printed earlier. */ |
| 622 |
__argp_state_help (&parser->state, parser->state.err_stream, |
| 623 |
ARGP_HELP_STD_ERR); |
| 624 |
|
| 625 |
/* Since we didn't exit, give each parser an error indication. */ |
| 626 |
for (struct group *group = parser->groups; group < parser->egroup; group++) |
| 627 |
group_parse (group, &parser->state, ARGP_KEY_ERROR, NULL); |
| 628 |
} |
| 629 |
else |
| 630 |
/* Notify parsers of success, and propagate back values from parsers. */ |
| 631 |
{ |
| 632 |
/* We pass over the groups in reverse order so that child groups are |
| 633 |
given a chance to do there processing before passing back a value to |
| 634 |
the parent. */ |
| 635 |
for (struct group *group = parser->egroup - 1 |
| 636 |
; group >= parser->groups && (!err || err == EBADKEY) |
| 637 |
; group--) |
| 638 |
err = group_parse (group, &parser->state, ARGP_KEY_SUCCESS, NULL); |
| 639 |
if (err == EBADKEY) |
| 640 |
err = 0; /* Some parser didn't understand. */ |
| 641 |
} |
| 642 |
|
| 643 |
/* Call parsers once more, to do any final cleanup. Errors are ignored. */ |
| 644 |
for (struct group *group = parser->egroup - 1; group >= parser->groups; group--) |
| 645 |
group_parse (group, &parser->state, ARGP_KEY_FINI, NULL); |
| 646 |
|
| 647 |
if (err == EBADKEY) |
| 648 |
err = EINVAL; |
| 649 |
|
| 650 |
free (parser->storage); |
| 651 |
|
| 652 |
return err; |
| 653 |
} |
| 654 |
|
| 655 |
/* Call the user parsers to parse the non-option argument VAL, at the current |
| 656 |
position, returning any error. The state NEXT pointer is assumed to have |
| 657 |
been adjusted (by getopt) to point after this argument; this function will |
| 658 |
adjust it correctly to reflect however many args actually end up being |
| 659 |
consumed. */ |
| 660 |
static error_t |
| 661 |
parser_parse_arg (struct parser *parser, char *val) |
| 662 |
{ |
| 663 |
/* Save the starting value of NEXT, first adjusting it so that the arg |
| 664 |
we're parsing is again the front of the arg vector. */ |
| 665 |
int index = --parser->state.next; |
| 666 |
error_t err = EBADKEY; |
| 667 |
struct group *group; |
| 668 |
int key = 0; /* Which of ARGP_KEY_ARG[S] we used. */ |
| 669 |
|
| 670 |
/* Try to parse the argument in each parser. */ |
| 671 |
for (group = parser->groups |
| 672 |
; group < parser->egroup && err == EBADKEY |
| 673 |
; group++) |
| 674 |
{ |
| 675 |
parser->state.next++; /* For ARGP_KEY_ARG, consume the arg. */ |
| 676 |
key = ARGP_KEY_ARG; |
| 677 |
err = group_parse (group, &parser->state, key, val); |
| 678 |
|
| 679 |
if (err == EBADKEY) |
| 680 |
/* This parser doesn't like ARGP_KEY_ARG; try ARGP_KEY_ARGS instead. */ |
| 681 |
{ |
| 682 |
parser->state.next--; /* For ARGP_KEY_ARGS, put back the arg. */ |
| 683 |
key = ARGP_KEY_ARGS; |
| 684 |
err = group_parse (group, &parser->state, key, NULL); |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
if (! err) |
| 689 |
{ |
| 690 |
if (key == ARGP_KEY_ARGS) |
| 691 |
/* The default for ARGP_KEY_ARGS is to assume that if NEXT isn't |
| 692 |
changed by the user, *all* arguments should be considered |
| 693 |
consumed. */ |
| 694 |
parser->state.next = parser->state.argc; |
| 695 |
|
| 696 |
if (parser->state.next > index) |
| 697 |
/* Remember that we successfully processed a non-option |
| 698 |
argument -- but only if the user hasn't gotten tricky and set |
| 699 |
the clock back. */ |
| 700 |
(--group)->args_processed += (parser->state.next - index); |
| 701 |
else |
| 702 |
/* The user wants to reparse some args, give getopt another try. */ |
| 703 |
parser->try_getopt = 1; |
| 704 |
} |
| 705 |
|
| 706 |
return err; |
| 707 |
} |
| 708 |
|
| 709 |
/* Call the user parsers to parse the option OPT, with argument VAL, at the |
| 710 |
current position, returning any error. */ |
| 711 |
static error_t |
| 712 |
parser_parse_opt (struct parser *parser, int opt, char *val) |
| 713 |
{ |
| 714 |
/* The group key encoded in the high bits; 0 for short opts or |
| 715 |
group_number + 1 for long opts. */ |
| 716 |
int group_key = opt >> USER_BITS; |
| 717 |
error_t err = EBADKEY; |
| 718 |
|
| 719 |
if (group_key == 0) |
| 720 |
/* A short option. By comparing OPT's position in SHORT_OPTS to the |
| 721 |
various starting positions in each group's SHORT_END field, we can |
| 722 |
determine which group OPT came from. */ |
| 723 |
{ |
| 724 |
struct group *group; |
| 725 |
char *short_index = strchr (parser->short_opts, opt); |
| 726 |
|
| 727 |
if (short_index) |
| 728 |
for (group = parser->groups; group < parser->egroup; group++) |
| 729 |
if (group->short_end > short_index) |
| 730 |
{ |
| 731 |
err = group_parse (group, &parser->state, opt, |
| 732 |
parser->opt_data.optarg); |
| 733 |
break; |
| 734 |
} |
| 735 |
} |
| 736 |
else |
| 737 |
/* A long option. Preserve the sign in the user key, without |
| 738 |
invoking undefined behavior. Assume two's complement. */ |
| 739 |
{ |
| 740 |
int user_key = |
| 741 |
((opt & (1 << (USER_BITS - 1))) ? ~USER_MASK : 0) | (opt & USER_MASK); |
| 742 |
err = |
| 743 |
group_parse (&parser->groups[group_key - 1], &parser->state, |
| 744 |
user_key, parser->opt_data.optarg); |
| 745 |
} |
| 746 |
|
| 747 |
if (err == EBADKEY) |
| 748 |
/* At least currently, an option not recognized is an error in the |
| 749 |
parser, because we pre-compute which parser is supposed to deal |
| 750 |
with each option. */ |
| 751 |
{ |
| 752 |
static const char bad_key_err[] = |
| 753 |
N_("(PROGRAM ERROR) Option should have been recognized!?"); |
| 754 |
if (group_key == 0) |
| 755 |
__argp_error (&parser->state, "-%c: %s", opt, |
| 756 |
dgettext (parser->argp->argp_domain, bad_key_err)); |
| 757 |
else |
| 758 |
{ |
| 759 |
struct option *long_opt = parser->long_opts; |
| 760 |
while (long_opt->val != opt && long_opt->name) |
| 761 |
long_opt++; |
| 762 |
__argp_error (&parser->state, "--%s: %s", |
| 763 |
long_opt->name ? long_opt->name : "???", |
| 764 |
dgettext (parser->argp->argp_domain, bad_key_err)); |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
return err; |
| 769 |
} |
| 770 |
|
| 771 |
/* Parse the next argument in PARSER (as indicated by PARSER->state.next). |
| 772 |
Any error from the parsers is returned, and *ARGP_EBADKEY indicates |
| 773 |
whether a value of EBADKEY is due to an unrecognized argument (which is |
| 774 |
generally not fatal). */ |
| 775 |
static error_t |
| 776 |
parser_parse_next (struct parser *parser, int *arg_ebadkey) |
| 777 |
{ |
| 778 |
if (parser->state.quoted && parser->state.next < parser->state.quoted) |
| 779 |
/* The next argument pointer has been moved to before the quoted |
| 780 |
region, so pretend we never saw the quoting "--", and give getopt |
| 781 |
another chance. If the user hasn't removed it, getopt will just |
| 782 |
process it again. */ |
| 783 |
parser->state.quoted = 0; |
| 784 |
|
| 785 |
int opt; |
| 786 |
if (parser->try_getopt && !parser->state.quoted) |
| 787 |
/* Give getopt a chance to parse this. */ |
| 788 |
{ |
| 789 |
/* Put it back in OPTIND for getopt. */ |
| 790 |
parser->opt_data.optind = parser->state.next; |
| 791 |
/* Distinguish KEY_ERR from a real option. */ |
| 792 |
parser->opt_data.optopt = KEY_END; |
| 793 |
if (parser->state.flags & ARGP_LONG_ONLY) |
| 794 |
opt = _getopt_long_only_r (parser->state.argc, parser->state.argv, |
| 795 |
parser->short_opts, parser->long_opts, NULL, |
| 796 |
&parser->opt_data); |
| 797 |
else |
| 798 |
opt = _getopt_long_r (parser->state.argc, parser->state.argv, |
| 799 |
parser->short_opts, parser->long_opts, NULL, |
| 800 |
&parser->opt_data); |
| 801 |
/* And see what getopt did. */ |
| 802 |
parser->state.next = parser->opt_data.optind; |
| 803 |
|
| 804 |
if (opt == KEY_END) |
| 805 |
/* Getopt says there are no more options, so stop using |
| 806 |
getopt; we'll continue if necessary on our own. */ |
| 807 |
{ |
| 808 |
parser->try_getopt = 0; |
| 809 |
if (parser->state.next > 1 |
| 810 |
&& streq (parser->state.argv[parser->state.next - 1], QUOTE)) |
| 811 |
/* Not only is this the end of the options, but it's a |
| 812 |
"quoted" region, which may have args that *look* like |
| 813 |
options, so we definitely shouldn't try to use getopt past |
| 814 |
here, whatever happens. */ |
| 815 |
parser->state.quoted = parser->state.next; |
| 816 |
} |
| 817 |
else if (opt == KEY_ERR && parser->opt_data.optopt != KEY_END) |
| 818 |
/* KEY_ERR can have the same value as a valid user short |
| 819 |
option, but in the case of a real error, getopt sets OPTOPT |
| 820 |
to the offending character, which can never be KEY_END. */ |
| 821 |
{ |
| 822 |
*arg_ebadkey = 0; |
| 823 |
return EBADKEY; |
| 824 |
} |
| 825 |
} |
| 826 |
else |
| 827 |
opt = KEY_END; |
| 828 |
|
| 829 |
if (opt == KEY_END) |
| 830 |
{ |
| 831 |
/* We're past what getopt considers the options. */ |
| 832 |
if (parser->state.next >= parser->state.argc |
| 833 |
|| (parser->state.flags & ARGP_NO_ARGS)) |
| 834 |
/* Indicate that we're done. */ |
| 835 |
{ |
| 836 |
*arg_ebadkey = 1; |
| 837 |
return EBADKEY; |
| 838 |
} |
| 839 |
else |
| 840 |
/* A non-option arg; simulate what getopt might have done. */ |
| 841 |
{ |
| 842 |
opt = KEY_ARG; |
| 843 |
parser->opt_data.optarg = parser->state.argv[parser->state.next++]; |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
error_t err; |
| 848 |
if (opt == KEY_ARG) |
| 849 |
/* A non-option argument; try each parser in turn. */ |
| 850 |
err = parser_parse_arg (parser, parser->opt_data.optarg); |
| 851 |
else |
| 852 |
err = parser_parse_opt (parser, opt, parser->opt_data.optarg); |
| 853 |
|
| 854 |
if (err == EBADKEY) |
| 855 |
*arg_ebadkey = (opt == KEY_END || opt == KEY_ARG); |
| 856 |
|
| 857 |
return err; |
| 858 |
} |
| 859 |
|
| 860 |
/* Parse the options strings in ARGC & ARGV according to the argp in ARGP. |
| 861 |
FLAGS is one of the ARGP_ flags above. If END_INDEX is non-NULL, the |
| 862 |
index in ARGV of the first unparsed option is returned in it. If an |
| 863 |
unknown option is present, EINVAL is returned; if some parser routine |
| 864 |
returned a non-zero value, it is returned; otherwise 0 is returned. */ |
| 865 |
error_t |
| 866 |
__argp_parse (const struct argp *argp, int argc, char **argv, unsigned flags, |
| 867 |
int *end_index, void *input) |
| 868 |
{ |
| 869 |
#ifndef _LIBC |
| 870 |
if (!(flags & ARGP_PARSE_ARGV0)) |
| 871 |
{ |
| 872 |
#if HAVE_DECL_PROGRAM_INVOCATION_NAME |
| 873 |
if (!program_invocation_name) |
| 874 |
program_invocation_name = argv[0]; |
| 875 |
#endif |
| 876 |
#if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME |
| 877 |
if (!program_invocation_short_name) |
| 878 |
program_invocation_short_name = __argp_base_name (argv[0]); |
| 879 |
#endif |
| 880 |
} |
| 881 |
#endif |
| 882 |
|
| 883 |
if (! (flags & ARGP_NO_HELP)) |
| 884 |
/* Add our own options. */ |
| 885 |
{ |
| 886 |
struct argp_child *child = alloca (4 * sizeof (struct argp_child)); |
| 887 |
struct argp *top_argp = alloca (sizeof (struct argp)); |
| 888 |
|
| 889 |
/* TOP_ARGP has no options, it just serves to group the user & default |
| 890 |
argps. */ |
| 891 |
memset (top_argp, 0, sizeof (*top_argp)); |
| 892 |
top_argp->children = child; |
| 893 |
|
| 894 |
memset (child, 0, 4 * sizeof (struct argp_child)); |
| 895 |
|
| 896 |
if (argp) |
| 897 |
(child++)->argp = argp; |
| 898 |
(child++)->argp = &argp_default_argp; |
| 899 |
if (argp_program_version || argp_program_version_hook) |
| 900 |
(child++)->argp = &argp_version_argp; |
| 901 |
child->argp = NULL; |
| 902 |
|
| 903 |
argp = top_argp; |
| 904 |
} |
| 905 |
|
| 906 |
/* Construct a parser for these arguments. */ |
| 907 |
struct parser parser; |
| 908 |
error_t err = parser_init (&parser, argp, argc, argv, flags, input); |
| 909 |
|
| 910 |
if (! err) |
| 911 |
/* Parse! */ |
| 912 |
{ |
| 913 |
/* If true, then err == EBADKEY is a result of a non-option argument |
| 914 |
failing to be parsed (which in some cases isn't actually an error). */ |
| 915 |
int arg_ebadkey = 0; |
| 916 |
|
| 917 |
while (! err) |
| 918 |
err = parser_parse_next (&parser, &arg_ebadkey); |
| 919 |
err = parser_finalize (&parser, err, arg_ebadkey, end_index); |
| 920 |
} |
| 921 |
|
| 922 |
return err; |
| 923 |
} |
| 924 |
#ifdef weak_alias |
| 925 |
weak_alias (__argp_parse, argp_parse) |
| 926 |
#endif |
| 927 |
|
| 928 |
/* Return the input field for ARGP in the parser corresponding to STATE; used |
| 929 |
by the help routines. */ |
| 930 |
void * |
| 931 |
__argp_input (const struct argp *argp, const struct argp_state *state) |
| 932 |
{ |
| 933 |
if (state) |
| 934 |
{ |
| 935 |
struct parser *parser = state->pstate; |
| 936 |
|
| 937 |
for (struct group *group = parser->groups; |
| 938 |
group < parser->egroup; |
| 939 |
group++) |
| 940 |
if (group->argp == argp) |
| 941 |
return group->input; |
| 942 |
} |
| 943 |
|
| 944 |
return NULL; |
| 945 |
} |
| 946 |
#ifdef weak_alias |
| 947 |
weak_alias (__argp_input, _argp_input) |
| 948 |
#endif |