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 | * |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 4 | * Copyright (C) 1999,2000 by Lineo, inc. |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 5 | * Written by Mark Whitley <markw@lineo.com>, <markw@enol.com> |
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> |
| 25 | #include <unistd.h> /* for getopt() */ |
| 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> |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 29 | #include "internal.h" |
| 30 | |
| 31 | extern int optind; /* in unistd.h */ |
| 32 | extern int errno; /* for use with strerror() */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 33 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 34 | static const char grep_usage[] = |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 35 | "grep [-ihHnqvs] pattern [files...]\n" |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 36 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 37 | "\nSearch for PATTERN in each FILE or standard input.\n\n" |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 38 | "OPTIONS:\n" |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 39 | "\t-H\tprefix output lines with filename where match was found\n" |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 40 | "\t-h\tsuppress the prefixing filename on output\n" |
| 41 | "\t-i\tignore case distinctions\n" |
| 42 | "\t-n\tprint line number with output lines\n" |
John Beppu | f93a95d | 2000-04-24 18:07:30 +0000 | [diff] [blame] | 43 | "\t-q\tbe quiet. Returns 0 if result was found, 1 otherwise\n" |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 44 | "\t-v\tselect non-matching lines\n" |
| 45 | "\t-s\tsuppress file open/read error messages\n\n" |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 46 | #endif |
| 47 | ; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 48 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 49 | static const int GROWBY = 80; /* how large we will grow strings by */ |
Eric Andersen | fbb39c8 | 1999-11-08 17:00:52 +0000 | [diff] [blame] | 50 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 51 | /* options */ |
| 52 | static int ignore_case = 0; |
| 53 | static int print_filename = 0; |
| 54 | static int print_line_num = 0; |
| 55 | static int be_quiet = 0; |
| 56 | static int invert_search = 0; |
| 57 | static int suppress_err_msgs = 0; |
| 58 | |
| 59 | /* globals */ |
| 60 | static regex_t regex; /* storage space for compiled regular expression */ |
| 61 | static int nmatches = 0; /* keeps track of the number of matches */ |
| 62 | static char *cur_file = NULL; /* the current file we are reading */ |
| 63 | |
| 64 | |
| 65 | /* This returns a malloc'ed char * which must be stored and free'ed */ |
| 66 | /* XXX: This function should probably go in a 'common'/'util'/'misc' file |
| 67 | * somewhere so it can be used by other folks. */ |
| 68 | static char *get_line_from_file(FILE *file) |
Eric Andersen | fbb39c8 | 1999-11-08 17:00:52 +0000 | [diff] [blame] | 69 | { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 70 | int ch; |
| 71 | int idx = 0; |
| 72 | char *linebuf = NULL; |
| 73 | int linebufsz = 0; |
Eric Andersen | fbb39c8 | 1999-11-08 17:00:52 +0000 | [diff] [blame] | 74 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 75 | while (1) { |
| 76 | ch = fgetc(file); |
| 77 | if (ch == EOF) |
| 78 | break; |
| 79 | /* grow the line buffer as necessary */ |
| 80 | if (idx > linebufsz-1) |
| 81 | linebuf = realloc(linebuf, linebufsz += GROWBY); |
| 82 | linebuf[idx++] = (char)ch; |
| 83 | if ((char)ch == '\n') |
| 84 | break; |
Eric Andersen | fbb39c8 | 1999-11-08 17:00:52 +0000 | [diff] [blame] | 85 | } |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 86 | |
| 87 | if (idx == 0) |
| 88 | return NULL; |
| 89 | |
| 90 | linebuf[idx] = 0; |
| 91 | return linebuf; |
Eric Andersen | fbb39c8 | 1999-11-08 17:00:52 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 94 | static void print_matched_line(char *line, int linenum) |
| 95 | { |
| 96 | if (print_filename) |
| 97 | printf("%s:", cur_file); |
| 98 | if (print_line_num) |
| 99 | printf("%i:", linenum); |
| 100 | |
| 101 | printf("%s", line); |
| 102 | } |
| 103 | |
| 104 | static void grep_file(FILE *file) |
| 105 | { |
| 106 | char *line = NULL; |
| 107 | int ret; |
| 108 | int linenum = 0; |
| 109 | |
| 110 | while ((line = get_line_from_file(file)) != NULL) { |
| 111 | linenum++; |
| 112 | ret = regexec(®ex, line, 0, NULL, 0); |
| 113 | if (ret == 0 && !invert_search) { /* match */ |
| 114 | |
| 115 | /* if we found a match but were told to be quiet, stop here and |
| 116 | * return success */ |
| 117 | if (be_quiet) { |
| 118 | regfree(®ex); |
| 119 | exit(0); |
| 120 | } |
| 121 | |
| 122 | nmatches++; |
| 123 | |
| 124 | print_matched_line(line, linenum); |
| 125 | |
| 126 | } else if (ret == REG_NOMATCH && invert_search) { |
| 127 | print_matched_line(line, linenum); |
| 128 | } |
| 129 | |
| 130 | free(line); |
| 131 | } |
| 132 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 133 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 134 | extern int grep_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 135 | { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 136 | int opt; |
| 137 | int reflags; |
| 138 | int ret; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 139 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 140 | /* do special-case option parsing */ |
| 141 | if (argv[1] && (strcmp(argv[1], "--help") == 0)) |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 142 | usage(grep_usage); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 143 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 144 | /* do normal option parsing */ |
| 145 | while ((opt = getopt(argc, argv, "iHhnqvs")) > 0) { |
| 146 | switch (opt) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 147 | case 'i': |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 148 | ignore_case++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 149 | break; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 150 | case 'H': |
| 151 | print_filename++; |
| 152 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 153 | case 'h': |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 154 | print_filename--; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 155 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 156 | case 'n': |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 157 | print_line_num++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 158 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 159 | case 'q': |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 160 | be_quiet++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 161 | break; |
John Beppu | f93a95d | 2000-04-24 18:07:30 +0000 | [diff] [blame] | 162 | case 'v': |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 163 | invert_search++; |
John Beppu | f93a95d | 2000-04-24 18:07:30 +0000 | [diff] [blame] | 164 | break; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 165 | case 's': |
| 166 | suppress_err_msgs++; |
| 167 | break; |
Eric Andersen | 053b146 | 2000-06-13 06:24:53 +0000 | [diff] [blame] | 168 | } |
Eric Andersen | 053b146 | 2000-06-13 06:24:53 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 171 | /* argv[optind] should be the regex pattern; no pattern, no worky */ |
| 172 | if (argv[optind] == NULL) |
| 173 | usage(grep_usage); |
| 174 | |
| 175 | /* compile the regular expression */ |
| 176 | reflags = REG_NOSUB; /* we're not going to mess with sub-expressions */ |
| 177 | if (ignore_case) |
| 178 | reflags |= REG_ICASE; |
| 179 | if ((ret = regcomp(®ex, argv[optind], reflags)) != 0) { |
| 180 | int errmsgsz = regerror(ret, ®ex, NULL, 0); |
| 181 | char *errmsg = malloc(errmsgsz); |
| 182 | if (errmsg == NULL) { |
| 183 | fprintf(stderr, "grep: memory error\n"); |
| 184 | regfree(®ex); |
| 185 | exit(1); |
| 186 | } |
| 187 | regerror(ret, ®ex, errmsg, errmsgsz); |
| 188 | fprintf(stderr, "grep: %s\n", errmsg); |
| 189 | free(errmsg); |
| 190 | regfree(®ex); |
| 191 | exit(1); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 194 | /* argv[(optind+1)..(argc-1)] should be names of file to grep through. If |
| 195 | * there is more than one file to grep, we will print the filenames */ |
| 196 | if ((argc-1) - (optind+1) > 0) |
| 197 | print_filename++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 198 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 199 | /* If no files were specified, take input from stdin. Otherwise, we grep |
| 200 | * through all the files specified. */ |
| 201 | if (argv[optind+1] == NULL) { |
| 202 | grep_file(stdin); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 203 | } else { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 204 | int i; |
| 205 | FILE *file; |
| 206 | for (i = optind + 1; i < argc; i++) { |
| 207 | cur_file = argv[i]; |
| 208 | file = fopen(cur_file, "r"); |
| 209 | if (file == NULL) { |
| 210 | if (!suppress_err_msgs) |
| 211 | fprintf(stderr, "grep: %s: %s\n", cur_file, strerror(errno)); |
| 212 | } else { |
| 213 | grep_file(file); |
| 214 | fclose(file); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 215 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame^] | 218 | |
| 219 | regfree(®ex); |
| 220 | |
| 221 | if (nmatches == 0) |
| 222 | return 1; |
| 223 | |
| 224 | return 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 225 | } |