summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2007-03-29 13:55:17 +0000
committerBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2007-03-29 13:55:17 +0000
commit557b458767f8cd90877630b3520933850ad9459d (patch)
treec93103f89c40e07acc0728aab9240345221808c6
parent32773aca55a25b6102b0169a6c160ad52831181a (diff)
downloadbusybox-w32-557b458767f8cd90877630b3520933850ad9459d.tar.gz
busybox-w32-557b458767f8cd90877630b3520933850ad9459d.tar.bz2
busybox-w32-557b458767f8cd90877630b3520933850ad9459d.zip
- implement a TODO: Use index_in_str_array in parse_params
text data bss dec hex filename 2771 1 12 2784 ae0 find.o.r18272 2749 1 12 2762 aca find.o.-try-switch 2706 1 12 2719 a9f find.o.r18273
-rw-r--r--findutils/find.c158
1 files changed, 118 insertions, 40 deletions
diff --git a/findutils/find.c b/findutils/find.c
index e7a514cca..453ed4b1c 100644
--- a/findutils/find.c
+++ b/findutils/find.c
@@ -336,13 +336,81 @@ static const char* plus_minus_num(const char* str)
336} 336}
337#endif 337#endif
338 338
339#define PARM_a 0
340#define PARM_o 1
341#define PARM_char_not (PARM_o + ENABLE_FEATURE_FIND_NOT)
342#define PARM_print (PARM_char_not + 1)
343#define PARM_print0 (PARM_print + ENABLE_FEATURE_FIND_PRINT0)
344#define PARM_name (PARM_print0 + 1)
345#define PARM_type (PARM_name + ENABLE_FEATURE_FIND_TYPE)
346#define PARM_perm (PARM_type + ENABLE_FEATURE_FIND_PERM)
347#define PARM_mtime (PARM_perm + ENABLE_FEATURE_FIND_MTIME)
348#define PARM_mmin (PARM_mtime + ENABLE_FEATURE_FIND_MMIN)
349#define PARM_newer (PARM_mmin + ENABLE_FEATURE_FIND_NEWER)
350#define PARM_inum (PARM_newer + ENABLE_FEATURE_FIND_INUM)
351#define PARM_exec (PARM_inum + ENABLE_FEATURE_FIND_EXEC)
352#define PARM_user (PARM_exec + ENABLE_FEATURE_FIND_USER)
353#if ENABLE_DESKTOP
354#define PARM_and (PARM_user + 1)
355#define PARM_or (PARM_and + 1)
356#define PARM_not (PARM_or + ENABLE_FEATURE_FIND_NOT)
357#define PARM_char_brace (PARM_not + 1)
358#define PARM_prune (PARM_char_brace + 1)
359#define PARM_size (PARM_prune + 1)
360#endif
339static action*** parse_params(char **argv) 361static action*** parse_params(char **argv)
340{ 362{
341 action*** appp; 363 action*** appp;
342 unsigned cur_group = 0; 364 unsigned cur_group = 0;
343 unsigned cur_action = 0; 365 unsigned cur_action = 0;
344 USE_FEATURE_FIND_NOT( bool invert_flag = 0; ) 366 USE_FEATURE_FIND_NOT( bool invert_flag = 0; )
345 367 const char * const params[] = {
368 "-a",
369 "-o",
370#if ENABLE_FEATURE_FIND_NOT
371 "!",
372#endif
373 "-print",
374#if ENABLE_FEATURE_FIND_PRINT0
375 "-print0",
376#endif
377 "-name",
378#if ENABLE_FEATURE_FIND_TYPE
379 "-type",
380#endif
381#if ENABLE_FEATURE_FIND_PERM
382 "-perm",
383#endif
384#if ENABLE_FEATURE_FIND_MTIME
385 "-mtime",
386#endif
387#if ENABLE_FEATURE_FIND_MMIN
388 "-mmin",
389#endif
390#if ENABLE_FEATURE_FIND_NEWER
391 "-newer",
392#endif
393#if ENABLE_FEATURE_FIND_INUM
394 "-inum",
395#endif
396#if ENABLE_FEATURE_FIND_EXEC
397 "-exec",
398#endif
399#if ENABLE_FEATURE_FIND_USER
400 "-user",
401#endif
402#if ENABLE_DESKTOP
403 "-and",
404 "-or",
405# if ENABLE_FEATURE_FIND_NOT
406 "-not",
407# endif
408 "(",
409 "-prune",
410 "-size",
411#endif
412 NULL
413 };
346 action* alloc_action(int sizeof_struct, action_fp f) 414 action* alloc_action(int sizeof_struct, action_fp f)
347 { 415 {
348 action *ap; 416 action *ap;
@@ -358,33 +426,31 @@ static action*** parse_params(char **argv)
358 426
359 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */ 427 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
360 428
361// Actions have side effects and return a true or false value 429/* Actions have side effects and return a true or false value
362// We implement: -print, -print0, -exec 430 * We implement: -print, -print0, -exec
363 431 *
364// The rest are tests. 432 * The rest are tests.
365 433 *
366// Tests and actions are grouped by operators 434 * Tests and actions are grouped by operators
367// ( expr ) Force precedence 435 * ( expr ) Force precedence
368// ! expr True if expr is false 436 * ! expr True if expr is false
369// -not expr Same as ! expr 437 * -not expr Same as ! expr
370// expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false 438 * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
371// expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true 439 * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
372// expr1 , expr2 List; both expr1 and expr2 are always evaluated 440 * expr1 , expr2 List; both expr1 and expr2 are always evaluated
373// We implement: (), -a, -o 441 * We implement: (), -a, -o
374 442 */
375//XXX: TODO: Use index_in_str_array here
376 while (*argv) { 443 while (*argv) {
377 const char *arg = argv[0]; 444 const char *arg = argv[0];
378 const char *arg1 = argv[1]; 445 const char *arg1 = argv[1];
446 int parm = index_in_str_array(params, arg);
379 /* --- Operators --- */ 447 /* --- Operators --- */
380 if (strcmp(arg, "-a") == 0 448 if (parm == PARM_a USE_DESKTOP(|| parm == PARM_and))
381 USE_DESKTOP(|| strcmp(arg, "-and") == 0) 449 {
382 ) {
383 /* no further special handling required */ 450 /* no further special handling required */
384 } 451 }
385 else if (strcmp(arg, "-o") == 0 452 else if (parm == PARM_o USE_DESKTOP(|| parm == PARM_or))
386 USE_DESKTOP(|| strcmp(arg, "-or") == 0) 453 {
387 ) {
388 /* start new OR group */ 454 /* start new OR group */
389 cur_group++; 455 cur_group++;
390 appp = xrealloc(appp, (cur_group+2) * sizeof(*appp)); 456 appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
@@ -393,29 +459,31 @@ static action*** parse_params(char **argv)
393 cur_action = 0; 459 cur_action = 0;
394 } 460 }
395#if ENABLE_FEATURE_FIND_NOT 461#if ENABLE_FEATURE_FIND_NOT
396 else if (LONE_CHAR(arg, '!') 462 else if (parm == PARM_char_not USE_DESKTOP(|| parm == PARM_not))
397 USE_DESKTOP(|| strcmp(arg, "-not") == 0) 463 {
398 ) {
399 /* also handles "find ! ! -name 'foo*'" */ 464 /* also handles "find ! ! -name 'foo*'" */
400 invert_flag ^= 1; 465 invert_flag ^= 1;
401 } 466 }
402#endif 467#endif
403 468
404 /* --- Tests and actions --- */ 469 /* --- Tests and actions --- */
405 else if (strcmp(arg, "-print") == 0) { 470 else if (parm == PARM_print)
471 {
406 need_print = 0; 472 need_print = 0;
407 /* GNU find ignores '!' here: "find ! -print" */ 473 /* GNU find ignores '!' here: "find ! -print" */
408 USE_FEATURE_FIND_NOT( invert_flag = 0; ) 474 USE_FEATURE_FIND_NOT( invert_flag = 0; )
409 (void) ALLOC_ACTION(print); 475 (void) ALLOC_ACTION(print);
410 } 476 }
411#if ENABLE_FEATURE_FIND_PRINT0 477#if ENABLE_FEATURE_FIND_PRINT0
412 else if (strcmp(arg, "-print0") == 0) { 478 else if (parm == PARM_print0)
479 {
413 need_print = 0; 480 need_print = 0;
414 USE_FEATURE_FIND_NOT( invert_flag = 0; ) 481 USE_FEATURE_FIND_NOT( invert_flag = 0; )
415 (void) ALLOC_ACTION(print0); 482 (void) ALLOC_ACTION(print0);
416 } 483 }
417#endif 484#endif
418 else if (strcmp(arg, "-name") == 0) { 485 else if (parm == PARM_name)
486 {
419 action_name *ap; 487 action_name *ap;
420 if (!*++argv) 488 if (!*++argv)
421 bb_error_msg_and_die(bb_msg_requires_arg, arg); 489 bb_error_msg_and_die(bb_msg_requires_arg, arg);
@@ -423,7 +491,8 @@ static action*** parse_params(char **argv)
423 ap->pattern = arg1; 491 ap->pattern = arg1;
424 } 492 }
425#if ENABLE_FEATURE_FIND_TYPE 493#if ENABLE_FEATURE_FIND_TYPE
426 else if (strcmp(arg, "-type") == 0) { 494 else if (parm == PARM_type)
495 {
427 action_type *ap; 496 action_type *ap;
428 if (!*++argv) 497 if (!*++argv)
429 bb_error_msg_and_die(bb_msg_requires_arg, arg); 498 bb_error_msg_and_die(bb_msg_requires_arg, arg);
@@ -438,7 +507,8 @@ static action*** parse_params(char **argv)
438 * -perm -mode All of the permission bits mode are set for the file. 507 * -perm -mode All of the permission bits mode are set for the file.
439 * -perm +mode Any of the permission bits mode are set for the file. 508 * -perm +mode Any of the permission bits mode are set for the file.
440 */ 509 */
441 else if (strcmp(arg, "-perm") == 0) { 510 else if (parm == PARM_perm)
511 {
442 action_perm *ap; 512 action_perm *ap;
443 if (!*++argv) 513 if (!*++argv)
444 bb_error_msg_and_die(bb_msg_requires_arg, arg); 514 bb_error_msg_and_die(bb_msg_requires_arg, arg);
@@ -451,7 +521,8 @@ static action*** parse_params(char **argv)
451 } 521 }
452#endif 522#endif
453#if ENABLE_FEATURE_FIND_MTIME 523#if ENABLE_FEATURE_FIND_MTIME
454 else if (strcmp(arg, "-mtime") == 0) { 524 else if (parm == PARM_mtime)
525 {
455 action_mtime *ap; 526 action_mtime *ap;
456 if (!*++argv) 527 if (!*++argv)
457 bb_error_msg_and_die(bb_msg_requires_arg, arg); 528 bb_error_msg_and_die(bb_msg_requires_arg, arg);
@@ -461,7 +532,8 @@ static action*** parse_params(char **argv)
461 } 532 }
462#endif 533#endif
463#if ENABLE_FEATURE_FIND_MMIN 534#if ENABLE_FEATURE_FIND_MMIN
464 else if (strcmp(arg, "-mmin") == 0) { 535 else if (parm == PARM_mmin)
536 {
465 action_mmin *ap; 537 action_mmin *ap;
466 if (!*++argv) 538 if (!*++argv)
467 bb_error_msg_and_die(bb_msg_requires_arg, arg); 539 bb_error_msg_and_die(bb_msg_requires_arg, arg);
@@ -471,7 +543,8 @@ static action*** parse_params(char **argv)
471 } 543 }
472#endif 544#endif
473#if ENABLE_FEATURE_FIND_NEWER 545#if ENABLE_FEATURE_FIND_NEWER
474 else if (strcmp(arg, "-newer") == 0) { 546 else if (parm == PARM_newer)
547 {
475 action_newer *ap; 548 action_newer *ap;
476 struct stat stat_newer; 549 struct stat stat_newer;
477 if (!*++argv) 550 if (!*++argv)
@@ -482,7 +555,8 @@ static action*** parse_params(char **argv)
482 } 555 }
483#endif 556#endif
484#if ENABLE_FEATURE_FIND_INUM 557#if ENABLE_FEATURE_FIND_INUM
485 else if (strcmp(arg, "-inum") == 0) { 558 else if (parm == PARM_inum)
559 {
486 action_inum *ap; 560 action_inum *ap;
487 if (!*++argv) 561 if (!*++argv)
488 bb_error_msg_and_die(bb_msg_requires_arg, arg); 562 bb_error_msg_and_die(bb_msg_requires_arg, arg);
@@ -491,7 +565,8 @@ static action*** parse_params(char **argv)
491 } 565 }
492#endif 566#endif
493#if ENABLE_FEATURE_FIND_EXEC 567#if ENABLE_FEATURE_FIND_EXEC
494 else if (strcmp(arg, "-exec") == 0) { 568 else if (parm == PARM_exec)
569 {
495 int i; 570 int i;
496 action_exec *ap; 571 action_exec *ap;
497 need_print = 0; 572 need_print = 0;
@@ -516,7 +591,8 @@ static action*** parse_params(char **argv)
516 } 591 }
517#endif 592#endif
518#if ENABLE_FEATURE_FIND_USER 593#if ENABLE_FEATURE_FIND_USER
519 else if (strcmp(arg, "-user") == 0) { 594 else if (parm == PARM_user)
595 {
520 action_user *ap; 596 action_user *ap;
521 if (!*++argv) 597 if (!*++argv)
522 bb_error_msg_and_die(bb_msg_requires_arg, arg); 598 bb_error_msg_and_die(bb_msg_requires_arg, arg);
@@ -527,7 +603,8 @@ static action*** parse_params(char **argv)
527 } 603 }
528#endif 604#endif
529#if ENABLE_DESKTOP 605#if ENABLE_DESKTOP
530 else if (LONE_CHAR(arg, '(')) { 606 else if (parm == PARM_char_brace)
607 {
531 action_paren *ap; 608 action_paren *ap;
532 char **endarg; 609 char **endarg;
533 unsigned nested = 1; 610 unsigned nested = 1;
@@ -548,11 +625,13 @@ static action*** parse_params(char **argv)
548 *endarg = (char*) ")"; /* restore NULLed parameter */ 625 *endarg = (char*) ")"; /* restore NULLed parameter */
549 argv = endarg; 626 argv = endarg;
550 } 627 }
551 else if (strcmp(arg, "-prune") == 0) { 628 else if (parm == PARM_prune)
629 {
552 USE_FEATURE_FIND_NOT( invert_flag = 0; ) 630 USE_FEATURE_FIND_NOT( invert_flag = 0; )
553 (void) ALLOC_ACTION(prune); 631 (void) ALLOC_ACTION(prune);
554 } 632 }
555 else if (strcmp(arg, "-size") == 0) { 633 else if (parm == PARM_size)
634 {
556 action_size *ap; 635 action_size *ap;
557 if (!*++argv) 636 if (!*++argv)
558 bb_error_msg_and_die(bb_msg_requires_arg, arg); 637 bb_error_msg_and_die(bb_msg_requires_arg, arg);
@@ -564,7 +643,6 @@ static action*** parse_params(char **argv)
564 bb_show_usage(); 643 bb_show_usage();
565 argv++; 644 argv++;
566 } 645 }
567
568 return appp; 646 return appp;
569#undef ALLOC_ACTION 647#undef ALLOC_ACTION
570} 648}