blob: c1cb7dd0cc88d34eb49efd87af2df9e38d29b28f [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
Eric Andersened3ef502001-01-27 08:24:39 +000025
Mark Whitleyd3721892000-06-28 22:00:26 +000026/* options */
Denis Vlasenko3a6755f2006-10-14 14:24:30 +000027#define GREP_OPTS "lnqvscFiHhe:f:Lor"
Eric Andersenabc513a2004-05-26 11:48:29 +000028#define GREP_OPT_l (1<<0)
Denis Vlasenko3a6755f2006-10-14 14:24:30 +000029#define PRINT_FILES_WITH_MATCHES (option_mask32 & GREP_OPT_l)
Eric Andersenabc513a2004-05-26 11:48:29 +000030#define GREP_OPT_n (1<<1)
Denis Vlasenko3a6755f2006-10-14 14:24:30 +000031#define PRINT_LINE_NUM (option_mask32 & GREP_OPT_n)
Eric Andersenabc513a2004-05-26 11:48:29 +000032#define GREP_OPT_q (1<<2)
Denis Vlasenko3a6755f2006-10-14 14:24:30 +000033#define BE_QUIET (option_mask32 & GREP_OPT_q)
Eric Andersenabc513a2004-05-26 11:48:29 +000034#define GREP_OPT_v (1<<3)
Eric Andersenabc513a2004-05-26 11:48:29 +000035#define GREP_OPT_s (1<<4)
Denis Vlasenko3a6755f2006-10-14 14:24:30 +000036#define SUPPRESS_ERR_MSGS (option_mask32 & GREP_OPT_s)
Eric Andersenabc513a2004-05-26 11:48:29 +000037#define GREP_OPT_c (1<<5)
Denis Vlasenko3a6755f2006-10-14 14:24:30 +000038#define PRINT_MATCH_COUNTS (option_mask32 & GREP_OPT_c)
Eric Andersenabc513a2004-05-26 11:48:29 +000039#define GREP_OPT_F (1<<6)
Denis Vlasenko3a6755f2006-10-14 14:24:30 +000040#define FGREP_FLAG (option_mask32 & GREP_OPT_F)
Eric Andersenabc513a2004-05-26 11:48:29 +000041#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)
Denis Vlasenko3a6755f2006-10-14 14:24:30 +000047#define PRINT_FILES_WITHOUT_MATCHES (option_mask32 & GREP_OPT_L)
Denis Vlasenko51937532006-09-29 20:58:53 +000048#define GREP_OPT_o (1<<13)
Denis Vlasenko3a6755f2006-10-14 14:24:30 +000049#define GREP_OPT_r (1<<14)
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000050#if ENABLE_FEATURE_GREP_CONTEXT
Denis Vlasenko3a6755f2006-10-14 14:24:30 +000051# 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)
Eric Andersen8876fb22003-06-20 09:01:58 +000056#else
Denis Vlasenko3a6755f2006-10-14 14:24:30 +000057# 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)
Eric Andersen8876fb22003-06-20 09:01:58 +000062#endif
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000063#if ENABLE_FEATURE_GREP_EGREP_ALIAS
Eric Andersen8876fb22003-06-20 09:01:58 +000064# define OPT_EGREP "E"
65#else
66# define OPT_EGREP ""
67#endif
68
Denis Vlasenko3a6755f2006-10-14 14:24:30 +000069typedef unsigned char byte_t;
70
Eric Andersen8876fb22003-06-20 09:01:58 +000071static int reflags;
Denis Vlasenko3a6755f2006-10-14 14:24:30 +000072static byte_t invert_search;
73static byte_t print_filename;
74static byte_t open_errors;
Mark Whitleyd3721892000-06-28 22:00:26 +000075
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000076#if ENABLE_FEATURE_GREP_CONTEXT
Eric Andersen8876fb22003-06-20 09:01:58 +000077static int lines_before;
78static int lines_after;
79static char **before_buf;
80static int last_line_printed;
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000081#endif /* ENABLE_FEATURE_GREP_CONTEXT */
Mark Whitley2fd52982001-02-09 00:41:10 +000082
83/* globals used internally */
Eric Andersen8876fb22003-06-20 09:01:58 +000084static llist_t *pattern_head; /* growable list of patterns to match */
Denis Vlasenko3a6755f2006-10-14 14:24:30 +000085static const char *cur_file; /* the current file we are reading */
Mark Whitleyd3721892000-06-28 22:00:26 +000086
Denis Vlasenko4222ae42007-02-25 02:37:49 +000087typedef struct grep_list_data_t {
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +000088 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;
Mark Whitleyfa43e542001-05-24 18:36:18 +000094
Mark Whitley2fd52982001-02-09 00:41:10 +000095static void print_line(const char *line, int linenum, char decoration)
96{
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000097#if ENABLE_FEATURE_GREP_CONTEXT
Eric Andersenaff114c2004-04-14 17:51:38 +000098 /* possibly print the little '--' separator */
Matt Kraaiedc80652001-05-22 14:29:27 +000099 if ((lines_before || lines_after) && last_line_printed &&
100 last_line_printed < linenum - 1) {
Mark Whitley2fd52982001-02-09 00:41:10 +0000101 puts("--");
102 }
103 last_line_printed = linenum;
104#endif
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000105 if (print_filename)
Mark Whitley2fd52982001-02-09 00:41:10 +0000106 printf("%s%c", cur_file, decoration);
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000107 if (PRINT_LINE_NUM)
Mark Whitley2fd52982001-02-09 00:41:10 +0000108 printf("%i%c", linenum, decoration);
Denis Vlasenko51937532006-09-29 20:58:53 +0000109 /* Emulate weird GNU grep behavior with -ov */
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000110 if ((option_mask32 & (GREP_OPT_v+GREP_OPT_o)) != (GREP_OPT_v+GREP_OPT_o))
Denis Vlasenko51937532006-09-29 20:58:53 +0000111 puts(line);
Mark Whitley2fd52982001-02-09 00:41:10 +0000112}
Mark Whitleyd3721892000-06-28 22:00:26 +0000113
Eric Andersen8876fb22003-06-20 09:01:58 +0000114
115static int grep_file(FILE *file)
Mark Whitleyd3721892000-06-28 22:00:26 +0000116{
Eric Andersen8876fb22003-06-20 09:01:58 +0000117 char *line;
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000118 byte_t ret;
Mark Whitleyd3721892000-06-28 22:00:26 +0000119 int linenum = 0;
Matt Kraaideb95f62000-08-06 15:25:53 +0000120 int nmatches = 0;
Denis Vlasenko51937532006-09-29 20:58:53 +0000121 regmatch_t regmatch;
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000122#if ENABLE_FEATURE_GREP_CONTEXT
Mark Whitley2fd52982001-02-09 00:41:10 +0000123 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 */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000126#endif /* ENABLE_FEATURE_GREP_CONTEXT */
Mark Whitleyd3721892000-06-28 22:00:26 +0000127
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000128 while ((line = xmalloc_getline(file)) != NULL) {
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000129 llist_t *pattern_ptr = pattern_head;
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000130 grep_list_data_t * gl;
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000131
Mark Whitleyd3721892000-06-28 22:00:26 +0000132 linenum++;
Eric Andersen8876fb22003-06-20 09:01:58 +0000133 ret = 0;
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000134 while (pattern_ptr) {
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000135 gl = (grep_list_data_t *)pattern_ptr->data;
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000136 if (FGREP_FLAG) {
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000137 ret = strstr(line, gl->pattern) != NULL;
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000138 } 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 */
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000145 if (!(gl->flg_mem_alocated_compiled & COMPILED)) {
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000146 gl->flg_mem_alocated_compiled |= COMPILED;
147 xregcomp(&(gl->preg), gl->pattern, reflags);
148 }
Denis Vlasenko51937532006-09-29 20:58:53 +0000149 regmatch.rm_so = 0;
150 regmatch.rm_eo = 0;
151 ret |= regexec(&(gl->preg), line, 1, &regmatch, 0) == 0;
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000152 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000153 pattern_ptr = pattern_ptr->link;
154 } /* while (pattern_ptr) */
Mark Whitleyd3721892000-06-28 22:00:26 +0000155
Denis Vlasenko51937532006-09-29 20:58:53 +0000156 if (ret ^ invert_search) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000157
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000158 if (PRINT_FILES_WITH_MATCHES || BE_QUIET)
Eric Andersen8876fb22003-06-20 09:01:58 +0000159 free(line);
160
161 /* if we found a match but were told to be quiet, stop here */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000162 if (BE_QUIET || PRINT_FILES_WITHOUT_MATCHES)
Eric Andersen8876fb22003-06-20 09:01:58 +0000163 return -1;
Mark Whitleyd3721892000-06-28 22:00:26 +0000164
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000165 /* keep track of matches */
166 nmatches++;
Mark Whitley1d9d4112001-05-21 21:13:00 +0000167
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000168 /* if we're just printing filenames, we stop after the first match */
169 if (PRINT_FILES_WITH_MATCHES)
170 break;
Mark Whitley1d9d4112001-05-21 21:13:00 +0000171
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000172 /* print the matched line */
173 if (PRINT_MATCH_COUNTS == 0) {
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000174#if ENABLE_FEATURE_GREP_CONTEXT
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000175 int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
Mark Whitley2fd52982001-02-09 00:41:10 +0000176
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000177 /* 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;
Mark Whitley2fd52982001-02-09 00:41:10 +0000181
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000182 /* 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++;
Mark Whitley2fd52982001-02-09 00:41:10 +0000189 }
190
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000191 /* 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;
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000203#endif
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000204 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, ':');
Mark Whitley2fd52982001-02-09 00:41:10 +0000209 }
Matt Kraai0810f722001-01-04 15:11:52 +0000210 }
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000211 }
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000212#if ENABLE_FEATURE_GREP_CONTEXT
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000213 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;
Mark Whitley2fd52982001-02-09 00:41:10 +0000219 }
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000220 }
Mark Whitley2fd52982001-02-09 00:41:10 +0000221
Denis Vlasenko4222ae42007-02-25 02:37:49 +0000222 /* 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 }
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000227#endif /* ENABLE_FEATURE_GREP_CONTEXT */
Mark Whitleyd3721892000-06-28 22:00:26 +0000228 free(line);
229 }
Mark Whitley8f122432000-07-18 18:37:01 +0000230
Mark Whitley35e59be2001-05-14 19:40:32 +0000231 /* special-case file post-processing for options where we don't print line
Mark Whitley1d9d4112001-05-21 21:13:00 +0000232 * matches, just filenames and possibly match counts */
Mark Whitley35e59be2001-05-14 19:40:32 +0000233
Mark Whitley1d9d4112001-05-21 21:13:00 +0000234 /* grep -c: print [filename:]count, even if count is zero */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000235 if (PRINT_MATCH_COUNTS) {
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000236 if (print_filename)
Mark Whitley1d9d4112001-05-21 21:13:00 +0000237 printf("%s:", cur_file);
Denis Vlasenko92758142006-10-03 19:56:34 +0000238 printf("%d\n", nmatches);
Mark Whitley35e59be2001-05-14 19:40:32 +0000239 }
Mark Whitley1d9d4112001-05-21 21:13:00 +0000240
241 /* grep -l: print just the filename, but only if we grepped the line in the file */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000242 if (PRINT_FILES_WITH_MATCHES && nmatches > 0) {
Matt Kraai59df6f72001-05-16 14:21:09 +0000243 puts(cur_file);
Mark Whitley35e59be2001-05-14 19:40:32 +0000244 }
245
Eric Andersendec7f812004-05-26 11:47:55 +0000246 /* 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 +0000247 if (PRINT_FILES_WITHOUT_MATCHES && nmatches == 0) {
Eric Andersendec7f812004-05-26 11:47:55 +0000248 puts(cur_file);
249 }
250
Eric Andersen8876fb22003-06-20 09:01:58 +0000251 return nmatches;
Mark Whitleyd3721892000-06-28 22:00:26 +0000252}
Eric Andersencc8ed391999-10-05 16:24:54 +0000253
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000254#if ENABLE_FEATURE_CLEAN_UP
255#define new_grep_list_data(p, m) add_grep_list_data(p, m)
256static 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)
259static 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
Eric Andersen8876fb22003-06-20 09:01:58 +0000273static void load_regexes_from_file(llist_t *fopt)
Mark Whitleyfa43e542001-05-24 18:36:18 +0000274{
275 char *line;
Eric Andersen8876fb22003-06-20 09:01:58 +0000276 FILE *f;
277
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000278 while (fopt) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000279 llist_t *cur = fopt;
280 char *ffile = cur->data;
281
282 fopt = cur->link;
283 free(cur);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000284 f = xfopen(ffile, "r");
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000285 while ((line = xmalloc_getline(f)) != NULL) {
Rob Landley8bb50782006-05-26 23:44:51 +0000286 llist_add_to(&pattern_head,
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000287 new_grep_list_data(line, PATTERN_MEM_A));
Eric Andersen31c27a92004-10-08 08:10:57 +0000288 }
Mark Whitleyfa43e542001-05-24 18:36:18 +0000289 }
290}
Mark Whitleyfa43e542001-05-24 18:36:18 +0000291
292
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000293static int file_action_grep(const char *filename, struct stat *statbuf, void* matched, int depth)
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000294{
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);
Denis Vlasenkobf392162006-10-15 18:38:01 +0000304 fclose(file);
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000305 return 1;
306}
307
308
309static 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,
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000318 /* userData= */ &matched,
319 /* depth= */ 0);
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000320 return matched;
321}
322
323
Denis Vlasenko06af2162007-02-03 17:28:39 +0000324int grep_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +0000325int grep_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000326{
Eric Andersen8876fb22003-06-20 09:01:58 +0000327 FILE *file;
328 int matched;
Eric Andersen31c27a92004-10-08 08:10:57 +0000329 llist_t *fopt = NULL;
Matt Kraai999623e2001-10-29 15:49:03 +0000330
Mark Whitleyd3721892000-06-28 22:00:26 +0000331 /* do normal option parsing */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000332#if ENABLE_FEATURE_GREP_CONTEXT
Eric Andersen8876fb22003-06-20 09:01:58 +0000333 char *slines_after;
334 char *slines_before;
335 char *Copt;
336
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000337 opt_complementary = "H-h:e::f::C-AB";
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000338 getopt32(argc, argv,
Eric Andersen8876fb22003-06-20 09:01:58 +0000339 GREP_OPTS GREP_OPT_CONTEXT OPT_EGREP,
340 &pattern_head, &fopt,
341 &slines_after, &slines_before, &Copt);
342
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000343 if (option_mask32 & GREP_OPT_C) {
Denis Vlasenko2295b492006-10-22 11:42:51 +0000344 /* -C unsets prev -A and -B, but following -A or -B
345 may override it */
346 if (!(option_mask32 & GREP_OPT_A)) /* not overridden */
Eric Andersen8876fb22003-06-20 09:01:58 +0000347 slines_after = Copt;
Denis Vlasenko2295b492006-10-22 11:42:51 +0000348 if (!(option_mask32 & GREP_OPT_B)) /* not overridden */
Eric Andersen8876fb22003-06-20 09:01:58 +0000349 slines_before = Copt;
Denis Vlasenko2295b492006-10-22 11:42:51 +0000350 option_mask32 |= GREP_OPT_A|GREP_OPT_B; /* for parser */
Eric Andersen053b1462000-06-13 06:24:53 +0000351 }
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000352 if (option_mask32 & GREP_OPT_A) {
353 lines_after = xatoi_u(slines_after);
Eric Andersen8876fb22003-06-20 09:01:58 +0000354 }
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000355 if (option_mask32 & GREP_OPT_B) {
356 lines_before = xatoi_u(slines_before);
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000357 }
Denis Vlasenko2295b492006-10-22 11:42:51 +0000358 /* sanity checks */
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000359 if (option_mask32 & (GREP_OPT_c|GREP_OPT_q|GREP_OPT_l|GREP_OPT_L)) {
360 option_mask32 &= ~GREP_OPT_n;
Eric Andersen8876fb22003-06-20 09:01:58 +0000361 lines_before = 0;
362 lines_after = 0;
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000363 } else if (lines_before > 0)
Denis Vlasenko4cccc032006-12-22 18:37:07 +0000364 before_buf = xzalloc(lines_before * sizeof(char *));
Eric Andersen8876fb22003-06-20 09:01:58 +0000365#else
366 /* with auto sanity checks */
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000367 opt_complementary = "H-h:e::f::c-n:q-n:l-n";
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000368 getopt32(argc, argv, GREP_OPTS OPT_EGREP,
Eric Andersen8876fb22003-06-20 09:01:58 +0000369 &pattern_head, &fopt);
Eric Andersen8876fb22003-06-20 09:01:58 +0000370#endif
Denis Vlasenko2295b492006-10-22 11:42:51 +0000371 invert_search = ((option_mask32 & GREP_OPT_v) != 0); /* 0 | 1 */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000372
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000373 if (pattern_head != NULL) {
374 /* convert char *argv[] to grep_list_data_t */
375 llist_t *cur;
376
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000377 for (cur = pattern_head; cur; cur = cur->link)
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000378 cur->data = new_grep_list_data(cur->data, 0);
379 }
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000380 if (option_mask32 & GREP_OPT_f)
Eric Andersen8876fb22003-06-20 09:01:58 +0000381 load_regexes_from_file(fopt);
382
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000383 if (ENABLE_FEATURE_GREP_FGREP_ALIAS && applet_name[0] == 'f')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000384 option_mask32 |= GREP_OPT_F;
Mike Frysinger15ca5862005-07-31 22:41:05 +0000385
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000386 if (!(option_mask32 & GREP_OPT_o))
Denis Vlasenko51937532006-09-29 20:58:53 +0000387 reflags = REG_NOSUB;
388
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000389 if (ENABLE_FEATURE_GREP_EGREP_ALIAS &&
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000390 (applet_name[0] == 'e' || (option_mask32 & GREP_OPT_E)))
Denis Vlasenko51937532006-09-29 20:58:53 +0000391 reflags |= REG_EXTENDED;
Eric Andersen8876fb22003-06-20 09:01:58 +0000392
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000393 if (option_mask32 & GREP_OPT_i)
Eric Andersen8876fb22003-06-20 09:01:58 +0000394 reflags |= REG_ICASE;
395
396 argv += optind;
397 argc -= optind;
Eric Andersen053b1462000-06-13 06:24:53 +0000398
Mark Whitleyfa43e542001-05-24 18:36:18 +0000399 /* 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 */
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000401 if (pattern_head == NULL) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000402 if (*argv == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000403 bb_show_usage();
Mark Whitleyfa43e542001-05-24 18:36:18 +0000404 else {
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000405 char *pattern = new_grep_list_data(*argv++, 0);
406
Rob Landley8bb50782006-05-26 23:44:51 +0000407 llist_add_to(&pattern_head, pattern);
Eric Andersen8876fb22003-06-20 09:01:58 +0000408 argc--;
Mark Whitleyfa43e542001-05-24 18:36:18 +0000409 }
410 }
Mark Whitleyd3721892000-06-28 22:00:26 +0000411
Mark Whitleyfa43e542001-05-24 18:36:18 +0000412 /* argv[(optind)..(argc-1)] should be names of file to grep through. If
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000413 * there is more than one file to grep, we will print the filenames. */
Denis Vlasenko2295b492006-10-22 11:42:51 +0000414 if (argc > 1)
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000415 print_filename = 1;
Denis Vlasenko2295b492006-10-22 11:42:51 +0000416 /* -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;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000421
Mark Whitley2e1148b2000-06-28 22:59:30 +0000422 /* If no files were specified, or '-' was specified, take input from
423 * stdin. Otherwise, we grep through all the files specified. */
Denis Vlasenko2295b492006-10-22 11:42:51 +0000424 if (argc == 0)
Eric Andersen8876fb22003-06-20 09:01:58 +0000425 argc++;
Eric Andersen8876fb22003-06-20 09:01:58 +0000426 matched = 0;
427 while (argc--) {
428 cur_file = *argv++;
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000429 file = stdin;
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000430 if (!cur_file || (*cur_file == '-' && !cur_file[1])) {
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000431 cur_file = "(standard input)";
Eric Andersen8876fb22003-06-20 09:01:58 +0000432 } else {
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000433 if (option_mask32 & GREP_OPT_r) {
434 struct stat st;
435 if (stat(cur_file, &st) == 0 && S_ISDIR(st.st_mode)) {
Denis Vlasenko3544ae62006-10-14 14:51:59 +0000436 if (!(option_mask32 & GREP_OPT_h))
437 print_filename = 1;
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000438 matched += grep_dir(cur_file);
439 goto grep_done;
440 }
Mark Whitley2ef880b2000-07-18 21:02:06 +0000441 }
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000442 /* 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 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450 }
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000451 matched += grep_file(file);
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000452 fclose_if_not_stdin(file);
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000453 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 }
"Vladimir N. Oleynik"cf40d812005-09-23 13:23:15 +0000459 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000460
Eric Andersen8876fb22003-06-20 09:01:58 +0000461 /* destroy all the elments in the pattern list */
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000462 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;
Eric Andersen8876fb22003-06-20 09:01:58 +0000467
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000468 pattern_head = pattern_head->link;
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000469 if ((gl->flg_mem_alocated_compiled & PATTERN_MEM_A))
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000470 free(gl->pattern);
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000471 if ((gl->flg_mem_alocated_compiled & COMPILED))
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000472 regfree(&(gl->preg));
473 free(pattern_head_ptr);
474 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000475 }
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000476 /* 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. */
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000479 if (BE_QUIET && matched)
"Vladimir N. Oleynik"bf449742005-09-23 13:50:24 +0000480 return 0;
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000481 if (open_errors)
"Vladimir N. Oleynik"bf449742005-09-23 13:50:24 +0000482 return 2;
Mark Whitleyb5c29852001-02-01 21:02:41 +0000483 return !matched; /* invert return value 0 = success, 1 = failed */
Eric Andersencc8ed391999-10-05 16:24:54 +0000484}