blob: 702a8a5315f9006832eed2da713f831f703c1901 [file] [log] [blame]
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/*
Mark Whitleyd3721892000-06-28 22:00:26 +00003 * Mini grep implementation for busybox using libc regex.
Eric Andersenc4996011999-10-20 22:08:37 +00004 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley
Eric Andersenc7bda1c2004-03-15 08:29:22 +00006 * Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org>
Eric Andersencc8ed391999-10-05 16:24:54 +00007 *
Bernhard Reutner-Fischer7fee0c42006-09-13 16:39:19 +00008 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersencc8ed391999-10-05 16:24:54 +00009 */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000010/* 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 */
Eric Andersen8876fb22003-06-20 09:01:58 +000013/*
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +000014 * 2004,2006 (C) Vladimir Oleynik <dzo@simtreas.ru> -
Eric Andersen7f164cd2004-05-26 09:46:41 +000015 * correction "-e pattern1 -e pattern2" logic and more optimizations.
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +000016 * precompiled regex
Denis Vlasenko51937532006-09-29 20:58:53 +000017 */
18/*
19 * (C) 2006 Jac Goudsmit added -o option
20 */
Eric Andersencc8ed391999-10-05 16:24:54 +000021
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000022#include "busybox.h"
"Vladimir N. Oleynik"23f62fc2005-09-14 16:59:11 +000023#include "xregex.h"
Mark Whitleyd3721892000-06-28 22:00:26 +000024
Mark Whitleyd3721892000-06-28 22:00:26 +000025/* options */
Denis Vlasenko385304d2007-02-25 02:38:20 +000026#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
32enum {
33 OPTBIT_l,
34 OPTBIT_n,
35 OPTBIT_q,
36 OPTBIT_v,
37 OPTBIT_s,
38 OPTBIT_c,
39 OPTBIT_F,
40 OPTBIT_i,
41 OPTBIT_H,
42 OPTBIT_h,
43 OPTBIT_e,
44 OPTBIT_f,
45 OPTBIT_L,
46 OPTBIT_o,
47 OPTBIT_r,
48 USE_FEATURE_GREP_CONTEXT(OPTBIT_A ,)
49 USE_FEATURE_GREP_CONTEXT(OPTBIT_B ,)
50 USE_FEATURE_GREP_CONTEXT(OPTBIT_C ,)
51 USE_FEATURE_GREP_CONTEXT(OPTBIT_E ,)
52 USE_DESKTOP( OPTBIT_w ,)
53 OPT_l = 1 << OPTBIT_l,
54 OPT_n = 1 << OPTBIT_n,
55 OPT_q = 1 << OPTBIT_q,
56 OPT_v = 1 << OPTBIT_v,
57 OPT_s = 1 << OPTBIT_s,
58 OPT_c = 1 << OPTBIT_c,
59 OPT_F = 1 << OPTBIT_F,
60 OPT_i = 1 << OPTBIT_i,
61 OPT_H = 1 << OPTBIT_H,
62 OPT_h = 1 << OPTBIT_h,
63 OPT_e = 1 << OPTBIT_e,
64 OPT_f = 1 << OPTBIT_f,
65 OPT_L = 1 << OPTBIT_L,
66 OPT_o = 1 << OPTBIT_o,
67 OPT_r = 1 << OPTBIT_r,
68 OPT_A = USE_FEATURE_GREP_CONTEXT((1 << OPTBIT_A)) + 0,
69 OPT_B = USE_FEATURE_GREP_CONTEXT((1 << OPTBIT_B)) + 0,
70 OPT_C = USE_FEATURE_GREP_CONTEXT((1 << OPTBIT_C)) + 0,
71 OPT_E = USE_FEATURE_GREP_CONTEXT((1 << OPTBIT_E)) + 0,
72 OPT_w = USE_DESKTOP( (1 << OPTBIT_w)) + 0,
73};
74
75#define PRINT_FILES_WITH_MATCHES (option_mask32 & OPT_l)
76#define PRINT_LINE_NUM (option_mask32 & OPT_n)
77#define BE_QUIET (option_mask32 & OPT_q)
78#define SUPPRESS_ERR_MSGS (option_mask32 & OPT_s)
79#define PRINT_MATCH_COUNTS (option_mask32 & OPT_c)
80#define FGREP_FLAG (option_mask32 & OPT_F)
81#define PRINT_FILES_WITHOUT_MATCHES (option_mask32 & OPT_L)
Eric Andersen8876fb22003-06-20 09:01:58 +000082
Denis Vlasenko3a6755f2006-10-14 14:24:30 +000083typedef unsigned char byte_t;
84
Eric Andersen8876fb22003-06-20 09:01:58 +000085static int reflags;
Denis Vlasenko3a6755f2006-10-14 14:24:30 +000086static byte_t invert_search;
87static byte_t print_filename;
88static byte_t open_errors;
Mark Whitleyd3721892000-06-28 22:00:26 +000089
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000090#if ENABLE_FEATURE_GREP_CONTEXT
Eric Andersen8876fb22003-06-20 09:01:58 +000091static int lines_before;
92static int lines_after;
93static char **before_buf;
94static int last_line_printed;
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000095#endif /* ENABLE_FEATURE_GREP_CONTEXT */
Mark Whitley2fd52982001-02-09 00:41:10 +000096
97/* globals used internally */
Eric Andersen8876fb22003-06-20 09:01:58 +000098static llist_t *pattern_head; /* growable list of patterns to match */
Denis Vlasenko3a6755f2006-10-14 14:24:30 +000099static const char *cur_file; /* the current file we are reading */
Mark Whitleyd3721892000-06-28 22:00:26 +0000100
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000101typedef struct grep_list_data_t {
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000102 char *pattern;
103 regex_t preg;
104#define PATTERN_MEM_A 1
105#define COMPILED 2
106 int flg_mem_alocated_compiled;
107} grep_list_data_t;
Mark Whitleyfa43e542001-05-24 18:36:18 +0000108
Mark Whitley2fd52982001-02-09 00:41:10 +0000109static void print_line(const char *line, int linenum, char decoration)
110{
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000111#if ENABLE_FEATURE_GREP_CONTEXT
Eric Andersenaff114c2004-04-14 17:51:38 +0000112 /* possibly print the little '--' separator */
Matt Kraaiedc80652001-05-22 14:29:27 +0000113 if ((lines_before || lines_after) && last_line_printed &&
114 last_line_printed < linenum - 1) {
Mark Whitley2fd52982001-02-09 00:41:10 +0000115 puts("--");
116 }
117 last_line_printed = linenum;
118#endif
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000119 if (print_filename)
Mark Whitley2fd52982001-02-09 00:41:10 +0000120 printf("%s%c", cur_file, decoration);
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000121 if (PRINT_LINE_NUM)
Mark Whitley2fd52982001-02-09 00:41:10 +0000122 printf("%i%c", linenum, decoration);
Denis Vlasenko51937532006-09-29 20:58:53 +0000123 /* Emulate weird GNU grep behavior with -ov */
Denis Vlasenko385304d2007-02-25 02:38:20 +0000124 if ((option_mask32 & (OPT_v+OPT_o)) != (OPT_v+OPT_o))
Denis Vlasenko51937532006-09-29 20:58:53 +0000125 puts(line);
Mark Whitley2fd52982001-02-09 00:41:10 +0000126}
Mark Whitleyd3721892000-06-28 22:00:26 +0000127
Eric Andersen8876fb22003-06-20 09:01:58 +0000128static int grep_file(FILE *file)
Mark Whitleyd3721892000-06-28 22:00:26 +0000129{
Eric Andersen8876fb22003-06-20 09:01:58 +0000130 char *line;
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000131 byte_t ret;
Mark Whitleyd3721892000-06-28 22:00:26 +0000132 int linenum = 0;
Matt Kraaideb95f62000-08-06 15:25:53 +0000133 int nmatches = 0;
Denis Vlasenko51937532006-09-29 20:58:53 +0000134 regmatch_t regmatch;
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000135#if ENABLE_FEATURE_GREP_CONTEXT
Mark Whitley2fd52982001-02-09 00:41:10 +0000136 int print_n_lines_after = 0;
137 int curpos = 0; /* track where we are in the circular 'before' buffer */
138 int idx = 0; /* used for iteration through the circular buffer */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000139#endif /* ENABLE_FEATURE_GREP_CONTEXT */
Mark Whitleyd3721892000-06-28 22:00:26 +0000140
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000141 while ((line = xmalloc_getline(file)) != NULL) {
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000142 llist_t *pattern_ptr = pattern_head;
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000143 grep_list_data_t * gl;
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000144
Mark Whitleyd3721892000-06-28 22:00:26 +0000145 linenum++;
Eric Andersen8876fb22003-06-20 09:01:58 +0000146 ret = 0;
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000147 while (pattern_ptr) {
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000148 gl = (grep_list_data_t *)pattern_ptr->data;
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000149 if (FGREP_FLAG) {
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000150 ret = strstr(line, gl->pattern) != NULL;
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000151 } else {
152 /*
153 * test for a postitive-assertion match (regexec returns success (0)
154 * and the user did not specify invert search), or a negative-assertion
155 * match (regexec returns failure (REG_NOMATCH) and the user specified
156 * invert search)
157 */
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000158 if (!(gl->flg_mem_alocated_compiled & COMPILED)) {
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000159 gl->flg_mem_alocated_compiled |= COMPILED;
160 xregcomp(&(gl->preg), gl->pattern, reflags);
161 }
Denis Vlasenko51937532006-09-29 20:58:53 +0000162 regmatch.rm_so = 0;
163 regmatch.rm_eo = 0;
Denis Vlasenko385304d2007-02-25 02:38:20 +0000164 if (regexec(&(gl->preg), line, 1, &regmatch, 0) == 0) {
165 if (!(option_mask32 & OPT_w))
166 ret = 1;
167 else {
168 char c = ' ';
169 if (regmatch.rm_so)
170 c = line[regmatch.rm_so - 1];
171 if (!isalnum(c) && c != '_') {
172 c = line[regmatch.rm_eo];
173 if (!c || (!isalnum(c) && c != '_'))
174 ret = 1;
175 }
176 }
177 }
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000178 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000179 pattern_ptr = pattern_ptr->link;
180 } /* while (pattern_ptr) */
Mark Whitleyd3721892000-06-28 22:00:26 +0000181
Denis Vlasenko51937532006-09-29 20:58:53 +0000182 if (ret ^ invert_search) {
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000183 if (PRINT_FILES_WITH_MATCHES || BE_QUIET)
Eric Andersen8876fb22003-06-20 09:01:58 +0000184 free(line);
185
186 /* if we found a match but were told to be quiet, stop here */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000187 if (BE_QUIET || PRINT_FILES_WITHOUT_MATCHES)
Eric Andersen8876fb22003-06-20 09:01:58 +0000188 return -1;
Mark Whitleyd3721892000-06-28 22:00:26 +0000189
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000190 /* keep track of matches */
191 nmatches++;
Mark Whitley1d9d4112001-05-21 21:13:00 +0000192
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000193 /* if we're just printing filenames, we stop after the first match */
194 if (PRINT_FILES_WITH_MATCHES)
195 break;
Mark Whitley1d9d4112001-05-21 21:13:00 +0000196
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000197 /* print the matched line */
198 if (PRINT_MATCH_COUNTS == 0) {
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000199#if ENABLE_FEATURE_GREP_CONTEXT
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000200 int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
Mark Whitley2fd52982001-02-09 00:41:10 +0000201
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000202 /* if we were told to print 'before' lines and there is at least
203 * one line in the circular buffer, print them */
204 if (lines_before && before_buf[prevpos] != NULL) {
205 int first_buf_entry_line_num = linenum - lines_before;
Mark Whitley2fd52982001-02-09 00:41:10 +0000206
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000207 /* advance to the first entry in the circular buffer, and
208 * figure out the line number is of the first line in the
209 * buffer */
210 idx = curpos;
211 while (before_buf[idx] == NULL) {
212 idx = (idx + 1) % lines_before;
213 first_buf_entry_line_num++;
Mark Whitley2fd52982001-02-09 00:41:10 +0000214 }
215
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000216 /* now print each line in the buffer, clearing them as we go */
217 while (before_buf[idx] != NULL) {
218 print_line(before_buf[idx], first_buf_entry_line_num, '-');
219 free(before_buf[idx]);
220 before_buf[idx] = NULL;
221 idx = (idx + 1) % lines_before;
222 first_buf_entry_line_num++;
223 }
224 }
225
226 /* make a note that we need to print 'after' lines */
227 print_n_lines_after = lines_after;
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000228#endif
Denis Vlasenko385304d2007-02-25 02:38:20 +0000229 if (option_mask32 & OPT_o) {
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000230 line[regmatch.rm_eo] = '\0';
231 print_line(line + regmatch.rm_so, linenum, ':');
232 } else {
233 print_line(line, linenum, ':');
Mark Whitley2fd52982001-02-09 00:41:10 +0000234 }
Matt Kraai0810f722001-01-04 15:11:52 +0000235 }
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000236 }
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000237#if ENABLE_FEATURE_GREP_CONTEXT
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000238 else { /* no match */
239 /* Add the line to the circular 'before' buffer */
240 if (lines_before) {
241 free(before_buf[curpos]);
242 before_buf[curpos] = xstrdup(line);
243 curpos = (curpos + 1) % lines_before;
Mark Whitley2fd52982001-02-09 00:41:10 +0000244 }
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000245 }
Mark Whitley2fd52982001-02-09 00:41:10 +0000246
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000247 /* if we need to print some context lines after the last match, do so */
248 if (print_n_lines_after && (last_line_printed != linenum)) {
249 print_line(line, linenum, '-');
250 print_n_lines_after--;
251 }
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000252#endif /* ENABLE_FEATURE_GREP_CONTEXT */
Mark Whitleyd3721892000-06-28 22:00:26 +0000253 free(line);
254 }
Mark Whitley8f122432000-07-18 18:37:01 +0000255
Mark Whitley35e59be2001-05-14 19:40:32 +0000256 /* special-case file post-processing for options where we don't print line
Mark Whitley1d9d4112001-05-21 21:13:00 +0000257 * matches, just filenames and possibly match counts */
Mark Whitley35e59be2001-05-14 19:40:32 +0000258
Mark Whitley1d9d4112001-05-21 21:13:00 +0000259 /* grep -c: print [filename:]count, even if count is zero */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000260 if (PRINT_MATCH_COUNTS) {
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000261 if (print_filename)
Mark Whitley1d9d4112001-05-21 21:13:00 +0000262 printf("%s:", cur_file);
Denis Vlasenko92758142006-10-03 19:56:34 +0000263 printf("%d\n", nmatches);
Mark Whitley35e59be2001-05-14 19:40:32 +0000264 }
Mark Whitley1d9d4112001-05-21 21:13:00 +0000265
266 /* grep -l: print just the filename, but only if we grepped the line in the file */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000267 if (PRINT_FILES_WITH_MATCHES && nmatches > 0) {
Matt Kraai59df6f72001-05-16 14:21:09 +0000268 puts(cur_file);
Mark Whitley35e59be2001-05-14 19:40:32 +0000269 }
270
Eric Andersendec7f812004-05-26 11:47:55 +0000271 /* grep -L: print just the filename, but only if we didn't grep the line in the file */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000272 if (PRINT_FILES_WITHOUT_MATCHES && nmatches == 0) {
Eric Andersendec7f812004-05-26 11:47:55 +0000273 puts(cur_file);
274 }
275
Eric Andersen8876fb22003-06-20 09:01:58 +0000276 return nmatches;
Mark Whitleyd3721892000-06-28 22:00:26 +0000277}
Eric Andersencc8ed391999-10-05 16:24:54 +0000278
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000279#if ENABLE_FEATURE_CLEAN_UP
280#define new_grep_list_data(p, m) add_grep_list_data(p, m)
281static char * add_grep_list_data(char *pattern, int flg_used_mem)
282#else
283#define new_grep_list_data(p, m) add_grep_list_data(p)
284static char * add_grep_list_data(char *pattern)
285#endif
286{
287 grep_list_data_t *gl = xmalloc(sizeof(grep_list_data_t));
288 gl->pattern = pattern;
289#if ENABLE_FEATURE_CLEAN_UP
290 gl->flg_mem_alocated_compiled = flg_used_mem;
291#else
292 gl->flg_mem_alocated_compiled = 0;
293#endif
294 return (char *)gl;
295}
296
297
Eric Andersen8876fb22003-06-20 09:01:58 +0000298static void load_regexes_from_file(llist_t *fopt)
Mark Whitleyfa43e542001-05-24 18:36:18 +0000299{
300 char *line;
Eric Andersen8876fb22003-06-20 09:01:58 +0000301 FILE *f;
302
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000303 while (fopt) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000304 llist_t *cur = fopt;
305 char *ffile = cur->data;
306
307 fopt = cur->link;
308 free(cur);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000309 f = xfopen(ffile, "r");
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000310 while ((line = xmalloc_getline(f)) != NULL) {
Rob Landley8bb50782006-05-26 23:44:51 +0000311 llist_add_to(&pattern_head,
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000312 new_grep_list_data(line, PATTERN_MEM_A));
Eric Andersen31c27a92004-10-08 08:10:57 +0000313 }
Mark Whitleyfa43e542001-05-24 18:36:18 +0000314 }
315}
Mark Whitleyfa43e542001-05-24 18:36:18 +0000316
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000317static int file_action_grep(const char *filename, struct stat *statbuf, void* matched, int depth)
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000318{
319 FILE *file = fopen(filename, "r");
320 if (file == NULL) {
321 if (!SUPPRESS_ERR_MSGS)
322 bb_perror_msg("%s", cur_file);
323 open_errors = 1;
324 return 0;
325 }
326 cur_file = filename;
327 *(int*)matched += grep_file(file);
Denis Vlasenkobf392162006-10-15 18:38:01 +0000328 fclose(file);
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000329 return 1;
330}
331
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000332static int grep_dir(const char *dir)
333{
334 int matched = 0;
335 recursive_action(dir,
336 /* recurse= */ 1,
337 /* followLinks= */ 0,
338 /* depthFirst= */ 1,
339 /* fileAction= */ file_action_grep,
340 /* dirAction= */ NULL,
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000341 /* userData= */ &matched,
342 /* depth= */ 0);
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000343 return matched;
344}
345
Denis Vlasenko06af2162007-02-03 17:28:39 +0000346int grep_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +0000347int grep_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000348{
Eric Andersen8876fb22003-06-20 09:01:58 +0000349 FILE *file;
350 int matched;
Eric Andersen31c27a92004-10-08 08:10:57 +0000351 llist_t *fopt = NULL;
Matt Kraai999623e2001-10-29 15:49:03 +0000352
Mark Whitleyd3721892000-06-28 22:00:26 +0000353 /* do normal option parsing */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000354#if ENABLE_FEATURE_GREP_CONTEXT
Eric Andersen8876fb22003-06-20 09:01:58 +0000355 char *slines_after;
356 char *slines_before;
357 char *Copt;
358
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000359 opt_complementary = "H-h:e::f::C-AB";
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000360 getopt32(argc, argv,
Denis Vlasenko385304d2007-02-25 02:38:20 +0000361 OPTSTR_GREP,
Eric Andersen8876fb22003-06-20 09:01:58 +0000362 &pattern_head, &fopt,
363 &slines_after, &slines_before, &Copt);
364
Denis Vlasenko385304d2007-02-25 02:38:20 +0000365 if (option_mask32 & OPT_C) {
Denis Vlasenko2295b492006-10-22 11:42:51 +0000366 /* -C unsets prev -A and -B, but following -A or -B
367 may override it */
Denis Vlasenko385304d2007-02-25 02:38:20 +0000368 if (!(option_mask32 & OPT_A)) /* not overridden */
Eric Andersen8876fb22003-06-20 09:01:58 +0000369 slines_after = Copt;
Denis Vlasenko385304d2007-02-25 02:38:20 +0000370 if (!(option_mask32 & OPT_B)) /* not overridden */
Eric Andersen8876fb22003-06-20 09:01:58 +0000371 slines_before = Copt;
Denis Vlasenko385304d2007-02-25 02:38:20 +0000372 option_mask32 |= OPT_A|OPT_B; /* for parser */
Eric Andersen053b1462000-06-13 06:24:53 +0000373 }
Denis Vlasenko385304d2007-02-25 02:38:20 +0000374 if (option_mask32 & OPT_A) {
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000375 lines_after = xatoi_u(slines_after);
Eric Andersen8876fb22003-06-20 09:01:58 +0000376 }
Denis Vlasenko385304d2007-02-25 02:38:20 +0000377 if (option_mask32 & OPT_B) {
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000378 lines_before = xatoi_u(slines_before);
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000379 }
Denis Vlasenko2295b492006-10-22 11:42:51 +0000380 /* sanity checks */
Denis Vlasenko385304d2007-02-25 02:38:20 +0000381 if (option_mask32 & (OPT_c|OPT_q|OPT_l|OPT_L)) {
382 option_mask32 &= ~OPT_n;
Eric Andersen8876fb22003-06-20 09:01:58 +0000383 lines_before = 0;
384 lines_after = 0;
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000385 } else if (lines_before > 0)
Denis Vlasenko4cccc032006-12-22 18:37:07 +0000386 before_buf = xzalloc(lines_before * sizeof(char *));
Eric Andersen8876fb22003-06-20 09:01:58 +0000387#else
388 /* with auto sanity checks */
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000389 opt_complementary = "H-h:e::f::c-n:q-n:l-n";
Denis Vlasenko385304d2007-02-25 02:38:20 +0000390 getopt32(argc, argv, OPTSTR_GREP,
Eric Andersen8876fb22003-06-20 09:01:58 +0000391 &pattern_head, &fopt);
Eric Andersen8876fb22003-06-20 09:01:58 +0000392#endif
Denis Vlasenko385304d2007-02-25 02:38:20 +0000393 invert_search = ((option_mask32 & OPT_v) != 0); /* 0 | 1 */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000394
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000395 if (pattern_head != NULL) {
396 /* convert char *argv[] to grep_list_data_t */
397 llist_t *cur;
398
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000399 for (cur = pattern_head; cur; cur = cur->link)
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000400 cur->data = new_grep_list_data(cur->data, 0);
401 }
Denis Vlasenko385304d2007-02-25 02:38:20 +0000402 if (option_mask32 & OPT_f)
Eric Andersen8876fb22003-06-20 09:01:58 +0000403 load_regexes_from_file(fopt);
404
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000405 if (ENABLE_FEATURE_GREP_FGREP_ALIAS && applet_name[0] == 'f')
Denis Vlasenko385304d2007-02-25 02:38:20 +0000406 option_mask32 |= OPT_F;
Mike Frysinger15ca5862005-07-31 22:41:05 +0000407
Denis Vlasenko385304d2007-02-25 02:38:20 +0000408 if (!(option_mask32 & (OPT_o | OPT_w)))
Denis Vlasenko51937532006-09-29 20:58:53 +0000409 reflags = REG_NOSUB;
410
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000411 if (ENABLE_FEATURE_GREP_EGREP_ALIAS &&
Denis Vlasenko385304d2007-02-25 02:38:20 +0000412 (applet_name[0] == 'e' || (option_mask32 & OPT_E)))
Denis Vlasenko51937532006-09-29 20:58:53 +0000413 reflags |= REG_EXTENDED;
Eric Andersen8876fb22003-06-20 09:01:58 +0000414
Denis Vlasenko385304d2007-02-25 02:38:20 +0000415 if (option_mask32 & OPT_i)
Eric Andersen8876fb22003-06-20 09:01:58 +0000416 reflags |= REG_ICASE;
417
418 argv += optind;
419 argc -= optind;
Eric Andersen053b1462000-06-13 06:24:53 +0000420
Mark Whitleyfa43e542001-05-24 18:36:18 +0000421 /* if we didn't get a pattern from a -e and no command file was specified,
422 * argv[optind] should be the pattern. no pattern, no worky */
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000423 if (pattern_head == NULL) {
Denis Vlasenko385304d2007-02-25 02:38:20 +0000424 char *pattern;
Eric Andersen8876fb22003-06-20 09:01:58 +0000425 if (*argv == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000426 bb_show_usage();
Denis Vlasenko385304d2007-02-25 02:38:20 +0000427 pattern = new_grep_list_data(*argv++, 0);
428 llist_add_to(&pattern_head, pattern);
429 argc--;
Mark Whitleyfa43e542001-05-24 18:36:18 +0000430 }
Mark Whitleyd3721892000-06-28 22:00:26 +0000431
Mark Whitleyfa43e542001-05-24 18:36:18 +0000432 /* argv[(optind)..(argc-1)] should be names of file to grep through. If
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000433 * there is more than one file to grep, we will print the filenames. */
Denis Vlasenko2295b492006-10-22 11:42:51 +0000434 if (argc > 1)
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000435 print_filename = 1;
Denis Vlasenko2295b492006-10-22 11:42:51 +0000436 /* -H / -h of course override */
Denis Vlasenko385304d2007-02-25 02:38:20 +0000437 if (option_mask32 & OPT_H)
Denis Vlasenko2295b492006-10-22 11:42:51 +0000438 print_filename = 1;
Denis Vlasenko385304d2007-02-25 02:38:20 +0000439 if (option_mask32 & OPT_h)
Denis Vlasenko2295b492006-10-22 11:42:51 +0000440 print_filename = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000441
Mark Whitley2e1148b2000-06-28 22:59:30 +0000442 /* If no files were specified, or '-' was specified, take input from
443 * stdin. Otherwise, we grep through all the files specified. */
Denis Vlasenko2295b492006-10-22 11:42:51 +0000444 if (argc == 0)
Eric Andersen8876fb22003-06-20 09:01:58 +0000445 argc++;
Eric Andersen8876fb22003-06-20 09:01:58 +0000446 matched = 0;
447 while (argc--) {
448 cur_file = *argv++;
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000449 file = stdin;
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000450 if (!cur_file || (*cur_file == '-' && !cur_file[1])) {
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000451 cur_file = "(standard input)";
Eric Andersen8876fb22003-06-20 09:01:58 +0000452 } else {
Denis Vlasenko385304d2007-02-25 02:38:20 +0000453 if (option_mask32 & OPT_r) {
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000454 struct stat st;
455 if (stat(cur_file, &st) == 0 && S_ISDIR(st.st_mode)) {
Denis Vlasenko385304d2007-02-25 02:38:20 +0000456 if (!(option_mask32 & OPT_h))
Denis Vlasenko3544ae62006-10-14 14:51:59 +0000457 print_filename = 1;
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000458 matched += grep_dir(cur_file);
459 goto grep_done;
460 }
Mark Whitley2ef880b2000-07-18 21:02:06 +0000461 }
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000462 /* else: fopen(dir) will succeed, but reading won't */
463 file = fopen(cur_file, "r");
464 if (file == NULL) {
465 if (!SUPPRESS_ERR_MSGS)
466 bb_perror_msg("%s", cur_file);
467 open_errors = 1;
468 continue;
469 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000470 }
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000471 matched += grep_file(file);
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000472 fclose_if_not_stdin(file);
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000473 grep_done:
474 if (matched < 0) {
475 /* we found a match but were told to be quiet, stop here and
476 * return success */
477 break;
478 }
"Vladimir N. Oleynik"cf40d812005-09-23 13:23:15 +0000479 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000480
Eric Andersen8876fb22003-06-20 09:01:58 +0000481 /* destroy all the elments in the pattern list */
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000482 if (ENABLE_FEATURE_CLEAN_UP) {
483 while (pattern_head) {
484 llist_t *pattern_head_ptr = pattern_head;
485 grep_list_data_t *gl =
486 (grep_list_data_t *)pattern_head_ptr->data;
Eric Andersen8876fb22003-06-20 09:01:58 +0000487
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000488 pattern_head = pattern_head->link;
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000489 if ((gl->flg_mem_alocated_compiled & PATTERN_MEM_A))
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000490 free(gl->pattern);
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000491 if ((gl->flg_mem_alocated_compiled & COMPILED))
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000492 regfree(&(gl->preg));
493 free(pattern_head_ptr);
494 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000495 }
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000496 /* 0 = success, 1 = failed, 2 = error */
497 /* If the -q option is specified, the exit status shall be zero
498 * if an input line is selected, even if an error was detected. */
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000499 if (BE_QUIET && matched)
"Vladimir N. Oleynik"bf449742005-09-23 13:50:24 +0000500 return 0;
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000501 if (open_errors)
"Vladimir N. Oleynik"bf449742005-09-23 13:50:24 +0000502 return 2;
Mark Whitleyb5c29852001-02-01 21:02:41 +0000503 return !matched; /* invert return value 0 = success, 1 = failed */
Eric Andersencc8ed391999-10-05 16:24:54 +0000504}