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 | extern void xregcomp(regex_t *preg, const char *regex, int cflags); |
| 32 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 33 | extern int optind; /* in unistd.h */ |
| 34 | extern int errno; /* for use with strerror() */ |
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 | |
| 45 | /* globals */ |
| 46 | static regex_t regex; /* storage space for compiled regular expression */ |
Matt Kraai | deb95f6 | 2000-08-06 15:25:53 +0000 | [diff] [blame] | 47 | static int matched; /* keeps track of whether we ever matched */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 48 | static char *cur_file = NULL; /* the current file we are reading */ |
| 49 | |
| 50 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 51 | static void grep_file(FILE *file) |
| 52 | { |
| 53 | char *line = NULL; |
| 54 | int ret; |
| 55 | int linenum = 0; |
Matt Kraai | deb95f6 | 2000-08-06 15:25:53 +0000 | [diff] [blame] | 56 | int nmatches = 0; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 57 | |
| 58 | while ((line = get_line_from_file(file)) != NULL) { |
Matt Kraai | 05e782d | 2001-02-01 16:49:30 +0000 | [diff] [blame] | 59 | chomp(line); |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 60 | linenum++; |
| 61 | ret = regexec(®ex, line, 0, NULL, 0); |
Mark Whitley | b5c2985 | 2001-02-01 21:02:41 +0000 | [diff] [blame^] | 62 | |
| 63 | /* test for a postitive-assertion match (regexec returned success (0) |
| 64 | * and the user did not specify invert search), or a negative-assertion |
| 65 | * match (vice versa) */ |
| 66 | if ((ret == 0 && !invert_search) || (ret == REG_NOMATCH && invert_search)) { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 67 | |
| 68 | /* if we found a match but were told to be quiet, stop here and |
| 69 | * return success */ |
| 70 | if (be_quiet) { |
| 71 | regfree(®ex); |
| 72 | exit(0); |
| 73 | } |
| 74 | |
Mark Whitley | b5c2985 | 2001-02-01 21:02:41 +0000 | [diff] [blame^] | 75 | /* otherwise, keep track of matches, print the matched line, and |
| 76 | * whatever else the user wanted */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 77 | nmatches++; |
Mark Whitley | b5c2985 | 2001-02-01 21:02:41 +0000 | [diff] [blame^] | 78 | if (!print_count_only) { |
| 79 | if (print_filename) |
| 80 | printf("%s:", cur_file); |
| 81 | if (print_line_num) |
| 82 | printf("%i:", linenum); |
| 83 | puts(line); |
Matt Kraai | 0810f72 | 2001-01-04 15:11:52 +0000 | [diff] [blame] | 84 | } |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 85 | } |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 86 | free(line); |
| 87 | } |
Mark Whitley | 8f12243 | 2000-07-18 18:37:01 +0000 | [diff] [blame] | 88 | |
| 89 | /* special-case post processing */ |
| 90 | if (print_count_only) { |
| 91 | if (print_filename) |
| 92 | printf("%s:", cur_file); |
| 93 | printf("%i\n", nmatches); |
| 94 | } |
| 95 | |
Matt Kraai | deb95f6 | 2000-08-06 15:25:53 +0000 | [diff] [blame] | 96 | /* record if we matched */ |
| 97 | if (nmatches != 0) |
| 98 | matched = 1; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 99 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 100 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 101 | extern int grep_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 102 | { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 103 | int opt; |
| 104 | int reflags; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 105 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 106 | /* do normal option parsing */ |
Mark Whitley | 8f12243 | 2000-07-18 18:37:01 +0000 | [diff] [blame] | 107 | while ((opt = getopt(argc, argv, "iHhnqvsc")) > 0) { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 108 | switch (opt) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 109 | case 'i': |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 110 | ignore_case++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 111 | break; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 112 | case 'H': |
| 113 | print_filename++; |
| 114 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 115 | case 'h': |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 116 | print_filename--; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 117 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 118 | case 'n': |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 119 | print_line_num++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 120 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 121 | case 'q': |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 122 | be_quiet++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 123 | break; |
John Beppu | f93a95d | 2000-04-24 18:07:30 +0000 | [diff] [blame] | 124 | case 'v': |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 125 | invert_search++; |
John Beppu | f93a95d | 2000-04-24 18:07:30 +0000 | [diff] [blame] | 126 | break; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 127 | case 's': |
| 128 | suppress_err_msgs++; |
| 129 | break; |
Mark Whitley | 8f12243 | 2000-07-18 18:37:01 +0000 | [diff] [blame] | 130 | case 'c': |
| 131 | print_count_only++; |
| 132 | break; |
Eric Andersen | 053b146 | 2000-06-13 06:24:53 +0000 | [diff] [blame] | 133 | } |
Eric Andersen | 053b146 | 2000-06-13 06:24:53 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 136 | /* argv[optind] should be the regex pattern; no pattern, no worky */ |
| 137 | if (argv[optind] == NULL) |
| 138 | usage(grep_usage); |
| 139 | |
Mark Whitley | 44735f8 | 2000-07-10 15:50:26 +0000 | [diff] [blame] | 140 | /* compile the regular expression |
| 141 | * we're not going to mess with sub-expressions, and we need to |
| 142 | * treat newlines right. */ |
Matt Kraai | 567cdd1 | 2000-10-13 18:55:06 +0000 | [diff] [blame] | 143 | reflags = REG_NOSUB; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 144 | if (ignore_case) |
| 145 | reflags |= REG_ICASE; |
Mark Whitley | c41e8c8 | 2000-07-12 23:35:21 +0000 | [diff] [blame] | 146 | xregcomp(®ex, argv[optind], reflags); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 147 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 148 | /* argv[(optind+1)..(argc-1)] should be names of file to grep through. If |
| 149 | * there is more than one file to grep, we will print the filenames */ |
| 150 | if ((argc-1) - (optind+1) > 0) |
| 151 | print_filename++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 152 | |
Mark Whitley | 2e1148b | 2000-06-28 22:59:30 +0000 | [diff] [blame] | 153 | /* If no files were specified, or '-' was specified, take input from |
| 154 | * stdin. Otherwise, we grep through all the files specified. */ |
Mark Whitley | 8bd891c | 2000-06-28 22:55:59 +0000 | [diff] [blame] | 155 | if (argv[optind+1] == NULL || (strcmp(argv[optind+1], "-") == 0)) { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 156 | grep_file(stdin); |
Mark Whitley | 2ef880b | 2000-07-18 21:02:06 +0000 | [diff] [blame] | 157 | } |
| 158 | else { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 159 | int i; |
| 160 | FILE *file; |
| 161 | for (i = optind + 1; i < argc; i++) { |
| 162 | cur_file = argv[i]; |
| 163 | file = fopen(cur_file, "r"); |
| 164 | if (file == NULL) { |
| 165 | if (!suppress_err_msgs) |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 166 | perror_msg("%s", cur_file); |
Mark Whitley | 2ef880b | 2000-07-18 21:02:06 +0000 | [diff] [blame] | 167 | } |
| 168 | else { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 169 | grep_file(file); |
| 170 | fclose(file); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 171 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 174 | |
| 175 | regfree(®ex); |
| 176 | |
Mark Whitley | b5c2985 | 2001-02-01 21:02:41 +0000 | [diff] [blame^] | 177 | return !matched; /* invert return value 0 = success, 1 = failed */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 178 | } |