blob: ce975e771269d5b929a9d753deea76d206c71c00 [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 Vlasenko67b23e62006-10-03 21:00:06 +000027static unsigned opt;
Denis Vlasenko51937532006-09-29 20:58:53 +000028#define GREP_OPTS "lnqvscFiHhe:f:Lo"
Eric Andersenabc513a2004-05-26 11:48:29 +000029#define GREP_OPT_l (1<<0)
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000030#define PRINT_FILES_WITH_MATCHES (opt & GREP_OPT_l)
Eric Andersenabc513a2004-05-26 11:48:29 +000031#define GREP_OPT_n (1<<1)
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000032#define PRINT_LINE_NUM (opt & GREP_OPT_n)
Eric Andersenabc513a2004-05-26 11:48:29 +000033#define GREP_OPT_q (1<<2)
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000034#define BE_QUIET (opt & GREP_OPT_q)
Eric Andersenabc513a2004-05-26 11:48:29 +000035#define GREP_OPT_v (1<<3)
Eric Andersen8876fb22003-06-20 09:01:58 +000036typedef char invert_search_t;
37static invert_search_t invert_search;
Eric Andersenabc513a2004-05-26 11:48:29 +000038#define GREP_OPT_s (1<<4)
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000039#define SUPPRESS_ERR_MSGS (opt & GREP_OPT_s)
Eric Andersenabc513a2004-05-26 11:48:29 +000040#define GREP_OPT_c (1<<5)
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000041#define PRINT_MATCH_COUNTS (opt & GREP_OPT_c)
Eric Andersenabc513a2004-05-26 11:48:29 +000042#define GREP_OPT_F (1<<6)
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000043#define FGREP_FLAG (opt & GREP_OPT_F)
Eric Andersenabc513a2004-05-26 11:48:29 +000044#define GREP_OPT_i (1<<7)
45#define GREP_OPT_H (1<<8)
46#define GREP_OPT_h (1<<9)
47#define GREP_OPT_e (1<<10)
48#define GREP_OPT_f (1<<11)
49#define GREP_OPT_L (1<<12)
Denis Vlasenko51937532006-09-29 20:58:53 +000050#define PRINT_FILES_WITHOUT_MATCHES (opt & GREP_OPT_L)
51#define GREP_OPT_o (1<<13)
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000052#if ENABLE_FEATURE_GREP_CONTEXT
Eric Andersen3312c982006-09-25 22:18:56 +000053#define GREP_OPT_CONTEXT "A:B:C:"
Denis Vlasenko51937532006-09-29 20:58:53 +000054#define GREP_OPT_A (1<<14)
55#define GREP_OPT_B (1<<15)
56#define GREP_OPT_C (1<<16)
57#define GREP_OPT_E (1<<17)
Eric Andersen8876fb22003-06-20 09:01:58 +000058#else
59#define GREP_OPT_CONTEXT ""
Denis Vlasenko51937532006-09-29 20:58:53 +000060#define GREP_OPT_A 0
61#define GREP_OPT_B 0
62#define GREP_OPT_C 0
63#define GREP_OPT_E (1<<14)
Eric Andersen8876fb22003-06-20 09:01:58 +000064#endif
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000065#if ENABLE_FEATURE_GREP_EGREP_ALIAS
Eric Andersen8876fb22003-06-20 09:01:58 +000066# define OPT_EGREP "E"
67#else
68# define OPT_EGREP ""
69#endif
70
71static int reflags;
72static int print_filename;
Mark Whitleyd3721892000-06-28 22:00:26 +000073
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000074#if ENABLE_FEATURE_GREP_CONTEXT
Eric Andersen8876fb22003-06-20 09:01:58 +000075static int lines_before;
76static int lines_after;
77static char **before_buf;
78static int last_line_printed;
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000079#endif /* ENABLE_FEATURE_GREP_CONTEXT */
Mark Whitley2fd52982001-02-09 00:41:10 +000080
81/* globals used internally */
Eric Andersen8876fb22003-06-20 09:01:58 +000082static llist_t *pattern_head; /* growable list of patterns to match */
83static char *cur_file; /* the current file we are reading */
Mark Whitleyd3721892000-06-28 22:00:26 +000084
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +000085typedef struct GREP_LIST_DATA {
86 char *pattern;
87 regex_t preg;
88#define PATTERN_MEM_A 1
89#define COMPILED 2
90 int flg_mem_alocated_compiled;
91} grep_list_data_t;
Mark Whitleyfa43e542001-05-24 18:36:18 +000092
Mark Whitley2fd52982001-02-09 00:41:10 +000093static void print_line(const char *line, int linenum, char decoration)
94{
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000095#if ENABLE_FEATURE_GREP_CONTEXT
Eric Andersenaff114c2004-04-14 17:51:38 +000096 /* possibly print the little '--' separator */
Matt Kraaiedc80652001-05-22 14:29:27 +000097 if ((lines_before || lines_after) && last_line_printed &&
98 last_line_printed < linenum - 1) {
Mark Whitley2fd52982001-02-09 00:41:10 +000099 puts("--");
100 }
101 last_line_printed = linenum;
102#endif
Mike Frysinger5ba5f4d2005-04-16 04:56:11 +0000103 if (print_filename > 0)
Mark Whitley2fd52982001-02-09 00:41:10 +0000104 printf("%s%c", cur_file, decoration);
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000105 if (PRINT_LINE_NUM)
Mark Whitley2fd52982001-02-09 00:41:10 +0000106 printf("%i%c", linenum, decoration);
Denis Vlasenko51937532006-09-29 20:58:53 +0000107 /* Emulate weird GNU grep behavior with -ov */
108 if ((opt & (GREP_OPT_v+GREP_OPT_o)) != (GREP_OPT_v+GREP_OPT_o))
109 puts(line);
Mark Whitley2fd52982001-02-09 00:41:10 +0000110}
Mark Whitleyd3721892000-06-28 22:00:26 +0000111
Eric Andersen8876fb22003-06-20 09:01:58 +0000112
113static int grep_file(FILE *file)
Mark Whitleyd3721892000-06-28 22:00:26 +0000114{
Eric Andersen8876fb22003-06-20 09:01:58 +0000115 char *line;
116 invert_search_t ret;
Mark Whitleyd3721892000-06-28 22:00:26 +0000117 int linenum = 0;
Matt Kraaideb95f62000-08-06 15:25:53 +0000118 int nmatches = 0;
Denis Vlasenko51937532006-09-29 20:58:53 +0000119 regmatch_t regmatch;
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000120#if ENABLE_FEATURE_GREP_CONTEXT
Mark Whitley2fd52982001-02-09 00:41:10 +0000121 int print_n_lines_after = 0;
122 int curpos = 0; /* track where we are in the circular 'before' buffer */
123 int idx = 0; /* used for iteration through the circular buffer */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000124#endif /* ENABLE_FEATURE_GREP_CONTEXT */
Mark Whitleyd3721892000-06-28 22:00:26 +0000125
Manuel Novoa III cad53642003-03-19 09:13:01 +0000126 while ((line = bb_get_chomped_line_from_file(file)) != NULL) {
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000127 llist_t *pattern_ptr = pattern_head;
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000128 grep_list_data_t * gl;
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000129
Mark Whitleyd3721892000-06-28 22:00:26 +0000130 linenum++;
Eric Andersen8876fb22003-06-20 09:01:58 +0000131 ret = 0;
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000132 while (pattern_ptr) {
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000133 gl = (grep_list_data_t *)pattern_ptr->data;
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000134 if (FGREP_FLAG) {
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000135 ret = strstr(line, gl->pattern) != NULL;
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000136 } else {
137 /*
138 * test for a postitive-assertion match (regexec returns success (0)
139 * and the user did not specify invert search), or a negative-assertion
140 * match (regexec returns failure (REG_NOMATCH) and the user specified
141 * invert search)
142 */
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000143 if (!(gl->flg_mem_alocated_compiled & COMPILED)) {
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000144 gl->flg_mem_alocated_compiled |= COMPILED;
145 xregcomp(&(gl->preg), gl->pattern, reflags);
146 }
Denis Vlasenko51937532006-09-29 20:58:53 +0000147 regmatch.rm_so = 0;
148 regmatch.rm_eo = 0;
149 ret |= regexec(&(gl->preg), line, 1, &regmatch, 0) == 0;
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000150 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000151 pattern_ptr = pattern_ptr->link;
152 } /* while (pattern_ptr) */
Mark Whitleyd3721892000-06-28 22:00:26 +0000153
Denis Vlasenko51937532006-09-29 20:58:53 +0000154 if (ret ^ invert_search) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000155
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000156 if (PRINT_FILES_WITH_MATCHES || BE_QUIET)
Eric Andersen8876fb22003-06-20 09:01:58 +0000157 free(line);
158
159 /* if we found a match but were told to be quiet, stop here */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000160 if (BE_QUIET || PRINT_FILES_WITHOUT_MATCHES)
Eric Andersen8876fb22003-06-20 09:01:58 +0000161 return -1;
Mark Whitleyd3721892000-06-28 22:00:26 +0000162
Mark Whitleyfa43e542001-05-24 18:36:18 +0000163 /* keep track of matches */
164 nmatches++;
Mark Whitley1d9d4112001-05-21 21:13:00 +0000165
Mark Whitleyfa43e542001-05-24 18:36:18 +0000166 /* if we're just printing filenames, we stop after the first match */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000167 if (PRINT_FILES_WITH_MATCHES)
Mark Whitleyfa43e542001-05-24 18:36:18 +0000168 break;
Mark Whitley1d9d4112001-05-21 21:13:00 +0000169
Mark Whitleyfa43e542001-05-24 18:36:18 +0000170 /* print the matched line */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000171 if (PRINT_MATCH_COUNTS == 0) {
172#if ENABLE_FEATURE_GREP_CONTEXT
Mark Whitleyfa43e542001-05-24 18:36:18 +0000173 int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
Mark Whitley2fd52982001-02-09 00:41:10 +0000174
Mark Whitleyfa43e542001-05-24 18:36:18 +0000175 /* if we were told to print 'before' lines and there is at least
176 * one line in the circular buffer, print them */
177 if (lines_before && before_buf[prevpos] != NULL) {
178 int first_buf_entry_line_num = linenum - lines_before;
Mark Whitley2fd52982001-02-09 00:41:10 +0000179
Mark Whitleyfa43e542001-05-24 18:36:18 +0000180 /* advance to the first entry in the circular buffer, and
181 * figure out the line number is of the first line in the
182 * buffer */
183 idx = curpos;
184 while (before_buf[idx] == NULL) {
185 idx = (idx + 1) % lines_before;
186 first_buf_entry_line_num++;
187 }
188
189 /* now print each line in the buffer, clearing them as we go */
190 while (before_buf[idx] != NULL) {
191 print_line(before_buf[idx], first_buf_entry_line_num, '-');
192 free(before_buf[idx]);
193 before_buf[idx] = NULL;
194 idx = (idx + 1) % lines_before;
195 first_buf_entry_line_num++;
196 }
Mark Whitley2fd52982001-02-09 00:41:10 +0000197 }
198
Mark Whitleyfa43e542001-05-24 18:36:18 +0000199 /* make a note that we need to print 'after' lines */
200 print_n_lines_after = lines_after;
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000201#endif
Denis Vlasenko51937532006-09-29 20:58:53 +0000202 if (opt & GREP_OPT_o) {
203 line[regmatch.rm_eo] = '\0';
204 print_line(line + regmatch.rm_so, linenum, ':');
205 } else {
206 print_line(line, linenum, ':');
207 }
Mark Whitley2fd52982001-02-09 00:41:10 +0000208 }
Matt Kraai0810f722001-01-04 15:11:52 +0000209 }
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000210#if ENABLE_FEATURE_GREP_CONTEXT
Mark Whitleyfa43e542001-05-24 18:36:18 +0000211 else { /* no match */
212 /* Add the line to the circular 'before' buffer */
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000213 if (lines_before) {
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000214 free(before_buf[curpos]);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000215 before_buf[curpos] = xstrdup(line);
Mark Whitleyfa43e542001-05-24 18:36:18 +0000216 curpos = (curpos + 1) % lines_before;
217 }
Mark Whitley2fd52982001-02-09 00:41:10 +0000218 }
Mark Whitley2fd52982001-02-09 00:41:10 +0000219
Mark Whitleyfa43e542001-05-24 18:36:18 +0000220 /* if we need to print some context lines after the last match, do so */
221 if (print_n_lines_after && (last_line_printed != linenum)) {
222 print_line(line, linenum, '-');
223 print_n_lines_after--;
224 }
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000225#endif /* ENABLE_FEATURE_GREP_CONTEXT */
Mark Whitleyd3721892000-06-28 22:00:26 +0000226 free(line);
227 }
Mark Whitley8f122432000-07-18 18:37:01 +0000228
Mark Whitley35e59be2001-05-14 19:40:32 +0000229 /* special-case file post-processing for options where we don't print line
Mark Whitley1d9d4112001-05-21 21:13:00 +0000230 * matches, just filenames and possibly match counts */
Mark Whitley35e59be2001-05-14 19:40:32 +0000231
Mark Whitley1d9d4112001-05-21 21:13:00 +0000232 /* grep -c: print [filename:]count, even if count is zero */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000233 if (PRINT_MATCH_COUNTS) {
Mike Frysinger5ba5f4d2005-04-16 04:56:11 +0000234 if (print_filename > 0)
Mark Whitley1d9d4112001-05-21 21:13:00 +0000235 printf("%s:", cur_file);
Denis Vlasenko92758142006-10-03 19:56:34 +0000236 printf("%d\n", nmatches);
Mark Whitley35e59be2001-05-14 19:40:32 +0000237 }
Mark Whitley1d9d4112001-05-21 21:13:00 +0000238
239 /* grep -l: print just the filename, but only if we grepped the line in the file */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000240 if (PRINT_FILES_WITH_MATCHES && nmatches > 0) {
Matt Kraai59df6f72001-05-16 14:21:09 +0000241 puts(cur_file);
Mark Whitley35e59be2001-05-14 19:40:32 +0000242 }
243
Eric Andersendec7f812004-05-26 11:47:55 +0000244 /* 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 +0000245 if (PRINT_FILES_WITHOUT_MATCHES && nmatches == 0) {
Eric Andersendec7f812004-05-26 11:47:55 +0000246 puts(cur_file);
247 }
248
Eric Andersen8876fb22003-06-20 09:01:58 +0000249 return nmatches;
Mark Whitleyd3721892000-06-28 22:00:26 +0000250}
Eric Andersencc8ed391999-10-05 16:24:54 +0000251
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000252#if ENABLE_FEATURE_CLEAN_UP
253#define new_grep_list_data(p, m) add_grep_list_data(p, m)
254static char * add_grep_list_data(char *pattern, int flg_used_mem)
255#else
256#define new_grep_list_data(p, m) add_grep_list_data(p)
257static char * add_grep_list_data(char *pattern)
258#endif
259{
260 grep_list_data_t *gl = xmalloc(sizeof(grep_list_data_t));
261 gl->pattern = pattern;
262#if ENABLE_FEATURE_CLEAN_UP
263 gl->flg_mem_alocated_compiled = flg_used_mem;
264#else
265 gl->flg_mem_alocated_compiled = 0;
266#endif
267 return (char *)gl;
268}
269
270
Eric Andersen8876fb22003-06-20 09:01:58 +0000271static void load_regexes_from_file(llist_t *fopt)
Mark Whitleyfa43e542001-05-24 18:36:18 +0000272{
273 char *line;
Eric Andersen8876fb22003-06-20 09:01:58 +0000274 FILE *f;
275
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000276 while (fopt) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000277 llist_t *cur = fopt;
278 char *ffile = cur->data;
279
280 fopt = cur->link;
281 free(cur);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000282 f = xfopen(ffile, "r");
Eric Andersen31c27a92004-10-08 08:10:57 +0000283 while ((line = bb_get_chomped_line_from_file(f)) != NULL) {
Rob Landley8bb50782006-05-26 23:44:51 +0000284 llist_add_to(&pattern_head,
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000285 new_grep_list_data(line, PATTERN_MEM_A));
Eric Andersen31c27a92004-10-08 08:10:57 +0000286 }
Mark Whitleyfa43e542001-05-24 18:36:18 +0000287 }
288}
Mark Whitleyfa43e542001-05-24 18:36:18 +0000289
290
Rob Landleydfba7412006-03-06 20:47:33 +0000291int grep_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000292{
Eric Andersen8876fb22003-06-20 09:01:58 +0000293 FILE *file;
294 int matched;
Eric Andersen31c27a92004-10-08 08:10:57 +0000295 llist_t *fopt = NULL;
"Vladimir N. Oleynik"cf40d812005-09-23 13:23:15 +0000296 int error_open_count = 0;
Matt Kraai999623e2001-10-29 15:49:03 +0000297
Mark Whitleyd3721892000-06-28 22:00:26 +0000298 /* do normal option parsing */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000299#if ENABLE_FEATURE_GREP_CONTEXT
Eric Andersen8876fb22003-06-20 09:01:58 +0000300 char *junk;
301 char *slines_after;
302 char *slines_before;
303 char *Copt;
304
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000305 opt_complementary = "H-h:e::f::C-AB";
306 opt = getopt32(argc, argv,
Eric Andersen8876fb22003-06-20 09:01:58 +0000307 GREP_OPTS GREP_OPT_CONTEXT OPT_EGREP,
308 &pattern_head, &fopt,
309 &slines_after, &slines_before, &Copt);
310
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000311 if (opt & GREP_OPT_C) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000312 /* C option unseted A and B options, but next -A or -B
313 may be ovewrite own option */
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000314 if (!(opt & GREP_OPT_A)) /* not overwtited */
Eric Andersen8876fb22003-06-20 09:01:58 +0000315 slines_after = Copt;
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000316 if (!(opt & GREP_OPT_B)) /* not overwtited */
Eric Andersen8876fb22003-06-20 09:01:58 +0000317 slines_before = Copt;
318 opt |= GREP_OPT_A|GREP_OPT_B; /* set for parse now */
Eric Andersen053b1462000-06-13 06:24:53 +0000319 }
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000320 if (opt & GREP_OPT_A) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000321 lines_after = strtoul(slines_after, &junk, 10);
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000322 if (*junk != '\0')
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000323 bb_error_msg_and_die(bb_msg_invalid_arg, slines_after, "-A");
Eric Andersen8876fb22003-06-20 09:01:58 +0000324 }
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000325 if (opt & GREP_OPT_B) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000326 lines_before = strtoul(slines_before, &junk, 10);
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000327 if (*junk != '\0')
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000328 bb_error_msg_and_die(bb_msg_invalid_arg, slines_before, "-B");
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000329 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000330 /* sanity checks after parse may be invalid numbers ;-) */
Denis Vlasenko51937532006-09-29 20:58:53 +0000331 if (opt & (GREP_OPT_c|GREP_OPT_q|GREP_OPT_l|GREP_OPT_L)) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000332 opt &= ~GREP_OPT_n;
333 lines_before = 0;
334 lines_after = 0;
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000335 } else if (lines_before > 0)
Rob Landley081e3842006-08-03 20:07:35 +0000336 before_buf = (char **)xzalloc(lines_before * sizeof(char *));
Eric Andersen8876fb22003-06-20 09:01:58 +0000337#else
338 /* with auto sanity checks */
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000339 opt_complementary = "H-h:e::f::c-n:q-n:l-n";
340 opt = getopt32(argc, argv, GREP_OPTS OPT_EGREP,
Eric Andersen8876fb22003-06-20 09:01:58 +0000341 &pattern_head, &fopt);
Eric Andersen8876fb22003-06-20 09:01:58 +0000342#endif
Eric Andersen8876fb22003-06-20 09:01:58 +0000343 invert_search = (opt & GREP_OPT_v) != 0; /* 0 | 1 */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000344
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000345 if (opt & GREP_OPT_H)
Eric Andersen8876fb22003-06-20 09:01:58 +0000346 print_filename++;
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000347 if (opt & GREP_OPT_h)
Eric Andersen8876fb22003-06-20 09:01:58 +0000348 print_filename--;
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000349 if (pattern_head != NULL) {
350 /* convert char *argv[] to grep_list_data_t */
351 llist_t *cur;
352
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000353 for (cur = pattern_head; cur; cur = cur->link)
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000354 cur->data = new_grep_list_data(cur->data, 0);
355 }
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000356 if (opt & GREP_OPT_f)
Eric Andersen8876fb22003-06-20 09:01:58 +0000357 load_regexes_from_file(fopt);
358
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000359 if (ENABLE_FEATURE_GREP_FGREP_ALIAS && applet_name[0] == 'f')
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000360 opt |= GREP_OPT_F;
Mike Frysinger15ca5862005-07-31 22:41:05 +0000361
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000362 if (!(opt & GREP_OPT_o))
Denis Vlasenko51937532006-09-29 20:58:53 +0000363 reflags = REG_NOSUB;
364
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000365 if (ENABLE_FEATURE_GREP_EGREP_ALIAS &&
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000366 (applet_name[0] == 'e' || (opt & GREP_OPT_E)))
Denis Vlasenko51937532006-09-29 20:58:53 +0000367 reflags |= REG_EXTENDED;
Eric Andersen8876fb22003-06-20 09:01:58 +0000368
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000369 if (opt & GREP_OPT_i)
Eric Andersen8876fb22003-06-20 09:01:58 +0000370 reflags |= REG_ICASE;
371
372 argv += optind;
373 argc -= optind;
Eric Andersen053b1462000-06-13 06:24:53 +0000374
Mark Whitleyfa43e542001-05-24 18:36:18 +0000375 /* if we didn't get a pattern from a -e and no command file was specified,
376 * argv[optind] should be the pattern. no pattern, no worky */
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000377 if (pattern_head == NULL) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000378 if (*argv == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000379 bb_show_usage();
Mark Whitleyfa43e542001-05-24 18:36:18 +0000380 else {
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000381 char *pattern = new_grep_list_data(*argv++, 0);
382
Rob Landley8bb50782006-05-26 23:44:51 +0000383 llist_add_to(&pattern_head, pattern);
Eric Andersen8876fb22003-06-20 09:01:58 +0000384 argc--;
Mark Whitleyfa43e542001-05-24 18:36:18 +0000385 }
386 }
Mark Whitleyd3721892000-06-28 22:00:26 +0000387
Mark Whitleyfa43e542001-05-24 18:36:18 +0000388 /* argv[(optind)..(argc-1)] should be names of file to grep through. If
Mark Whitleyd3721892000-06-28 22:00:26 +0000389 * there is more than one file to grep, we will print the filenames */
Eric Andersen8876fb22003-06-20 09:01:58 +0000390 if (argc > 1) {
Mark Whitleyd3721892000-06-28 22:00:26 +0000391 print_filename++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000392
Mark Whitley2e1148b2000-06-28 22:59:30 +0000393 /* If no files were specified, or '-' was specified, take input from
394 * stdin. Otherwise, we grep through all the files specified. */
Eric Andersen8876fb22003-06-20 09:01:58 +0000395 } else if (argc == 0) {
396 argc++;
Mark Whitley2ef880b2000-07-18 21:02:06 +0000397 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000398 matched = 0;
399 while (argc--) {
400 cur_file = *argv++;
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000401 if (!cur_file || (*cur_file == '-' && !cur_file[1])) {
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000402 cur_file = "(standard input)";
Eric Andersen8876fb22003-06-20 09:01:58 +0000403 file = stdin;
404 } else {
Mark Whitleyd3721892000-06-28 22:00:26 +0000405 file = fopen(cur_file, "r");
Eric Andersen8876fb22003-06-20 09:01:58 +0000406 }
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000407 if (file == NULL) {
408 if (!SUPPRESS_ERR_MSGS)
409 bb_perror_msg("%s", cur_file);
410 error_open_count++;
Eric Andersen8876fb22003-06-20 09:01:58 +0000411 } else {
412 matched += grep_file(file);
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000413 if (matched < 0) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000414 /* we found a match but were told to be quiet, stop here and
415 * return success */
416 break;
Mark Whitley2ef880b2000-07-18 21:02:06 +0000417 }
"Vladimir N. Oleynik"cf40d812005-09-23 13:23:15 +0000418 fclose(file);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000419 }
"Vladimir N. Oleynik"cf40d812005-09-23 13:23:15 +0000420 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000421
Eric Andersen8876fb22003-06-20 09:01:58 +0000422 /* destroy all the elments in the pattern list */
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000423 if (ENABLE_FEATURE_CLEAN_UP) {
424 while (pattern_head) {
425 llist_t *pattern_head_ptr = pattern_head;
426 grep_list_data_t *gl =
427 (grep_list_data_t *)pattern_head_ptr->data;
Eric Andersen8876fb22003-06-20 09:01:58 +0000428
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000429 pattern_head = pattern_head->link;
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000430 if ((gl->flg_mem_alocated_compiled & PATTERN_MEM_A))
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000431 free(gl->pattern);
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000432 if ((gl->flg_mem_alocated_compiled & COMPILED))
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000433 regfree(&(gl->preg));
434 free(pattern_head_ptr);
435 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000436 }
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000437 /* 0 = success, 1 = failed, 2 = error */
438 /* If the -q option is specified, the exit status shall be zero
439 * if an input line is selected, even if an error was detected. */
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000440 if (BE_QUIET && matched)
"Vladimir N. Oleynik"bf449742005-09-23 13:50:24 +0000441 return 0;
Denis Vlasenko6c30db82006-09-29 21:04:12 +0000442 if (error_open_count)
"Vladimir N. Oleynik"bf449742005-09-23 13:50:24 +0000443 return 2;
Mark Whitleyb5c29852001-02-01 21:02:41 +0000444 return !matched; /* invert return value 0 = success, 1 = failed */
Eric Andersencc8ed391999-10-05 16:24:54 +0000445}