annotate lib/args.c @ 392:4fb1fa3e6603

Fix "tar cvjfC file dir", make @ not eat an argument, add debug check for (as yet) unsupported multi-function option (ala "x*@").
author Rob Landley <rob@landley.net>
date Mon, 28 Nov 2011 00:22:15 -0600
parents 523441f8ed01
children bfc208c5ac79
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* vi: set sw=4 ts=4 :
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
2 * args.c - Command line argument parsing.
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
3 *
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
4 * Copyright 2006 Rob Landley <rob@landley.net>
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
5 */
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
6
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
7 #include "toys.h"
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
8
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
9 // Design goals:
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
10 // Don't use getopt()
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
11 // Don't permute original arguments.
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
12 // handle --long gracefully "(noshort)a(along)b(blong1)(blong2)"
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
13 // After each argument:
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
14 // Note that pointer and long are always the same size, even on 64 bit.
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
15 // : plus a string argument, keep most recent if more than one
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
16 // * plus a string argument, appended to a list
78
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
17 // # plus a signed long argument (TODO: Bounds checking?)
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
18 // @ plus an occurrence counter (which is a long)
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
19 // (longopt)
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
20 // | this is required. If more than one marked, only one required.
306
523441f8ed01 Teach option parsing logic that ^ means stop parsing after this option.
Rob Landley <rob@landley.net>
parents: 304
diff changeset
21 // ^ Stop parsing after encountering this argument
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
22 //
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
23 // These modify other option letters (previously seen in string):
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
24 // +X enabling this enables X (switch on)
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
25 // ~X enabling this disables X (switch off)
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
26 // !X die with error if X already set (x!x die if x supplied twice)
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
27 // [yz] needs at least one of y or z.
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
28 // at the beginning:
304
93223118c813 Option parsing: stopearly is now a ^ prefix (not +), and an option string with
Rob Landley <rob@landley.net>
parents: 298
diff changeset
29 // ^ stop at first nonoption argument
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
30 // <0 at least # leftover arguments needed (default 0)
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
31 // >9 at most # leftover arguments needed (default MAX_INT)
78
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
32 // ? don't show_usage() on unknown argument.
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
33 // & first argument has imaginary dash (ala tar/ps)
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
34 // If given twice, all arguments have imaginary dash
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
35
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
36 // Notes from getopt man page
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
37 // - and -- cannot be arguments.
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
38 // -- force end of arguments
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
39 // - is a synonym for stdin in file arguments
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
40 // -abc means -a -b -c
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
41
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
42 /* This uses a getopt-like option string, but not getopt() itself. We call
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
43 * it the get_opt string.
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 144
diff changeset
44 *
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
45 * Each option in the get_opt string corresponds to a bit position in the
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
46 * return value. The rightmost argument is (1<<0), the next to last is (1<<1)
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
47 * and so on. If the option isn't seen in argv[], its bit remains 0.
78
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
48 *
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
49 * Options which have an argument fill in the corresponding slot in the global
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
50 * union "this" (see generated/globals.h), which it treats as an array of longs
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
51 * (note that sizeof(long)==sizeof(pointer) is guaranteed by LP64).
78
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
52 *
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
53 * You don't have to free the option strings, which point into the environment
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
54 * space. List objects should be freed by main() when command_main() returns.
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
55 *
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
56 * Example:
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
57 * Calling get_optflags() when toys.which->options="ab:c:d" and
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
58 * argv = ["command", "-b", "fruit", "-d", "walrus"] results in:
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
59 *
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
60 * Changes to struct toys:
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
61 * toys.optflags = 5 (-b=4 | -d=1)
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
62 * toys.optargs[0]="walrus" (leftover argument)
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
63 * toys.optargs[1]=NULL (end of list)
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
64 * toys.optc=1 (there was 1 leftover argument)
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
65 *
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
66 * Changes to union this:
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
67 * this[0]=NULL (because -c didn't get an argument this time)
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
68 * this[1]="fruit" (argument to -b)
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
69 */
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
70
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
71 // Linked list of all known options (get_opt string is parsed into this).
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
72 struct opts {
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
73 struct opts *next;
306
523441f8ed01 Teach option parsing logic that ^ means stop parsing after this option.
Rob Landley <rob@landley.net>
parents: 304
diff changeset
74 long *arg; // Pointer into union "this" to store arguments at.
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
75 uint32_t edx[3]; // Flag mask to enable/disable/exclude.
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
76 int c; // Short argument character
306
523441f8ed01 Teach option parsing logic that ^ means stop parsing after this option.
Rob Landley <rob@landley.net>
parents: 304
diff changeset
77 int flags; // |=1, ^=2
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
78 char type; // Type of arguments to store
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
79 };
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
80
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
81 // State during argument parsing.
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
82 struct getoptflagstate
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
83 {
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
84 int argc;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
85 char *arg;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
86 struct opts *opts, *this;
306
523441f8ed01 Teach option parsing logic that ^ means stop parsing after this option.
Rob Landley <rob@landley.net>
parents: 304
diff changeset
87 int noerror, nodash_now, stopearly;
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
88 uint32_t excludes;
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
89 };
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
90
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
91 // Parse one command line option.
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
92
294
3de1ea4d6b56 Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents: 289
diff changeset
93 static int gotflag(struct getoptflagstate *gof)
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
94 {
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
95 int type;
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
96 struct opts *opt = gof->this;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
97
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
98 // Did we recognize this option?
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
99 if (!opt) {
294
3de1ea4d6b56 Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents: 289
diff changeset
100 if (gof->noerror) return 1;
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
101 error_exit("Unknown option %s", gof->arg);
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
102 }
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
103 toys.optflags |= opt->edx[0];
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
104 toys.optflags &= ~opt->edx[1];
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
105 gof->excludes = opt->edx[2];
306
523441f8ed01 Teach option parsing logic that ^ means stop parsing after this option.
Rob Landley <rob@landley.net>
parents: 304
diff changeset
106 if (opt->flags&2) gof->stopearly=2;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
107
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
108 // Does this option take an argument?
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
109 gof->arg++;
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
110 type = opt->type;
392
4fb1fa3e6603 Fix "tar cvjfC file dir", make @ not eat an argument, add debug check for (as yet) unsupported multi-function option (ala "x*@").
Rob Landley <rob@landley.net>
parents: 306
diff changeset
111 if (type == '@') ++*(opt->arg);
4fb1fa3e6603 Fix "tar cvjfC file dir", make @ not eat an argument, add debug check for (as yet) unsupported multi-function option (ala "x*@").
Rob Landley <rob@landley.net>
parents: 306
diff changeset
112 else if (type) {
4fb1fa3e6603 Fix "tar cvjfC file dir", make @ not eat an argument, add debug check for (as yet) unsupported multi-function option (ala "x*@").
Rob Landley <rob@landley.net>
parents: 306
diff changeset
113 char *arg = gof->arg;
32
993eab821bd5 More work on option parsing. "df -t tmpfs" actually seems to work now.
Rob Landley <rob@landley.net>
parents: 29
diff changeset
114
993eab821bd5 More work on option parsing. "df -t tmpfs" actually seems to work now.
Rob Landley <rob@landley.net>
parents: 29
diff changeset
115 // Handle "-xblah" and "-x blah", but also a third case: "abxc blah"
993eab821bd5 More work on option parsing. "df -t tmpfs" actually seems to work now.
Rob Landley <rob@landley.net>
parents: 29
diff changeset
116 // to make "tar xCjfv blah1 blah2 thingy" work like
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
117 // "tar -x -C blah1 -j -f blah2 -v thingy"
392
4fb1fa3e6603 Fix "tar cvjfC file dir", make @ not eat an argument, add debug check for (as yet) unsupported multi-function option (ala "x*@").
Rob Landley <rob@landley.net>
parents: 306
diff changeset
118 if (gof->nodash_now || !gof->arg[0]) arg = toys.argv[++gof->argc];
4fb1fa3e6603 Fix "tar cvjfC file dir", make @ not eat an argument, add debug check for (as yet) unsupported multi-function option (ala "x*@").
Rob Landley <rob@landley.net>
parents: 306
diff changeset
119 // TODO: The following line doesn't display --longopt correctly
4fb1fa3e6603 Fix "tar cvjfC file dir", make @ not eat an argument, add debug check for (as yet) unsupported multi-function option (ala "x*@").
Rob Landley <rob@landley.net>
parents: 306
diff changeset
120 if (!arg) error_exit("Missing argument to -%c", opt->c);
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
121
392
4fb1fa3e6603 Fix "tar cvjfC file dir", make @ not eat an argument, add debug check for (as yet) unsupported multi-function option (ala "x*@").
Rob Landley <rob@landley.net>
parents: 306
diff changeset
122 if (type == ':') *(opt->arg) = (long)arg;
29
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
123 else if (type == '*') {
298
1bb9f53a7101 Assemble '*' repeated argument list in order. Also implement '@' counter.
Rob Landley <rob@landley.net>
parents: 294
diff changeset
124 struct arg_list **list;
1bb9f53a7101 Assemble '*' repeated argument list in order. Also implement '@' counter.
Rob Landley <rob@landley.net>
parents: 294
diff changeset
125
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
126 list = (struct arg_list **)opt->arg;
298
1bb9f53a7101 Assemble '*' repeated argument list in order. Also implement '@' counter.
Rob Landley <rob@landley.net>
parents: 294
diff changeset
127 while (*list) list=&((*list)->next);
1bb9f53a7101 Assemble '*' repeated argument list in order. Also implement '@' counter.
Rob Landley <rob@landley.net>
parents: 294
diff changeset
128 *list = xzalloc(sizeof(struct arg_list));
392
4fb1fa3e6603 Fix "tar cvjfC file dir", make @ not eat an argument, add debug check for (as yet) unsupported multi-function option (ala "x*@").
Rob Landley <rob@landley.net>
parents: 306
diff changeset
129 (*list)->arg = arg;
4fb1fa3e6603 Fix "tar cvjfC file dir", make @ not eat an argument, add debug check for (as yet) unsupported multi-function option (ala "x*@").
Rob Landley <rob@landley.net>
parents: 306
diff changeset
130 } else if (type == '#') *(opt->arg) = atolx((char *)arg);
29
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
131
392
4fb1fa3e6603 Fix "tar cvjfC file dir", make @ not eat an argument, add debug check for (as yet) unsupported multi-function option (ala "x*@").
Rob Landley <rob@landley.net>
parents: 306
diff changeset
132 if (!gof->nodash_now) gof->arg = "";
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
133 }
32
993eab821bd5 More work on option parsing. "df -t tmpfs" actually seems to work now.
Rob Landley <rob@landley.net>
parents: 29
diff changeset
134
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
135 gof->this = NULL;
294
3de1ea4d6b56 Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents: 289
diff changeset
136 return 0;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
137 }
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
138
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
139 // Fill out toys.optflags and toys.optargs.
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
140
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
141 static char *plustildenot = "+~!";
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
142 void get_optflags(void)
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
143 {
306
523441f8ed01 Teach option parsing logic that ^ means stop parsing after this option.
Rob Landley <rob@landley.net>
parents: 304
diff changeset
144 int nodash = 0, minargs = 0, maxargs;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
145 struct longopts {
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
146 struct longopts *next;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
147 struct opts *opt;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
148 char *str;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
149 int len;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
150 } *longopts = NULL;
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
151 struct getoptflagstate gof;
294
3de1ea4d6b56 Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents: 289
diff changeset
152 long *nextarg = (long *)&this, saveflags;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
153 char *options = toys.which->options;
289
a1fdf34d9504 Fluffier error message.
Rob Landley <rob@landley.net>
parents: 268
diff changeset
154 char *letters[]={"s",""};
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
155
144
1fbc50374a30 Promote help to global config option, teach error_exit() to output usage message when called
Rob Landley <rob@landley.net>
parents: 127
diff changeset
156 if (CFG_HELP) toys.exithelp++;
28
be59ed005902 Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents: 26
diff changeset
157 // Allocate memory for optargs
be59ed005902 Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents: 26
diff changeset
158 maxargs = 0;
be59ed005902 Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents: 26
diff changeset
159 while (toys.argv[maxargs++]);
be59ed005902 Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents: 26
diff changeset
160 toys.optargs = xzalloc(sizeof(char *)*maxargs);
be59ed005902 Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents: 26
diff changeset
161 maxargs = INT_MAX;
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
162 bzero(&gof, sizeof(struct getoptflagstate));
28
be59ed005902 Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents: 26
diff changeset
163
be59ed005902 Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents: 26
diff changeset
164 // Parse option format
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
165 if (options) {
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
166
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
167 // Parse leading special behavior indicators
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
168 for (;;) {
306
523441f8ed01 Teach option parsing logic that ^ means stop parsing after this option.
Rob Landley <rob@landley.net>
parents: 304
diff changeset
169 if (*options == '^') gof.stopearly++;
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
170 else if (*options == '<') minargs=*(++options)-'0';
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
171 else if (*options == '>') maxargs=*(++options)-'0';
78
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
172 else if (*options == '?') gof.noerror++;
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
173 else if (*options == '&') nodash++;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
174 else break;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
175 options++;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
176 }
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
177
306
523441f8ed01 Teach option parsing logic that ^ means stop parsing after this option.
Rob Landley <rob@landley.net>
parents: 304
diff changeset
178 if (!*options) gof.stopearly++;
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
179 // Parse rest of opts into array
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
180 while (*options) {
268
56a29fb3c9f6 Enabling debugging should not change behavior. Oops.
Rob Landley <rob@landley.net>
parents: 261
diff changeset
181 char *temp;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
182
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
183 // Allocate a new option entry when necessary
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
184 if (!gof.this) {
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
185 gof.this = xzalloc(sizeof(struct opts));
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
186 gof.this->next = gof.opts;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
187 gof.opts = gof.this;
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
188 ++*(gof.this->edx);
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
189 }
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
190 // Each option must start with "(" or an option character. (Bare
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
191 // longopts only come at the start of the string.)
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
192 if (*options == '(') {
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
193 char *end;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
194 struct longopts *lo = xmalloc(sizeof(struct longopts));
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
195
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
196 // Find the end of the longopt
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
197 for (end = ++options; *end && *end != ')'; end++);
90
7c77c6ec17ee Add "make defconfig". Modify global options to start with CONFIG_TOYBOX_.
Rob Landley <rob@landley.net>
parents: 78
diff changeset
198 if (CFG_TOYBOX_DEBUG && !*end)
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
199 error_exit("Bug1 in get_opt");
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
200
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
201 // Allocate and init a new struct longopts
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
202 lo = xmalloc(sizeof(struct longopts));
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
203 lo->next = longopts;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
204 lo->opt = gof.this;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
205 lo->str = options;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
206 lo->len = end-options;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
207 longopts = lo;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
208 options = end;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
209
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
210 // Mark this as struct opt as used, even when no short opt.
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
211 if (!gof.this->c) gof.this->c = -1;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
212
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
213 // If this is the start of a new option that wasn't a longopt,
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
214
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
215 } else if (strchr(":*#@", *options)) {
392
4fb1fa3e6603 Fix "tar cvjfC file dir", make @ not eat an argument, add debug check for (as yet) unsupported multi-function option (ala "x*@").
Rob Landley <rob@landley.net>
parents: 306
diff changeset
216 if (CFG_TOYBOX_DEBUG && gof.this->type)
4fb1fa3e6603 Fix "tar cvjfC file dir", make @ not eat an argument, add debug check for (as yet) unsupported multi-function option (ala "x*@").
Rob Landley <rob@landley.net>
parents: 306
diff changeset
217 error_exit("Bug4 in get_opt");
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
218 gof.this->type = *options;
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
219 } else if (0 != (temp = strchr(plustildenot, *options))) {
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
220 int i=0, idx = temp - plustildenot;
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
221 struct opts *opt;
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
222
268
56a29fb3c9f6 Enabling debugging should not change behavior. Oops.
Rob Landley <rob@landley.net>
parents: 261
diff changeset
223 if (!*++options && CFG_TOYBOX_DEBUG)
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
224 error_exit("Bug2 in get_opt");
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
225 // Find this option flag (in previously parsed struct opt)
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
226 for (opt = gof.this; ; opt = opt->next) {
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
227 if (CFG_TOYBOX_DEBUG && !opt) error_exit("Bug3 in get_opt");
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
228 if (opt->c == *options) break;
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
229 i++;
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
230 }
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
231 gof.this->edx[idx] |= 1<<i;
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
232
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
233 } else if (*options == '[') {
306
523441f8ed01 Teach option parsing logic that ^ means stop parsing after this option.
Rob Landley <rob@landley.net>
parents: 304
diff changeset
234 } else if (*options == '|') gof.this->flags |= 1;
523441f8ed01 Teach option parsing logic that ^ means stop parsing after this option.
Rob Landley <rob@landley.net>
parents: 304
diff changeset
235 else if (*options == '^') gof.this->flags |= 2;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
236
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
237 // At this point, we've hit the end of the previous option. The
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
238 // current character is the start of a new option. If we've already
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
239 // assigned an option to this struct, loop to allocate a new one.
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
240 // (It'll get back here afterwards and fall through to next else.)
306
523441f8ed01 Teach option parsing logic that ^ means stop parsing after this option.
Rob Landley <rob@landley.net>
parents: 304
diff changeset
241 else if(gof.this->c) {
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
242 gof.this = NULL;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
243 continue;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
244
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
245 // Claim this option, loop to see what's after it.
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
246 } else gof.this->c = *options;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
247
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
248 options++;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
249 }
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
250 }
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
251
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
252 // Initialize enable/disable/exclude masks and pointers to store arguments.
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
253 // (We have to calculate all this ahead of time because longopts jump into
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
254 // the middle of the list.)
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
255 gof.argc = 0;
78
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
256 for (gof.this = gof.opts; gof.this; gof.this = gof.this->next) {
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
257 int i;
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
258
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
259 for (i=0;i<3;i++) gof.this->edx[i] <<= gof.argc;
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
260 gof.argc++;
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
261 if (gof.this->type) {
78
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
262 gof.this->arg = (void *)nextarg;
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
263 *(nextarg++) = 0;
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
264 }
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
265 }
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
266
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
267 // Iterate through command line arguments, skipping argv[0]
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
268 for (gof.argc=1; toys.argv[gof.argc]; gof.argc++) {
29
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
269 gof.arg = toys.argv[gof.argc];
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
270 gof.this = NULL;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
271
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
272 // Parse this argument
306
523441f8ed01 Teach option parsing logic that ^ means stop parsing after this option.
Rob Landley <rob@landley.net>
parents: 304
diff changeset
273 if (gof.stopearly>1) goto notflag;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
274
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
275 gof.nodash_now = 0;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
276
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
277 // Various things with dashes
29
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
278 if (*gof.arg == '-') {
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
279
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
280 // Handle -
29
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
281 if (!gof.arg[1]) goto notflag;
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
282 gof.arg++;
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
283 if (*gof.arg=='-') {
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
284 struct longopts *lo;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
285
29
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
286 gof.arg++;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
287 // Handle --
29
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
288 if (!*gof.arg) {
306
523441f8ed01 Teach option parsing logic that ^ means stop parsing after this option.
Rob Landley <rob@landley.net>
parents: 304
diff changeset
289 gof.stopearly += 2;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
290 goto notflag;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
291 }
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
292 // Handle --longopt
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
293
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
294 for (lo = longopts; lo; lo = lo->next) {
29
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
295 if (!strncmp(gof.arg, lo->str, lo->len)) {
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
296 if (gof.arg[lo->len]) {
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
297 if (gof.arg[lo->len]=='=' && lo->opt->type)
29
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
298 gof.arg += lo->len;
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
299 else continue;
29
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
300 }
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
301 // It's a match.
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
302 gof.arg = "";
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
303 gof.this = lo->opt;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
304 break;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
305 }
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
306 }
29
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
307
294
3de1ea4d6b56 Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents: 289
diff changeset
308 // Should we handle this --longopt as a non-option argument?
3de1ea4d6b56 Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents: 289
diff changeset
309 if (!lo && gof.noerror) {
3de1ea4d6b56 Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents: 289
diff changeset
310 gof.arg-=2;
3de1ea4d6b56 Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents: 289
diff changeset
311 goto notflag;
3de1ea4d6b56 Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents: 289
diff changeset
312 }
3de1ea4d6b56 Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents: 289
diff changeset
313
29
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
314 // Long option parsed, handle option.
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
315 gotflag(&gof);
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
316 continue;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
317 }
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
318
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
319 // Handle things that don't start with a dash.
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
320 } else {
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
321 if (nodash && (nodash>1 || gof.argc == 1)) gof.nodash_now = 1;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
322 else goto notflag;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
323 }
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
324
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
325 // At this point, we have the args part of -args. Loop through
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
326 // each entry (could be -abc meaning -a -b -c)
294
3de1ea4d6b56 Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents: 289
diff changeset
327 saveflags = toys.optflags;
29
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
328 while (*gof.arg) {
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
329
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
330 // Identify next option char.
29
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
331 for (gof.this = gof.opts; gof.this; gof.this = gof.this->next)
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
332 if (*gof.arg == gof.this->c) break;
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
333
5566beb17d5a Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents: 28
diff changeset
334 // Handle option char (advancing past what was used)
294
3de1ea4d6b56 Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents: 289
diff changeset
335 if (gotflag(&gof) ) {
3de1ea4d6b56 Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents: 289
diff changeset
336 toys.optflags = saveflags;
3de1ea4d6b56 Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents: 289
diff changeset
337 gof.arg = toys.argv[gof.argc];
3de1ea4d6b56 Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents: 289
diff changeset
338 goto notflag;
3de1ea4d6b56 Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents: 289
diff changeset
339 }
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
340 }
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
341 continue;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
342
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
343 // Not a flag, save value in toys.optargs[]
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
344 notflag:
306
523441f8ed01 Teach option parsing logic that ^ means stop parsing after this option.
Rob Landley <rob@landley.net>
parents: 304
diff changeset
345 if (gof.stopearly) gof.stopearly++;
255
3fe66e630944 Add toys.optc, an argv-style count for toys.optargs.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
346 toys.optargs[toys.optc++] = toys.argv[gof.argc];
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
347 }
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
348
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
349 // Sanity check
289
a1fdf34d9504 Fluffier error message.
Rob Landley <rob@landley.net>
parents: 268
diff changeset
350 if (toys.optc<minargs) {
a1fdf34d9504 Fluffier error message.
Rob Landley <rob@landley.net>
parents: 268
diff changeset
351 error_exit("Need%s %d argument%s", letters[!!(minargs-1)], minargs,
a1fdf34d9504 Fluffier error message.
Rob Landley <rob@landley.net>
parents: 268
diff changeset
352 letters[!(minargs-1)]);
a1fdf34d9504 Fluffier error message.
Rob Landley <rob@landley.net>
parents: 268
diff changeset
353 }
255
3fe66e630944 Add toys.optc, an argv-style count for toys.optargs.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
354 if (toys.optc>maxargs)
289
a1fdf34d9504 Fluffier error message.
Rob Landley <rob@landley.net>
parents: 268
diff changeset
355 error_exit("Max %d argument%s", maxargs, letters[!(maxargs-1)]);
144
1fbc50374a30 Promote help to global config option, teach error_exit() to output usage message when called
Rob Landley <rob@landley.net>
parents: 127
diff changeset
356 if (CFG_HELP) toys.exithelp = 0;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
357 }