blob: 20084f36a3c7ae7a0d482356570fe69f12727e34 [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
Rob Landley1e7b49e2015-07-10 14:12:44 -05006// 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 Landley8324b892006-11-19 02:49:22 -050012#include "toys.h"
13
14// Design goals:
Rob Landleydc6db1a2013-09-21 12:24:04 -050015// 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 Landley8324b892006-11-19 02:49:22 -050018
Rob Landley7aa651a2012-11-13 17:14:08 -060019/* This uses a getopt-like option string, but not getopt() itself. We call
Rob Landley61190a32008-02-18 03:32:17 -060020 * it the get_opt string.
Rob Landley2c226852007-11-15 18:30:30 -060021 *
Rob Landley61190a32008-02-18 03:32:17 -060022 * Each option in the get_opt string corresponds to a bit position in the
Rob Landley7aa651a2012-11-13 17:14:08 -060023 * 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 Landley028a5442007-01-25 16:11:23 -050025 *
26 * Options which have an argument fill in the corresponding slot in the global
Rob Landley61190a32008-02-18 03:32:17 -060027 * 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 Landley028a5442007-01-25 16:11:23 -050029 *
30 * You don't have to free the option strings, which point into the environment
Rob Landley7aa651a2012-11-13 17:14:08 -060031 * space. List objects should be freed by main() when command_main() returns.
Rob Landley8324b892006-11-19 02:49:22 -050032 *
33 * Example:
Rob Landley61190a32008-02-18 03:32:17 -060034 * 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 Landleydc6db1a2013-09-21 12:24:04 -050038 * 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 Landley61190a32008-02-18 03:32:17 -060042 *
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 Landley8324b892006-11-19 02:49:22 -050046 */
47
Rob Landleydc6db1a2013-09-21 12:24:04 -050048// 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 Landleyc705b952013-09-21 13:46:44 -050084// + Synonyms (switch on all) [+abc] means -ab=-abc, -c=-abc
Rob Landleydc6db1a2013-09-21 12:24:04 -050085// ! More than one in group is error [!abc] means -ab calls error_exit()
Rob Landleydc6db1a2013-09-21 12:24:04 -050086// primarily useful if you can switch things back off again.
Rob Landleydc6db1a2013-09-21 12:24:04 -050087
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 Landley7f909bd2012-11-19 01:49:53 -060094// Linked list of all known options (option string parsed into this).
Rob Landleydc6db1a2013-09-21 12:24:04 -050095// Hangs off getoptflagstate, freed at end of option parsing.
Rob Landley8324b892006-11-19 02:49:22 -050096struct opts {
Rob Landley7aa651a2012-11-13 17:14:08 -060097 struct opts *next;
98 long *arg; // Pointer into union "this" to store arguments at.
Rob Landley7f909bd2012-11-19 01:49:53 -060099 int c; // Argument character to match
Rob Landleyaaecbba2015-12-10 15:57:08 -0600100 int flags; // |=1, ^=2, " "=4, ;=8
101 unsigned long long dex[3]; // bits to disable/enable/exclude in toys.optflags
Rob Landley7f909bd2012-11-19 01:49:53 -0600102 char type; // Type of arguments to store union "this"
Rob Landley7aa651a2012-11-13 17:14:08 -0600103 union {
104 long l;
105 FLOAT f;
106 } val[3]; // low, high, default - range of allowed values
Rob Landley8324b892006-11-19 02:49:22 -0500107};
108
Rob Landleydc6db1a2013-09-21 12:24:04 -0500109// 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 Landley763e42b2011-11-30 07:21:23 -0600113struct longopts {
Rob Landley7aa651a2012-11-13 17:14:08 -0600114 struct longopts *next;
115 struct opts *opt;
116 char *str;
117 int len;
Rob Landley763e42b2011-11-30 07:21:23 -0600118};
119
Rob Landley61190a32008-02-18 03:32:17 -0600120// State during argument parsing.
121struct getoptflagstate
Rob Landley8324b892006-11-19 02:49:22 -0500122{
Rob Landley7aa651a2012-11-13 17:14:08 -0600123 int argc, minargs, maxargs, nodash;
124 char *arg;
Rob Landley7f909bd2012-11-19 01:49:53 -0600125 struct opts *opts;
Rob Landley7aa651a2012-11-13 17:14:08 -0600126 struct longopts *longopts;
127 int noerror, nodash_now, stopearly;
Rob Landley9e89d472013-07-31 03:24:58 -0500128 unsigned excludes, requires;
Rob Landley61190a32008-02-18 03:32:17 -0600129};
Rob Landley8324b892006-11-19 02:49:22 -0500130
Rob Landley7f909bd2012-11-19 01:49:53 -0600131// Use getoptflagstate to parse parse one command line option from argv
132static int gotflag(struct getoptflagstate *gof, struct opts *opt)
Rob Landley8324b892006-11-19 02:49:22 -0500133{
Rob Landley7aa651a2012-11-13 17:14:08 -0600134 int type;
Rob Landley8324b892006-11-19 02:49:22 -0500135
Rob Landley7aa651a2012-11-13 17:14:08 -0600136 // Did we recognize this option?
137 if (!opt) {
138 if (gof->noerror) return 1;
Rob Landleyd3267c62017-03-23 11:35:09 -0500139 error_exit("Unknown option %s", gof->arg);
Rob Landley7aa651a2012-11-13 17:14:08 -0600140 }
Rob Landley763e42b2011-11-30 07:21:23 -0600141
Rob Landley845c8da2014-08-15 18:53:01 -0500142 // Might enabling this switch off something else?
Rob Landleyc8a49972013-09-06 12:18:46 -0500143 if (toys.optflags & opt->dex[0]) {
144 struct opts *clr;
Rob Landleyaaecbba2015-12-10 15:57:08 -0600145 unsigned long long i = 1;
Rob Landleyc8a49972013-09-06 12:18:46 -0500146
Rob Landley845c8da2014-08-15 18:53:01 -0500147 // Forget saved argument for flag we switch back off
Rob Landley9d4cd462013-09-09 02:44:02 -0500148 for (clr=gof->opts, i=1; clr; clr = clr->next, i<<=1)
Rob Landley845c8da2014-08-15 18:53:01 -0500149 if (clr->arg && (i & toys.optflags & opt->dex[0])) *clr->arg = 0;
Rob Landleyc8a49972013-09-06 12:18:46 -0500150 toys.optflags &= ~opt->dex[0];
151 }
Rob Landley845c8da2014-08-15 18:53:01 -0500152
153 // Set flags
Rob Landley7f909bd2012-11-19 01:49:53 -0600154 toys.optflags |= opt->dex[1];
155 gof->excludes |= opt->dex[2];
Rob Landley7aa651a2012-11-13 17:14:08 -0600156 if (opt->flags&2) gof->stopearly=2;
Rob Landley8324b892006-11-19 02:49:22 -0500157
Rob Landleyde511922012-11-25 14:40:25 -0600158 if (toys.optflags & gof->excludes) {
Rob Landley7f909bd2012-11-19 01:49:53 -0600159 struct opts *bad;
160 unsigned i = 1;
161
Rob Landley4d4ca282017-01-04 01:14:07 -0600162 for (bad=gof->opts, i=1; bad ;bad = bad->next, i<<=1) {
Rob Landley8abf0952012-12-29 03:18:34 -0600163 if (opt == bad || !(i & toys.optflags)) continue;
164 if (toys.optflags & bad->dex[2]) break;
165 }
Rob Landleyd3267c62017-03-23 11:35:09 -0500166 if (bad) error_exit("No '%c' with '%c'", opt->c, bad->c);
Rob Landley7f909bd2012-11-19 01:49:53 -0600167 }
168
Rob Landley7aa651a2012-11-13 17:14:08 -0600169 // Does this option take an argument?
Rob Landleyb7162a42013-09-01 07:50:32 -0500170 if (!gof->arg) {
171 if (opt->flags & 8) return 0;
172 gof->arg = "";
173 } else gof->arg++;
Rob Landley7aa651a2012-11-13 17:14:08 -0600174 type = opt->type;
Rob Landley4d3b3da2013-06-22 15:36:25 -0500175
176 if (type == '@') ++*(opt->arg);
177 else if (type) {
Rob Landley7aa651a2012-11-13 17:14:08 -0600178 char *arg = gof->arg;
Rob Landley58c6c1b2006-11-25 13:34:51 -0500179
Rob Landley7aa651a2012-11-13 17:14:08 -0600180 // 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 Landley763e42b2011-11-30 07:21:23 -0600183
Rob Landleyb7162a42013-09-01 07:50:32 -0500184 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 Landleyd3267c62017-03-23 11:35:09 -0500190 if (opt->c != -1) error_exit("%s-%c", s, opt->c);
Rob Landleyb7162a42013-09-01 07:50:32 -0500191
192 for (lo = gof->longopts; lo->opt != opt; lo = lo->next);
Rob Landleyd3267c62017-03-23 11:35:09 -0500193 error_exit("%s--%.*s", s, lo->len, lo->str);
Rob Landleyb7162a42013-09-01 07:50:32 -0500194 }
Rob Landley8324b892006-11-19 02:49:22 -0500195
Rob Landley7aa651a2012-11-13 17:14:08 -0600196 if (type == ':') *(opt->arg) = (long)arg;
197 else if (type == '*') {
198 struct arg_list **list;
Rob Landley4ac66562008-06-16 19:27:35 -0500199
Rob Landley7aa651a2012-11-13 17:14:08 -0600200 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 Landleyd3267c62017-03-23 11:35:09 -0500207 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 Landleyb6063de2012-01-29 13:54:13 -0600209
Rob Landley7aa651a2012-11-13 17:14:08 -0600210 *(opt->arg) = l;
211 } else if (CFG_TOYBOX_FLOAT && type == '.') {
212 FLOAT *f = (FLOAT *)(opt->arg);
Rob Landleyb6063de2012-01-29 13:54:13 -0600213
Rob Landley7aa651a2012-11-13 17:14:08 -0600214 *f = strtod(arg, &arg);
215 if (opt->val[0].l != LONG_MIN && *f < opt->val[0].f)
Rob Landleyd3267c62017-03-23 11:35:09 -0500216 error_exit("-%c < %lf", opt->c, (double)opt->val[0].f);
Rob Landley7aa651a2012-11-13 17:14:08 -0600217 if (opt->val[1].l != LONG_MAX && *f > opt->val[1].f)
Rob Landleyd3267c62017-03-23 11:35:09 -0500218 error_exit("-%c > %lf", opt->c, (double)opt->val[1].f);
Rob Landley4d3b3da2013-06-22 15:36:25 -0500219 }
Rob Landley8324b892006-11-19 02:49:22 -0500220
Rob Landley7aa651a2012-11-13 17:14:08 -0600221 if (!gof->nodash_now) gof->arg = "";
222 }
Rob Landley58c6c1b2006-11-25 13:34:51 -0500223
Rob Landley7aa651a2012-11-13 17:14:08 -0600224 return 0;
Rob Landley8324b892006-11-19 02:49:22 -0500225}
226
Rob Landley7f909bd2012-11-19 01:49:53 -0600227// 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 Landley763e42b2011-11-30 07:21:23 -0600229void parse_optflaglist(struct getoptflagstate *gof)
Rob Landley8324b892006-11-19 02:49:22 -0500230{
Rob Landley7aa651a2012-11-13 17:14:08 -0600231 char *options = toys.which->options;
232 long *nextarg = (long *)&this;
233 struct opts *new = 0;
Rob Landley7f909bd2012-11-19 01:49:53 -0600234 int idx;
Rob Landley54ebcce2006-11-19 20:35:19 -0500235
Rob Landley7aa651a2012-11-13 17:14:08 -0600236 // Parse option format string
237 memset(gof, 0, sizeof(struct getoptflagstate));
238 gof->maxargs = INT_MAX;
239 if (!options) return;
Rob Landley61190a32008-02-18 03:32:17 -0600240
Rob Landley7aa651a2012-11-13 17:14:08 -0600241 // 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 Landley763e42b2011-11-30 07:21:23 -0600251
Rob Landley7f909bd2012-11-19 01:49:53 -0600252 // Parse option string into a linked list of options with attributes.
Rob Landley763e42b2011-11-30 07:21:23 -0600253
Rob Landley7aa651a2012-11-13 17:14:08 -0600254 if (!*options) gof->stopearly++;
255 while (*options) {
256 char *temp;
Rob Landley7f909bd2012-11-19 01:49:53 -0600257
258 // Option groups come after all options are defined
259 if (*options == '[') break;
Rob Landley763e42b2011-11-30 07:21:23 -0600260
Rob Landley7aa651a2012-11-13 17:14:08 -0600261 // 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 Landley7aa651a2012-11-13 17:14:08 -0600268 }
269 // Each option must start with "(" or an option character. (Bare
270 // longopts only come at the start of the string.)
Rob Landleyb7162a42013-09-01 07:50:32 -0500271 if (*options == '(' && new->c != -1) {
Rob Landley7aa651a2012-11-13 17:14:08 -0600272 char *end;
Rob Landleyb7162a42013-09-01 07:50:32 -0500273 struct longopts *lo;
Rob Landley8324b892006-11-19 02:49:22 -0500274
Rob Landley7aa651a2012-11-13 17:14:08 -0600275 // 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 Landley8324b892006-11-19 02:49:22 -0500278
Rob Landley7aa651a2012-11-13 17:14:08 -0600279 // init a new struct longopts
Rob Landleyb7162a42013-09-01 07:50:32 -0500280 lo = xmalloc(sizeof(struct longopts));
Rob Landley7aa651a2012-11-13 17:14:08 -0600281 lo->next = gof->longopts;
282 lo->opt = new;
283 lo->str = options;
284 lo->len = end-options;
285 gof->longopts = lo;
Rob Landleyb7162a42013-09-01 07:50:32 -0500286 options = ++end;
Rob Landley763e42b2011-11-30 07:21:23 -0600287
Rob Landley7aa651a2012-11-13 17:14:08 -0600288 // Mark this struct opt as used, even when no short opt.
Rob Landleyb7162a42013-09-01 07:50:32 -0500289 if (!new->c) new->c = -1;
290
291 continue;
Rob Landley763e42b2011-11-30 07:21:23 -0600292
Rob Landley7aa651a2012-11-13 17:14:08 -0600293 // If this is the start of a new option that wasn't a longopt,
Rob Landley763e42b2011-11-30 07:21:23 -0600294
Rob Landley7aa651a2012-11-13 17:14:08 -0600295 } 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 Landleyb7162a42013-09-01 07:50:32 -0500299 } else if (-1 != (idx = stridx("|^ ;", *options))) new->flags |= 1<<idx;
Rob Landley7aa651a2012-11-13 17:14:08 -0600300 // 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 Landley8324b892006-11-19 02:49:22 -0500310
Rob Landley7aa651a2012-11-13 17:14:08 -0600311 // 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 Landleyb7162a42013-09-01 07:50:32 -0500315 } else if (new->c) {
316 new = 0;
Rob Landley7aa651a2012-11-13 17:14:08 -0600317 continue;
Rob Landley8324b892006-11-19 02:49:22 -0500318
Rob Landley7aa651a2012-11-13 17:14:08 -0600319 // Claim this option, loop to see what's after it.
320 } else new->c = *options;
Rob Landley8324b892006-11-19 02:49:22 -0500321
Rob Landley7aa651a2012-11-13 17:14:08 -0600322 options++;
323 }
Rob Landley8324b892006-11-19 02:49:22 -0500324
Rob Landley7aa651a2012-11-13 17:14:08 -0600325 // Initialize enable/disable/exclude masks and pointers to store arguments.
Rob Landley7f909bd2012-11-19 01:49:53 -0600326 // (This goes right to left so we need the whole list before we can start.)
327 idx = 0;
Rob Landley7aa651a2012-11-13 17:14:08 -0600328 for (new = gof->opts; new; new = new->next) {
Rob Landleyaaecbba2015-12-10 15:57:08 -0600329 unsigned long long u = 1L<<idx++;
Rob Landley9e89d472013-07-31 03:24:58 -0500330
Rob Landleycf2516a2015-01-01 16:19:40 -0600331 if (new->c == 1) new->c = 0;
Rob Landley9e89d472013-07-31 03:24:58 -0500332 new->dex[1] = u;
333 if (new->flags & 1) gof->requires |= u;
Rob Landley7aa651a2012-11-13 17:14:08 -0600334 if (new->type) {
335 new->arg = (void *)nextarg;
336 *(nextarg++) = new->val[2].l;
337 }
338 }
Rob Landley7f909bd2012-11-19 01:49:53 -0600339
340 // Parse trailing group indicators
341 while (*options) {
342 unsigned bits = 0;
343
Rob Landley6d91e0f2012-12-27 18:44:37 -0600344 if (CFG_TOYBOX_DEBUG && *options != '[') error_exit("trailing %s", options);
Rob Landley7f909bd2012-11-19 01:49:53 -0600345
Rob Landleyc705b952013-09-21 13:46:44 -0500346 idx = stridx("-+!", *++options);
Rob Landley7f909bd2012-11-19 01:49:53 -0600347 if (CFG_TOYBOX_DEBUG && idx == -1) error_exit("[ needs +-!");
Rob Landleyc705b952013-09-21 13:46:44 -0500348 if (CFG_TOYBOX_DEBUG && (options[1] == ']' || !options[1]))
Rob Landleydc6db1a2013-09-21 12:24:04 -0500349 error_exit("empty []");
Rob Landley7f909bd2012-11-19 01:49:53 -0600350
351 // Don't advance past ] but do process it once in loop.
Rob Landleyc705b952013-09-21 13:46:44 -0500352 while (*options++ != ']') {
353 struct opts *opt;
Rob Landley7f909bd2012-11-19 01:49:53 -0600354 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 Landley7f909bd2012-11-19 01:49:53 -0600361 if (bits&(1<<i)) opt->dex[idx] |= bits&~(1<<i);
362 } else {
Rob Landley0ed17f12016-07-04 08:32:42 -0500363 if (*options==1) break;
Rob Landley7f909bd2012-11-19 01:49:53 -0600364 if (CFG_TOYBOX_DEBUG && !opt)
365 error_exit("[] unknown target %c", *options);
366 if (opt->c == *options) {
367 bits |= 1<<i;
Rob Landley7f909bd2012-11-19 01:49:53 -0600368 break;
369 }
370 }
371 }
372 }
373 }
Rob Landley763e42b2011-11-30 07:21:23 -0600374}
375
Rob Landley7f909bd2012-11-19 01:49:53 -0600376// Fill out toys.optflags, toys.optargs, and this[] from toys.argv
377
Rob Landley763e42b2011-11-30 07:21:23 -0600378void get_optflags(void)
379{
Rob Landley7aa651a2012-11-13 17:14:08 -0600380 struct getoptflagstate gof;
Rob Landley7f909bd2012-11-19 01:49:53 -0600381 struct opts *catch;
Rob Landleyaaecbba2015-12-10 15:57:08 -0600382 unsigned long long saveflags;
Rob Landley7aa651a2012-11-13 17:14:08 -0600383 char *letters[]={"s",""};
Rob Landley763e42b2011-11-30 07:21:23 -0600384
Rob Landley7aa651a2012-11-13 17:14:08 -0600385 // 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 Landleyb6063de2012-01-29 13:54:13 -0600387
Rob Landley7aa651a2012-11-13 17:14:08 -0600388 // Allocate memory for optargs
389 saveflags = 0;
390 while (toys.argv[saveflags++]);
391 toys.optargs = xzalloc(sizeof(char *)*saveflags);
Rob Landley763e42b2011-11-30 07:21:23 -0600392
Rob Landley7aa651a2012-11-13 17:14:08 -0600393 parse_optflaglist(&gof);
Rob Landley8324b892006-11-19 02:49:22 -0500394
Rob Landley7aa651a2012-11-13 17:14:08 -0600395 // 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 Landley7f909bd2012-11-19 01:49:53 -0600398 catch = NULL;
Rob Landley8324b892006-11-19 02:49:22 -0500399
Rob Landley7aa651a2012-11-13 17:14:08 -0600400 // Parse this argument
401 if (gof.stopearly>1) goto notflag;
Rob Landley8324b892006-11-19 02:49:22 -0500402
Rob Landley7aa651a2012-11-13 17:14:08 -0600403 gof.nodash_now = 0;
Rob Landley8324b892006-11-19 02:49:22 -0500404
Rob Landley7aa651a2012-11-13 17:14:08 -0600405 // Various things with dashes
406 if (*gof.arg == '-') {
Rob Landley8324b892006-11-19 02:49:22 -0500407
Rob Landley7aa651a2012-11-13 17:14:08 -0600408 // Handle -
409 if (!gof.arg[1]) goto notflag;
410 gof.arg++;
411 if (*gof.arg=='-') {
412 struct longopts *lo;
Rob Landley8324b892006-11-19 02:49:22 -0500413
Rob Landley7aa651a2012-11-13 17:14:08 -0600414 gof.arg++;
415 // Handle --
416 if (!*gof.arg) {
417 gof.stopearly += 2;
Rob Landleya137c3f2013-04-06 19:57:54 -0500418 continue;
Rob Landley7aa651a2012-11-13 17:14:08 -0600419 }
Rob Landley8324b892006-11-19 02:49:22 -0500420
Rob Landleyb7162a42013-09-01 07:50:32 -0500421 // do we match a known --longopt?
Rob Landley7aa651a2012-11-13 17:14:08 -0600422 for (lo = gof.longopts; lo; lo = lo->next) {
423 if (!strncmp(gof.arg, lo->str, lo->len)) {
Rob Landleyb7162a42013-09-01 07:50:32 -0500424 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 Landley7aa651a2012-11-13 17:14:08 -0600428 // It's a match.
Rob Landley7f909bd2012-11-19 01:49:53 -0600429 catch = lo->opt;
Rob Landley7aa651a2012-11-13 17:14:08 -0600430 break;
431 }
432 }
Rob Landleyfdb667e2006-11-24 00:15:21 -0500433
Rob Landley7aa651a2012-11-13 17:14:08 -0600434 // Should we handle this --longopt as a non-option argument?
435 if (!lo && gof.noerror) {
Rob Landleyb7162a42013-09-01 07:50:32 -0500436 gof.arg -= 2;
Rob Landley7aa651a2012-11-13 17:14:08 -0600437 goto notflag;
438 }
Rob Landley1a221d92008-05-17 17:13:26 -0500439
Rob Landley7aa651a2012-11-13 17:14:08 -0600440 // Long option parsed, handle option.
Rob Landley7f909bd2012-11-19 01:49:53 -0600441 gotflag(&gof, catch);
Rob Landley7aa651a2012-11-13 17:14:08 -0600442 continue;
443 }
Rob Landley8324b892006-11-19 02:49:22 -0500444
Rob Landley7aa651a2012-11-13 17:14:08 -0600445 // 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 Landley8324b892006-11-19 02:49:22 -0500450
Rob Landley7aa651a2012-11-13 17:14:08 -0600451 // 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 Landleyfdb667e2006-11-24 00:15:21 -0500455
Rob Landley7aa651a2012-11-13 17:14:08 -0600456 // Identify next option char.
Rob Landley7f909bd2012-11-19 01:49:53 -0600457 for (catch = gof.opts; catch; catch = catch->next)
458 if (*gof.arg == catch->c)
459 if (!((catch->flags&4) && gof.arg[1])) break;
Rob Landleyfdb667e2006-11-24 00:15:21 -0500460
Rob Landley7aa651a2012-11-13 17:14:08 -0600461 // Handle option char (advancing past what was used)
Rob Landley7f909bd2012-11-19 01:49:53 -0600462 if (gotflag(&gof, catch) ) {
Rob Landley7aa651a2012-11-13 17:14:08 -0600463 toys.optflags = saveflags;
464 gof.arg = toys.argv[gof.argc];
465 goto notflag;
466 }
467 }
468 continue;
Rob Landley8324b892006-11-19 02:49:22 -0500469
Rob Landley7aa651a2012-11-13 17:14:08 -0600470 // Not a flag, save value in toys.optargs[]
Rob Landley8324b892006-11-19 02:49:22 -0500471notflag:
Rob Landley7aa651a2012-11-13 17:14:08 -0600472 if (gof.stopearly) gof.stopearly++;
473 toys.optargs[toys.optc++] = toys.argv[gof.argc];
474 }
Rob Landley8324b892006-11-19 02:49:22 -0500475
Rob Landley7aa651a2012-11-13 17:14:08 -0600476 // Sanity check
477 if (toys.optc<gof.minargs)
Rob Landleyd3267c62017-03-23 11:35:09 -0500478 error_exit("Need%s %d argument%s", letters[!!(gof.minargs-1)],
Rob Landley7aa651a2012-11-13 17:14:08 -0600479 gof.minargs, letters[!(gof.minargs-1)]);
480 if (toys.optc>gof.maxargs)
Rob Landleyd3267c62017-03-23 11:35:09 -0500481 error_exit("Max %d argument%s", gof.maxargs, letters[!(gof.maxargs-1)]);
Rob Landley9e89d472013-07-31 03:24:58 -0500482 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 Landleyd3267c62017-03-23 11:35:09 -0500490 error_exit("Needs %s-%s", s[1] ? "one of " : "", needs);
Rob Landley9e89d472013-07-31 03:24:58 -0500491 }
Rob Landley9e2b6db2012-07-15 17:22:04 -0500492
Rob Landley7aa651a2012-11-13 17:14:08 -0600493 if (CFG_TOYBOX_FREE) {
494 llist_traverse(gof.opts, free);
495 llist_traverse(gof.longopts, free);
496 }
Rob Landley8324b892006-11-19 02:49:22 -0500497}