comparison lib/args.c @ 684:4d9fa8b8a300

Use stridx.
author Rob Landley <rob@landley.net>
date Fri, 02 Nov 2012 09:50:09 -0500
parents 1e8b9acdafeb
children 786841fdb1e0
comparison
equal deleted inserted replaced
683:8d7fbb4c2205 684:4d9fa8b8a300
177 177
178 // Fill out toys.optflags and toys.optargs. 178 // Fill out toys.optflags and toys.optargs.
179 179
180 void parse_optflaglist(struct getoptflagstate *gof) 180 void parse_optflaglist(struct getoptflagstate *gof)
181 { 181 {
182 char *options = toys.which->options, *plustildenot = "+~!", 182 char *options = toys.which->options;
183 *limits = "<>=", *flagbits="|^ ";
184 long *nextarg = (long *)&this; 183 long *nextarg = (long *)&this;
185 struct opts *new = 0; 184 struct opts *new = 0;
186 int i;
187 185
188 // Parse option format string 186 // Parse option format string
189 memset(gof, 0, sizeof(struct getoptflagstate)); 187 memset(gof, 0, sizeof(struct getoptflagstate));
190 gof->maxargs = INT_MAX; 188 gof->maxargs = INT_MAX;
191 if (!options) return; 189 if (!options) return;
205 // of options with attributes. 203 // of options with attributes.
206 204
207 if (!*options) gof->stopearly++; 205 if (!*options) gof->stopearly++;
208 while (*options) { 206 while (*options) {
209 char *temp; 207 char *temp;
208 int idx;
210 209
211 // Allocate a new list entry when necessary 210 // Allocate a new list entry when necessary
212 if (!new) { 211 if (!new) {
213 new = xzalloc(sizeof(struct opts)); 212 new = xzalloc(sizeof(struct opts));
214 new->next = gof->opts; 213 new->next = gof->opts;
243 242
244 } else if (strchr(":*#@.-", *options)) { 243 } else if (strchr(":*#@.-", *options)) {
245 if (CFG_TOYBOX_DEBUG && new->type) 244 if (CFG_TOYBOX_DEBUG && new->type)
246 error_exit("multiple types %c:%c%c", new->c, new->type, *options); 245 error_exit("multiple types %c:%c%c", new->c, new->type, *options);
247 new->type = *options; 246 new->type = *options;
248 } else if (0 != (temp = strchr(plustildenot, *options))) { 247 } else if (-1 != (idx = stridx("+~!", *options))) {
249 int idx = temp - plustildenot;
250 struct opts *opt; 248 struct opts *opt;
249 int i;
251 250
252 if (!*++options && CFG_TOYBOX_DEBUG) 251 if (!*++options && CFG_TOYBOX_DEBUG)
253 error_exit("+~! no target"); 252 error_exit("+~! no target");
254 // Find this option flag (in previously parsed struct opt) 253 // Find this option flag (in previously parsed struct opt)
255 for (i=0, opt = new; ; opt = opt->next) { 254 for (i=0, opt = new; ; opt = opt->next) {
258 if (opt->c == *options) break; 257 if (opt->c == *options) break;
259 i++; 258 i++;
260 } 259 }
261 new->edx[idx] |= 1<<i; 260 new->edx[idx] |= 1<<i;
262 } else if (*options == '[') { // TODO 261 } else if (*options == '[') { // TODO
263 } else if (0 != (temp = strchr(flagbits, *options))) 262 } else if (-1 != (idx = stridx("|^ ", *options)))
264 new->flags |= 1<<(temp-flagbits); 263 new->flags |= 1<<idx;
265 // bounds checking 264 // bounds checking
266 else if (0 != (temp = strchr(limits, *options))) { 265 else if (-1 != (idx = stridx("<>=", *options))) {
267 i = temp - limits;
268 if (new->type == '#') { 266 if (new->type == '#') {
269 long l = strtol(++options, &temp, 10); 267 long l = strtol(++options, &temp, 10);
270 if (temp != options) new->val[i].l = l; 268 if (temp != options) new->val[idx].l = l;
271 } else if (CFG_TOYBOX_FLOAT && new->type == '.') { 269 } else if (CFG_TOYBOX_FLOAT && new->type == '.') {
272 FLOAT f = strtod(++options, &temp); 270 FLOAT f = strtod(++options, &temp);
273 if (temp != options) new->val[i].f = f; 271 if (temp != options) new->val[idx].f = f;
274 } else if (CFG_TOYBOX_DEBUG) error_exit("<>= only after .#"); 272 } else if (CFG_TOYBOX_DEBUG) error_exit("<>= only after .#");
275 options = --temp; 273 options = --temp;
276 } 274 }
277 275
278 // At this point, we've hit the end of the previous option. The 276 // At this point, we've hit the end of the previous option. The
293 // (We have to calculate all this ahead of time because longopts jump into 291 // (We have to calculate all this ahead of time because longopts jump into
294 // the middle of the list. We have to do this after creating the list 292 // the middle of the list. We have to do this after creating the list
295 // because we reverse direction: last entry created gets first global slot.) 293 // because we reverse direction: last entry created gets first global slot.)
296 int pos = 0; 294 int pos = 0;
297 for (new = gof->opts; new; new = new->next) { 295 for (new = gof->opts; new; new = new->next) {
296 int i;
297
298 for (i=0;i<3;i++) new->edx[i] <<= pos; 298 for (i=0;i<3;i++) new->edx[i] <<= pos;
299 pos++; 299 pos++;
300 if (new->type) { 300 if (new->type) {
301 new->arg = (void *)nextarg; 301 new->arg = (void *)nextarg;
302 *(nextarg++) = new->val[2].l; 302 *(nextarg++) = new->val[2].l;