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