ce2990f646306039e57b9a1c5b3342ebee1b5a29
[oweals/busybox.git] / grep.c
1 /*
2  * Mini grep implementation for busybox using libc regex.
3  *
4  * Copyright (C) 1999,2000,2001 by Lineo, inc.
5  * Written by Mark Whitley <markw@lineo.com>, <markw@codepoet.org>
6  *
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.
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  *
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <getopt.h>
26 #include <regex.h>
27 #include <string.h> /* for strerror() */
28 #include <errno.h>
29 #include "busybox.h"
30
31 extern void xregcomp(regex_t *preg, const char *regex, int cflags);
32
33 extern int optind; /* in unistd.h */
34 extern int errno;  /* for use with strerror() */
35
36 /* options */
37 static int ignore_case       = 0;
38 static int print_filename    = 0;
39 static int print_line_num    = 0;
40 static int print_count_only  = 0;
41 static int be_quiet          = 0;
42 static int invert_search     = 0;
43 static int suppress_err_msgs = 0;
44
45 /* globals */
46 static regex_t regex; /* storage space for compiled regular expression */
47 static int matched; /* keeps track of whether we ever matched */
48 static char *cur_file = NULL; /* the current file we are reading */
49
50
51 static void grep_file(FILE *file)
52 {
53         char *line = NULL;
54         int ret;
55         int linenum = 0;
56         int nmatches = 0;
57
58         while ((line = get_line_from_file(file)) != NULL) {
59                 chomp(line);
60                 linenum++;
61                 ret = regexec(&regex, line, 0, NULL, 0);
62
63                 /* test for a postitive-assertion match (regexec returned success (0)
64                  * and the user did not specify invert search), or a negative-assertion
65                  * match (vice versa) */
66                 if ((ret == 0 && !invert_search) || (ret == REG_NOMATCH && invert_search)) {
67
68                         /* if we found a match but were told to be quiet, stop here and
69                          * return success */
70                         if (be_quiet) {
71                                 regfree(&regex);
72                                 exit(0);
73                         }
74
75                         /* otherwise, keep track of matches, print the matched line, and
76                          * whatever else the user wanted */
77                         nmatches++;
78                         if (!print_count_only) {
79                                 if (print_filename)
80                                         printf("%s:", cur_file);
81                                 if (print_line_num)
82                                         printf("%i:", linenum);
83                                 puts(line);
84                         }
85                 }
86                 free(line);
87         }
88
89         /* special-case post processing */
90         if (print_count_only) {
91                 if (print_filename)
92                         printf("%s:", cur_file);
93                 printf("%i\n", nmatches);
94         }
95
96         /* record if we matched */
97         if (nmatches != 0)
98                 matched = 1;
99 }
100
101 extern int grep_main(int argc, char **argv)
102 {
103         int opt;
104         int reflags;
105
106         /* do normal option parsing */
107         while ((opt = getopt(argc, argv, "iHhnqvsc")) > 0) {
108                 switch (opt) {
109                         case 'i':
110                                 ignore_case++;
111                                 break;
112                         case 'H':
113                                 print_filename++;
114                                 break;
115                         case 'h':
116                                 print_filename--;
117                                 break;
118                         case 'n':
119                                 print_line_num++;
120                                 break;
121                         case 'q':
122                                 be_quiet++;
123                                 break;
124                         case 'v':
125                                 invert_search++;
126                                 break;
127                         case 's':
128                                 suppress_err_msgs++;
129                                 break;
130                         case 'c':
131                                 print_count_only++;
132                                 break;
133                 }
134         }
135
136         /* argv[optind] should be the regex pattern; no pattern, no worky */
137         if (argv[optind] == NULL)
138                 usage(grep_usage);
139
140         /* compile the regular expression
141          * we're not going to mess with sub-expressions, and we need to
142          * treat newlines right. */
143         reflags = REG_NOSUB; 
144         if (ignore_case)
145                 reflags |= REG_ICASE;
146         xregcomp(&regex, argv[optind], reflags);
147
148         /* argv[(optind+1)..(argc-1)] should be names of file to grep through. If
149          * there is more than one file to grep, we will print the filenames */
150         if ((argc-1) - (optind+1) > 0)
151                 print_filename++;
152
153         /* If no files were specified, or '-' was specified, take input from
154          * stdin. Otherwise, we grep through all the files specified. */
155         if (argv[optind+1] == NULL || (strcmp(argv[optind+1], "-") == 0)) {
156                 grep_file(stdin);
157         }
158         else {
159                 int i;
160                 FILE *file;
161                 for (i = optind + 1; i < argc; i++) {
162                         cur_file = argv[i];
163                         file = fopen(cur_file, "r");
164                         if (file == NULL) {
165                                 if (!suppress_err_msgs)
166                                         perror_msg("%s", cur_file);
167                         }
168                         else {
169                                 grep_file(file);
170                                 fclose(file);
171                         }
172                 }
173         }
174
175         regfree(&regex);
176
177         return !matched; /* invert return value 0 = success, 1 = failed */
178 }