123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937 |
- #ifdef HAVE_CONFIG_H
- #include <config.h>
- #endif
- #ifndef __GNUC__
- # if HAVE_ALLOCA_H || defined _LIBC
- # include <alloca.h>
- # else
- # ifdef _AIX
- #pragma alloca
- # else
- # ifndef alloca
- char *alloca ();
- # endif
- # endif
- # endif
- #endif
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <limits.h>
- #include <getopt.h>
- #include <bits/getopt_int.h>
- #include <features.h>
- #ifndef _
- # define dgettext(domain, msgid) (msgid)
- # define gettext(msgid) (msgid)
- #endif
- #ifndef N_
- # define N_(msgid) (msgid)
- #endif
- #include <argp.h>
- #define KEY_END (-1)
- #define KEY_ARG 1
- #define KEY_ERR '?'
- #define QUOTE "--"
- #define GROUP_BITS CHAR_BIT
- #define USER_BITS ((sizeof ((struct option *)0)->val * CHAR_BIT) - GROUP_BITS)
- #define USER_MASK ((1 << USER_BITS) - 1)
- #define EBADKEY ARGP_ERR_UNKNOWN
- static volatile int _argp_hang;
- #define OPT_PROGNAME -2
- #define OPT_USAGE -3
- #define OPT_HANG -4
- static const struct argp_option argp_default_options[] =
- {
- {"help", '?', 0, 0, N_("Give this help list"), -1},
- {"usage", OPT_USAGE, 0, 0, N_("Give a short usage message")},
- {"program-name",OPT_PROGNAME,"NAME", OPTION_HIDDEN, N_("Set the program name")},
- {"HANG", OPT_HANG, "SECS", OPTION_ARG_OPTIONAL | OPTION_HIDDEN,
- N_("Hang for SECS seconds (default 3600)")},
- {0, 0}
- };
- static error_t
- argp_default_parser (int key, char *arg, struct argp_state *state)
- {
- switch (key)
- {
- case '?':
- argp_state_help (state, state->out_stream, ARGP_HELP_STD_HELP);
- break;
- case OPT_USAGE:
- argp_state_help (state, state->out_stream,
- ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
- break;
- case OPT_PROGNAME:
- #if defined _LIBC && defined(__UCLIBC_HAS_PROGRAM_INVOCATION_NAME__)
- program_invocation_name = arg;
- #endif
-
-
- state->name = strrchr (arg, '/');
- if (state->name)
- state->name++;
- else
- state->name = arg;
- #if defined _LIBC && defined(__UCLIBC_HAS_PROGRAM_INVOCATION_NAME__)
- program_invocation_short_name = state->name;
- #endif
- if ((state->flags & (ARGP_PARSE_ARGV0 | ARGP_NO_ERRS))
- == ARGP_PARSE_ARGV0)
-
- state->argv[0] = arg;
- break;
- case OPT_HANG:
- _argp_hang = atoi (arg ? arg : "3600");
- while (_argp_hang-- > 0)
- sleep (1);
- break;
- default:
- return EBADKEY;
- }
- return 0;
- }
- static const struct argp argp_default_argp =
- {argp_default_options, &argp_default_parser, NULL, NULL, NULL, NULL, "libc"};
- static const struct argp_option argp_version_options[] =
- {
- {"version", 'V', 0, 0, N_("Print program version"), -1},
- {0, 0}
- };
- static error_t
- argp_version_parser (int key, char *arg, struct argp_state *state)
- {
- switch (key)
- {
- case 'V':
- if (argp_program_version_hook)
- (*argp_program_version_hook) (state->out_stream, state);
- else if (argp_program_version)
- fprintf (state->out_stream, "%s\n", argp_program_version);
- else
- argp_error (state, dgettext (state->root_argp->argp_domain,
- "(PROGRAM ERROR) No version known!?"));
- if (! (state->flags & ARGP_NO_EXIT))
- exit (0);
- break;
- default:
- return EBADKEY;
- }
- return 0;
- }
- static const struct argp argp_version_argp =
- {argp_version_options, &argp_version_parser, NULL, NULL, NULL, NULL, "libc"};
- static int
- find_long_option (struct option *long_options, const char *name)
- {
- struct option *l = long_options;
- while (l->name != NULL)
- if (name != NULL && strcmp (l->name, name) == 0)
- return l - long_options;
- else
- l++;
- if (name == NULL)
- return l - long_options;
- else
- return -1;
- }
- struct group
- {
-
- argp_parser_t parser;
-
- const struct argp *argp;
-
- char *short_end;
-
- unsigned args_processed;
-
- struct group *parent;
- unsigned parent_index;
-
- void *input, **child_inputs;
- void *hook;
- };
- static error_t
- group_parse (struct group *group, struct argp_state *state, int key, char *arg)
- {
- if (group->parser)
- {
- error_t err;
- state->hook = group->hook;
- state->input = group->input;
- state->child_inputs = group->child_inputs;
- state->arg_num = group->args_processed;
- err = (*group->parser)(key, arg, state);
- group->hook = state->hook;
- return err;
- }
- else
- return EBADKEY;
- }
- struct parser
- {
- const struct argp *argp;
-
- char *short_opts;
-
- struct option *long_opts;
-
- struct _getopt_data opt_data;
-
- struct group *groups;
-
- struct group *egroup;
-
- void **child_inputs;
-
- int try_getopt;
-
- struct argp_state state;
-
- void *storage;
- };
- struct parser_convert_state
- {
- struct parser *parser;
- char *short_end;
- struct option *long_end;
- void **child_inputs_end;
- };
- static struct group *
- convert_options (const struct argp *argp,
- struct group *parent, unsigned parent_index,
- struct group *group, struct parser_convert_state *cvt)
- {
-
- const struct argp_option *real = argp->options;
- const struct argp_child *children = argp->children;
- if (real || argp->parser)
- {
- const struct argp_option *opt;
- if (real)
- for (opt = real; !__option_is_end (opt); opt++)
- {
- if (! (opt->flags & OPTION_ALIAS))
-
- real = opt;
- if (! (real->flags & OPTION_DOC))
-
- {
- if (__option_is_short (opt))
-
- {
- *cvt->short_end++ = opt->key;
- if (real->arg)
- {
- *cvt->short_end++ = ':';
- if (real->flags & OPTION_ARG_OPTIONAL)
- *cvt->short_end++ = ':';
- }
- *cvt->short_end = '\0';
- }
- if (opt->name
- && find_long_option (cvt->parser->long_opts, opt->name) < 0)
-
- {
- cvt->long_end->name = opt->name;
- cvt->long_end->has_arg =
- (real->arg
- ? (real->flags & OPTION_ARG_OPTIONAL
- ? optional_argument
- : required_argument)
- : no_argument);
- cvt->long_end->flag = 0;
-
- cvt->long_end->val =
- ((opt->key | real->key) & USER_MASK)
- + (((group - cvt->parser->groups) + 1) << USER_BITS);
-
- (++cvt->long_end)->name = NULL;
- }
- }
- }
- group->parser = argp->parser;
- group->argp = argp;
- group->short_end = cvt->short_end;
- group->args_processed = 0;
- group->parent = parent;
- group->parent_index = parent_index;
- group->input = 0;
- group->hook = 0;
- group->child_inputs = 0;
- if (children)
-
- {
- unsigned num_children = 0;
- while (children[num_children].argp)
- num_children++;
- group->child_inputs = cvt->child_inputs_end;
- cvt->child_inputs_end += num_children;
- }
- parent = group++;
- }
- else
- parent = 0;
- if (children)
- {
- unsigned index = 0;
- while (children->argp)
- group =
- convert_options (children++->argp, parent, index++, group, cvt);
- }
- return group;
- }
- static void
- parser_convert (struct parser *parser, const struct argp *argp, int flags)
- {
- struct parser_convert_state cvt;
- cvt.parser = parser;
- cvt.short_end = parser->short_opts;
- cvt.long_end = parser->long_opts;
- cvt.child_inputs_end = parser->child_inputs;
- if (flags & ARGP_IN_ORDER)
- *cvt.short_end++ = '-';
- else if (flags & ARGP_NO_ARGS)
- *cvt.short_end++ = '+';
- *cvt.short_end = '\0';
- cvt.long_end->name = NULL;
- parser->argp = argp;
- if (argp)
- parser->egroup = convert_options (argp, 0, 0, parser->groups, &cvt);
- else
- parser->egroup = parser->groups;
- }
- struct parser_sizes
- {
- size_t short_len;
- size_t long_len;
- size_t num_groups;
- size_t num_child_inputs;
- };
- static void
- calc_sizes (const struct argp *argp, struct parser_sizes *szs)
- {
- const struct argp_child *child = argp->children;
- const struct argp_option *opt = argp->options;
- if (opt || argp->parser)
- {
- szs->num_groups++;
- if (opt)
- {
- int num_opts = 0;
- while (!__option_is_end (opt++))
- num_opts++;
- szs->short_len += num_opts * 3;
- szs->long_len += num_opts;
- }
- }
- if (child)
- while (child->argp)
- {
- calc_sizes ((child++)->argp, szs);
- szs->num_child_inputs++;
- }
- }
- extern char * __argp_short_program_name (void);
- static error_t
- parser_init (struct parser *parser, const struct argp *argp,
- int argc, char **argv, int flags, void *input)
- {
- error_t err = 0;
- struct group *group;
- struct parser_sizes szs;
- struct _getopt_data opt_data = _GETOPT_DATA_INITIALIZER;
- szs.short_len = (flags & ARGP_NO_ARGS) ? 0 : 1;
- szs.long_len = 0;
- szs.num_groups = 0;
- szs.num_child_inputs = 0;
- if (argp)
- calc_sizes (argp, &szs);
-
- #define GLEN (szs.num_groups + 1) * sizeof (struct group)
- #define CLEN (szs.num_child_inputs * sizeof (void *))
- #define LLEN ((szs.long_len + 1) * sizeof (struct option))
- #define SLEN (szs.short_len + 1)
- parser->storage = malloc (GLEN + CLEN + LLEN + SLEN);
- if (! parser->storage)
- return ENOMEM;
- parser->groups = parser->storage;
- parser->child_inputs = parser->storage + GLEN;
- parser->long_opts = parser->storage + GLEN + CLEN;
- parser->short_opts = parser->storage + GLEN + CLEN + LLEN;
- parser->opt_data = opt_data;
- memset (parser->child_inputs, 0, szs.num_child_inputs * sizeof (void *));
- parser_convert (parser, argp, flags);
- memset (&parser->state, 0, sizeof (struct argp_state));
- parser->state.root_argp = parser->argp;
- parser->state.argc = argc;
- parser->state.argv = argv;
- parser->state.flags = flags;
- parser->state.err_stream = stderr;
- parser->state.out_stream = stdout;
- parser->state.next = 0;
- parser->state.pstate = parser;
- parser->try_getopt = 1;
-
- if (parser->groups < parser->egroup)
- parser->groups->input = input;
- for (group = parser->groups;
- group < parser->egroup && (!err || err == EBADKEY);
- group++)
- {
- if (group->parent)
-
- group->input = group->parent->child_inputs[group->parent_index];
- if (!group->parser
- && group->argp->children && group->argp->children->argp)
-
- group->child_inputs[0] = group->input;
- err = group_parse (group, &parser->state, ARGP_KEY_INIT, 0);
- }
- if (err == EBADKEY)
- err = 0;
- if (err)
- return err;
- if (parser->state.flags & ARGP_NO_ERRS)
- {
- parser->opt_data.opterr = 0;
- if (parser->state.flags & ARGP_PARSE_ARGV0)
-
- parser->state.argv--, parser->state.argc++;
- }
- else
- parser->opt_data.opterr = 1;
- if (parser->state.argv == argv && argv[0])
-
- {
- char *short_name = strrchr (argv[0], '/');
- parser->state.name = short_name ? short_name + 1 : argv[0];
- }
- else
- parser->state.name = __argp_short_program_name ();
- return 0;
- }
- static error_t
- parser_finalize (struct parser *parser,
- error_t err, int arg_ebadkey, int *end_index)
- {
- struct group *group;
- if (err == EBADKEY && arg_ebadkey)
-
- err = 0;
- if (! err)
- {
- if (parser->state.next == parser->state.argc)
-
- {
- for (group = parser->groups;
- group < parser->egroup && (!err || err==EBADKEY);
- group++)
- if (group->args_processed == 0)
- err = group_parse (group, &parser->state, ARGP_KEY_NO_ARGS, 0);
- for (group = parser->egroup - 1;
- group >= parser->groups && (!err || err==EBADKEY);
- group--)
- err = group_parse (group, &parser->state, ARGP_KEY_END, 0);
- if (err == EBADKEY)
- err = 0;
-
- if (end_index)
- *end_index = parser->state.next;
- }
- else if (end_index)
-
- *end_index = parser->state.next;
- else
-
- {
- if (!(parser->state.flags & ARGP_NO_ERRS)
- && parser->state.err_stream)
- fprintf (parser->state.err_stream,
- dgettext (parser->argp->argp_domain,
- "%s: Too many arguments\n"),
- parser->state.name);
- err = EBADKEY;
- }
- }
-
- if (err)
- {
-
- if (err == EBADKEY)
-
- argp_state_help (&parser->state, parser->state.err_stream,
- ARGP_HELP_STD_ERR);
-
- for (group = parser->groups; group < parser->egroup; group++)
- group_parse (group, &parser->state, ARGP_KEY_ERROR, 0);
- }
- else
-
- {
-
- for (group = parser->egroup - 1
- ; group >= parser->groups && (!err || err == EBADKEY)
- ; group--)
- err = group_parse (group, &parser->state, ARGP_KEY_SUCCESS, 0);
- if (err == EBADKEY)
- err = 0;
- }
-
- for (group = parser->egroup - 1; group >= parser->groups; group--)
- group_parse (group, &parser->state, ARGP_KEY_FINI, 0);
- if (err == EBADKEY)
- err = EINVAL;
- free (parser->storage);
- return err;
- }
- static error_t
- parser_parse_arg (struct parser *parser, char *val)
- {
-
- int index = --parser->state.next;
- error_t err = EBADKEY;
- struct group *group;
- int key = 0;
-
- for (group = parser->groups
- ; group < parser->egroup && err == EBADKEY
- ; group++)
- {
- parser->state.next++;
- key = ARGP_KEY_ARG;
- err = group_parse (group, &parser->state, key, val);
- if (err == EBADKEY)
-
- {
- parser->state.next--;
- key = ARGP_KEY_ARGS;
- err = group_parse (group, &parser->state, key, 0);
- }
- }
- if (! err)
- {
- if (key == ARGP_KEY_ARGS)
-
- parser->state.next = parser->state.argc;
- if (parser->state.next > index)
-
- (--group)->args_processed += (parser->state.next - index);
- else
-
- parser->try_getopt = 1;
- }
- return err;
- }
- static error_t
- parser_parse_opt (struct parser *parser, int opt, char *val)
- {
-
- int group_key = opt >> USER_BITS;
- error_t err = EBADKEY;
- if (group_key == 0)
-
- {
- struct group *group;
- char *short_index = strchr (parser->short_opts, opt);
- if (short_index)
- for (group = parser->groups; group < parser->egroup; group++)
- if (group->short_end > short_index)
- {
- err = group_parse (group, &parser->state, opt,
- parser->opt_data.optarg);
- break;
- }
- }
- else
-
- err =
- group_parse (&parser->groups[group_key - 1], &parser->state,
- (opt << GROUP_BITS) >> GROUP_BITS,
- parser->opt_data.optarg);
- if (err == EBADKEY)
-
- {
- static const char bad_key_err[] =
- N_("(PROGRAM ERROR) Option should have been recognized!?");
- if (group_key == 0)
- argp_error (&parser->state, "-%c: %s", opt,
- dgettext (parser->argp->argp_domain, bad_key_err));
- else
- {
- struct option *long_opt = parser->long_opts;
- while (long_opt->val != opt && long_opt->name)
- long_opt++;
- argp_error (&parser->state, "--%s: %s",
- long_opt->name ? long_opt->name : "???",
- dgettext (parser->argp->argp_domain, bad_key_err));
- }
- }
- return err;
- }
- static error_t
- parser_parse_next (struct parser *parser, int *arg_ebadkey)
- {
- int opt;
- error_t err = 0;
- if (parser->state.quoted && parser->state.next < parser->state.quoted)
-
- parser->state.quoted = 0;
- if (parser->try_getopt && !parser->state.quoted)
-
- {
-
- parser->opt_data.optind = parser->state.next;
-
- parser->opt_data.optopt = KEY_END;
- if (parser->state.flags & ARGP_LONG_ONLY)
- opt = _getopt_long_only_r (parser->state.argc, parser->state.argv,
- parser->short_opts, parser->long_opts, 0,
- &parser->opt_data);
- else
- opt = _getopt_long_r (parser->state.argc, parser->state.argv,
- parser->short_opts, parser->long_opts, 0,
- &parser->opt_data);
-
- parser->state.next = parser->opt_data.optind;
- if (opt == KEY_END)
-
- {
- parser->try_getopt = 0;
- if (parser->state.next > 1
- && strcmp (parser->state.argv[parser->state.next - 1], QUOTE)
- == 0)
-
- parser->state.quoted = parser->state.next;
- }
- else if (opt == KEY_ERR && parser->opt_data.optopt != KEY_END)
-
- {
- *arg_ebadkey = 0;
- return EBADKEY;
- }
- }
- else
- opt = KEY_END;
- if (opt == KEY_END)
- {
-
- if (parser->state.next >= parser->state.argc
- || (parser->state.flags & ARGP_NO_ARGS))
-
- {
- *arg_ebadkey = 1;
- return EBADKEY;
- }
- else
-
- {
- opt = KEY_ARG;
- parser->opt_data.optarg = parser->state.argv[parser->state.next++];
- }
- }
- if (opt == KEY_ARG)
-
- err = parser_parse_arg (parser, parser->opt_data.optarg);
- else
- err = parser_parse_opt (parser, opt, parser->opt_data.optarg);
- if (err == EBADKEY)
- *arg_ebadkey = (opt == KEY_END || opt == KEY_ARG);
- return err;
- }
- error_t
- argp_parse (const struct argp *argp, int argc, char **argv, unsigned flags,
- int *end_index, void *input)
- {
- error_t err;
- struct parser parser;
-
- int arg_ebadkey = 0;
- if (! (flags & ARGP_NO_HELP))
-
- {
- struct argp_child *child = alloca (4 * sizeof (struct argp_child));
- struct argp *top_argp = alloca (sizeof (struct argp));
-
- memset (top_argp, 0, sizeof (*top_argp));
- top_argp->children = child;
- memset (child, 0, 4 * sizeof (struct argp_child));
- if (argp)
- (child++)->argp = argp;
- (child++)->argp = &argp_default_argp;
- if (argp_program_version || argp_program_version_hook)
- (child++)->argp = &argp_version_argp;
- child->argp = 0;
- argp = top_argp;
- }
-
- err = parser_init (&parser, argp, argc, argv, flags, input);
- if (! err)
-
- {
- while (! err)
- err = parser_parse_next (&parser, &arg_ebadkey);
- err = parser_finalize (&parser, err, arg_ebadkey, end_index);
- }
- return err;
- }
- void *
- __argp_input (const struct argp *argp, const struct argp_state *state)
- {
- if (state)
- {
- struct group *group;
- struct parser *parser = state->pstate;
- for (group = parser->groups; group < parser->egroup; group++)
- if (group->argp == argp)
- return group->input;
- }
- return 0;
- }
|