blob: 320655bf4eb71b1c17f32a6d1d642c40acec9890 [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 +000031extern void xregcomp(regex_t *preg, const char *regex, int cflags);
32
Mark Whitleyd3721892000-06-28 22:00:26 +000033extern int optind; /* in unistd.h */
34extern int errno; /* for use with strerror() */
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;
44
45/* globals */
46static regex_t regex; /* storage space for compiled regular expression */
Matt Kraaideb95f62000-08-06 15:25:53 +000047static int matched; /* keeps track of whether we ever matched */
Mark Whitleyd3721892000-06-28 22:00:26 +000048static char *cur_file = NULL; /* the current file we are reading */
49
50
Mark Whitleyd3721892000-06-28 22:00:26 +000051static void print_matched_line(char *line, int linenum)
52{
Mark Whitley2ef880b2000-07-18 21:02:06 +000053 if (print_count_only)
54 return;
55
Mark Whitleyd3721892000-06-28 22:00:26 +000056 if (print_filename)
57 printf("%s:", cur_file);
58 if (print_line_num)
59 printf("%i:", linenum);
60
Matt Kraai567cdd12000-10-13 18:55:06 +000061 puts(line);
Mark Whitleyd3721892000-06-28 22:00:26 +000062}
63
64static void grep_file(FILE *file)
65{
66 char *line = NULL;
67 int ret;
68 int linenum = 0;
Matt Kraaideb95f62000-08-06 15:25:53 +000069 int nmatches = 0;
Mark Whitleyd3721892000-06-28 22:00:26 +000070
71 while ((line = get_line_from_file(file)) != NULL) {
Matt Kraai567cdd12000-10-13 18:55:06 +000072 if (line[strlen(line)-1] == '\n')
73 line[strlen(line)-1] = '\0';
Mark Whitleyd3721892000-06-28 22:00:26 +000074 linenum++;
75 ret = regexec(&regex, line, 0, NULL, 0);
76 if (ret == 0 && !invert_search) { /* match */
77
78 /* if we found a match but were told to be quiet, stop here and
79 * return success */
80 if (be_quiet) {
81 regfree(&regex);
82 exit(0);
83 }
84
85 nmatches++;
Mark Whitley2ef880b2000-07-18 21:02:06 +000086 print_matched_line(line, linenum);
Mark Whitleyd3721892000-06-28 22:00:26 +000087
Mark Whitley2ef880b2000-07-18 21:02:06 +000088 }
89 else if (ret == REG_NOMATCH && invert_search) {
Matt Kraai0810f722001-01-04 15:11:52 +000090 if (be_quiet) {
91 regfree(&regex);
92 exit(0);
93 }
94
Mark Whitley8f122432000-07-18 18:37:01 +000095 nmatches++;
Mark Whitley2ef880b2000-07-18 21:02:06 +000096 print_matched_line(line, linenum);
Mark Whitleyd3721892000-06-28 22:00:26 +000097 }
98
99 free(line);
100 }
Mark Whitley8f122432000-07-18 18:37:01 +0000101
102 /* special-case post processing */
103 if (print_count_only) {
104 if (print_filename)
105 printf("%s:", cur_file);
106 printf("%i\n", nmatches);
107 }
108
Matt Kraaideb95f62000-08-06 15:25:53 +0000109 /* record if we matched */
110 if (nmatches != 0)
111 matched = 1;
Mark Whitleyd3721892000-06-28 22:00:26 +0000112}
Eric Andersencc8ed391999-10-05 16:24:54 +0000113
Erik Andersene49d5ec2000-02-08 19:58:47 +0000114extern int grep_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000115{
Mark Whitleyd3721892000-06-28 22:00:26 +0000116 int opt;
117 int reflags;
Eric Andersencc8ed391999-10-05 16:24:54 +0000118
Mark Whitleyd3721892000-06-28 22:00:26 +0000119 /* do normal option parsing */
Mark Whitley8f122432000-07-18 18:37:01 +0000120 while ((opt = getopt(argc, argv, "iHhnqvsc")) > 0) {
Mark Whitleyd3721892000-06-28 22:00:26 +0000121 switch (opt) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122 case 'i':
Mark Whitleyd3721892000-06-28 22:00:26 +0000123 ignore_case++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000124 break;
Mark Whitleyd3721892000-06-28 22:00:26 +0000125 case 'H':
126 print_filename++;
127 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000128 case 'h':
Mark Whitleyd3721892000-06-28 22:00:26 +0000129 print_filename--;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000130 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000131 case 'n':
Mark Whitleyd3721892000-06-28 22:00:26 +0000132 print_line_num++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000133 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000134 case 'q':
Mark Whitleyd3721892000-06-28 22:00:26 +0000135 be_quiet++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000136 break;
John Beppuf93a95d2000-04-24 18:07:30 +0000137 case 'v':
Mark Whitleyd3721892000-06-28 22:00:26 +0000138 invert_search++;
John Beppuf93a95d2000-04-24 18:07:30 +0000139 break;
Mark Whitleyd3721892000-06-28 22:00:26 +0000140 case 's':
141 suppress_err_msgs++;
142 break;
Mark Whitley8f122432000-07-18 18:37:01 +0000143 case 'c':
144 print_count_only++;
145 break;
Eric Andersen053b1462000-06-13 06:24:53 +0000146 }
Eric Andersen053b1462000-06-13 06:24:53 +0000147 }
148
Mark Whitleyd3721892000-06-28 22:00:26 +0000149 /* argv[optind] should be the regex pattern; no pattern, no worky */
150 if (argv[optind] == NULL)
151 usage(grep_usage);
152
Mark Whitley44735f82000-07-10 15:50:26 +0000153 /* compile the regular expression
154 * we're not going to mess with sub-expressions, and we need to
155 * treat newlines right. */
Matt Kraai567cdd12000-10-13 18:55:06 +0000156 reflags = REG_NOSUB;
Mark Whitleyd3721892000-06-28 22:00:26 +0000157 if (ignore_case)
158 reflags |= REG_ICASE;
Mark Whitleyc41e8c82000-07-12 23:35:21 +0000159 xregcomp(&regex, argv[optind], reflags);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000160
Mark Whitleyd3721892000-06-28 22:00:26 +0000161 /* argv[(optind+1)..(argc-1)] should be names of file to grep through. If
162 * there is more than one file to grep, we will print the filenames */
163 if ((argc-1) - (optind+1) > 0)
164 print_filename++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000165
Mark Whitley2e1148b2000-06-28 22:59:30 +0000166 /* If no files were specified, or '-' was specified, take input from
167 * stdin. Otherwise, we grep through all the files specified. */
Mark Whitley8bd891c2000-06-28 22:55:59 +0000168 if (argv[optind+1] == NULL || (strcmp(argv[optind+1], "-") == 0)) {
Mark Whitleyd3721892000-06-28 22:00:26 +0000169 grep_file(stdin);
Mark Whitley2ef880b2000-07-18 21:02:06 +0000170 }
171 else {
Mark Whitleyd3721892000-06-28 22:00:26 +0000172 int i;
173 FILE *file;
174 for (i = optind + 1; i < argc; i++) {
175 cur_file = argv[i];
176 file = fopen(cur_file, "r");
177 if (file == NULL) {
178 if (!suppress_err_msgs)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000179 perror_msg("%s", cur_file);
Mark Whitley2ef880b2000-07-18 21:02:06 +0000180 }
181 else {
Mark Whitleyd3721892000-06-28 22:00:26 +0000182 grep_file(file);
183 fclose(file);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000184 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000185 }
186 }
Mark Whitleyd3721892000-06-28 22:00:26 +0000187
188 regfree(&regex);
189
Matt Kraaideb95f62000-08-06 15:25:53 +0000190 if (!matched)
Mark Whitleyd3721892000-06-28 22:00:26 +0000191 return 1;
192
193 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000194}