123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424 |
- #if ENABLE_FEATURE_GETOPT_LONG
- # include <getopt.h>
- #endif
- #include "libbb.h"
- enum {
- NON_OPT = 1,
- #if ENABLE_FEATURE_GETOPT_LONG
- LONG_OPT = 2
- #endif
- };
- enum {
- OPT_o = 0x1,
- OPT_n = 0x2,
- OPT_q = 0x4,
- OPT_Q = 0x8,
- OPT_s = 0x10,
- OPT_T = 0x20,
- OPT_u = 0x40,
- #if ENABLE_FEATURE_GETOPT_LONG
- OPT_a = 0x80,
- OPT_l = 0x100,
- #endif
- SHELL_IS_TCSH = 0x8000,
- };
- #define alternative (option_mask32 & OPT_a)
- #define quiet_errors (option_mask32 & OPT_q)
- #define quiet_output (option_mask32 & OPT_Q)
- #define quote (!(option_mask32 & OPT_u))
- #define shell_TCSH (option_mask32 & SHELL_IS_TCSH)
- static const char *normalize(const char *arg)
- {
- char *bufptr;
- #if ENABLE_FEATURE_CLEAN_UP
- static char *BUFFER = NULL;
- free(BUFFER);
- #else
- char *BUFFER;
- #endif
- if (!quote) {
- BUFFER = xstrdup(arg);
- return BUFFER;
- }
-
- BUFFER = xmalloc(strlen(arg)*4 + 3);
- bufptr = BUFFER;
- *bufptr ++= '\'';
- while (*arg) {
- if (*arg == '\'') {
-
- *bufptr ++= '\'';
- *bufptr ++= '\\';
- *bufptr ++= '\'';
- *bufptr ++= '\'';
- } else if (shell_TCSH && *arg == '!') {
-
- *bufptr ++= '\'';
- *bufptr ++= '\\';
- *bufptr ++= '!';
- *bufptr ++= '\'';
- } else if (shell_TCSH && *arg == '\n') {
-
- *bufptr ++= '\\';
- *bufptr ++= 'n';
- } else if (shell_TCSH && isspace(*arg)) {
-
- *bufptr ++= '\'';
- *bufptr ++= '\\';
- *bufptr ++= *arg;
- *bufptr ++= '\'';
- } else
-
- *bufptr ++= *arg;
- arg++;
- }
- *bufptr ++= '\'';
- *bufptr ++= '\0';
- return BUFFER;
- }
- #if !ENABLE_FEATURE_GETOPT_LONG
- #define generate_output(argv,argc,optstr,longopts) \
- generate_output(argv,argc,optstr)
- #endif
- static int generate_output(char **argv, int argc, const char *optstr, const struct option *longopts)
- {
- int exit_code = 0;
- if (quiet_errors)
- opterr = 0;
-
- GETOPT_RESET();
- while (1) {
- #if ENABLE_FEATURE_GETOPT_LONG
- int longindex;
- int opt = alternative
- ? getopt_long_only(argc, argv, optstr, longopts, &longindex)
- : getopt_long(argc, argv, optstr, longopts, &longindex)
- ;
- #else
- int opt = getopt(argc, argv, optstr);
- #endif
- if (opt == -1)
- break;
- if (opt == '?' || opt == ':' )
- exit_code = 1;
- else if (!quiet_output) {
- #if ENABLE_FEATURE_GETOPT_LONG
- if (opt == LONG_OPT) {
- printf(" --%s", longopts[longindex].name);
- if (longopts[longindex].has_arg)
- printf(" %s",
- normalize(optarg ? optarg : ""));
- } else
- #endif
- if (opt == NON_OPT)
- printf(" %s", normalize(optarg));
- else {
- const char *charptr;
- printf(" -%c", opt);
- charptr = strchr(optstr, opt);
- if (charptr && *++charptr == ':')
- printf(" %s",
- normalize(optarg ? optarg : ""));
- }
- }
- }
- if (!quiet_output) {
- unsigned idx;
- printf(" --");
- idx = optind;
- while (argv[idx])
- printf(" %s", normalize(argv[idx++]));
- bb_putchar('\n');
- }
- return exit_code;
- }
- #if ENABLE_FEATURE_GETOPT_LONG
- static struct option *add_long_options(struct option *long_options, char *options)
- {
- int long_nr = 0;
- int arg_opt, tlen;
- char *tokptr = strtok(options, ", \t\n");
- if (long_options)
- while (long_options[long_nr].name)
- long_nr++;
- while (tokptr) {
- arg_opt = no_argument;
- tlen = strlen(tokptr);
- if (tlen) {
- tlen--;
- if (tokptr[tlen] == ':') {
- arg_opt = required_argument;
- if (tlen && tokptr[tlen-1] == ':') {
- tlen--;
- arg_opt = optional_argument;
- }
- tokptr[tlen] = '\0';
- if (tlen == 0)
- bb_error_msg_and_die("empty long option specified");
- }
- long_options = xrealloc_vector(long_options, 4, long_nr);
- long_options[long_nr].has_arg = arg_opt;
-
- long_options[long_nr].val = LONG_OPT;
- long_options[long_nr].name = xstrdup(tokptr);
- long_nr++;
-
- }
- tokptr = strtok(NULL, ", \t\n");
- }
- return long_options;
- }
- #endif
- static void set_shell(const char *new_shell)
- {
- if (strcmp(new_shell, "bash") == 0 || strcmp(new_shell, "sh") == 0)
- return;
- if (strcmp(new_shell, "tcsh") == 0 || strcmp(new_shell, "csh") == 0)
- option_mask32 |= SHELL_IS_TCSH;
- else
- bb_error_msg("unknown shell '%s', assuming bash", new_shell);
- }
- #if ENABLE_FEATURE_GETOPT_LONG
- static const char getopt_longopts[] ALIGN1 =
- "options\0" Required_argument "o"
- "longoptions\0" Required_argument "l"
- "quiet\0" No_argument "q"
- "quiet-output\0" No_argument "Q"
- "shell\0" Required_argument "s"
- "test\0" No_argument "T"
- "unquoted\0" No_argument "u"
- "alternative\0" No_argument "a"
- "name\0" Required_argument "n"
- ;
- #endif
- int getopt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int getopt_main(int argc, char **argv)
- {
- int n;
- char *optstr = NULL;
- char *name = NULL;
- unsigned opt;
- const char *compatible;
- char *s_arg;
- #if ENABLE_FEATURE_GETOPT_LONG
- struct option *long_options = NULL;
- llist_t *l_arg = NULL;
- #endif
- compatible = getenv("GETOPT_COMPATIBLE");
- if (!argv[1]) {
- if (compatible) {
-
- puts(" --");
- return 0;
- }
- bb_error_msg_and_die("missing optstring argument");
- }
- if (argv[1][0] != '-' || compatible) {
- char *s = argv[1];
- option_mask32 |= OPT_u;
- s = xstrdup(s + strspn(s, "-+"));
- argv[1] = argv[0];
- return generate_output(argv+1, argc-1, s, long_options);
- }
- #if !ENABLE_FEATURE_GETOPT_LONG
- opt = getopt32(argv, "+o:n:qQs:Tu", &optstr, &name, &s_arg);
- #else
- opt = getopt32long(argv, "+o:n:qQs:Tual:*", getopt_longopts,
- &optstr, &name, &s_arg, &l_arg);
-
- while (l_arg) {
- long_options = add_long_options(long_options, llist_pop(&l_arg));
- }
- #endif
- if (opt & OPT_s) {
- set_shell(s_arg);
- }
- if (opt & OPT_T) {
- return 4;
- }
-
- n = optind - 1;
- if (!optstr) {
- optstr = argv[++n];
- if (!optstr)
- bb_error_msg_and_die("missing optstring argument");
- }
- argv[n] = name ? name : argv[0];
- return generate_output(argv + n, argc - n, optstr, long_options);
- }
|