2 * Mini grep implementation for busybox using libc regex.
4 * Copyright (C) 1999,2000,2001 by Lineo, inc.
5 * Written by Mark Whitley <markw@lineo.com>, <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 int errno; /* for use with strerror() */
34 extern void xregcomp(regex_t *preg, const char *regex, int cflags); /* in busybox.h */
37 static int ignore_case = 0;
38 static int print_filename = 0;
39 static int print_line_num = 0;
40 static int print_match_counts = 0;
41 static int be_quiet = 0;
42 static int invert_search = 0;
43 static int suppress_err_msgs = 0;
44 static int print_files_with_matches = 0;
46 #ifdef BB_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 /* BB_FEATURE_GREP_CONTEXT */
54 /* globals used internally */
55 static regex_t regex; /* storage space for compiled regular expression */
56 static int matched; /* keeps track of whether we ever matched */
57 static char *cur_file = NULL; /* the current file we are reading */
59 static void print_line(const char *line, int linenum, char decoration)
61 #ifdef BB_FEATURE_GREP_CONTEXT
62 /* possibly print the little '--' seperator */
63 if (last_line_printed && last_line_printed < linenum - 1) {
66 last_line_printed = linenum;
69 printf("%s%c", cur_file, decoration);
71 printf("%i%c", linenum, decoration);
75 static void grep_file(FILE *file)
81 #ifdef BB_FEATURE_GREP_CONTEXT
82 int print_n_lines_after = 0;
83 int curpos = 0; /* track where we are in the circular 'before' buffer */
84 int idx = 0; /* used for iteration through the circular buffer */
85 #endif /* BB_FEATURE_GREP_CONTEXT */
87 while ((line = get_line_from_file(file)) != NULL) {
92 * test for a postitive-assertion match (regexec returns success (0)
93 * and the user did not specify invert search), or a negative-assertion
94 * match (regexec returns failure (REG_NOMATCH) and the user specified
97 ret = regexec(®ex, line, 0, NULL, 0);
98 if ((ret == 0 && !invert_search) || (ret == REG_NOMATCH && invert_search)) {
100 /* if we found a match but were told to be quiet, stop here and
107 /* keep track of matches */
110 /* if we're just printing filenames, we stop after the first match */
111 if (print_files_with_matches)
114 /* print the matched line */
115 if (print_match_counts == 0) {
116 #ifdef BB_FEATURE_GREP_CONTEXT
117 int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
119 /* if we were told to print 'before' lines and there is at least
120 * one line in the circular buffer, print them */
121 if (lines_before && before_buf[prevpos] != NULL) {
122 int first_buf_entry_line_num = linenum - lines_before;
124 /* advance to the first entry in the circular buffer, and
125 * figure out the line number is of the first line in the
128 while (before_buf[idx] == NULL) {
129 idx = (idx + 1) % lines_before;
130 first_buf_entry_line_num++;
133 /* now print each line in the buffer, clearing them as we go */
134 while (before_buf[idx] != NULL) {
135 print_line(before_buf[idx], first_buf_entry_line_num, '-');
136 free(before_buf[idx]);
137 before_buf[idx] = NULL;
138 idx = (idx + 1) % lines_before;
139 first_buf_entry_line_num++;
143 /* make a note that we need to print 'after' lines */
144 print_n_lines_after = lines_after;
145 #endif /* BB_FEATURE_GREP_CONTEXT */
146 print_line(line, linenum, ':');
149 #ifdef BB_FEATURE_GREP_CONTEXT
150 else { /* no match */
151 /* Add the line to the circular 'before' buffer */
153 if(before_buf[curpos])
154 free(before_buf[curpos]);
155 before_buf[curpos] = strdup(line);
156 curpos = (curpos + 1) % lines_before;
160 /* if we need to print some context lines after the last match, do so */
161 if (print_n_lines_after && (last_line_printed != linenum)) {
162 print_line(line, linenum, '-');
163 print_n_lines_after--;
165 #endif /* BB_FEATURE_GREP_CONTEXT */
170 /* special-case file post-processing for options where we don't print line
171 * matches, just filenames and possibly match counts */
173 /* grep -c: print [filename:]count, even if count is zero */
174 if (print_match_counts) {
176 printf("%s:", cur_file);
177 printf("%d\n", nmatches);
180 /* grep -l: print just the filename, but only if we grepped the line in the file */
181 if (print_files_with_matches && nmatches > 0) {
186 /* remember if we matched */
191 extern int grep_main(int argc, char **argv)
195 #ifdef BB_FEATURE_GREP_CONTEXT
199 /* do normal option parsing */
200 while ((opt = getopt(argc, argv, "iHhlnqvsc"
201 #ifdef BB_FEATURE_GREP_CONTEXT
210 print_files_with_matches++;
231 print_match_counts++;
233 #ifdef BB_FEATURE_GREP_CONTEXT
235 lines_after = strtoul(optarg, &junk, 10);
237 error_msg_and_die("invalid context length argument");
240 lines_before = strtoul(optarg, &junk, 10);
242 error_msg_and_die("invalid context length argument");
243 before_buf = (char **)calloc(lines_before, sizeof(char *));
246 lines_after = lines_before = strtoul(optarg, &junk, 10);
248 error_msg_and_die("invalid context length argument");
249 before_buf = (char **)calloc(lines_before, sizeof(char *));
251 #endif /* BB_FEATURE_GREP_CONTEXT */
257 /* argv[optind] should be the regex pattern; no pattern, no worky */
258 if (argv[optind] == NULL)
262 if (print_match_counts || be_quiet || print_files_with_matches) {
264 #ifdef BB_FEATURE_GREP_CONTEXT
270 /* compile the regular expression
271 * we're not going to mess with sub-expressions, and we need to
272 * treat newlines right. */
275 reflags |= REG_ICASE;
276 xregcomp(®ex, argv[optind], reflags);
278 /* argv[(optind+1)..(argc-1)] should be names of file to grep through. If
279 * there is more than one file to grep, we will print the filenames */
280 if ((argc-1) - (optind+1) > 0)
283 /* If no files were specified, or '-' was specified, take input from
284 * stdin. Otherwise, we grep through all the files specified. */
285 if (argv[optind+1] == NULL || (strcmp(argv[optind+1], "-") == 0)) {
291 for (i = optind + 1; i < argc; i++) {
293 file = fopen(cur_file, "r");
295 if (!suppress_err_msgs)
296 perror_msg("%s", cur_file);
307 return !matched; /* invert return value 0 = success, 1 = failed */