Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1 | /* |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 2 | * Mini grep implementation for busybox using libc regex. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 3 | * |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 4 | * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 6 | * |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 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 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 21 | */ |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 22 | /* |
Eric Andersen | 7f164cd | 2004-05-26 09:46:41 +0000 | [diff] [blame^] | 23 | * Apr 2004 by Vladimir Oleynik <dzo@simtreas.ru> - |
| 24 | * correction "-e pattern1 -e pattern2" logic and more optimizations. |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 25 | */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 26 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 28 | #include <stdlib.h> |
Eric Andersen | e9b527a | 2000-07-09 05:56:14 +0000 | [diff] [blame] | 29 | #include <getopt.h> |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 30 | #include <regex.h> |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 31 | #include <string.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 32 | #include <errno.h> |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 33 | #include "busybox.h" |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 34 | |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 35 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 36 | /* options */ |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 37 | #define GREP_OPTS "lnqvscFiHhe:f:" |
| 38 | #define GREP_OPT_l 1 |
| 39 | static char print_files_with_matches; |
| 40 | #define GREP_OPT_n 2 |
| 41 | static char print_line_num; |
| 42 | #define GREP_OPT_q 4 |
| 43 | static char be_quiet; |
| 44 | #define GREP_OPT_v 8 |
| 45 | typedef char invert_search_t; |
| 46 | static invert_search_t invert_search; |
| 47 | #define GREP_OPT_s 16 |
| 48 | static char suppress_err_msgs; |
| 49 | #define GREP_OPT_c 32 |
| 50 | static char print_match_counts; |
| 51 | #define GREP_OPT_F 64 |
| 52 | static char fgrep_flag; |
| 53 | #define GREP_OPT_i 128 |
| 54 | #define GREP_OPT_H 256 |
| 55 | #define GREP_OPT_h 512 |
| 56 | #define GREP_OPT_e 1024 |
| 57 | #define GREP_OPT_f 2048 |
| 58 | #ifdef CONFIG_FEATURE_GREP_CONTEXT |
| 59 | #define GREP_OPT_CONTEXT "A:B:C" |
| 60 | #define GREP_OPT_A 4096 |
| 61 | #define GREP_OPT_B 8192 |
| 62 | #define GREP_OPT_C 16384 |
| 63 | #define GREP_OPT_E 32768U |
| 64 | #else |
| 65 | #define GREP_OPT_CONTEXT "" |
| 66 | #define GREP_OPT_E 4096 |
| 67 | #endif |
| 68 | #ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS |
| 69 | # define OPT_EGREP "E" |
| 70 | #else |
| 71 | # define OPT_EGREP "" |
| 72 | #endif |
| 73 | |
| 74 | static int reflags; |
| 75 | static int print_filename; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 76 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 77 | #ifdef CONFIG_FEATURE_GREP_CONTEXT |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 78 | static int lines_before; |
| 79 | static int lines_after; |
| 80 | static char **before_buf; |
| 81 | static int last_line_printed; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 82 | #endif /* CONFIG_FEATURE_GREP_CONTEXT */ |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 83 | |
| 84 | /* globals used internally */ |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 85 | static llist_t *pattern_head; /* growable list of patterns to match */ |
| 86 | static char *cur_file; /* the current file we are reading */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 87 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 88 | |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 89 | static void print_line(const char *line, int linenum, char decoration) |
| 90 | { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 91 | #ifdef CONFIG_FEATURE_GREP_CONTEXT |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 92 | /* possibly print the little '--' separator */ |
Matt Kraai | edc8065 | 2001-05-22 14:29:27 +0000 | [diff] [blame] | 93 | if ((lines_before || lines_after) && last_line_printed && |
| 94 | last_line_printed < linenum - 1) { |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 95 | puts("--"); |
| 96 | } |
| 97 | last_line_printed = linenum; |
| 98 | #endif |
| 99 | if (print_filename) |
| 100 | printf("%s%c", cur_file, decoration); |
| 101 | if (print_line_num) |
| 102 | printf("%i%c", linenum, decoration); |
| 103 | puts(line); |
| 104 | } |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 105 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 106 | extern void xregcomp(regex_t *preg, const char *regex, int cflags); |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 107 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 108 | |
| 109 | static int grep_file(FILE *file) |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 110 | { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 111 | char *line; |
| 112 | invert_search_t ret; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 113 | int linenum = 0; |
Matt Kraai | deb95f6 | 2000-08-06 15:25:53 +0000 | [diff] [blame] | 114 | int nmatches = 0; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 115 | #ifdef CONFIG_FEATURE_GREP_CONTEXT |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 116 | int print_n_lines_after = 0; |
| 117 | int curpos = 0; /* track where we are in the circular 'before' buffer */ |
| 118 | int idx = 0; /* used for iteration through the circular buffer */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 119 | #endif /* CONFIG_FEATURE_GREP_CONTEXT */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 120 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 121 | while ((line = bb_get_chomped_line_from_file(file)) != NULL) { |
Glenn L McGrath | 26df70a | 2003-04-27 01:50:57 +0000 | [diff] [blame] | 122 | llist_t *pattern_ptr = pattern_head; |
| 123 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 124 | linenum++; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 125 | ret = 0; |
Glenn L McGrath | 26df70a | 2003-04-27 01:50:57 +0000 | [diff] [blame] | 126 | while (pattern_ptr) { |
| 127 | if (fgrep_flag) { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 128 | ret = strstr(line, pattern_ptr->data) != NULL; |
Glenn L McGrath | 26df70a | 2003-04-27 01:50:57 +0000 | [diff] [blame] | 129 | } else { |
| 130 | /* |
| 131 | * test for a postitive-assertion match (regexec returns success (0) |
| 132 | * and the user did not specify invert search), or a negative-assertion |
| 133 | * match (regexec returns failure (REG_NOMATCH) and the user specified |
| 134 | * invert search) |
| 135 | */ |
| 136 | regex_t regex; |
| 137 | xregcomp(®ex, pattern_ptr->data, reflags); |
Eric Andersen | 7f164cd | 2004-05-26 09:46:41 +0000 | [diff] [blame^] | 138 | ret |= regexec(®ex, line, 0, NULL, 0) == 0; |
Glenn L McGrath | 26df70a | 2003-04-27 01:50:57 +0000 | [diff] [blame] | 139 | regfree(®ex); |
| 140 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 141 | pattern_ptr = pattern_ptr->link; |
| 142 | } /* while (pattern_ptr) */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 143 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 144 | if ((ret ^ invert_search)) { |
| 145 | |
| 146 | if (print_files_with_matches || be_quiet) |
| 147 | free(line); |
| 148 | |
| 149 | /* if we found a match but were told to be quiet, stop here */ |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 150 | if (be_quiet) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 151 | return -1; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 152 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 153 | /* keep track of matches */ |
| 154 | nmatches++; |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 155 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 156 | /* if we're just printing filenames, we stop after the first match */ |
| 157 | if (print_files_with_matches) |
| 158 | break; |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 159 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 160 | /* print the matched line */ |
| 161 | if (print_match_counts == 0) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 162 | #ifdef CONFIG_FEATURE_GREP_CONTEXT |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 163 | int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1; |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 164 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 165 | /* if we were told to print 'before' lines and there is at least |
| 166 | * one line in the circular buffer, print them */ |
| 167 | if (lines_before && before_buf[prevpos] != NULL) { |
| 168 | int first_buf_entry_line_num = linenum - lines_before; |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 169 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 170 | /* advance to the first entry in the circular buffer, and |
| 171 | * figure out the line number is of the first line in the |
| 172 | * buffer */ |
| 173 | idx = curpos; |
| 174 | while (before_buf[idx] == NULL) { |
| 175 | idx = (idx + 1) % lines_before; |
| 176 | first_buf_entry_line_num++; |
| 177 | } |
| 178 | |
| 179 | /* now print each line in the buffer, clearing them as we go */ |
| 180 | while (before_buf[idx] != NULL) { |
| 181 | print_line(before_buf[idx], first_buf_entry_line_num, '-'); |
| 182 | free(before_buf[idx]); |
| 183 | before_buf[idx] = NULL; |
| 184 | idx = (idx + 1) % lines_before; |
| 185 | first_buf_entry_line_num++; |
| 186 | } |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 189 | /* make a note that we need to print 'after' lines */ |
| 190 | print_n_lines_after = lines_after; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 191 | #endif /* CONFIG_FEATURE_GREP_CONTEXT */ |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 192 | print_line(line, linenum, ':'); |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 193 | } |
Matt Kraai | 0810f72 | 2001-01-04 15:11:52 +0000 | [diff] [blame] | 194 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 195 | #ifdef CONFIG_FEATURE_GREP_CONTEXT |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 196 | else { /* no match */ |
| 197 | /* Add the line to the circular 'before' buffer */ |
| 198 | if(lines_before) { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 199 | free(before_buf[curpos]); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 200 | before_buf[curpos] = bb_xstrdup(line); |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 201 | curpos = (curpos + 1) % lines_before; |
| 202 | } |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 203 | } |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 204 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 205 | /* if we need to print some context lines after the last match, do so */ |
| 206 | if (print_n_lines_after && (last_line_printed != linenum)) { |
| 207 | print_line(line, linenum, '-'); |
| 208 | print_n_lines_after--; |
| 209 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 210 | #endif /* CONFIG_FEATURE_GREP_CONTEXT */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 211 | free(line); |
| 212 | } |
Mark Whitley | 8f12243 | 2000-07-18 18:37:01 +0000 | [diff] [blame] | 213 | |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 214 | |
| 215 | /* special-case file post-processing for options where we don't print line |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 216 | * matches, just filenames and possibly match counts */ |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 217 | |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 218 | /* grep -c: print [filename:]count, even if count is zero */ |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 219 | if (print_match_counts) { |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 220 | if (print_filename) |
| 221 | printf("%s:", cur_file); |
Eric Andersen | 0034443 | 2001-07-31 23:18:49 +0000 | [diff] [blame] | 222 | printf("%d\n", nmatches); |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 223 | } |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 224 | |
| 225 | /* grep -l: print just the filename, but only if we grepped the line in the file */ |
| 226 | if (print_files_with_matches && nmatches > 0) { |
Matt Kraai | 59df6f7 | 2001-05-16 14:21:09 +0000 | [diff] [blame] | 227 | puts(cur_file); |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 230 | return nmatches; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 231 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 232 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 233 | static void load_regexes_from_file(llist_t *fopt) |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 234 | { |
| 235 | char *line; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 236 | FILE *f; |
| 237 | |
| 238 | while(fopt) { |
| 239 | llist_t *cur = fopt; |
| 240 | char *ffile = cur->data; |
| 241 | |
| 242 | fopt = cur->link; |
| 243 | free(cur); |
| 244 | f = bb_xfopen(ffile, "r"); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 245 | while ((line = bb_get_chomped_line_from_file(f)) != NULL) { |
Glenn L McGrath | 26df70a | 2003-04-27 01:50:57 +0000 | [diff] [blame] | 246 | pattern_head = llist_add_to(pattern_head, line); |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 247 | } |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 250 | |
| 251 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 252 | extern int grep_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 253 | { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 254 | FILE *file; |
| 255 | int matched; |
| 256 | unsigned long opt; |
| 257 | llist_t *fopt; |
Matt Kraai | 999623e | 2001-10-29 15:49:03 +0000 | [diff] [blame] | 258 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 259 | /* do normal option parsing */ |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 260 | #ifdef CONFIG_FEATURE_GREP_CONTEXT |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 261 | { |
| 262 | char *junk; |
| 263 | char *slines_after; |
| 264 | char *slines_before; |
| 265 | char *Copt; |
| 266 | |
| 267 | bb_opt_complementaly = "H-h:e*:f*:C-AB"; |
| 268 | opt = bb_getopt_ulflags(argc, argv, |
| 269 | GREP_OPTS GREP_OPT_CONTEXT OPT_EGREP, |
| 270 | &pattern_head, &fopt, |
| 271 | &slines_after, &slines_before, &Copt); |
| 272 | |
| 273 | if(opt & GREP_OPT_C) { |
| 274 | /* C option unseted A and B options, but next -A or -B |
| 275 | may be ovewrite own option */ |
| 276 | if(!(opt & GREP_OPT_A)) /* not overwtited */ |
| 277 | slines_after = Copt; |
| 278 | if(!(opt & GREP_OPT_B)) /* not overwtited */ |
| 279 | slines_before = Copt; |
| 280 | opt |= GREP_OPT_A|GREP_OPT_B; /* set for parse now */ |
Eric Andersen | 053b146 | 2000-06-13 06:24:53 +0000 | [diff] [blame] | 281 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 282 | if(opt & GREP_OPT_A) { |
| 283 | lines_after = strtoul(slines_after, &junk, 10); |
| 284 | if(*junk != '\0') |
| 285 | bb_error_msg_and_die("invalid context length argument"); |
| 286 | } |
| 287 | if(opt & GREP_OPT_B) { |
| 288 | lines_before = strtoul(slines_before, &junk, 10); |
| 289 | if(*junk != '\0') |
| 290 | bb_error_msg_and_die("invalid context length argument"); |
| 291 | } |
| 292 | /* sanity checks after parse may be invalid numbers ;-) */ |
| 293 | if ((opt & (GREP_OPT_c|GREP_OPT_q|GREP_OPT_l))) { |
| 294 | opt &= ~GREP_OPT_n; |
| 295 | lines_before = 0; |
| 296 | lines_after = 0; |
| 297 | } else if(lines_before > 0) |
| 298 | before_buf = (char **)xcalloc(lines_before, sizeof(char *)); |
| 299 | } |
| 300 | #else |
| 301 | /* with auto sanity checks */ |
| 302 | bb_opt_complementaly = "H-h:e*:f*:c-n:q-n:l-n"; |
| 303 | opt = bb_getopt_ulflags(argc, argv, GREP_OPTS OPT_EGREP, |
| 304 | &pattern_head, &fopt); |
| 305 | |
| 306 | #endif |
| 307 | print_files_with_matches = opt & GREP_OPT_l; |
| 308 | print_line_num = opt & GREP_OPT_n; |
| 309 | be_quiet = opt & GREP_OPT_q; |
| 310 | invert_search = (opt & GREP_OPT_v) != 0; /* 0 | 1 */ |
| 311 | suppress_err_msgs = opt & GREP_OPT_s; |
| 312 | print_match_counts = opt & GREP_OPT_c; |
| 313 | fgrep_flag = opt & GREP_OPT_F; |
| 314 | if(opt & GREP_OPT_H) |
| 315 | print_filename++; |
| 316 | if(opt & GREP_OPT_h) |
| 317 | print_filename--; |
| 318 | if(opt & GREP_OPT_f) |
| 319 | load_regexes_from_file(fopt); |
| 320 | |
| 321 | #ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS |
| 322 | if(bb_applet_name[0] == 'e' || (opt & GREP_OPT_E)) |
| 323 | reflags = REG_EXTENDED | REG_NOSUB; |
| 324 | else |
| 325 | #endif |
| 326 | reflags = REG_NOSUB; |
| 327 | |
| 328 | if(opt & GREP_OPT_i) |
| 329 | reflags |= REG_ICASE; |
| 330 | |
| 331 | argv += optind; |
| 332 | argc -= optind; |
Eric Andersen | 053b146 | 2000-06-13 06:24:53 +0000 | [diff] [blame] | 333 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 334 | /* if we didn't get a pattern from a -e and no command file was specified, |
| 335 | * argv[optind] should be the pattern. no pattern, no worky */ |
Glenn L McGrath | 26df70a | 2003-04-27 01:50:57 +0000 | [diff] [blame] | 336 | if (pattern_head == NULL) { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 337 | if (*argv == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 338 | bb_show_usage(); |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 339 | else { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 340 | pattern_head = llist_add_to(pattern_head, *argv++); |
| 341 | argc--; |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 342 | } |
| 343 | } |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 344 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 345 | /* argv[(optind)..(argc-1)] should be names of file to grep through. If |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 346 | * there is more than one file to grep, we will print the filenames */ |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 347 | if (argc > 1) { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 348 | print_filename++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 349 | |
Mark Whitley | 2e1148b | 2000-06-28 22:59:30 +0000 | [diff] [blame] | 350 | /* If no files were specified, or '-' was specified, take input from |
| 351 | * stdin. Otherwise, we grep through all the files specified. */ |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 352 | } else if (argc == 0) { |
| 353 | argc++; |
Mark Whitley | 2ef880b | 2000-07-18 21:02:06 +0000 | [diff] [blame] | 354 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 355 | matched = 0; |
| 356 | while (argc--) { |
| 357 | cur_file = *argv++; |
| 358 | if(!cur_file || (*cur_file == '-' && !cur_file[1])) { |
| 359 | cur_file = "-"; |
| 360 | file = stdin; |
| 361 | } else { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 362 | file = fopen(cur_file, "r"); |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 363 | } |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 364 | if (file == NULL) { |
| 365 | if (!suppress_err_msgs) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 366 | bb_perror_msg("%s", cur_file); |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 367 | } else { |
| 368 | matched += grep_file(file); |
| 369 | if(matched < 0) { |
| 370 | /* we found a match but were told to be quiet, stop here and |
| 371 | * return success */ |
| 372 | break; |
Mark Whitley | 2ef880b | 2000-07-18 21:02:06 +0000 | [diff] [blame] | 373 | } |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 374 | fclose(file); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 375 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 376 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 377 | |
| 378 | #ifdef CONFIG_FEATURE_CLEAN_UP |
| 379 | /* destroy all the elments in the pattern list */ |
| 380 | while (pattern_head) { |
| 381 | llist_t *pattern_head_ptr = pattern_head; |
| 382 | |
| 383 | pattern_head = pattern_head->link; |
| 384 | free(pattern_head_ptr); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 385 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 386 | #endif |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 387 | |
Mark Whitley | b5c2985 | 2001-02-01 21:02:41 +0000 | [diff] [blame] | 388 | return !matched; /* invert return value 0 = success, 1 = failed */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 389 | } |