blob: c2c4e709506c4a39c188a299018f3c7b3f834d2e [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-Fischeraaf0e232005-09-23 15:38:49 +00008 * Licensed under the GPL v2, 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
Eric Andersen8876fb22003-06-20 09:01:58 +000017*/
Eric Andersencc8ed391999-10-05 16:24:54 +000018
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000019#include "busybox.h"
"Vladimir N. Oleynik"23f62fc2005-09-14 16:59:11 +000020#include "xregex.h"
Mark Whitleyd3721892000-06-28 22:00:26 +000021
Eric Andersened3ef502001-01-27 08:24:39 +000022
Mark Whitleyd3721892000-06-28 22:00:26 +000023/* options */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000024static unsigned long opt;
Eric Andersendec7f812004-05-26 11:47:55 +000025#define GREP_OPTS "lnqvscFiHhe:f:L"
Eric Andersenabc513a2004-05-26 11:48:29 +000026#define GREP_OPT_l (1<<0)
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000027#define PRINT_FILES_WITH_MATCHES (opt & GREP_OPT_l)
Eric Andersenabc513a2004-05-26 11:48:29 +000028#define GREP_OPT_n (1<<1)
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000029#define PRINT_LINE_NUM (opt & GREP_OPT_n)
Eric Andersenabc513a2004-05-26 11:48:29 +000030#define GREP_OPT_q (1<<2)
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000031#define BE_QUIET (opt & GREP_OPT_q)
Eric Andersenabc513a2004-05-26 11:48:29 +000032#define GREP_OPT_v (1<<3)
Eric Andersen8876fb22003-06-20 09:01:58 +000033typedef char invert_search_t;
34static invert_search_t invert_search;
Eric Andersenabc513a2004-05-26 11:48:29 +000035#define GREP_OPT_s (1<<4)
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000036#define SUPPRESS_ERR_MSGS (opt & GREP_OPT_s)
Eric Andersenabc513a2004-05-26 11:48:29 +000037#define GREP_OPT_c (1<<5)
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000038#define PRINT_MATCH_COUNTS (opt & GREP_OPT_c)
Eric Andersenabc513a2004-05-26 11:48:29 +000039#define GREP_OPT_F (1<<6)
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000040#define FGREP_FLAG (opt & 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)
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000047#define PRINT_FILES_WITHOUT_MATCHES ((opt & GREP_OPT_L) != 0)
48#if ENABLE_FEATURE_GREP_CONTEXT
Eric Andersen8876fb22003-06-20 09:01:58 +000049#define GREP_OPT_CONTEXT "A:B:C"
Eric Andersenabc513a2004-05-26 11:48:29 +000050#define GREP_OPT_A (1<<13)
51#define GREP_OPT_B (1<<14)
52#define GREP_OPT_C (1<<15)
53#define GREP_OPT_E (1<<16)
Eric Andersen8876fb22003-06-20 09:01:58 +000054#else
55#define GREP_OPT_CONTEXT ""
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000056#define GREP_OPT_A (0)
57#define GREP_OPT_B (0)
58#define GREP_OPT_C (0)
Eric Andersenabc513a2004-05-26 11:48:29 +000059#define GREP_OPT_E (1<<13)
Eric Andersen8876fb22003-06-20 09:01:58 +000060#endif
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000061#if ENABLE_FEATURE_GREP_EGREP_ALIAS
Eric Andersen8876fb22003-06-20 09:01:58 +000062# define OPT_EGREP "E"
63#else
64# define OPT_EGREP ""
65#endif
66
67static int reflags;
68static int print_filename;
Mark Whitleyd3721892000-06-28 22:00:26 +000069
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000070#if ENABLE_FEATURE_GREP_CONTEXT
Eric Andersen8876fb22003-06-20 09:01:58 +000071static int lines_before;
72static int lines_after;
73static char **before_buf;
74static int last_line_printed;
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000075#endif /* ENABLE_FEATURE_GREP_CONTEXT */
Mark Whitley2fd52982001-02-09 00:41:10 +000076
77/* globals used internally */
Eric Andersen8876fb22003-06-20 09:01:58 +000078static llist_t *pattern_head; /* growable list of patterns to match */
79static char *cur_file; /* the current file we are reading */
Mark Whitleyd3721892000-06-28 22:00:26 +000080
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +000081typedef struct GREP_LIST_DATA {
82 char *pattern;
83 regex_t preg;
84#define PATTERN_MEM_A 1
85#define COMPILED 2
86 int flg_mem_alocated_compiled;
87} grep_list_data_t;
Mark Whitleyfa43e542001-05-24 18:36:18 +000088
Mark Whitley2fd52982001-02-09 00:41:10 +000089static void print_line(const char *line, int linenum, char decoration)
90{
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000091#if ENABLE_FEATURE_GREP_CONTEXT
Eric Andersenaff114c2004-04-14 17:51:38 +000092 /* possibly print the little '--' separator */
Matt Kraaiedc80652001-05-22 14:29:27 +000093 if ((lines_before || lines_after) && last_line_printed &&
94 last_line_printed < linenum - 1) {
Mark Whitley2fd52982001-02-09 00:41:10 +000095 puts("--");
96 }
97 last_line_printed = linenum;
98#endif
Mike Frysinger5ba5f4d2005-04-16 04:56:11 +000099 if (print_filename > 0)
Mark Whitley2fd52982001-02-09 00:41:10 +0000100 printf("%s%c", cur_file, decoration);
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000101 if (PRINT_LINE_NUM)
Mark Whitley2fd52982001-02-09 00:41:10 +0000102 printf("%i%c", linenum, decoration);
103 puts(line);
104}
Mark Whitleyd3721892000-06-28 22:00:26 +0000105
Eric Andersen8876fb22003-06-20 09:01:58 +0000106
107static int grep_file(FILE *file)
Mark Whitleyd3721892000-06-28 22:00:26 +0000108{
Eric Andersen8876fb22003-06-20 09:01:58 +0000109 char *line;
110 invert_search_t ret;
Mark Whitleyd3721892000-06-28 22:00:26 +0000111 int linenum = 0;
Matt Kraaideb95f62000-08-06 15:25:53 +0000112 int nmatches = 0;
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000113#if ENABLE_FEATURE_GREP_CONTEXT
Mark Whitley2fd52982001-02-09 00:41:10 +0000114 int print_n_lines_after = 0;
115 int curpos = 0; /* track where we are in the circular 'before' buffer */
116 int idx = 0; /* used for iteration through the circular buffer */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000117#endif /* ENABLE_FEATURE_GREP_CONTEXT */
Mark Whitleyd3721892000-06-28 22:00:26 +0000118
Manuel Novoa III cad53642003-03-19 09:13:01 +0000119 while ((line = bb_get_chomped_line_from_file(file)) != NULL) {
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000120 llist_t *pattern_ptr = pattern_head;
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000121 grep_list_data_t * gl;
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000122
Mark Whitleyd3721892000-06-28 22:00:26 +0000123 linenum++;
Eric Andersen8876fb22003-06-20 09:01:58 +0000124 ret = 0;
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000125 while (pattern_ptr) {
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000126 gl = (grep_list_data_t *)pattern_ptr->data;
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000127 if (FGREP_FLAG) {
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000128 ret = strstr(line, gl->pattern) != NULL;
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000129 } else {
130 /*
131 * test for a postitive-assertion match (regexec returns success (0)
132 * and the user did not specify invert search), or a negative-assertion
133 * match (regexec returns failure (REG_NOMATCH) and the user specified
134 * invert search)
135 */
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000136 if(!(gl->flg_mem_alocated_compiled & COMPILED)) {
137 gl->flg_mem_alocated_compiled |= COMPILED;
138 xregcomp(&(gl->preg), gl->pattern, reflags);
139 }
140 ret |= regexec(&(gl->preg), line, 0, NULL, 0) == 0;
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000141 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000142 pattern_ptr = pattern_ptr->link;
143 } /* while (pattern_ptr) */
Mark Whitleyd3721892000-06-28 22:00:26 +0000144
Eric Andersen8876fb22003-06-20 09:01:58 +0000145 if ((ret ^ invert_search)) {
146
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000147 if (PRINT_FILES_WITH_MATCHES || BE_QUIET)
Eric Andersen8876fb22003-06-20 09:01:58 +0000148 free(line);
149
150 /* if we found a match but were told to be quiet, stop here */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000151 if (BE_QUIET || PRINT_FILES_WITHOUT_MATCHES)
Eric Andersen8876fb22003-06-20 09:01:58 +0000152 return -1;
Mark Whitleyd3721892000-06-28 22:00:26 +0000153
Mark Whitleyfa43e542001-05-24 18:36:18 +0000154 /* keep track of matches */
155 nmatches++;
Mark Whitley1d9d4112001-05-21 21:13:00 +0000156
Mark Whitleyfa43e542001-05-24 18:36:18 +0000157 /* if we're just printing filenames, we stop after the first match */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000158 if (PRINT_FILES_WITH_MATCHES)
Mark Whitleyfa43e542001-05-24 18:36:18 +0000159 break;
Mark Whitley1d9d4112001-05-21 21:13:00 +0000160
Mark Whitleyfa43e542001-05-24 18:36:18 +0000161 /* print the matched line */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000162 if (PRINT_MATCH_COUNTS == 0) {
163#if ENABLE_FEATURE_GREP_CONTEXT
Mark Whitleyfa43e542001-05-24 18:36:18 +0000164 int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
Mark Whitley2fd52982001-02-09 00:41:10 +0000165
Mark Whitleyfa43e542001-05-24 18:36:18 +0000166 /* if we were told to print 'before' lines and there is at least
167 * one line in the circular buffer, print them */
168 if (lines_before && before_buf[prevpos] != NULL) {
169 int first_buf_entry_line_num = linenum - lines_before;
Mark Whitley2fd52982001-02-09 00:41:10 +0000170
Mark Whitleyfa43e542001-05-24 18:36:18 +0000171 /* advance to the first entry in the circular buffer, and
172 * figure out the line number is of the first line in the
173 * buffer */
174 idx = curpos;
175 while (before_buf[idx] == NULL) {
176 idx = (idx + 1) % lines_before;
177 first_buf_entry_line_num++;
178 }
179
180 /* now print each line in the buffer, clearing them as we go */
181 while (before_buf[idx] != NULL) {
182 print_line(before_buf[idx], first_buf_entry_line_num, '-');
183 free(before_buf[idx]);
184 before_buf[idx] = NULL;
185 idx = (idx + 1) % lines_before;
186 first_buf_entry_line_num++;
187 }
Mark Whitley2fd52982001-02-09 00:41:10 +0000188 }
189
Mark Whitleyfa43e542001-05-24 18:36:18 +0000190 /* make a note that we need to print 'after' lines */
191 print_n_lines_after = lines_after;
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000192#endif
Mark Whitleyfa43e542001-05-24 18:36:18 +0000193 print_line(line, linenum, ':');
Mark Whitley2fd52982001-02-09 00:41:10 +0000194 }
Matt Kraai0810f722001-01-04 15:11:52 +0000195 }
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000196#if ENABLE_FEATURE_GREP_CONTEXT
Mark Whitleyfa43e542001-05-24 18:36:18 +0000197 else { /* no match */
198 /* Add the line to the circular 'before' buffer */
199 if(lines_before) {
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000200 free(before_buf[curpos]);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000201 before_buf[curpos] = xstrdup(line);
Mark Whitleyfa43e542001-05-24 18:36:18 +0000202 curpos = (curpos + 1) % lines_before;
203 }
Mark Whitley2fd52982001-02-09 00:41:10 +0000204 }
Mark Whitley2fd52982001-02-09 00:41:10 +0000205
Mark Whitleyfa43e542001-05-24 18:36:18 +0000206 /* if we need to print some context lines after the last match, do so */
207 if (print_n_lines_after && (last_line_printed != linenum)) {
208 print_line(line, linenum, '-');
209 print_n_lines_after--;
210 }
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000211#endif /* ENABLE_FEATURE_GREP_CONTEXT */
Mark Whitleyd3721892000-06-28 22:00:26 +0000212 free(line);
213 }
Mark Whitley8f122432000-07-18 18:37:01 +0000214
Mark Whitley35e59be2001-05-14 19:40:32 +0000215
216 /* special-case file post-processing for options where we don't print line
Mark Whitley1d9d4112001-05-21 21:13:00 +0000217 * matches, just filenames and possibly match counts */
Mark Whitley35e59be2001-05-14 19:40:32 +0000218
Mark Whitley1d9d4112001-05-21 21:13:00 +0000219 /* grep -c: print [filename:]count, even if count is zero */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000220 if (PRINT_MATCH_COUNTS) {
Mike Frysinger5ba5f4d2005-04-16 04:56:11 +0000221 if (print_filename > 0)
Mark Whitley1d9d4112001-05-21 21:13:00 +0000222 printf("%s:", cur_file);
Eric Andersen00344432001-07-31 23:18:49 +0000223 printf("%d\n", nmatches);
Mark Whitley35e59be2001-05-14 19:40:32 +0000224 }
Mark Whitley1d9d4112001-05-21 21:13:00 +0000225
226 /* grep -l: print just the filename, but only if we grepped the line in the file */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000227 if (PRINT_FILES_WITH_MATCHES && nmatches > 0) {
Matt Kraai59df6f72001-05-16 14:21:09 +0000228 puts(cur_file);
Mark Whitley35e59be2001-05-14 19:40:32 +0000229 }
230
Eric Andersendec7f812004-05-26 11:47:55 +0000231 /* 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 +0000232 if (PRINT_FILES_WITHOUT_MATCHES && nmatches == 0) {
Eric Andersendec7f812004-05-26 11:47:55 +0000233 puts(cur_file);
234 }
235
Eric Andersen8876fb22003-06-20 09:01:58 +0000236 return nmatches;
Mark Whitleyd3721892000-06-28 22:00:26 +0000237}
Eric Andersencc8ed391999-10-05 16:24:54 +0000238
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000239#if ENABLE_FEATURE_CLEAN_UP
240#define new_grep_list_data(p, m) add_grep_list_data(p, m)
241static char * add_grep_list_data(char *pattern, int flg_used_mem)
242#else
243#define new_grep_list_data(p, m) add_grep_list_data(p)
244static char * add_grep_list_data(char *pattern)
245#endif
246{
247 grep_list_data_t *gl = xmalloc(sizeof(grep_list_data_t));
248 gl->pattern = pattern;
249#if ENABLE_FEATURE_CLEAN_UP
250 gl->flg_mem_alocated_compiled = flg_used_mem;
251#else
252 gl->flg_mem_alocated_compiled = 0;
253#endif
254 return (char *)gl;
255}
256
257
Eric Andersen8876fb22003-06-20 09:01:58 +0000258static void load_regexes_from_file(llist_t *fopt)
Mark Whitleyfa43e542001-05-24 18:36:18 +0000259{
260 char *line;
Eric Andersen8876fb22003-06-20 09:01:58 +0000261 FILE *f;
262
263 while(fopt) {
264 llist_t *cur = fopt;
265 char *ffile = cur->data;
266
267 fopt = cur->link;
268 free(cur);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000269 f = xfopen(ffile, "r");
Eric Andersen31c27a92004-10-08 08:10:57 +0000270 while ((line = bb_get_chomped_line_from_file(f)) != NULL) {
Rob Landley8bb50782006-05-26 23:44:51 +0000271 llist_add_to(&pattern_head,
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000272 new_grep_list_data(line, PATTERN_MEM_A));
Eric Andersen31c27a92004-10-08 08:10:57 +0000273 }
Mark Whitleyfa43e542001-05-24 18:36:18 +0000274 }
275}
Mark Whitleyfa43e542001-05-24 18:36:18 +0000276
277
Rob Landleydfba7412006-03-06 20:47:33 +0000278int grep_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000279{
Eric Andersen8876fb22003-06-20 09:01:58 +0000280 FILE *file;
281 int matched;
Eric Andersen31c27a92004-10-08 08:10:57 +0000282 llist_t *fopt = NULL;
"Vladimir N. Oleynik"cf40d812005-09-23 13:23:15 +0000283 int error_open_count = 0;
Matt Kraai999623e2001-10-29 15:49:03 +0000284
Mark Whitleyd3721892000-06-28 22:00:26 +0000285 /* do normal option parsing */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000286#if ENABLE_FEATURE_GREP_CONTEXT
Eric Andersen8876fb22003-06-20 09:01:58 +0000287 {
288 char *junk;
289 char *slines_after;
290 char *slines_before;
291 char *Copt;
292
"Vladimir N. Oleynik"f704b272005-10-14 09:56:52 +0000293 bb_opt_complementally = "H-h:e::f::C-AB";
Eric Andersen8876fb22003-06-20 09:01:58 +0000294 opt = bb_getopt_ulflags(argc, argv,
295 GREP_OPTS GREP_OPT_CONTEXT OPT_EGREP,
296 &pattern_head, &fopt,
297 &slines_after, &slines_before, &Copt);
298
299 if(opt & GREP_OPT_C) {
300 /* C option unseted A and B options, but next -A or -B
301 may be ovewrite own option */
302 if(!(opt & GREP_OPT_A)) /* not overwtited */
303 slines_after = Copt;
304 if(!(opt & GREP_OPT_B)) /* not overwtited */
305 slines_before = Copt;
306 opt |= GREP_OPT_A|GREP_OPT_B; /* set for parse now */
Eric Andersen053b1462000-06-13 06:24:53 +0000307 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000308 if(opt & GREP_OPT_A) {
309 lines_after = strtoul(slines_after, &junk, 10);
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000310 if(*junk != '\0')
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000311 bb_error_msg_and_die(bb_msg_invalid_arg, slines_after, "-A");
Eric Andersen8876fb22003-06-20 09:01:58 +0000312 }
313 if(opt & GREP_OPT_B) {
314 lines_before = strtoul(slines_before, &junk, 10);
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000315 if(*junk != '\0')
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000316 bb_error_msg_and_die(bb_msg_invalid_arg, slines_before, "-B");
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000317 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000318 /* sanity checks after parse may be invalid numbers ;-) */
Eric Andersendec7f812004-05-26 11:47:55 +0000319 if ((opt & (GREP_OPT_c|GREP_OPT_q|GREP_OPT_l|GREP_OPT_L))) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000320 opt &= ~GREP_OPT_n;
321 lines_before = 0;
322 lines_after = 0;
323 } else if(lines_before > 0)
Rob Landley081e3842006-08-03 20:07:35 +0000324 before_buf = (char **)xzalloc(lines_before * sizeof(char *));
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000325 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000326#else
327 /* with auto sanity checks */
"Vladimir N. Oleynik"f704b272005-10-14 09:56:52 +0000328 bb_opt_complementally = "H-h:e::f::c-n:q-n:l-n";
Eric Andersen8876fb22003-06-20 09:01:58 +0000329 opt = bb_getopt_ulflags(argc, argv, GREP_OPTS OPT_EGREP,
330 &pattern_head, &fopt);
Eric Andersen8876fb22003-06-20 09:01:58 +0000331#endif
Eric Andersen8876fb22003-06-20 09:01:58 +0000332 invert_search = (opt & GREP_OPT_v) != 0; /* 0 | 1 */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000333
Eric Andersen8876fb22003-06-20 09:01:58 +0000334 if(opt & GREP_OPT_H)
335 print_filename++;
336 if(opt & GREP_OPT_h)
337 print_filename--;
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000338 if (pattern_head != NULL) {
339 /* convert char *argv[] to grep_list_data_t */
340 llist_t *cur;
341
342 for(cur = pattern_head; cur; cur = cur->link)
343 cur->data = new_grep_list_data(cur->data, 0);
344 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000345 if(opt & GREP_OPT_f)
346 load_regexes_from_file(fopt);
347
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000348 if(ENABLE_FEATURE_GREP_FGREP_ALIAS && bb_applet_name[0] == 'f')
349 opt |= GREP_OPT_F;
Mike Frysinger15ca5862005-07-31 22:41:05 +0000350
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000351 if(ENABLE_FEATURE_GREP_EGREP_ALIAS &&
352 (bb_applet_name[0] == 'e' || (opt & GREP_OPT_E)))
Eric Andersen8876fb22003-06-20 09:01:58 +0000353 reflags = REG_EXTENDED | REG_NOSUB;
354 else
Eric Andersen8876fb22003-06-20 09:01:58 +0000355 reflags = REG_NOSUB;
356
357 if(opt & GREP_OPT_i)
358 reflags |= REG_ICASE;
359
360 argv += optind;
361 argc -= optind;
Eric Andersen053b1462000-06-13 06:24:53 +0000362
Mark Whitleyfa43e542001-05-24 18:36:18 +0000363 /* if we didn't get a pattern from a -e and no command file was specified,
364 * argv[optind] should be the pattern. no pattern, no worky */
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000365 if (pattern_head == NULL) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000366 if (*argv == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000367 bb_show_usage();
Mark Whitleyfa43e542001-05-24 18:36:18 +0000368 else {
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000369 char *pattern = new_grep_list_data(*argv++, 0);
370
Rob Landley8bb50782006-05-26 23:44:51 +0000371 llist_add_to(&pattern_head, pattern);
Eric Andersen8876fb22003-06-20 09:01:58 +0000372 argc--;
Mark Whitleyfa43e542001-05-24 18:36:18 +0000373 }
374 }
Mark Whitleyd3721892000-06-28 22:00:26 +0000375
Mark Whitleyfa43e542001-05-24 18:36:18 +0000376 /* argv[(optind)..(argc-1)] should be names of file to grep through. If
Mark Whitleyd3721892000-06-28 22:00:26 +0000377 * there is more than one file to grep, we will print the filenames */
Eric Andersen8876fb22003-06-20 09:01:58 +0000378 if (argc > 1) {
Mark Whitleyd3721892000-06-28 22:00:26 +0000379 print_filename++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000380
Mark Whitley2e1148b2000-06-28 22:59:30 +0000381 /* If no files were specified, or '-' was specified, take input from
382 * stdin. Otherwise, we grep through all the files specified. */
Eric Andersen8876fb22003-06-20 09:01:58 +0000383 } else if (argc == 0) {
384 argc++;
Mark Whitley2ef880b2000-07-18 21:02:06 +0000385 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000386 matched = 0;
387 while (argc--) {
388 cur_file = *argv++;
389 if(!cur_file || (*cur_file == '-' && !cur_file[1])) {
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000390 cur_file = "(standard input)";
Eric Andersen8876fb22003-06-20 09:01:58 +0000391 file = stdin;
392 } else {
Mark Whitleyd3721892000-06-28 22:00:26 +0000393 file = fopen(cur_file, "r");
Eric Andersen8876fb22003-06-20 09:01:58 +0000394 }
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000395 if (file == NULL) {
396 if (!SUPPRESS_ERR_MSGS)
397 bb_perror_msg("%s", cur_file);
398 error_open_count++;
Eric Andersen8876fb22003-06-20 09:01:58 +0000399 } else {
400 matched += grep_file(file);
401 if(matched < 0) {
402 /* we found a match but were told to be quiet, stop here and
403 * return success */
404 break;
Mark Whitley2ef880b2000-07-18 21:02:06 +0000405 }
"Vladimir N. Oleynik"cf40d812005-09-23 13:23:15 +0000406 fclose(file);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000407 }
"Vladimir N. Oleynik"cf40d812005-09-23 13:23:15 +0000408 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000409
Eric Andersen8876fb22003-06-20 09:01:58 +0000410 /* destroy all the elments in the pattern list */
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000411 if (ENABLE_FEATURE_CLEAN_UP) {
412 while (pattern_head) {
413 llist_t *pattern_head_ptr = pattern_head;
414 grep_list_data_t *gl =
415 (grep_list_data_t *)pattern_head_ptr->data;
Eric Andersen8876fb22003-06-20 09:01:58 +0000416
"Vladimir N. Oleynik"716bbe92006-02-28 10:10:19 +0000417 pattern_head = pattern_head->link;
418 if((gl->flg_mem_alocated_compiled & PATTERN_MEM_A))
419 free(gl->pattern);
420 if((gl->flg_mem_alocated_compiled & COMPILED))
421 regfree(&(gl->preg));
422 free(pattern_head_ptr);
423 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000424 }
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000425 /* 0 = success, 1 = failed, 2 = error */
426 /* If the -q option is specified, the exit status shall be zero
427 * if an input line is selected, even if an error was detected. */
428 if(BE_QUIET && matched)
"Vladimir N. Oleynik"bf449742005-09-23 13:50:24 +0000429 return 0;
430 if(error_open_count)
431 return 2;
Mark Whitleyb5c29852001-02-01 21:02:41 +0000432 return !matched; /* invert return value 0 = success, 1 = failed */
Eric Andersencc8ed391999-10-05 16:24:54 +0000433}