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