blob: 3c5167e2858f107ec225b0f441837e7b6903a1f8 [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/*
Eric Andersen7f164cd2004-05-26 09:46:41 +000014 * Apr 2004 by Vladimir Oleynik <dzo@simtreas.ru> -
15 * correction "-e pattern1 -e pattern2" logic and more optimizations.
Eric Andersen8876fb22003-06-20 09:01:58 +000016*/
Eric Andersencc8ed391999-10-05 16:24:54 +000017
Eric Andersencc8ed391999-10-05 16:24:54 +000018#include <stdio.h>
Mark Whitleyd3721892000-06-28 22:00:26 +000019#include <stdlib.h>
Eric Andersene9b527a2000-07-09 05:56:14 +000020#include <getopt.h>
Eric Andersen8876fb22003-06-20 09:01:58 +000021#include <string.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000022#include <errno.h>
Eric Andersen3570a342000-09-25 21:45:58 +000023#include "busybox.h"
"Vladimir N. Oleynik"23f62fc2005-09-14 16:59:11 +000024#include "xregex.h"
Mark Whitleyd3721892000-06-28 22:00:26 +000025
Eric Andersened3ef502001-01-27 08:24:39 +000026
Mark Whitleyd3721892000-06-28 22:00:26 +000027/* options */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000028static unsigned long opt;
Eric Andersendec7f812004-05-26 11:47:55 +000029#define GREP_OPTS "lnqvscFiHhe:f:L"
Eric Andersenabc513a2004-05-26 11:48:29 +000030#define GREP_OPT_l (1<<0)
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000031#define PRINT_FILES_WITH_MATCHES (opt & GREP_OPT_l)
Eric Andersenabc513a2004-05-26 11:48:29 +000032#define GREP_OPT_n (1<<1)
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000033#define PRINT_LINE_NUM (opt & GREP_OPT_n)
Eric Andersenabc513a2004-05-26 11:48:29 +000034#define GREP_OPT_q (1<<2)
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000035#define BE_QUIET (opt & GREP_OPT_q)
Eric Andersenabc513a2004-05-26 11:48:29 +000036#define GREP_OPT_v (1<<3)
Eric Andersen8876fb22003-06-20 09:01:58 +000037typedef char invert_search_t;
38static invert_search_t invert_search;
Eric Andersenabc513a2004-05-26 11:48:29 +000039#define GREP_OPT_s (1<<4)
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000040#define SUPPRESS_ERR_MSGS (opt & GREP_OPT_s)
Eric Andersenabc513a2004-05-26 11:48:29 +000041#define GREP_OPT_c (1<<5)
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000042#define PRINT_MATCH_COUNTS (opt & GREP_OPT_c)
Eric Andersenabc513a2004-05-26 11:48:29 +000043#define GREP_OPT_F (1<<6)
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000044#define FGREP_FLAG (opt & GREP_OPT_F)
Eric Andersenabc513a2004-05-26 11:48:29 +000045#define GREP_OPT_i (1<<7)
46#define GREP_OPT_H (1<<8)
47#define GREP_OPT_h (1<<9)
48#define GREP_OPT_e (1<<10)
49#define GREP_OPT_f (1<<11)
50#define GREP_OPT_L (1<<12)
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000051#define PRINT_FILES_WITHOUT_MATCHES ((opt & GREP_OPT_L) != 0)
52#if ENABLE_FEATURE_GREP_CONTEXT
Eric Andersen8876fb22003-06-20 09:01:58 +000053#define GREP_OPT_CONTEXT "A:B:C"
Eric Andersenabc513a2004-05-26 11:48:29 +000054#define GREP_OPT_A (1<<13)
55#define GREP_OPT_B (1<<14)
56#define GREP_OPT_C (1<<15)
57#define GREP_OPT_E (1<<16)
Eric Andersen8876fb22003-06-20 09:01:58 +000058#else
59#define GREP_OPT_CONTEXT ""
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000060#define GREP_OPT_A (0)
61#define GREP_OPT_B (0)
62#define GREP_OPT_C (0)
Eric Andersenabc513a2004-05-26 11:48:29 +000063#define GREP_OPT_E (1<<13)
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
Mark Whitleyfa43e542001-05-24 18:36:18 +000085
Mark Whitley2fd52982001-02-09 00:41:10 +000086static void print_line(const char *line, int linenum, char decoration)
87{
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000088#if ENABLE_FEATURE_GREP_CONTEXT
Eric Andersenaff114c2004-04-14 17:51:38 +000089 /* possibly print the little '--' separator */
Matt Kraaiedc80652001-05-22 14:29:27 +000090 if ((lines_before || lines_after) && last_line_printed &&
91 last_line_printed < linenum - 1) {
Mark Whitley2fd52982001-02-09 00:41:10 +000092 puts("--");
93 }
94 last_line_printed = linenum;
95#endif
Mike Frysinger5ba5f4d2005-04-16 04:56:11 +000096 if (print_filename > 0)
Mark Whitley2fd52982001-02-09 00:41:10 +000097 printf("%s%c", cur_file, decoration);
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +000098 if (PRINT_LINE_NUM)
Mark Whitley2fd52982001-02-09 00:41:10 +000099 printf("%i%c", linenum, decoration);
100 puts(line);
101}
Mark Whitleyd3721892000-06-28 22:00:26 +0000102
Eric Andersen8876fb22003-06-20 09:01:58 +0000103
104static int grep_file(FILE *file)
Mark Whitleyd3721892000-06-28 22:00:26 +0000105{
Eric Andersen8876fb22003-06-20 09:01:58 +0000106 char *line;
107 invert_search_t ret;
Mark Whitleyd3721892000-06-28 22:00:26 +0000108 int linenum = 0;
Matt Kraaideb95f62000-08-06 15:25:53 +0000109 int nmatches = 0;
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000110#if ENABLE_FEATURE_GREP_CONTEXT
Mark Whitley2fd52982001-02-09 00:41:10 +0000111 int print_n_lines_after = 0;
112 int curpos = 0; /* track where we are in the circular 'before' buffer */
113 int idx = 0; /* used for iteration through the circular buffer */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000114#endif /* ENABLE_FEATURE_GREP_CONTEXT */
Mark Whitleyd3721892000-06-28 22:00:26 +0000115
Manuel Novoa III cad53642003-03-19 09:13:01 +0000116 while ((line = bb_get_chomped_line_from_file(file)) != NULL) {
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000117 llist_t *pattern_ptr = pattern_head;
118
Mark Whitleyd3721892000-06-28 22:00:26 +0000119 linenum++;
Eric Andersen8876fb22003-06-20 09:01:58 +0000120 ret = 0;
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000121 while (pattern_ptr) {
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000122 if (FGREP_FLAG) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000123 ret = strstr(line, pattern_ptr->data) != NULL;
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000124 } else {
125 /*
126 * test for a postitive-assertion match (regexec returns success (0)
127 * and the user did not specify invert search), or a negative-assertion
128 * match (regexec returns failure (REG_NOMATCH) and the user specified
129 * invert search)
130 */
131 regex_t regex;
132 xregcomp(&regex, pattern_ptr->data, reflags);
Eric Andersen7f164cd2004-05-26 09:46:41 +0000133 ret |= regexec(&regex, line, 0, NULL, 0) == 0;
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000134 regfree(&regex);
135 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000136 pattern_ptr = pattern_ptr->link;
137 } /* while (pattern_ptr) */
Mark Whitleyd3721892000-06-28 22:00:26 +0000138
Eric Andersen8876fb22003-06-20 09:01:58 +0000139 if ((ret ^ invert_search)) {
140
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000141 if (PRINT_FILES_WITH_MATCHES || BE_QUIET)
Eric Andersen8876fb22003-06-20 09:01:58 +0000142 free(line);
143
144 /* if we found a match but were told to be quiet, stop here */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000145 if (BE_QUIET || PRINT_FILES_WITHOUT_MATCHES)
Eric Andersen8876fb22003-06-20 09:01:58 +0000146 return -1;
Mark Whitleyd3721892000-06-28 22:00:26 +0000147
Mark Whitleyfa43e542001-05-24 18:36:18 +0000148 /* keep track of matches */
149 nmatches++;
Mark Whitley1d9d4112001-05-21 21:13:00 +0000150
Mark Whitleyfa43e542001-05-24 18:36:18 +0000151 /* if we're just printing filenames, we stop after the first match */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000152 if (PRINT_FILES_WITH_MATCHES)
Mark Whitleyfa43e542001-05-24 18:36:18 +0000153 break;
Mark Whitley1d9d4112001-05-21 21:13:00 +0000154
Mark Whitleyfa43e542001-05-24 18:36:18 +0000155 /* print the matched line */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000156 if (PRINT_MATCH_COUNTS == 0) {
157#if ENABLE_FEATURE_GREP_CONTEXT
Mark Whitleyfa43e542001-05-24 18:36:18 +0000158 int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
Mark Whitley2fd52982001-02-09 00:41:10 +0000159
Mark Whitleyfa43e542001-05-24 18:36:18 +0000160 /* if we were told to print 'before' lines and there is at least
161 * one line in the circular buffer, print them */
162 if (lines_before && before_buf[prevpos] != NULL) {
163 int first_buf_entry_line_num = linenum - lines_before;
Mark Whitley2fd52982001-02-09 00:41:10 +0000164
Mark Whitleyfa43e542001-05-24 18:36:18 +0000165 /* advance to the first entry in the circular buffer, and
166 * figure out the line number is of the first line in the
167 * buffer */
168 idx = curpos;
169 while (before_buf[idx] == NULL) {
170 idx = (idx + 1) % lines_before;
171 first_buf_entry_line_num++;
172 }
173
174 /* now print each line in the buffer, clearing them as we go */
175 while (before_buf[idx] != NULL) {
176 print_line(before_buf[idx], first_buf_entry_line_num, '-');
177 free(before_buf[idx]);
178 before_buf[idx] = NULL;
179 idx = (idx + 1) % lines_before;
180 first_buf_entry_line_num++;
181 }
Mark Whitley2fd52982001-02-09 00:41:10 +0000182 }
183
Mark Whitleyfa43e542001-05-24 18:36:18 +0000184 /* make a note that we need to print 'after' lines */
185 print_n_lines_after = lines_after;
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000186#endif
Mark Whitleyfa43e542001-05-24 18:36:18 +0000187 print_line(line, linenum, ':');
Mark Whitley2fd52982001-02-09 00:41:10 +0000188 }
Matt Kraai0810f722001-01-04 15:11:52 +0000189 }
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000190#if ENABLE_FEATURE_GREP_CONTEXT
Mark Whitleyfa43e542001-05-24 18:36:18 +0000191 else { /* no match */
192 /* Add the line to the circular 'before' buffer */
193 if(lines_before) {
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000194 free(before_buf[curpos]);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000195 before_buf[curpos] = bb_xstrdup(line);
Mark Whitleyfa43e542001-05-24 18:36:18 +0000196 curpos = (curpos + 1) % lines_before;
197 }
Mark Whitley2fd52982001-02-09 00:41:10 +0000198 }
Mark Whitley2fd52982001-02-09 00:41:10 +0000199
Mark Whitleyfa43e542001-05-24 18:36:18 +0000200 /* if we need to print some context lines after the last match, do so */
201 if (print_n_lines_after && (last_line_printed != linenum)) {
202 print_line(line, linenum, '-');
203 print_n_lines_after--;
204 }
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000205#endif /* ENABLE_FEATURE_GREP_CONTEXT */
Mark Whitleyd3721892000-06-28 22:00:26 +0000206 free(line);
207 }
Mark Whitley8f122432000-07-18 18:37:01 +0000208
Mark Whitley35e59be2001-05-14 19:40:32 +0000209
210 /* special-case file post-processing for options where we don't print line
Mark Whitley1d9d4112001-05-21 21:13:00 +0000211 * matches, just filenames and possibly match counts */
Mark Whitley35e59be2001-05-14 19:40:32 +0000212
Mark Whitley1d9d4112001-05-21 21:13:00 +0000213 /* grep -c: print [filename:]count, even if count is zero */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000214 if (PRINT_MATCH_COUNTS) {
Mike Frysinger5ba5f4d2005-04-16 04:56:11 +0000215 if (print_filename > 0)
Mark Whitley1d9d4112001-05-21 21:13:00 +0000216 printf("%s:", cur_file);
Eric Andersen00344432001-07-31 23:18:49 +0000217 printf("%d\n", nmatches);
Mark Whitley35e59be2001-05-14 19:40:32 +0000218 }
Mark Whitley1d9d4112001-05-21 21:13:00 +0000219
220 /* grep -l: print just the filename, but only if we grepped the line in the file */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000221 if (PRINT_FILES_WITH_MATCHES && nmatches > 0) {
Matt Kraai59df6f72001-05-16 14:21:09 +0000222 puts(cur_file);
Mark Whitley35e59be2001-05-14 19:40:32 +0000223 }
224
Eric Andersendec7f812004-05-26 11:47:55 +0000225 /* 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 +0000226 if (PRINT_FILES_WITHOUT_MATCHES && nmatches == 0) {
Eric Andersendec7f812004-05-26 11:47:55 +0000227 puts(cur_file);
228 }
229
Eric Andersen8876fb22003-06-20 09:01:58 +0000230 return nmatches;
Mark Whitleyd3721892000-06-28 22:00:26 +0000231}
Eric Andersencc8ed391999-10-05 16:24:54 +0000232
Eric Andersen8876fb22003-06-20 09:01:58 +0000233static void load_regexes_from_file(llist_t *fopt)
Mark Whitleyfa43e542001-05-24 18:36:18 +0000234{
235 char *line;
Eric Andersen8876fb22003-06-20 09:01:58 +0000236 FILE *f;
237
238 while(fopt) {
239 llist_t *cur = fopt;
240 char *ffile = cur->data;
241
242 fopt = cur->link;
243 free(cur);
244 f = bb_xfopen(ffile, "r");
Eric Andersen31c27a92004-10-08 08:10:57 +0000245 while ((line = bb_get_chomped_line_from_file(f)) != NULL) {
246 pattern_head = llist_add_to(pattern_head, line);
247 }
Mark Whitleyfa43e542001-05-24 18:36:18 +0000248 }
249}
Mark Whitleyfa43e542001-05-24 18:36:18 +0000250
251
Erik Andersene49d5ec2000-02-08 19:58:47 +0000252extern int grep_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000253{
Eric Andersen8876fb22003-06-20 09:01:58 +0000254 FILE *file;
255 int matched;
Eric Andersen31c27a92004-10-08 08:10:57 +0000256 llist_t *fopt = NULL;
"Vladimir N. Oleynik"cf40d812005-09-23 13:23:15 +0000257 int error_open_count = 0;
Matt Kraai999623e2001-10-29 15:49:03 +0000258
Mark Whitleyd3721892000-06-28 22:00:26 +0000259 /* do normal option parsing */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000260#if ENABLE_FEATURE_GREP_CONTEXT
Eric Andersen8876fb22003-06-20 09:01:58 +0000261 {
262 char *junk;
263 char *slines_after;
264 char *slines_before;
265 char *Copt;
266
"Vladimir N. Oleynik"f704b272005-10-14 09:56:52 +0000267 bb_opt_complementally = "H-h:e::f::C-AB";
Eric Andersen8876fb22003-06-20 09:01:58 +0000268 opt = bb_getopt_ulflags(argc, argv,
269 GREP_OPTS GREP_OPT_CONTEXT OPT_EGREP,
270 &pattern_head, &fopt,
271 &slines_after, &slines_before, &Copt);
272
273 if(opt & GREP_OPT_C) {
274 /* C option unseted A and B options, but next -A or -B
275 may be ovewrite own option */
276 if(!(opt & GREP_OPT_A)) /* not overwtited */
277 slines_after = Copt;
278 if(!(opt & GREP_OPT_B)) /* not overwtited */
279 slines_before = Copt;
280 opt |= GREP_OPT_A|GREP_OPT_B; /* set for parse now */
Eric Andersen053b1462000-06-13 06:24:53 +0000281 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000282 if(opt & GREP_OPT_A) {
283 lines_after = strtoul(slines_after, &junk, 10);
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000284 if(*junk != '\0')
285 bb_error_msg_and_die("invalid context length argument");
Eric Andersen8876fb22003-06-20 09:01:58 +0000286 }
287 if(opt & GREP_OPT_B) {
288 lines_before = strtoul(slines_before, &junk, 10);
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000289 if(*junk != '\0')
290 bb_error_msg_and_die("invalid context length argument");
291 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000292 /* sanity checks after parse may be invalid numbers ;-) */
Eric Andersendec7f812004-05-26 11:47:55 +0000293 if ((opt & (GREP_OPT_c|GREP_OPT_q|GREP_OPT_l|GREP_OPT_L))) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000294 opt &= ~GREP_OPT_n;
295 lines_before = 0;
296 lines_after = 0;
297 } else if(lines_before > 0)
298 before_buf = (char **)xcalloc(lines_before, sizeof(char *));
299 }
300#else
301 /* with auto sanity checks */
"Vladimir N. Oleynik"f704b272005-10-14 09:56:52 +0000302 bb_opt_complementally = "H-h:e::f::c-n:q-n:l-n";
Eric Andersen8876fb22003-06-20 09:01:58 +0000303 opt = bb_getopt_ulflags(argc, argv, GREP_OPTS OPT_EGREP,
304 &pattern_head, &fopt);
Eric Andersen8876fb22003-06-20 09:01:58 +0000305#endif
Eric Andersen8876fb22003-06-20 09:01:58 +0000306 invert_search = (opt & GREP_OPT_v) != 0; /* 0 | 1 */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000307
Eric Andersen8876fb22003-06-20 09:01:58 +0000308 if(opt & GREP_OPT_H)
309 print_filename++;
310 if(opt & GREP_OPT_h)
311 print_filename--;
312 if(opt & GREP_OPT_f)
313 load_regexes_from_file(fopt);
314
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000315 if(ENABLE_FEATURE_GREP_FGREP_ALIAS && bb_applet_name[0] == 'f')
316 opt |= GREP_OPT_F;
Mike Frysinger15ca5862005-07-31 22:41:05 +0000317
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000318 if(ENABLE_FEATURE_GREP_EGREP_ALIAS &&
319 (bb_applet_name[0] == 'e' || (opt & GREP_OPT_E)))
Eric Andersen8876fb22003-06-20 09:01:58 +0000320 reflags = REG_EXTENDED | REG_NOSUB;
321 else
Eric Andersen8876fb22003-06-20 09:01:58 +0000322 reflags = REG_NOSUB;
323
324 if(opt & GREP_OPT_i)
325 reflags |= REG_ICASE;
326
327 argv += optind;
328 argc -= optind;
Eric Andersen053b1462000-06-13 06:24:53 +0000329
Mark Whitleyfa43e542001-05-24 18:36:18 +0000330 /* if we didn't get a pattern from a -e and no command file was specified,
331 * argv[optind] should be the pattern. no pattern, no worky */
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000332 if (pattern_head == NULL) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000333 if (*argv == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000334 bb_show_usage();
Mark Whitleyfa43e542001-05-24 18:36:18 +0000335 else {
Eric Andersen8876fb22003-06-20 09:01:58 +0000336 pattern_head = llist_add_to(pattern_head, *argv++);
337 argc--;
Mark Whitleyfa43e542001-05-24 18:36:18 +0000338 }
339 }
Mark Whitleyd3721892000-06-28 22:00:26 +0000340
Mark Whitleyfa43e542001-05-24 18:36:18 +0000341 /* argv[(optind)..(argc-1)] should be names of file to grep through. If
Mark Whitleyd3721892000-06-28 22:00:26 +0000342 * there is more than one file to grep, we will print the filenames */
Eric Andersen8876fb22003-06-20 09:01:58 +0000343 if (argc > 1) {
Mark Whitleyd3721892000-06-28 22:00:26 +0000344 print_filename++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000345
Mark Whitley2e1148b2000-06-28 22:59:30 +0000346 /* If no files were specified, or '-' was specified, take input from
347 * stdin. Otherwise, we grep through all the files specified. */
Eric Andersen8876fb22003-06-20 09:01:58 +0000348 } else if (argc == 0) {
349 argc++;
Mark Whitley2ef880b2000-07-18 21:02:06 +0000350 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000351 matched = 0;
352 while (argc--) {
353 cur_file = *argv++;
354 if(!cur_file || (*cur_file == '-' && !cur_file[1])) {
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000355 cur_file = "(standard input)";
Eric Andersen8876fb22003-06-20 09:01:58 +0000356 file = stdin;
357 } else {
Mark Whitleyd3721892000-06-28 22:00:26 +0000358 file = fopen(cur_file, "r");
Eric Andersen8876fb22003-06-20 09:01:58 +0000359 }
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000360 if (file == NULL) {
361 if (!SUPPRESS_ERR_MSGS)
362 bb_perror_msg("%s", cur_file);
363 error_open_count++;
Eric Andersen8876fb22003-06-20 09:01:58 +0000364 } else {
365 matched += grep_file(file);
366 if(matched < 0) {
367 /* we found a match but were told to be quiet, stop here and
368 * return success */
369 break;
Mark Whitley2ef880b2000-07-18 21:02:06 +0000370 }
"Vladimir N. Oleynik"cf40d812005-09-23 13:23:15 +0000371 fclose(file);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000372 }
"Vladimir N. Oleynik"cf40d812005-09-23 13:23:15 +0000373 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000374
Eric Andersen8876fb22003-06-20 09:01:58 +0000375 /* destroy all the elments in the pattern list */
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000376 if (ENABLE_FEATURE_CLEAN_UP)
Eric Andersen8876fb22003-06-20 09:01:58 +0000377 while (pattern_head) {
378 llist_t *pattern_head_ptr = pattern_head;
379
380 pattern_head = pattern_head->link;
381 free(pattern_head_ptr);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000382 }
Mark Whitleyd3721892000-06-28 22:00:26 +0000383
Bernhard Reutner-Fischeraaf0e232005-09-23 15:38:49 +0000384 /* 0 = success, 1 = failed, 2 = error */
385 /* If the -q option is specified, the exit status shall be zero
386 * if an input line is selected, even if an error was detected. */
387 if(BE_QUIET && matched)
"Vladimir N. Oleynik"bf449742005-09-23 13:50:24 +0000388 return 0;
389 if(error_open_count)
390 return 2;
Mark Whitleyb5c29852001-02-01 21:02:41 +0000391 return !matched; /* invert return value 0 = success, 1 = failed */
Eric Andersencc8ed391999-10-05 16:24:54 +0000392}