comparison lib/args.c @ 1072:7a45b9b54d3d draft

Tweak args (yank old + that never worked, rename | to +), and add uname -o as a synonym for -s.
author Rob Landley <rob@landley.net>
date Sat, 21 Sep 2013 13:46:44 -0500
parents e7454bb7af5a
children 1d79c0c23f69
comparison
equal deleted inserted replaced
1071:e7454bb7af5a 1072:7a45b9b54d3d
77 // & first argument has imaginary dash (ala tar/ps) 77 // & first argument has imaginary dash (ala tar/ps)
78 // If given twice, all arguments have imaginary dash 78 // If given twice, all arguments have imaginary dash
79 // 79 //
80 // At the end: [groups] of previously seen options 80 // At the end: [groups] of previously seen options
81 // - Only one in group (switch off) [-abc] means -ab=-b, -ba=-a, -abc=-c 81 // - Only one in group (switch off) [-abc] means -ab=-b, -ba=-a, -abc=-c
82 // | Synonyms (switch on all) [|abc] means -ab=-abc, -c=-abc 82 // + Synonyms (switch on all) [+abc] means -ab=-abc, -c=-abc
83 // ! More than one in group is error [!abc] means -ab calls error_exit() 83 // ! More than one in group is error [!abc] means -ab calls error_exit()
84 // + First in group switches rest on [+abc] means -a=-abc, -b=-b, -c=-c
85 // primarily useful if you can switch things back off again. 84 // primarily useful if you can switch things back off again.
86 // 85 //
87 86
88 // Notes from getopt man page 87 // Notes from getopt man page
89 // - and -- cannot be arguments. 88 // - and -- cannot be arguments.
337 while (*options) { 336 while (*options) {
338 unsigned bits = 0; 337 unsigned bits = 0;
339 338
340 if (CFG_TOYBOX_DEBUG && *options != '[') error_exit("trailing %s", options); 339 if (CFG_TOYBOX_DEBUG && *options != '[') error_exit("trailing %s", options);
341 340
342 idx = stridx("-|!+", *++options); 341 idx = stridx("-+!", *++options);
343 if (CFG_TOYBOX_DEBUG && idx == -1) error_exit("[ needs +-!"); 342 if (CFG_TOYBOX_DEBUG && idx == -1) error_exit("[ needs +-!");
344 if (CFG_TOYBOX_DEBUG && (*options == ']' || !options)) 343 if (CFG_TOYBOX_DEBUG && (options[1] == ']' || !options[1]))
345 error_exit("empty []"); 344 error_exit("empty []");
346 345
347 // Don't advance past ] but do process it once in loop. 346 // Don't advance past ] but do process it once in loop.
348 while (*(options++) != ']') { 347 while (*options++ != ']') {
349 struct opts *opt, *opt2 = 0; 348 struct opts *opt;
350 int i; 349 int i;
351 350
352 if (CFG_TOYBOX_DEBUG && !*options) error_exit("[ without ]"); 351 if (CFG_TOYBOX_DEBUG && !*options) error_exit("[ without ]");
353 // Find this option flag (in previously parsed struct opt) 352 // Find this option flag (in previously parsed struct opt)
354 for (i=0, opt = gof->opts; ; i++, opt = opt->next) { 353 for (i=0, opt = gof->opts; ; i++, opt = opt->next) {
355 if (*options == ']') { 354 if (*options == ']') {
356 if (!opt) break; 355 if (!opt) break;
357 if (idx == 3) {
358 opt2->dex[1] |= bits;
359 break;
360 }
361 if (bits&(1<<i)) opt->dex[idx] |= bits&~(1<<i); 356 if (bits&(1<<i)) opt->dex[idx] |= bits&~(1<<i);
362 } else { 357 } else {
363 if (CFG_TOYBOX_DEBUG && !opt) 358 if (CFG_TOYBOX_DEBUG && !opt)
364 error_exit("[] unknown target %c", *options); 359 error_exit("[] unknown target %c", *options);
365 if (opt->c == *options) { 360 if (opt->c == *options) {
366 bits |= 1<<i; 361 bits |= 1<<i;
367 if (!opt2) opt2=opt;
368 break; 362 break;
369 } 363 }
370 } 364 }
371 } 365 }
372 } 366 }