Mercurial > hg > toybox
annotate lib/args.c @ 294:3de1ea4d6b56
Fix command line option parsing so "echo -xen" actually prints "-xen". Add
echo.test while I'm at it.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 17 May 2008 17:13:26 -0500 |
parents | a1fdf34d9504 |
children | 1bb9f53a7101 |
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. |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
21 // |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
22 // 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
|
23 // +X enabling this enables X (switch on) |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
24 // ~X enabling this disables X (switch off) |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
25 // !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
|
26 // [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
|
27 // at the beginning: |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
28 // + stop at first nonoption argument |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
29 // <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
|
30 // >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
|
31 // ? 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
|
32 // & 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
|
33 // 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
|
34 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
35 // 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
|
36 // - 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
|
37 // -- 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
|
38 // - 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
|
39 // -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
|
40 |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
41 /* 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
|
42 * 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
|
43 * |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
44 * 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
|
45 * 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
|
46 * 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
|
47 * |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
48 * 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
|
49 * 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
|
50 * (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
|
51 * |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
52 * 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
|
53 * 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
|
54 * |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
55 * Example: |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
56 * 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
|
57 * 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
|
58 * |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
59 * Changes to struct toys: |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
60 * toys.optflags = 5 (-b=4 | -d=1) |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
61 * toys.optargs[0]="walrus" (leftover argument) |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
62 * toys.optargs[1]=NULL (end of list) |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
63 * 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
|
64 * |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
65 * Changes to union this: |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
66 * 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
|
67 * 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
|
68 */ |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
69 |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
70 // 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
|
71 struct opts { |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
72 struct opts *next; |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
73 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
|
74 long *arg; // Pointer into union this to store arguments at. |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
75 int c; // Short argument character |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
76 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
|
77 }; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
78 |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
79 // State during argument parsing. |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
80 struct getoptflagstate |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
81 { |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
82 int argc; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
83 char *arg; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
84 struct opts *opts, *this; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
85 int noerror, nodash_now; |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
86 uint32_t excludes; |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
87 }; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
88 |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
89 // Parse one command line option. |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
90 |
294
3de1ea4d6b56
Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents:
289
diff
changeset
|
91 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
|
92 { |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
93 int type; |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
94 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
|
95 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
96 // Did we recognize this option? |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
97 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
|
98 if (gof->noerror) return 1; |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
99 error_exit("Unknown option %s", gof->arg); |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
100 } |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
101 toys.optflags |= opt->edx[0]; |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
102 toys.optflags &= ~opt->edx[1]; |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
103 gof->excludes = opt->edx[2]; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
104 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
105 // Does this option take an argument? |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
106 gof->arg++; |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
107 type = opt->type; |
32
993eab821bd5
More work on option parsing. "df -t tmpfs" actually seems to work now.
Rob Landley <rob@landley.net>
parents:
29
diff
changeset
|
108 if (type) { |
993eab821bd5
More work on option parsing. "df -t tmpfs" actually seems to work now.
Rob Landley <rob@landley.net>
parents:
29
diff
changeset
|
109 |
993eab821bd5
More work on option parsing. "df -t tmpfs" actually seems to work now.
Rob Landley <rob@landley.net>
parents:
29
diff
changeset
|
110 // 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
|
111 // 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
|
112 // "tar -x -C blah1 -j -f blah2 -v thingy" |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
113 if (!gof->nodash_now && !gof->arg[0]) { |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
114 gof->arg = toys.argv[++gof->argc]; |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
115 // TODO: The following line doesn't display --longopt correctly |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
116 if (!gof->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
|
117 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
118 |
32
993eab821bd5
More work on option parsing. "df -t tmpfs" actually seems to work now.
Rob Landley <rob@landley.net>
parents:
29
diff
changeset
|
119 // Grab argument. |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
120 if (!gof->arg && !(gof->arg = toys.argv[++(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
|
121 error_exit("Missing argument"); |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
122 if (type == ':') *(opt->arg) = (long)gof->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 == '*') { |
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
|
124 struct arg_list *temp, **list; |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
125 list = (struct arg_list **)opt->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
|
126 temp = xmalloc(sizeof(struct arg_list)); |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
127 temp->arg = gof->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
|
128 temp->next = *list; |
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
|
129 *list = temp; |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
130 } else if (type == '#') *(opt->arg) = atolx((char *)gof->arg); |
78
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
131 else if (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
|
132 } |
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
|
133 |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
134 gof->arg = ""; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
135 } |
32
993eab821bd5
More work on option parsing. "df -t tmpfs" actually seems to work now.
Rob Landley <rob@landley.net>
parents:
29
diff
changeset
|
136 |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
137 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
|
138 return 0; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
139 } |
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 // 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
|
142 |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
143 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
|
144 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
|
145 { |
255
3fe66e630944
Add toys.optc, an argv-style count for toys.optargs.
Rob Landley <rob@landley.net>
parents:
237
diff
changeset
|
146 int stopearly = 0, 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
|
147 struct longopts { |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
148 struct longopts *next; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
149 struct opts *opt; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
150 char *str; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
151 int len; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
152 } *longopts = NULL; |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
153 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
|
154 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
|
155 char *options = toys.which->options; |
289 | 156 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
|
157 |
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
|
158 if (CFG_HELP) toys.exithelp++; |
28
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
159 // Allocate memory for optargs |
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
160 maxargs = 0; |
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
161 while (toys.argv[maxargs++]); |
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
162 toys.optargs = xzalloc(sizeof(char *)*maxargs); |
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
163 maxargs = INT_MAX; |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
164 bzero(&gof, sizeof(struct getoptflagstate)); |
28
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
165 |
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
166 // Parse option format |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
167 if (options) { |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
168 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
169 // Parse leading special behavior indicators |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
170 for (;;) { |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
171 if (*options == '+') stopearly++; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
172 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
|
173 else if (*options == '>') maxargs=*(++options)-'0'; |
78
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
174 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
|
175 else if (*options == '&') nodash++; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
176 else break; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
177 options++; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
178 } |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
179 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
180 // 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
|
181 while (*options) { |
268
56a29fb3c9f6
Enabling debugging should not change behavior. Oops.
Rob Landley <rob@landley.net>
parents:
261
diff
changeset
|
182 char *temp; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
183 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
184 // 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
|
185 if (!gof.this) { |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
186 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
|
187 gof.this->next = gof.opts; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
188 gof.opts = gof.this; |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
189 ++*(gof.this->edx); |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
190 } |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
191 // 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
|
192 // 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
|
193 if (*options == '(') { |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
194 char *end; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
195 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
|
196 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
197 // 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
|
198 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
|
199 if (CFG_TOYBOX_DEBUG && !*end) |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
200 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
|
201 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
202 // 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
|
203 lo = xmalloc(sizeof(struct longopts)); |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
204 lo->next = longopts; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
205 lo->opt = gof.this; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
206 lo->str = options; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
207 lo->len = end-options; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
208 longopts = lo; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
209 options = end; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
210 |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
211 // 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
|
212 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
|
213 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
214 // 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
|
215 |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
216 } else if (strchr(":*#@", *options)) { |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
217 gof.this->type = *options; |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
218 } else if (0 != (temp = strchr(plustildenot, *options))) { |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
219 int i=0, idx = temp - plustildenot; |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
220 struct opts *opt; |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
221 |
268
56a29fb3c9f6
Enabling debugging should not change behavior. Oops.
Rob Landley <rob@landley.net>
parents:
261
diff
changeset
|
222 if (!*++options && CFG_TOYBOX_DEBUG) |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
223 error_exit("Bug2 in get_opt"); |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
224 // 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
|
225 for (opt = gof.this; ; opt = opt->next) { |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
226 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
|
227 if (opt->c == *options) break; |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
228 i++; |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
229 } |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
230 gof.this->edx[idx] |= 1<<i; |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
231 |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
232 } else if (*options == '[') { |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
233 } else if (*options == '|') { |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
234 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
235 // 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
|
236 // 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
|
237 // 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
|
238 // (It'll get back here afterwards and fall through to next else.) |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
239 } 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
|
240 gof.this = NULL; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
241 continue; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
242 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
243 // 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
|
244 } 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
|
245 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
246 options++; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
247 } |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
248 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
249 |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
250 // 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
|
251 // (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
|
252 // 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
|
253 gof.argc = 0; |
78
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
254 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
|
255 int i; |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
256 |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
257 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
|
258 gof.argc++; |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
259 if (gof.this->type) { |
78
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
260 gof.this->arg = (void *)nextarg; |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
261 *(nextarg++) = 0; |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
262 } |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
263 } |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
264 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
265 // 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
|
266 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
|
267 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
|
268 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
|
269 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
270 // Parse this argument |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
271 if (stopearly>1) goto notflag; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
272 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
273 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
|
274 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
275 // 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
|
276 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
|
277 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
278 // 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
|
279 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
|
280 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
|
281 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
|
282 struct longopts *lo; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
283 |
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
|
284 gof.arg++; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
285 // 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
|
286 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
|
287 stopearly += 2; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
288 goto notflag; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
289 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
290 // Handle --longopt |
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 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
|
293 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
|
294 if (gof.arg[lo->len]) { |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
295 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
|
296 gof.arg += lo->len; |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
297 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
|
298 } |
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
|
299 // 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
|
300 gof.arg = ""; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
301 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
|
302 break; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
303 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
304 } |
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
|
305 |
294
3de1ea4d6b56
Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents:
289
diff
changeset
|
306 // 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
|
307 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
|
308 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
|
309 goto notflag; |
3de1ea4d6b56
Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents:
289
diff
changeset
|
310 } |
3de1ea4d6b56
Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents:
289
diff
changeset
|
311 |
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
|
312 // Long option parsed, handle option. |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
313 gotflag(&gof); |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
314 continue; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
315 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
316 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
317 // 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
|
318 } else { |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
319 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
|
320 else goto notflag; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
321 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
322 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
323 // 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
|
324 // 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
|
325 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
|
326 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
|
327 |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
328 // 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
|
329 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
|
330 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
|
331 |
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 // 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
|
333 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
|
334 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
|
335 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
|
336 goto notflag; |
3de1ea4d6b56
Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents:
289
diff
changeset
|
337 } |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
338 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
339 continue; |
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 // 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
|
342 notflag: |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
343 if (stopearly) stopearly++; |
255
3fe66e630944
Add toys.optc, an argv-style count for toys.optargs.
Rob Landley <rob@landley.net>
parents:
237
diff
changeset
|
344 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
|
345 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
346 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
347 // Sanity check |
289 | 348 if (toys.optc<minargs) { |
349 error_exit("Need%s %d argument%s", letters[!!(minargs-1)], minargs, | |
350 letters[!(minargs-1)]); | |
351 } | |
255
3fe66e630944
Add toys.optc, an argv-style count for toys.optargs.
Rob Landley <rob@landley.net>
parents:
237
diff
changeset
|
352 if (toys.optc>maxargs) |
289 | 353 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
|
354 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
|
355 } |