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 | 8ec10a9 | 2001-01-27 09:33:39 +0000 | [diff] [blame] | 4 | * Copyright (C) 1999,2000,2001 by Lineo, inc. |
Mark Whitley | 6c6ea6c | 2001-01-04 22:21:13 +0000 | [diff] [blame] | 5 | * Written by Mark Whitley <markw@lineo.com>, <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 | */ |
| 22 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 23 | #include <stdio.h> |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 24 | #include <stdlib.h> |
Eric Andersen | e9b527a | 2000-07-09 05:56:14 +0000 | [diff] [blame] | 25 | #include <getopt.h> |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 26 | #include <regex.h> |
| 27 | #include <string.h> /* for strerror() */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 28 | #include <errno.h> |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 29 | #include "busybox.h" |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 30 | |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 31 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 32 | extern int optind; /* in unistd.h */ |
| 33 | extern int errno; /* for use with strerror() */ |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 34 | extern void xregcomp(regex_t *preg, const char *regex, int cflags); /* in busybox.h */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 35 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 36 | /* options */ |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 37 | static int reflags = REG_NOSUB; |
| 38 | static int print_filename = 0; |
| 39 | static int print_line_num = 0; |
| 40 | static int print_match_counts = 0; |
| 41 | static int be_quiet = 0; |
| 42 | static int invert_search = 0; |
| 43 | static int suppress_err_msgs = 0; |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 44 | static int print_files_with_matches = 0; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 45 | |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 46 | #ifdef BB_FEATURE_GREP_CONTEXT |
| 47 | extern char *optarg; /* in getopt.h */ |
| 48 | static int lines_before = 0; |
| 49 | static int lines_after = 0; |
| 50 | static char **before_buf = NULL; |
| 51 | static int last_line_printed = 0; |
| 52 | #endif /* BB_FEATURE_GREP_CONTEXT */ |
| 53 | |
| 54 | /* globals used internally */ |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 55 | static regex_t *regexes = NULL; /* growable array of compiled regular expressions */ |
| 56 | static int nregexes = 0; /* number of elements in above arrary */ |
Matt Kraai | deb95f6 | 2000-08-06 15:25:53 +0000 | [diff] [blame] | 57 | static int matched; /* keeps track of whether we ever matched */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 58 | static char *cur_file = NULL; /* the current file we are reading */ |
| 59 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 60 | |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 61 | static void print_line(const char *line, int linenum, char decoration) |
| 62 | { |
| 63 | #ifdef BB_FEATURE_GREP_CONTEXT |
| 64 | /* possibly print the little '--' seperator */ |
Matt Kraai | edc8065 | 2001-05-22 14:29:27 +0000 | [diff] [blame] | 65 | if ((lines_before || lines_after) && last_line_printed && |
| 66 | last_line_printed < linenum - 1) { |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 67 | puts("--"); |
| 68 | } |
| 69 | last_line_printed = linenum; |
| 70 | #endif |
| 71 | if (print_filename) |
| 72 | printf("%s%c", cur_file, decoration); |
| 73 | if (print_line_num) |
| 74 | printf("%i%c", linenum, decoration); |
| 75 | puts(line); |
| 76 | } |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 77 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 78 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 79 | static void grep_file(FILE *file) |
| 80 | { |
| 81 | char *line = NULL; |
| 82 | int ret; |
| 83 | int linenum = 0; |
Matt Kraai | deb95f6 | 2000-08-06 15:25:53 +0000 | [diff] [blame] | 84 | int nmatches = 0; |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 85 | int i; |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 86 | #ifdef BB_FEATURE_GREP_CONTEXT |
| 87 | int print_n_lines_after = 0; |
| 88 | int curpos = 0; /* track where we are in the circular 'before' buffer */ |
| 89 | int idx = 0; /* used for iteration through the circular buffer */ |
| 90 | #endif /* BB_FEATURE_GREP_CONTEXT */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 91 | |
| 92 | while ((line = get_line_from_file(file)) != NULL) { |
Matt Kraai | 05e782d | 2001-02-01 16:49:30 +0000 | [diff] [blame] | 93 | chomp(line); |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 94 | linenum++; |
Mark Whitley | b5c2985 | 2001-02-01 21:02:41 +0000 | [diff] [blame] | 95 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 96 | for (i = 0; i < nregexes; i++) { |
| 97 | /* |
| 98 | * test for a postitive-assertion match (regexec returns success (0) |
| 99 | * and the user did not specify invert search), or a negative-assertion |
| 100 | * match (regexec returns failure (REG_NOMATCH) and the user specified |
| 101 | * invert search) |
| 102 | */ |
| 103 | ret = regexec(®exes[i], line, 0, NULL, 0); |
| 104 | if ((ret == 0 && !invert_search) || (ret == REG_NOMATCH && invert_search)) { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 105 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 106 | /* if we found a match but were told to be quiet, stop here and |
| 107 | * return success */ |
| 108 | if (be_quiet) |
| 109 | exit(0); |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 110 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 111 | /* keep track of matches */ |
| 112 | nmatches++; |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 113 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 114 | /* if we're just printing filenames, we stop after the first match */ |
| 115 | if (print_files_with_matches) |
| 116 | break; |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 117 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 118 | /* print the matched line */ |
| 119 | if (print_match_counts == 0) { |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 120 | #ifdef BB_FEATURE_GREP_CONTEXT |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 121 | int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1; |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 122 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 123 | /* if we were told to print 'before' lines and there is at least |
| 124 | * one line in the circular buffer, print them */ |
| 125 | if (lines_before && before_buf[prevpos] != NULL) { |
| 126 | int first_buf_entry_line_num = linenum - lines_before; |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 127 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 128 | /* advance to the first entry in the circular buffer, and |
| 129 | * figure out the line number is of the first line in the |
| 130 | * buffer */ |
| 131 | idx = curpos; |
| 132 | while (before_buf[idx] == NULL) { |
| 133 | idx = (idx + 1) % lines_before; |
| 134 | first_buf_entry_line_num++; |
| 135 | } |
| 136 | |
| 137 | /* now print each line in the buffer, clearing them as we go */ |
| 138 | while (before_buf[idx] != NULL) { |
| 139 | print_line(before_buf[idx], first_buf_entry_line_num, '-'); |
| 140 | free(before_buf[idx]); |
| 141 | before_buf[idx] = NULL; |
| 142 | idx = (idx + 1) % lines_before; |
| 143 | first_buf_entry_line_num++; |
| 144 | } |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 147 | /* make a note that we need to print 'after' lines */ |
| 148 | print_n_lines_after = lines_after; |
| 149 | #endif /* BB_FEATURE_GREP_CONTEXT */ |
| 150 | print_line(line, linenum, ':'); |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 151 | } |
Matt Kraai | 0810f72 | 2001-01-04 15:11:52 +0000 | [diff] [blame] | 152 | } |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 153 | #ifdef BB_FEATURE_GREP_CONTEXT |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 154 | else { /* no match */ |
| 155 | /* Add the line to the circular 'before' buffer */ |
| 156 | if(lines_before) { |
| 157 | if(before_buf[curpos]) |
| 158 | free(before_buf[curpos]); |
| 159 | before_buf[curpos] = strdup(line); |
| 160 | curpos = (curpos + 1) % lines_before; |
| 161 | } |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 162 | } |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 163 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 164 | /* if we need to print some context lines after the last match, do so */ |
| 165 | if (print_n_lines_after && (last_line_printed != linenum)) { |
| 166 | print_line(line, linenum, '-'); |
| 167 | print_n_lines_after--; |
| 168 | } |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 169 | #endif /* BB_FEATURE_GREP_CONTEXT */ |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 170 | } /* for */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 171 | free(line); |
| 172 | } |
Mark Whitley | 8f12243 | 2000-07-18 18:37:01 +0000 | [diff] [blame] | 173 | |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 174 | |
| 175 | /* 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] | 176 | * matches, just filenames and possibly match counts */ |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 177 | |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 178 | /* grep -c: print [filename:]count, even if count is zero */ |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 179 | if (print_match_counts) { |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 180 | if (print_filename) |
| 181 | printf("%s:", cur_file); |
| 182 | printf("%d\n", nmatches); |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 183 | } |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 184 | |
| 185 | /* grep -l: print just the filename, but only if we grepped the line in the file */ |
| 186 | if (print_files_with_matches && nmatches > 0) { |
Matt Kraai | 59df6f7 | 2001-05-16 14:21:09 +0000 | [diff] [blame] | 187 | puts(cur_file); |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Mark Whitley | 8f12243 | 2000-07-18 18:37:01 +0000 | [diff] [blame] | 190 | |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 191 | /* remember if we matched */ |
Matt Kraai | deb95f6 | 2000-08-06 15:25:53 +0000 | [diff] [blame] | 192 | if (nmatches != 0) |
| 193 | matched = 1; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 194 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 195 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 196 | |
| 197 | static void add_regex(const char *restr) |
| 198 | { |
| 199 | regexes = xrealloc(regexes, sizeof(regex_t) * (++nregexes)); |
| 200 | xregcomp(®exes[nregexes-1], restr, reflags); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | static void load_regexes_from_file(const char *filename) |
| 205 | { |
| 206 | char *line; |
| 207 | FILE *f = xfopen(filename, "r"); |
| 208 | while ((line = get_line_from_file(f)) != NULL) { |
| 209 | chomp(line); |
| 210 | add_regex(line); |
| 211 | free(line); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | |
| 216 | #ifdef BB_FEATURE_CLEAN_UP |
| 217 | static void destroy_regexes() |
| 218 | { |
| 219 | if (regexes == NULL) |
| 220 | return; |
| 221 | |
| 222 | /* destroy all the elments in the array */ |
| 223 | while (--nregexes >= 0) { |
Eric Andersen | 86f0167 | 2001-05-29 22:36:39 +0000 | [diff] [blame] | 224 | regfree(®exes[nregexes]); |
| 225 | free(®exes[nregexes]); |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | #endif |
| 229 | |
| 230 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 231 | extern int grep_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 232 | { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 233 | int opt; |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 234 | #ifdef BB_FEATURE_GREP_CONTEXT |
| 235 | char *junk; |
| 236 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 237 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 238 | #ifdef BB_FEATURE_CLEAN_UP |
| 239 | /* destroy command strings on exit */ |
| 240 | if (atexit(destroy_regexes) == -1) |
| 241 | perror_msg_and_die("atexit"); |
| 242 | #endif |
| 243 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 244 | /* do normal option parsing */ |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 245 | while ((opt = getopt(argc, argv, "iHhlnqvsce:f:" |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 246 | #ifdef BB_FEATURE_GREP_CONTEXT |
| 247 | "A:B:C:" |
| 248 | #endif |
| 249 | )) > 0) { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 250 | switch (opt) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 251 | case 'i': |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 252 | reflags |= REG_ICASE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 253 | break; |
Eric Andersen | 303dd28 | 2001-04-09 23:26:31 +0000 | [diff] [blame] | 254 | case 'l': |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 255 | print_files_with_matches++; |
Eric Andersen | 303dd28 | 2001-04-09 23:26:31 +0000 | [diff] [blame] | 256 | break; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 257 | case 'H': |
| 258 | print_filename++; |
| 259 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 260 | case 'h': |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 261 | print_filename--; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 262 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 263 | case 'n': |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 264 | print_line_num++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 265 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 266 | case 'q': |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 267 | be_quiet++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 268 | break; |
John Beppu | f93a95d | 2000-04-24 18:07:30 +0000 | [diff] [blame] | 269 | case 'v': |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 270 | invert_search++; |
John Beppu | f93a95d | 2000-04-24 18:07:30 +0000 | [diff] [blame] | 271 | break; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 272 | case 's': |
| 273 | suppress_err_msgs++; |
| 274 | break; |
Mark Whitley | 8f12243 | 2000-07-18 18:37:01 +0000 | [diff] [blame] | 275 | case 'c': |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 276 | print_match_counts++; |
Mark Whitley | 8f12243 | 2000-07-18 18:37:01 +0000 | [diff] [blame] | 277 | break; |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 278 | case 'e': |
| 279 | add_regex(optarg); |
| 280 | break; |
| 281 | case 'f': |
| 282 | load_regexes_from_file(optarg); |
| 283 | break; |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 284 | #ifdef BB_FEATURE_GREP_CONTEXT |
| 285 | case 'A': |
| 286 | lines_after = strtoul(optarg, &junk, 10); |
| 287 | if(*junk != '\0') |
| 288 | error_msg_and_die("invalid context length argument"); |
| 289 | break; |
| 290 | case 'B': |
| 291 | lines_before = strtoul(optarg, &junk, 10); |
| 292 | if(*junk != '\0') |
| 293 | error_msg_and_die("invalid context length argument"); |
| 294 | before_buf = (char **)calloc(lines_before, sizeof(char *)); |
| 295 | break; |
| 296 | case 'C': |
| 297 | lines_after = lines_before = strtoul(optarg, &junk, 10); |
| 298 | if(*junk != '\0') |
| 299 | error_msg_and_die("invalid context length argument"); |
| 300 | before_buf = (char **)calloc(lines_before, sizeof(char *)); |
| 301 | break; |
| 302 | #endif /* BB_FEATURE_GREP_CONTEXT */ |
Mark Whitley | 4391a16 | 2001-04-09 23:00:07 +0000 | [diff] [blame] | 303 | default: |
| 304 | show_usage(); |
Eric Andersen | 053b146 | 2000-06-13 06:24:53 +0000 | [diff] [blame] | 305 | } |
Eric Andersen | 053b146 | 2000-06-13 06:24:53 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 308 | /* if we didn't get a pattern from a -e and no command file was specified, |
| 309 | * argv[optind] should be the pattern. no pattern, no worky */ |
| 310 | if (nregexes == 0) { |
| 311 | if (argv[optind] == NULL) |
| 312 | show_usage(); |
| 313 | else { |
| 314 | add_regex(argv[optind]); |
| 315 | optind++; |
| 316 | } |
| 317 | } |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 318 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 319 | /* sanity checks */ |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 320 | if (print_match_counts || be_quiet || print_files_with_matches) { |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 321 | print_line_num = 0; |
| 322 | #ifdef BB_FEATURE_GREP_CONTEXT |
| 323 | lines_before = 0; |
| 324 | lines_after = 0; |
| 325 | #endif |
| 326 | } |
| 327 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 328 | /* 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] | 329 | * there is more than one file to grep, we will print the filenames */ |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 330 | if ((argc-1) - (optind) > 0) |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 331 | print_filename++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 332 | |
Mark Whitley | 2e1148b | 2000-06-28 22:59:30 +0000 | [diff] [blame] | 333 | /* If no files were specified, or '-' was specified, take input from |
| 334 | * stdin. Otherwise, we grep through all the files specified. */ |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 335 | if (argv[optind] == NULL || (strcmp(argv[optind], "-") == 0)) { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 336 | grep_file(stdin); |
Mark Whitley | 2ef880b | 2000-07-18 21:02:06 +0000 | [diff] [blame] | 337 | } |
| 338 | else { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 339 | int i; |
| 340 | FILE *file; |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 341 | for (i = optind; i < argc; i++) { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 342 | cur_file = argv[i]; |
| 343 | file = fopen(cur_file, "r"); |
| 344 | if (file == NULL) { |
| 345 | if (!suppress_err_msgs) |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 346 | perror_msg("%s", cur_file); |
Mark Whitley | 2ef880b | 2000-07-18 21:02:06 +0000 | [diff] [blame] | 347 | } |
| 348 | else { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 349 | grep_file(file); |
| 350 | fclose(file); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 351 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 352 | } |
| 353 | } |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 354 | |
Mark Whitley | b5c2985 | 2001-02-01 21:02:41 +0000 | [diff] [blame] | 355 | return !matched; /* invert return value 0 = success, 1 = failed */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 356 | } |