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