grep: fix buglets with context printing
[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 the GPL v2 or later, see the file LICENSE in this tarball.
9  */
10 /* BB_AUDIT SUSv3 defects - unsupported option -x.  */
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 /*
19  * (C) 2006 Jac Goudsmit added -o option
20  */
21
22 #include "libbb.h"
23 #include "xregex.h"
24
25 /* options */
26 #define OPTSTR_GREP \
27         "lnqvscFiHhe:f:Lor" \
28         USE_FEATURE_GREP_CONTEXT("A:B:C:") \
29         USE_FEATURE_GREP_EGREP_ALIAS("E") \
30         USE_DESKTOP("w") \
31         "aI"
32 /* ignored: -a "assume all files to be text" */
33 /* ignored: -I "assume binary files have no matches" */
34
35 enum {
36         OPTBIT_l,
37         OPTBIT_n,
38         OPTBIT_q,
39         OPTBIT_v,
40         OPTBIT_s,
41         OPTBIT_c,
42         OPTBIT_F,
43         OPTBIT_i,
44         OPTBIT_H,
45         OPTBIT_h,
46         OPTBIT_e,
47         OPTBIT_f,
48         OPTBIT_L,
49         OPTBIT_o,
50         OPTBIT_r,
51         USE_FEATURE_GREP_CONTEXT(    OPTBIT_A ,)
52         USE_FEATURE_GREP_CONTEXT(    OPTBIT_B ,)
53         USE_FEATURE_GREP_CONTEXT(    OPTBIT_C ,)
54         USE_FEATURE_GREP_EGREP_ALIAS(OPTBIT_E ,)
55         USE_DESKTOP(                 OPTBIT_w ,)
56         OPT_l = 1 << OPTBIT_l,
57         OPT_n = 1 << OPTBIT_n,
58         OPT_q = 1 << OPTBIT_q,
59         OPT_v = 1 << OPTBIT_v,
60         OPT_s = 1 << OPTBIT_s,
61         OPT_c = 1 << OPTBIT_c,
62         OPT_F = 1 << OPTBIT_F,
63         OPT_i = 1 << OPTBIT_i,
64         OPT_H = 1 << OPTBIT_H,
65         OPT_h = 1 << OPTBIT_h,
66         OPT_e = 1 << OPTBIT_e,
67         OPT_f = 1 << OPTBIT_f,
68         OPT_L = 1 << OPTBIT_L,
69         OPT_o = 1 << OPTBIT_o,
70         OPT_r = 1 << OPTBIT_r,
71         OPT_A = USE_FEATURE_GREP_CONTEXT(    (1 << OPTBIT_A)) + 0,
72         OPT_B = USE_FEATURE_GREP_CONTEXT(    (1 << OPTBIT_B)) + 0,
73         OPT_C = USE_FEATURE_GREP_CONTEXT(    (1 << OPTBIT_C)) + 0,
74         OPT_E = USE_FEATURE_GREP_EGREP_ALIAS((1 << OPTBIT_E)) + 0,
75         OPT_w = USE_DESKTOP(                 (1 << OPTBIT_w)) + 0,
76 };
77
78 #define PRINT_FILES_WITH_MATCHES    (option_mask32 & OPT_l)
79 #define PRINT_LINE_NUM              (option_mask32 & OPT_n)
80 #define BE_QUIET                    (option_mask32 & OPT_q)
81 #define SUPPRESS_ERR_MSGS           (option_mask32 & OPT_s)
82 #define PRINT_MATCH_COUNTS          (option_mask32 & OPT_c)
83 #define FGREP_FLAG                  (option_mask32 & OPT_F)
84 #define PRINT_FILES_WITHOUT_MATCHES (option_mask32 & OPT_L)
85
86 typedef unsigned char byte_t;
87
88 static int reflags;
89 static byte_t invert_search;
90 static byte_t print_filename;
91 static byte_t open_errors;
92
93 #if ENABLE_FEATURE_GREP_CONTEXT
94 static byte_t did_print_line;
95 static int lines_before;
96 static int lines_after;
97 static char **before_buf;
98 static int last_line_printed;
99 #endif /* ENABLE_FEATURE_GREP_CONTEXT */
100
101 /* globals used internally */
102 static llist_t *pattern_head;   /* growable list of patterns to match */
103 static const char *cur_file;    /* the current file we are reading */
104
105 typedef struct grep_list_data_t {
106         char *pattern;
107         regex_t preg;
108 #define PATTERN_MEM_A 1
109 #define COMPILED 2
110         int flg_mem_alocated_compiled;
111 } grep_list_data_t;
112
113 static void print_line(const char *line, int linenum, char decoration)
114 {
115 #if ENABLE_FEATURE_GREP_CONTEXT
116         /* Happens when we go to next file, immediately hit match
117          * and try to print prev context... from prev file! Don't do it */
118         if (linenum < 1)
119                 return;
120         /* possibly print the little '--' separator */
121         if ((lines_before || lines_after) && did_print_line &&
122                         last_line_printed != linenum - 1) {
123                 puts("--");
124         }
125         /* guard against printing "--" before first line of first file */
126         did_print_line = 1;
127         last_line_printed = linenum;
128 #endif
129         if (print_filename)
130                 printf("%s%c", cur_file, decoration);
131         if (PRINT_LINE_NUM)
132                 printf("%i%c", linenum, decoration);
133         /* Emulate weird GNU grep behavior with -ov */
134         if ((option_mask32 & (OPT_v|OPT_o)) != (OPT_v|OPT_o))
135                 puts(line);
136 }
137
138 static int grep_file(FILE *file)
139 {
140         char *line;
141         byte_t ret;
142         int linenum = 0;
143         int nmatches = 0;
144         regmatch_t regmatch;
145 #if ENABLE_FEATURE_GREP_CONTEXT
146         int print_n_lines_after = 0;
147         int curpos = 0; /* track where we are in the circular 'before' buffer */
148         int idx = 0; /* used for iteration through the circular buffer */
149 #endif /* ENABLE_FEATURE_GREP_CONTEXT */
150
151         while ((line = xmalloc_getline(file)) != NULL) {
152                 llist_t *pattern_ptr = pattern_head;
153                 grep_list_data_t * gl;
154
155                 linenum++;
156                 ret = 0;
157                 while (pattern_ptr) {
158                         gl = (grep_list_data_t *)pattern_ptr->data;
159                         if (FGREP_FLAG) {
160                                 ret = strstr(line, gl->pattern) != NULL;
161                         } else {
162                                 /*
163                                  * test for a postitive-assertion match (regexec returns success (0)
164                                  * and the user did not specify invert search), or a negative-assertion
165                                  * match (regexec returns failure (REG_NOMATCH) and the user specified
166                                  * invert search)
167                                  */
168                                 if (!(gl->flg_mem_alocated_compiled & COMPILED)) {
169                                         gl->flg_mem_alocated_compiled |= COMPILED;
170                                         xregcomp(&(gl->preg), gl->pattern, reflags);
171                                 }
172                                 regmatch.rm_so = 0;
173                                 regmatch.rm_eo = 0;
174                                 if (regexec(&(gl->preg), line, 1, &regmatch, 0) == 0) {
175                                         if (!(option_mask32 & OPT_w))
176                                                 ret = 1;
177                                         else {
178                                                 char c = ' ';
179                                                 if (regmatch.rm_so)
180                                                         c = line[regmatch.rm_so - 1];
181                                                 if (!isalnum(c) && c != '_') {
182                                                         c = line[regmatch.rm_eo];
183                                                         if (!c || (!isalnum(c) && c != '_'))
184                                                                 ret = 1;
185                                                 }
186                                         }
187                                 }
188                         }
189                         pattern_ptr = pattern_ptr->link;
190                 } /* while (pattern_ptr) */
191
192                 if (ret ^ invert_search) {
193                         /* keep track of matches */
194                         nmatches++;
195
196                         /* quiet/print (non)matching file names only? */
197                         if (option_mask32 & (OPT_q|OPT_l|OPT_L)) {
198                                 free(line); /* we don't need line anymore */
199                                 if (BE_QUIET) {
200                                         /* manpage says about -q:
201                                          * "exit immediately with zero status
202                                          * if any match is found,
203                                          * even if errors were detected" */
204                                         exit(0);
205                                 }
206                                 /* if we're just printing filenames, we stop after the first match */
207                                 if (PRINT_FILES_WITH_MATCHES) {
208                                         puts(cur_file);
209                                         /* fall thru to "return 1" */
210                                 }
211                                 /* OPT_L aka PRINT_FILES_WITHOUT_MATCHES: return early */
212                                 return 1; /* one match */
213                         }
214
215                         /* print the matched line */
216                         if (PRINT_MATCH_COUNTS == 0) {
217 #if ENABLE_FEATURE_GREP_CONTEXT
218                                 int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
219
220                                 /* if we were told to print 'before' lines and there is at least
221                                  * one line in the circular buffer, print them */
222                                 if (lines_before && before_buf[prevpos] != NULL) {
223                                         int first_buf_entry_line_num = linenum - lines_before;
224
225                                         /* advance to the first entry in the circular buffer, and
226                                          * figure out the line number is of the first line in the
227                                          * buffer */
228                                         idx = curpos;
229                                         while (before_buf[idx] == NULL) {
230                                                 idx = (idx + 1) % lines_before;
231                                                 first_buf_entry_line_num++;
232                                         }
233
234                                         /* now print each line in the buffer, clearing them as we go */
235                                         while (before_buf[idx] != NULL) {
236                                                 print_line(before_buf[idx], first_buf_entry_line_num, '-');
237                                                 free(before_buf[idx]);
238                                                 before_buf[idx] = NULL;
239                                                 idx = (idx + 1) % lines_before;
240                                                 first_buf_entry_line_num++;
241                                         }
242                                 }
243
244                                 /* make a note that we need to print 'after' lines */
245                                 print_n_lines_after = lines_after;
246 #endif
247                                 if (option_mask32 & OPT_o) {
248                                         line[regmatch.rm_eo] = '\0';
249                                         print_line(line + regmatch.rm_so, linenum, ':');
250                                 } else {
251                                         print_line(line, linenum, ':');
252                                 }
253                         }
254                 }
255 #if ENABLE_FEATURE_GREP_CONTEXT
256                 else { /* no match */
257                         /* if we need to print some context lines after the last match, do so */
258                         if (print_n_lines_after /* && (last_line_printed != linenum) */ ) {
259                                 print_line(line, linenum, '-');
260                                 print_n_lines_after--;
261                         } else if (lines_before) {
262                                 /* Add the line to the circular 'before' buffer */
263                                 free(before_buf[curpos]);
264                                 before_buf[curpos] = line;
265                                 curpos = (curpos + 1) % lines_before;
266                                 /* avoid free(line) - we took line */
267                                 continue;
268                         }
269                 }
270
271 #endif /* ENABLE_FEATURE_GREP_CONTEXT */
272                 free(line);
273         }
274
275         /* special-case file post-processing for options where we don't print line
276          * matches, just filenames and possibly match counts */
277
278         /* grep -c: print [filename:]count, even if count is zero */
279         if (PRINT_MATCH_COUNTS) {
280                 if (print_filename)
281                         printf("%s:", cur_file);
282                 printf("%d\n", nmatches);
283         }
284
285         /* grep -L: print just the filename */
286         if (PRINT_FILES_WITHOUT_MATCHES) {
287                 /* nmatches is zero, no need to check it:
288                  * we return 1 early if we detected a match
289                  * and PRINT_FILES_WITHOUT_MATCHES is set */
290                 puts(cur_file);
291         }
292
293         return nmatches;
294 }
295
296 #if ENABLE_FEATURE_CLEAN_UP
297 #define new_grep_list_data(p, m) add_grep_list_data(p, m)
298 static char * add_grep_list_data(char *pattern, int flg_used_mem)
299 #else
300 #define new_grep_list_data(p, m) add_grep_list_data(p)
301 static char * add_grep_list_data(char *pattern)
302 #endif
303 {
304         grep_list_data_t *gl = xmalloc(sizeof(grep_list_data_t));
305         gl->pattern = pattern;
306 #if ENABLE_FEATURE_CLEAN_UP
307         gl->flg_mem_alocated_compiled = flg_used_mem;
308 #else
309         gl->flg_mem_alocated_compiled = 0;
310 #endif
311         return (char *)gl;
312 }
313
314
315 static void load_regexes_from_file(llist_t *fopt)
316 {
317         char *line;
318         FILE *f;
319
320         while (fopt) {
321                 llist_t *cur = fopt;
322                 char *ffile = cur->data;
323
324                 fopt = cur->link;
325                 free(cur);
326                 f = xfopen(ffile, "r");
327                 while ((line = xmalloc_getline(f)) != NULL) {
328                         llist_add_to(&pattern_head,
329                                 new_grep_list_data(line, PATTERN_MEM_A));
330                 }
331         }
332 }
333
334 static int file_action_grep(const char *filename, struct stat *statbuf, void* matched, int depth)
335 {
336         FILE *file = fopen(filename, "r");
337         if (file == NULL) {
338                 if (!SUPPRESS_ERR_MSGS)
339                         bb_perror_msg("%s", cur_file);
340                 open_errors = 1;
341                 return 0;
342         }
343         cur_file = filename;
344         *(int*)matched += grep_file(file);
345         fclose(file);
346         return 1;
347 }
348
349 static int grep_dir(const char *dir)
350 {
351         int matched = 0;
352         recursive_action(dir,
353                 /* recurse=yes */ ACTION_RECURSE |
354                 /* followLinks=no */
355                 /* depthFirst=yes */ ACTION_DEPTHFIRST,
356                 /* fileAction= */ file_action_grep,
357                 /* dirAction= */ NULL,
358                 /* userData= */ &matched,
359                 /* depth= */ 0);
360         return matched;
361 }
362
363 int grep_main(int argc, char **argv);
364 int grep_main(int argc, char **argv)
365 {
366         FILE *file;
367         int matched;
368         llist_t *fopt = NULL;
369
370         /* do normal option parsing */
371 #if ENABLE_FEATURE_GREP_CONTEXT
372         char *slines_after;
373         char *slines_before;
374         char *Copt;
375
376         opt_complementary = "H-h:e::f::C-AB";
377         getopt32(argc, argv,
378                 OPTSTR_GREP,
379                 &pattern_head, &fopt,
380                 &slines_after, &slines_before, &Copt);
381
382         if (option_mask32 & OPT_C) {
383                 /* -C unsets prev -A and -B, but following -A or -B
384                    may override it */
385                 if (!(option_mask32 & OPT_A)) /* not overridden */
386                         slines_after = Copt;
387                 if (!(option_mask32 & OPT_B)) /* not overridden */
388                         slines_before = Copt;
389                 option_mask32 |= OPT_A|OPT_B; /* for parser */
390         }
391         if (option_mask32 & OPT_A) {
392                 lines_after = xatoi_u(slines_after);
393         }
394         if (option_mask32 & OPT_B) {
395                 lines_before = xatoi_u(slines_before);
396         }
397         /* sanity checks */
398         if (option_mask32 & (OPT_c|OPT_q|OPT_l|OPT_L)) {
399                 option_mask32 &= ~OPT_n;
400                 lines_before = 0;
401                 lines_after = 0;
402         } else if (lines_before > 0)
403                 before_buf = xzalloc(lines_before * sizeof(char *));
404 #else
405         /* with auto sanity checks */
406         opt_complementary = "H-h:e::f::c-n:q-n:l-n";
407         getopt32(argc, argv, OPTSTR_GREP,
408                 &pattern_head, &fopt);
409 #endif
410         invert_search = ((option_mask32 & OPT_v) != 0); /* 0 | 1 */
411
412         if (pattern_head != NULL) {
413                 /* convert char **argv to grep_list_data_t */
414                 llist_t *cur;
415
416                 for (cur = pattern_head; cur; cur = cur->link)
417                         cur->data = new_grep_list_data(cur->data, 0);
418         }
419         if (option_mask32 & OPT_f)
420                 load_regexes_from_file(fopt);
421
422         if (ENABLE_FEATURE_GREP_FGREP_ALIAS && applet_name[0] == 'f')
423                 option_mask32 |= OPT_F;
424
425         if (!(option_mask32 & (OPT_o | OPT_w)))
426                 reflags = REG_NOSUB;
427
428         if (ENABLE_FEATURE_GREP_EGREP_ALIAS &&
429                         (applet_name[0] == 'e' || (option_mask32 & OPT_E)))
430                 reflags |= REG_EXTENDED;
431
432         if (option_mask32 & OPT_i)
433                 reflags |= REG_ICASE;
434
435         argv += optind;
436         argc -= optind;
437
438         /* if we didn't get a pattern from a -e and no command file was specified,
439          * argv[optind] should be the pattern. no pattern, no worky */
440         if (pattern_head == NULL) {
441                 char *pattern;
442                 if (*argv == NULL)
443                         bb_show_usage();
444                 pattern = new_grep_list_data(*argv++, 0);
445                 llist_add_to(&pattern_head, pattern);
446                 argc--;
447         }
448
449         /* argv[(optind)..(argc-1)] should be names of file to grep through. If
450          * there is more than one file to grep, we will print the filenames. */
451         if (argc > 1)
452                 print_filename = 1;
453         /* -H / -h of course override */
454         if (option_mask32 & OPT_H)
455                 print_filename = 1;
456         if (option_mask32 & OPT_h)
457                 print_filename = 0;
458
459         /* If no files were specified, or '-' was specified, take input from
460          * stdin. Otherwise, we grep through all the files specified. */
461         if (argc == 0)
462                 argc++;
463         matched = 0;
464         while (argc--) {
465                 cur_file = *argv++;
466                 file = stdin;
467                 if (!cur_file || (*cur_file == '-' && !cur_file[1])) {
468                         cur_file = "(standard input)";
469                 } else {
470                         if (option_mask32 & OPT_r) {
471                                 struct stat st;
472                                 if (stat(cur_file, &st) == 0 && S_ISDIR(st.st_mode)) {
473                                         if (!(option_mask32 & OPT_h))
474                                                 print_filename = 1;
475                                         matched += grep_dir(cur_file);
476                                         goto grep_done;
477                                 }
478                         }
479                         /* else: fopen(dir) will succeed, but reading won't */
480                         file = fopen(cur_file, "r");
481                         if (file == NULL) {
482                                 if (!SUPPRESS_ERR_MSGS)
483                                         bb_perror_msg("%s", cur_file);
484                                 open_errors = 1;
485                                 continue;
486                         }
487                 }
488                 matched += grep_file(file);
489                 fclose_if_not_stdin(file);
490  grep_done:
491                 if (matched < 0) {
492                         /* we found a match but were told to be quiet, stop here and
493                         * return success */
494                         break;
495                 }
496         }
497
498         /* destroy all the elments in the pattern list */
499         if (ENABLE_FEATURE_CLEAN_UP) {
500                 while (pattern_head) {
501                         llist_t *pattern_head_ptr = pattern_head;
502                         grep_list_data_t *gl =
503                                 (grep_list_data_t *)pattern_head_ptr->data;
504
505                         pattern_head = pattern_head->link;
506                         if ((gl->flg_mem_alocated_compiled & PATTERN_MEM_A))
507                                 free(gl->pattern);
508                         if ((gl->flg_mem_alocated_compiled & COMPILED))
509                                 regfree(&(gl->preg));
510                         free(gl);
511                         free(pattern_head_ptr);
512                 }
513         }
514         /* 0 = success, 1 = failed, 2 = error */
515         /* If the -q option is specified, the exit status shall be zero
516          * if an input line is selected, even if an error was detected.  */
517         if (BE_QUIET && matched)
518                 return 0;
519         if (open_errors)
520                 return 2;
521         return !matched; /* invert return value 0 = success, 1 = failed */
522 }