blob: b519adf180fe8ded213eede3e400ab7ec3cbfc0a [file] [log] [blame]
Eric Andersencc8ed391999-10-05 16:24:54 +00001/*
Mark Whitleyd3721892000-06-28 22:00:26 +00002 * Mini grep implementation for busybox using libc regex.
Eric Andersenc4996011999-10-20 22:08:37 +00003 *
Eric Andersen8ec10a92001-01-27 09:33:39 +00004 * Copyright (C) 1999,2000,2001 by Lineo, inc.
Mark Whitley6c6ea6c2001-01-04 22:21:13 +00005 * Written by Mark Whitley <markw@lineo.com>, <markw@codepoet.org>
Eric Andersencc8ed391999-10-05 16:24:54 +00006 *
Eric Andersen3e0fbae1999-10-19 06:02:44 +00007 * 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.
11 *
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.
16 *
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
20 *
Eric Andersencc8ed391999-10-05 16:24:54 +000021 */
22
Eric Andersencc8ed391999-10-05 16:24:54 +000023#include <stdio.h>
Mark Whitleyd3721892000-06-28 22:00:26 +000024#include <stdlib.h>
Eric Andersene9b527a2000-07-09 05:56:14 +000025#include <getopt.h>
Mark Whitleyd3721892000-06-28 22:00:26 +000026#include <regex.h>
27#include <string.h> /* for strerror() */
Eric Andersencc8ed391999-10-05 16:24:54 +000028#include <errno.h>
Eric Andersen3570a342000-09-25 21:45:58 +000029#include "busybox.h"
Mark Whitleyd3721892000-06-28 22:00:26 +000030
Eric Andersened3ef502001-01-27 08:24:39 +000031
Mark Whitleyd3721892000-06-28 22:00:26 +000032extern int optind; /* in unistd.h */
33extern int errno; /* for use with strerror() */
Mark Whitley2fd52982001-02-09 00:41:10 +000034extern void xregcomp(regex_t *preg, const char *regex, int cflags); /* in busybox.h */
Eric Andersencc8ed391999-10-05 16:24:54 +000035
Mark Whitleyd3721892000-06-28 22:00:26 +000036/* options */
37static int ignore_case = 0;
38static int print_filename = 0;
39static int print_line_num = 0;
Mark Whitley8f122432000-07-18 18:37:01 +000040static int print_count_only = 0;
Mark Whitleyd3721892000-06-28 22:00:26 +000041static int be_quiet = 0;
42static int invert_search = 0;
43static int suppress_err_msgs = 0;
Eric Andersen303dd282001-04-09 23:26:31 +000044static int files_that_match = 0;
Mark Whitleyd3721892000-06-28 22:00:26 +000045
Mark Whitley2fd52982001-02-09 00:41:10 +000046#ifdef BB_FEATURE_GREP_CONTEXT
47extern char *optarg; /* in getopt.h */
48static int lines_before = 0;
49static int lines_after = 0;
50static char **before_buf = NULL;
51static int last_line_printed = 0;
52#endif /* BB_FEATURE_GREP_CONTEXT */
53
54/* globals used internally */
Mark Whitleyd3721892000-06-28 22:00:26 +000055static regex_t regex; /* storage space for compiled regular expression */
Matt Kraaideb95f62000-08-06 15:25:53 +000056static int matched; /* keeps track of whether we ever matched */
Mark Whitleyd3721892000-06-28 22:00:26 +000057static char *cur_file = NULL; /* the current file we are reading */
58
Mark Whitley2fd52982001-02-09 00:41:10 +000059static void print_line(const char *line, int linenum, char decoration)
60{
61#ifdef BB_FEATURE_GREP_CONTEXT
62 /* possibly print the little '--' seperator */
63 if (last_line_printed && last_line_printed < linenum - 1) {
64 puts("--");
65 }
66 last_line_printed = linenum;
67#endif
68 if (print_filename)
69 printf("%s%c", cur_file, decoration);
70 if (print_line_num)
71 printf("%i%c", linenum, decoration);
72 puts(line);
73}
Mark Whitleyd3721892000-06-28 22:00:26 +000074
Mark Whitleyd3721892000-06-28 22:00:26 +000075static void grep_file(FILE *file)
76{
77 char *line = NULL;
78 int ret;
79 int linenum = 0;
Matt Kraaideb95f62000-08-06 15:25:53 +000080 int nmatches = 0;
Mark Whitley2fd52982001-02-09 00:41:10 +000081#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 */
Mark Whitleyd3721892000-06-28 22:00:26 +000086
87 while ((line = get_line_from_file(file)) != NULL) {
Matt Kraai05e782d2001-02-01 16:49:30 +000088 chomp(line);
Mark Whitleyd3721892000-06-28 22:00:26 +000089 linenum++;
Mark Whitleyb5c29852001-02-01 21:02:41 +000090
Mark Whitley2fd52982001-02-09 00:41:10 +000091 /*
92 * test for a postitive-assertion match (regexec returns success (0)
Mark Whitleyb5c29852001-02-01 21:02:41 +000093 * and the user did not specify invert search), or a negative-assertion
Mark Whitley2fd52982001-02-09 00:41:10 +000094 * match (regexec returns failure (REG_NOMATCH) and the user specified
95 * invert search)
96 */
97 ret = regexec(&regex, line, 0, NULL, 0);
Mark Whitleyb5c29852001-02-01 21:02:41 +000098 if ((ret == 0 && !invert_search) || (ret == REG_NOMATCH && invert_search)) {
Mark Whitleyd3721892000-06-28 22:00:26 +000099
100 /* if we found a match but were told to be quiet, stop here and
101 * return success */
102 if (be_quiet) {
103 regfree(&regex);
104 exit(0);
105 }
106
Mark Whitley2fd52982001-02-09 00:41:10 +0000107 /* otherwise, keep track of matches and print the matched line */
Mark Whitleyd3721892000-06-28 22:00:26 +0000108 nmatches++;
Eric Andersen303dd282001-04-09 23:26:31 +0000109 if (print_count_only==0 && files_that_match==0) {
Mark Whitley2fd52982001-02-09 00:41:10 +0000110#ifdef BB_FEATURE_GREP_CONTEXT
111 int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
112
113 /* if we were told to print 'before' lines and there is at least
114 * one line in the circular buffer, print them */
115 if (lines_before && before_buf[prevpos] != NULL)
116 {
117 int first_buf_entry_line_num = linenum - lines_before;
118
119 /* advance to the first entry in the circular buffer, and
120 * figure out the line number is of the first line in the
121 * buffer */
122 idx = curpos;
123 while (before_buf[idx] == NULL) {
124 idx = (idx + 1) % lines_before;
125 first_buf_entry_line_num++;
126 }
127
128 /* now print each line in the buffer, clearing them as we go */
129 while (before_buf[idx] != NULL) {
130 print_line(before_buf[idx], first_buf_entry_line_num, '-');
131 free(before_buf[idx]);
132 before_buf[idx] = NULL;
133 idx = (idx + 1) % lines_before;
134 first_buf_entry_line_num++;
135 }
136
137 }
138
139 /* make a note that we need to print 'after' lines */
140 print_n_lines_after = lines_after;
141#endif /* BB_FEATURE_GREP_CONTEXT */
142 print_line(line, linenum, ':');
Matt Kraai0810f722001-01-04 15:11:52 +0000143 }
Mark Whitleyd3721892000-06-28 22:00:26 +0000144 }
Mark Whitley2fd52982001-02-09 00:41:10 +0000145#ifdef BB_FEATURE_GREP_CONTEXT
146 else { /* no match */
147 /* Add the line to the circular 'before' buffer */
148 if(lines_before)
149 {
150 if(before_buf[curpos])
151 free(before_buf[curpos]);
152 before_buf[curpos] = strdup(line);
153 curpos = (curpos + 1) % lines_before;
154 }
155 }
156
157 /* if we need to print some context lines after the last match, do so */
158 if (print_n_lines_after && (last_line_printed != linenum)) {
159 print_line(line, linenum, '-');
160 print_n_lines_after--;
161 }
162#endif /* BB_FEATURE_GREP_CONTEXT */
Mark Whitleyd3721892000-06-28 22:00:26 +0000163 free(line);
164 }
Mark Whitley8f122432000-07-18 18:37:01 +0000165
166 /* special-case post processing */
Eric Andersen303dd282001-04-09 23:26:31 +0000167 if (files_that_match) {
168 if (nmatches > 0) {
169 printf("%s", cur_file);
170 if (nmatches)
171 printf(":%d", nmatches);
172 printf("\n");
173 }
174 } else if (print_count_only) {
Mark Whitley8f122432000-07-18 18:37:01 +0000175 if (print_filename)
176 printf("%s:", cur_file);
177 printf("%i\n", nmatches);
Eric Andersen303dd282001-04-09 23:26:31 +0000178 }
Mark Whitley8f122432000-07-18 18:37:01 +0000179
Mark Whitley2fd52982001-02-09 00:41:10 +0000180 /* remember if we matched */
Matt Kraaideb95f62000-08-06 15:25:53 +0000181 if (nmatches != 0)
182 matched = 1;
Mark Whitleyd3721892000-06-28 22:00:26 +0000183}
Eric Andersencc8ed391999-10-05 16:24:54 +0000184
Erik Andersene49d5ec2000-02-08 19:58:47 +0000185extern int grep_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000186{
Mark Whitleyd3721892000-06-28 22:00:26 +0000187 int opt;
188 int reflags;
Mark Whitley2fd52982001-02-09 00:41:10 +0000189#ifdef BB_FEATURE_GREP_CONTEXT
190 char *junk;
191#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000192
Mark Whitleyd3721892000-06-28 22:00:26 +0000193 /* do normal option parsing */
Eric Andersen303dd282001-04-09 23:26:31 +0000194 while ((opt = getopt(argc, argv, "iHhlnqvsc"
Mark Whitley2fd52982001-02-09 00:41:10 +0000195#ifdef BB_FEATURE_GREP_CONTEXT
196"A:B:C:"
197#endif
198)) > 0) {
Mark Whitleyd3721892000-06-28 22:00:26 +0000199 switch (opt) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000200 case 'i':
Mark Whitleyd3721892000-06-28 22:00:26 +0000201 ignore_case++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000202 break;
Eric Andersen303dd282001-04-09 23:26:31 +0000203 case 'l':
204 files_that_match++;
205 break;
Mark Whitleyd3721892000-06-28 22:00:26 +0000206 case 'H':
207 print_filename++;
208 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000209 case 'h':
Mark Whitleyd3721892000-06-28 22:00:26 +0000210 print_filename--;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000211 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000212 case 'n':
Mark Whitleyd3721892000-06-28 22:00:26 +0000213 print_line_num++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000214 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000215 case 'q':
Mark Whitleyd3721892000-06-28 22:00:26 +0000216 be_quiet++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000217 break;
John Beppuf93a95d2000-04-24 18:07:30 +0000218 case 'v':
Mark Whitleyd3721892000-06-28 22:00:26 +0000219 invert_search++;
John Beppuf93a95d2000-04-24 18:07:30 +0000220 break;
Mark Whitleyd3721892000-06-28 22:00:26 +0000221 case 's':
222 suppress_err_msgs++;
223 break;
Mark Whitley8f122432000-07-18 18:37:01 +0000224 case 'c':
225 print_count_only++;
226 break;
Mark Whitley2fd52982001-02-09 00:41:10 +0000227#ifdef BB_FEATURE_GREP_CONTEXT
228 case 'A':
229 lines_after = strtoul(optarg, &junk, 10);
230 if(*junk != '\0')
231 error_msg_and_die("invalid context length argument");
232 break;
233 case 'B':
234 lines_before = strtoul(optarg, &junk, 10);
235 if(*junk != '\0')
236 error_msg_and_die("invalid context length argument");
237 before_buf = (char **)calloc(lines_before, sizeof(char *));
238 break;
239 case 'C':
240 lines_after = lines_before = strtoul(optarg, &junk, 10);
241 if(*junk != '\0')
242 error_msg_and_die("invalid context length argument");
243 before_buf = (char **)calloc(lines_before, sizeof(char *));
244 break;
245#endif /* BB_FEATURE_GREP_CONTEXT */
Mark Whitley4391a162001-04-09 23:00:07 +0000246 default:
247 show_usage();
Eric Andersen053b1462000-06-13 06:24:53 +0000248 }
Eric Andersen053b1462000-06-13 06:24:53 +0000249 }
250
Mark Whitleyd3721892000-06-28 22:00:26 +0000251 /* argv[optind] should be the regex pattern; no pattern, no worky */
252 if (argv[optind] == NULL)
Eric Andersen67991cf2001-02-14 21:23:06 +0000253 show_usage();
Mark Whitleyd3721892000-06-28 22:00:26 +0000254
Mark Whitley2fd52982001-02-09 00:41:10 +0000255 /* sanity check */
Eric Andersen303dd282001-04-09 23:26:31 +0000256 if (print_count_only || be_quiet || files_that_match) {
Mark Whitley2fd52982001-02-09 00:41:10 +0000257 print_line_num = 0;
258#ifdef BB_FEATURE_GREP_CONTEXT
259 lines_before = 0;
260 lines_after = 0;
261#endif
262 }
263
Mark Whitley44735f82000-07-10 15:50:26 +0000264 /* compile the regular expression
265 * we're not going to mess with sub-expressions, and we need to
266 * treat newlines right. */
Matt Kraai567cdd12000-10-13 18:55:06 +0000267 reflags = REG_NOSUB;
Mark Whitleyd3721892000-06-28 22:00:26 +0000268 if (ignore_case)
269 reflags |= REG_ICASE;
Mark Whitleyc41e8c82000-07-12 23:35:21 +0000270 xregcomp(&regex, argv[optind], reflags);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000271
Mark Whitleyd3721892000-06-28 22:00:26 +0000272 /* argv[(optind+1)..(argc-1)] should be names of file to grep through. If
273 * there is more than one file to grep, we will print the filenames */
274 if ((argc-1) - (optind+1) > 0)
275 print_filename++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000276
Mark Whitley2e1148b2000-06-28 22:59:30 +0000277 /* If no files were specified, or '-' was specified, take input from
278 * stdin. Otherwise, we grep through all the files specified. */
Mark Whitley8bd891c2000-06-28 22:55:59 +0000279 if (argv[optind+1] == NULL || (strcmp(argv[optind+1], "-") == 0)) {
Mark Whitleyd3721892000-06-28 22:00:26 +0000280 grep_file(stdin);
Mark Whitley2ef880b2000-07-18 21:02:06 +0000281 }
282 else {
Mark Whitleyd3721892000-06-28 22:00:26 +0000283 int i;
284 FILE *file;
285 for (i = optind + 1; i < argc; i++) {
286 cur_file = argv[i];
287 file = fopen(cur_file, "r");
288 if (file == NULL) {
289 if (!suppress_err_msgs)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000290 perror_msg("%s", cur_file);
Mark Whitley2ef880b2000-07-18 21:02:06 +0000291 }
292 else {
Mark Whitleyd3721892000-06-28 22:00:26 +0000293 grep_file(file);
294 fclose(file);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000295 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000296 }
297 }
Mark Whitleyd3721892000-06-28 22:00:26 +0000298
299 regfree(&regex);
300
Mark Whitleyb5c29852001-02-01 21:02:41 +0000301 return !matched; /* invert return value 0 = success, 1 = failed */
Eric Andersencc8ed391999-10-05 16:24:54 +0000302}