blob: cc2b697e8c63009b26e736481b167620b0ca8da0 [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 Andersenbdfd0d72001-10-24 05:00:29 +00004 * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley
5 * Copyright (C) 1999,2000,2001 by Mark Whitley <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 */
Mark Whitley2fd52982001-02-09 00:41:10 +000033extern void xregcomp(regex_t *preg, const char *regex, int cflags); /* in busybox.h */
Eric Andersencc8ed391999-10-05 16:24:54 +000034
Mark Whitleyd3721892000-06-28 22:00:26 +000035/* options */
Mark Whitleyfa43e542001-05-24 18:36:18 +000036static int reflags = REG_NOSUB;
37static int print_filename = 0;
38static int print_line_num = 0;
39static int print_match_counts = 0;
40static int be_quiet = 0;
41static int invert_search = 0;
42static int suppress_err_msgs = 0;
Mark Whitley35e59be2001-05-14 19:40:32 +000043static int print_files_with_matches = 0;
Mark Whitleyd3721892000-06-28 22:00:26 +000044
Eric Andersenbdfd0d72001-10-24 05:00:29 +000045#ifdef CONFIG_FEATURE_GREP_CONTEXT
Mark Whitley2fd52982001-02-09 00:41:10 +000046extern char *optarg; /* in getopt.h */
47static int lines_before = 0;
48static int lines_after = 0;
49static char **before_buf = NULL;
50static int last_line_printed = 0;
Eric Andersenbdfd0d72001-10-24 05:00:29 +000051#endif /* CONFIG_FEATURE_GREP_CONTEXT */
Mark Whitley2fd52982001-02-09 00:41:10 +000052
53/* globals used internally */
Mark Whitleyfa43e542001-05-24 18:36:18 +000054static regex_t *regexes = NULL; /* growable array of compiled regular expressions */
55static int nregexes = 0; /* number of elements in above arrary */
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 Whitleyfa43e542001-05-24 18:36:18 +000059
Mark Whitley2fd52982001-02-09 00:41:10 +000060static void print_line(const char *line, int linenum, char decoration)
61{
Eric Andersenbdfd0d72001-10-24 05:00:29 +000062#ifdef CONFIG_FEATURE_GREP_CONTEXT
Mark Whitley2fd52982001-02-09 00:41:10 +000063 /* possibly print the little '--' seperator */
Matt Kraaiedc80652001-05-22 14:29:27 +000064 if ((lines_before || lines_after) && last_line_printed &&
65 last_line_printed < linenum - 1) {
Mark Whitley2fd52982001-02-09 00:41:10 +000066 puts("--");
67 }
68 last_line_printed = linenum;
69#endif
70 if (print_filename)
71 printf("%s%c", cur_file, decoration);
72 if (print_line_num)
73 printf("%i%c", linenum, decoration);
74 puts(line);
75}
Mark Whitleyd3721892000-06-28 22:00:26 +000076
Mark Whitleyfa43e542001-05-24 18:36:18 +000077
Mark Whitleyd3721892000-06-28 22:00:26 +000078static void grep_file(FILE *file)
79{
80 char *line = NULL;
81 int ret;
82 int linenum = 0;
Matt Kraaideb95f62000-08-06 15:25:53 +000083 int nmatches = 0;
Mark Whitleyfa43e542001-05-24 18:36:18 +000084 int i;
Eric Andersenbdfd0d72001-10-24 05:00:29 +000085#ifdef CONFIG_FEATURE_GREP_CONTEXT
Mark Whitley2fd52982001-02-09 00:41:10 +000086 int print_n_lines_after = 0;
87 int curpos = 0; /* track where we are in the circular 'before' buffer */
88 int idx = 0; /* used for iteration through the circular buffer */
Eric Andersenbdfd0d72001-10-24 05:00:29 +000089#endif /* CONFIG_FEATURE_GREP_CONTEXT */
Mark Whitleyd3721892000-06-28 22:00:26 +000090
91 while ((line = get_line_from_file(file)) != NULL) {
Matt Kraai05e782d2001-02-01 16:49:30 +000092 chomp(line);
Mark Whitleyd3721892000-06-28 22:00:26 +000093 linenum++;
Mark Whitleyb5c29852001-02-01 21:02:41 +000094
Mark Whitleyfa43e542001-05-24 18:36:18 +000095 for (i = 0; i < nregexes; i++) {
96 /*
97 * test for a postitive-assertion match (regexec returns success (0)
98 * and the user did not specify invert search), or a negative-assertion
99 * match (regexec returns failure (REG_NOMATCH) and the user specified
100 * invert search)
101 */
102 ret = regexec(&regexes[i], line, 0, NULL, 0);
103 if ((ret == 0 && !invert_search) || (ret == REG_NOMATCH && invert_search)) {
Mark Whitleyd3721892000-06-28 22:00:26 +0000104
Mark Whitleyfa43e542001-05-24 18:36:18 +0000105 /* if we found a match but were told to be quiet, stop here and
106 * return success */
107 if (be_quiet)
108 exit(0);
Mark Whitleyd3721892000-06-28 22:00:26 +0000109
Mark Whitleyfa43e542001-05-24 18:36:18 +0000110 /* keep track of matches */
111 nmatches++;
Mark Whitley1d9d4112001-05-21 21:13:00 +0000112
Mark Whitleyfa43e542001-05-24 18:36:18 +0000113 /* if we're just printing filenames, we stop after the first match */
114 if (print_files_with_matches)
115 break;
Mark Whitley1d9d4112001-05-21 21:13:00 +0000116
Mark Whitleyfa43e542001-05-24 18:36:18 +0000117 /* print the matched line */
118 if (print_match_counts == 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000119#ifdef CONFIG_FEATURE_GREP_CONTEXT
Mark Whitleyfa43e542001-05-24 18:36:18 +0000120 int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
Mark Whitley2fd52982001-02-09 00:41:10 +0000121
Mark Whitleyfa43e542001-05-24 18:36:18 +0000122 /* if we were told to print 'before' lines and there is at least
123 * one line in the circular buffer, print them */
124 if (lines_before && before_buf[prevpos] != NULL) {
125 int first_buf_entry_line_num = linenum - lines_before;
Mark Whitley2fd52982001-02-09 00:41:10 +0000126
Mark Whitleyfa43e542001-05-24 18:36:18 +0000127 /* advance to the first entry in the circular buffer, and
128 * figure out the line number is of the first line in the
129 * buffer */
130 idx = curpos;
131 while (before_buf[idx] == NULL) {
132 idx = (idx + 1) % lines_before;
133 first_buf_entry_line_num++;
134 }
135
136 /* now print each line in the buffer, clearing them as we go */
137 while (before_buf[idx] != NULL) {
138 print_line(before_buf[idx], first_buf_entry_line_num, '-');
139 free(before_buf[idx]);
140 before_buf[idx] = NULL;
141 idx = (idx + 1) % lines_before;
142 first_buf_entry_line_num++;
143 }
Mark Whitley2fd52982001-02-09 00:41:10 +0000144 }
145
Mark Whitleyfa43e542001-05-24 18:36:18 +0000146 /* make a note that we need to print 'after' lines */
147 print_n_lines_after = lines_after;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000148#endif /* CONFIG_FEATURE_GREP_CONTEXT */
Mark Whitleyfa43e542001-05-24 18:36:18 +0000149 print_line(line, linenum, ':');
Mark Whitley2fd52982001-02-09 00:41:10 +0000150 }
Matt Kraai0810f722001-01-04 15:11:52 +0000151 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000152#ifdef CONFIG_FEATURE_GREP_CONTEXT
Mark Whitleyfa43e542001-05-24 18:36:18 +0000153 else { /* no match */
154 /* Add the line to the circular 'before' buffer */
155 if(lines_before) {
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000156 free(before_buf[curpos]);
Matt Kraaia21c24b2001-11-12 16:58:07 +0000157 before_buf[curpos] = xstrdup(line);
Mark Whitleyfa43e542001-05-24 18:36:18 +0000158 curpos = (curpos + 1) % lines_before;
159 }
Mark Whitley2fd52982001-02-09 00:41:10 +0000160 }
Mark Whitley2fd52982001-02-09 00:41:10 +0000161
Mark Whitleyfa43e542001-05-24 18:36:18 +0000162 /* if we need to print some context lines after the last match, do so */
163 if (print_n_lines_after && (last_line_printed != linenum)) {
164 print_line(line, linenum, '-');
165 print_n_lines_after--;
166 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000167#endif /* CONFIG_FEATURE_GREP_CONTEXT */
Mark Whitleyfa43e542001-05-24 18:36:18 +0000168 } /* for */
Mark Whitleyd3721892000-06-28 22:00:26 +0000169 free(line);
170 }
Mark Whitley8f122432000-07-18 18:37:01 +0000171
Mark Whitley35e59be2001-05-14 19:40:32 +0000172
173 /* special-case file post-processing for options where we don't print line
Mark Whitley1d9d4112001-05-21 21:13:00 +0000174 * matches, just filenames and possibly match counts */
Mark Whitley35e59be2001-05-14 19:40:32 +0000175
Mark Whitley1d9d4112001-05-21 21:13:00 +0000176 /* grep -c: print [filename:]count, even if count is zero */
Mark Whitley35e59be2001-05-14 19:40:32 +0000177 if (print_match_counts) {
Mark Whitley1d9d4112001-05-21 21:13:00 +0000178 if (print_filename)
179 printf("%s:", cur_file);
Eric Andersen00344432001-07-31 23:18:49 +0000180 if (print_files_with_matches && nmatches > 0)
181 printf("1\n");
182 else
183 printf("%d\n", nmatches);
Mark Whitley35e59be2001-05-14 19:40:32 +0000184 }
Mark Whitley1d9d4112001-05-21 21:13:00 +0000185
186 /* grep -l: print just the filename, but only if we grepped the line in the file */
187 if (print_files_with_matches && nmatches > 0) {
Matt Kraai59df6f72001-05-16 14:21:09 +0000188 puts(cur_file);
Mark Whitley35e59be2001-05-14 19:40:32 +0000189 }
190
Mark Whitley8f122432000-07-18 18:37:01 +0000191
Mark Whitley2fd52982001-02-09 00:41:10 +0000192 /* remember if we matched */
Matt Kraaideb95f62000-08-06 15:25:53 +0000193 if (nmatches != 0)
194 matched = 1;
Mark Whitleyd3721892000-06-28 22:00:26 +0000195}
Eric Andersencc8ed391999-10-05 16:24:54 +0000196
Mark Whitleyfa43e542001-05-24 18:36:18 +0000197
198static void add_regex(const char *restr)
199{
200 regexes = xrealloc(regexes, sizeof(regex_t) * (++nregexes));
201 xregcomp(&regexes[nregexes-1], restr, reflags);
202}
203
204
205static void load_regexes_from_file(const char *filename)
206{
207 char *line;
208 FILE *f = xfopen(filename, "r");
209 while ((line = get_line_from_file(f)) != NULL) {
210 chomp(line);
211 add_regex(line);
212 free(line);
213 }
214}
215
216
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000217#ifdef CONFIG_FEATURE_CLEAN_UP
Eric Andersen6f8b7ea2001-11-19 11:45:39 +0000218static void destroy_regexes(void)
Mark Whitleyfa43e542001-05-24 18:36:18 +0000219{
220 if (regexes == NULL)
221 return;
222
223 /* destroy all the elments in the array */
224 while (--nregexes >= 0) {
Eric Andersen6f8b7ea2001-11-19 11:45:39 +0000225 regfree(&(regexes[nregexes]));
Mark Whitleyfa43e542001-05-24 18:36:18 +0000226 }
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000227 free(regexes);
Mark Whitleyfa43e542001-05-24 18:36:18 +0000228}
229#endif
230
231
Erik Andersene49d5ec2000-02-08 19:58:47 +0000232extern int grep_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000233{
Mark Whitleyd3721892000-06-28 22:00:26 +0000234 int opt;
Matt Kraaieed94512002-02-22 22:08:16 +0000235#if defined (CONFIG_FEATURE_GREP_CONTEXT)
Mark Whitley2fd52982001-02-09 00:41:10 +0000236 char *junk;
237#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000238
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000239#ifdef CONFIG_FEATURE_CLEAN_UP
Mark Whitleyfa43e542001-05-24 18:36:18 +0000240 /* destroy command strings on exit */
Eric Andersen55805bc2001-11-19 10:59:37 +0000241 atexit(destroy_regexes);
Mark Whitleyfa43e542001-05-24 18:36:18 +0000242#endif
243
Matt Kraai999623e2001-10-29 15:49:03 +0000244#ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS
Matt Kraaieed94512002-02-22 22:08:16 +0000245 if (strcmp(get_last_path_component(argv[0]), "egrep") == 0)
Eric Andersenccd96f12001-10-31 10:02:46 +0000246 reflags |= REG_EXTENDED;
Matt Kraai999623e2001-10-29 15:49:03 +0000247#endif
248
Mark Whitleyd3721892000-06-28 22:00:26 +0000249 /* do normal option parsing */
Mark Whitleyfa43e542001-05-24 18:36:18 +0000250 while ((opt = getopt(argc, argv, "iHhlnqvsce:f:"
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000251#ifdef CONFIG_FEATURE_GREP_CONTEXT
Mark Whitley2fd52982001-02-09 00:41:10 +0000252"A:B:C:"
253#endif
Eric Andersendba4e6f2001-11-10 12:54:00 +0000254#ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS
255"E"
256#endif
Mark Whitley2fd52982001-02-09 00:41:10 +0000257)) > 0) {
Mark Whitleyd3721892000-06-28 22:00:26 +0000258 switch (opt) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000259 case 'i':
Mark Whitleyfa43e542001-05-24 18:36:18 +0000260 reflags |= REG_ICASE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000261 break;
Eric Andersen303dd282001-04-09 23:26:31 +0000262 case 'l':
Mark Whitley35e59be2001-05-14 19:40:32 +0000263 print_files_with_matches++;
Eric Andersen303dd282001-04-09 23:26:31 +0000264 break;
Mark Whitleyd3721892000-06-28 22:00:26 +0000265 case 'H':
266 print_filename++;
267 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000268 case 'h':
Mark Whitleyd3721892000-06-28 22:00:26 +0000269 print_filename--;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000270 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000271 case 'n':
Mark Whitleyd3721892000-06-28 22:00:26 +0000272 print_line_num++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000273 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000274 case 'q':
Mark Whitleyd3721892000-06-28 22:00:26 +0000275 be_quiet++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000276 break;
John Beppuf93a95d2000-04-24 18:07:30 +0000277 case 'v':
Mark Whitleyd3721892000-06-28 22:00:26 +0000278 invert_search++;
John Beppuf93a95d2000-04-24 18:07:30 +0000279 break;
Mark Whitleyd3721892000-06-28 22:00:26 +0000280 case 's':
281 suppress_err_msgs++;
282 break;
Mark Whitley8f122432000-07-18 18:37:01 +0000283 case 'c':
Mark Whitley35e59be2001-05-14 19:40:32 +0000284 print_match_counts++;
Mark Whitley8f122432000-07-18 18:37:01 +0000285 break;
Mark Whitleyfa43e542001-05-24 18:36:18 +0000286 case 'e':
287 add_regex(optarg);
288 break;
Eric Andersendba4e6f2001-11-10 12:54:00 +0000289#ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS
290 case 'E':
291 reflags |= REG_EXTENDED;
292 break;
293#endif
Mark Whitleyfa43e542001-05-24 18:36:18 +0000294 case 'f':
295 load_regexes_from_file(optarg);
296 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000297#ifdef CONFIG_FEATURE_GREP_CONTEXT
Mark Whitley2fd52982001-02-09 00:41:10 +0000298 case 'A':
299 lines_after = strtoul(optarg, &junk, 10);
300 if(*junk != '\0')
301 error_msg_and_die("invalid context length argument");
302 break;
303 case 'B':
304 lines_before = strtoul(optarg, &junk, 10);
305 if(*junk != '\0')
306 error_msg_and_die("invalid context length argument");
Matt Kraaia21c24b2001-11-12 16:58:07 +0000307 before_buf = (char **)xcalloc(lines_before, sizeof(char *));
Mark Whitley2fd52982001-02-09 00:41:10 +0000308 break;
309 case 'C':
310 lines_after = lines_before = strtoul(optarg, &junk, 10);
311 if(*junk != '\0')
312 error_msg_and_die("invalid context length argument");
Matt Kraaia21c24b2001-11-12 16:58:07 +0000313 before_buf = (char **)xcalloc(lines_before, sizeof(char *));
Mark Whitley2fd52982001-02-09 00:41:10 +0000314 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000315#endif /* CONFIG_FEATURE_GREP_CONTEXT */
Mark Whitley4391a162001-04-09 23:00:07 +0000316 default:
317 show_usage();
Eric Andersen053b1462000-06-13 06:24:53 +0000318 }
Eric Andersen053b1462000-06-13 06:24:53 +0000319 }
320
Mark Whitleyfa43e542001-05-24 18:36:18 +0000321 /* if we didn't get a pattern from a -e and no command file was specified,
322 * argv[optind] should be the pattern. no pattern, no worky */
323 if (nregexes == 0) {
324 if (argv[optind] == NULL)
325 show_usage();
326 else {
327 add_regex(argv[optind]);
328 optind++;
329 }
330 }
Mark Whitleyd3721892000-06-28 22:00:26 +0000331
Mark Whitleyfa43e542001-05-24 18:36:18 +0000332 /* sanity checks */
Mark Whitley35e59be2001-05-14 19:40:32 +0000333 if (print_match_counts || be_quiet || print_files_with_matches) {
Mark Whitley2fd52982001-02-09 00:41:10 +0000334 print_line_num = 0;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000335#ifdef CONFIG_FEATURE_GREP_CONTEXT
Mark Whitley2fd52982001-02-09 00:41:10 +0000336 lines_before = 0;
337 lines_after = 0;
338#endif
339 }
340
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 */
Mark Whitleyfa43e542001-05-24 18:36:18 +0000343 if ((argc-1) - (optind) > 0)
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. */
Mark Whitleyfa43e542001-05-24 18:36:18 +0000348 if (argv[optind] == NULL || (strcmp(argv[optind], "-") == 0)) {
Mark Whitleyd3721892000-06-28 22:00:26 +0000349 grep_file(stdin);
Mark Whitley2ef880b2000-07-18 21:02:06 +0000350 }
351 else {
Mark Whitleyd3721892000-06-28 22:00:26 +0000352 int i;
353 FILE *file;
Mark Whitleyfa43e542001-05-24 18:36:18 +0000354 for (i = optind; i < argc; i++) {
Mark Whitleyd3721892000-06-28 22:00:26 +0000355 cur_file = argv[i];
356 file = fopen(cur_file, "r");
357 if (file == NULL) {
358 if (!suppress_err_msgs)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000359 perror_msg("%s", cur_file);
Mark Whitley2ef880b2000-07-18 21:02:06 +0000360 }
361 else {
Mark Whitleyd3721892000-06-28 22:00:26 +0000362 grep_file(file);
363 fclose(file);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000364 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000365 }
366 }
Mark Whitleyd3721892000-06-28 22:00:26 +0000367
Mark Whitleyb5c29852001-02-01 21:02:41 +0000368 return !matched; /* invert return value 0 = success, 1 = failed */
Eric Andersencc8ed391999-10-05 16:24:54 +0000369}