blob: ee915b46d5beb08a8c714a94c9d770df95e4e60f [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.
21//
22// These modify other option letters (previously seen in string):
23// +X enabling this enables X (switch on)
24// ~X enabling this disables X (switch off)
25// !X die with error if X already set (x!x die if x supplied twice)
26// [yz] needs at least one of y or z.
Rob Landley8324b892006-11-19 02:49:22 -050027// at the beginning:
28// + stop at first nonoption argument
Rob Landley8324b892006-11-19 02:49:22 -050029// <0 at least # leftover arguments needed (default 0)
30// >9 at most # leftover arguments needed (default MAX_INT)
Rob Landley028a5442007-01-25 16:11:23 -050031// ? don't show_usage() on unknown argument.
Rob Landley8324b892006-11-19 02:49:22 -050032// & first argument has imaginary dash (ala tar/ps)
33// If given twice, all arguments have imaginary dash
34
35// Notes from getopt man page
36// - and -- cannot be arguments.
37// -- force end of arguments
38// - is a synonym for stdin in file arguments
39// -abc means -a -b -c
40
Rob Landley61190a32008-02-18 03:32:17 -060041/* This uses a getopt-like option string, but not getopt() itself. We call
42 * it the get_opt string.
Rob Landley2c226852007-11-15 18:30:30 -060043 *
Rob Landley61190a32008-02-18 03:32:17 -060044 * Each option in the get_opt string corresponds to a bit position in the
45 * return value. The rightmost argument is (1<<0), the next to last is (1<<1)
46 * and so on. If the option isn't seen in argv[], its bit remains 0.
Rob Landley028a5442007-01-25 16:11:23 -050047 *
48 * Options which have an argument fill in the corresponding slot in the global
Rob Landley61190a32008-02-18 03:32:17 -060049 * union "this" (see generated/globals.h), which it treats as an array of longs
50 * (note that sizeof(long)==sizeof(pointer) is guaranteed by LP64).
Rob Landley028a5442007-01-25 16:11:23 -050051 *
52 * You don't have to free the option strings, which point into the environment
Rob Landley61190a32008-02-18 03:32:17 -060053 * space. List objects should be freed by main() when command_main() returns.
Rob Landley8324b892006-11-19 02:49:22 -050054 *
55 * Example:
Rob Landley61190a32008-02-18 03:32:17 -060056 * Calling get_optflags() when toys.which->options="ab:c:d" and
57 * argv = ["command", "-b", "fruit", "-d", "walrus"] results in:
58 *
59 * Changes to struct toys:
60 * toys.optflags = 5 (-b=4 | -d=1)
61 * toys.optargs[0]="walrus" (leftover argument)
62 * toys.optargs[1]=NULL (end of list)
63 * toys.optc=1 (there was 1 leftover argument)
64 *
65 * Changes to union this:
66 * this[0]=NULL (because -c didn't get an argument this time)
67 * this[1]="fruit" (argument to -b)
Rob Landley8324b892006-11-19 02:49:22 -050068 */
69
Rob Landley61190a32008-02-18 03:32:17 -060070// Linked list of all known options (get_opt string is parsed into this).
Rob Landley8324b892006-11-19 02:49:22 -050071struct opts {
72 struct opts *next;
Rob Landley61190a32008-02-18 03:32:17 -060073 uint32_t edx[3]; // Flag mask to enable/disable/exclude.
74 long *arg; // Pointer into union this to store arguments at.
75 int c; // Short argument character
76 char type; // Type of arguments to store
Rob Landley8324b892006-11-19 02:49:22 -050077};
78
Rob Landley61190a32008-02-18 03:32:17 -060079// State during argument parsing.
80struct getoptflagstate
Rob Landley8324b892006-11-19 02:49:22 -050081{
82 int argc;
83 char *arg;
84 struct opts *opts, *this;
85 int noerror, nodash_now;
Rob Landley61190a32008-02-18 03:32:17 -060086 uint32_t excludes;
87};
Rob Landley8324b892006-11-19 02:49:22 -050088
Rob Landley61190a32008-02-18 03:32:17 -060089// Parse one command line option.
90
Rob Landley1a221d92008-05-17 17:13:26 -050091static int gotflag(struct getoptflagstate *gof)
Rob Landley8324b892006-11-19 02:49:22 -050092{
Rob Landley8324b892006-11-19 02:49:22 -050093 int type;
Rob Landley61190a32008-02-18 03:32:17 -060094 struct opts *opt = gof->this;
Rob Landley8324b892006-11-19 02:49:22 -050095
96 // Did we recognize this option?
Rob Landley61190a32008-02-18 03:32:17 -060097 if (!opt) {
Rob Landley1a221d92008-05-17 17:13:26 -050098 if (gof->noerror) return 1;
Rob Landley61190a32008-02-18 03:32:17 -060099 error_exit("Unknown option %s", gof->arg);
100 }
101 toys.optflags |= opt->edx[0];
102 toys.optflags &= ~opt->edx[1];
103 gof->excludes = opt->edx[2];
Rob Landley8324b892006-11-19 02:49:22 -0500104
105 // Does this option take an argument?
Rob Landley61190a32008-02-18 03:32:17 -0600106 gof->arg++;
107 type = opt->type;
Rob Landley58c6c1b2006-11-25 13:34:51 -0500108 if (type) {
109
110 // Handle "-xblah" and "-x blah", but also a third case: "abxc blah"
111 // to make "tar xCjfv blah1 blah2 thingy" work like
Rob Landley8324b892006-11-19 02:49:22 -0500112 // "tar -x -C blah1 -j -f blah2 -v thingy"
Rob Landley61190a32008-02-18 03:32:17 -0600113 if (!gof->nodash_now && !gof->arg[0]) {
114 gof->arg = toys.argv[++gof->argc];
115 // TODO: The following line doesn't display --longopt correctly
116 if (!gof->arg) error_exit("Missing argument to -%c",opt->c);
Rob Landley8324b892006-11-19 02:49:22 -0500117 }
Rob Landley8324b892006-11-19 02:49:22 -0500118
Rob Landley58c6c1b2006-11-25 13:34:51 -0500119 // Grab argument.
Rob Landley61190a32008-02-18 03:32:17 -0600120 if (!gof->arg && !(gof->arg = toys.argv[++(gof->argc)]))
Rob Landleyfdb667e2006-11-24 00:15:21 -0500121 error_exit("Missing argument");
Rob Landley61190a32008-02-18 03:32:17 -0600122 if (type == ':') *(opt->arg) = (long)gof->arg;
Rob Landleyfdb667e2006-11-24 00:15:21 -0500123 else if (type == '*') {
Rob Landley4ac66562008-06-16 19:27:35 -0500124 struct arg_list **list;
125
Rob Landley61190a32008-02-18 03:32:17 -0600126 list = (struct arg_list **)opt->arg;
Rob Landley4ac66562008-06-16 19:27:35 -0500127 while (*list) list=&((*list)->next);
128 *list = xzalloc(sizeof(struct arg_list));
129 (*list)->arg = gof->arg;
Rob Landley61190a32008-02-18 03:32:17 -0600130 } else if (type == '#') *(opt->arg) = atolx((char *)gof->arg);
Rob Landley4ac66562008-06-16 19:27:35 -0500131 else if (type == '@') ++*(opt->arg);
Rob Landley8324b892006-11-19 02:49:22 -0500132
Rob Landley61190a32008-02-18 03:32:17 -0600133 gof->arg = "";
Rob Landleyfdb667e2006-11-24 00:15:21 -0500134 }
Rob Landley58c6c1b2006-11-25 13:34:51 -0500135
Rob Landley61190a32008-02-18 03:32:17 -0600136 gof->this = NULL;
Rob Landley1a221d92008-05-17 17:13:26 -0500137 return 0;
Rob Landley8324b892006-11-19 02:49:22 -0500138}
139
Rob Landley61190a32008-02-18 03:32:17 -0600140// Fill out toys.optflags and toys.optargs.
Rob Landley8324b892006-11-19 02:49:22 -0500141
Rob Landley61190a32008-02-18 03:32:17 -0600142static char *plustildenot = "+~!";
Rob Landley8324b892006-11-19 02:49:22 -0500143void get_optflags(void)
144{
Rob Landley26bf9e62008-02-12 17:36:13 -0600145 int stopearly = 0, nodash = 0, minargs = 0, maxargs;
Rob Landley8324b892006-11-19 02:49:22 -0500146 struct longopts {
147 struct longopts *next;
148 struct opts *opt;
149 char *str;
150 int len;
151 } *longopts = NULL;
Rob Landley61190a32008-02-18 03:32:17 -0600152 struct getoptflagstate gof;
Rob Landley1a221d92008-05-17 17:13:26 -0500153 long *nextarg = (long *)&this, saveflags;
Rob Landley8324b892006-11-19 02:49:22 -0500154 char *options = toys.which->options;
Rob Landley8e998742008-05-04 18:52:29 -0500155 char *letters[]={"s",""};
Rob Landley8324b892006-11-19 02:49:22 -0500156
Rob Landleyd06c58d2007-10-11 15:36:36 -0500157 if (CFG_HELP) toys.exithelp++;
Rob Landley54ebcce2006-11-19 20:35:19 -0500158 // Allocate memory for optargs
159 maxargs = 0;
160 while (toys.argv[maxargs++]);
161 toys.optargs = xzalloc(sizeof(char *)*maxargs);
162 maxargs = INT_MAX;
Rob Landley61190a32008-02-18 03:32:17 -0600163 bzero(&gof, sizeof(struct getoptflagstate));
Rob Landley54ebcce2006-11-19 20:35:19 -0500164
165 // Parse option format
Rob Landley2a813ff2006-11-19 17:29:35 -0500166 if (options) {
Rob Landley61190a32008-02-18 03:32:17 -0600167
Rob Landley2a813ff2006-11-19 17:29:35 -0500168 // Parse leading special behavior indicators
169 for (;;) {
170 if (*options == '+') stopearly++;
171 else if (*options == '<') minargs=*(++options)-'0';
172 else if (*options == '>') maxargs=*(++options)-'0';
Rob Landley028a5442007-01-25 16:11:23 -0500173 else if (*options == '?') gof.noerror++;
Rob Landley2a813ff2006-11-19 17:29:35 -0500174 else if (*options == '&') nodash++;
175 else break;
176 options++;
Rob Landley8324b892006-11-19 02:49:22 -0500177 }
Rob Landley8324b892006-11-19 02:49:22 -0500178
Rob Landley2a813ff2006-11-19 17:29:35 -0500179 // Parse rest of opts into array
180 while (*options) {
Rob Landleyaba353e2008-03-24 00:32:25 -0500181 char *temp;
Rob Landley8324b892006-11-19 02:49:22 -0500182
Rob Landley2a813ff2006-11-19 17:29:35 -0500183 // Allocate a new option entry when necessary
184 if (!gof.this) {
185 gof.this = xzalloc(sizeof(struct opts));
186 gof.this->next = gof.opts;
187 gof.opts = gof.this;
Rob Landley61190a32008-02-18 03:32:17 -0600188 ++*(gof.this->edx);
Rob Landley2a813ff2006-11-19 17:29:35 -0500189 }
Rob Landley61190a32008-02-18 03:32:17 -0600190 // Each option must start with "(" or an option character. (Bare
Rob Landley2a813ff2006-11-19 17:29:35 -0500191 // longopts only come at the start of the string.)
192 if (*options == '(') {
193 char *end;
194 struct longopts *lo = xmalloc(sizeof(struct longopts));
Rob Landley8324b892006-11-19 02:49:22 -0500195
Rob Landley2a813ff2006-11-19 17:29:35 -0500196 // Find the end of the longopt
197 for (end = ++options; *end && *end != ')'; end++);
Rob Landleyde05a702007-01-31 14:37:01 -0500198 if (CFG_TOYBOX_DEBUG && !*end)
Rob Landley61190a32008-02-18 03:32:17 -0600199 error_exit("Bug1 in get_opt");
Rob Landley8324b892006-11-19 02:49:22 -0500200
Rob Landley2a813ff2006-11-19 17:29:35 -0500201 // Allocate and init a new struct longopts
202 lo = xmalloc(sizeof(struct longopts));
203 lo->next = longopts;
204 lo->opt = gof.this;
205 lo->str = options;
206 lo->len = end-options;
207 longopts = lo;
208 options = end;
Rob Landley8324b892006-11-19 02:49:22 -0500209
Rob Landley61190a32008-02-18 03:32:17 -0600210 // Mark this as struct opt as used, even when no short opt.
211 if (!gof.this->c) gof.this->c = -1;
Rob Landley8324b892006-11-19 02:49:22 -0500212
Rob Landley2a813ff2006-11-19 17:29:35 -0500213 // If this is the start of a new option that wasn't a longopt,
Rob Landley8324b892006-11-19 02:49:22 -0500214
Rob Landley61190a32008-02-18 03:32:17 -0600215 } else if (strchr(":*#@", *options)) {
216 gof.this->type = *options;
217 } else if (0 != (temp = strchr(plustildenot, *options))) {
218 int i=0, idx = temp - plustildenot;
219 struct opts *opt;
220
Rob Landleyaba353e2008-03-24 00:32:25 -0500221 if (!*++options && CFG_TOYBOX_DEBUG)
Rob Landley61190a32008-02-18 03:32:17 -0600222 error_exit("Bug2 in get_opt");
223 // Find this option flag (in previously parsed struct opt)
224 for (opt = gof.this; ; opt = opt->next) {
225 if (CFG_TOYBOX_DEBUG && !opt) error_exit("Bug3 in get_opt");
226 if (opt->c == *options) break;
227 i++;
228 }
229 gof.this->edx[idx] |= 1<<i;
230
Rob Landley2a813ff2006-11-19 17:29:35 -0500231 } else if (*options == '[') {
Rob Landley61190a32008-02-18 03:32:17 -0600232 } else if (*options == '|') {
Rob Landley8324b892006-11-19 02:49:22 -0500233
Rob Landley2a813ff2006-11-19 17:29:35 -0500234 // At this point, we've hit the end of the previous option. The
235 // current character is the start of a new option. If we've already
236 // assigned an option to this struct, loop to allocate a new one.
Rob Landley61190a32008-02-18 03:32:17 -0600237 // (It'll get back here afterwards and fall through to next else.)
238 } else if(gof.this->c) {
Rob Landley2a813ff2006-11-19 17:29:35 -0500239 gof.this = NULL;
240 continue;
241
242 // Claim this option, loop to see what's after it.
243 } else gof.this->c = *options;
244
245 options++;
246 }
Rob Landley8324b892006-11-19 02:49:22 -0500247 }
248
Rob Landley61190a32008-02-18 03:32:17 -0600249 // Initialize enable/disable/exclude masks and pointers to store arguments.
250 // (We have to calculate all this ahead of time because longopts jump into
251 // the middle of the list.)
Rob Landley8324b892006-11-19 02:49:22 -0500252 gof.argc = 0;
Rob Landley028a5442007-01-25 16:11:23 -0500253 for (gof.this = gof.opts; gof.this; gof.this = gof.this->next) {
Rob Landley61190a32008-02-18 03:32:17 -0600254 int i;
255
256 for (i=0;i<3;i++) gof.this->edx[i] <<= gof.argc;
257 gof.argc++;
258 if (gof.this->type) {
Rob Landley028a5442007-01-25 16:11:23 -0500259 gof.this->arg = (void *)nextarg;
260 *(nextarg++) = 0;
261 }
262 }
Rob Landley8324b892006-11-19 02:49:22 -0500263
264 // Iterate through command line arguments, skipping argv[0]
265 for (gof.argc=1; toys.argv[gof.argc]; gof.argc++) {
Rob Landleyfdb667e2006-11-24 00:15:21 -0500266 gof.arg = toys.argv[gof.argc];
267 gof.this = NULL;
Rob Landley8324b892006-11-19 02:49:22 -0500268
269 // Parse this argument
270 if (stopearly>1) goto notflag;
271
272 gof.nodash_now = 0;
273
274 // Various things with dashes
Rob Landleyfdb667e2006-11-24 00:15:21 -0500275 if (*gof.arg == '-') {
Rob Landley8324b892006-11-19 02:49:22 -0500276
277 // Handle -
Rob Landleyfdb667e2006-11-24 00:15:21 -0500278 if (!gof.arg[1]) goto notflag;
279 gof.arg++;
280 if (*gof.arg=='-') {
Rob Landley8324b892006-11-19 02:49:22 -0500281 struct longopts *lo;
282
Rob Landleyfdb667e2006-11-24 00:15:21 -0500283 gof.arg++;
Rob Landley8324b892006-11-19 02:49:22 -0500284 // Handle --
Rob Landleyfdb667e2006-11-24 00:15:21 -0500285 if (!*gof.arg) {
Rob Landley8324b892006-11-19 02:49:22 -0500286 stopearly += 2;
287 goto notflag;
288 }
289 // Handle --longopt
290
291 for (lo = longopts; lo; lo = lo->next) {
Rob Landleyfdb667e2006-11-24 00:15:21 -0500292 if (!strncmp(gof.arg, lo->str, lo->len)) {
293 if (gof.arg[lo->len]) {
Rob Landley61190a32008-02-18 03:32:17 -0600294 if (gof.arg[lo->len]=='=' && lo->opt->type)
Rob Landleyfdb667e2006-11-24 00:15:21 -0500295 gof.arg += lo->len;
Rob Landley61190a32008-02-18 03:32:17 -0600296 else continue;
Rob Landleyfdb667e2006-11-24 00:15:21 -0500297 }
298 // It's a match.
299 gof.arg = "";
Rob Landley8324b892006-11-19 02:49:22 -0500300 gof.this = lo->opt;
301 break;
302 }
303 }
Rob Landleyfdb667e2006-11-24 00:15:21 -0500304
Rob Landley1a221d92008-05-17 17:13:26 -0500305 // Should we handle this --longopt as a non-option argument?
306 if (!lo && gof.noerror) {
307 gof.arg-=2;
308 goto notflag;
309 }
310
Rob Landleyfdb667e2006-11-24 00:15:21 -0500311 // Long option parsed, handle option.
Rob Landley61190a32008-02-18 03:32:17 -0600312 gotflag(&gof);
Rob Landley8324b892006-11-19 02:49:22 -0500313 continue;
314 }
315
316 // Handle things that don't start with a dash.
317 } else {
318 if (nodash && (nodash>1 || gof.argc == 1)) gof.nodash_now = 1;
319 else goto notflag;
320 }
321
322 // At this point, we have the args part of -args. Loop through
323 // each entry (could be -abc meaning -a -b -c)
Rob Landley1a221d92008-05-17 17:13:26 -0500324 saveflags = toys.optflags;
Rob Landleyfdb667e2006-11-24 00:15:21 -0500325 while (*gof.arg) {
326
Rob Landley8324b892006-11-19 02:49:22 -0500327 // Identify next option char.
Rob Landleyfdb667e2006-11-24 00:15:21 -0500328 for (gof.this = gof.opts; gof.this; gof.this = gof.this->next)
329 if (*gof.arg == gof.this->c) break;
330
331 // Handle option char (advancing past what was used)
Rob Landley1a221d92008-05-17 17:13:26 -0500332 if (gotflag(&gof) ) {
333 toys.optflags = saveflags;
334 gof.arg = toys.argv[gof.argc];
335 goto notflag;
336 }
Rob Landley8324b892006-11-19 02:49:22 -0500337 }
338 continue;
339
340 // Not a flag, save value in toys.optargs[]
341notflag:
342 if (stopearly) stopearly++;
Rob Landley26bf9e62008-02-12 17:36:13 -0600343 toys.optargs[toys.optc++] = toys.argv[gof.argc];
Rob Landley8324b892006-11-19 02:49:22 -0500344 }
345
346 // Sanity check
Rob Landley8e998742008-05-04 18:52:29 -0500347 if (toys.optc<minargs) {
348 error_exit("Need%s %d argument%s", letters[!!(minargs-1)], minargs,
349 letters[!(minargs-1)]);
350 }
Rob Landley26bf9e62008-02-12 17:36:13 -0600351 if (toys.optc>maxargs)
Rob Landley8e998742008-05-04 18:52:29 -0500352 error_exit("Max %d argument%s", maxargs, letters[!(maxargs-1)]);
Rob Landleyd06c58d2007-10-11 15:36:36 -0500353 if (CFG_HELP) toys.exithelp = 0;
Rob Landley8324b892006-11-19 02:49:22 -0500354}