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