Mercurial > hg > toybox
comparison lib/args.c @ 25:eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
existing applets. Still a bit buggy, but bits of it work.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 19 Nov 2006 02:49:22 -0500 |
parents | |
children | 75b80e55bcfc |
comparison
equal
deleted
inserted
replaced
24:c8d0f1876c40 | 25:eb46bb5626cb |
---|---|
1 /* vi: set sw=4 ts=4 : | |
2 * args.c - Command line argument parsing. | |
3 * | |
4 * Copyright 2006 Rob Landley <rob@landley.net> | |
5 */ | |
6 | |
7 #include "toys.h" | |
8 | |
9 // Design goals: | |
10 // Don't use getopt() | |
11 // Don't permute original arguments. | |
12 // handle --long gracefully "(noshort)a(along)b(blong1)(blong2)" | |
13 // After each argument: | |
14 // Note that pointer and long are always the same size, even on 64 bit. | |
15 // : plus a string argument, keep most recent if more than one | |
16 // * plus a string argument, appended to a list | |
17 // ? plus a signed long argument (TODO: Bounds checking?) | |
18 // @ plus an occurrence counter (which is a long) | |
19 // | this is required. If more than one marked, only one required. | |
20 // (longopt) | |
21 // +X enabling this enables X (switch on) | |
22 // ~X enabling this disables X (switch off) | |
23 // x~x means toggle x, I.E. specifying it again switches it off. | |
24 // !X die with error if X already set (x!x die if x supplied twice) | |
25 // [yz] needs at least one of y or z. | |
26 // at the beginning: | |
27 // + stop at first nonoption argument | |
28 // ? return array of remaining arguments in first vararg | |
29 // <0 at least # leftover arguments needed (default 0) | |
30 // >9 at most # leftover arguments needed (default MAX_INT) | |
31 // # don't show_usage() on unknown argument. | |
32 // & first argument has imaginary dash (ala tar/ps) | |
33 // If given twice, all arguments have imaginary dash | |
34 | |
35 // Notes from getopt man page | |
36 // - and -- cannot be arguments. | |
37 // -- force end of arguments | |
38 // - is a synonym for stdin in file arguments | |
39 // -abc means -a -b -c | |
40 | |
41 /* This uses a getopt-like option string, but not getopt() itself. | |
42 * | |
43 * Each option in options corresponds to a bit position in the return | |
44 * value (last argument is (1<<0), the next to last is (1<<1) and so on. | |
45 * If the option isn't seen in argv its bit is 0. Options which have an | |
46 * argument use the next vararg. (So varargs used by options go from left to | |
47 * right, but bits set by arguments go from right to left.) | |
48 * | |
49 * Example: | |
50 * get_optflags("ab:c:d", NULL, &bstring, &cstring); | |
51 * argv = ["command", "-b", "fruit", "-d"] | |
52 * flags = 5, bstring="fruit", cstring=NULL; | |
53 */ | |
54 | |
55 struct opts { | |
56 struct opts *next; | |
57 char c; | |
58 int type; | |
59 int shift; | |
60 void *arg; | |
61 }; | |
62 | |
63 struct getoptflagstate | |
64 { | |
65 int argc; | |
66 char *arg; | |
67 struct opts *opts, *this; | |
68 int noerror, nodash_now; | |
69 }; | |
70 | |
71 static struct getoptflagstate gof; | |
72 | |
73 // Returns zero if it didn't consume the rest of the current -abcdef | |
74 static int gotflag(void) | |
75 { | |
76 char *arg = NULL; | |
77 int type; | |
78 int ret = 0; | |
79 | |
80 // Did we recognize this option? | |
81 if (!gof.this && !gof.noerror) error_exit("Unknown option %s\n", gof.arg); | |
82 else toys.optflags |= 1 << gof.this->shift; | |
83 | |
84 // Does this option take an argument? | |
85 gof.arg++; | |
86 if (gof.this->type & 255) { | |
87 // Make "tar xCjfv blah1 blah2 thingy" work like | |
88 // "tar -x -C blah1 -j -f blah2 -v thingy" | |
89 if (!gof.nodash_now && !*gof.arg) { | |
90 gof.arg = toys.argv[++gof.argc]; | |
91 if (!gof.arg) error_exit("Missing argument"); | |
92 } else { | |
93 arg = gof.arg; | |
94 ret++; | |
95 } | |
96 } else gof.this = NULL; | |
97 | |
98 // If the last option had an argument, grab it. | |
99 if (!gof.this) return 0; | |
100 type = gof.this->type & 255; | |
101 if (!gof.arg && !(gof.arg = toys.argv[++gof.argc])) | |
102 error_exit("Missing argument"); | |
103 if (type == ':') gof.this->arg = arg; | |
104 else if (type == '*') { | |
105 struct arg_list *temp, **list; | |
106 list = (struct arg_list **)gof.this->arg; | |
107 temp = xmalloc(sizeof(struct arg_list)); | |
108 temp->arg = arg; | |
109 temp->next = *list; | |
110 *list = temp; | |
111 } else if (type == '?') { | |
112 } else if (type == '@') { | |
113 } | |
114 | |
115 return ret; | |
116 } | |
117 | |
118 // Fill out toys.optflags and toys.optargs. This isn't reentrant because | |
119 // we don't bzero(&gof, sizeof(gof)); | |
120 | |
121 void get_optflags(void) | |
122 { | |
123 int stopearly = 0, optarg = 0, nodash = 0, minargs = 0, maxargs = INT_MAX; | |
124 struct longopts { | |
125 struct longopts *next; | |
126 struct opts *opt; | |
127 char *str; | |
128 int len; | |
129 } *longopts = NULL; | |
130 long *nextarg = (long *)&toy; | |
131 char *options = toys.which->options; | |
132 | |
133 // Parse leading special behavior indicators | |
134 for (;;) { | |
135 if (*options == '+') stopearly++; | |
136 else if (*options == '<') minargs=*(++options)-'0'; | |
137 else if (*options == '>') maxargs=*(++options)-'0'; | |
138 else if (*options == '#') gof.noerror++; | |
139 else if (*options == '&') nodash++; | |
140 else break; | |
141 options++; | |
142 } | |
143 | |
144 // Parse rest of opts into array | |
145 while (*options) { | |
146 | |
147 // Allocate a new option entry when necessary | |
148 if (!gof.this) { | |
149 gof.this = xzalloc(sizeof(struct opts)); | |
150 gof.this->next = gof.opts; | |
151 gof.opts = gof.this; | |
152 } | |
153 // Each option must start with (or an option character. (Bare | |
154 // longopts only come at the start of the string.) | |
155 if (*options == '(') { | |
156 char *end; | |
157 struct longopts *lo = xmalloc(sizeof(struct longopts)); | |
158 | |
159 // Find the end of the longopt | |
160 for (end = ++options; *end && *end != ')'; end++); | |
161 if (CFG_DEBUG && !*end) error_exit("Unterminated optstring"); | |
162 | |
163 // Allocate and init a new struct longopts | |
164 lo = xmalloc(sizeof(struct longopts)); | |
165 lo->next = longopts; | |
166 lo->opt = gof.this; | |
167 lo->str = options; | |
168 lo->len = end-options; | |
169 longopts = lo; | |
170 options = end; | |
171 | |
172 // For leading longopts (with no corresponding short opt), note | |
173 // that this option struct has been used. | |
174 gof.this->shift++; | |
175 | |
176 // If this is the start of a new option that wasn't a longopt, | |
177 | |
178 } else if (index(":*?@", *options)) { | |
179 gof.this->type |= *options; | |
180 // Pointer and long guaranteed to be the same size by LP64. | |
181 *(++nextarg) = 0; | |
182 gof.this->arg = (void *)nextarg; | |
183 } else if (*options == '|') { | |
184 } else if (*options == '+') { | |
185 } else if (*options == '~') { | |
186 } else if (*options == '!') { | |
187 } else if (*options == '[') { | |
188 | |
189 // At this point, we've hit the end of the previous option. The | |
190 // current character is the start of a new option. If we've already | |
191 // assigned an option to this struct, loop to allocate a new one. | |
192 // (It'll get back here afterwards.) | |
193 } else if(gof.this->shift || gof.this->c) { | |
194 gof.this = NULL; | |
195 continue; | |
196 | |
197 // Claim this option, loop to see what's after it. | |
198 } else gof.this->c = *options; | |
199 | |
200 options++; | |
201 } | |
202 | |
203 // Initialize shift bits (have to calculate this ahead of time because | |
204 // longopts jump into the middle of the list), and allocate space to | |
205 // store optargs. | |
206 gof.argc = 0; | |
207 for (gof.this = gof.opts; gof.this; gof.this = gof.this->next) | |
208 gof.this->shift = gof.argc++; | |
209 toys.optargs = xzalloc(sizeof(char *)*(++gof.argc)); | |
210 | |
211 // Iterate through command line arguments, skipping argv[0] | |
212 for (gof.argc=1; toys.argv[gof.argc]; gof.argc++) { | |
213 char *arg = toys.argv[gof.argc]; | |
214 | |
215 // Parse this argument | |
216 if (stopearly>1) goto notflag; | |
217 | |
218 gof.nodash_now = 0; | |
219 | |
220 // Various things with dashes | |
221 if (*arg == '-') { | |
222 | |
223 // Handle - | |
224 if (!arg[1]) goto notflag; | |
225 arg++; | |
226 if (*arg=='-') { | |
227 struct longopts *lo; | |
228 | |
229 arg++; | |
230 // Handle -- | |
231 if (!*arg) { | |
232 stopearly += 2; | |
233 goto notflag; | |
234 } | |
235 // Handle --longopt | |
236 | |
237 for (lo = longopts; lo; lo = lo->next) { | |
238 if (!strncmp(arg, lo->str, lo->len)) { | |
239 if (arg[lo->len]) { | |
240 if (arg[lo->len]=='=' | |
241 && (lo->opt->type & 255)) | |
242 { | |
243 arg += lo->len; | |
244 } else continue; | |
245 | |
246 // *options should be nul, this makes sure | |
247 // that the while (*arg) loop terminates; | |
248 } arg = options-1; | |
249 gof.this = lo->opt; | |
250 break; | |
251 } | |
252 } | |
253 // Long option parsed, jump to option handling. | |
254 gotflag(); | |
255 continue; | |
256 } | |
257 | |
258 // Handle things that don't start with a dash. | |
259 } else { | |
260 if (nodash && (nodash>1 || gof.argc == 1)) gof.nodash_now = 1; | |
261 else goto notflag; | |
262 } | |
263 | |
264 // At this point, we have the args part of -args. Loop through | |
265 // each entry (could be -abc meaning -a -b -c) | |
266 while (*arg) { | |
267 // Identify next option char. | |
268 for (gof.this = gof.opts; gof.this && *arg != gof.this->c; | |
269 gof.this = gof.this->next); | |
270 if (gotflag()) break; | |
271 arg++; | |
272 } | |
273 continue; | |
274 | |
275 // Not a flag, save value in toys.optargs[] | |
276 notflag: | |
277 if (stopearly) stopearly++; | |
278 toys.optargs[optarg++] = toys.argv[gof.argc]; | |
279 } | |
280 | |
281 // Sanity check | |
282 if (optarg<minargs) error_exit("Need %d arguments", minargs); | |
283 if (optarg>maxargs) error_exit("Max %d arguments", maxargs); | |
284 } |