annotate lib/args.c @ 587:82ffae226c40

Convert another realpath use to xrealpath().
author Rob Landley <rob@landley.net>
date Fri, 01 Jun 2012 17:59:11 -0500
parents c2f39708a4c4
children 1e8b9acdafeb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* vi: set sw=4 ts=4 :
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
2 * args.c - Command line argument parsing.
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
3 *
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
4 * Copyright 2006 Rob Landley <rob@landley.net>
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
5 */
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
6
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
7 #include "toys.h"
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
8
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
9 // Design goals:
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
10 // Don't use getopt()
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
11 // Don't permute original arguments.
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
12 // handle --long gracefully "(noshort)a(along)b(blong1)(blong2)"
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
13 // After each argument:
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
14 // Note that pointer and long are always the same size, even on 64 bit.
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
15 // : plus a string argument, keep most recent if more than one
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
16 // * plus a string argument, appended to a list
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
504
a497beb97eee Add "-" type to optargs and teach tail.c to use it. Tighten up help text, use xzalloc() and xputc() as appropriate.
Rob Landley <rob@landley.net>
parents: 499
diff changeset
21 // - plus a signed long argument defaulting to negative
415
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
22 // . 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
23 // 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
24 // 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
25 // @ 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
26 // (longopt)
499
bc4615e2e339 Teach lib/args.c that " " this option must take a _separate_ argument, so "kill -stop" and "kill -s top" aren't the same thing. Make kill.c use it, and remove leftover debug printfs.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
27 // | this is required. If more than one marked, only one required. TODO
306
523441f8ed01 Teach option parsing logic that ^ means stop parsing after this option.
Rob Landley <rob@landley.net>
parents: 304
diff changeset
28 // ^ Stop parsing after encountering this argument
499
bc4615e2e339 Teach lib/args.c that " " this option must take a _separate_ argument, so "kill -stop" and "kill -s top" aren't the same thing. Make kill.c use it, and remove leftover debug printfs.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
29 // " " (space char) the "plus an argument" must be separate
bc4615e2e339 Teach lib/args.c that " " this option must take a _separate_ argument, so "kill -stop" and "kill -s top" aren't the same thing. Make kill.c use it, and remove leftover debug printfs.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
30 // I.E. "-j 3" not "-j3". So "kill -stop" != "kill -s top"
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
31 //
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
32 // 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
33 // +X enabling this enables X (switch on)
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
34 // ~X enabling this disables X (switch off)
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
35 // !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
36 // [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
37 // 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
38 // ^ stop at first nonoption argument
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
39 // <0 die if less than # leftover arguments (default 0)
415
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
40 // >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
41 // ? 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
42 // & 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
43 // 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
44
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
45 // 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
46 // - 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
47 // -- 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
48 // - 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
49 // -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
50
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
51 /* 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
52 * 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
53 *
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
54 * 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
55 * 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
56 * 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
57 *
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
58 * 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
59 * 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
60 * (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
61 *
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
62 * 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
63 * 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
64 *
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
65 * Example:
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
66 * 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
67 * 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
68 *
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
69 * Changes to struct toys:
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
70 * toys.optflags = 5 (-b=4 | -d=1)
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
71 * toys.optargs[0]="walrus" (leftover argument)
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
72 * toys.optargs[1]=NULL (end of list)
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
73 * 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
74 *
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
75 * Changes to union this:
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
76 * 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
77 * 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
78 */
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
79
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
80 // 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
81 struct opts {
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
82 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
83 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
84 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
85 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
86 int flags; // |=1, ^=2
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
87 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
88 union {
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
89 long l;
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
90 FLOAT f;
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
91 } 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
92 };
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
93
393
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
94 struct longopts {
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
95 struct longopts *next;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
96 struct opts *opt;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
97 char *str;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
98 int len;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
99 };
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
100
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
101 // State during argument parsing.
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
102 struct getoptflagstate
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
103 {
393
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
104 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
105 char *arg;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
106 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
107 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
108 int noerror, nodash_now, stopearly;
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
109 uint32_t excludes;
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
110 };
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
111
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
112 // 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
113 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
114 {
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
115 int type;
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
116 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
117
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
118 // Did we recognize this option?
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
119 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
120 if (gof->noerror) return 1;
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
121 error_exit("Unknown option %s", gof->arg);
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
122 }
393
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
123
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
124 // Set flags
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
125 toys.optflags |= opt->edx[0];
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
126 toys.optflags &= ~opt->edx[1];
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
127 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
128 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
129
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
130 // Does this option take an argument?
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
131 gof->arg++;
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
132 type = opt->type;
393
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
133 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
134 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
135
993eab821bd5 More work on option parsing. "df -t tmpfs" actually seems to work now.
Rob Landley <rob@landley.net>
parents: 29
diff changeset
136 // 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
137 // 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
138 // "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
139
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
140 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
141 // 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
142 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
143
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
144 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
145 else if (type == '*') {
298
1bb9f53a7101 Assemble '*' repeated argument list in order. Also implement '@' counter.
Rob Landley <rob@landley.net>
parents: 294
diff changeset
146 struct arg_list **list;
1bb9f53a7101 Assemble '*' repeated argument list in order. Also implement '@' counter.
Rob Landley <rob@landley.net>
parents: 294
diff changeset
147
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
148 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
149 while (*list) list=&((*list)->next);
1bb9f53a7101 Assemble '*' repeated argument list in order. Also implement '@' counter.
Rob Landley <rob@landley.net>
parents: 294
diff changeset
150 *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
151 (*list)->arg = arg;
504
a497beb97eee Add "-" type to optargs and teach tail.c to use it. Tighten up help text, use xzalloc() and xputc() as appropriate.
Rob Landley <rob@landley.net>
parents: 499
diff changeset
152 } else if (type == '#' || type == '-') {
415
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
153 long l = atolx(arg);
504
a497beb97eee Add "-" type to optargs and teach tail.c to use it. Tighten up help text, use xzalloc() and xputc() as appropriate.
Rob Landley <rob@landley.net>
parents: 499
diff changeset
154 if (type == '-' && !ispunct(*arg)) l*=-1;
415
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
155 if (l < opt->val[0].l)
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
156 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
157 if (l > opt->val[1].l)
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
158 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
159
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
160 *(opt->arg) = l;
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
161 } else if (CFG_TOYBOX_FLOAT && type == '.') {
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
162 FLOAT *f = (FLOAT *)(opt->arg);
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
163
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
164 *f = strtod(arg, &arg);
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
165 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
166 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
167 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
168 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
169 } 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
170
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
171 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
172 }
32
993eab821bd5 More work on option parsing. "df -t tmpfs" actually seems to work now.
Rob Landley <rob@landley.net>
parents: 29
diff changeset
173
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
174 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
175 return 0;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
176 }
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
177
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
178 // 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
179
393
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
180 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
181 {
499
bc4615e2e339 Teach lib/args.c that " " this option must take a _separate_ argument, so "kill -stop" and "kill -s top" aren't the same thing. Make kill.c use it, and remove leftover debug printfs.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
182 char *options = toys.which->options, *plustildenot = "+~!",
bc4615e2e339 Teach lib/args.c that " " this option must take a _separate_ argument, so "kill -stop" and "kill -s top" aren't the same thing. Make kill.c use it, and remove leftover debug printfs.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
183 *limits = "<>=", *flagbits="|^ ";
393
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
184 long *nextarg = (long *)&this;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
185 struct opts *new = 0;
415
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
186 int i;
28
be59ed005902 Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents: 26
diff changeset
187
415
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
188 // Parse option format string
480
f558dce66095 Nathan McSween convinced me compilers that inline memset() can optimize the bzero case pretty well.
Rob Landley <rob@landley.net>
parents: 424
diff changeset
189 memset(gof, 0, sizeof(struct getoptflagstate));
393
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
190 gof->maxargs = INT_MAX;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
191 if (!options) return;
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
192
393
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
193 // 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
194 for (;;) {
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
195 if (*options == '^') gof->stopearly++;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
196 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
197 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
198 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
199 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
200 else break;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
201 options++;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
202 }
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
203
415
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
204 // 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
205 // 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
206
393
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
207 if (!*options) gof->stopearly++;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
208 while (*options) {
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
209 char *temp;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
210
393
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
211 // 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
212 if (!new) {
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
213 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
214 new->next = gof->opts;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
215 gof->opts = new;
415
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
216 new->val[0].l = LONG_MIN;
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
217 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
218 ++*(new->edx);
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
219 }
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
220 // 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
221 // 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
222 if (*options == '(') {
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
223 char *end;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
224 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
225
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
226 // 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
227 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
228 if (CFG_TOYBOX_DEBUG && !*end)
415
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
229 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
230
424
85a5e01e7ad8 Removing unecessary allocation.
Luis Felipe Strano Moraes <luis.strano@gmail.com>
parents: 415
diff changeset
231 // 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
232 lo->next = gof->longopts;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
233 lo->opt = new;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
234 lo->str = options;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
235 lo->len = end-options;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
236 gof->longopts = lo;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
237 options = end;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
238
393
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
239 // 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
240 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
241
393
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
242 // 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
243
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
244 } 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
245 if (CFG_TOYBOX_DEBUG && new->type)
415
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
246 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
247 new->type = *options;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
248 } 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
249 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
250 struct opts *opt;
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
251
393
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
252 if (!*++options && CFG_TOYBOX_DEBUG)
415
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
253 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
254 // 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
255 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
256 if (CFG_TOYBOX_DEBUG && !opt)
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
257 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
258 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
259 i++;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
260 }
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
261 new->edx[idx] |= 1<<i;
415
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
262 } else if (*options == '[') { // TODO
499
bc4615e2e339 Teach lib/args.c that " " this option must take a _separate_ argument, so "kill -stop" and "kill -s top" aren't the same thing. Make kill.c use it, and remove leftover debug printfs.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
263 } else if (0 != (temp = strchr(flagbits, *options)))
bc4615e2e339 Teach lib/args.c that " " this option must take a _separate_ argument, so "kill -stop" and "kill -s top" aren't the same thing. Make kill.c use it, and remove leftover debug printfs.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
264 new->flags |= 1<<(temp-flagbits);
415
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
265 // bounds checking
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
266 else if (0 != (temp = strchr(limits, *options))) {
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
267 i = temp - limits;
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
268 if (new->type == '#') {
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
269 long l = strtol(++options, &temp, 10);
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
270 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
271 } else if (CFG_TOYBOX_FLOAT && new->type == '.') {
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
272 FLOAT f = strtod(++options, &temp);
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
273 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
274 } 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
275 options = --temp;
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
276 }
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
277
393
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
278 // 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
279 // 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
280 // 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
281 // (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
282 else if (new->c) {
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
283 new = NULL;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
284 continue;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
285
393
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
286 // 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
287 } 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
288
393
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
289 options++;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
290 }
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
291
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
292 // 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
293 // (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
294 // 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
295 // 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
296 int pos = 0;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
297 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
298 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
299 pos++;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
300 if (new->type) {
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
301 new->arg = (void *)nextarg;
415
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
302 *(nextarg++) = new->val[2].l;
78
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
303 }
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
304 }
393
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
305 }
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
306
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
307 void get_optflags(void)
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
308 {
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
309 struct getoptflagstate gof;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
310 long saveflags;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
311 char *letters[]={"s",""};
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
312
415
2cbbd4c5eaaa Add <>= to lib/args.c, with documentation.
Rob Landley <rob@landley.net>
parents: 393
diff changeset
313 // 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
314 // 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
315
393
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
316 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
317 // Allocate memory for optargs
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
318 saveflags = 0;
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
319 while (toys.argv[saveflags++]);
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
320 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
321
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
322 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
323
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
324 // 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
325 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
326 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
327 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
328
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
329 // 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
330 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
331
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
332 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
333
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
334 // 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
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
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
337 // 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
338 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
339 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
340 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
341 struct longopts *lo;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
342
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
343 gof.arg++;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
344 // 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
345 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
346 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
347 goto notflag;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
348 }
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
349 // Handle --longopt
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
350
393
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
351 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
352 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
353 if (gof.arg[lo->len]) {
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
354 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
355 gof.arg += lo->len;
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
356 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
357 }
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
358 // 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
359 gof.arg = "";
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
360 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
361 break;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
362 }
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
363 }
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
364
294
3de1ea4d6b56 Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents: 289
diff changeset
365 // 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
366 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
367 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
368 goto notflag;
3de1ea4d6b56 Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents: 289
diff changeset
369 }
3de1ea4d6b56 Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents: 289
diff changeset
370
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
371 // Long option parsed, handle option.
261
ae693c7bf2f7 Add enable/disable/exclude logic, update docs.
Rob Landley <rob@landley.net>
parents: 255
diff changeset
372 gotflag(&gof);
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
373 continue;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
374 }
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
375
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
376 // 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
377 } else {
393
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
378 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
379 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
380 else goto notflag;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
381 }
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
382
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
383 // 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
384 // 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
385 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
386 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
387
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
388 // 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
389 for (gof.this = gof.opts; gof.this; gof.this = gof.this->next)
499
bc4615e2e339 Teach lib/args.c that " " this option must take a _separate_ argument, so "kill -stop" and "kill -s top" aren't the same thing. Make kill.c use it, and remove leftover debug printfs.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
390 if (*gof.arg == gof.this->c)
bc4615e2e339 Teach lib/args.c that " " this option must take a _separate_ argument, so "kill -stop" and "kill -s top" aren't the same thing. Make kill.c use it, and remove leftover debug printfs.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
391 if (!((gof.this->flags&4) && gof.arg[1])) break;
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
392
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
393 // 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
394 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
395 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
396 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
397 goto notflag;
3de1ea4d6b56 Fix command line option parsing so "echo -xen" actually prints "-xen". Add
Rob Landley <rob@landley.net>
parents: 289
diff changeset
398 }
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
399 }
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
400 continue;
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 // 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
403 notflag:
306
523441f8ed01 Teach option parsing logic that ^ means stop parsing after this option.
Rob Landley <rob@landley.net>
parents: 304
diff changeset
404 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
405 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
406 }
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
407
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
408 // Sanity check
393
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
409 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
410 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
411 gof.minargs, letters[!(gof.minargs-1)]);
289
a1fdf34d9504 Fluffier error message.
Rob Landley <rob@landley.net>
parents: 268
diff changeset
412 }
393
bfc208c5ac79 Split out parse_optflaglist(), and move local variables to optflagstate.
Rob Landley <rob@landley.net>
parents: 392
diff changeset
413 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
414 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
415 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
416 }