Annotation of /trunk/mkinitrd-magellan/busybox/findutils/grep.c
Parent Directory | Revision Log
Revision 984 -
(hide annotations)
(download)
Sun May 30 11:32:42 2010 UTC (14 years ago) by niro
File MIME type: text/plain
File size: 19845 byte(s)
Sun May 30 11:32:42 2010 UTC (14 years ago) by niro
File MIME type: text/plain
File size: 19845 byte(s)
-updated to busybox-1.16.1 and enabled blkid/uuid support in default config
1 | niro | 532 | /* vi: set sw=4 ts=4: */ |
2 | /* | ||
3 | * Mini grep implementation for busybox using libc regex. | ||
4 | * | ||
5 | * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley | ||
6 | * Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org> | ||
7 | * | ||
8 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. | ||
9 | */ | ||
10 | niro | 816 | /* BB_AUDIT SUSv3 defects - unsupported option -x "match whole line only". */ |
11 | niro | 532 | /* BB_AUDIT GNU defects - always acts as -a. */ |
12 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/grep.html */ | ||
13 | /* | ||
14 | * 2004,2006 (C) Vladimir Oleynik <dzo@simtreas.ru> - | ||
15 | * correction "-e pattern1 -e pattern2" logic and more optimizations. | ||
16 | * precompiled regex | ||
17 | */ | ||
18 | /* | ||
19 | * (C) 2006 Jac Goudsmit added -o option | ||
20 | */ | ||
21 | |||
22 | niro | 816 | #include "libbb.h" |
23 | niro | 532 | #include "xregex.h" |
24 | |||
25 | niro | 816 | /* options */ |
26 | #define OPTSTR_GREP \ | ||
27 | "lnqvscFiHhe:f:Lorm:" \ | ||
28 | niro | 984 | IF_FEATURE_GREP_CONTEXT("A:B:C:") \ |
29 | IF_FEATURE_GREP_EGREP_ALIAS("E") \ | ||
30 | IF_DESKTOP("w") \ | ||
31 | IF_EXTRA_COMPAT("z") \ | ||
32 | niro | 816 | "aI" |
33 | niro | 984 | |
34 | niro | 816 | /* ignored: -a "assume all files to be text" */ |
35 | /* ignored: -I "assume binary files have no matches" */ | ||
36 | niro | 532 | |
37 | niro | 816 | enum { |
38 | OPTBIT_l, /* list matched file names only */ | ||
39 | OPTBIT_n, /* print line# */ | ||
40 | OPTBIT_q, /* quiet - exit(EXIT_SUCCESS) of first match */ | ||
41 | OPTBIT_v, /* invert the match, to select non-matching lines */ | ||
42 | OPTBIT_s, /* suppress errors about file open errors */ | ||
43 | OPTBIT_c, /* count matches per file (suppresses normal output) */ | ||
44 | OPTBIT_F, /* literal match */ | ||
45 | OPTBIT_i, /* case-insensitive */ | ||
46 | OPTBIT_H, /* force filename display */ | ||
47 | OPTBIT_h, /* inhibit filename display */ | ||
48 | OPTBIT_e, /* -e PATTERN */ | ||
49 | OPTBIT_f, /* -f FILE_WITH_PATTERNS */ | ||
50 | OPTBIT_L, /* list unmatched file names only */ | ||
51 | OPTBIT_o, /* show only matching parts of lines */ | ||
52 | OPTBIT_r, /* recurse dirs */ | ||
53 | OPTBIT_m, /* -m MAX_MATCHES */ | ||
54 | niro | 984 | IF_FEATURE_GREP_CONTEXT( OPTBIT_A ,) /* -A NUM: after-match context */ |
55 | IF_FEATURE_GREP_CONTEXT( OPTBIT_B ,) /* -B NUM: before-match context */ | ||
56 | IF_FEATURE_GREP_CONTEXT( OPTBIT_C ,) /* -C NUM: -A and -B combined */ | ||
57 | IF_FEATURE_GREP_EGREP_ALIAS(OPTBIT_E ,) /* extended regexp */ | ||
58 | IF_DESKTOP( OPTBIT_w ,) /* whole word match */ | ||
59 | IF_EXTRA_COMPAT( OPTBIT_z ,) /* input is NUL terminated */ | ||
60 | niro | 816 | OPT_l = 1 << OPTBIT_l, |
61 | OPT_n = 1 << OPTBIT_n, | ||
62 | OPT_q = 1 << OPTBIT_q, | ||
63 | OPT_v = 1 << OPTBIT_v, | ||
64 | OPT_s = 1 << OPTBIT_s, | ||
65 | OPT_c = 1 << OPTBIT_c, | ||
66 | OPT_F = 1 << OPTBIT_F, | ||
67 | OPT_i = 1 << OPTBIT_i, | ||
68 | OPT_H = 1 << OPTBIT_H, | ||
69 | OPT_h = 1 << OPTBIT_h, | ||
70 | OPT_e = 1 << OPTBIT_e, | ||
71 | OPT_f = 1 << OPTBIT_f, | ||
72 | OPT_L = 1 << OPTBIT_L, | ||
73 | OPT_o = 1 << OPTBIT_o, | ||
74 | OPT_r = 1 << OPTBIT_r, | ||
75 | OPT_m = 1 << OPTBIT_m, | ||
76 | niro | 984 | OPT_A = IF_FEATURE_GREP_CONTEXT( (1 << OPTBIT_A)) + 0, |
77 | OPT_B = IF_FEATURE_GREP_CONTEXT( (1 << OPTBIT_B)) + 0, | ||
78 | OPT_C = IF_FEATURE_GREP_CONTEXT( (1 << OPTBIT_C)) + 0, | ||
79 | OPT_E = IF_FEATURE_GREP_EGREP_ALIAS((1 << OPTBIT_E)) + 0, | ||
80 | OPT_w = IF_DESKTOP( (1 << OPTBIT_w)) + 0, | ||
81 | OPT_z = IF_EXTRA_COMPAT( (1 << OPTBIT_z)) + 0, | ||
82 | niro | 816 | }; |
83 | |||
84 | #define PRINT_FILES_WITH_MATCHES (option_mask32 & OPT_l) | ||
85 | #define PRINT_LINE_NUM (option_mask32 & OPT_n) | ||
86 | #define BE_QUIET (option_mask32 & OPT_q) | ||
87 | #define SUPPRESS_ERR_MSGS (option_mask32 & OPT_s) | ||
88 | #define PRINT_MATCH_COUNTS (option_mask32 & OPT_c) | ||
89 | #define FGREP_FLAG (option_mask32 & OPT_F) | ||
90 | #define PRINT_FILES_WITHOUT_MATCHES (option_mask32 & OPT_L) | ||
91 | niro | 984 | #define NUL_DELIMITED (option_mask32 & OPT_z) |
92 | niro | 816 | |
93 | struct globals { | ||
94 | int max_matches; | ||
95 | #if !ENABLE_EXTRA_COMPAT | ||
96 | int reflags; | ||
97 | niro | 532 | #else |
98 | niro | 816 | RE_TRANSLATE_TYPE case_fold; /* RE_TRANSLATE_TYPE is [[un]signed] char* */ |
99 | niro | 532 | #endif |
100 | niro | 816 | smalluint invert_search; |
101 | smalluint print_filename; | ||
102 | smalluint open_errors; | ||
103 | #if ENABLE_FEATURE_GREP_CONTEXT | ||
104 | smalluint did_print_line; | ||
105 | int lines_before; | ||
106 | int lines_after; | ||
107 | char **before_buf; | ||
108 | niro | 984 | IF_EXTRA_COMPAT(size_t *before_buf_size;) |
109 | niro | 816 | int last_line_printed; |
110 | #endif | ||
111 | /* globals used internally */ | ||
112 | llist_t *pattern_head; /* growable list of patterns to match */ | ||
113 | const char *cur_file; /* the current file we are reading */ | ||
114 | }; | ||
115 | #define G (*(struct globals*)&bb_common_bufsiz1) | ||
116 | #define INIT_G() do { \ | ||
117 | struct G_sizecheck { \ | ||
118 | char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \ | ||
119 | }; \ | ||
120 | } while (0) | ||
121 | #define max_matches (G.max_matches ) | ||
122 | #if !ENABLE_EXTRA_COMPAT | ||
123 | niro | 984 | # define reflags (G.reflags ) |
124 | niro | 532 | #else |
125 | niro | 984 | # define case_fold (G.case_fold ) |
126 | niro | 816 | /* http://www.delorie.com/gnu/docs/regex/regex_46.html */ |
127 | niro | 984 | # define reflags re_syntax_options |
128 | # undef REG_NOSUB | ||
129 | # undef REG_EXTENDED | ||
130 | # undef REG_ICASE | ||
131 | # define REG_NOSUB bug:is:here /* should not be used */ | ||
132 | /* Just RE_SYNTAX_EGREP is not enough, need to enable {n[,[m]]} too */ | ||
133 | # define REG_EXTENDED (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES) | ||
134 | # define REG_ICASE bug:is:here /* should not be used */ | ||
135 | niro | 532 | #endif |
136 | niro | 816 | #define invert_search (G.invert_search ) |
137 | #define print_filename (G.print_filename ) | ||
138 | #define open_errors (G.open_errors ) | ||
139 | #define did_print_line (G.did_print_line ) | ||
140 | #define lines_before (G.lines_before ) | ||
141 | #define lines_after (G.lines_after ) | ||
142 | #define before_buf (G.before_buf ) | ||
143 | #define before_buf_size (G.before_buf_size ) | ||
144 | #define last_line_printed (G.last_line_printed ) | ||
145 | #define pattern_head (G.pattern_head ) | ||
146 | #define cur_file (G.cur_file ) | ||
147 | niro | 532 | |
148 | |||
149 | niro | 816 | typedef struct grep_list_data_t { |
150 | niro | 532 | char *pattern; |
151 | niro | 816 | /* for GNU regex, matched_range must be persistent across grep_file() calls */ |
152 | #if !ENABLE_EXTRA_COMPAT | ||
153 | regex_t compiled_regex; | ||
154 | regmatch_t matched_range; | ||
155 | #else | ||
156 | struct re_pattern_buffer compiled_regex; | ||
157 | struct re_registers matched_range; | ||
158 | #endif | ||
159 | #define ALLOCATED 1 | ||
160 | niro | 532 | #define COMPILED 2 |
161 | int flg_mem_alocated_compiled; | ||
162 | } grep_list_data_t; | ||
163 | |||
164 | niro | 816 | #if !ENABLE_EXTRA_COMPAT |
165 | #define print_line(line, line_len, linenum, decoration) \ | ||
166 | print_line(line, linenum, decoration) | ||
167 | #endif | ||
168 | static void print_line(const char *line, size_t line_len, int linenum, char decoration) | ||
169 | niro | 532 | { |
170 | #if ENABLE_FEATURE_GREP_CONTEXT | ||
171 | niro | 816 | /* Happens when we go to next file, immediately hit match |
172 | * and try to print prev context... from prev file! Don't do it */ | ||
173 | if (linenum < 1) | ||
174 | return; | ||
175 | niro | 532 | /* possibly print the little '--' separator */ |
176 | niro | 816 | if ((lines_before || lines_after) && did_print_line |
177 | && last_line_printed != linenum - 1 | ||
178 | ) { | ||
179 | niro | 532 | puts("--"); |
180 | } | ||
181 | niro | 816 | /* guard against printing "--" before first line of first file */ |
182 | did_print_line = 1; | ||
183 | niro | 532 | last_line_printed = linenum; |
184 | #endif | ||
185 | if (print_filename) | ||
186 | printf("%s%c", cur_file, decoration); | ||
187 | if (PRINT_LINE_NUM) | ||
188 | printf("%i%c", linenum, decoration); | ||
189 | /* Emulate weird GNU grep behavior with -ov */ | ||
190 | niro | 816 | if ((option_mask32 & (OPT_v|OPT_o)) != (OPT_v|OPT_o)) { |
191 | #if !ENABLE_EXTRA_COMPAT | ||
192 | niro | 532 | puts(line); |
193 | niro | 816 | #else |
194 | fwrite(line, 1, line_len, stdout); | ||
195 | niro | 984 | putchar(NUL_DELIMITED ? '\0' : '\n'); |
196 | niro | 816 | #endif |
197 | } | ||
198 | niro | 532 | } |
199 | |||
200 | niro | 816 | #if ENABLE_EXTRA_COMPAT |
201 | /* Unlike getline, this one removes trailing '\n' */ | ||
202 | static ssize_t FAST_FUNC bb_getline(char **line_ptr, size_t *line_alloc_len, FILE *file) | ||
203 | { | ||
204 | ssize_t res_sz; | ||
205 | char *line; | ||
206 | niro | 984 | int delim = (NUL_DELIMITED ? '\0' : '\n'); |
207 | niro | 532 | |
208 | niro | 984 | res_sz = getdelim(line_ptr, line_alloc_len, delim, file); |
209 | niro | 816 | line = *line_ptr; |
210 | |||
211 | if (res_sz > 0) { | ||
212 | niro | 984 | if (line[res_sz - 1] == delim) |
213 | niro | 816 | line[--res_sz] = '\0'; |
214 | } else { | ||
215 | free(line); /* uclibc allocates a buffer even on EOF. WTF? */ | ||
216 | } | ||
217 | return res_sz; | ||
218 | } | ||
219 | #endif | ||
220 | |||
221 | niro | 532 | static int grep_file(FILE *file) |
222 | { | ||
223 | niro | 816 | smalluint found; |
224 | niro | 532 | int linenum = 0; |
225 | int nmatches = 0; | ||
226 | niro | 816 | #if !ENABLE_EXTRA_COMPAT |
227 | char *line; | ||
228 | #else | ||
229 | char *line = NULL; | ||
230 | ssize_t line_len; | ||
231 | size_t line_alloc_len; | ||
232 | niro | 984 | # define rm_so start[0] |
233 | # define rm_eo end[0] | ||
234 | niro | 816 | #endif |
235 | niro | 532 | #if ENABLE_FEATURE_GREP_CONTEXT |
236 | int print_n_lines_after = 0; | ||
237 | int curpos = 0; /* track where we are in the circular 'before' buffer */ | ||
238 | int idx = 0; /* used for iteration through the circular buffer */ | ||
239 | niro | 816 | #else |
240 | enum { print_n_lines_after = 0 }; | ||
241 | niro | 984 | #endif |
242 | niro | 532 | |
243 | niro | 816 | while ( |
244 | #if !ENABLE_EXTRA_COMPAT | ||
245 | (line = xmalloc_fgetline(file)) != NULL | ||
246 | #else | ||
247 | (line_len = bb_getline(&line, &line_alloc_len, file)) >= 0 | ||
248 | #endif | ||
249 | ) { | ||
250 | niro | 532 | llist_t *pattern_ptr = pattern_head; |
251 | niro | 816 | grep_list_data_t *gl = gl; /* for gcc */ |
252 | niro | 532 | |
253 | linenum++; | ||
254 | niro | 816 | found = 0; |
255 | niro | 532 | while (pattern_ptr) { |
256 | gl = (grep_list_data_t *)pattern_ptr->data; | ||
257 | if (FGREP_FLAG) { | ||
258 | niro | 816 | found |= (strstr(line, gl->pattern) != NULL); |
259 | niro | 532 | } else { |
260 | if (!(gl->flg_mem_alocated_compiled & COMPILED)) { | ||
261 | gl->flg_mem_alocated_compiled |= COMPILED; | ||
262 | niro | 816 | #if !ENABLE_EXTRA_COMPAT |
263 | xregcomp(&gl->compiled_regex, gl->pattern, reflags); | ||
264 | #else | ||
265 | memset(&gl->compiled_regex, 0, sizeof(gl->compiled_regex)); | ||
266 | gl->compiled_regex.translate = case_fold; /* for -i */ | ||
267 | if (re_compile_pattern(gl->pattern, strlen(gl->pattern), &gl->compiled_regex)) | ||
268 | bb_error_msg_and_die("bad regex '%s'", gl->pattern); | ||
269 | #endif | ||
270 | niro | 532 | } |
271 | niro | 816 | #if !ENABLE_EXTRA_COMPAT |
272 | gl->matched_range.rm_so = 0; | ||
273 | gl->matched_range.rm_eo = 0; | ||
274 | #endif | ||
275 | if ( | ||
276 | #if !ENABLE_EXTRA_COMPAT | ||
277 | regexec(&gl->compiled_regex, line, 1, &gl->matched_range, 0) == 0 | ||
278 | #else | ||
279 | re_search(&gl->compiled_regex, line, line_len, | ||
280 | /*start:*/ 0, /*range:*/ line_len, | ||
281 | &gl->matched_range) >= 0 | ||
282 | #endif | ||
283 | ) { | ||
284 | if (!(option_mask32 & OPT_w)) | ||
285 | found = 1; | ||
286 | else { | ||
287 | char c = ' '; | ||
288 | if (gl->matched_range.rm_so) | ||
289 | c = line[gl->matched_range.rm_so - 1]; | ||
290 | if (!isalnum(c) && c != '_') { | ||
291 | c = line[gl->matched_range.rm_eo]; | ||
292 | if (!c || (!isalnum(c) && c != '_')) | ||
293 | found = 1; | ||
294 | } | ||
295 | } | ||
296 | } | ||
297 | niro | 532 | } |
298 | niro | 816 | /* If it's non-inverted search, we can stop |
299 | * at first match */ | ||
300 | if (found && !invert_search) | ||
301 | goto do_found; | ||
302 | niro | 532 | pattern_ptr = pattern_ptr->link; |
303 | } /* while (pattern_ptr) */ | ||
304 | |||
305 | niro | 816 | if (found ^ invert_search) { |
306 | do_found: | ||
307 | /* keep track of matches */ | ||
308 | nmatches++; | ||
309 | niro | 532 | |
310 | niro | 816 | /* quiet/print (non)matching file names only? */ |
311 | if (option_mask32 & (OPT_q|OPT_l|OPT_L)) { | ||
312 | free(line); /* we don't need line anymore */ | ||
313 | if (BE_QUIET) { | ||
314 | /* manpage says about -q: | ||
315 | * "exit immediately with zero status | ||
316 | * if any match is found, | ||
317 | * even if errors were detected" */ | ||
318 | exit(EXIT_SUCCESS); | ||
319 | } | ||
320 | niro | 532 | /* if we're just printing filenames, we stop after the first match */ |
321 | niro | 816 | if (PRINT_FILES_WITH_MATCHES) { |
322 | puts(cur_file); | ||
323 | /* fall through to "return 1" */ | ||
324 | } | ||
325 | /* OPT_L aka PRINT_FILES_WITHOUT_MATCHES: return early */ | ||
326 | return 1; /* one match */ | ||
327 | } | ||
328 | niro | 532 | |
329 | #if ENABLE_FEATURE_GREP_CONTEXT | ||
330 | niro | 816 | /* Were we printing context and saw next (unwanted) match? */ |
331 | if ((option_mask32 & OPT_m) && nmatches > max_matches) | ||
332 | break; | ||
333 | #endif | ||
334 | niro | 532 | |
335 | niro | 816 | /* print the matched line */ |
336 | if (PRINT_MATCH_COUNTS == 0) { | ||
337 | #if ENABLE_FEATURE_GREP_CONTEXT | ||
338 | int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1; | ||
339 | niro | 532 | |
340 | niro | 816 | /* if we were told to print 'before' lines and there is at least |
341 | * one line in the circular buffer, print them */ | ||
342 | if (lines_before && before_buf[prevpos] != NULL) { | ||
343 | int first_buf_entry_line_num = linenum - lines_before; | ||
344 | niro | 532 | |
345 | niro | 816 | /* advance to the first entry in the circular buffer, and |
346 | * figure out the line number is of the first line in the | ||
347 | * buffer */ | ||
348 | idx = curpos; | ||
349 | while (before_buf[idx] == NULL) { | ||
350 | idx = (idx + 1) % lines_before; | ||
351 | first_buf_entry_line_num++; | ||
352 | niro | 532 | } |
353 | |||
354 | niro | 816 | /* now print each line in the buffer, clearing them as we go */ |
355 | while (before_buf[idx] != NULL) { | ||
356 | print_line(before_buf[idx], before_buf_size[idx], first_buf_entry_line_num, '-'); | ||
357 | free(before_buf[idx]); | ||
358 | before_buf[idx] = NULL; | ||
359 | idx = (idx + 1) % lines_before; | ||
360 | first_buf_entry_line_num++; | ||
361 | niro | 532 | } |
362 | } | ||
363 | niro | 816 | |
364 | /* make a note that we need to print 'after' lines */ | ||
365 | print_n_lines_after = lines_after; | ||
366 | #endif | ||
367 | if (option_mask32 & OPT_o) { | ||
368 | if (FGREP_FLAG) { | ||
369 | /* -Fo just prints the pattern | ||
370 | * (unless -v: -Fov doesnt print anything at all) */ | ||
371 | if (found) | ||
372 | print_line(gl->pattern, strlen(gl->pattern), linenum, ':'); | ||
373 | } else while (1) { | ||
374 | niro | 984 | unsigned end = gl->matched_range.rm_eo; |
375 | char old = line[end]; | ||
376 | line[end] = '\0'; | ||
377 | niro | 816 | print_line(line + gl->matched_range.rm_so, |
378 | niro | 984 | end - gl->matched_range.rm_so, |
379 | niro | 816 | linenum, ':'); |
380 | niro | 984 | if (old == '\0') |
381 | break; | ||
382 | line[end] = old; | ||
383 | niro | 816 | #if !ENABLE_EXTRA_COMPAT |
384 | niro | 984 | if (regexec(&gl->compiled_regex, line + end, |
385 | 1, &gl->matched_range, REG_NOTBOL) != 0) | ||
386 | break; | ||
387 | gl->matched_range.rm_so += end; | ||
388 | gl->matched_range.rm_eo += end; | ||
389 | niro | 816 | #else |
390 | if (re_search(&gl->compiled_regex, line, line_len, | ||
391 | niro | 984 | end, line_len - end, |
392 | niro | 816 | &gl->matched_range) < 0) |
393 | break; | ||
394 | #endif | ||
395 | niro | 984 | } |
396 | niro | 816 | } else { |
397 | print_line(line, line_len, linenum, ':'); | ||
398 | } | ||
399 | niro | 532 | } |
400 | niro | 816 | } |
401 | niro | 532 | #if ENABLE_FEATURE_GREP_CONTEXT |
402 | niro | 816 | else { /* no match */ |
403 | /* if we need to print some context lines after the last match, do so */ | ||
404 | if (print_n_lines_after) { | ||
405 | print_line(line, strlen(line), linenum, '-'); | ||
406 | print_n_lines_after--; | ||
407 | } else if (lines_before) { | ||
408 | niro | 532 | /* Add the line to the circular 'before' buffer */ |
409 | niro | 816 | free(before_buf[curpos]); |
410 | before_buf[curpos] = line; | ||
411 | niro | 984 | IF_EXTRA_COMPAT(before_buf_size[curpos] = line_len;) |
412 | niro | 816 | curpos = (curpos + 1) % lines_before; |
413 | /* avoid free(line) - we took the line */ | ||
414 | line = NULL; | ||
415 | niro | 532 | } |
416 | niro | 816 | } |
417 | niro | 532 | |
418 | #endif /* ENABLE_FEATURE_GREP_CONTEXT */ | ||
419 | niro | 816 | #if !ENABLE_EXTRA_COMPAT |
420 | niro | 532 | free(line); |
421 | niro | 816 | #endif |
422 | /* Did we print all context after last requested match? */ | ||
423 | if ((option_mask32 & OPT_m) | ||
424 | niro | 984 | && !print_n_lines_after |
425 | && nmatches == max_matches | ||
426 | ) { | ||
427 | niro | 816 | break; |
428 | niro | 984 | } |
429 | niro | 816 | } /* while (read line) */ |
430 | niro | 532 | |
431 | /* special-case file post-processing for options where we don't print line | ||
432 | * matches, just filenames and possibly match counts */ | ||
433 | |||
434 | /* grep -c: print [filename:]count, even if count is zero */ | ||
435 | if (PRINT_MATCH_COUNTS) { | ||
436 | if (print_filename) | ||
437 | printf("%s:", cur_file); | ||
438 | printf("%d\n", nmatches); | ||
439 | } | ||
440 | |||
441 | niro | 816 | /* grep -L: print just the filename */ |
442 | if (PRINT_FILES_WITHOUT_MATCHES) { | ||
443 | /* nmatches is zero, no need to check it: | ||
444 | * we return 1 early if we detected a match | ||
445 | * and PRINT_FILES_WITHOUT_MATCHES is set */ | ||
446 | niro | 532 | puts(cur_file); |
447 | } | ||
448 | |||
449 | return nmatches; | ||
450 | } | ||
451 | |||
452 | #if ENABLE_FEATURE_CLEAN_UP | ||
453 | #define new_grep_list_data(p, m) add_grep_list_data(p, m) | ||
454 | niro | 816 | static char *add_grep_list_data(char *pattern, int flg_used_mem) |
455 | niro | 532 | #else |
456 | #define new_grep_list_data(p, m) add_grep_list_data(p) | ||
457 | niro | 816 | static char *add_grep_list_data(char *pattern) |
458 | niro | 532 | #endif |
459 | { | ||
460 | niro | 816 | grep_list_data_t *gl = xzalloc(sizeof(*gl)); |
461 | niro | 532 | gl->pattern = pattern; |
462 | #if ENABLE_FEATURE_CLEAN_UP | ||
463 | gl->flg_mem_alocated_compiled = flg_used_mem; | ||
464 | #else | ||
465 | niro | 816 | /*gl->flg_mem_alocated_compiled = 0;*/ |
466 | niro | 532 | #endif |
467 | return (char *)gl; | ||
468 | } | ||
469 | |||
470 | static void load_regexes_from_file(llist_t *fopt) | ||
471 | { | ||
472 | char *line; | ||
473 | FILE *f; | ||
474 | |||
475 | while (fopt) { | ||
476 | llist_t *cur = fopt; | ||
477 | char *ffile = cur->data; | ||
478 | |||
479 | fopt = cur->link; | ||
480 | free(cur); | ||
481 | niro | 816 | f = xfopen_stdin(ffile); |
482 | while ((line = xmalloc_fgetline(f)) != NULL) { | ||
483 | niro | 532 | llist_add_to(&pattern_head, |
484 | niro | 816 | new_grep_list_data(line, ALLOCATED)); |
485 | niro | 532 | } |
486 | } | ||
487 | } | ||
488 | |||
489 | niro | 816 | static int FAST_FUNC file_action_grep(const char *filename, |
490 | struct stat *statbuf UNUSED_PARAM, | ||
491 | void* matched, | ||
492 | int depth UNUSED_PARAM) | ||
493 | niro | 532 | { |
494 | niro | 816 | FILE *file = fopen_for_read(filename); |
495 | niro | 532 | if (file == NULL) { |
496 | if (!SUPPRESS_ERR_MSGS) | ||
497 | niro | 816 | bb_simple_perror_msg(filename); |
498 | niro | 532 | open_errors = 1; |
499 | return 0; | ||
500 | } | ||
501 | cur_file = filename; | ||
502 | *(int*)matched += grep_file(file); | ||
503 | fclose(file); | ||
504 | return 1; | ||
505 | } | ||
506 | |||
507 | static int grep_dir(const char *dir) | ||
508 | { | ||
509 | int matched = 0; | ||
510 | recursive_action(dir, | ||
511 | niro | 816 | /* recurse=yes */ ACTION_RECURSE | |
512 | /* followLinks=no */ | ||
513 | /* depthFirst=yes */ ACTION_DEPTHFIRST, | ||
514 | niro | 532 | /* fileAction= */ file_action_grep, |
515 | /* dirAction= */ NULL, | ||
516 | /* userData= */ &matched, | ||
517 | /* depth= */ 0); | ||
518 | return matched; | ||
519 | } | ||
520 | |||
521 | niro | 816 | int grep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
522 | niro | 984 | int grep_main(int argc UNUSED_PARAM, char **argv) |
523 | niro | 532 | { |
524 | FILE *file; | ||
525 | int matched; | ||
526 | llist_t *fopt = NULL; | ||
527 | |||
528 | /* do normal option parsing */ | ||
529 | #if ENABLE_FEATURE_GREP_CONTEXT | ||
530 | niro | 816 | int Copt; |
531 | niro | 532 | |
532 | niro | 816 | /* -H unsets -h; -C unsets -A,-B; -e,-f are lists; |
533 | * -m,-A,-B,-C have numeric param */ | ||
534 | opt_complementary = "H-h:C-AB:e::f::m+:A+:B+:C+"; | ||
535 | getopt32(argv, | ||
536 | OPTSTR_GREP, | ||
537 | &pattern_head, &fopt, &max_matches, | ||
538 | &lines_after, &lines_before, &Copt); | ||
539 | niro | 532 | |
540 | niro | 816 | if (option_mask32 & OPT_C) { |
541 | niro | 532 | /* -C unsets prev -A and -B, but following -A or -B |
542 | may override it */ | ||
543 | niro | 816 | if (!(option_mask32 & OPT_A)) /* not overridden */ |
544 | lines_after = Copt; | ||
545 | if (!(option_mask32 & OPT_B)) /* not overridden */ | ||
546 | lines_before = Copt; | ||
547 | niro | 532 | } |
548 | /* sanity checks */ | ||
549 | niro | 816 | if (option_mask32 & (OPT_c|OPT_q|OPT_l|OPT_L)) { |
550 | option_mask32 &= ~OPT_n; | ||
551 | niro | 532 | lines_before = 0; |
552 | lines_after = 0; | ||
553 | niro | 816 | } else if (lines_before > 0) { |
554 | before_buf = xzalloc(lines_before * sizeof(before_buf[0])); | ||
555 | niro | 984 | IF_EXTRA_COMPAT(before_buf_size = xzalloc(lines_before * sizeof(before_buf_size[0]));) |
556 | niro | 816 | } |
557 | niro | 532 | #else |
558 | /* with auto sanity checks */ | ||
559 | niro | 816 | /* -H unsets -h; -c,-q or -l unset -n; -e,-f are lists; -m N */ |
560 | opt_complementary = "H-h:c-n:q-n:l-n:e::f::m+"; | ||
561 | getopt32(argv, OPTSTR_GREP, | ||
562 | &pattern_head, &fopt, &max_matches); | ||
563 | niro | 532 | #endif |
564 | niro | 816 | invert_search = ((option_mask32 & OPT_v) != 0); /* 0 | 1 */ |
565 | niro | 532 | |
566 | if (pattern_head != NULL) { | ||
567 | niro | 816 | /* convert char **argv to grep_list_data_t */ |
568 | niro | 532 | llist_t *cur; |
569 | |||
570 | for (cur = pattern_head; cur; cur = cur->link) | ||
571 | cur->data = new_grep_list_data(cur->data, 0); | ||
572 | } | ||
573 | niro | 816 | if (option_mask32 & OPT_f) |
574 | niro | 532 | load_regexes_from_file(fopt); |
575 | |||
576 | if (ENABLE_FEATURE_GREP_FGREP_ALIAS && applet_name[0] == 'f') | ||
577 | niro | 816 | option_mask32 |= OPT_F; |
578 | niro | 532 | |
579 | niro | 816 | #if !ENABLE_EXTRA_COMPAT |
580 | if (!(option_mask32 & (OPT_o | OPT_w))) | ||
581 | niro | 532 | reflags = REG_NOSUB; |
582 | niro | 816 | #endif |
583 | niro | 532 | |
584 | niro | 816 | if (ENABLE_FEATURE_GREP_EGREP_ALIAS |
585 | && (applet_name[0] == 'e' || (option_mask32 & OPT_E)) | ||
586 | ) { | ||
587 | niro | 532 | reflags |= REG_EXTENDED; |
588 | niro | 816 | } |
589 | #if ENABLE_EXTRA_COMPAT | ||
590 | else { | ||
591 | reflags = RE_SYNTAX_GREP; | ||
592 | } | ||
593 | #endif | ||
594 | niro | 532 | |
595 | niro | 816 | if (option_mask32 & OPT_i) { |
596 | #if !ENABLE_EXTRA_COMPAT | ||
597 | niro | 532 | reflags |= REG_ICASE; |
598 | niro | 816 | #else |
599 | int i; | ||
600 | case_fold = xmalloc(256); | ||
601 | for (i = 0; i < 256; i++) | ||
602 | case_fold[i] = (unsigned char)i; | ||
603 | for (i = 'a'; i <= 'z'; i++) | ||
604 | case_fold[i] = (unsigned char)(i - ('a' - 'A')); | ||
605 | #endif | ||
606 | } | ||
607 | niro | 532 | |
608 | argv += optind; | ||
609 | |||
610 | niro | 816 | /* if we didn't get a pattern from -e and no command file was specified, |
611 | * first parameter should be the pattern. no pattern, no worky */ | ||
612 | niro | 532 | if (pattern_head == NULL) { |
613 | niro | 816 | char *pattern; |
614 | niro | 532 | if (*argv == NULL) |
615 | bb_show_usage(); | ||
616 | niro | 816 | pattern = new_grep_list_data(*argv++, 0); |
617 | llist_add_to(&pattern_head, pattern); | ||
618 | niro | 532 | } |
619 | |||
620 | niro | 816 | /* argv[0..(argc-1)] should be names of file to grep through. If |
621 | niro | 532 | * there is more than one file to grep, we will print the filenames. */ |
622 | niro | 984 | if (argv[0] && argv[1]) |
623 | niro | 532 | print_filename = 1; |
624 | /* -H / -h of course override */ | ||
625 | niro | 816 | if (option_mask32 & OPT_H) |
626 | niro | 532 | print_filename = 1; |
627 | niro | 816 | if (option_mask32 & OPT_h) |
628 | niro | 532 | print_filename = 0; |
629 | |||
630 | /* If no files were specified, or '-' was specified, take input from | ||
631 | * stdin. Otherwise, we grep through all the files specified. */ | ||
632 | matched = 0; | ||
633 | niro | 816 | do { |
634 | niro | 984 | cur_file = *argv; |
635 | niro | 532 | file = stdin; |
636 | niro | 816 | if (!cur_file || LONE_DASH(cur_file)) { |
637 | niro | 532 | cur_file = "(standard input)"; |
638 | } else { | ||
639 | niro | 816 | if (option_mask32 & OPT_r) { |
640 | niro | 532 | struct stat st; |
641 | if (stat(cur_file, &st) == 0 && S_ISDIR(st.st_mode)) { | ||
642 | niro | 816 | if (!(option_mask32 & OPT_h)) |
643 | niro | 532 | print_filename = 1; |
644 | matched += grep_dir(cur_file); | ||
645 | goto grep_done; | ||
646 | } | ||
647 | } | ||
648 | /* else: fopen(dir) will succeed, but reading won't */ | ||
649 | niro | 816 | file = fopen_for_read(cur_file); |
650 | niro | 532 | if (file == NULL) { |
651 | if (!SUPPRESS_ERR_MSGS) | ||
652 | niro | 816 | bb_simple_perror_msg(cur_file); |
653 | niro | 532 | open_errors = 1; |
654 | continue; | ||
655 | } | ||
656 | } | ||
657 | matched += grep_file(file); | ||
658 | fclose_if_not_stdin(file); | ||
659 | niro | 816 | grep_done: ; |
660 | niro | 984 | } while (*argv && *++argv); |
661 | niro | 532 | |
662 | /* destroy all the elments in the pattern list */ | ||
663 | if (ENABLE_FEATURE_CLEAN_UP) { | ||
664 | while (pattern_head) { | ||
665 | llist_t *pattern_head_ptr = pattern_head; | ||
666 | niro | 816 | grep_list_data_t *gl = (grep_list_data_t *)pattern_head_ptr->data; |
667 | niro | 532 | |
668 | pattern_head = pattern_head->link; | ||
669 | niro | 816 | if (gl->flg_mem_alocated_compiled & ALLOCATED) |
670 | niro | 532 | free(gl->pattern); |
671 | niro | 816 | if (gl->flg_mem_alocated_compiled & COMPILED) |
672 | regfree(&gl->compiled_regex); | ||
673 | free(gl); | ||
674 | niro | 532 | free(pattern_head_ptr); |
675 | } | ||
676 | } | ||
677 | /* 0 = success, 1 = failed, 2 = error */ | ||
678 | if (open_errors) | ||
679 | return 2; | ||
680 | niro | 816 | return !matched; /* invert return value: 0 = success, 1 = failed */ |
681 | niro | 532 | } |