123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619 |
- #if ENABLE_LONG_OPTS
- # include <getopt.h>
- #endif
- #include "libbb.h"
- const char *const bb_argv_dash[] = { "-", NULL };
- enum {
- PARAM_STRING,
- PARAM_LIST,
- PARAM_INT,
- };
- typedef struct {
- unsigned char opt_char;
- smallint param_type;
- unsigned switch_on;
- unsigned switch_off;
- unsigned incongruously;
- unsigned requires;
- void **optarg;
- int *counter;
- } t_complementary;
- uint32_t option_mask32;
- #if ENABLE_LONG_OPTS
- static const struct option bb_null_long_options[1] = {
- { 0, 0, 0, 0 }
- };
- #else
- #define vgetopt32(argv,applet_opts,applet_long_options,p) \
- vgetopt32(argv,applet_opts,p)
- #endif
- static uint32_t
- vgetopt32(char **argv, const char *applet_opts, const char *applet_long_options, va_list p)
- {
- int argc;
- unsigned flags = 0;
- unsigned requires = 0;
- unsigned len;
- t_complementary complementary[33];
- char dont_die_flag;
- int c;
- const unsigned char *s;
- const char *opt_complementary;
- t_complementary *on_off;
- #if ENABLE_LONG_OPTS
- const struct option *l_o;
- struct option *long_options = (struct option *) &bb_null_long_options;
- #endif
- unsigned trigger;
- int min_arg = 0;
- int max_arg = -1;
- int spec_flgs = 0;
- #define SHOW_USAGE_IF_ERROR 1
- on_off = complementary;
- memset(on_off, 0, sizeof(complementary));
- len = strlen(applet_opts);
-
- opt_complementary = NULL;
- if (applet_opts[0] == '^') {
- applet_opts++;
-
- opt_complementary = applet_opts + len;
- }
-
- dont_die_flag = applet_opts[0];
- if (dont_die_flag == '!')
- applet_opts++;
- applet_opts = strcpy(alloca(len + 1), applet_opts);
-
- s = (const unsigned char *)applet_opts;
- if (*s == '+' || *s == '-')
- s++;
- c = 0;
- while (*s) {
- if (c >= 32)
- break;
- on_off->opt_char = *s;
- on_off->switch_on = (1U << c);
- if (*++s == ':') {
- on_off->optarg = va_arg(p, void **);
- if (s[1] == '+' || s[1] == '*') {
-
- on_off->param_type = (s[1] == '+') ?
- PARAM_INT : PARAM_LIST;
- overlapping_strcpy((char*)s + 1, (char*)s + 2);
- }
-
- while (*++s == ':')
- continue;
- }
- on_off++;
- c++;
- }
- #if ENABLE_LONG_OPTS
- if (applet_long_options) {
- const char *optstr;
- unsigned i, count;
- count = 1;
- optstr = applet_long_options;
- while (optstr[0]) {
- optstr += strlen(optstr) + 3;
- count++;
- }
-
- long_options = alloca(count * sizeof(*long_options));
- memset(long_options, 0, count * sizeof(*long_options));
- i = 0;
- optstr = applet_long_options;
- while (--count) {
- long_options[i].name = optstr;
- optstr += strlen(optstr) + 1;
- long_options[i].has_arg = (unsigned char)(*optstr++);
-
- long_options[i].val = (unsigned char)(*optstr++);
- i++;
- }
- for (l_o = long_options; l_o->name; l_o++) {
- if (l_o->flag)
- continue;
- for (on_off = complementary; on_off->opt_char; on_off++)
- if (on_off->opt_char == l_o->val)
- goto next_long;
- if (c >= 32)
- break;
- on_off->opt_char = l_o->val;
- on_off->switch_on = (1U << c);
- if (l_o->has_arg != no_argument)
- on_off->optarg = va_arg(p, void **);
- c++;
- next_long: ;
- }
- }
- #endif
- s = (const unsigned char *)opt_complementary;
- if (s) for (; *s; s++) {
- t_complementary *pair;
- unsigned *pair_switch;
- if (*s == ':')
- continue;
- c = s[1];
- if (*s == '?') {
- if (c < '0' || c > '9') {
- spec_flgs |= SHOW_USAGE_IF_ERROR;
- } else {
- max_arg = c - '0';
- s++;
- }
- continue;
- }
- if (*s == '-') {
- if (c >= '0' && c <= '9') {
- min_arg = c - '0';
- s++;
- }
- continue;
- }
- if (*s == '=') {
- min_arg = max_arg = c - '0';
- s++;
- continue;
- }
- for (on_off = complementary; on_off->opt_char; on_off++)
- if (on_off->opt_char == *s)
- goto found_opt;
-
- bb_error_msg_and_die("NO OPT %c!", *s);
- found_opt:
- if (c == ':' && s[2] == ':') {
- on_off->param_type = PARAM_LIST;
- continue;
- }
- if (c == '+' && (s[2] == ':' || s[2] == '\0')) {
- on_off->param_type = PARAM_INT;
- s++;
- continue;
- }
- if (c == ':' || c == '\0') {
- requires |= on_off->switch_on;
- continue;
- }
- if (c == '-' && (s[2] == ':' || s[2] == '\0')) {
- flags |= on_off->switch_on;
- on_off->incongruously |= on_off->switch_on;
- s++;
- continue;
- }
- if (c == *s) {
- on_off->counter = va_arg(p, int *);
- s++;
- }
- pair = on_off;
- pair_switch = &pair->switch_on;
- for (s++; *s && *s != ':'; s++) {
- if (*s == '?') {
- pair_switch = &pair->requires;
- } else if (*s == '-') {
- if (pair_switch == &pair->switch_off)
- pair_switch = &pair->incongruously;
- else
- pair_switch = &pair->switch_off;
- } else {
- for (on_off = complementary; on_off->opt_char; on_off++)
- if (on_off->opt_char == *s) {
- *pair_switch |= on_off->switch_on;
- break;
- }
- }
- }
- s--;
- }
-
- GETOPT_RESET();
-
- argc = 1 + string_array_len(argv + 1);
-
- #if ENABLE_LONG_OPTS
- while ((c = getopt_long(argc, argv, applet_opts,
- long_options, NULL)) != -1) {
- #else
- while ((c = getopt(argc, argv, applet_opts)) != -1) {
- #endif
-
- c &= 0xff;
- for (on_off = complementary; on_off->opt_char != c; on_off++) {
-
- if (on_off->opt_char == '\0' ) {
-
- goto error;
- }
- }
- if (flags & on_off->incongruously)
- goto error;
- trigger = on_off->switch_on & on_off->switch_off;
- flags &= ~(on_off->switch_off ^ trigger);
- flags |= on_off->switch_on ^ trigger;
- flags ^= trigger;
- if (on_off->counter)
- (*(on_off->counter))++;
- if (optarg) {
- if (on_off->param_type == PARAM_LIST) {
- llist_add_to_end((llist_t **)(on_off->optarg), optarg);
- } else if (on_off->param_type == PARAM_INT) {
- *(unsigned*)(on_off->optarg) = xatoi_positive(optarg);
- } else if (on_off->optarg) {
- *(char **)(on_off->optarg) = optarg;
- }
- }
- }
-
- for (on_off = complementary; on_off->opt_char; on_off++) {
- if (on_off->requires
- && (flags & on_off->switch_on)
- && (flags & on_off->requires) == 0
- ) {
- goto error;
- }
- }
- if (requires && (flags & requires) == 0)
- goto error;
- argc -= optind;
- if (argc < min_arg || (max_arg >= 0 && argc > max_arg))
- goto error;
- option_mask32 = flags;
- return flags;
- error:
- if (dont_die_flag != '!')
- bb_show_usage();
- return (int32_t)-1;
- }
- uint32_t FAST_FUNC
- getopt32(char **argv, const char *applet_opts, ...)
- {
- uint32_t opt;
- va_list p;
- va_start(p, applet_opts);
- opt = vgetopt32(argv, applet_opts, NULL, p);
- va_end(p);
- return opt;
- }
- #if ENABLE_LONG_OPTS
- uint32_t FAST_FUNC
- getopt32long(char **argv, const char *applet_opts, const char *longopts, ...)
- {
- uint32_t opt;
- va_list p;
- va_start(p, longopts);
- opt = vgetopt32(argv, applet_opts, longopts, p);
- va_end(p);
- return opt;
- }
- #endif
|