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