annotate lib/args.c @ 268:56a29fb3c9f6

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