Start 1.33.0 development cycle
[oweals/busybox.git] / findutils / grep.c
1 /* 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 GPLv2 or later, see file LICENSE in this source tree.
9  */
10 /* BB_AUDIT SUSv3 defects - unsupported option -x "match whole line only". */
11 /* 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  * (C) 2006 Jac Goudsmit added -o option
19  */
20 //config:config GREP
21 //config:       bool "grep (8.6 kb)"
22 //config:       default y
23 //config:       help
24 //config:       grep is used to search files for a specified pattern.
25 //config:
26 //config:config EGREP
27 //config:       bool "egrep (7.8 kb)"
28 //config:       default y
29 //config:       help
30 //config:       Alias to "grep -E".
31 //config:
32 //config:config FGREP
33 //config:       bool "fgrep (7.8 kb)"
34 //config:       default y
35 //config:       help
36 //config:       Alias to "grep -F".
37 //config:
38 //config:config FEATURE_GREP_CONTEXT
39 //config:       bool "Enable before and after context flags (-A, -B and -C)"
40 //config:       default y
41 //config:       depends on GREP || EGREP || FGREP
42 //config:       help
43 //config:       Print the specified number of leading (-B) and/or trailing (-A)
44 //config:       context surrounding our matching lines.
45 //config:       Print the specified number of context lines (-C).
46
47 //applet:IF_GREP(APPLET(grep, BB_DIR_BIN, BB_SUID_DROP))
48 //                APPLET_ODDNAME:name   main  location    suid_type     help
49 //applet:IF_EGREP(APPLET_ODDNAME(egrep, grep, BB_DIR_BIN, BB_SUID_DROP, egrep))
50 //applet:IF_FGREP(APPLET_ODDNAME(fgrep, grep, BB_DIR_BIN, BB_SUID_DROP, fgrep))
51
52 //kbuild:lib-$(CONFIG_GREP) += grep.o
53 //kbuild:lib-$(CONFIG_EGREP) += grep.o
54 //kbuild:lib-$(CONFIG_FGREP) += grep.o
55
56 #include "libbb.h"
57 #include "common_bufsiz.h"
58 #include "xregex.h"
59
60
61 /* options */
62 //usage:#define grep_trivial_usage
63 //usage:       "[-HhnlLoqvsrRiwFE"
64 //usage:        IF_EXTRA_COMPAT("z")
65 //usage:       "] [-m N] "
66 //usage:        IF_FEATURE_GREP_CONTEXT("[-A/B/C N] ")
67 //usage:       "PATTERN/-e PATTERN.../-f FILE [FILE]..."
68 //usage:#define grep_full_usage "\n\n"
69 //usage:       "Search for PATTERN in FILEs (or stdin)\n"
70 //usage:     "\n        -H      Add 'filename:' prefix"
71 //usage:     "\n        -h      Do not add 'filename:' prefix"
72 //usage:     "\n        -n      Add 'line_no:' prefix"
73 //usage:     "\n        -l      Show only names of files that match"
74 //usage:     "\n        -L      Show only names of files that don't match"
75 //usage:     "\n        -c      Show only count of matching lines"
76 //usage:     "\n        -o      Show only the matching part of line"
77 //usage:     "\n        -q      Quiet. Return 0 if PATTERN is found, 1 otherwise"
78 //usage:     "\n        -v      Select non-matching lines"
79 //usage:     "\n        -s      Suppress open and read errors"
80 //usage:     "\n        -r      Recurse"
81 //usage:     "\n        -R      Recurse and dereference symlinks"
82 //usage:     "\n        -i      Ignore case"
83 //usage:     "\n        -w      Match whole words only"
84 //usage:     "\n        -x      Match whole lines only"
85 //usage:     "\n        -F      PATTERN is a literal (not regexp)"
86 //usage:     "\n        -E      PATTERN is an extended regexp"
87 //usage:        IF_EXTRA_COMPAT(
88 //usage:     "\n        -z      Input is NUL terminated"
89 //usage:        )
90 //usage:     "\n        -m N    Match up to N times per file"
91 //usage:        IF_FEATURE_GREP_CONTEXT(
92 //usage:     "\n        -A N    Print N lines of trailing context"
93 //usage:     "\n        -B N    Print N lines of leading context"
94 //usage:     "\n        -C N    Same as '-A N -B N'"
95 //usage:        )
96 //usage:     "\n        -e PTRN Pattern to match"
97 //usage:     "\n        -f FILE Read pattern from file"
98 //usage:
99 //usage:#define grep_example_usage
100 //usage:       "$ grep root /etc/passwd\n"
101 //usage:       "root:x:0:0:root:/root:/bin/bash\n"
102 //usage:       "$ grep ^[rR]oo. /etc/passwd\n"
103 //usage:       "root:x:0:0:root:/root:/bin/bash\n"
104 //usage:
105 //usage:#define egrep_trivial_usage NOUSAGE_STR
106 //usage:#define egrep_full_usage ""
107 //usage:#define fgrep_trivial_usage NOUSAGE_STR
108 //usage:#define fgrep_full_usage ""
109
110 /* -e,-f are lists; -m,-A,-B,-C have numeric param */
111 #define OPTSTR_GREP \
112         "lnqvscFiHhe:*f:*LorRm:+wx" \
113         IF_FEATURE_GREP_CONTEXT("A:+B:+C:+") \
114         "E" \
115         IF_EXTRA_COMPAT("z") \
116         "aI"
117 /* ignored: -a "assume all files to be text" */
118 /* ignored: -I "assume binary files have no matches" */
119 enum {
120         OPTBIT_l, /* list matched file names only */
121         OPTBIT_n, /* print line# */
122         OPTBIT_q, /* quiet - exit(EXIT_SUCCESS) of first match */
123         OPTBIT_v, /* invert the match, to select non-matching lines */
124         OPTBIT_s, /* suppress errors about file open errors */
125         OPTBIT_c, /* count matches per file (suppresses normal output) */
126         OPTBIT_F, /* literal match */
127         OPTBIT_i, /* case-insensitive */
128         OPTBIT_H, /* force filename display */
129         OPTBIT_h, /* inhibit filename display */
130         OPTBIT_e, /* -e PATTERN */
131         OPTBIT_f, /* -f FILE_WITH_PATTERNS */
132         OPTBIT_L, /* list unmatched file names only */
133         OPTBIT_o, /* show only matching parts of lines */
134         OPTBIT_r, /* recurse dirs */
135         OPTBIT_R, /* recurse dirs and symlinks to dirs */
136         OPTBIT_m, /* -m MAX_MATCHES */
137         OPTBIT_w, /* -w whole word match */
138         OPTBIT_x, /* -x whole line match */
139         IF_FEATURE_GREP_CONTEXT(    OPTBIT_A ,) /* -A NUM: after-match context */
140         IF_FEATURE_GREP_CONTEXT(    OPTBIT_B ,) /* -B NUM: before-match context */
141         IF_FEATURE_GREP_CONTEXT(    OPTBIT_C ,) /* -C NUM: -A and -B combined */
142         OPTBIT_E, /* extended regexp */
143         IF_EXTRA_COMPAT(            OPTBIT_z ,) /* input is NUL terminated */
144         OPT_l = 1 << OPTBIT_l,
145         OPT_n = 1 << OPTBIT_n,
146         OPT_q = 1 << OPTBIT_q,
147         OPT_v = 1 << OPTBIT_v,
148         OPT_s = 1 << OPTBIT_s,
149         OPT_c = 1 << OPTBIT_c,
150         OPT_F = 1 << OPTBIT_F,
151         OPT_i = 1 << OPTBIT_i,
152         OPT_H = 1 << OPTBIT_H,
153         OPT_h = 1 << OPTBIT_h,
154         OPT_e = 1 << OPTBIT_e,
155         OPT_f = 1 << OPTBIT_f,
156         OPT_L = 1 << OPTBIT_L,
157         OPT_o = 1 << OPTBIT_o,
158         OPT_r = 1 << OPTBIT_r,
159         OPT_R = 1 << OPTBIT_R,
160         OPT_m = 1 << OPTBIT_m,
161         OPT_w = 1 << OPTBIT_w,
162         OPT_x = 1 << OPTBIT_x,
163         OPT_A = IF_FEATURE_GREP_CONTEXT(    (1 << OPTBIT_A)) + 0,
164         OPT_B = IF_FEATURE_GREP_CONTEXT(    (1 << OPTBIT_B)) + 0,
165         OPT_C = IF_FEATURE_GREP_CONTEXT(    (1 << OPTBIT_C)) + 0,
166         OPT_E = 1 << OPTBIT_E,
167         OPT_z = IF_EXTRA_COMPAT(            (1 << OPTBIT_z)) + 0,
168 };
169
170 #define PRINT_FILES_WITH_MATCHES    (option_mask32 & OPT_l)
171 #define PRINT_LINE_NUM              (option_mask32 & OPT_n)
172 #define BE_QUIET                    (option_mask32 & OPT_q)
173 #define SUPPRESS_ERR_MSGS           (option_mask32 & OPT_s)
174 #define PRINT_MATCH_COUNTS          (option_mask32 & OPT_c)
175 #define FGREP_FLAG                  (option_mask32 & OPT_F)
176 #define PRINT_FILES_WITHOUT_MATCHES (option_mask32 & OPT_L)
177 #define NUL_DELIMITED               (option_mask32 & OPT_z)
178
179 struct globals {
180         int max_matches;
181 #if !ENABLE_EXTRA_COMPAT
182         int reflags;
183 #else
184         RE_TRANSLATE_TYPE case_fold; /* RE_TRANSLATE_TYPE is [[un]signed] char* */
185 #endif
186         smalluint invert_search;
187         smalluint print_filename;
188         smalluint open_errors;
189 #if ENABLE_FEATURE_GREP_CONTEXT
190         smalluint did_print_line;
191         int lines_before;
192         int lines_after;
193         char **before_buf;
194         IF_EXTRA_COMPAT(size_t *before_buf_size;)
195         int last_line_printed;
196 #endif
197         /* globals used internally */
198         llist_t *pattern_head;   /* growable list of patterns to match */
199         const char *cur_file;    /* the current file we are reading */
200 } FIX_ALIASING;
201 #define G (*(struct globals*)bb_common_bufsiz1)
202 #define INIT_G() do { \
203         setup_common_bufsiz(); \
204         BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
205 } while (0)
206 #define max_matches       (G.max_matches         )
207 #if !ENABLE_EXTRA_COMPAT
208 # define reflags          (G.reflags             )
209 #else
210 # define case_fold        (G.case_fold           )
211 /* http://www.delorie.com/gnu/docs/regex/regex_46.html */
212 # define reflags           re_syntax_options
213 # undef REG_NOSUB
214 # undef REG_EXTENDED
215 # undef REG_ICASE
216 # define REG_NOSUB    bug:is:here /* should not be used */
217 /* Just RE_SYNTAX_EGREP is not enough, need to enable {n[,[m]]} too */
218 # define REG_EXTENDED (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES)
219 # define REG_ICASE    bug:is:here /* should not be used */
220 #endif
221 #define invert_search     (G.invert_search       )
222 #define print_filename    (G.print_filename      )
223 #define open_errors       (G.open_errors         )
224 #define did_print_line    (G.did_print_line      )
225 #define lines_before      (G.lines_before        )
226 #define lines_after       (G.lines_after         )
227 #define before_buf        (G.before_buf          )
228 #define before_buf_size   (G.before_buf_size     )
229 #define last_line_printed (G.last_line_printed   )
230 #define pattern_head      (G.pattern_head        )
231 #define cur_file          (G.cur_file            )
232
233
234 typedef struct grep_list_data_t {
235         char *pattern;
236 /* for GNU regex, matched_range must be persistent across grep_file() calls */
237 #if !ENABLE_EXTRA_COMPAT
238         regex_t compiled_regex;
239         regmatch_t matched_range;
240 #else
241         struct re_pattern_buffer compiled_regex;
242         struct re_registers matched_range;
243 #endif
244 #define ALLOCATED 1
245 #define COMPILED 2
246         int flg_mem_allocated_compiled;
247 } grep_list_data_t;
248
249 #if !ENABLE_EXTRA_COMPAT
250 #define print_line(line, line_len, linenum, decoration) \
251         print_line(line, linenum, decoration)
252 #endif
253 static void print_line(const char *line, size_t line_len, int linenum, char decoration)
254 {
255 #if ENABLE_FEATURE_GREP_CONTEXT
256         /* Happens when we go to next file, immediately hit match
257          * and try to print prev context... from prev file! Don't do it */
258         if (linenum < 1)
259                 return;
260         /* possibly print the little '--' separator */
261         if ((lines_before || lines_after) && did_print_line
262          && last_line_printed != linenum - 1
263         ) {
264                 puts("--");
265         }
266         /* guard against printing "--" before first line of first file */
267         did_print_line = 1;
268         last_line_printed = linenum;
269 #endif
270         if (print_filename)
271                 printf("%s%c", cur_file, decoration);
272         if (PRINT_LINE_NUM)
273                 printf("%i%c", linenum, decoration);
274         /* Emulate weird GNU grep behavior with -ov */
275         if ((option_mask32 & (OPT_v|OPT_o)) != (OPT_v|OPT_o)) {
276 #if !ENABLE_EXTRA_COMPAT
277                 puts(line);
278 #else
279                 fwrite(line, 1, line_len, stdout);
280                 putchar(NUL_DELIMITED ? '\0' : '\n');
281 #endif
282         }
283 }
284
285 #if ENABLE_EXTRA_COMPAT
286 /* Unlike getline, this one removes trailing '\n' */
287 static ssize_t FAST_FUNC bb_getline(char **line_ptr, size_t *line_alloc_len, FILE *file)
288 {
289         ssize_t res_sz;
290         char *line;
291         int delim = (NUL_DELIMITED ? '\0' : '\n');
292
293         res_sz = getdelim(line_ptr, line_alloc_len, delim, file);
294         line = *line_ptr;
295
296         if (res_sz > 0) {
297                 if (line[res_sz - 1] == delim)
298                         line[--res_sz] = '\0';
299         } else {
300                 free(line); /* uclibc allocates a buffer even on EOF. WTF? */
301         }
302         return res_sz;
303 }
304 #endif
305
306 static int grep_file(FILE *file)
307 {
308         smalluint found;
309         int linenum = 0;
310         int nmatches = 0;
311 #if !ENABLE_EXTRA_COMPAT
312         char *line;
313 #else
314         char *line = NULL;
315         ssize_t line_len;
316         size_t line_alloc_len;
317 # define rm_so start[0]
318 # define rm_eo end[0]
319 #endif
320 #if ENABLE_FEATURE_GREP_CONTEXT
321         int print_n_lines_after = 0;
322         int curpos = 0; /* track where we are in the circular 'before' buffer */
323         int idx = 0; /* used for iteration through the circular buffer */
324 #else
325         enum { print_n_lines_after = 0 };
326 #endif
327
328         while (
329 #if !ENABLE_EXTRA_COMPAT
330                 (line = xmalloc_fgetline(file)) != NULL
331 #else
332                 (line_len = bb_getline(&line, &line_alloc_len, file)) >= 0
333 #endif
334         ) {
335                 llist_t *pattern_ptr = pattern_head;
336                 grep_list_data_t *gl = gl; /* for gcc */
337
338                 linenum++;
339                 found = 0;
340                 while (pattern_ptr) {
341                         gl = (grep_list_data_t *)pattern_ptr->data;
342                         if (FGREP_FLAG) {
343                                 char *match;
344                                 char *str = line;
345  opt_f_again:
346                                 match = ((option_mask32 & OPT_i)
347                                         ? strcasestr(str, gl->pattern)
348                                         : strstr(str, gl->pattern)
349                                         );
350                                 if (match) {
351                                         if (option_mask32 & OPT_x) {
352                                                 if (match != str)
353                                                         goto opt_f_not_found;
354                                                 if (str[strlen(gl->pattern)] != '\0')
355                                                         goto opt_f_not_found;
356                                         } else
357                                         if (option_mask32 & OPT_w) {
358                                                 char c = (match != line) ? match[-1] : ' ';
359                                                 if (!isalnum(c) && c != '_') {
360                                                         c = match[strlen(gl->pattern)];
361                                                         if (!c || (!isalnum(c) && c != '_'))
362                                                                 goto opt_f_found;
363                                                 }
364                                                 str = match + 1;
365                                                 goto opt_f_again;
366                                         }
367  opt_f_found:
368                                         found = 1;
369  opt_f_not_found: ;
370                                 }
371                         } else {
372 #if ENABLE_EXTRA_COMPAT
373                                 unsigned start_pos;
374 #else
375                                 int match_flg;
376 #endif
377                                 char *match_at;
378
379                                 if (!(gl->flg_mem_allocated_compiled & COMPILED)) {
380                                         gl->flg_mem_allocated_compiled |= COMPILED;
381 #if !ENABLE_EXTRA_COMPAT
382                                         xregcomp(&gl->compiled_regex, gl->pattern, reflags);
383 #else
384                                         memset(&gl->compiled_regex, 0, sizeof(gl->compiled_regex));
385                                         gl->compiled_regex.translate = case_fold; /* for -i */
386                                         if (re_compile_pattern(gl->pattern, strlen(gl->pattern), &gl->compiled_regex))
387                                                 bb_error_msg_and_die("bad regex '%s'", gl->pattern);
388 #endif
389                                 }
390 #if !ENABLE_EXTRA_COMPAT
391                                 gl->matched_range.rm_so = 0;
392                                 gl->matched_range.rm_eo = 0;
393                                 match_flg = 0;
394 #else
395                                 start_pos = 0;
396 #endif
397                                 match_at = line;
398  opt_w_again:
399 //bb_error_msg("'%s' start_pos:%d line_len:%d", match_at, start_pos, line_len);
400                                 if (
401 #if !ENABLE_EXTRA_COMPAT
402                                         regexec(&gl->compiled_regex, match_at, 1, &gl->matched_range, match_flg) == 0
403 #else
404                                         re_search(&gl->compiled_regex, match_at, line_len,
405                                                         start_pos, /*range:*/ line_len,
406                                                         &gl->matched_range) >= 0
407 #endif
408                                 ) {
409                                         if (option_mask32 & OPT_x) {
410                                                 found |= (gl->matched_range.rm_so == 0
411                                                          && match_at[gl->matched_range.rm_eo] == '\0');
412                                         } else
413                                         if (!(option_mask32 & OPT_w)) {
414                                                 found = 1;
415                                         } else {
416                                                 char c = ' ';
417                                                 if (match_at > line || gl->matched_range.rm_so != 0) {
418                                                         c = match_at[gl->matched_range.rm_so - 1];
419                                                 }
420                                                 if (!isalnum(c) && c != '_') {
421                                                         c = match_at[gl->matched_range.rm_eo];
422                                                 }
423                                                 if (!isalnum(c) && c != '_') {
424                                                         found = 1;
425                                                 } else {
426                         /*
427                          * Why check gl->matched_range.rm_eo?
428                          * Zero-length match makes -w skip the line:
429                          * "echo foo | grep ^" prints "foo",
430                          * "echo foo | grep -w ^" prints nothing.
431                          * Without such check, we can loop forever.
432                          */
433 #if !ENABLE_EXTRA_COMPAT
434                                                         if (gl->matched_range.rm_eo != 0) {
435                                                                 match_at += gl->matched_range.rm_eo;
436                                                                 match_flg |= REG_NOTBOL;
437                                                                 goto opt_w_again;
438                                                         }
439 #else
440                                                         if (gl->matched_range.rm_eo > start_pos) {
441                                                                 start_pos = gl->matched_range.rm_eo;
442                                                                 goto opt_w_again;
443                                                         }
444 #endif
445                                                 }
446                                         }
447                                 }
448                         }
449                         /* If it's a non-inverted search, we can stop
450                          * at first match and report it.
451                          * If it's an inverted search, we can move on
452                          * to the next line of input, ignoring the
453                          * rest of the patterns.
454                          */
455                         if (found) {
456                                 //if (invert_search)
457                                 //      goto do_not_found;
458                                 //goto do_found;
459                                 break; // this accomplishes both
460                         }
461                         pattern_ptr = pattern_ptr->link;
462                 } /* while (pattern_ptr) */
463
464                 if (found ^ invert_search) {
465  //do_found:
466                         /* keep track of matches */
467                         nmatches++;
468
469                         /* quiet/print (non)matching file names only? */
470                         if (option_mask32 & (OPT_q|OPT_l|OPT_L)) {
471                                 free(line); /* we don't need line anymore */
472                                 if (BE_QUIET) {
473                                         /* manpage says about -q:
474                                          * "exit immediately with zero status
475                                          * if any match is found,
476                                          * even if errors were detected" */
477                                         exit(EXIT_SUCCESS);
478                                 }
479                                 /* if we're just printing filenames, we stop after the first match */
480                                 if (PRINT_FILES_WITH_MATCHES) {
481                                         puts(cur_file);
482                                         /* fall through to "return 1" */
483                                 }
484                                 /* OPT_L aka PRINT_FILES_WITHOUT_MATCHES: return early */
485                                 return 1; /* one match */
486                         }
487
488 #if ENABLE_FEATURE_GREP_CONTEXT
489                         /* Were we printing context and saw next (unwanted) match? */
490                         if ((option_mask32 & OPT_m) && nmatches > max_matches)
491                                 break;
492 #endif
493
494                         /* print the matched line */
495                         if (PRINT_MATCH_COUNTS == 0) {
496 #if ENABLE_FEATURE_GREP_CONTEXT
497                                 int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
498
499                                 /* if we were told to print 'before' lines and there is at least
500                                  * one line in the circular buffer, print them */
501                                 if (lines_before && before_buf[prevpos] != NULL) {
502                                         int first_buf_entry_line_num = linenum - lines_before;
503
504                                         /* advance to the first entry in the circular buffer, and
505                                          * figure out the line number is of the first line in the
506                                          * buffer */
507                                         idx = curpos;
508                                         while (before_buf[idx] == NULL) {
509                                                 idx = (idx + 1) % lines_before;
510                                                 first_buf_entry_line_num++;
511                                         }
512
513                                         /* now print each line in the buffer, clearing them as we go */
514                                         while (before_buf[idx] != NULL) {
515                                                 print_line(before_buf[idx], before_buf_size[idx], first_buf_entry_line_num, '-');
516                                                 free(before_buf[idx]);
517                                                 before_buf[idx] = NULL;
518                                                 idx = (idx + 1) % lines_before;
519                                                 first_buf_entry_line_num++;
520                                         }
521                                 }
522
523                                 /* make a note that we need to print 'after' lines */
524                                 print_n_lines_after = lines_after;
525 #endif
526                                 if (option_mask32 & OPT_o) {
527                                         if (FGREP_FLAG) {
528                                                 /* -Fo just prints the pattern
529                                                  * (unless -v: -Fov doesn't print anything at all) */
530                                                 if (found)
531                                                         print_line(gl->pattern, strlen(gl->pattern), linenum, ':');
532                                         } else while (1) {
533                                                 unsigned start = gl->matched_range.rm_so;
534                                                 unsigned end = gl->matched_range.rm_eo;
535                                                 unsigned len = end - start;
536                                                 char old = line[end];
537                                                 line[end] = '\0';
538                                                 /* Empty match is not printed: try "echo test | grep -o ''" */
539                                                 if (len != 0)
540                                                         print_line(line + start, len, linenum, ':');
541                                                 if (old == '\0')
542                                                         break;
543                                                 line[end] = old;
544                                                 if (len == 0)
545                                                         end++;
546 #if !ENABLE_EXTRA_COMPAT
547                                                 if (regexec(&gl->compiled_regex, line + end,
548                                                                 1, &gl->matched_range, REG_NOTBOL) != 0)
549                                                         break;
550                                                 gl->matched_range.rm_so += end;
551                                                 gl->matched_range.rm_eo += end;
552 #else
553                                                 if (re_search(&gl->compiled_regex, line, line_len,
554                                                                 end, line_len - end,
555                                                                 &gl->matched_range) < 0)
556                                                         break;
557 #endif
558                                         }
559                                 } else {
560                                         print_line(line, line_len, linenum, ':');
561                                 }
562                         }
563                 }
564 #if ENABLE_FEATURE_GREP_CONTEXT
565                 else { /* no match */
566  //do_not_found:
567                         /* if we need to print some context lines after the last match, do so */
568                         if (print_n_lines_after) {
569                                 print_line(line, strlen(line), linenum, '-');
570                                 print_n_lines_after--;
571                         } else if (lines_before) {
572                                 /* Add the line to the circular 'before' buffer */
573                                 free(before_buf[curpos]);
574                                 before_buf[curpos] = line;
575                                 IF_EXTRA_COMPAT(before_buf_size[curpos] = line_len;)
576                                 curpos = (curpos + 1) % lines_before;
577                                 /* avoid free(line) - we took the line */
578                                 line = NULL;
579                         }
580                 }
581
582 #endif /* ENABLE_FEATURE_GREP_CONTEXT */
583 #if !ENABLE_EXTRA_COMPAT
584                 free(line);
585 #endif
586                 /* Did we print all context after last requested match? */
587                 if ((option_mask32 & OPT_m)
588                  && !print_n_lines_after
589                  && nmatches == max_matches
590                 ) {
591                         break;
592                 }
593         } /* while (read line) */
594
595         /* special-case file post-processing for options where we don't print line
596          * matches, just filenames and possibly match counts */
597
598         /* grep -c: print [filename:]count, even if count is zero */
599         if (PRINT_MATCH_COUNTS) {
600                 if (print_filename)
601                         printf("%s:", cur_file);
602                 printf("%d\n", nmatches);
603         }
604
605         /* grep -L: print just the filename */
606         if (PRINT_FILES_WITHOUT_MATCHES) {
607                 /* nmatches is zero, no need to check it:
608                  * we return 1 early if we detected a match
609                  * and PRINT_FILES_WITHOUT_MATCHES is set */
610                 puts(cur_file);
611         }
612
613         return nmatches;
614 }
615
616 #if ENABLE_FEATURE_CLEAN_UP
617 #define new_grep_list_data(p, m) add_grep_list_data(p, m)
618 static char *add_grep_list_data(char *pattern, int flg_used_mem)
619 #else
620 #define new_grep_list_data(p, m) add_grep_list_data(p)
621 static char *add_grep_list_data(char *pattern)
622 #endif
623 {
624         grep_list_data_t *gl = xzalloc(sizeof(*gl));
625         gl->pattern = pattern;
626 #if ENABLE_FEATURE_CLEAN_UP
627         gl->flg_mem_allocated_compiled = flg_used_mem;
628 #else
629         /*gl->flg_mem_allocated_compiled = 0;*/
630 #endif
631         return (char *)gl;
632 }
633
634 static void load_regexes_from_file(llist_t *fopt)
635 {
636         while (fopt) {
637                 char *line;
638                 FILE *fp;
639                 llist_t *cur = fopt;
640                 char *ffile = cur->data;
641
642                 fopt = cur->link;
643                 free(cur);
644                 fp = xfopen_stdin(ffile);
645                 while ((line = xmalloc_fgetline(fp)) != NULL) {
646                         llist_add_to(&pattern_head,
647                                 new_grep_list_data(line, ALLOCATED));
648                 }
649                 fclose_if_not_stdin(fp);
650         }
651 }
652
653 static void load_pattern_list(llist_t **lst, char *pattern)
654 {
655         char *p;
656         while ((p = strsep(&pattern, "\n")) != NULL)
657                 llist_add_to(lst, new_grep_list_data(p, 0));
658 }
659
660 static int FAST_FUNC file_action_grep(const char *filename,
661                         struct stat *statbuf,
662                         void* matched,
663                         int depth UNUSED_PARAM)
664 {
665         FILE *file;
666
667         /* If we are given a link to a directory, we should bail out now, rather
668          * than trying to open the "file" and hoping getline gives us nothing,
669          * since that is not portable across operating systems (FreeBSD for
670          * example will return the raw directory contents). */
671         if (S_ISLNK(statbuf->st_mode)) {
672                 struct stat sb;
673                 if (stat(filename, &sb) != 0) {
674                         if (!SUPPRESS_ERR_MSGS)
675                                 bb_simple_perror_msg(filename);
676                         return 0;
677                 }
678                 if (S_ISDIR(sb.st_mode))
679                         return 1;
680         }
681
682         file = fopen_for_read(filename);
683         if (file == NULL) {
684                 if (!SUPPRESS_ERR_MSGS)
685                         bb_simple_perror_msg(filename);
686                 open_errors = 1;
687                 return 0;
688         }
689         cur_file = filename;
690         *(int*)matched += grep_file(file);
691         fclose(file);
692         return 1;
693 }
694
695 static int grep_dir(const char *dir)
696 {
697         int matched = 0;
698         recursive_action(dir,
699                 /* recurse=yes */ ACTION_RECURSE |
700                 /* followLinks=always */ ((option_mask32 & OPT_R) ? ACTION_FOLLOWLINKS : 0) |
701                 /* followLinks=command line only */ ACTION_FOLLOWLINKS_L0 |
702                 /* depthFirst=yes */ ACTION_DEPTHFIRST,
703                 /* fileAction= */ file_action_grep,
704                 /* dirAction= */ NULL,
705                 /* userData= */ &matched,
706                 /* depth= */ 0);
707         return matched;
708 }
709
710 int grep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
711 int grep_main(int argc UNUSED_PARAM, char **argv)
712 {
713         FILE *file;
714         int matched;
715         llist_t *fopt = NULL;
716 #if ENABLE_FEATURE_GREP_CONTEXT
717         int Copt, opts;
718 #endif
719         INIT_G();
720
721         /* For grep, exitcode of 1 is "not found". Other errors are 2: */
722         xfunc_error_retval = 2;
723
724         /* do normal option parsing */
725 #if ENABLE_FEATURE_GREP_CONTEXT
726         /* -H unsets -h; -C unsets -A,-B */
727         opts = getopt32long(argv, "^"
728                 OPTSTR_GREP
729                         "\0"
730                         "H-h:C-AB",
731                 "color\0" Optional_argument "\xff",
732                 &pattern_head, &fopt, &max_matches,
733                 &lines_after, &lines_before, &Copt
734                 , NULL
735         );
736
737         if (opts & OPT_C) {
738                 /* -C unsets prev -A and -B, but following -A or -B
739                  * may override it */
740                 if (!(opts & OPT_A)) /* not overridden */
741                         lines_after = Copt;
742                 if (!(opts & OPT_B)) /* not overridden */
743                         lines_before = Copt;
744         }
745         /* sanity checks */
746         if (opts & (OPT_c|OPT_q|OPT_l|OPT_L)) {
747                 option_mask32 &= ~OPT_n;
748                 lines_before = 0;
749                 lines_after = 0;
750         } else if (lines_before > 0) {
751                 if (lines_before > INT_MAX / sizeof(long long))
752                         lines_before = INT_MAX / sizeof(long long);
753                 /* overflow in (lines_before * sizeof(x)) is prevented (above) */
754                 before_buf = xzalloc(lines_before * sizeof(before_buf[0]));
755                 IF_EXTRA_COMPAT(before_buf_size = xzalloc(lines_before * sizeof(before_buf_size[0]));)
756         }
757 #else
758         /* with auto sanity checks */
759         getopt32(argv, "^" OPTSTR_GREP "\0" "H-h:c-n:q-n:l-n:", // why trailing ":"?
760                 &pattern_head, &fopt, &max_matches);
761 #endif
762         invert_search = ((option_mask32 & OPT_v) != 0); /* 0 | 1 */
763
764         {       /* convert char **argv to pattern_list */
765                 llist_t *cur, *new = NULL;
766                 for (cur = pattern_head; cur; cur = cur->link)
767                         load_pattern_list(&new, cur->data);
768                 llist_free(pattern_head, NULL);
769                 pattern_head = new;
770         }
771         if (option_mask32 & OPT_f) {
772                 load_regexes_from_file(fopt);
773                 if (!pattern_head) { /* -f EMPTY_FILE? */
774                         /* GNU grep treats it as "nothing matches" except when -x */
775                         const char *data = (option_mask32 & OPT_x) ? ".*" : "";
776                         llist_add_to(&pattern_head, new_grep_list_data((char*)data, 0));
777                         invert_search ^= 1;
778                 }
779         }
780
781         if (ENABLE_FGREP && applet_name[0] == 'f')
782                 option_mask32 |= OPT_F;
783
784 #if !ENABLE_EXTRA_COMPAT
785         if (!(option_mask32 & (OPT_o | OPT_w | OPT_x)))
786                 reflags = REG_NOSUB;
787 #endif
788
789         if ((ENABLE_EGREP && applet_name[0] == 'e')
790          || (option_mask32 & OPT_E)
791         ) {
792                 reflags |= REG_EXTENDED;
793         }
794 #if ENABLE_EXTRA_COMPAT
795         else {
796                 reflags = RE_SYNTAX_GREP;
797         }
798 #endif
799
800         if (option_mask32 & OPT_i) {
801 #if !ENABLE_EXTRA_COMPAT
802                 reflags |= REG_ICASE;
803 #else
804                 int i;
805                 case_fold = xmalloc(256);
806                 for (i = 0; i < 256; i++)
807                         case_fold[i] = (unsigned char)i;
808                 for (i = 'a'; i <= 'z'; i++)
809                         case_fold[i] = (unsigned char)(i - ('a' - 'A'));
810 #endif
811         }
812
813         argv += optind;
814
815         /* if we didn't get a pattern from -e and no command file was specified,
816          * first parameter should be the pattern. no pattern, no worky */
817         if (pattern_head == NULL) {
818                 if (*argv == NULL)
819                         bb_show_usage();
820                 load_pattern_list(&pattern_head, *argv++);
821         }
822
823         /* argv[0..(argc-1)] should be names of file to grep through. If
824          * there is more than one file to grep, we will print the filenames. */
825         if (argv[0] && argv[1])
826                 print_filename = 1;
827         /* -H / -h of course override */
828         if (option_mask32 & OPT_H)
829                 print_filename = 1;
830         if (option_mask32 & OPT_h)
831                 print_filename = 0;
832
833         /* If no files were specified, or '-' was specified, take input from
834          * stdin. Otherwise, we grep through all the files specified. */
835         matched = 0;
836         do {
837                 cur_file = *argv;
838                 file = stdin;
839                 if (!cur_file || LONE_DASH(cur_file)) {
840                         cur_file = "(standard input)";
841                 } else {
842                         if (option_mask32 & (OPT_r|OPT_R)) {
843                                 struct stat st;
844                                 if (stat(cur_file, &st) == 0 && S_ISDIR(st.st_mode)) {
845                                         if (!(option_mask32 & OPT_h))
846                                                 print_filename = 1;
847                                         matched += grep_dir(cur_file);
848                                         goto grep_done;
849                                 }
850                         }
851                         /* else: fopen(dir) will succeed, but reading won't */
852                         file = fopen_for_read(cur_file);
853                         if (file == NULL) {
854                                 if (!SUPPRESS_ERR_MSGS)
855                                         bb_simple_perror_msg(cur_file);
856                                 open_errors = 1;
857                                 continue;
858                         }
859                 }
860                 matched += grep_file(file);
861                 fclose_if_not_stdin(file);
862  grep_done: ;
863         } while (*argv && *++argv);
864
865         /* destroy all the elements in the pattern list */
866         if (ENABLE_FEATURE_CLEAN_UP) {
867                 while (pattern_head) {
868                         llist_t *pattern_head_ptr = pattern_head;
869                         grep_list_data_t *gl = (grep_list_data_t *)pattern_head_ptr->data;
870
871                         pattern_head = pattern_head->link;
872                         if (gl->flg_mem_allocated_compiled & ALLOCATED)
873                                 free(gl->pattern);
874                         if (gl->flg_mem_allocated_compiled & COMPILED)
875                                 regfree(&gl->compiled_regex);
876                         free(gl);
877                         free(pattern_head_ptr);
878                 }
879         }
880         /* 0 = success, 1 = failed, 2 = error */
881         if (open_errors)
882                 return 2;
883         return !matched; /* invert return value: 0 = success, 1 = failed */
884 }