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