Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 1 | /* args.c - Command line argument parsing. |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 2 | * |
| 3 | * Copyright 2006 Rob Landley <rob@landley.net> |
| 4 | */ |
| 5 | |
Rob Landley | 1e7b49e | 2015-07-10 14:12:44 -0500 | [diff] [blame] | 6 | // NOTE: If option parsing segfaults, switch on TOYBOX_DEBUG in menuconfig. |
| 7 | |
| 8 | // Enabling TOYBOX_DEBUG in .config adds syntax checks to option string parsing |
| 9 | // which aren't needed in the final code (your option string is hardwired and |
| 10 | // should be correct when you ship), but are useful for development. |
| 11 | |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 12 | #include "toys.h" |
| 13 | |
| 14 | // Design goals: |
Rob Landley | dc6db1a | 2013-09-21 12:24:04 -0500 | [diff] [blame] | 15 | // Don't use getopt() out of libc. |
| 16 | // Don't permute original arguments (screwing up ps/top output). |
| 17 | // Integrated --long options "(noshort)a(along)b(blong1)(blong2)" |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 18 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 19 | /* This uses a getopt-like option string, but not getopt() itself. We call |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 20 | * it the get_opt string. |
Rob Landley | 2c22685 | 2007-11-15 18:30:30 -0600 | [diff] [blame] | 21 | * |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 22 | * Each option in the get_opt string corresponds to a bit position in the |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 23 | * return value. The rightmost argument is (1<<0), the next to last is (1<<1) |
| 24 | * and so on. If the option isn't seen in argv[], its bit remains 0. |
Rob Landley | 028a544 | 2007-01-25 16:11:23 -0500 | [diff] [blame] | 25 | * |
| 26 | * Options which have an argument fill in the corresponding slot in the global |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 27 | * union "this" (see generated/globals.h), which it treats as an array of longs |
| 28 | * (note that sizeof(long)==sizeof(pointer) is guaranteed by LP64). |
Rob Landley | 028a544 | 2007-01-25 16:11:23 -0500 | [diff] [blame] | 29 | * |
| 30 | * You don't have to free the option strings, which point into the environment |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 31 | * space. List objects should be freed by main() when command_main() returns. |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 32 | * |
| 33 | * Example: |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 34 | * Calling get_optflags() when toys.which->options="ab:c:d" and |
| 35 | * argv = ["command", "-b", "fruit", "-d", "walrus"] results in: |
| 36 | * |
| 37 | * Changes to struct toys: |
Rob Landley | dc6db1a | 2013-09-21 12:24:04 -0500 | [diff] [blame] | 38 | * toys.optflags = 5 (I.E. 0101 so -b = 4 | -d = 1) |
| 39 | * toys.optargs[0] = "walrus" (leftover argument) |
| 40 | * toys.optargs[1] = NULL (end of list) |
| 41 | * toys.optc = 1 (there was 1 leftover argument) |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 42 | * |
| 43 | * Changes to union this: |
| 44 | * this[0]=NULL (because -c didn't get an argument this time) |
| 45 | * this[1]="fruit" (argument to -b) |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 46 | */ |
| 47 | |
Rob Landley | dc6db1a | 2013-09-21 12:24:04 -0500 | [diff] [blame] | 48 | // What you can put in a get_opt string: |
| 49 | // Any otherwise unused character (all letters, unprefixed numbers) specify |
| 50 | // an option that sets a flag. The bit value is the same as the binary digit |
| 51 | // if you string the option characters together in order. |
| 52 | // So in "abcdefgh" a = 128, h = 1 |
| 53 | // |
| 54 | // Suffixes specify that this option takes an argument (stored in GLOBALS): |
| 55 | // Note that pointer and long are always the same size, even on 64 bit. |
| 56 | // : plus a string argument, keep most recent if more than one |
| 57 | // * plus a string argument, appended to a list |
| 58 | // # plus a signed long argument |
| 59 | // <LOW - die if less than LOW |
| 60 | // >HIGH - die if greater than HIGH |
| 61 | // =DEFAULT - value if not specified |
| 62 | // - plus a signed long argument defaulting to negative (say + for positive) |
| 63 | // . plus a double precision floating point argument (with CFG_TOYBOX_FLOAT) |
| 64 | // Chop this option out with USE_TOYBOX_FLOAT() in option string |
| 65 | // Same <LOW>HIGH=DEFAULT as # |
| 66 | // @ plus an occurrence counter (which is a long) |
| 67 | // (longopt) |
| 68 | // | this is required. If more than one marked, only one required. |
| 69 | // ; long option's argument is optional (can only be supplied with --opt=) |
| 70 | // ^ Stop parsing after encountering this argument |
| 71 | // " " (space char) the "plus an argument" must be separate |
| 72 | // I.E. "-j 3" not "-j3". So "kill -stop" != "kill -s top" |
| 73 | // |
| 74 | // At the beginning of the get_opt string (before any options): |
| 75 | // ^ stop at first nonoption argument |
| 76 | // <0 die if less than # leftover arguments (default 0) |
| 77 | // >9 die if > # leftover arguments (default MAX_INT) |
| 78 | // ? Allow unknown arguments (pass them through to command). |
| 79 | // & first argument has imaginary dash (ala tar/ps) |
| 80 | // If given twice, all arguments have imaginary dash |
| 81 | // |
| 82 | // At the end: [groups] of previously seen options |
| 83 | // - Only one in group (switch off) [-abc] means -ab=-b, -ba=-a, -abc=-c |
Rob Landley | c705b95 | 2013-09-21 13:46:44 -0500 | [diff] [blame] | 84 | // + Synonyms (switch on all) [+abc] means -ab=-abc, -c=-abc |
Rob Landley | dc6db1a | 2013-09-21 12:24:04 -0500 | [diff] [blame] | 85 | // ! More than one in group is error [!abc] means -ab calls error_exit() |
Rob Landley | dc6db1a | 2013-09-21 12:24:04 -0500 | [diff] [blame] | 86 | // primarily useful if you can switch things back off again. |
Rob Landley | dc6db1a | 2013-09-21 12:24:04 -0500 | [diff] [blame] | 87 | |
| 88 | // Notes from getopt man page |
| 89 | // - and -- cannot be arguments. |
| 90 | // -- force end of arguments |
| 91 | // - is a synonym for stdin in file arguments |
| 92 | // -abcd means -a -b -c -d (but if -b takes an argument, then it's -a -b cd) |
| 93 | |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 94 | // Linked list of all known options (option string parsed into this). |
Rob Landley | dc6db1a | 2013-09-21 12:24:04 -0500 | [diff] [blame] | 95 | // Hangs off getoptflagstate, freed at end of option parsing. |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 96 | struct opts { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 97 | struct opts *next; |
| 98 | long *arg; // Pointer into union "this" to store arguments at. |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 99 | int c; // Argument character to match |
Rob Landley | aaecbba | 2015-12-10 15:57:08 -0600 | [diff] [blame] | 100 | int flags; // |=1, ^=2, " "=4, ;=8 |
| 101 | unsigned long long dex[3]; // bits to disable/enable/exclude in toys.optflags |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 102 | char type; // Type of arguments to store union "this" |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 103 | union { |
| 104 | long l; |
| 105 | FLOAT f; |
| 106 | } val[3]; // low, high, default - range of allowed values |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 107 | }; |
| 108 | |
Rob Landley | dc6db1a | 2013-09-21 12:24:04 -0500 | [diff] [blame] | 109 | // linked list of long options. (Hangs off getoptflagstate, free at end of |
| 110 | // option parsing, details about flag to set and global slot to fill out |
| 111 | // stored in related short option struct, but if opt->c = -1 the long option |
| 112 | // is "bare" (has no corresponding short option). |
Rob Landley | 763e42b | 2011-11-30 07:21:23 -0600 | [diff] [blame] | 113 | struct longopts { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 114 | struct longopts *next; |
| 115 | struct opts *opt; |
| 116 | char *str; |
| 117 | int len; |
Rob Landley | 763e42b | 2011-11-30 07:21:23 -0600 | [diff] [blame] | 118 | }; |
| 119 | |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 120 | // State during argument parsing. |
| 121 | struct getoptflagstate |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 122 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 123 | int argc, minargs, maxargs, nodash; |
| 124 | char *arg; |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 125 | struct opts *opts; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 126 | struct longopts *longopts; |
| 127 | int noerror, nodash_now, stopearly; |
Rob Landley | 9e89d47 | 2013-07-31 03:24:58 -0500 | [diff] [blame] | 128 | unsigned excludes, requires; |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 129 | }; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 130 | |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 131 | // Use getoptflagstate to parse parse one command line option from argv |
| 132 | static int gotflag(struct getoptflagstate *gof, struct opts *opt) |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 133 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 134 | int type; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 135 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 136 | // Did we recognize this option? |
| 137 | if (!opt) { |
| 138 | if (gof->noerror) return 1; |
Rob Landley | d3267c6 | 2017-03-23 11:35:09 -0500 | [diff] [blame] | 139 | error_exit("Unknown option %s", gof->arg); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 140 | } |
Rob Landley | 763e42b | 2011-11-30 07:21:23 -0600 | [diff] [blame] | 141 | |
Rob Landley | 845c8da | 2014-08-15 18:53:01 -0500 | [diff] [blame] | 142 | // Might enabling this switch off something else? |
Rob Landley | c8a4997 | 2013-09-06 12:18:46 -0500 | [diff] [blame] | 143 | if (toys.optflags & opt->dex[0]) { |
| 144 | struct opts *clr; |
Rob Landley | aaecbba | 2015-12-10 15:57:08 -0600 | [diff] [blame] | 145 | unsigned long long i = 1; |
Rob Landley | c8a4997 | 2013-09-06 12:18:46 -0500 | [diff] [blame] | 146 | |
Rob Landley | 845c8da | 2014-08-15 18:53:01 -0500 | [diff] [blame] | 147 | // Forget saved argument for flag we switch back off |
Rob Landley | 9d4cd46 | 2013-09-09 02:44:02 -0500 | [diff] [blame] | 148 | for (clr=gof->opts, i=1; clr; clr = clr->next, i<<=1) |
Rob Landley | 845c8da | 2014-08-15 18:53:01 -0500 | [diff] [blame] | 149 | if (clr->arg && (i & toys.optflags & opt->dex[0])) *clr->arg = 0; |
Rob Landley | c8a4997 | 2013-09-06 12:18:46 -0500 | [diff] [blame] | 150 | toys.optflags &= ~opt->dex[0]; |
| 151 | } |
Rob Landley | 845c8da | 2014-08-15 18:53:01 -0500 | [diff] [blame] | 152 | |
| 153 | // Set flags |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 154 | toys.optflags |= opt->dex[1]; |
| 155 | gof->excludes |= opt->dex[2]; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 156 | if (opt->flags&2) gof->stopearly=2; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 157 | |
Rob Landley | de51192 | 2012-11-25 14:40:25 -0600 | [diff] [blame] | 158 | if (toys.optflags & gof->excludes) { |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 159 | struct opts *bad; |
| 160 | unsigned i = 1; |
| 161 | |
Rob Landley | 4d4ca28 | 2017-01-04 01:14:07 -0600 | [diff] [blame] | 162 | for (bad=gof->opts, i=1; bad ;bad = bad->next, i<<=1) { |
Rob Landley | 8abf095 | 2012-12-29 03:18:34 -0600 | [diff] [blame] | 163 | if (opt == bad || !(i & toys.optflags)) continue; |
| 164 | if (toys.optflags & bad->dex[2]) break; |
| 165 | } |
Rob Landley | d3267c6 | 2017-03-23 11:35:09 -0500 | [diff] [blame] | 166 | if (bad) error_exit("No '%c' with '%c'", opt->c, bad->c); |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 167 | } |
| 168 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 169 | // Does this option take an argument? |
Rob Landley | b7162a4 | 2013-09-01 07:50:32 -0500 | [diff] [blame] | 170 | if (!gof->arg) { |
| 171 | if (opt->flags & 8) return 0; |
| 172 | gof->arg = ""; |
| 173 | } else gof->arg++; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 174 | type = opt->type; |
Rob Landley | 4d3b3da | 2013-06-22 15:36:25 -0500 | [diff] [blame] | 175 | |
| 176 | if (type == '@') ++*(opt->arg); |
| 177 | else if (type) { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 178 | char *arg = gof->arg; |
Rob Landley | 58c6c1b | 2006-11-25 13:34:51 -0500 | [diff] [blame] | 179 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 180 | // Handle "-xblah" and "-x blah", but also a third case: "abxc blah" |
| 181 | // to make "tar xCjfv blah1 blah2 thingy" work like |
| 182 | // "tar -x -C blah1 -j -f blah2 -v thingy" |
Rob Landley | 763e42b | 2011-11-30 07:21:23 -0600 | [diff] [blame] | 183 | |
Rob Landley | b7162a4 | 2013-09-01 07:50:32 -0500 | [diff] [blame] | 184 | if (gof->nodash_now || (!arg[0] && !(opt->flags & 8))) |
| 185 | arg = toys.argv[++gof->argc]; |
| 186 | if (!arg) { |
| 187 | char *s = "Missing argument to "; |
| 188 | struct longopts *lo; |
| 189 | |
Rob Landley | d3267c6 | 2017-03-23 11:35:09 -0500 | [diff] [blame] | 190 | if (opt->c != -1) error_exit("%s-%c", s, opt->c); |
Rob Landley | b7162a4 | 2013-09-01 07:50:32 -0500 | [diff] [blame] | 191 | |
| 192 | for (lo = gof->longopts; lo->opt != opt; lo = lo->next); |
Rob Landley | d3267c6 | 2017-03-23 11:35:09 -0500 | [diff] [blame] | 193 | error_exit("%s--%.*s", s, lo->len, lo->str); |
Rob Landley | b7162a4 | 2013-09-01 07:50:32 -0500 | [diff] [blame] | 194 | } |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 195 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 196 | if (type == ':') *(opt->arg) = (long)arg; |
| 197 | else if (type == '*') { |
| 198 | struct arg_list **list; |
Rob Landley | 4ac6656 | 2008-06-16 19:27:35 -0500 | [diff] [blame] | 199 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 200 | list = (struct arg_list **)opt->arg; |
| 201 | while (*list) list=&((*list)->next); |
| 202 | *list = xzalloc(sizeof(struct arg_list)); |
| 203 | (*list)->arg = arg; |
| 204 | } else if (type == '#' || type == '-') { |
| 205 | long l = atolx(arg); |
| 206 | if (type == '-' && !ispunct(*arg)) l*=-1; |
Rob Landley | d3267c6 | 2017-03-23 11:35:09 -0500 | [diff] [blame] | 207 | if (l < opt->val[0].l) error_exit("-%c < %ld", opt->c, opt->val[0].l); |
| 208 | if (l > opt->val[1].l) error_exit("-%c > %ld", opt->c, opt->val[1].l); |
Rob Landley | b6063de | 2012-01-29 13:54:13 -0600 | [diff] [blame] | 209 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 210 | *(opt->arg) = l; |
| 211 | } else if (CFG_TOYBOX_FLOAT && type == '.') { |
| 212 | FLOAT *f = (FLOAT *)(opt->arg); |
Rob Landley | b6063de | 2012-01-29 13:54:13 -0600 | [diff] [blame] | 213 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 214 | *f = strtod(arg, &arg); |
| 215 | if (opt->val[0].l != LONG_MIN && *f < opt->val[0].f) |
Rob Landley | d3267c6 | 2017-03-23 11:35:09 -0500 | [diff] [blame] | 216 | error_exit("-%c < %lf", opt->c, (double)opt->val[0].f); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 217 | if (opt->val[1].l != LONG_MAX && *f > opt->val[1].f) |
Rob Landley | d3267c6 | 2017-03-23 11:35:09 -0500 | [diff] [blame] | 218 | error_exit("-%c > %lf", opt->c, (double)opt->val[1].f); |
Rob Landley | 4d3b3da | 2013-06-22 15:36:25 -0500 | [diff] [blame] | 219 | } |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 220 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 221 | if (!gof->nodash_now) gof->arg = ""; |
| 222 | } |
Rob Landley | 58c6c1b | 2006-11-25 13:34:51 -0500 | [diff] [blame] | 223 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 224 | return 0; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 225 | } |
| 226 | |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 227 | // Parse this command's options string into struct getoptflagstate, which |
| 228 | // includes a struct opts linked list in reverse order (I.E. right-to-left) |
Rob Landley | 763e42b | 2011-11-30 07:21:23 -0600 | [diff] [blame] | 229 | void parse_optflaglist(struct getoptflagstate *gof) |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 230 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 231 | char *options = toys.which->options; |
| 232 | long *nextarg = (long *)&this; |
| 233 | struct opts *new = 0; |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 234 | int idx; |
Rob Landley | 54ebcce | 2006-11-19 20:35:19 -0500 | [diff] [blame] | 235 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 236 | // Parse option format string |
| 237 | memset(gof, 0, sizeof(struct getoptflagstate)); |
| 238 | gof->maxargs = INT_MAX; |
| 239 | if (!options) return; |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 240 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 241 | // Parse leading special behavior indicators |
| 242 | for (;;) { |
| 243 | if (*options == '^') gof->stopearly++; |
| 244 | else if (*options == '<') gof->minargs=*(++options)-'0'; |
| 245 | else if (*options == '>') gof->maxargs=*(++options)-'0'; |
| 246 | else if (*options == '?') gof->noerror++; |
| 247 | else if (*options == '&') gof->nodash++; |
| 248 | else break; |
| 249 | options++; |
| 250 | } |
Rob Landley | 763e42b | 2011-11-30 07:21:23 -0600 | [diff] [blame] | 251 | |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 252 | // Parse option string into a linked list of options with attributes. |
Rob Landley | 763e42b | 2011-11-30 07:21:23 -0600 | [diff] [blame] | 253 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 254 | if (!*options) gof->stopearly++; |
| 255 | while (*options) { |
| 256 | char *temp; |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 257 | |
| 258 | // Option groups come after all options are defined |
| 259 | if (*options == '[') break; |
Rob Landley | 763e42b | 2011-11-30 07:21:23 -0600 | [diff] [blame] | 260 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 261 | // Allocate a new list entry when necessary |
| 262 | if (!new) { |
| 263 | new = xzalloc(sizeof(struct opts)); |
| 264 | new->next = gof->opts; |
| 265 | gof->opts = new; |
| 266 | new->val[0].l = LONG_MIN; |
| 267 | new->val[1].l = LONG_MAX; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 268 | } |
| 269 | // Each option must start with "(" or an option character. (Bare |
| 270 | // longopts only come at the start of the string.) |
Rob Landley | b7162a4 | 2013-09-01 07:50:32 -0500 | [diff] [blame] | 271 | if (*options == '(' && new->c != -1) { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 272 | char *end; |
Rob Landley | b7162a4 | 2013-09-01 07:50:32 -0500 | [diff] [blame] | 273 | struct longopts *lo; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 274 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 275 | // Find the end of the longopt |
| 276 | for (end = ++options; *end && *end != ')'; end++); |
| 277 | if (CFG_TOYBOX_DEBUG && !*end) error_exit("(longopt) didn't end"); |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 278 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 279 | // init a new struct longopts |
Rob Landley | b7162a4 | 2013-09-01 07:50:32 -0500 | [diff] [blame] | 280 | lo = xmalloc(sizeof(struct longopts)); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 281 | lo->next = gof->longopts; |
| 282 | lo->opt = new; |
| 283 | lo->str = options; |
| 284 | lo->len = end-options; |
| 285 | gof->longopts = lo; |
Rob Landley | b7162a4 | 2013-09-01 07:50:32 -0500 | [diff] [blame] | 286 | options = ++end; |
Rob Landley | 763e42b | 2011-11-30 07:21:23 -0600 | [diff] [blame] | 287 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 288 | // Mark this struct opt as used, even when no short opt. |
Rob Landley | b7162a4 | 2013-09-01 07:50:32 -0500 | [diff] [blame] | 289 | if (!new->c) new->c = -1; |
| 290 | |
| 291 | continue; |
Rob Landley | 763e42b | 2011-11-30 07:21:23 -0600 | [diff] [blame] | 292 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 293 | // If this is the start of a new option that wasn't a longopt, |
Rob Landley | 763e42b | 2011-11-30 07:21:23 -0600 | [diff] [blame] | 294 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 295 | } else if (strchr(":*#@.-", *options)) { |
| 296 | if (CFG_TOYBOX_DEBUG && new->type) |
| 297 | error_exit("multiple types %c:%c%c", new->c, new->type, *options); |
| 298 | new->type = *options; |
Rob Landley | b7162a4 | 2013-09-01 07:50:32 -0500 | [diff] [blame] | 299 | } else if (-1 != (idx = stridx("|^ ;", *options))) new->flags |= 1<<idx; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 300 | // bounds checking |
| 301 | else if (-1 != (idx = stridx("<>=", *options))) { |
| 302 | if (new->type == '#') { |
| 303 | long l = strtol(++options, &temp, 10); |
| 304 | if (temp != options) new->val[idx].l = l; |
| 305 | } else if (CFG_TOYBOX_FLOAT && new->type == '.') { |
| 306 | FLOAT f = strtod(++options, &temp); |
| 307 | if (temp != options) new->val[idx].f = f; |
| 308 | } else if (CFG_TOYBOX_DEBUG) error_exit("<>= only after .#"); |
| 309 | options = --temp; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 310 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 311 | // At this point, we've hit the end of the previous option. The |
| 312 | // current character is the start of a new option. If we've already |
| 313 | // assigned an option to this struct, loop to allocate a new one. |
| 314 | // (It'll get back here afterwards and fall through to next else.) |
Rob Landley | b7162a4 | 2013-09-01 07:50:32 -0500 | [diff] [blame] | 315 | } else if (new->c) { |
| 316 | new = 0; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 317 | continue; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 318 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 319 | // Claim this option, loop to see what's after it. |
| 320 | } else new->c = *options; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 321 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 322 | options++; |
| 323 | } |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 324 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 325 | // Initialize enable/disable/exclude masks and pointers to store arguments. |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 326 | // (This goes right to left so we need the whole list before we can start.) |
| 327 | idx = 0; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 328 | for (new = gof->opts; new; new = new->next) { |
Rob Landley | aaecbba | 2015-12-10 15:57:08 -0600 | [diff] [blame] | 329 | unsigned long long u = 1L<<idx++; |
Rob Landley | 9e89d47 | 2013-07-31 03:24:58 -0500 | [diff] [blame] | 330 | |
Rob Landley | cf2516a | 2015-01-01 16:19:40 -0600 | [diff] [blame] | 331 | if (new->c == 1) new->c = 0; |
Rob Landley | 9e89d47 | 2013-07-31 03:24:58 -0500 | [diff] [blame] | 332 | new->dex[1] = u; |
| 333 | if (new->flags & 1) gof->requires |= u; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 334 | if (new->type) { |
| 335 | new->arg = (void *)nextarg; |
| 336 | *(nextarg++) = new->val[2].l; |
| 337 | } |
| 338 | } |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 339 | |
| 340 | // Parse trailing group indicators |
| 341 | while (*options) { |
| 342 | unsigned bits = 0; |
| 343 | |
Rob Landley | 6d91e0f | 2012-12-27 18:44:37 -0600 | [diff] [blame] | 344 | if (CFG_TOYBOX_DEBUG && *options != '[') error_exit("trailing %s", options); |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 345 | |
Rob Landley | c705b95 | 2013-09-21 13:46:44 -0500 | [diff] [blame] | 346 | idx = stridx("-+!", *++options); |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 347 | if (CFG_TOYBOX_DEBUG && idx == -1) error_exit("[ needs +-!"); |
Rob Landley | c705b95 | 2013-09-21 13:46:44 -0500 | [diff] [blame] | 348 | if (CFG_TOYBOX_DEBUG && (options[1] == ']' || !options[1])) |
Rob Landley | dc6db1a | 2013-09-21 12:24:04 -0500 | [diff] [blame] | 349 | error_exit("empty []"); |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 350 | |
| 351 | // Don't advance past ] but do process it once in loop. |
Rob Landley | c705b95 | 2013-09-21 13:46:44 -0500 | [diff] [blame] | 352 | while (*options++ != ']') { |
| 353 | struct opts *opt; |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 354 | int i; |
| 355 | |
| 356 | if (CFG_TOYBOX_DEBUG && !*options) error_exit("[ without ]"); |
| 357 | // Find this option flag (in previously parsed struct opt) |
| 358 | for (i=0, opt = gof->opts; ; i++, opt = opt->next) { |
| 359 | if (*options == ']') { |
| 360 | if (!opt) break; |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 361 | if (bits&(1<<i)) opt->dex[idx] |= bits&~(1<<i); |
| 362 | } else { |
Rob Landley | 0ed17f1 | 2016-07-04 08:32:42 -0500 | [diff] [blame] | 363 | if (*options==1) break; |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 364 | if (CFG_TOYBOX_DEBUG && !opt) |
| 365 | error_exit("[] unknown target %c", *options); |
| 366 | if (opt->c == *options) { |
| 367 | bits |= 1<<i; |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 368 | break; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | } |
Rob Landley | 763e42b | 2011-11-30 07:21:23 -0600 | [diff] [blame] | 374 | } |
| 375 | |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 376 | // Fill out toys.optflags, toys.optargs, and this[] from toys.argv |
| 377 | |
Rob Landley | 763e42b | 2011-11-30 07:21:23 -0600 | [diff] [blame] | 378 | void get_optflags(void) |
| 379 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 380 | struct getoptflagstate gof; |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 381 | struct opts *catch; |
Rob Landley | aaecbba | 2015-12-10 15:57:08 -0600 | [diff] [blame] | 382 | unsigned long long saveflags; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 383 | char *letters[]={"s",""}; |
Rob Landley | 763e42b | 2011-11-30 07:21:23 -0600 | [diff] [blame] | 384 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 385 | // Option parsing is a two stage process: parse the option string into |
| 386 | // a struct opts list, then use that list to process argv[]; |
Rob Landley | b6063de | 2012-01-29 13:54:13 -0600 | [diff] [blame] | 387 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 388 | // Allocate memory for optargs |
| 389 | saveflags = 0; |
| 390 | while (toys.argv[saveflags++]); |
| 391 | toys.optargs = xzalloc(sizeof(char *)*saveflags); |
Rob Landley | 763e42b | 2011-11-30 07:21:23 -0600 | [diff] [blame] | 392 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 393 | parse_optflaglist(&gof); |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 394 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 395 | // Iterate through command line arguments, skipping argv[0] |
| 396 | for (gof.argc=1; toys.argv[gof.argc]; gof.argc++) { |
| 397 | gof.arg = toys.argv[gof.argc]; |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 398 | catch = NULL; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 399 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 400 | // Parse this argument |
| 401 | if (gof.stopearly>1) goto notflag; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 402 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 403 | gof.nodash_now = 0; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 404 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 405 | // Various things with dashes |
| 406 | if (*gof.arg == '-') { |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 407 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 408 | // Handle - |
| 409 | if (!gof.arg[1]) goto notflag; |
| 410 | gof.arg++; |
| 411 | if (*gof.arg=='-') { |
| 412 | struct longopts *lo; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 413 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 414 | gof.arg++; |
| 415 | // Handle -- |
| 416 | if (!*gof.arg) { |
| 417 | gof.stopearly += 2; |
Rob Landley | a137c3f | 2013-04-06 19:57:54 -0500 | [diff] [blame] | 418 | continue; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 419 | } |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 420 | |
Rob Landley | b7162a4 | 2013-09-01 07:50:32 -0500 | [diff] [blame] | 421 | // do we match a known --longopt? |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 422 | for (lo = gof.longopts; lo; lo = lo->next) { |
| 423 | if (!strncmp(gof.arg, lo->str, lo->len)) { |
Rob Landley | b7162a4 | 2013-09-01 07:50:32 -0500 | [diff] [blame] | 424 | if (!gof.arg[lo->len]) gof.arg = 0; |
| 425 | else if (gof.arg[lo->len] == '=' && lo->opt->type) |
| 426 | gof.arg += lo->len; |
| 427 | else continue; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 428 | // It's a match. |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 429 | catch = lo->opt; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 430 | break; |
| 431 | } |
| 432 | } |
Rob Landley | fdb667e | 2006-11-24 00:15:21 -0500 | [diff] [blame] | 433 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 434 | // Should we handle this --longopt as a non-option argument? |
| 435 | if (!lo && gof.noerror) { |
Rob Landley | b7162a4 | 2013-09-01 07:50:32 -0500 | [diff] [blame] | 436 | gof.arg -= 2; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 437 | goto notflag; |
| 438 | } |
Rob Landley | 1a221d9 | 2008-05-17 17:13:26 -0500 | [diff] [blame] | 439 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 440 | // Long option parsed, handle option. |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 441 | gotflag(&gof, catch); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 442 | continue; |
| 443 | } |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 444 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 445 | // Handle things that don't start with a dash. |
| 446 | } else { |
| 447 | if (gof.nodash && (gof.nodash>1 || gof.argc == 1)) gof.nodash_now = 1; |
| 448 | else goto notflag; |
| 449 | } |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 450 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 451 | // At this point, we have the args part of -args. Loop through |
| 452 | // each entry (could be -abc meaning -a -b -c) |
| 453 | saveflags = toys.optflags; |
| 454 | while (*gof.arg) { |
Rob Landley | fdb667e | 2006-11-24 00:15:21 -0500 | [diff] [blame] | 455 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 456 | // Identify next option char. |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 457 | for (catch = gof.opts; catch; catch = catch->next) |
| 458 | if (*gof.arg == catch->c) |
| 459 | if (!((catch->flags&4) && gof.arg[1])) break; |
Rob Landley | fdb667e | 2006-11-24 00:15:21 -0500 | [diff] [blame] | 460 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 461 | // Handle option char (advancing past what was used) |
Rob Landley | 7f909bd | 2012-11-19 01:49:53 -0600 | [diff] [blame] | 462 | if (gotflag(&gof, catch) ) { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 463 | toys.optflags = saveflags; |
| 464 | gof.arg = toys.argv[gof.argc]; |
| 465 | goto notflag; |
| 466 | } |
| 467 | } |
| 468 | continue; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 469 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 470 | // Not a flag, save value in toys.optargs[] |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 471 | notflag: |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 472 | if (gof.stopearly) gof.stopearly++; |
| 473 | toys.optargs[toys.optc++] = toys.argv[gof.argc]; |
| 474 | } |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 475 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 476 | // Sanity check |
| 477 | if (toys.optc<gof.minargs) |
Rob Landley | d3267c6 | 2017-03-23 11:35:09 -0500 | [diff] [blame] | 478 | error_exit("Need%s %d argument%s", letters[!!(gof.minargs-1)], |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 479 | gof.minargs, letters[!(gof.minargs-1)]); |
| 480 | if (toys.optc>gof.maxargs) |
Rob Landley | d3267c6 | 2017-03-23 11:35:09 -0500 | [diff] [blame] | 481 | error_exit("Max %d argument%s", gof.maxargs, letters[!(gof.maxargs-1)]); |
Rob Landley | 9e89d47 | 2013-07-31 03:24:58 -0500 | [diff] [blame] | 482 | if (gof.requires && !(gof.requires & toys.optflags)) { |
| 483 | struct opts *req; |
| 484 | char needs[32], *s = needs; |
| 485 | |
| 486 | for (req = gof.opts; req; req = req->next) |
| 487 | if (req->flags & 1) *(s++) = req->c; |
| 488 | *s = 0; |
| 489 | |
Rob Landley | d3267c6 | 2017-03-23 11:35:09 -0500 | [diff] [blame] | 490 | error_exit("Needs %s-%s", s[1] ? "one of " : "", needs); |
Rob Landley | 9e89d47 | 2013-07-31 03:24:58 -0500 | [diff] [blame] | 491 | } |
Rob Landley | 9e2b6db | 2012-07-15 17:22:04 -0500 | [diff] [blame] | 492 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 493 | if (CFG_TOYBOX_FREE) { |
| 494 | llist_traverse(gof.opts, free); |
| 495 | llist_traverse(gof.longopts, free); |
| 496 | } |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 497 | } |