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 */ |
| 37 | static int ignore_case = 0; |
| 38 | static int print_filename = 0; |
| 39 | static int print_line_num = 0; |
Mark Whitley | 8f12243 | 2000-07-18 18:37:01 +0000 | [diff] [blame] | 40 | static int print_count_only = 0; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 41 | static int be_quiet = 0; |
| 42 | static int invert_search = 0; |
| 43 | static int suppress_err_msgs = 0; |
| 44 | |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame^] | 45 | #ifdef BB_FEATURE_GREP_CONTEXT |
| 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; |
| 51 | #endif /* BB_FEATURE_GREP_CONTEXT */ |
| 52 | |
| 53 | /* globals used internally */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 54 | static regex_t regex; /* storage space for compiled regular expression */ |
Matt Kraai | deb95f6 | 2000-08-06 15:25:53 +0000 | [diff] [blame] | 55 | static int matched; /* keeps track of whether we ever matched */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 56 | static char *cur_file = NULL; /* the current file we are reading */ |
| 57 | |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame^] | 58 | static void print_line(const char *line, int linenum, char decoration) |
| 59 | { |
| 60 | #ifdef BB_FEATURE_GREP_CONTEXT |
| 61 | /* possibly print the little '--' seperator */ |
| 62 | if (last_line_printed && last_line_printed < linenum - 1) { |
| 63 | puts("--"); |
| 64 | } |
| 65 | last_line_printed = linenum; |
| 66 | #endif |
| 67 | if (print_filename) |
| 68 | printf("%s%c", cur_file, decoration); |
| 69 | if (print_line_num) |
| 70 | printf("%i%c", linenum, decoration); |
| 71 | puts(line); |
| 72 | } |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 73 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 74 | static void grep_file(FILE *file) |
| 75 | { |
| 76 | char *line = NULL; |
| 77 | int ret; |
| 78 | int linenum = 0; |
Matt Kraai | deb95f6 | 2000-08-06 15:25:53 +0000 | [diff] [blame] | 79 | int nmatches = 0; |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame^] | 80 | #ifdef BB_FEATURE_GREP_CONTEXT |
| 81 | int print_n_lines_after = 0; |
| 82 | int curpos = 0; /* track where we are in the circular 'before' buffer */ |
| 83 | int idx = 0; /* used for iteration through the circular buffer */ |
| 84 | #endif /* BB_FEATURE_GREP_CONTEXT */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 85 | |
| 86 | while ((line = get_line_from_file(file)) != NULL) { |
Matt Kraai | 05e782d | 2001-02-01 16:49:30 +0000 | [diff] [blame] | 87 | chomp(line); |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 88 | linenum++; |
Mark Whitley | b5c2985 | 2001-02-01 21:02:41 +0000 | [diff] [blame] | 89 | |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame^] | 90 | /* |
| 91 | * test for a postitive-assertion match (regexec returns success (0) |
Mark Whitley | b5c2985 | 2001-02-01 21:02:41 +0000 | [diff] [blame] | 92 | * and the user did not specify invert search), or a negative-assertion |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame^] | 93 | * match (regexec returns failure (REG_NOMATCH) and the user specified |
| 94 | * invert search) |
| 95 | */ |
| 96 | ret = regexec(®ex, line, 0, NULL, 0); |
Mark Whitley | b5c2985 | 2001-02-01 21:02:41 +0000 | [diff] [blame] | 97 | if ((ret == 0 && !invert_search) || (ret == REG_NOMATCH && invert_search)) { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 98 | |
| 99 | /* if we found a match but were told to be quiet, stop here and |
| 100 | * return success */ |
| 101 | if (be_quiet) { |
| 102 | regfree(®ex); |
| 103 | exit(0); |
| 104 | } |
| 105 | |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame^] | 106 | /* otherwise, keep track of matches and print the matched line */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 107 | nmatches++; |
Mark Whitley | b5c2985 | 2001-02-01 21:02:41 +0000 | [diff] [blame] | 108 | if (!print_count_only) { |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame^] | 109 | #ifdef BB_FEATURE_GREP_CONTEXT |
| 110 | int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1; |
| 111 | |
| 112 | /* if we were told to print 'before' lines and there is at least |
| 113 | * one line in the circular buffer, print them */ |
| 114 | if (lines_before && before_buf[prevpos] != NULL) |
| 115 | { |
| 116 | int first_buf_entry_line_num = linenum - lines_before; |
| 117 | |
| 118 | /* advance to the first entry in the circular buffer, and |
| 119 | * figure out the line number is of the first line in the |
| 120 | * buffer */ |
| 121 | idx = curpos; |
| 122 | while (before_buf[idx] == NULL) { |
| 123 | idx = (idx + 1) % lines_before; |
| 124 | first_buf_entry_line_num++; |
| 125 | } |
| 126 | |
| 127 | /* now print each line in the buffer, clearing them as we go */ |
| 128 | while (before_buf[idx] != NULL) { |
| 129 | print_line(before_buf[idx], first_buf_entry_line_num, '-'); |
| 130 | free(before_buf[idx]); |
| 131 | before_buf[idx] = NULL; |
| 132 | idx = (idx + 1) % lines_before; |
| 133 | first_buf_entry_line_num++; |
| 134 | } |
| 135 | |
| 136 | } |
| 137 | |
| 138 | /* make a note that we need to print 'after' lines */ |
| 139 | print_n_lines_after = lines_after; |
| 140 | #endif /* BB_FEATURE_GREP_CONTEXT */ |
| 141 | print_line(line, linenum, ':'); |
Matt Kraai | 0810f72 | 2001-01-04 15:11:52 +0000 | [diff] [blame] | 142 | } |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 143 | } |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame^] | 144 | #ifdef BB_FEATURE_GREP_CONTEXT |
| 145 | else { /* no match */ |
| 146 | /* Add the line to the circular 'before' buffer */ |
| 147 | if(lines_before) |
| 148 | { |
| 149 | if(before_buf[curpos]) |
| 150 | free(before_buf[curpos]); |
| 151 | before_buf[curpos] = strdup(line); |
| 152 | curpos = (curpos + 1) % lines_before; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /* if we need to print some context lines after the last match, do so */ |
| 157 | if (print_n_lines_after && (last_line_printed != linenum)) { |
| 158 | print_line(line, linenum, '-'); |
| 159 | print_n_lines_after--; |
| 160 | } |
| 161 | #endif /* BB_FEATURE_GREP_CONTEXT */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 162 | free(line); |
| 163 | } |
Mark Whitley | 8f12243 | 2000-07-18 18:37:01 +0000 | [diff] [blame] | 164 | |
| 165 | /* special-case post processing */ |
| 166 | if (print_count_only) { |
| 167 | if (print_filename) |
| 168 | printf("%s:", cur_file); |
| 169 | printf("%i\n", nmatches); |
| 170 | } |
| 171 | |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame^] | 172 | /* remember if we matched */ |
Matt Kraai | deb95f6 | 2000-08-06 15:25:53 +0000 | [diff] [blame] | 173 | if (nmatches != 0) |
| 174 | matched = 1; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 175 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 176 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 177 | extern int grep_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 178 | { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 179 | int opt; |
| 180 | int reflags; |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame^] | 181 | #ifdef BB_FEATURE_GREP_CONTEXT |
| 182 | char *junk; |
| 183 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 184 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 185 | /* do normal option parsing */ |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame^] | 186 | while ((opt = getopt(argc, argv, "iHhnqvsc" |
| 187 | #ifdef BB_FEATURE_GREP_CONTEXT |
| 188 | "A:B:C:" |
| 189 | #endif |
| 190 | )) > 0) { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 191 | switch (opt) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 192 | case 'i': |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 193 | ignore_case++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 194 | break; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 195 | case 'H': |
| 196 | print_filename++; |
| 197 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 198 | case 'h': |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 199 | print_filename--; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 200 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 201 | case 'n': |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 202 | print_line_num++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 203 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 204 | case 'q': |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 205 | be_quiet++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 206 | break; |
John Beppu | f93a95d | 2000-04-24 18:07:30 +0000 | [diff] [blame] | 207 | case 'v': |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 208 | invert_search++; |
John Beppu | f93a95d | 2000-04-24 18:07:30 +0000 | [diff] [blame] | 209 | break; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 210 | case 's': |
| 211 | suppress_err_msgs++; |
| 212 | break; |
Mark Whitley | 8f12243 | 2000-07-18 18:37:01 +0000 | [diff] [blame] | 213 | case 'c': |
| 214 | print_count_only++; |
| 215 | break; |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame^] | 216 | #ifdef BB_FEATURE_GREP_CONTEXT |
| 217 | case 'A': |
| 218 | lines_after = strtoul(optarg, &junk, 10); |
| 219 | if(*junk != '\0') |
| 220 | error_msg_and_die("invalid context length argument"); |
| 221 | break; |
| 222 | case 'B': |
| 223 | lines_before = strtoul(optarg, &junk, 10); |
| 224 | if(*junk != '\0') |
| 225 | error_msg_and_die("invalid context length argument"); |
| 226 | before_buf = (char **)calloc(lines_before, sizeof(char *)); |
| 227 | break; |
| 228 | case 'C': |
| 229 | lines_after = lines_before = strtoul(optarg, &junk, 10); |
| 230 | if(*junk != '\0') |
| 231 | error_msg_and_die("invalid context length argument"); |
| 232 | before_buf = (char **)calloc(lines_before, sizeof(char *)); |
| 233 | break; |
| 234 | #endif /* BB_FEATURE_GREP_CONTEXT */ |
Eric Andersen | 053b146 | 2000-06-13 06:24:53 +0000 | [diff] [blame] | 235 | } |
Eric Andersen | 053b146 | 2000-06-13 06:24:53 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 238 | /* argv[optind] should be the regex pattern; no pattern, no worky */ |
| 239 | if (argv[optind] == NULL) |
| 240 | usage(grep_usage); |
| 241 | |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame^] | 242 | /* sanity check */ |
| 243 | if (print_count_only || be_quiet) { |
| 244 | print_line_num = 0; |
| 245 | #ifdef BB_FEATURE_GREP_CONTEXT |
| 246 | lines_before = 0; |
| 247 | lines_after = 0; |
| 248 | #endif |
| 249 | } |
| 250 | |
Mark Whitley | 44735f8 | 2000-07-10 15:50:26 +0000 | [diff] [blame] | 251 | /* compile the regular expression |
| 252 | * we're not going to mess with sub-expressions, and we need to |
| 253 | * treat newlines right. */ |
Matt Kraai | 567cdd1 | 2000-10-13 18:55:06 +0000 | [diff] [blame] | 254 | reflags = REG_NOSUB; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 255 | if (ignore_case) |
| 256 | reflags |= REG_ICASE; |
Mark Whitley | c41e8c8 | 2000-07-12 23:35:21 +0000 | [diff] [blame] | 257 | xregcomp(®ex, argv[optind], reflags); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 258 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 259 | /* argv[(optind+1)..(argc-1)] should be names of file to grep through. If |
| 260 | * there is more than one file to grep, we will print the filenames */ |
| 261 | if ((argc-1) - (optind+1) > 0) |
| 262 | print_filename++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 263 | |
Mark Whitley | 2e1148b | 2000-06-28 22:59:30 +0000 | [diff] [blame] | 264 | /* If no files were specified, or '-' was specified, take input from |
| 265 | * stdin. Otherwise, we grep through all the files specified. */ |
Mark Whitley | 8bd891c | 2000-06-28 22:55:59 +0000 | [diff] [blame] | 266 | if (argv[optind+1] == NULL || (strcmp(argv[optind+1], "-") == 0)) { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 267 | grep_file(stdin); |
Mark Whitley | 2ef880b | 2000-07-18 21:02:06 +0000 | [diff] [blame] | 268 | } |
| 269 | else { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 270 | int i; |
| 271 | FILE *file; |
| 272 | for (i = optind + 1; i < argc; i++) { |
| 273 | cur_file = argv[i]; |
| 274 | file = fopen(cur_file, "r"); |
| 275 | if (file == NULL) { |
| 276 | if (!suppress_err_msgs) |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 277 | perror_msg("%s", cur_file); |
Mark Whitley | 2ef880b | 2000-07-18 21:02:06 +0000 | [diff] [blame] | 278 | } |
| 279 | else { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 280 | grep_file(file); |
| 281 | fclose(file); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 282 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 283 | } |
| 284 | } |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 285 | |
| 286 | regfree(®ex); |
| 287 | |
Mark Whitley | b5c2985 | 2001-02-01 21:02:41 +0000 | [diff] [blame] | 288 | return !matched; /* invert return value 0 = success, 1 = failed */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 289 | } |