aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-09-10 12:18:32 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-09-10 12:18:32 +0000
commit04ea11b033b38b8cde6e2f478845d0b756c330b4 (patch)
tree0e78c558e2505ebf6d65c6d6c194746a3f305ed8
parent476b0700364f1d2fc3879a49ed53c71b4cc776df (diff)
downloadbusybox-w32-04ea11b033b38b8cde6e2f478845d0b756c330b4.tar.gz
busybox-w32-04ea11b033b38b8cde6e2f478845d0b756c330b4.tar.bz2
busybox-w32-04ea11b033b38b8cde6e2f478845d0b756c330b4.zip
grep: fix grep -F -e str1 -e str2 (was matching str2 only)
by replacing "=" by "|=" here: found |= strstr(line, gl->pattern) != NULL; grep: optimization: stop on first -e match grep: stop using bss. -40 bytes
-rw-r--r--findutils/grep.c98
1 files changed, 61 insertions, 37 deletions
diff --git a/findutils/grep.c b/findutils/grep.c
index c45d0ed89..a051c2f18 100644
--- a/findutils/grep.c
+++ b/findutils/grep.c
@@ -85,24 +85,43 @@ enum {
85#define FGREP_FLAG (option_mask32 & OPT_F) 85#define FGREP_FLAG (option_mask32 & OPT_F)
86#define PRINT_FILES_WITHOUT_MATCHES (option_mask32 & OPT_L) 86#define PRINT_FILES_WITHOUT_MATCHES (option_mask32 & OPT_L)
87 87
88typedef unsigned char byte_t; 88struct globals {
89 89 int max_matches;
90static int max_matches; 90 int reflags;
91static int reflags; 91 smalluint invert_search;
92static byte_t invert_search; 92 smalluint print_filename;
93static byte_t print_filename; 93 smalluint open_errors;
94static byte_t open_errors;
95
96#if ENABLE_FEATURE_GREP_CONTEXT 94#if ENABLE_FEATURE_GREP_CONTEXT
97static byte_t did_print_line; 95 smalluint did_print_line;
98static int lines_before; 96 int lines_before;
99static int lines_after; 97 int lines_after;
100static char **before_buf; 98 char **before_buf;
101static int last_line_printed; 99 int last_line_printed;
102#endif /* ENABLE_FEATURE_GREP_CONTEXT */ 100#endif
103/* globals used internally */ 101 /* globals used internally */
104static llist_t *pattern_head; /* growable list of patterns to match */ 102 llist_t *pattern_head; /* growable list of patterns to match */
105static const char *cur_file; /* the current file we are reading */ 103 const char *cur_file; /* the current file we are reading */
104};
105#define G (*(struct globals*)&bb_common_bufsiz1)
106#define INIT_G() \
107 do { \
108 struct G_sizecheck { \
109 char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \
110 }; \
111 } while (0)
112#define max_matches (G.max_matches )
113#define reflags (G.reflags )
114#define invert_search (G.invert_search )
115#define print_filename (G.print_filename )
116#define open_errors (G.open_errors )
117#define did_print_line (G.did_print_line )
118#define lines_before (G.lines_before )
119#define lines_after (G.lines_after )
120#define before_buf (G.before_buf )
121#define last_line_printed (G.last_line_printed )
122#define pattern_head (G.pattern_head )
123#define cur_file (G.cur_file )
124
106 125
107typedef struct grep_list_data_t { 126typedef struct grep_list_data_t {
108 char *pattern; 127 char *pattern;
@@ -112,6 +131,7 @@ typedef struct grep_list_data_t {
112 int flg_mem_alocated_compiled; 131 int flg_mem_alocated_compiled;
113} grep_list_data_t; 132} grep_list_data_t;
114 133
134
115static void print_line(const char *line, int linenum, char decoration) 135static void print_line(const char *line, int linenum, char decoration)
116{ 136{
117#if ENABLE_FEATURE_GREP_CONTEXT 137#if ENABLE_FEATURE_GREP_CONTEXT
@@ -140,7 +160,7 @@ static void print_line(const char *line, int linenum, char decoration)
140static int grep_file(FILE *file) 160static int grep_file(FILE *file)
141{ 161{
142 char *line; 162 char *line;
143 byte_t ret; 163 smalluint found;
144 int linenum = 0; 164 int linenum = 0;
145 int nmatches = 0; 165 int nmatches = 0;
146 regmatch_t regmatch; 166 regmatch_t regmatch;
@@ -154,14 +174,14 @@ static int grep_file(FILE *file)
154 174
155 while ((line = xmalloc_getline(file)) != NULL) { 175 while ((line = xmalloc_getline(file)) != NULL) {
156 llist_t *pattern_ptr = pattern_head; 176 llist_t *pattern_ptr = pattern_head;
157 grep_list_data_t * gl; 177 grep_list_data_t *gl;
158 178
159 linenum++; 179 linenum++;
160 ret = 0; 180 found = 0;
161 while (pattern_ptr) { 181 while (pattern_ptr) {
162 gl = (grep_list_data_t *)pattern_ptr->data; 182 gl = (grep_list_data_t *)pattern_ptr->data;
163 if (FGREP_FLAG) { 183 if (FGREP_FLAG) {
164 ret = strstr(line, gl->pattern) != NULL; 184 found |= strstr(line, gl->pattern) != NULL;
165 } else { 185 } else {
166 /* 186 /*
167 * test for a postitive-assertion match (regexec returns success (0) 187 * test for a postitive-assertion match (regexec returns success (0)
@@ -177,7 +197,7 @@ static int grep_file(FILE *file)
177 regmatch.rm_eo = 0; 197 regmatch.rm_eo = 0;
178 if (regexec(&(gl->preg), line, 1, &regmatch, 0) == 0) { 198 if (regexec(&(gl->preg), line, 1, &regmatch, 0) == 0) {
179 if (!(option_mask32 & OPT_w)) 199 if (!(option_mask32 & OPT_w))
180 ret = 1; 200 found = 1;
181 else { 201 else {
182 char c = ' '; 202 char c = ' ';
183 if (regmatch.rm_so) 203 if (regmatch.rm_so)
@@ -185,15 +205,20 @@ static int grep_file(FILE *file)
185 if (!isalnum(c) && c != '_') { 205 if (!isalnum(c) && c != '_') {
186 c = line[regmatch.rm_eo]; 206 c = line[regmatch.rm_eo];
187 if (!c || (!isalnum(c) && c != '_')) 207 if (!c || (!isalnum(c) && c != '_'))
188 ret = 1; 208 found = 1;
189 } 209 }
190 } 210 }
191 } 211 }
192 } 212 }
213 /* If it's non-inverted search, we can stop
214 * at first match */
215 if (found && !invert_search)
216 goto do_found;
193 pattern_ptr = pattern_ptr->link; 217 pattern_ptr = pattern_ptr->link;
194 } /* while (pattern_ptr) */ 218 } /* while (pattern_ptr) */
195 219
196 if (ret ^ invert_search) { 220 if (found ^ invert_search) {
221 do_found:
197 /* keep track of matches */ 222 /* keep track of matches */
198 nmatches++; 223 nmatches++;
199 224
@@ -310,18 +335,18 @@ static int grep_file(FILE *file)
310 335
311#if ENABLE_FEATURE_CLEAN_UP 336#if ENABLE_FEATURE_CLEAN_UP
312#define new_grep_list_data(p, m) add_grep_list_data(p, m) 337#define new_grep_list_data(p, m) add_grep_list_data(p, m)
313static char * add_grep_list_data(char *pattern, int flg_used_mem) 338static char *add_grep_list_data(char *pattern, int flg_used_mem)
314#else 339#else
315#define new_grep_list_data(p, m) add_grep_list_data(p) 340#define new_grep_list_data(p, m) add_grep_list_data(p)
316static char * add_grep_list_data(char *pattern) 341static char *add_grep_list_data(char *pattern)
317#endif 342#endif
318{ 343{
319 grep_list_data_t *gl = xmalloc(sizeof(grep_list_data_t)); 344 grep_list_data_t *gl = xzalloc(sizeof(*gl));
320 gl->pattern = pattern; 345 gl->pattern = pattern;
321#if ENABLE_FEATURE_CLEAN_UP 346#if ENABLE_FEATURE_CLEAN_UP
322 gl->flg_mem_alocated_compiled = flg_used_mem; 347 gl->flg_mem_alocated_compiled = flg_used_mem;
323#else 348#else
324 gl->flg_mem_alocated_compiled = 0; 349 /*gl->flg_mem_alocated_compiled = 0;*/
325#endif 350#endif
326 return (char *)gl; 351 return (char *)gl;
327} 352}
@@ -443,9 +468,11 @@ int grep_main(int argc, char **argv)
443 if (!(option_mask32 & (OPT_o | OPT_w))) 468 if (!(option_mask32 & (OPT_o | OPT_w)))
444 reflags = REG_NOSUB; 469 reflags = REG_NOSUB;
445 470
446 if (ENABLE_FEATURE_GREP_EGREP_ALIAS && 471 if (ENABLE_FEATURE_GREP_EGREP_ALIAS
447 (applet_name[0] == 'e' || (option_mask32 & OPT_E))) 472 && (applet_name[0] == 'e' || (option_mask32 & OPT_E))
473 ) {
448 reflags |= REG_EXTENDED; 474 reflags |= REG_EXTENDED;
475 }
449 476
450 if (option_mask32 & OPT_i) 477 if (option_mask32 & OPT_i)
451 reflags |= REG_ICASE; 478 reflags |= REG_ICASE;
@@ -476,10 +503,8 @@ int grep_main(int argc, char **argv)
476 503
477 /* If no files were specified, or '-' was specified, take input from 504 /* If no files were specified, or '-' was specified, take input from
478 * stdin. Otherwise, we grep through all the files specified. */ 505 * stdin. Otherwise, we grep through all the files specified. */
479 if (argc == 0)
480 argc++;
481 matched = 0; 506 matched = 0;
482 while (argc--) { 507 do {
483 cur_file = *argv++; 508 cur_file = *argv++;
484 file = stdin; 509 file = stdin;
485 if (!cur_file || (*cur_file == '-' && !cur_file[1])) { 510 if (!cur_file || (*cur_file == '-' && !cur_file[1])) {
@@ -506,14 +531,13 @@ int grep_main(int argc, char **argv)
506 matched += grep_file(file); 531 matched += grep_file(file);
507 fclose_if_not_stdin(file); 532 fclose_if_not_stdin(file);
508 grep_done: ; 533 grep_done: ;
509 } 534 } while (--argc > 0);
510 535
511 /* destroy all the elments in the pattern list */ 536 /* destroy all the elments in the pattern list */
512 if (ENABLE_FEATURE_CLEAN_UP) { 537 if (ENABLE_FEATURE_CLEAN_UP) {
513 while (pattern_head) { 538 while (pattern_head) {
514 llist_t *pattern_head_ptr = pattern_head; 539 llist_t *pattern_head_ptr = pattern_head;
515 grep_list_data_t *gl = 540 grep_list_data_t *gl = (grep_list_data_t *)pattern_head_ptr->data;
516 (grep_list_data_t *)pattern_head_ptr->data;
517 541
518 pattern_head = pattern_head->link; 542 pattern_head = pattern_head->link;
519 if ((gl->flg_mem_alocated_compiled & PATTERN_MEM_A)) 543 if ((gl->flg_mem_alocated_compiled & PATTERN_MEM_A))
@@ -527,5 +551,5 @@ int grep_main(int argc, char **argv)
527 /* 0 = success, 1 = failed, 2 = error */ 551 /* 0 = success, 1 = failed, 2 = error */
528 if (open_errors) 552 if (open_errors)
529 return 2; 553 return 2;
530 return !matched; /* invert return value 0 = success, 1 = failed */ 554 return !matched; /* invert return value: 0 = success, 1 = failed */
531} 555}