blob: 592d33c840bd74b5353e7dee7a36899382822892 [file] [log] [blame]
Rob Landley7aa651a2012-11-13 17:14:08 -06001/* args.c - Command line argument parsing.
Rob Landley8324b892006-11-19 02:49:22 -05002 *
3 * Copyright 2006 Rob Landley <rob@landley.net>
4 */
5
6#include "toys.h"
7
8// Design goals:
Rob Landleydc6db1a2013-09-21 12:24:04 -05009// 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 Landley8324b892006-11-19 02:49:22 -050012
Rob Landley7aa651a2012-11-13 17:14:08 -060013/* This uses a getopt-like option string, but not getopt() itself. We call
Rob Landley61190a32008-02-18 03:32:17 -060014 * it the get_opt string.
Rob Landley2c226852007-11-15 18:30:30 -060015 *
Rob Landley61190a32008-02-18 03:32:17 -060016 * Each option in the get_opt string corresponds to a bit position in the
Rob Landley7aa651a2012-11-13 17:14:08 -060017 * 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 Landley028a5442007-01-25 16:11:23 -050019 *
20 * Options which have an argument fill in the corresponding slot in the global
Rob Landley61190a32008-02-18 03:32:17 -060021 * 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 Landley028a5442007-01-25 16:11:23 -050023 *
24 * You don't have to free the option strings, which point into the environment
Rob Landley7aa651a2012-11-13 17:14:08 -060025 * space. List objects should be freed by main() when command_main() returns.
Rob Landley8324b892006-11-19 02:49:22 -050026 *
27 * Example:
Rob Landley61190a32008-02-18 03:32:17 -060028 * 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 Landleydc6db1a2013-09-21 12:24:04 -050032 * 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 Landley61190a32008-02-18 03:32:17 -060036 *
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 Landley8324b892006-11-19 02:49:22 -050040 */
41
Rob Landleydc6db1a2013-09-21 12:24:04 -050042// 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 Landleyc705b952013-09-21 13:46:44 -050082// + Synonyms (switch on all) [+abc] means -ab=-abc, -c=-abc
Rob Landleydc6db1a2013-09-21 12:24:04 -050083// ! More than one in group is error [!abc] means -ab calls error_exit()
Rob Landleydc6db1a2013-09-21 12:24:04 -050084// primarily useful if you can switch things back off again.
Rob Landleydc6db1a2013-09-21 12:24:04 -050085
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 Landley7f909bd2012-11-19 01:49:53 -060092// Linked list of all known options (option string parsed into this).
Rob Landleydc6db1a2013-09-21 12:24:04 -050093// Hangs off getoptflagstate, freed at end of option parsing.
Rob Landley8324b892006-11-19 02:49:22 -050094struct opts {
Rob Landley7aa651a2012-11-13 17:14:08 -060095 struct opts *next;
96 long *arg; // Pointer into union "this" to store arguments at.
Rob Landley7f909bd2012-11-19 01:49:53 -060097 int c; // Argument character to match
Rob Landley7aa651a2012-11-13 17:14:08 -060098 int flags; // |=1, ^=2
Rob Landley7f909bd2012-11-19 01:49:53 -060099 unsigned dex[3]; // which bits to disable/enable/exclude in toys.optflags
100 char type; // Type of arguments to store union "this"
Rob Landley7aa651a2012-11-13 17:14:08 -0600101 union {
102 long l;
103 FLOAT f;
104 } val[3]; // low, high, default - range of allowed values
Rob Landley8324b892006-11-19 02:49:22 -0500105};
106
Rob Landleydc6db1a2013-09-21 12:24:04 -0500107// 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 Landley763e42b2011-11-30 07:21:23 -0600111struct longopts {
Rob Landley7aa651a2012-11-13 17:14:08 -0600112 struct longopts *next;
113 struct opts *opt;
114 char *str;
115 int len;
Rob Landley763e42b2011-11-30 07:21:23 -0600116};
117
Rob Landley61190a32008-02-18 03:32:17 -0600118// State during argument parsing.
119struct getoptflagstate
Rob Landley8324b892006-11-19 02:49:22 -0500120{
Rob Landley7aa651a2012-11-13 17:14:08 -0600121 int argc, minargs, maxargs, nodash;
122 char *arg;
Rob Landley7f909bd2012-11-19 01:49:53 -0600123 struct opts *opts;
Rob Landley7aa651a2012-11-13 17:14:08 -0600124 struct longopts *longopts;
125 int noerror, nodash_now, stopearly;
Rob Landley9e89d472013-07-31 03:24:58 -0500126 unsigned excludes, requires;
Rob Landley61190a32008-02-18 03:32:17 -0600127};
Rob Landley8324b892006-11-19 02:49:22 -0500128
Rob Landley7f909bd2012-11-19 01:49:53 -0600129// Use getoptflagstate to parse parse one command line option from argv
130static int gotflag(struct getoptflagstate *gof, struct opts *opt)
Rob Landley8324b892006-11-19 02:49:22 -0500131{
Rob Landley7aa651a2012-11-13 17:14:08 -0600132 int type;
Rob Landley8324b892006-11-19 02:49:22 -0500133
Rob Landley7aa651a2012-11-13 17:14:08 -0600134 // Did we recognize this option?
135 if (!opt) {
136 if (gof->noerror) return 1;
137 error_exit("Unknown option %s", gof->arg);
138 }
Rob Landley763e42b2011-11-30 07:21:23 -0600139
Rob Landley845c8da2014-08-15 18:53:01 -0500140 // Might enabling this switch off something else?
Rob Landleyc8a49972013-09-06 12:18:46 -0500141 if (toys.optflags & opt->dex[0]) {
142 struct opts *clr;
143 unsigned i = 1;
144
Rob Landley845c8da2014-08-15 18:53:01 -0500145 // Forget saved argument for flag we switch back off
Rob Landley9d4cd462013-09-09 02:44:02 -0500146 for (clr=gof->opts, i=1; clr; clr = clr->next, i<<=1)
Rob Landley845c8da2014-08-15 18:53:01 -0500147 if (clr->arg && (i & toys.optflags & opt->dex[0])) *clr->arg = 0;
Rob Landleyc8a49972013-09-06 12:18:46 -0500148 toys.optflags &= ~opt->dex[0];
149 }
Rob Landley845c8da2014-08-15 18:53:01 -0500150
151 // Set flags
Rob Landley7f909bd2012-11-19 01:49:53 -0600152 toys.optflags |= opt->dex[1];
153 gof->excludes |= opt->dex[2];
Rob Landley7aa651a2012-11-13 17:14:08 -0600154 if (opt->flags&2) gof->stopearly=2;
Rob Landley8324b892006-11-19 02:49:22 -0500155
Rob Landleyde511922012-11-25 14:40:25 -0600156 if (toys.optflags & gof->excludes) {
Rob Landley7f909bd2012-11-19 01:49:53 -0600157 struct opts *bad;
158 unsigned i = 1;
159
Rob Landley8abf0952012-12-29 03:18:34 -0600160 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 Landley7f909bd2012-11-19 01:49:53 -0600164 error_exit("No '%c' with '%c'", opt->c, bad->c);
165 }
166
Rob Landley7aa651a2012-11-13 17:14:08 -0600167 // Does this option take an argument?
Rob Landleyb7162a42013-09-01 07:50:32 -0500168 if (!gof->arg) {
169 if (opt->flags & 8) return 0;
170 gof->arg = "";
171 } else gof->arg++;
Rob Landley7aa651a2012-11-13 17:14:08 -0600172 type = opt->type;
Rob Landley4d3b3da2013-06-22 15:36:25 -0500173
174 if (type == '@') ++*(opt->arg);
175 else if (type) {
Rob Landley7aa651a2012-11-13 17:14:08 -0600176 char *arg = gof->arg;
Rob Landley58c6c1b2006-11-25 13:34:51 -0500177
Rob Landley7aa651a2012-11-13 17:14:08 -0600178 // 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 Landley763e42b2011-11-30 07:21:23 -0600181
Rob Landleyb7162a42013-09-01 07:50:32 -0500182 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 Landley8324b892006-11-19 02:49:22 -0500193
Rob Landley7aa651a2012-11-13 17:14:08 -0600194 if (type == ':') *(opt->arg) = (long)arg;
195 else if (type == '*') {
196 struct arg_list **list;
Rob Landley4ac66562008-06-16 19:27:35 -0500197
Rob Landley7aa651a2012-11-13 17:14:08 -0600198 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 Landleyb6063de2012-01-29 13:54:13 -0600207
Rob Landley7aa651a2012-11-13 17:14:08 -0600208 *(opt->arg) = l;
209 } else if (CFG_TOYBOX_FLOAT && type == '.') {
210 FLOAT *f = (FLOAT *)(opt->arg);
Rob Landleyb6063de2012-01-29 13:54:13 -0600211
Rob Landley7aa651a2012-11-13 17:14:08 -0600212 *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 Landley4d3b3da2013-06-22 15:36:25 -0500217 }
Rob Landley8324b892006-11-19 02:49:22 -0500218
Rob Landley7aa651a2012-11-13 17:14:08 -0600219 if (!gof->nodash_now) gof->arg = "";
220 }
Rob Landley58c6c1b2006-11-25 13:34:51 -0500221
Rob Landley7aa651a2012-11-13 17:14:08 -0600222 return 0;
Rob Landley8324b892006-11-19 02:49:22 -0500223}
224
Rob Landley7f909bd2012-11-19 01:49:53 -0600225// 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 Landley763e42b2011-11-30 07:21:23 -0600227void parse_optflaglist(struct getoptflagstate *gof)
Rob Landley8324b892006-11-19 02:49:22 -0500228{
Rob Landley7aa651a2012-11-13 17:14:08 -0600229 char *options = toys.which->options;
230 long *nextarg = (long *)&this;
231 struct opts *new = 0;
Rob Landley7f909bd2012-11-19 01:49:53 -0600232 int idx;
Rob Landley54ebcce2006-11-19 20:35:19 -0500233
Rob Landley7aa651a2012-11-13 17:14:08 -0600234 // Parse option format string
235 memset(gof, 0, sizeof(struct getoptflagstate));
236 gof->maxargs = INT_MAX;
237 if (!options) return;
Rob Landley61190a32008-02-18 03:32:17 -0600238
Rob Landley7aa651a2012-11-13 17:14:08 -0600239 // 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 Landley763e42b2011-11-30 07:21:23 -0600249
Rob Landley7f909bd2012-11-19 01:49:53 -0600250 // Parse option string into a linked list of options with attributes.
Rob Landley763e42b2011-11-30 07:21:23 -0600251
Rob Landley7aa651a2012-11-13 17:14:08 -0600252 if (!*options) gof->stopearly++;
253 while (*options) {
254 char *temp;
Rob Landley7f909bd2012-11-19 01:49:53 -0600255
256 // Option groups come after all options are defined
257 if (*options == '[') break;
Rob Landley763e42b2011-11-30 07:21:23 -0600258
Rob Landley7aa651a2012-11-13 17:14:08 -0600259 // 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 Landley7aa651a2012-11-13 17:14:08 -0600266 }
267 // Each option must start with "(" or an option character. (Bare
268 // longopts only come at the start of the string.)
Rob Landleyb7162a42013-09-01 07:50:32 -0500269 if (*options == '(' && new->c != -1) {
Rob Landley7aa651a2012-11-13 17:14:08 -0600270 char *end;
Rob Landleyb7162a42013-09-01 07:50:32 -0500271 struct longopts *lo;
Rob Landley8324b892006-11-19 02:49:22 -0500272
Rob Landley7aa651a2012-11-13 17:14:08 -0600273 // 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 Landley8324b892006-11-19 02:49:22 -0500276
Rob Landley7aa651a2012-11-13 17:14:08 -0600277 // init a new struct longopts
Rob Landleyb7162a42013-09-01 07:50:32 -0500278 lo = xmalloc(sizeof(struct longopts));
Rob Landley7aa651a2012-11-13 17:14:08 -0600279 lo->next = gof->longopts;
280 lo->opt = new;
281 lo->str = options;
282 lo->len = end-options;
283 gof->longopts = lo;
Rob Landleyb7162a42013-09-01 07:50:32 -0500284 options = ++end;
Rob Landley763e42b2011-11-30 07:21:23 -0600285
Rob Landley7aa651a2012-11-13 17:14:08 -0600286 // Mark this struct opt as used, even when no short opt.
Rob Landleyb7162a42013-09-01 07:50:32 -0500287 if (!new->c) new->c = -1;
288
289 continue;
Rob Landley763e42b2011-11-30 07:21:23 -0600290
Rob Landley7aa651a2012-11-13 17:14:08 -0600291 // If this is the start of a new option that wasn't a longopt,
Rob Landley763e42b2011-11-30 07:21:23 -0600292
Rob Landley7aa651a2012-11-13 17:14:08 -0600293 } 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 Landleyb7162a42013-09-01 07:50:32 -0500297 } else if (-1 != (idx = stridx("|^ ;", *options))) new->flags |= 1<<idx;
Rob Landley7aa651a2012-11-13 17:14:08 -0600298 // 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 Landley8324b892006-11-19 02:49:22 -0500308
Rob Landley7aa651a2012-11-13 17:14:08 -0600309 // 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 Landleyb7162a42013-09-01 07:50:32 -0500313 } else if (new->c) {
314 new = 0;
Rob Landley7aa651a2012-11-13 17:14:08 -0600315 continue;
Rob Landley8324b892006-11-19 02:49:22 -0500316
Rob Landley7aa651a2012-11-13 17:14:08 -0600317 // Claim this option, loop to see what's after it.
318 } else new->c = *options;
Rob Landley8324b892006-11-19 02:49:22 -0500319
Rob Landley7aa651a2012-11-13 17:14:08 -0600320 options++;
321 }
Rob Landley8324b892006-11-19 02:49:22 -0500322
Rob Landley7aa651a2012-11-13 17:14:08 -0600323 // Initialize enable/disable/exclude masks and pointers to store arguments.
Rob Landley7f909bd2012-11-19 01:49:53 -0600324 // (This goes right to left so we need the whole list before we can start.)
325 idx = 0;
Rob Landley7aa651a2012-11-13 17:14:08 -0600326 for (new = gof->opts; new; new = new->next) {
Rob Landley9e89d472013-07-31 03:24:58 -0500327 unsigned u = 1<<idx++;
328
Rob Landleycf2516a2015-01-01 16:19:40 -0600329 if (new->c == 1) new->c = 0;
Rob Landley9e89d472013-07-31 03:24:58 -0500330 new->dex[1] = u;
331 if (new->flags & 1) gof->requires |= u;
Rob Landley7aa651a2012-11-13 17:14:08 -0600332 if (new->type) {
333 new->arg = (void *)nextarg;
334 *(nextarg++) = new->val[2].l;
335 }
336 }
Rob Landley7f909bd2012-11-19 01:49:53 -0600337
338 // Parse trailing group indicators
339 while (*options) {
340 unsigned bits = 0;
341
Rob Landley6d91e0f2012-12-27 18:44:37 -0600342 if (CFG_TOYBOX_DEBUG && *options != '[') error_exit("trailing %s", options);
Rob Landley7f909bd2012-11-19 01:49:53 -0600343
Rob Landleyc705b952013-09-21 13:46:44 -0500344 idx = stridx("-+!", *++options);
Rob Landley7f909bd2012-11-19 01:49:53 -0600345 if (CFG_TOYBOX_DEBUG && idx == -1) error_exit("[ needs +-!");
Rob Landleyc705b952013-09-21 13:46:44 -0500346 if (CFG_TOYBOX_DEBUG && (options[1] == ']' || !options[1]))
Rob Landleydc6db1a2013-09-21 12:24:04 -0500347 error_exit("empty []");
Rob Landley7f909bd2012-11-19 01:49:53 -0600348
349 // Don't advance past ] but do process it once in loop.
Rob Landleyc705b952013-09-21 13:46:44 -0500350 while (*options++ != ']') {
351 struct opts *opt;
Rob Landley7f909bd2012-11-19 01:49:53 -0600352 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 Landley7f909bd2012-11-19 01:49:53 -0600359 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 Landley7f909bd2012-11-19 01:49:53 -0600365 break;
366 }
367 }
368 }
369 }
370 }
Rob Landley763e42b2011-11-30 07:21:23 -0600371}
372
Rob Landley7f909bd2012-11-19 01:49:53 -0600373// Fill out toys.optflags, toys.optargs, and this[] from toys.argv
374
Rob Landley763e42b2011-11-30 07:21:23 -0600375void get_optflags(void)
376{
Rob Landley7aa651a2012-11-13 17:14:08 -0600377 struct getoptflagstate gof;
Rob Landley7f909bd2012-11-19 01:49:53 -0600378 struct opts *catch;
Rob Landley7aa651a2012-11-13 17:14:08 -0600379 long saveflags;
380 char *letters[]={"s",""};
Rob Landley763e42b2011-11-30 07:21:23 -0600381
Rob Landley7aa651a2012-11-13 17:14:08 -0600382 // 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 Landleyb6063de2012-01-29 13:54:13 -0600384
Rob Landley36ffc5a2013-04-14 21:43:22 -0500385 toys.exithelp++;
Rob Landley7aa651a2012-11-13 17:14:08 -0600386 // Allocate memory for optargs
387 saveflags = 0;
388 while (toys.argv[saveflags++]);
389 toys.optargs = xzalloc(sizeof(char *)*saveflags);
Rob Landley763e42b2011-11-30 07:21:23 -0600390
Rob Landley7aa651a2012-11-13 17:14:08 -0600391 parse_optflaglist(&gof);
Rob Landley8324b892006-11-19 02:49:22 -0500392
Rob Landley7aa651a2012-11-13 17:14:08 -0600393 // 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 Landley7f909bd2012-11-19 01:49:53 -0600396 catch = NULL;
Rob Landley8324b892006-11-19 02:49:22 -0500397
Rob Landley7aa651a2012-11-13 17:14:08 -0600398 // Parse this argument
399 if (gof.stopearly>1) goto notflag;
Rob Landley8324b892006-11-19 02:49:22 -0500400
Rob Landley7aa651a2012-11-13 17:14:08 -0600401 gof.nodash_now = 0;
Rob Landley8324b892006-11-19 02:49:22 -0500402
Rob Landley7aa651a2012-11-13 17:14:08 -0600403 // Various things with dashes
404 if (*gof.arg == '-') {
Rob Landley8324b892006-11-19 02:49:22 -0500405
Rob Landley7aa651a2012-11-13 17:14:08 -0600406 // Handle -
407 if (!gof.arg[1]) goto notflag;
408 gof.arg++;
409 if (*gof.arg=='-') {
410 struct longopts *lo;
Rob Landley8324b892006-11-19 02:49:22 -0500411
Rob Landley7aa651a2012-11-13 17:14:08 -0600412 gof.arg++;
413 // Handle --
414 if (!*gof.arg) {
415 gof.stopearly += 2;
Rob Landleya137c3f2013-04-06 19:57:54 -0500416 continue;
Rob Landley7aa651a2012-11-13 17:14:08 -0600417 }
Rob Landley8324b892006-11-19 02:49:22 -0500418
Rob Landleyb7162a42013-09-01 07:50:32 -0500419 // do we match a known --longopt?
Rob Landley7aa651a2012-11-13 17:14:08 -0600420 for (lo = gof.longopts; lo; lo = lo->next) {
421 if (!strncmp(gof.arg, lo->str, lo->len)) {
Rob Landleyb7162a42013-09-01 07:50:32 -0500422 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 Landley7aa651a2012-11-13 17:14:08 -0600426 // It's a match.
Rob Landley7f909bd2012-11-19 01:49:53 -0600427 catch = lo->opt;
Rob Landley7aa651a2012-11-13 17:14:08 -0600428 break;
429 }
430 }
Rob Landleyfdb667e2006-11-24 00:15:21 -0500431
Rob Landley7aa651a2012-11-13 17:14:08 -0600432 // Should we handle this --longopt as a non-option argument?
433 if (!lo && gof.noerror) {
Rob Landleyb7162a42013-09-01 07:50:32 -0500434 gof.arg -= 2;
Rob Landley7aa651a2012-11-13 17:14:08 -0600435 goto notflag;
436 }
Rob Landley1a221d92008-05-17 17:13:26 -0500437
Rob Landley7aa651a2012-11-13 17:14:08 -0600438 // Long option parsed, handle option.
Rob Landley7f909bd2012-11-19 01:49:53 -0600439 gotflag(&gof, catch);
Rob Landley7aa651a2012-11-13 17:14:08 -0600440 continue;
441 }
Rob Landley8324b892006-11-19 02:49:22 -0500442
Rob Landley7aa651a2012-11-13 17:14:08 -0600443 // 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 Landley8324b892006-11-19 02:49:22 -0500448
Rob Landley7aa651a2012-11-13 17:14:08 -0600449 // 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 Landleyfdb667e2006-11-24 00:15:21 -0500453
Rob Landley7aa651a2012-11-13 17:14:08 -0600454 // Identify next option char.
Rob Landley7f909bd2012-11-19 01:49:53 -0600455 for (catch = gof.opts; catch; catch = catch->next)
456 if (*gof.arg == catch->c)
457 if (!((catch->flags&4) && gof.arg[1])) break;
Rob Landleyfdb667e2006-11-24 00:15:21 -0500458
Rob Landley7aa651a2012-11-13 17:14:08 -0600459 // Handle option char (advancing past what was used)
Rob Landley7f909bd2012-11-19 01:49:53 -0600460 if (gotflag(&gof, catch) ) {
Rob Landley7aa651a2012-11-13 17:14:08 -0600461 toys.optflags = saveflags;
462 gof.arg = toys.argv[gof.argc];
463 goto notflag;
464 }
465 }
466 continue;
Rob Landley8324b892006-11-19 02:49:22 -0500467
Rob Landley7aa651a2012-11-13 17:14:08 -0600468 // Not a flag, save value in toys.optargs[]
Rob Landley8324b892006-11-19 02:49:22 -0500469notflag:
Rob Landley7aa651a2012-11-13 17:14:08 -0600470 if (gof.stopearly) gof.stopearly++;
471 toys.optargs[toys.optc++] = toys.argv[gof.argc];
472 }
Rob Landley8324b892006-11-19 02:49:22 -0500473
Rob Landley7aa651a2012-11-13 17:14:08 -0600474 // 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 Landley9e89d472013-07-31 03:24:58 -0500480 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 Landley36ffc5a2013-04-14 21:43:22 -0500490 toys.exithelp = 0;
Rob Landley9e2b6db2012-07-15 17:22:04 -0500491
Rob Landley7aa651a2012-11-13 17:14:08 -0600492 if (CFG_TOYBOX_FREE) {
493 llist_traverse(gof.opts, free);
494 llist_traverse(gof.longopts, free);
495 }
Rob Landley8324b892006-11-19 02:49:22 -0500496}