Changeset 3232 in MondoRescue for branches/3.2/mindi-busybox/findutils/grep.c
- Timestamp:
- Jan 1, 2014, 12:47:38 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.2/mindi-busybox/findutils/grep.c
r2725 r3232 19 19 */ 20 20 21 //applet:IF_GREP(APPLET(grep, _BB_DIR_BIN, _BB_SUID_DROP))22 //applet:IF_FEATURE_GREP_EGREP_ALIAS(APPLET_ODDNAME(egrep, grep, _BB_DIR_BIN, _BB_SUID_DROP, egrep))23 //applet:IF_FEATURE_GREP_FGREP_ALIAS(APPLET_ODDNAME(fgrep, grep, _BB_DIR_BIN, _BB_SUID_DROP, fgrep))21 //applet:IF_GREP(APPLET(grep, BB_DIR_BIN, BB_SUID_DROP)) 22 //applet:IF_FEATURE_GREP_EGREP_ALIAS(APPLET_ODDNAME(egrep, grep, BB_DIR_BIN, BB_SUID_DROP, egrep)) 23 //applet:IF_FEATURE_GREP_FGREP_ALIAS(APPLET_ODDNAME(fgrep, grep, BB_DIR_BIN, BB_SUID_DROP, fgrep)) 24 24 25 25 //kbuild:lib-$(CONFIG_GREP) += grep.o … … 73 73 //usage:#define grep_full_usage "\n\n" 74 74 //usage: "Search for PATTERN in FILEs (or stdin)\n" 75 //usage: "\nOptions:"76 75 //usage: "\n -H Add 'filename:' prefix" 77 76 //usage: "\n -h Do not add 'filename:' prefix" … … 87 86 //usage: "\n -i Ignore case" 88 87 //usage: "\n -w Match whole words only" 88 //usage: "\n -x Match whole lines only" 89 89 //usage: "\n -F PATTERN is a literal (not regexp)" 90 90 //usage: IF_FEATURE_GREP_EGREP_ALIAS( … … 115 115 116 116 #define OPTSTR_GREP \ 117 "lnqvscFiHhe:f:Lorm:w " \117 "lnqvscFiHhe:f:Lorm:wx" \ 118 118 IF_FEATURE_GREP_CONTEXT("A:B:C:") \ 119 119 IF_FEATURE_GREP_EGREP_ALIAS("E") \ … … 140 140 OPTBIT_m, /* -m MAX_MATCHES */ 141 141 OPTBIT_w, /* -w whole word match */ 142 OPTBIT_x, /* -x whole line match */ 142 143 IF_FEATURE_GREP_CONTEXT( OPTBIT_A ,) /* -A NUM: after-match context */ 143 144 IF_FEATURE_GREP_CONTEXT( OPTBIT_B ,) /* -B NUM: before-match context */ … … 162 163 OPT_m = 1 << OPTBIT_m, 163 164 OPT_w = 1 << OPTBIT_w, 165 OPT_x = 1 << OPTBIT_x, 164 166 OPT_A = IF_FEATURE_GREP_CONTEXT( (1 << OPTBIT_A)) + 0, 165 167 OPT_B = IF_FEATURE_GREP_CONTEXT( (1 << OPTBIT_B)) + 0, … … 343 345 gl = (grep_list_data_t *)pattern_ptr->data; 344 346 if (FGREP_FLAG) { 345 found |= (((option_mask32 & OPT_i) 346 ? strcasestr(line, gl->pattern) 347 : strstr(line, gl->pattern) 348 ) != NULL); 347 char *match; 348 char *str = line; 349 opt_f_again: 350 match = ((option_mask32 & OPT_i) 351 ? strcasestr(str, gl->pattern) 352 : strstr(str, gl->pattern) 353 ); 354 if (match) { 355 if (option_mask32 & OPT_x) { 356 if (match != str) 357 goto opt_f_not_found; 358 if (str[strlen(gl->pattern)] != '\0') 359 goto opt_f_not_found; 360 } else 361 if (option_mask32 & OPT_w) { 362 char c = (match != str) ? match[-1] : ' '; 363 if (!isalnum(c) && c != '_') { 364 c = match[strlen(gl->pattern)]; 365 if (!c || (!isalnum(c) && c != '_')) 366 goto opt_f_found; 367 } 368 str = match + 1; 369 goto opt_f_again; 370 } 371 opt_f_found: 372 found = 1; 373 opt_f_not_found: ; 374 } 349 375 } else { 350 376 if (!(gl->flg_mem_alocated_compiled & COMPILED)) { … … 372 398 #endif 373 399 ) { 374 if (!(option_mask32 & OPT_w)) 400 if (option_mask32 & OPT_x) { 401 found = (gl->matched_range.rm_so == 0 402 && line[gl->matched_range.rm_eo] == '\0'); 403 } else 404 if (!(option_mask32 & OPT_w)) { 375 405 found = 1; 376 else {406 } else { 377 407 char c = ' '; 378 408 if (gl->matched_range.rm_so) … … 383 413 found = 1; 384 414 } 415 //BUG: "echo foop foo | grep -w foo" should match, but doesn't: 416 //we bail out on first "mismatch" because it's not a word. 385 417 } 386 418 } … … 564 596 static void load_regexes_from_file(llist_t *fopt) 565 597 { 566 char *line;567 FILE *f;568 569 598 while (fopt) { 599 char *line; 600 FILE *fp; 570 601 llist_t *cur = fopt; 571 602 char *ffile = cur->data; … … 573 604 fopt = cur->link; 574 605 free(cur); 575 f = xfopen_stdin(ffile);576 while ((line = xmalloc_fgetline(f )) != NULL) {606 fp = xfopen_stdin(ffile); 607 while ((line = xmalloc_fgetline(fp)) != NULL) { 577 608 llist_add_to(&pattern_head, 578 609 new_grep_list_data(line, ALLOCATED)); 579 610 } 611 fclose_if_not_stdin(fp); 580 612 } 581 613 } … … 634 666 if (opts & OPT_C) { 635 667 /* -C unsets prev -A and -B, but following -A or -B 636 668 * may override it */ 637 669 if (!(opts & OPT_A)) /* not overridden */ 638 670 lines_after = Copt; … … 661 693 invert_search = ((option_mask32 & OPT_v) != 0); /* 0 | 1 */ 662 694 663 if (pattern_head != NULL) { 664 /* convert char **argv to grep_list_data_t */ 695 { /* convert char **argv to grep_list_data_t */ 665 696 llist_t *cur; 666 667 697 for (cur = pattern_head; cur; cur = cur->link) 668 698 cur->data = new_grep_list_data(cur->data, 0); 669 699 } 670 if (option_mask32 & OPT_f) 700 if (option_mask32 & OPT_f) { 671 701 load_regexes_from_file(fopt); 702 if (!pattern_head) { /* -f EMPTY_FILE? */ 703 /* GNU grep treats it as "nothing matches" */ 704 llist_add_to(&pattern_head, new_grep_list_data((char*) "", 0)); 705 invert_search ^= 1; 706 } 707 } 672 708 673 709 if (ENABLE_FEATURE_GREP_FGREP_ALIAS && applet_name[0] == 'f')
Note:
See TracChangeset
for help on using the changeset viewer.