Mercurial > hg > toybox
annotate lib/args.c @ 432:01473712c9fe
Document that optflags is always an int (so 32 bit and 64 bit platforms behave the same).
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 06 Feb 2012 21:15:19 -0600 |
parents | 85a5e01e7ad8 |
children | f558dce66095 |
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 |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
17 // # plus a signed long argument |
415
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
18 // <LOW - die if less than LOW |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
19 // >HIGH - die if greater than HIGH |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
20 // =DEFAULT - value if not specified |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
21 // . plus a double precision floating point argument (with CFG_TOYBOX_FLOAT) |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
22 // Chop this out with USE_TOYBOX_FLOAT() around option string |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
23 // Same <LOW>HIGH=DEFAULT as # |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
24 // @ 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
|
25 // (longopt) |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
26 // | 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
|
27 // ^ Stop parsing after encountering this argument |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
28 // |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
29 // 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
|
30 // +X enabling this enables X (switch on) |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
31 // ~X enabling this disables X (switch off) |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
32 // !X die with error if X already set (x!x die if x supplied twice) |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
33 // [yz] needs at least one of y or z. TODO |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
34 // 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
|
35 // ^ stop at first nonoption argument |
415
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
36 // <0 die if less than leftover arguments (default 0) |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
37 // >9 die if > # leftover arguments (default MAX_INT) |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
38 // ? Allow unknown arguments (pass them through to command). |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
39 // & 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
|
40 // 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
|
41 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
42 // 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
|
43 // - 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
|
44 // -- 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
|
45 // - 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
|
46 // -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
|
47 |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
48 /* 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
|
49 * 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
|
50 * |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
51 * 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
|
52 * 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
|
53 * 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
|
54 * |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
55 * 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
|
56 * 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
|
57 * (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
|
58 * |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
59 * 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
|
60 * 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
|
61 * |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
62 * Example: |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
63 * 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
|
64 * 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
|
65 * |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
66 * Changes to struct toys: |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
67 * toys.optflags = 5 (-b=4 | -d=1) |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
68 * toys.optargs[0]="walrus" (leftover argument) |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
69 * toys.optargs[1]=NULL (end of list) |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
70 * 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
|
71 * |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
72 * Changes to union this: |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
73 * 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
|
74 * 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
|
75 */ |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
76 |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
77 // 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
|
78 struct opts { |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
79 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
|
80 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
|
81 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
|
82 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
|
83 int flags; // |=1, ^=2 |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
84 char type; // Type of arguments to store |
415
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
85 union { |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
86 long l; |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
87 FLOAT f; |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
88 } val[3]; // low, high, default - range of allowed values |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
89 }; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
90 |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
91 struct longopts { |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
92 struct longopts *next; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
93 struct opts *opt; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
94 char *str; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
95 int len; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
96 }; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
97 |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
98 // State during argument parsing. |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
99 struct getoptflagstate |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
100 { |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
101 int argc, minargs, maxargs, nodash; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
102 char *arg; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
103 struct opts *opts, *this; |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
104 struct longopts *longopts; |
306
523441f8ed01
Teach option parsing logic that ^ means stop parsing after this option.
Rob Landley <rob@landley.net>
parents:
304
diff
changeset
|
105 int noerror, nodash_now, stopearly; |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
106 uint32_t excludes; |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
107 }; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
108 |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
109 // Parse one command line option. |
294
3de1ea4d6b56
Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents:
289
diff
changeset
|
110 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
|
111 { |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
112 int type; |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
113 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
|
114 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
115 // Did we recognize this option? |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
116 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
|
117 if (gof->noerror) return 1; |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
118 error_exit("Unknown option %s", gof->arg); |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
119 } |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
120 |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
121 // Set flags |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
122 toys.optflags |= opt->edx[0]; |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
123 toys.optflags &= ~opt->edx[1]; |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
124 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
|
125 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
|
126 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
127 // Does this option take an argument? |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
128 gof->arg++; |
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
129 type = opt->type; |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
130 if (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
|
131 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
|
132 |
993eab821bd5
More work on option parsing. "df -t tmpfs" actually seems to work now.
Rob Landley <rob@landley.net>
parents:
29
diff
changeset
|
133 // 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
|
134 // 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
|
135 // "tar -x -C blah1 -j -f blah2 -v thingy" |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
136 |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
137 if (gof->nodash_now || !arg[0]) arg = toys.argv[++gof->argc]; |
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
|
138 // 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
|
139 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
|
140 |
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
|
141 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
|
142 else if (type == '*') { |
298
1bb9f53a7101
Assemble '*' repeated argument list in order. Also implement '@' counter.
Rob Landley <rob@landley.net>
parents:
294
diff
changeset
|
143 struct arg_list **list; |
1bb9f53a7101
Assemble '*' repeated argument list in order. Also implement '@' counter.
Rob Landley <rob@landley.net>
parents:
294
diff
changeset
|
144 |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
145 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
|
146 while (*list) list=&((*list)->next); |
1bb9f53a7101
Assemble '*' repeated argument list in order. Also implement '@' counter.
Rob Landley <rob@landley.net>
parents:
294
diff
changeset
|
147 *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
|
148 (*list)->arg = arg; |
415
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
149 } else if (type == '#') { |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
150 long l = atolx(arg); |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
151 if (l < opt->val[0].l) |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
152 error_exit("-%c < %ld", opt->c, opt->val[0].l); |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
153 if (l > opt->val[1].l) |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
154 error_exit("-%c > %ld", opt->c, opt->val[1].l); |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
155 |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
156 *(opt->arg) = l; |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
157 } else if (CFG_TOYBOX_FLOAT && type == '.') { |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
158 FLOAT *f = (FLOAT *)(opt->arg); |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
159 |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
160 *f = strtod(arg, &arg); |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
161 if (opt->val[0].l != LONG_MIN && *f < opt->val[0].f) |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
162 error_exit("-%c < %lf", opt->c, (double)opt->val[0].f); |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
163 if (opt->val[1].l != LONG_MAX && *f > opt->val[1].f) |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
164 error_exit("-%c > %lf", opt->c, (double)opt->val[1].f); |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
165 } else if (type == '@') ++*(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
|
166 |
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
|
167 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
|
168 } |
32
993eab821bd5
More work on option parsing. "df -t tmpfs" actually seems to work now.
Rob Landley <rob@landley.net>
parents:
29
diff
changeset
|
169 |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
170 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
|
171 return 0; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
172 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
173 |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
174 // 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
|
175 |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
176 void parse_optflaglist(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
|
177 { |
415
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
178 char *options = toys.which->options, *plustildenot = "+~!", *limits = "<>="; |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
179 long *nextarg = (long *)&this; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
180 struct opts *new = 0; |
415
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
181 int i; |
28
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
182 |
415
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
183 // Parse option format string |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
184 bzero(gof, sizeof(struct getoptflagstate)); |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
185 gof->maxargs = INT_MAX; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
186 if (!options) return; |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
187 |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
188 // Parse leading special behavior indicators |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
189 for (;;) { |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
190 if (*options == '^') gof->stopearly++; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
191 else if (*options == '<') gof->minargs=*(++options)-'0'; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
192 else if (*options == '>') gof->maxargs=*(++options)-'0'; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
193 else if (*options == '?') gof->noerror++; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
194 else if (*options == '&') gof->nodash++; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
195 else break; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
196 options++; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
197 } |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
198 |
415
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
199 // Parse the rest of the option string into a linked list |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
200 // of options with attributes. |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
201 |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
202 if (!*options) gof->stopearly++; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
203 while (*options) { |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
204 char *temp; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
205 |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
206 // Allocate a new list entry when necessary |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
207 if (!new) { |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
208 new = xzalloc(sizeof(struct opts)); |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
209 new->next = gof->opts; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
210 gof->opts = new; |
415
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
211 new->val[0].l = LONG_MIN; |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
212 new->val[1].l = LONG_MAX; |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
213 ++*(new->edx); |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
214 } |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
215 // Each option must start with "(" or an option character. (Bare |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
216 // longopts only come at the start of the string.) |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
217 if (*options == '(') { |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
218 char *end; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
219 struct longopts *lo = xmalloc(sizeof(struct longopts)); |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
220 |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
221 // Find the end of the longopt |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
222 for (end = ++options; *end && *end != ')'; end++); |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
223 if (CFG_TOYBOX_DEBUG && !*end) |
415
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
224 error_exit("(longopt) didn't end"); |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
225 |
424
85a5e01e7ad8
Removing unecessary allocation.
Luis Felipe Strano Moraes <luis.strano@gmail.com>
parents:
415
diff
changeset
|
226 // init a new struct longopts |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
227 lo->next = gof->longopts; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
228 lo->opt = new; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
229 lo->str = options; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
230 lo->len = end-options; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
231 gof->longopts = lo; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
232 options = end; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
233 |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
234 // Mark this struct opt as used, even when no short opt. |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
235 if (!new->c) new->c = -1; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
236 |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
237 // 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
|
238 |
415
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
239 } else if (strchr(":*#@.", *options)) { |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
240 if (CFG_TOYBOX_DEBUG && new->type) |
415
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
241 error_exit("multiple types %c:%c%c", new->c, new->type, *options); |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
242 new->type = *options; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
243 } else if (0 != (temp = strchr(plustildenot, *options))) { |
415
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
244 int idx = temp - plustildenot; |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
245 struct opts *opt; |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
246 |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
247 if (!*++options && CFG_TOYBOX_DEBUG) |
415
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
248 error_exit("+~! no target"); |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
249 // Find this option flag (in previously parsed struct opt) |
415
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
250 for (i=0, opt = new; ; opt = opt->next) { |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
251 if (CFG_TOYBOX_DEBUG && !opt) |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
252 error_exit("+~! unknown target"); |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
253 if (opt->c == *options) break; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
254 i++; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
255 } |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
256 new->edx[idx] |= 1<<i; |
415
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
257 } else if (*options == '[') { // TODO |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
258 } else if (*options == '|') new->flags |= 1; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
259 else if (*options == '^') new->flags |= 2; |
415
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
260 // bounds checking |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
261 else if (0 != (temp = strchr(limits, *options))) { |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
262 i = temp - limits; |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
263 if (new->type == '#') { |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
264 long l = strtol(++options, &temp, 10); |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
265 if (temp != options) new->val[i].l = l; |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
266 } else if (CFG_TOYBOX_FLOAT && new->type == '.') { |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
267 FLOAT f = strtod(++options, &temp); |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
268 if (temp != options) new->val[i].f = f; |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
269 } else if (CFG_TOYBOX_DEBUG) error_exit("<>= only after .#"); |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
270 options = --temp; |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
271 } |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
272 |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
273 // At this point, we've hit the end of the previous option. The |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
274 // current character is the start of a new option. If we've already |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
275 // assigned an option to this struct, loop to allocate a new one. |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
276 // (It'll get back here afterwards and fall through to next else.) |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
277 else if (new->c) { |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
278 new = NULL; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
279 continue; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
280 |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
281 // Claim this option, loop to see what's after it. |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
282 } else new->c = *options; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
283 |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
284 options++; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
285 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
286 |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
287 // 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
|
288 // (We have to calculate all this ahead of time because longopts jump into |
415
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
289 // the middle of the list. We have to do this after creating the list |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
290 // because we reverse direction: last entry created gets first global slot.) |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
291 int pos = 0; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
292 for (new = gof->opts; new; new = new->next) { |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
293 for (i=0;i<3;i++) new->edx[i] <<= pos; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
294 pos++; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
295 if (new->type) { |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
296 new->arg = (void *)nextarg; |
415
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
297 *(nextarg++) = new->val[2].l; |
78
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
298 } |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
299 } |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
300 } |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
301 |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
302 void get_optflags(void) |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
303 { |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
304 struct getoptflagstate gof; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
305 long saveflags; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
306 char *letters[]={"s",""}; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
307 |
415
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
308 // Option parsing is a two stage process: parse the option string into |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
309 // a struct opts list, then use that list to process argv[]; |
2cbbd4c5eaaa
Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents:
393
diff
changeset
|
310 |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
311 if (CFG_HELP) toys.exithelp++; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
312 // Allocate memory for optargs |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
313 saveflags = 0; |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
314 while (toys.argv[saveflags++]); |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
315 toys.optargs = xzalloc(sizeof(char *)*saveflags); |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
316 |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
317 parse_optflaglist(&gof); |
25
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 // 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
|
320 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
|
321 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
|
322 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
|
323 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
324 // 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
|
325 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
|
326 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
327 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
|
328 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
329 // 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
|
330 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
|
331 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
332 // 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
|
333 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
|
334 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
|
335 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
|
336 struct longopts *lo; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
337 |
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
|
338 gof.arg++; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
339 // 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
|
340 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
|
341 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
|
342 goto notflag; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
343 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
344 // Handle --longopt |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
345 |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
346 for (lo = gof.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
|
347 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
|
348 if (gof.arg[lo->len]) { |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
349 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
|
350 gof.arg += lo->len; |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
351 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
|
352 } |
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
|
353 // 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
|
354 gof.arg = ""; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
355 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
|
356 break; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
357 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
358 } |
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
|
359 |
294
3de1ea4d6b56
Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents:
289
diff
changeset
|
360 // 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
|
361 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
|
362 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
|
363 goto notflag; |
3de1ea4d6b56
Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents:
289
diff
changeset
|
364 } |
3de1ea4d6b56
Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents:
289
diff
changeset
|
365 |
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
|
366 // Long option parsed, handle option. |
261
ae693c7bf2f7
Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents:
255
diff
changeset
|
367 gotflag(&gof); |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
368 continue; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
369 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
370 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
371 // 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
|
372 } else { |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
373 if (gof.nodash && (gof.nodash>1 || gof.argc == 1)) |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
374 gof.nodash_now = 1; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
375 else goto notflag; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
376 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
377 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
378 // 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
|
379 // 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
|
380 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
|
381 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
|
382 |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
383 // 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
|
384 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
|
385 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
|
386 |
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
|
387 // 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
|
388 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
|
389 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
|
390 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
|
391 goto notflag; |
3de1ea4d6b56
Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents:
289
diff
changeset
|
392 } |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
393 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
394 continue; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
395 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
396 // 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
|
397 notflag: |
306
523441f8ed01
Teach option parsing logic that ^ means stop parsing after this option.
Rob Landley <rob@landley.net>
parents:
304
diff
changeset
|
398 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
|
399 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
|
400 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
401 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
402 // Sanity check |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
403 if (toys.optc<gof.minargs) { |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
404 error_exit("Need%s %d argument%s", letters[!!(gof.minargs-1)], |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
405 gof.minargs, letters[!(gof.minargs-1)]); |
289 | 406 } |
393
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
407 if (toys.optc>gof.maxargs) |
bfc208c5ac79
Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents:
392
diff
changeset
|
408 error_exit("Max %d argument%s", gof.maxargs, letters[!(gof.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
|
409 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
|
410 } |