Make cin be static
[oweals/busybox.git] / findutils / grep.c
1 /*
2  * Mini grep implementation for busybox using libc regex.
3  *
4  * Copyright (C) 1999,2000 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 int optind; /* in unistd.h */
32 extern int errno;  /* for use with strerror() */
33
34 /* options */
35 static int ignore_case       = 0;
36 static int print_filename    = 0;
37 static int print_line_num    = 0;
38 static int print_count_only  = 0;
39 static int be_quiet          = 0;
40 static int invert_search     = 0;
41 static int suppress_err_msgs = 0;
42
43 /* globals */
44 static regex_t regex; /* storage space for compiled regular expression */
45 static int matched; /* keeps track of whether we ever matched */
46 static char *cur_file = NULL; /* the current file we are reading */
47
48
49 static void print_matched_line(char *line, int linenum)
50 {
51         if (print_count_only)
52                 return;
53
54         if (print_filename)
55                 printf("%s:", cur_file);
56         if (print_line_num)
57                 printf("%i:", linenum);
58
59         puts(line);
60 }
61
62 static void grep_file(FILE *file)
63 {
64         char *line = NULL;
65         int ret;
66         int linenum = 0;
67         int nmatches = 0;
68
69         while ((line = get_line_from_file(file)) != NULL) {
70                 if (line[strlen(line)-1] == '\n')
71                         line[strlen(line)-1] = '\0';
72                 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++;
84                         print_matched_line(line, linenum);
85
86                 }
87                 else if (ret == REG_NOMATCH && invert_search) {
88                         if (be_quiet) {
89                                 regfree(&regex);
90                                 exit(0);
91                         }
92
93                         nmatches++;
94                         print_matched_line(line, linenum);
95                 }
96
97                 free(line);
98         }
99
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
107         /* record if we matched */
108         if (nmatches != 0)
109                 matched = 1;
110 }
111
112 extern int grep_main(int argc, char **argv)
113 {
114         int opt;
115         int reflags;
116
117         /* do normal option parsing */
118         while ((opt = getopt(argc, argv, "iHhnqvsc")) > 0) {
119                 switch (opt) {
120                         case 'i':
121                                 ignore_case++;
122                                 break;
123                         case 'H':
124                                 print_filename++;
125                                 break;
126                         case 'h':
127                                 print_filename--;
128                                 break;
129                         case 'n':
130                                 print_line_num++;
131                                 break;
132                         case 'q':
133                                 be_quiet++;
134                                 break;
135                         case 'v':
136                                 invert_search++;
137                                 break;
138                         case 's':
139                                 suppress_err_msgs++;
140                                 break;
141                         case 'c':
142                                 print_count_only++;
143                                 break;
144                 }
145         }
146
147         /* argv[optind] should be the regex pattern; no pattern, no worky */
148         if (argv[optind] == NULL)
149                 usage(grep_usage);
150
151         /* compile the regular expression
152          * we're not going to mess with sub-expressions, and we need to
153          * treat newlines right. */
154         reflags = REG_NOSUB; 
155         if (ignore_case)
156                 reflags |= REG_ICASE;
157         xregcomp(&regex, argv[optind], reflags);
158
159         /* 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++;
163
164         /* If no files were specified, or '-' was specified, take input from
165          * stdin. Otherwise, we grep through all the files specified. */
166         if (argv[optind+1] == NULL || (strcmp(argv[optind+1], "-") == 0)) {
167                 grep_file(stdin);
168         }
169         else {
170                 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)
177                                         perror_msg("%s", cur_file);
178                         }
179                         else {
180                                 grep_file(file);
181                                 fclose(file);
182                         }
183                 }
184         }
185
186         regfree(&regex);
187
188         if (!matched)
189                 return 1;
190
191         return 0;
192 }