aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-09-29 08:23:42 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-09-29 08:23:42 +0000
commitb02ef82c99819607df32d3261cef1d674d67893a (patch)
treeed501f4001514318d7e9d2d3928cbbd8bed983cb
parent6248a734e2d59ebe95aeb39189326d90f018804e (diff)
downloadbusybox-w32-b02ef82c99819607df32d3261cef1d674d67893a.tar.gz
busybox-w32-b02ef82c99819607df32d3261cef1d674d67893a.tar.bz2
busybox-w32-b02ef82c99819607df32d3261cef1d674d67893a.zip
getopt_ulflags: formatting fixes
-rw-r--r--libbb/getopt_ulflags.c142
1 files changed, 71 insertions, 71 deletions
diff --git a/libbb/getopt_ulflags.c b/libbb/getopt_ulflags.c
index edc6a78bb..7ad26551a 100644
--- a/libbb/getopt_ulflags.c
+++ b/libbb/getopt_ulflags.c
@@ -13,7 +13,7 @@
13/* Documentation 13/* Documentation
14 14
15unsigned long 15unsigned long
16bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...) 16bb_getopt_ulflags(int argc, char **argv, const char *applet_opts, ...)
17 17
18 The command line options must be declared in const char 18 The command line options must be declared in const char
19 *applet_opts as a string of chars, for example: 19 *applet_opts as a string of chars, for example:
@@ -74,6 +74,7 @@ const struct option *bb_applet_long_options
74 (see getopt(3)) 74 (see getopt(3))
75 75
76 static const struct option applet_long_options[] = { 76 static const struct option applet_long_options[] = {
77 //name,has_arg,flag,val
77 { "verbose", 0, 0, 'v' }, 78 { "verbose", 0, 0, 'v' },
78 { 0, 0, 0, 0 } 79 { 0, 0, 0, 0 }
79 }; 80 };
@@ -120,7 +121,7 @@ const char *bb_opt_complementally
120 int w_counter = 0; 121 int w_counter = 0;
121 bb_opt_complementally = "ww"; 122 bb_opt_complementally = "ww";
122 bb_getopt_ulflags(argc, argv, "w", &w_counter); 123 bb_getopt_ulflags(argc, argv, "w", &w_counter);
123 if(w_counter) 124 if (w_counter)
124 width = (w_counter == 1) ? 132 : INT_MAX; 125 width = (w_counter == 1) ? 132 : INT_MAX;
125 else 126 else
126 get_terminal_width(...&width...); 127 get_terminal_width(...&width...);
@@ -136,11 +137,11 @@ const char *bb_opt_complementally
136 int verbose_level = 0; 137 int verbose_level = 0;
137 bb_opt_complementally = "vv:b::b-c:c-b"; 138 bb_opt_complementally = "vv:b::b-c:c-b";
138 f = bb_getopt_ulflags(argc, argv, "vb:c", &my_b, &verbose_level); 139 f = bb_getopt_ulflags(argc, argv, "vb:c", &my_b, &verbose_level);
139 if(f & 2) // -c after -b unsets -b flag 140 if (f & 2) // -c after -b unsets -b flag
140 while(my_b) { dosomething_with(my_b->data); my_b = my_b->link; } 141 while (my_b) { dosomething_with(my_b->data); my_b = my_b->link; }
141 if(my_b) // but llist is stored if -b is specified 142 if (my_b) // but llist is stored if -b is specified
142 free_llist(my_b); 143 free_llist(my_b);
143 if(verbose_level) bb_printf("verbose level is %d\n", verbose_level); 144 if (verbose_level) bb_printf("verbose level is %d\n", verbose_level);
144 145
145Special characters: 146Special characters:
146 147
@@ -207,6 +208,9 @@ Special characters:
207 if (flags & BB_GETOPT_ERROR) 208 if (flags & BB_GETOPT_ERROR)
208 bb_show_usage(); 209 bb_show_usage();
209 210
211 "x--x" Variation of the above, it means that -x option should occur
212 at most once.
213
210 "?" A "?" as the first char in a bb_opt_complementally group means: 214 "?" A "?" as the first char in a bb_opt_complementally group means:
211 if BB_GETOPT_ERROR is detected, don't return, call bb_show_usage 215 if BB_GETOPT_ERROR is detected, don't return, call bb_show_usage
212 and exit instead. Next char after '?' can't be a digit. 216 and exit instead. Next char after '?' can't be a digit.
@@ -255,7 +259,7 @@ Special characters:
255 $ id; id -u; id -g; id -ru; id -nu; id -rg; id -ng; id -rnu; id -rng 259 $ id; id -u; id -g; id -ru; id -nu; id -rg; id -ng; id -rnu; id -rng
256 260
257 "X" A bb_opt_complementally group with just a single letter means 261 "X" A bb_opt_complementally group with just a single letter means
258 that this this option is required. If more than one such group exists, 262 that this option is required. If more than one such group exists,
259 at least one option is required to occur (not all of them). 263 at least one option is required to occur (not all of them).
260 For example from "start-stop-daemon" applet: 264 For example from "start-stop-daemon" applet:
261 265
@@ -263,9 +267,6 @@ Special characters:
263 bb_opt_complementally = "K:S:?K--S:S--K"; 267 bb_opt_complementally = "K:S:?K--S:S--K";
264 flags = bb_getopt_ulflags(argc, argv, "KS...); 268 flags = bb_getopt_ulflags(argc, argv, "KS...);
265 269
266
267 "x--x" give error if double or more used -x option
268
269 Don't forget to use ':'. For example "?322-22-23X-x-a" is interpreted as 270 Don't forget to use ':'. For example "?322-22-23X-x-a" is interpreted as
270 "?3:22:-2:2-2:2-3Xa:2--x": max 3 args; count uses of '-2'; min 2 args; 271 "?3:22:-2:2-2:2-3Xa:2--x": max 3 args; count uses of '-2'; min 2 args;
271 if there is a '-2' option then unset '-3', '-X' and '-a'; if there is 272 if there is a '-2' option then unset '-3', '-X' and '-a'; if there is
@@ -299,7 +300,7 @@ const struct option *bb_applet_long_options = bb_default_long_options;
299#endif 300#endif
300 301
301unsigned long 302unsigned long
302bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...) 303bb_getopt_ulflags(int argc, char **argv, const char *applet_opts, ...)
303{ 304{
304 unsigned long flags = 0; 305 unsigned long flags = 0;
305 unsigned long requires = 0; 306 unsigned long requires = 0;
@@ -324,7 +325,7 @@ bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
324#define FREE_FIRST_ARGV_IS_OPT 8 325#define FREE_FIRST_ARGV_IS_OPT 8
325 int spec_flgs = 0; 326 int spec_flgs = 0;
326 327
327 va_start (p, applet_opts); 328 va_start(p, applet_opts);
328 329
329 c = 0; 330 c = 0;
330 on_off = complementally; 331 on_off = complementally;
@@ -332,15 +333,15 @@ bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
332 333
333 /* skip GNU extension */ 334 /* skip GNU extension */
334 s = (const unsigned char *)applet_opts; 335 s = (const unsigned char *)applet_opts;
335 if(*s == '+' || *s == '-') 336 if (*s == '+' || *s == '-')
336 s++; 337 s++;
337 for (; *s; s++) { 338 for (; *s; s++) {
338 if(c >= (int)(sizeof(flags)*8)) 339 if (c >= (int)(sizeof(flags)*8))
339 break; 340 break;
340 on_off->opt = *s; 341 on_off->opt = *s;
341 on_off->switch_on = (1 << c); 342 on_off->switch_on = (1 << c);
342 if (s[1] == ':') { 343 if (s[1] == ':') {
343 on_off->optarg = va_arg (p, void **); 344 on_off->optarg = va_arg(p, void **);
344 do 345 do
345 s++; 346 s++;
346 while (s[1] == ':'); 347 while (s[1] == ':');
@@ -350,19 +351,19 @@ bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
350 } 351 }
351 352
352#if ENABLE_GETOPT_LONG 353#if ENABLE_GETOPT_LONG
353 for(l_o = bb_applet_long_options; l_o->name; l_o++) { 354 for (l_o = bb_applet_long_options; l_o->name; l_o++) {
354 if(l_o->flag) 355 if (l_o->flag)
355 continue; 356 continue;
356 for(on_off = complementally; on_off->opt != 0; on_off++) 357 for (on_off = complementally; on_off->opt != 0; on_off++)
357 if(on_off->opt == l_o->val) 358 if (on_off->opt == l_o->val)
358 break; 359 break;
359 if(on_off->opt == 0) { 360 if (on_off->opt == 0) {
360 if(c >= (int)(sizeof(flags)*8)) 361 if (c >= (int)(sizeof(flags)*8))
361 break; 362 break;
362 on_off->opt = l_o->val; 363 on_off->opt = l_o->val;
363 on_off->switch_on = (1 << c); 364 on_off->switch_on = (1 << c);
364 if(l_o->has_arg != no_argument) 365 if (l_o->has_arg != no_argument)
365 on_off->optarg = va_arg (p, void **); 366 on_off->optarg = va_arg(p, void **);
366 c++; 367 c++;
367 } 368 }
368 } 369 }
@@ -374,8 +375,8 @@ bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
374 if (*s == ':') 375 if (*s == ':')
375 continue; 376 continue;
376 c = s[1]; 377 c = s[1];
377 if(*s == '?') { 378 if (*s == '?') {
378 if(c < '0' || c > '9') { 379 if (c < '0' || c > '9') {
379 spec_flgs |= SHOW_USAGE_IF_ERROR; 380 spec_flgs |= SHOW_USAGE_IF_ERROR;
380 } else { 381 } else {
381 max_arg = c - '0'; 382 max_arg = c - '0';
@@ -383,9 +384,9 @@ bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
383 } 384 }
384 continue; 385 continue;
385 } 386 }
386 if(*s == '-') { 387 if (*s == '-') {
387 if(c < '0' || c > '9') { 388 if (c < '0' || c > '9') {
388 if(c == '-') { 389 if (c == '-') {
389 spec_flgs |= FIRST_ARGV_IS_OPT; 390 spec_flgs |= FIRST_ARGV_IS_OPT;
390 s++; 391 s++;
391 } else 392 } else
@@ -399,99 +400,99 @@ bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
399 for (on_off = complementally; on_off->opt; on_off++) 400 for (on_off = complementally; on_off->opt; on_off++)
400 if (on_off->opt == *s) 401 if (on_off->opt == *s)
401 break; 402 break;
402 if(c == ':' && s[2] == ':') { 403 if (c == ':' && s[2] == ':') {
403 on_off->list_flg++; 404 on_off->list_flg++;
404 continue; 405 continue;
405 } 406 }
406 if(c == ':' || c == '\0') { 407 if (c == ':' || c == '\0') {
407 requires |= on_off->switch_on; 408 requires |= on_off->switch_on;
408 continue; 409 continue;
409 } 410 }
410 if(c == '-' && (s[2] == ':' || s[2] == '\0')) { 411 if (c == '-' && (s[2] == ':' || s[2] == '\0')) {
411 flags |= on_off->switch_on; 412 flags |= on_off->switch_on;
412 on_off->incongruously |= on_off->switch_on; 413 on_off->incongruously |= on_off->switch_on;
413 s++; 414 s++;
414 continue; 415 continue;
415 } 416 }
416 if(c == *s) { 417 if (c == *s) {
417 on_off->counter = va_arg (p, int *); 418 on_off->counter = va_arg(p, int *);
418 s++; 419 s++;
419 } 420 }
420 pair = on_off; 421 pair = on_off;
421 pair_switch = &(pair->switch_on); 422 pair_switch = &(pair->switch_on);
422 for(s++; *s && *s != ':'; s++) { 423 for (s++; *s && *s != ':'; s++) {
423 if(*s == '?') { 424 if (*s == '?') {
424 pair_switch = &(pair->requires); 425 pair_switch = &(pair->requires);
425 } else if (*s == '-') { 426 } else if (*s == '-') {
426 if(pair_switch == &(pair->switch_off)) 427 if (pair_switch == &(pair->switch_off))
427 pair_switch = &(pair->incongruously); 428 pair_switch = &(pair->incongruously);
428 else 429 else
429 pair_switch = &(pair->switch_off); 430 pair_switch = &(pair->switch_off);
430 } else { 431 } else {
431 for (on_off = complementally; on_off->opt; on_off++) 432 for (on_off = complementally; on_off->opt; on_off++)
432 if (on_off->opt == *s) { 433 if (on_off->opt == *s) {
433 *pair_switch |= on_off->switch_on; 434 *pair_switch |= on_off->switch_on;
434 break; 435 break;
435 } 436 }
436 } 437 }
437 } 438 }
438 s--; 439 s--;
439 } 440 }
440 va_end (p); 441 va_end (p);
441 442
442#if defined(CONFIG_AR) || defined(CONFIG_TAR) 443#if ENABLE_AR || ENABLE_TAR
443 if((spec_flgs & FIRST_ARGV_IS_OPT)) { 444 if (spec_flgs & FIRST_ARGV_IS_OPT) {
444 if(argv[1] && argv[1][0] != '-' && argv[1][0] != '\0') { 445 if (argv[1] && argv[1][0] != '-' && argv[1][0] != '\0') {
445 argv[1] = xasprintf("-%s", argv[1]); 446 argv[1] = xasprintf("-%s", argv[1]);
446 if(ENABLE_FEATURE_CLEAN_UP) 447 if (ENABLE_FEATURE_CLEAN_UP)
447 spec_flgs |= FREE_FIRST_ARGV_IS_OPT; 448 spec_flgs |= FREE_FIRST_ARGV_IS_OPT;
448 } 449 }
449 } 450 }
450#endif 451#endif
451#if ENABLE_GETOPT_LONG 452#if ENABLE_GETOPT_LONG
452 while ((c = getopt_long (argc, argv, applet_opts, 453 while ((c = getopt_long(argc, argv, applet_opts,
453 bb_applet_long_options, NULL)) >= 0) { 454 bb_applet_long_options, NULL)) >= 0) {
454#else 455#else
455 while ((c = getopt (argc, argv, applet_opts)) >= 0) { 456 while ((c = getopt(argc, argv, applet_opts)) >= 0) {
456#endif /* ENABLE_GETOPT_LONG */ 457#endif /* ENABLE_GETOPT_LONG */
457#ifdef CONFIG_PS 458#if ENABLE_PS
458loop_arg_is_opt: 459loop_arg_is_opt:
459#endif 460#endif
460 for (on_off = complementally; on_off->opt != c; on_off++) { 461 for (on_off = complementally; on_off->opt != c; on_off++) {
461 /* c==0 if long opt have non NULL flag */ 462 /* c==0 if long opt have non NULL flag */
462 if(on_off->opt == 0 && c != 0) 463 if (on_off->opt == 0 && c != 0)
463 bb_show_usage (); 464 bb_show_usage();
464 } 465 }
465 if(flags & on_off->incongruously) { 466 if (flags & on_off->incongruously) {
466 if((spec_flgs & SHOW_USAGE_IF_ERROR)) 467 if ((spec_flgs & SHOW_USAGE_IF_ERROR))
467 bb_show_usage (); 468 bb_show_usage();
468 flags |= BB_GETOPT_ERROR; 469 flags |= BB_GETOPT_ERROR;
469 } 470 }
470 trigger = on_off->switch_on & on_off->switch_off; 471 trigger = on_off->switch_on & on_off->switch_off;
471 flags &= ~(on_off->switch_off ^ trigger); 472 flags &= ~(on_off->switch_off ^ trigger);
472 flags |= on_off->switch_on ^ trigger; 473 flags |= on_off->switch_on ^ trigger;
473 flags ^= trigger; 474 flags ^= trigger;
474 if(on_off->counter) 475 if (on_off->counter)
475 (*(on_off->counter))++; 476 (*(on_off->counter))++;
476 if(on_off->list_flg) { 477 if (on_off->list_flg) {
477 llist_add_to((llist_t **)(on_off->optarg), optarg); 478 llist_add_to((llist_t **)(on_off->optarg), optarg);
478 } else if (on_off->optarg) { 479 } else if (on_off->optarg) {
479 *(char **)(on_off->optarg) = optarg; 480 *(char **)(on_off->optarg) = optarg;
480 } 481 }
481#ifdef CONFIG_PS 482#if ENABLE_PS
482 if(pargv != NULL) 483 if (pargv != NULL)
483 break; 484 break;
484#endif 485#endif
485 } 486 }
486 487
487#ifdef CONFIG_PS 488#if ENABLE_PS
488 if((spec_flgs & ALL_ARGV_IS_OPTS)) { 489 if (spec_flgs & ALL_ARGV_IS_OPTS) {
489 /* process argv is option, for example "ps" applet */ 490 /* process argv is option, for example "ps" applet */
490 if(pargv == NULL) 491 if (pargv == NULL)
491 pargv = argv + optind; 492 pargv = argv + optind;
492 while(*pargv) { 493 while (*pargv) {
493 c = **pargv; 494 c = **pargv;
494 if(c == '\0') { 495 if (c == '\0') {
495 pargv++; 496 pargv++;
496 } else { 497 } else {
497 (*pargv)++; 498 (*pargv)++;
@@ -501,21 +502,20 @@ loop_arg_is_opt:
501 } 502 }
502#endif 503#endif
503 504
504#if (defined(CONFIG_AR) || defined(CONFIG_TAR)) && \ 505#if (ENABLE_AR || ENABLE_TAR) && ENABLE_FEATURE_CLEAN_UP
505 defined(CONFIG_FEATURE_CLEAN_UP) 506 if (spec_flgs & FREE_FIRST_ARGV_IS_OPT)
506 if((spec_flgs & FREE_FIRST_ARGV_IS_OPT))
507 free(argv[1]); 507 free(argv[1]);
508#endif 508#endif
509 /* check depending requires for given options */ 509 /* check depending requires for given options */
510 for (on_off = complementally; on_off->opt; on_off++) { 510 for (on_off = complementally; on_off->opt; on_off++) {
511 if(on_off->requires && (flags & on_off->switch_on) && 511 if (on_off->requires && (flags & on_off->switch_on) &&
512 (flags & on_off->requires) == 0) 512 (flags & on_off->requires) == 0)
513 bb_show_usage (); 513 bb_show_usage();
514 } 514 }
515 if(requires && (flags & requires) == 0) 515 if (requires && (flags & requires) == 0)
516 bb_show_usage (); 516 bb_show_usage();
517 argc -= optind; 517 argc -= optind;
518 if(argc < min_arg || (max_arg >= 0 && argc > max_arg)) 518 if (argc < min_arg || (max_arg >= 0 && argc > max_arg))
519 bb_show_usage (); 519 bb_show_usage();
520 return flags; 520 return flags;
521} 521}