blob: 88e8bfe2df9b16dd5f60dd240e30719d901b7a45 [file] [log] [blame]
Rob Landley8324b892006-11-19 02:49:22 -05001/* vi: set sw=4 ts=4 :
2 * args.c - Command line argument parsing.
3 *
4 * Copyright 2006 Rob Landley <rob@landley.net>
5 */
6
7#include "toys.h"
8
9// Design goals:
10// Don't use getopt()
11// Don't permute original arguments.
12// handle --long gracefully "(noshort)a(along)b(blong1)(blong2)"
13// After each argument:
14// Note that pointer and long are always the same size, even on 64 bit.
15// : plus a string argument, keep most recent if more than one
16// * plus a string argument, appended to a list
Rob Landley028a5442007-01-25 16:11:23 -050017// # plus a signed long argument (TODO: Bounds checking?)
Rob Landley8324b892006-11-19 02:49:22 -050018// @ plus an occurrence counter (which is a long)
Rob Landley8324b892006-11-19 02:49:22 -050019// (longopt)
Rob Landley61190a32008-02-18 03:32:17 -060020// | this is required. If more than one marked, only one required.
Rob Landley1a35c472008-07-03 19:14:23 -050021// ^ Stop parsing after encountering this argument
Rob Landley61190a32008-02-18 03:32:17 -060022//
23// These modify other option letters (previously seen in string):
24// +X enabling this enables X (switch on)
25// ~X enabling this disables X (switch off)
26// !X die with error if X already set (x!x die if x supplied twice)
27// [yz] needs at least one of y or z.
Rob Landley8324b892006-11-19 02:49:22 -050028// at the beginning:
Rob Landleyb1487dc2008-06-26 22:48:43 -050029// ^ stop at first nonoption argument
Rob Landley8324b892006-11-19 02:49:22 -050030// <0 at least # leftover arguments needed (default 0)
31// >9 at most # leftover arguments needed (default MAX_INT)
Rob Landley028a5442007-01-25 16:11:23 -050032// ? don't show_usage() on unknown argument.
Rob Landley8324b892006-11-19 02:49:22 -050033// & first argument has imaginary dash (ala tar/ps)
34// If given twice, all arguments have imaginary dash
35
36// Notes from getopt man page
37// - and -- cannot be arguments.
38// -- force end of arguments
39// - is a synonym for stdin in file arguments
40// -abc means -a -b -c
41
Rob Landley61190a32008-02-18 03:32:17 -060042/* This uses a getopt-like option string, but not getopt() itself. We call
43 * it the get_opt string.
Rob Landley2c226852007-11-15 18:30:30 -060044 *
Rob Landley61190a32008-02-18 03:32:17 -060045 * Each option in the get_opt string corresponds to a bit position in the
46 * return value. The rightmost argument is (1<<0), the next to last is (1<<1)
47 * and so on. If the option isn't seen in argv[], its bit remains 0.
Rob Landley028a5442007-01-25 16:11:23 -050048 *
49 * Options which have an argument fill in the corresponding slot in the global
Rob Landley61190a32008-02-18 03:32:17 -060050 * union "this" (see generated/globals.h), which it treats as an array of longs
51 * (note that sizeof(long)==sizeof(pointer) is guaranteed by LP64).
Rob Landley028a5442007-01-25 16:11:23 -050052 *
53 * You don't have to free the option strings, which point into the environment
Rob Landley61190a32008-02-18 03:32:17 -060054 * space. List objects should be freed by main() when command_main() returns.
Rob Landley8324b892006-11-19 02:49:22 -050055 *
56 * Example:
Rob Landley61190a32008-02-18 03:32:17 -060057 * Calling get_optflags() when toys.which->options="ab:c:d" and
58 * argv = ["command", "-b", "fruit", "-d", "walrus"] results in:
59 *
60 * Changes to struct toys:
61 * toys.optflags = 5 (-b=4 | -d=1)
62 * toys.optargs[0]="walrus" (leftover argument)
63 * toys.optargs[1]=NULL (end of list)
64 * toys.optc=1 (there was 1 leftover argument)
65 *
66 * Changes to union this:
67 * this[0]=NULL (because -c didn't get an argument this time)
68 * this[1]="fruit" (argument to -b)
Rob Landley8324b892006-11-19 02:49:22 -050069 */
70
Rob Landley61190a32008-02-18 03:32:17 -060071// Linked list of all known options (get_opt string is parsed into this).
Rob Landley8324b892006-11-19 02:49:22 -050072struct opts {
73 struct opts *next;
Rob Landley1a35c472008-07-03 19:14:23 -050074 long *arg; // Pointer into union "this" to store arguments at.
Rob Landley61190a32008-02-18 03:32:17 -060075 uint32_t edx[3]; // Flag mask to enable/disable/exclude.
Rob Landley61190a32008-02-18 03:32:17 -060076 int c; // Short argument character
Rob Landley1a35c472008-07-03 19:14:23 -050077 int flags; // |=1, ^=2
Rob Landley61190a32008-02-18 03:32:17 -060078 char type; // Type of arguments to store
Rob Landley8324b892006-11-19 02:49:22 -050079};
80
Rob Landley61190a32008-02-18 03:32:17 -060081// State during argument parsing.
82struct getoptflagstate
Rob Landley8324b892006-11-19 02:49:22 -050083{
84 int argc;
85 char *arg;
86 struct opts *opts, *this;
Rob Landley1a35c472008-07-03 19:14:23 -050087 int noerror, nodash_now, stopearly;
Rob Landley61190a32008-02-18 03:32:17 -060088 uint32_t excludes;
89};
Rob Landley8324b892006-11-19 02:49:22 -050090
Rob Landley61190a32008-02-18 03:32:17 -060091// Parse one command line option.
92
Rob Landley1a221d92008-05-17 17:13:26 -050093static int gotflag(struct getoptflagstate *gof)
Rob Landley8324b892006-11-19 02:49:22 -050094{
Rob Landley8324b892006-11-19 02:49:22 -050095 int type;
Rob Landley61190a32008-02-18 03:32:17 -060096 struct opts *opt = gof->this;
Rob Landley8324b892006-11-19 02:49:22 -050097
98 // Did we recognize this option?
Rob Landley61190a32008-02-18 03:32:17 -060099 if (!opt) {
Rob Landley1a221d92008-05-17 17:13:26 -0500100 if (gof->noerror) return 1;
Rob Landley61190a32008-02-18 03:32:17 -0600101 error_exit("Unknown option %s", gof->arg);
102 }
103 toys.optflags |= opt->edx[0];
104 toys.optflags &= ~opt->edx[1];
105 gof->excludes = opt->edx[2];
Rob Landley1a35c472008-07-03 19:14:23 -0500106 if (opt->flags&2) gof->stopearly=2;
Rob Landley8324b892006-11-19 02:49:22 -0500107
108 // Does this option take an argument?
Rob Landley61190a32008-02-18 03:32:17 -0600109 gof->arg++;
110 type = opt->type;
Rob Landley58c6c1b2006-11-25 13:34:51 -0500111 if (type) {
112
113 // Handle "-xblah" and "-x blah", but also a third case: "abxc blah"
114 // to make "tar xCjfv blah1 blah2 thingy" work like
Rob Landley8324b892006-11-19 02:49:22 -0500115 // "tar -x -C blah1 -j -f blah2 -v thingy"
Rob Landley61190a32008-02-18 03:32:17 -0600116 if (!gof->nodash_now && !gof->arg[0]) {
117 gof->arg = toys.argv[++gof->argc];
118 // TODO: The following line doesn't display --longopt correctly
119 if (!gof->arg) error_exit("Missing argument to -%c",opt->c);
Rob Landley8324b892006-11-19 02:49:22 -0500120 }
Rob Landley8324b892006-11-19 02:49:22 -0500121
Rob Landley58c6c1b2006-11-25 13:34:51 -0500122 // Grab argument.
Rob Landley61190a32008-02-18 03:32:17 -0600123 if (!gof->arg && !(gof->arg = toys.argv[++(gof->argc)]))
Rob Landleyfdb667e2006-11-24 00:15:21 -0500124 error_exit("Missing argument");
Rob Landley61190a32008-02-18 03:32:17 -0600125 if (type == ':') *(opt->arg) = (long)gof->arg;
Rob Landleyfdb667e2006-11-24 00:15:21 -0500126 else if (type == '*') {
Rob Landley4ac66562008-06-16 19:27:35 -0500127 struct arg_list **list;
128
Rob Landley61190a32008-02-18 03:32:17 -0600129 list = (struct arg_list **)opt->arg;
Rob Landley4ac66562008-06-16 19:27:35 -0500130 while (*list) list=&((*list)->next);
131 *list = xzalloc(sizeof(struct arg_list));
132 (*list)->arg = gof->arg;
Rob Landley61190a32008-02-18 03:32:17 -0600133 } else if (type == '#') *(opt->arg) = atolx((char *)gof->arg);
Rob Landley4ac66562008-06-16 19:27:35 -0500134 else if (type == '@') ++*(opt->arg);
Rob Landley8324b892006-11-19 02:49:22 -0500135
Rob Landley61190a32008-02-18 03:32:17 -0600136 gof->arg = "";
Rob Landleyfdb667e2006-11-24 00:15:21 -0500137 }
Rob Landley58c6c1b2006-11-25 13:34:51 -0500138
Rob Landley61190a32008-02-18 03:32:17 -0600139 gof->this = NULL;
Rob Landley1a221d92008-05-17 17:13:26 -0500140 return 0;
Rob Landley8324b892006-11-19 02:49:22 -0500141}
142
Rob Landley61190a32008-02-18 03:32:17 -0600143// Fill out toys.optflags and toys.optargs.
Rob Landley8324b892006-11-19 02:49:22 -0500144
Rob Landley61190a32008-02-18 03:32:17 -0600145static char *plustildenot = "+~!";
Rob Landley8324b892006-11-19 02:49:22 -0500146void get_optflags(void)
147{
Rob Landley1a35c472008-07-03 19:14:23 -0500148 int nodash = 0, minargs = 0, maxargs;
Rob Landley8324b892006-11-19 02:49:22 -0500149 struct longopts {
150 struct longopts *next;
151 struct opts *opt;
152 char *str;
153 int len;
154 } *longopts = NULL;
Rob Landley61190a32008-02-18 03:32:17 -0600155 struct getoptflagstate gof;
Rob Landley1a221d92008-05-17 17:13:26 -0500156 long *nextarg = (long *)&this, saveflags;
Rob Landley8324b892006-11-19 02:49:22 -0500157 char *options = toys.which->options;
Rob Landley8e998742008-05-04 18:52:29 -0500158 char *letters[]={"s",""};
Rob Landley8324b892006-11-19 02:49:22 -0500159
Rob Landleyd06c58d2007-10-11 15:36:36 -0500160 if (CFG_HELP) toys.exithelp++;
Rob Landley54ebcce2006-11-19 20:35:19 -0500161 // Allocate memory for optargs
162 maxargs = 0;
163 while (toys.argv[maxargs++]);
164 toys.optargs = xzalloc(sizeof(char *)*maxargs);
165 maxargs = INT_MAX;
Rob Landley61190a32008-02-18 03:32:17 -0600166 bzero(&gof, sizeof(struct getoptflagstate));
Rob Landley54ebcce2006-11-19 20:35:19 -0500167
168 // Parse option format
Rob Landley2a813ff2006-11-19 17:29:35 -0500169 if (options) {
Rob Landley61190a32008-02-18 03:32:17 -0600170
Rob Landley2a813ff2006-11-19 17:29:35 -0500171 // Parse leading special behavior indicators
172 for (;;) {
Rob Landley1a35c472008-07-03 19:14:23 -0500173 if (*options == '^') gof.stopearly++;
Rob Landley2a813ff2006-11-19 17:29:35 -0500174 else if (*options == '<') minargs=*(++options)-'0';
175 else if (*options == '>') maxargs=*(++options)-'0';
Rob Landley028a5442007-01-25 16:11:23 -0500176 else if (*options == '?') gof.noerror++;
Rob Landley2a813ff2006-11-19 17:29:35 -0500177 else if (*options == '&') nodash++;
178 else break;
179 options++;
Rob Landley8324b892006-11-19 02:49:22 -0500180 }
Rob Landley8324b892006-11-19 02:49:22 -0500181
Rob Landley1a35c472008-07-03 19:14:23 -0500182 if (!*options) gof.stopearly++;
Rob Landley2a813ff2006-11-19 17:29:35 -0500183 // Parse rest of opts into array
184 while (*options) {
Rob Landleyaba353e2008-03-24 00:32:25 -0500185 char *temp;
Rob Landley8324b892006-11-19 02:49:22 -0500186
Rob Landley2a813ff2006-11-19 17:29:35 -0500187 // Allocate a new option entry when necessary
188 if (!gof.this) {
189 gof.this = xzalloc(sizeof(struct opts));
190 gof.this->next = gof.opts;
191 gof.opts = gof.this;
Rob Landley61190a32008-02-18 03:32:17 -0600192 ++*(gof.this->edx);
Rob Landley2a813ff2006-11-19 17:29:35 -0500193 }
Rob Landley61190a32008-02-18 03:32:17 -0600194 // Each option must start with "(" or an option character. (Bare
Rob Landley2a813ff2006-11-19 17:29:35 -0500195 // longopts only come at the start of the string.)
196 if (*options == '(') {
197 char *end;
198 struct longopts *lo = xmalloc(sizeof(struct longopts));
Rob Landley8324b892006-11-19 02:49:22 -0500199
Rob Landley2a813ff2006-11-19 17:29:35 -0500200 // Find the end of the longopt
201 for (end = ++options; *end && *end != ')'; end++);
Rob Landleyde05a702007-01-31 14:37:01 -0500202 if (CFG_TOYBOX_DEBUG && !*end)
Rob Landley61190a32008-02-18 03:32:17 -0600203 error_exit("Bug1 in get_opt");
Rob Landley8324b892006-11-19 02:49:22 -0500204
Rob Landley2a813ff2006-11-19 17:29:35 -0500205 // Allocate and init a new struct longopts
206 lo = xmalloc(sizeof(struct longopts));
207 lo->next = longopts;
208 lo->opt = gof.this;
209 lo->str = options;
210 lo->len = end-options;
211 longopts = lo;
212 options = end;
Rob Landley8324b892006-11-19 02:49:22 -0500213
Rob Landley61190a32008-02-18 03:32:17 -0600214 // Mark this as struct opt as used, even when no short opt.
215 if (!gof.this->c) gof.this->c = -1;
Rob Landley8324b892006-11-19 02:49:22 -0500216
Rob Landley2a813ff2006-11-19 17:29:35 -0500217 // If this is the start of a new option that wasn't a longopt,
Rob Landley8324b892006-11-19 02:49:22 -0500218
Rob Landley61190a32008-02-18 03:32:17 -0600219 } else if (strchr(":*#@", *options)) {
220 gof.this->type = *options;
221 } else if (0 != (temp = strchr(plustildenot, *options))) {
222 int i=0, idx = temp - plustildenot;
223 struct opts *opt;
224
Rob Landleyaba353e2008-03-24 00:32:25 -0500225 if (!*++options && CFG_TOYBOX_DEBUG)
Rob Landley61190a32008-02-18 03:32:17 -0600226 error_exit("Bug2 in get_opt");
227 // Find this option flag (in previously parsed struct opt)
228 for (opt = gof.this; ; opt = opt->next) {
229 if (CFG_TOYBOX_DEBUG && !opt) error_exit("Bug3 in get_opt");
230 if (opt->c == *options) break;
231 i++;
232 }
233 gof.this->edx[idx] |= 1<<i;
234
Rob Landley2a813ff2006-11-19 17:29:35 -0500235 } else if (*options == '[') {
Rob Landley1a35c472008-07-03 19:14:23 -0500236 } else if (*options == '|') gof.this->flags |= 1;
237 else if (*options == '^') gof.this->flags |= 2;
Rob Landley8324b892006-11-19 02:49:22 -0500238
Rob Landley2a813ff2006-11-19 17:29:35 -0500239 // At this point, we've hit the end of the previous option. The
240 // current character is the start of a new option. If we've already
241 // assigned an option to this struct, loop to allocate a new one.
Rob Landley61190a32008-02-18 03:32:17 -0600242 // (It'll get back here afterwards and fall through to next else.)
Rob Landley1a35c472008-07-03 19:14:23 -0500243 else if(gof.this->c) {
Rob Landley2a813ff2006-11-19 17:29:35 -0500244 gof.this = NULL;
245 continue;
246
247 // Claim this option, loop to see what's after it.
248 } else gof.this->c = *options;
249
250 options++;
251 }
Rob Landley8324b892006-11-19 02:49:22 -0500252 }
253
Rob Landley61190a32008-02-18 03:32:17 -0600254 // Initialize enable/disable/exclude masks and pointers to store arguments.
255 // (We have to calculate all this ahead of time because longopts jump into
256 // the middle of the list.)
Rob Landley8324b892006-11-19 02:49:22 -0500257 gof.argc = 0;
Rob Landley028a5442007-01-25 16:11:23 -0500258 for (gof.this = gof.opts; gof.this; gof.this = gof.this->next) {
Rob Landley61190a32008-02-18 03:32:17 -0600259 int i;
260
261 for (i=0;i<3;i++) gof.this->edx[i] <<= gof.argc;
262 gof.argc++;
263 if (gof.this->type) {
Rob Landley028a5442007-01-25 16:11:23 -0500264 gof.this->arg = (void *)nextarg;
265 *(nextarg++) = 0;
266 }
267 }
Rob Landley8324b892006-11-19 02:49:22 -0500268
269 // Iterate through command line arguments, skipping argv[0]
270 for (gof.argc=1; toys.argv[gof.argc]; gof.argc++) {
Rob Landleyfdb667e2006-11-24 00:15:21 -0500271 gof.arg = toys.argv[gof.argc];
272 gof.this = NULL;
Rob Landley8324b892006-11-19 02:49:22 -0500273
274 // Parse this argument
Rob Landley1a35c472008-07-03 19:14:23 -0500275 if (gof.stopearly>1) goto notflag;
Rob Landley8324b892006-11-19 02:49:22 -0500276
277 gof.nodash_now = 0;
278
279 // Various things with dashes
Rob Landleyfdb667e2006-11-24 00:15:21 -0500280 if (*gof.arg == '-') {
Rob Landley8324b892006-11-19 02:49:22 -0500281
282 // Handle -
Rob Landleyfdb667e2006-11-24 00:15:21 -0500283 if (!gof.arg[1]) goto notflag;
284 gof.arg++;
285 if (*gof.arg=='-') {
Rob Landley8324b892006-11-19 02:49:22 -0500286 struct longopts *lo;
287
Rob Landleyfdb667e2006-11-24 00:15:21 -0500288 gof.arg++;
Rob Landley8324b892006-11-19 02:49:22 -0500289 // Handle --
Rob Landleyfdb667e2006-11-24 00:15:21 -0500290 if (!*gof.arg) {
Rob Landley1a35c472008-07-03 19:14:23 -0500291 gof.stopearly += 2;
Rob Landley8324b892006-11-19 02:49:22 -0500292 goto notflag;
293 }
294 // Handle --longopt
295
296 for (lo = longopts; lo; lo = lo->next) {
Rob Landleyfdb667e2006-11-24 00:15:21 -0500297 if (!strncmp(gof.arg, lo->str, lo->len)) {
298 if (gof.arg[lo->len]) {
Rob Landley61190a32008-02-18 03:32:17 -0600299 if (gof.arg[lo->len]=='=' && lo->opt->type)
Rob Landleyfdb667e2006-11-24 00:15:21 -0500300 gof.arg += lo->len;
Rob Landley61190a32008-02-18 03:32:17 -0600301 else continue;
Rob Landleyfdb667e2006-11-24 00:15:21 -0500302 }
303 // It's a match.
304 gof.arg = "";
Rob Landley8324b892006-11-19 02:49:22 -0500305 gof.this = lo->opt;
306 break;
307 }
308 }
Rob Landleyfdb667e2006-11-24 00:15:21 -0500309
Rob Landley1a221d92008-05-17 17:13:26 -0500310 // Should we handle this --longopt as a non-option argument?
311 if (!lo && gof.noerror) {
312 gof.arg-=2;
313 goto notflag;
314 }
315
Rob Landleyfdb667e2006-11-24 00:15:21 -0500316 // Long option parsed, handle option.
Rob Landley61190a32008-02-18 03:32:17 -0600317 gotflag(&gof);
Rob Landley8324b892006-11-19 02:49:22 -0500318 continue;
319 }
320
321 // Handle things that don't start with a dash.
322 } else {
323 if (nodash && (nodash>1 || gof.argc == 1)) gof.nodash_now = 1;
324 else goto notflag;
325 }
326
327 // At this point, we have the args part of -args. Loop through
328 // each entry (could be -abc meaning -a -b -c)
Rob Landley1a221d92008-05-17 17:13:26 -0500329 saveflags = toys.optflags;
Rob Landleyfdb667e2006-11-24 00:15:21 -0500330 while (*gof.arg) {
331
Rob Landley8324b892006-11-19 02:49:22 -0500332 // Identify next option char.
Rob Landleyfdb667e2006-11-24 00:15:21 -0500333 for (gof.this = gof.opts; gof.this; gof.this = gof.this->next)
334 if (*gof.arg == gof.this->c) break;
335
336 // Handle option char (advancing past what was used)
Rob Landley1a221d92008-05-17 17:13:26 -0500337 if (gotflag(&gof) ) {
338 toys.optflags = saveflags;
339 gof.arg = toys.argv[gof.argc];
340 goto notflag;
341 }
Rob Landley8324b892006-11-19 02:49:22 -0500342 }
343 continue;
344
345 // Not a flag, save value in toys.optargs[]
346notflag:
Rob Landley1a35c472008-07-03 19:14:23 -0500347 if (gof.stopearly) gof.stopearly++;
Rob Landley26bf9e62008-02-12 17:36:13 -0600348 toys.optargs[toys.optc++] = toys.argv[gof.argc];
Rob Landley8324b892006-11-19 02:49:22 -0500349 }
350
351 // Sanity check
Rob Landley8e998742008-05-04 18:52:29 -0500352 if (toys.optc<minargs) {
353 error_exit("Need%s %d argument%s", letters[!!(minargs-1)], minargs,
354 letters[!(minargs-1)]);
355 }
Rob Landley26bf9e62008-02-12 17:36:13 -0600356 if (toys.optc>maxargs)
Rob Landley8e998742008-05-04 18:52:29 -0500357 error_exit("Max %d argument%s", maxargs, letters[!(maxargs-1)]);
Rob Landleyd06c58d2007-10-11 15:36:36 -0500358 if (CFG_HELP) toys.exithelp = 0;
Rob Landley8324b892006-11-19 02:49:22 -0500359}