2 * Mini grep implementation for busybox using libc regex.
4 * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley
5 * Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Jun 2003 by Vladimir Oleynik <dzo@simtreas.ru> -
24 * correction "-e pattern1 -e -e pattern2" logic and more optimizations.
37 #define GREP_OPTS "lnqvscFiHhe:f:"
39 static char print_files_with_matches;
41 static char print_line_num;
45 typedef char invert_search_t;
46 static invert_search_t invert_search;
48 static char suppress_err_msgs;
50 static char print_match_counts;
52 static char fgrep_flag;
53 #define GREP_OPT_i 128
54 #define GREP_OPT_H 256
55 #define GREP_OPT_h 512
56 #define GREP_OPT_e 1024
57 #define GREP_OPT_f 2048
58 #ifdef CONFIG_FEATURE_GREP_CONTEXT
59 #define GREP_OPT_CONTEXT "A:B:C"
60 #define GREP_OPT_A 4096
61 #define GREP_OPT_B 8192
62 #define GREP_OPT_C 16384
63 #define GREP_OPT_E 32768U
65 #define GREP_OPT_CONTEXT ""
66 #define GREP_OPT_E 4096
68 #ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS
69 # define OPT_EGREP "E"
75 static int print_filename;
77 #ifdef CONFIG_FEATURE_GREP_CONTEXT
78 static int lines_before;
79 static int lines_after;
80 static char **before_buf;
81 static int last_line_printed;
82 #endif /* CONFIG_FEATURE_GREP_CONTEXT */
84 /* globals used internally */
85 static llist_t *pattern_head; /* growable list of patterns to match */
86 static char *cur_file; /* the current file we are reading */
89 static void print_line(const char *line, int linenum, char decoration)
91 #ifdef CONFIG_FEATURE_GREP_CONTEXT
92 /* possibly print the little '--' seperator */
93 if ((lines_before || lines_after) && last_line_printed &&
94 last_line_printed < linenum - 1) {
97 last_line_printed = linenum;
100 printf("%s%c", cur_file, decoration);
102 printf("%i%c", linenum, decoration);
106 extern void xregcomp(regex_t *preg, const char *regex, int cflags);
109 static int grep_file(FILE *file)
115 #ifdef CONFIG_FEATURE_GREP_CONTEXT
116 int print_n_lines_after = 0;
117 int curpos = 0; /* track where we are in the circular 'before' buffer */
118 int idx = 0; /* used for iteration through the circular buffer */
119 #endif /* CONFIG_FEATURE_GREP_CONTEXT */
121 while ((line = bb_get_chomped_line_from_file(file)) != NULL) {
122 llist_t *pattern_ptr = pattern_head;
126 while (pattern_ptr) {
128 ret = strstr(line, pattern_ptr->data) != NULL;
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
137 xregcomp(®ex, pattern_ptr->data, reflags);
138 ret = regexec(®ex, line, 0, NULL, 0) == 0;
143 pattern_ptr = pattern_ptr->link;
144 } /* while (pattern_ptr) */
146 if ((ret ^ invert_search)) {
148 if (print_files_with_matches || be_quiet)
151 /* if we found a match but were told to be quiet, stop here */
155 /* keep track of matches */
158 /* if we're just printing filenames, we stop after the first match */
159 if (print_files_with_matches)
162 /* print the matched line */
163 if (print_match_counts == 0) {
164 #ifdef CONFIG_FEATURE_GREP_CONTEXT
165 int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
167 /* if we were told to print 'before' lines and there is at least
168 * one line in the circular buffer, print them */
169 if (lines_before && before_buf[prevpos] != NULL) {
170 int first_buf_entry_line_num = linenum - lines_before;
172 /* advance to the first entry in the circular buffer, and
173 * figure out the line number is of the first line in the
176 while (before_buf[idx] == NULL) {
177 idx = (idx + 1) % lines_before;
178 first_buf_entry_line_num++;
181 /* now print each line in the buffer, clearing them as we go */
182 while (before_buf[idx] != NULL) {
183 print_line(before_buf[idx], first_buf_entry_line_num, '-');
184 free(before_buf[idx]);
185 before_buf[idx] = NULL;
186 idx = (idx + 1) % lines_before;
187 first_buf_entry_line_num++;
191 /* make a note that we need to print 'after' lines */
192 print_n_lines_after = lines_after;
193 #endif /* CONFIG_FEATURE_GREP_CONTEXT */
194 print_line(line, linenum, ':');
197 #ifdef CONFIG_FEATURE_GREP_CONTEXT
198 else { /* no match */
199 /* Add the line to the circular 'before' buffer */
201 free(before_buf[curpos]);
202 before_buf[curpos] = bb_xstrdup(line);
203 curpos = (curpos + 1) % lines_before;
207 /* if we need to print some context lines after the last match, do so */
208 if (print_n_lines_after && (last_line_printed != linenum)) {
209 print_line(line, linenum, '-');
210 print_n_lines_after--;
212 #endif /* CONFIG_FEATURE_GREP_CONTEXT */
217 /* special-case file post-processing for options where we don't print line
218 * matches, just filenames and possibly match counts */
220 /* grep -c: print [filename:]count, even if count is zero */
221 if (print_match_counts) {
223 printf("%s:", cur_file);
224 printf("%d\n", nmatches);
227 /* grep -l: print just the filename, but only if we grepped the line in the file */
228 if (print_files_with_matches && nmatches > 0) {
235 static void load_regexes_from_file(llist_t *fopt)
242 char *ffile = cur->data;
246 f = bb_xfopen(ffile, "r");
247 while ((line = bb_get_chomped_line_from_file(f)) != NULL) {
248 pattern_head = llist_add_to(pattern_head, line);
254 extern int grep_main(int argc, char **argv)
261 /* do normal option parsing */
262 #ifdef CONFIG_FEATURE_GREP_CONTEXT
269 bb_opt_complementaly = "H-h:e*:f*:C-AB";
270 opt = bb_getopt_ulflags(argc, argv,
271 GREP_OPTS GREP_OPT_CONTEXT OPT_EGREP,
272 &pattern_head, &fopt,
273 &slines_after, &slines_before, &Copt);
275 if(opt & GREP_OPT_C) {
276 /* C option unseted A and B options, but next -A or -B
277 may be ovewrite own option */
278 if(!(opt & GREP_OPT_A)) /* not overwtited */
280 if(!(opt & GREP_OPT_B)) /* not overwtited */
281 slines_before = Copt;
282 opt |= GREP_OPT_A|GREP_OPT_B; /* set for parse now */
284 if(opt & GREP_OPT_A) {
285 lines_after = strtoul(slines_after, &junk, 10);
287 bb_error_msg_and_die("invalid context length argument");
289 if(opt & GREP_OPT_B) {
290 lines_before = strtoul(slines_before, &junk, 10);
292 bb_error_msg_and_die("invalid context length argument");
294 /* sanity checks after parse may be invalid numbers ;-) */
295 if ((opt & (GREP_OPT_c|GREP_OPT_q|GREP_OPT_l))) {
299 } else if(lines_before > 0)
300 before_buf = (char **)xcalloc(lines_before, sizeof(char *));
303 /* with auto sanity checks */
304 bb_opt_complementaly = "H-h:e*:f*:c-n:q-n:l-n";
305 opt = bb_getopt_ulflags(argc, argv, GREP_OPTS OPT_EGREP,
306 &pattern_head, &fopt);
309 print_files_with_matches = opt & GREP_OPT_l;
310 print_line_num = opt & GREP_OPT_n;
311 be_quiet = opt & GREP_OPT_q;
312 invert_search = (opt & GREP_OPT_v) != 0; /* 0 | 1 */
313 suppress_err_msgs = opt & GREP_OPT_s;
314 print_match_counts = opt & GREP_OPT_c;
315 fgrep_flag = opt & GREP_OPT_F;
321 load_regexes_from_file(fopt);
323 #ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS
324 if(bb_applet_name[0] == 'e' || (opt & GREP_OPT_E))
325 reflags = REG_EXTENDED | REG_NOSUB;
331 reflags |= REG_ICASE;
336 /* if we didn't get a pattern from a -e and no command file was specified,
337 * argv[optind] should be the pattern. no pattern, no worky */
338 if (pattern_head == NULL) {
342 pattern_head = llist_add_to(pattern_head, *argv++);
347 /* argv[(optind)..(argc-1)] should be names of file to grep through. If
348 * there is more than one file to grep, we will print the filenames */
352 /* If no files were specified, or '-' was specified, take input from
353 * stdin. Otherwise, we grep through all the files specified. */
354 } else if (argc == 0) {
360 if(!cur_file || (*cur_file == '-' && !cur_file[1])) {
364 file = fopen(cur_file, "r");
367 if (!suppress_err_msgs)
368 bb_perror_msg("%s", cur_file);
370 matched += grep_file(file);
372 /* we found a match but were told to be quiet, stop here and
380 #ifdef CONFIG_FEATURE_CLEAN_UP
381 /* destroy all the elments in the pattern list */
382 while (pattern_head) {
383 llist_t *pattern_head_ptr = pattern_head;
385 pattern_head = pattern_head->link;
386 free(pattern_head_ptr);
390 return !matched; /* invert return value 0 = success, 1 = failed */