aboutsummaryrefslogtreecommitdiff
path: root/findutils
diff options
context:
space:
mode:
authorbug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277>2003-04-27 01:50:57 +0000
committerbug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277>2003-04-27 01:50:57 +0000
commit283455d82a44abe994a5f47ec3d8d8aa9097dea0 (patch)
tree75993952bf47abd46b829c25d340a6f14cf78f5e /findutils
parent0436461b9b10ff4fcc4ee0758483a0b5055e8496 (diff)
downloadbusybox-w32-283455d82a44abe994a5f47ec3d8d8aa9097dea0.tar.gz
busybox-w32-283455d82a44abe994a5f47ec3d8d8aa9097dea0.tar.bz2
busybox-w32-283455d82a44abe994a5f47ec3d8d8aa9097dea0.zip
grep -f support, adds only 60 Bytes, but regex's now compiled as
required, rather than being pre-compiled. git-svn-id: svn://busybox.net/trunk/busybox@6818 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'findutils')
-rw-r--r--findutils/grep.c75
1 files changed, 48 insertions, 27 deletions
diff --git a/findutils/grep.c b/findutils/grep.c
index b4a25923e..908f39123 100644
--- a/findutils/grep.c
+++ b/findutils/grep.c
@@ -41,6 +41,7 @@ static int be_quiet = 0;
41static int invert_search = 0; 41static int invert_search = 0;
42static int suppress_err_msgs = 0; 42static int suppress_err_msgs = 0;
43static int print_files_with_matches = 0; 43static int print_files_with_matches = 0;
44static int fgrep_flag = 0;
44 45
45#ifdef CONFIG_FEATURE_GREP_CONTEXT 46#ifdef CONFIG_FEATURE_GREP_CONTEXT
46extern char *optarg; /* in getopt.h */ 47extern char *optarg; /* in getopt.h */
@@ -51,8 +52,7 @@ static int last_line_printed = 0;
51#endif /* CONFIG_FEATURE_GREP_CONTEXT */ 52#endif /* CONFIG_FEATURE_GREP_CONTEXT */
52 53
53/* globals used internally */ 54/* globals used internally */
54static regex_t *regexes = NULL; /* growable array of compiled regular expressions */ 55static llist_t *pattern_head = NULL; /* growable list of patterns to match */
55static int nregexes = 0; /* number of elements in above arrary */
56static int matched; /* keeps track of whether we ever matched */ 56static int matched; /* keeps track of whether we ever matched */
57static char *cur_file = NULL; /* the current file we are reading */ 57static char *cur_file = NULL; /* the current file we are reading */
58 58
@@ -81,7 +81,6 @@ static void grep_file(FILE *file)
81 int ret; 81 int ret;
82 int linenum = 0; 82 int linenum = 0;
83 int nmatches = 0; 83 int nmatches = 0;
84 int i;
85#ifdef CONFIG_FEATURE_GREP_CONTEXT 84#ifdef CONFIG_FEATURE_GREP_CONTEXT
86 int print_n_lines_after = 0; 85 int print_n_lines_after = 0;
87 int curpos = 0; /* track where we are in the circular 'before' buffer */ 86 int curpos = 0; /* track where we are in the circular 'before' buffer */
@@ -89,16 +88,30 @@ static void grep_file(FILE *file)
89#endif /* CONFIG_FEATURE_GREP_CONTEXT */ 88#endif /* CONFIG_FEATURE_GREP_CONTEXT */
90 89
91 while ((line = bb_get_chomped_line_from_file(file)) != NULL) { 90 while ((line = bb_get_chomped_line_from_file(file)) != NULL) {
91 llist_t *pattern_ptr = pattern_head;
92
92 linenum++; 93 linenum++;
93 94
94 for (i = 0; i < nregexes; i++) { 95 while (pattern_ptr) {
95 /* 96 if (fgrep_flag) {
96 * test for a postitive-assertion match (regexec returns success (0) 97 if (strstr(line, pattern_ptr->data)) {
97 * and the user did not specify invert search), or a negative-assertion 98 /* Match found */
98 * match (regexec returns failure (REG_NOMATCH) and the user specified 99 ret = 0;
99 * invert search) 100 } else {
100 */ 101 ret = 1;
101 ret = regexec(&regexes[i], line, 0, NULL, 0); 102 }
103 } else {
104 /*
105 * test for a postitive-assertion match (regexec returns success (0)
106 * and the user did not specify invert search), or a negative-assertion
107 * match (regexec returns failure (REG_NOMATCH) and the user specified
108 * invert search)
109 */
110 regex_t regex;
111 xregcomp(&regex, pattern_ptr->data, reflags);
112 ret = regexec(&regex, line, 0, NULL, 0);
113 regfree(&regex);
114 }
102 if ((ret == 0 && !invert_search) || (ret == REG_NOMATCH && invert_search)) { 115 if ((ret == 0 && !invert_search) || (ret == REG_NOMATCH && invert_search)) {
103 116
104 /* if we found a match but were told to be quiet, stop here and 117 /* if we found a match but were told to be quiet, stop here and
@@ -164,6 +177,7 @@ static void grep_file(FILE *file)
164 print_n_lines_after--; 177 print_n_lines_after--;
165 } 178 }
166#endif /* CONFIG_FEATURE_GREP_CONTEXT */ 179#endif /* CONFIG_FEATURE_GREP_CONTEXT */
180 pattern_ptr = pattern_ptr->link;
167 } /* for */ 181 } /* for */
168 free(line); 182 free(line);
169 } 183 }
@@ -193,21 +207,21 @@ static void grep_file(FILE *file)
193 matched = 1; 207 matched = 1;
194} 208}
195 209
196 210#if 0
197static void add_regex(const char *restr) 211static void add_pattern(char *restr)
198{ 212{
199 regexes = xrealloc(regexes, sizeof(regex_t) * (++nregexes)); 213// regexes = xrealloc(regexes, sizeof(regex_t) * (++nregexes));
200 xregcomp(&regexes[nregexes-1], restr, reflags); 214// xregcomp(&regexes[nregexes-1], restr, reflags);
215 pattern_head = llist_add_to(pattern_head, restr);
201} 216}
202 217#endif
203 218
204static void load_regexes_from_file(const char *filename) 219static void load_regexes_from_file(const char *filename)
205{ 220{
206 char *line; 221 char *line;
207 FILE *f = bb_xfopen(filename, "r"); 222 FILE *f = bb_xfopen(filename, "r");
208 while ((line = bb_get_chomped_line_from_file(f)) != NULL) { 223 while ((line = bb_get_chomped_line_from_file(f)) != NULL) {
209 add_regex(line); 224 pattern_head = llist_add_to(pattern_head, line);
210 free(line);
211 } 225 }
212} 226}
213 227
@@ -215,14 +229,18 @@ static void load_regexes_from_file(const char *filename)
215#ifdef CONFIG_FEATURE_CLEAN_UP 229#ifdef CONFIG_FEATURE_CLEAN_UP
216static void destroy_regexes(void) 230static void destroy_regexes(void)
217{ 231{
218 if (regexes == NULL) 232 llist_t *pattern_head_ptr;
233
234 if (pattern_head == NULL)
219 return; 235 return;
220 236
221 /* destroy all the elments in the array */ 237 /* destroy all the elments in the pattern list */
222 while (--nregexes >= 0) { 238 while (pattern_head) {
223 regfree(&(regexes[nregexes])); 239 pattern_head_ptr = pattern_head;
240 pattern_head = pattern_head->link;
241 free(pattern_head_ptr->data);
242 free(pattern_head_ptr);
224 } 243 }
225 free(regexes);
226} 244}
227#endif 245#endif
228 246
@@ -245,7 +263,7 @@ extern int grep_main(int argc, char **argv)
245#endif 263#endif
246 264
247 /* do normal option parsing */ 265 /* do normal option parsing */
248 while ((opt = getopt(argc, argv, "iHhlnqvsce:f:" 266 while ((opt = getopt(argc, argv, "iHhlnqvsce:f:F"
249#ifdef CONFIG_FEATURE_GREP_CONTEXT 267#ifdef CONFIG_FEATURE_GREP_CONTEXT
250"A:B:C:" 268"A:B:C:"
251#endif 269#endif
@@ -282,13 +300,16 @@ extern int grep_main(int argc, char **argv)
282 print_match_counts++; 300 print_match_counts++;
283 break; 301 break;
284 case 'e': 302 case 'e':
285 add_regex(optarg); 303 pattern_head = llist_add_to(pattern_head, strdup(optarg));
286 break; 304 break;
287#ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS 305#ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS
288 case 'E': 306 case 'E':
289 reflags |= REG_EXTENDED; 307 reflags |= REG_EXTENDED;
290 break; 308 break;
291#endif 309#endif
310 case 'F':
311 fgrep_flag = 1;
312 break;
292 case 'f': 313 case 'f':
293 load_regexes_from_file(optarg); 314 load_regexes_from_file(optarg);
294 break; 315 break;
@@ -318,11 +339,11 @@ extern int grep_main(int argc, char **argv)
318 339
319 /* if we didn't get a pattern from a -e and no command file was specified, 340 /* if we didn't get a pattern from a -e and no command file was specified,
320 * argv[optind] should be the pattern. no pattern, no worky */ 341 * argv[optind] should be the pattern. no pattern, no worky */
321 if (nregexes == 0) { 342 if (pattern_head == NULL) {
322 if (argv[optind] == NULL) 343 if (argv[optind] == NULL)
323 bb_show_usage(); 344 bb_show_usage();
324 else { 345 else {
325 add_regex(argv[optind]); 346 pattern_head = llist_add_to(pattern_head, argv[optind]);
326 optind++; 347 optind++;
327 } 348 }
328 } 349 }