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
27 #include <string.h> /* for strerror() */
32 extern int optind; /* in unistd.h */
33 extern void xregcomp(regex_t *preg, const char *regex, int cflags); /* in busybox.h */
36 static int reflags = REG_NOSUB;
37 static int print_filename = 0;
38 static int print_line_num = 0;
39 static int print_match_counts = 0;
40 static int be_quiet = 0;
41 static int invert_search = 0;
42 static int suppress_err_msgs = 0;
43 static int print_files_with_matches = 0;
44 static int fgrep_flag = 0;
46 #ifdef CONFIG_FEATURE_GREP_CONTEXT
47 extern char *optarg; /* in getopt.h */
48 static int lines_before = 0;
49 static int lines_after = 0;
50 static char **before_buf = NULL;
51 static int last_line_printed = 0;
52 #endif /* CONFIG_FEATURE_GREP_CONTEXT */
54 /* globals used internally */
55 static llist_t *pattern_head = NULL; /* growable list of patterns to match */
56 static int matched; /* keeps track of whether we ever matched */
57 static char *cur_file = NULL; /* the current file we are reading */
60 static void print_line(const char *line, int linenum, char decoration)
62 #ifdef CONFIG_FEATURE_GREP_CONTEXT
63 /* possibly print the little '--' seperator */
64 if ((lines_before || lines_after) && last_line_printed &&
65 last_line_printed < linenum - 1) {
68 last_line_printed = linenum;
71 printf("%s%c", cur_file, decoration);
73 printf("%i%c", linenum, decoration);
78 static void grep_file(FILE *file)
84 #ifdef CONFIG_FEATURE_GREP_CONTEXT
85 int print_n_lines_after = 0;
86 int curpos = 0; /* track where we are in the circular 'before' buffer */
87 int idx = 0; /* used for iteration through the circular buffer */
88 #endif /* CONFIG_FEATURE_GREP_CONTEXT */
90 while ((line = bb_get_chomped_line_from_file(file)) != NULL) {
91 llist_t *pattern_ptr = pattern_head;
97 if (strstr(line, pattern_ptr->data)) {
105 * test for a postitive-assertion match (regexec returns success (0)
106 * and the user did not specify invert search), or a negative-assertion
107 * match (regexec returns failure (REG_NOMATCH) and the user specified
111 xregcomp(®ex, pattern_ptr->data, reflags);
112 ret = regexec(®ex, line, 0, NULL, 0);
115 if ((ret == 0 && !invert_search) || (ret == REG_NOMATCH && invert_search)) {
117 /* if we found a match but were told to be quiet, stop here and
122 /* keep track of matches */
125 /* if we're just printing filenames, we stop after the first match */
126 if (print_files_with_matches)
129 /* print the matched line */
130 if (print_match_counts == 0) {
131 #ifdef CONFIG_FEATURE_GREP_CONTEXT
132 int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
134 /* if we were told to print 'before' lines and there is at least
135 * one line in the circular buffer, print them */
136 if (lines_before && before_buf[prevpos] != NULL) {
137 int first_buf_entry_line_num = linenum - lines_before;
139 /* advance to the first entry in the circular buffer, and
140 * figure out the line number is of the first line in the
143 while (before_buf[idx] == NULL) {
144 idx = (idx + 1) % lines_before;
145 first_buf_entry_line_num++;
148 /* now print each line in the buffer, clearing them as we go */
149 while (before_buf[idx] != NULL) {
150 print_line(before_buf[idx], first_buf_entry_line_num, '-');
151 free(before_buf[idx]);
152 before_buf[idx] = NULL;
153 idx = (idx + 1) % lines_before;
154 first_buf_entry_line_num++;
158 /* make a note that we need to print 'after' lines */
159 print_n_lines_after = lines_after;
160 #endif /* CONFIG_FEATURE_GREP_CONTEXT */
161 print_line(line, linenum, ':');
164 #ifdef CONFIG_FEATURE_GREP_CONTEXT
165 else { /* no match */
166 /* Add the line to the circular 'before' buffer */
168 free(before_buf[curpos]);
169 before_buf[curpos] = bb_xstrdup(line);
170 curpos = (curpos + 1) % lines_before;
174 /* if we need to print some context lines after the last match, do so */
175 if (print_n_lines_after && (last_line_printed != linenum)) {
176 print_line(line, linenum, '-');
177 print_n_lines_after--;
179 #endif /* CONFIG_FEATURE_GREP_CONTEXT */
180 pattern_ptr = pattern_ptr->link;
186 /* special-case file post-processing for options where we don't print line
187 * matches, just filenames and possibly match counts */
189 /* grep -c: print [filename:]count, even if count is zero */
190 if (print_match_counts) {
192 printf("%s:", cur_file);
193 if (print_files_with_matches && nmatches > 0)
196 printf("%d\n", nmatches);
199 /* grep -l: print just the filename, but only if we grepped the line in the file */
200 if (print_files_with_matches && nmatches > 0) {
205 /* remember if we matched */
211 static void add_pattern(char *restr)
213 // regexes = xrealloc(regexes, sizeof(regex_t) * (++nregexes));
214 // xregcomp(®exes[nregexes-1], restr, reflags);
215 pattern_head = llist_add_to(pattern_head, restr);
219 static void load_regexes_from_file(const char *filename)
222 FILE *f = bb_xfopen(filename, "r");
223 while ((line = bb_get_chomped_line_from_file(f)) != NULL) {
224 pattern_head = llist_add_to(pattern_head, line);
229 #ifdef CONFIG_FEATURE_CLEAN_UP
230 static void destroy_regexes(void)
232 llist_t *pattern_head_ptr;
234 if (pattern_head == NULL)
237 /* destroy all the elments in the pattern list */
238 while (pattern_head) {
239 pattern_head_ptr = pattern_head;
240 pattern_head = pattern_head->link;
241 free(pattern_head_ptr->data);
242 free(pattern_head_ptr);
248 extern int grep_main(int argc, char **argv)
251 #if defined (CONFIG_FEATURE_GREP_CONTEXT)
255 #ifdef CONFIG_FEATURE_CLEAN_UP
256 /* destroy command strings on exit */
257 atexit(destroy_regexes);
260 #ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS
261 if (strcmp(bb_get_last_path_component(argv[0]), "egrep") == 0)
262 reflags |= REG_EXTENDED;
265 /* do normal option parsing */
266 while ((opt = getopt(argc, argv, "iHhlnqvsce:f:F"
267 #ifdef CONFIG_FEATURE_GREP_CONTEXT
270 #ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS
276 reflags |= REG_ICASE;
279 print_files_with_matches++;
300 print_match_counts++;
303 pattern_head = llist_add_to(pattern_head, strdup(optarg));
305 #ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS
307 reflags |= REG_EXTENDED;
314 load_regexes_from_file(optarg);
316 #ifdef CONFIG_FEATURE_GREP_CONTEXT
318 lines_after = strtoul(optarg, &junk, 10);
320 bb_error_msg_and_die("invalid context length argument");
323 lines_before = strtoul(optarg, &junk, 10);
325 bb_error_msg_and_die("invalid context length argument");
326 before_buf = (char **)xcalloc(lines_before, sizeof(char *));
329 lines_after = lines_before = strtoul(optarg, &junk, 10);
331 bb_error_msg_and_die("invalid context length argument");
332 before_buf = (char **)xcalloc(lines_before, sizeof(char *));
334 #endif /* CONFIG_FEATURE_GREP_CONTEXT */
340 /* if we didn't get a pattern from a -e and no command file was specified,
341 * argv[optind] should be the pattern. no pattern, no worky */
342 if (pattern_head == NULL) {
343 if (argv[optind] == NULL)
346 pattern_head = llist_add_to(pattern_head, strdup(argv[optind]));
352 if (print_match_counts || be_quiet || print_files_with_matches) {
354 #ifdef CONFIG_FEATURE_GREP_CONTEXT
360 /* argv[(optind)..(argc-1)] should be names of file to grep through. If
361 * there is more than one file to grep, we will print the filenames */
362 if ((argc-1) - (optind) > 0)
365 /* If no files were specified, or '-' was specified, take input from
366 * stdin. Otherwise, we grep through all the files specified. */
367 if (argv[optind] == NULL || (strcmp(argv[optind], "-") == 0)) {
373 for (i = optind; i < argc; i++) {
375 file = fopen(cur_file, "r");
377 if (!suppress_err_msgs)
378 bb_perror_msg("%s", cur_file);
387 return !matched; /* invert return value 0 = success, 1 = failed */