blob: 69195bfdcaadf7d8910aac39cab75b2be2e74fe4 [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 *
Erik Andersen61677fe2000-04-13 01:18:56 +00004 * Copyright (C) 1999,2000 by Lineo, inc.
Mark Whitleyd3721892000-06-28 22:00:26 +00005 * Written by Mark Whitley <markw@lineo.com>, <markw@enol.com>
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
31extern int optind; /* in unistd.h */
32extern int errno; /* for use with strerror() */
Eric Andersencc8ed391999-10-05 16:24:54 +000033
Mark Whitleyd3721892000-06-28 22:00:26 +000034/* options */
35static int ignore_case = 0;
36static int print_filename = 0;
37static int print_line_num = 0;
Mark Whitley8f122432000-07-18 18:37:01 +000038static int print_count_only = 0;
Mark Whitleyd3721892000-06-28 22:00:26 +000039static int be_quiet = 0;
40static int invert_search = 0;
41static int suppress_err_msgs = 0;
42
43/* globals */
44static regex_t regex; /* storage space for compiled regular expression */
Matt Kraaideb95f62000-08-06 15:25:53 +000045static int matched; /* keeps track of whether we ever matched */
Mark Whitleyd3721892000-06-28 22:00:26 +000046static char *cur_file = NULL; /* the current file we are reading */
47
48
Mark Whitleyd3721892000-06-28 22:00:26 +000049static void print_matched_line(char *line, int linenum)
50{
Mark Whitley2ef880b2000-07-18 21:02:06 +000051 if (print_count_only)
52 return;
53
Mark Whitleyd3721892000-06-28 22:00:26 +000054 if (print_filename)
55 printf("%s:", cur_file);
56 if (print_line_num)
57 printf("%i:", linenum);
58
Matt Kraai567cdd12000-10-13 18:55:06 +000059 puts(line);
Mark Whitleyd3721892000-06-28 22:00:26 +000060}
61
62static void grep_file(FILE *file)
63{
64 char *line = NULL;
65 int ret;
66 int linenum = 0;
Matt Kraaideb95f62000-08-06 15:25:53 +000067 int nmatches = 0;
Mark Whitleyd3721892000-06-28 22:00:26 +000068
69 while ((line = get_line_from_file(file)) != NULL) {
Matt Kraai567cdd12000-10-13 18:55:06 +000070 if (line[strlen(line)-1] == '\n')
71 line[strlen(line)-1] = '\0';
Mark Whitleyd3721892000-06-28 22:00:26 +000072 linenum++;
73 ret = regexec(&regex, line, 0, NULL, 0);
74 if (ret == 0 && !invert_search) { /* match */
75
76 /* if we found a match but were told to be quiet, stop here and
77 * return success */
78 if (be_quiet) {
79 regfree(&regex);
80 exit(0);
81 }
82
83 nmatches++;
Mark Whitley2ef880b2000-07-18 21:02:06 +000084 print_matched_line(line, linenum);
Mark Whitleyd3721892000-06-28 22:00:26 +000085
Mark Whitley2ef880b2000-07-18 21:02:06 +000086 }
87 else if (ret == REG_NOMATCH && invert_search) {
Matt Kraai0810f722001-01-04 15:11:52 +000088 if (be_quiet) {
89 regfree(&regex);
90 exit(0);
91 }
92
Mark Whitley8f122432000-07-18 18:37:01 +000093 nmatches++;
Mark Whitley2ef880b2000-07-18 21:02:06 +000094 print_matched_line(line, linenum);
Mark Whitleyd3721892000-06-28 22:00:26 +000095 }
96
97 free(line);
98 }
Mark Whitley8f122432000-07-18 18:37:01 +000099
100 /* special-case post processing */
101 if (print_count_only) {
102 if (print_filename)
103 printf("%s:", cur_file);
104 printf("%i\n", nmatches);
105 }
106
Matt Kraaideb95f62000-08-06 15:25:53 +0000107 /* record if we matched */
108 if (nmatches != 0)
109 matched = 1;
Mark Whitleyd3721892000-06-28 22:00:26 +0000110}
Eric Andersencc8ed391999-10-05 16:24:54 +0000111
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112extern int grep_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000113{
Mark Whitleyd3721892000-06-28 22:00:26 +0000114 int opt;
115 int reflags;
Eric Andersencc8ed391999-10-05 16:24:54 +0000116
Mark Whitleyd3721892000-06-28 22:00:26 +0000117 /* do normal option parsing */
Mark Whitley8f122432000-07-18 18:37:01 +0000118 while ((opt = getopt(argc, argv, "iHhnqvsc")) > 0) {
Mark Whitleyd3721892000-06-28 22:00:26 +0000119 switch (opt) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000120 case 'i':
Mark Whitleyd3721892000-06-28 22:00:26 +0000121 ignore_case++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122 break;
Mark Whitleyd3721892000-06-28 22:00:26 +0000123 case 'H':
124 print_filename++;
125 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000126 case 'h':
Mark Whitleyd3721892000-06-28 22:00:26 +0000127 print_filename--;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000128 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000129 case 'n':
Mark Whitleyd3721892000-06-28 22:00:26 +0000130 print_line_num++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000131 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000132 case 'q':
Mark Whitleyd3721892000-06-28 22:00:26 +0000133 be_quiet++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000134 break;
John Beppuf93a95d2000-04-24 18:07:30 +0000135 case 'v':
Mark Whitleyd3721892000-06-28 22:00:26 +0000136 invert_search++;
John Beppuf93a95d2000-04-24 18:07:30 +0000137 break;
Mark Whitleyd3721892000-06-28 22:00:26 +0000138 case 's':
139 suppress_err_msgs++;
140 break;
Mark Whitley8f122432000-07-18 18:37:01 +0000141 case 'c':
142 print_count_only++;
143 break;
Eric Andersen053b1462000-06-13 06:24:53 +0000144 }
Eric Andersen053b1462000-06-13 06:24:53 +0000145 }
146
Mark Whitleyd3721892000-06-28 22:00:26 +0000147 /* argv[optind] should be the regex pattern; no pattern, no worky */
148 if (argv[optind] == NULL)
149 usage(grep_usage);
150
Mark Whitley44735f82000-07-10 15:50:26 +0000151 /* compile the regular expression
152 * we're not going to mess with sub-expressions, and we need to
153 * treat newlines right. */
Matt Kraai567cdd12000-10-13 18:55:06 +0000154 reflags = REG_NOSUB;
Mark Whitleyd3721892000-06-28 22:00:26 +0000155 if (ignore_case)
156 reflags |= REG_ICASE;
Mark Whitleyc41e8c82000-07-12 23:35:21 +0000157 xregcomp(&regex, argv[optind], reflags);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000158
Mark Whitleyd3721892000-06-28 22:00:26 +0000159 /* argv[(optind+1)..(argc-1)] should be names of file to grep through. If
160 * there is more than one file to grep, we will print the filenames */
161 if ((argc-1) - (optind+1) > 0)
162 print_filename++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000163
Mark Whitley2e1148b2000-06-28 22:59:30 +0000164 /* If no files were specified, or '-' was specified, take input from
165 * stdin. Otherwise, we grep through all the files specified. */
Mark Whitley8bd891c2000-06-28 22:55:59 +0000166 if (argv[optind+1] == NULL || (strcmp(argv[optind+1], "-") == 0)) {
Mark Whitleyd3721892000-06-28 22:00:26 +0000167 grep_file(stdin);
Mark Whitley2ef880b2000-07-18 21:02:06 +0000168 }
169 else {
Mark Whitleyd3721892000-06-28 22:00:26 +0000170 int i;
171 FILE *file;
172 for (i = optind + 1; i < argc; i++) {
173 cur_file = argv[i];
174 file = fopen(cur_file, "r");
175 if (file == NULL) {
176 if (!suppress_err_msgs)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000177 perror_msg("%s", cur_file);
Mark Whitley2ef880b2000-07-18 21:02:06 +0000178 }
179 else {
Mark Whitleyd3721892000-06-28 22:00:26 +0000180 grep_file(file);
181 fclose(file);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000182 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000183 }
184 }
Mark Whitleyd3721892000-06-28 22:00:26 +0000185
186 regfree(&regex);
187
Matt Kraaideb95f62000-08-06 15:25:53 +0000188 if (!matched)
Mark Whitleyd3721892000-06-28 22:00:26 +0000189 return 1;
190
191 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000192}