args.c 17 KB

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