Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 2 | /* |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 3 | * Mini grep implementation for busybox using libc regex. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 7 | * |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 8 | * Licensed under the GPL v2, see the file LICENSE in this tarball. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 9 | */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 10 | /* BB_AUDIT SUSv3 defects - unsupported option -x. */ |
| 11 | /* BB_AUDIT GNU defects - always acts as -a. */ |
| 12 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/grep.html */ |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 13 | /* |
Eric Andersen | 7f164cd | 2004-05-26 09:46:41 +0000 | [diff] [blame] | 14 | * Apr 2004 by Vladimir Oleynik <dzo@simtreas.ru> - |
| 15 | * correction "-e pattern1 -e pattern2" logic and more optimizations. |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 16 | */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 17 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 19 | #include <stdlib.h> |
Eric Andersen | e9b527a | 2000-07-09 05:56:14 +0000 | [diff] [blame] | 20 | #include <getopt.h> |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 21 | #include <string.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 22 | #include <errno.h> |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 23 | #include "busybox.h" |
"Vladimir N. Oleynik" | 23f62fc | 2005-09-14 16:59:11 +0000 | [diff] [blame] | 24 | #include "xregex.h" |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 25 | |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 26 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 27 | /* options */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 28 | static unsigned long opt; |
Eric Andersen | dec7f81 | 2004-05-26 11:47:55 +0000 | [diff] [blame] | 29 | #define GREP_OPTS "lnqvscFiHhe:f:L" |
Eric Andersen | abc513a | 2004-05-26 11:48:29 +0000 | [diff] [blame] | 30 | #define GREP_OPT_l (1<<0) |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 31 | #define PRINT_FILES_WITH_MATCHES (opt & GREP_OPT_l) |
Eric Andersen | abc513a | 2004-05-26 11:48:29 +0000 | [diff] [blame] | 32 | #define GREP_OPT_n (1<<1) |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 33 | #define PRINT_LINE_NUM (opt & GREP_OPT_n) |
Eric Andersen | abc513a | 2004-05-26 11:48:29 +0000 | [diff] [blame] | 34 | #define GREP_OPT_q (1<<2) |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 35 | #define BE_QUIET (opt & GREP_OPT_q) |
Eric Andersen | abc513a | 2004-05-26 11:48:29 +0000 | [diff] [blame] | 36 | #define GREP_OPT_v (1<<3) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 37 | typedef char invert_search_t; |
| 38 | static invert_search_t invert_search; |
Eric Andersen | abc513a | 2004-05-26 11:48:29 +0000 | [diff] [blame] | 39 | #define GREP_OPT_s (1<<4) |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 40 | #define SUPPRESS_ERR_MSGS (opt & GREP_OPT_s) |
Eric Andersen | abc513a | 2004-05-26 11:48:29 +0000 | [diff] [blame] | 41 | #define GREP_OPT_c (1<<5) |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 42 | #define PRINT_MATCH_COUNTS (opt & GREP_OPT_c) |
Eric Andersen | abc513a | 2004-05-26 11:48:29 +0000 | [diff] [blame] | 43 | #define GREP_OPT_F (1<<6) |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 44 | #define FGREP_FLAG (opt & GREP_OPT_F) |
Eric Andersen | abc513a | 2004-05-26 11:48:29 +0000 | [diff] [blame] | 45 | #define GREP_OPT_i (1<<7) |
| 46 | #define GREP_OPT_H (1<<8) |
| 47 | #define GREP_OPT_h (1<<9) |
| 48 | #define GREP_OPT_e (1<<10) |
| 49 | #define GREP_OPT_f (1<<11) |
| 50 | #define GREP_OPT_L (1<<12) |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 51 | #define PRINT_FILES_WITHOUT_MATCHES ((opt & GREP_OPT_L) != 0) |
| 52 | #if ENABLE_FEATURE_GREP_CONTEXT |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 53 | #define GREP_OPT_CONTEXT "A:B:C" |
Eric Andersen | abc513a | 2004-05-26 11:48:29 +0000 | [diff] [blame] | 54 | #define GREP_OPT_A (1<<13) |
| 55 | #define GREP_OPT_B (1<<14) |
| 56 | #define GREP_OPT_C (1<<15) |
| 57 | #define GREP_OPT_E (1<<16) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 58 | #else |
| 59 | #define GREP_OPT_CONTEXT "" |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 60 | #define GREP_OPT_A (0) |
| 61 | #define GREP_OPT_B (0) |
| 62 | #define GREP_OPT_C (0) |
Eric Andersen | abc513a | 2004-05-26 11:48:29 +0000 | [diff] [blame] | 63 | #define GREP_OPT_E (1<<13) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 64 | #endif |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 65 | #if ENABLE_FEATURE_GREP_EGREP_ALIAS |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 66 | # define OPT_EGREP "E" |
| 67 | #else |
| 68 | # define OPT_EGREP "" |
| 69 | #endif |
| 70 | |
| 71 | static int reflags; |
| 72 | static int print_filename; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 73 | |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 74 | #if ENABLE_FEATURE_GREP_CONTEXT |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 75 | static int lines_before; |
| 76 | static int lines_after; |
| 77 | static char **before_buf; |
| 78 | static int last_line_printed; |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 79 | #endif /* ENABLE_FEATURE_GREP_CONTEXT */ |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 80 | |
| 81 | /* globals used internally */ |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 82 | static llist_t *pattern_head; /* growable list of patterns to match */ |
| 83 | static char *cur_file; /* the current file we are reading */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 84 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 85 | |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 86 | static void print_line(const char *line, int linenum, char decoration) |
| 87 | { |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 88 | #if ENABLE_FEATURE_GREP_CONTEXT |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 89 | /* possibly print the little '--' separator */ |
Matt Kraai | edc8065 | 2001-05-22 14:29:27 +0000 | [diff] [blame] | 90 | if ((lines_before || lines_after) && last_line_printed && |
| 91 | last_line_printed < linenum - 1) { |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 92 | puts("--"); |
| 93 | } |
| 94 | last_line_printed = linenum; |
| 95 | #endif |
Mike Frysinger | 5ba5f4d | 2005-04-16 04:56:11 +0000 | [diff] [blame] | 96 | if (print_filename > 0) |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 97 | printf("%s%c", cur_file, decoration); |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 98 | if (PRINT_LINE_NUM) |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 99 | printf("%i%c", linenum, decoration); |
| 100 | puts(line); |
| 101 | } |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 102 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 103 | |
| 104 | static int grep_file(FILE *file) |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 105 | { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 106 | char *line; |
| 107 | invert_search_t ret; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 108 | int linenum = 0; |
Matt Kraai | deb95f6 | 2000-08-06 15:25:53 +0000 | [diff] [blame] | 109 | int nmatches = 0; |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 110 | #if ENABLE_FEATURE_GREP_CONTEXT |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 111 | int print_n_lines_after = 0; |
| 112 | int curpos = 0; /* track where we are in the circular 'before' buffer */ |
| 113 | int idx = 0; /* used for iteration through the circular buffer */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 114 | #endif /* ENABLE_FEATURE_GREP_CONTEXT */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 115 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 116 | while ((line = bb_get_chomped_line_from_file(file)) != NULL) { |
Glenn L McGrath | 26df70a | 2003-04-27 01:50:57 +0000 | [diff] [blame] | 117 | llist_t *pattern_ptr = pattern_head; |
| 118 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 119 | linenum++; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 120 | ret = 0; |
Glenn L McGrath | 26df70a | 2003-04-27 01:50:57 +0000 | [diff] [blame] | 121 | while (pattern_ptr) { |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 122 | if (FGREP_FLAG) { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 123 | ret = strstr(line, pattern_ptr->data) != NULL; |
Glenn L McGrath | 26df70a | 2003-04-27 01:50:57 +0000 | [diff] [blame] | 124 | } else { |
| 125 | /* |
| 126 | * test for a postitive-assertion match (regexec returns success (0) |
| 127 | * and the user did not specify invert search), or a negative-assertion |
| 128 | * match (regexec returns failure (REG_NOMATCH) and the user specified |
| 129 | * invert search) |
| 130 | */ |
| 131 | regex_t regex; |
| 132 | xregcomp(®ex, pattern_ptr->data, reflags); |
Eric Andersen | 7f164cd | 2004-05-26 09:46:41 +0000 | [diff] [blame] | 133 | ret |= regexec(®ex, line, 0, NULL, 0) == 0; |
Glenn L McGrath | 26df70a | 2003-04-27 01:50:57 +0000 | [diff] [blame] | 134 | regfree(®ex); |
| 135 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 136 | pattern_ptr = pattern_ptr->link; |
| 137 | } /* while (pattern_ptr) */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 138 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 139 | if ((ret ^ invert_search)) { |
| 140 | |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 141 | if (PRINT_FILES_WITH_MATCHES || BE_QUIET) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 142 | free(line); |
| 143 | |
| 144 | /* if we found a match but were told to be quiet, stop here */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 145 | if (BE_QUIET || PRINT_FILES_WITHOUT_MATCHES) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 146 | return -1; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 147 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 148 | /* keep track of matches */ |
| 149 | nmatches++; |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 150 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 151 | /* if we're just printing filenames, we stop after the first match */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 152 | if (PRINT_FILES_WITH_MATCHES) |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 153 | break; |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 154 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 155 | /* print the matched line */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 156 | if (PRINT_MATCH_COUNTS == 0) { |
| 157 | #if ENABLE_FEATURE_GREP_CONTEXT |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 158 | int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1; |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 159 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 160 | /* if we were told to print 'before' lines and there is at least |
| 161 | * one line in the circular buffer, print them */ |
| 162 | if (lines_before && before_buf[prevpos] != NULL) { |
| 163 | int first_buf_entry_line_num = linenum - lines_before; |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 164 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 165 | /* advance to the first entry in the circular buffer, and |
| 166 | * figure out the line number is of the first line in the |
| 167 | * buffer */ |
| 168 | idx = curpos; |
| 169 | while (before_buf[idx] == NULL) { |
| 170 | idx = (idx + 1) % lines_before; |
| 171 | first_buf_entry_line_num++; |
| 172 | } |
| 173 | |
| 174 | /* now print each line in the buffer, clearing them as we go */ |
| 175 | while (before_buf[idx] != NULL) { |
| 176 | print_line(before_buf[idx], first_buf_entry_line_num, '-'); |
| 177 | free(before_buf[idx]); |
| 178 | before_buf[idx] = NULL; |
| 179 | idx = (idx + 1) % lines_before; |
| 180 | first_buf_entry_line_num++; |
| 181 | } |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 184 | /* make a note that we need to print 'after' lines */ |
| 185 | print_n_lines_after = lines_after; |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 186 | #endif |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 187 | print_line(line, linenum, ':'); |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 188 | } |
Matt Kraai | 0810f72 | 2001-01-04 15:11:52 +0000 | [diff] [blame] | 189 | } |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 190 | #if ENABLE_FEATURE_GREP_CONTEXT |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 191 | else { /* no match */ |
| 192 | /* Add the line to the circular 'before' buffer */ |
| 193 | if(lines_before) { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 194 | free(before_buf[curpos]); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 195 | before_buf[curpos] = bb_xstrdup(line); |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 196 | curpos = (curpos + 1) % lines_before; |
| 197 | } |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 198 | } |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 199 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 200 | /* if we need to print some context lines after the last match, do so */ |
| 201 | if (print_n_lines_after && (last_line_printed != linenum)) { |
| 202 | print_line(line, linenum, '-'); |
| 203 | print_n_lines_after--; |
| 204 | } |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 205 | #endif /* ENABLE_FEATURE_GREP_CONTEXT */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 206 | free(line); |
| 207 | } |
Mark Whitley | 8f12243 | 2000-07-18 18:37:01 +0000 | [diff] [blame] | 208 | |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 209 | |
| 210 | /* 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] | 211 | * matches, just filenames and possibly match counts */ |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 212 | |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 213 | /* grep -c: print [filename:]count, even if count is zero */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 214 | if (PRINT_MATCH_COUNTS) { |
Mike Frysinger | 5ba5f4d | 2005-04-16 04:56:11 +0000 | [diff] [blame] | 215 | if (print_filename > 0) |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 216 | printf("%s:", cur_file); |
Eric Andersen | 0034443 | 2001-07-31 23:18:49 +0000 | [diff] [blame] | 217 | printf("%d\n", nmatches); |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 218 | } |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 219 | |
| 220 | /* grep -l: print just the filename, but only if we grepped the line in the file */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 221 | if (PRINT_FILES_WITH_MATCHES && nmatches > 0) { |
Matt Kraai | 59df6f7 | 2001-05-16 14:21:09 +0000 | [diff] [blame] | 222 | puts(cur_file); |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Eric Andersen | dec7f81 | 2004-05-26 11:47:55 +0000 | [diff] [blame] | 225 | /* grep -L: print just the filename, but only if we didn't grep the line in the file */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 226 | if (PRINT_FILES_WITHOUT_MATCHES && nmatches == 0) { |
Eric Andersen | dec7f81 | 2004-05-26 11:47:55 +0000 | [diff] [blame] | 227 | puts(cur_file); |
| 228 | } |
| 229 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 230 | return nmatches; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 231 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 232 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 233 | static void load_regexes_from_file(llist_t *fopt) |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 234 | { |
| 235 | char *line; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 236 | FILE *f; |
| 237 | |
| 238 | while(fopt) { |
| 239 | llist_t *cur = fopt; |
| 240 | char *ffile = cur->data; |
| 241 | |
| 242 | fopt = cur->link; |
| 243 | free(cur); |
| 244 | f = bb_xfopen(ffile, "r"); |
Eric Andersen | 31c27a9 | 2004-10-08 08:10:57 +0000 | [diff] [blame] | 245 | while ((line = bb_get_chomped_line_from_file(f)) != NULL) { |
| 246 | pattern_head = llist_add_to(pattern_head, line); |
| 247 | } |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 250 | |
| 251 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 252 | extern int grep_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 253 | { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 254 | FILE *file; |
| 255 | int matched; |
Eric Andersen | 31c27a9 | 2004-10-08 08:10:57 +0000 | [diff] [blame] | 256 | llist_t *fopt = NULL; |
"Vladimir N. Oleynik" | cf40d81 | 2005-09-23 13:23:15 +0000 | [diff] [blame] | 257 | int error_open_count = 0; |
Matt Kraai | 999623e | 2001-10-29 15:49:03 +0000 | [diff] [blame] | 258 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 259 | /* do normal option parsing */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 260 | #if ENABLE_FEATURE_GREP_CONTEXT |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 261 | { |
| 262 | char *junk; |
| 263 | char *slines_after; |
| 264 | char *slines_before; |
| 265 | char *Copt; |
| 266 | |
"Vladimir N. Oleynik" | f704b27 | 2005-10-14 09:56:52 +0000 | [diff] [blame] | 267 | bb_opt_complementally = "H-h:e::f::C-AB"; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 268 | opt = bb_getopt_ulflags(argc, argv, |
| 269 | GREP_OPTS GREP_OPT_CONTEXT OPT_EGREP, |
| 270 | &pattern_head, &fopt, |
| 271 | &slines_after, &slines_before, &Copt); |
| 272 | |
| 273 | if(opt & GREP_OPT_C) { |
| 274 | /* C option unseted A and B options, but next -A or -B |
| 275 | may be ovewrite own option */ |
| 276 | if(!(opt & GREP_OPT_A)) /* not overwtited */ |
| 277 | slines_after = Copt; |
| 278 | if(!(opt & GREP_OPT_B)) /* not overwtited */ |
| 279 | slines_before = Copt; |
| 280 | opt |= GREP_OPT_A|GREP_OPT_B; /* set for parse now */ |
Eric Andersen | 053b146 | 2000-06-13 06:24:53 +0000 | [diff] [blame] | 281 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 282 | if(opt & GREP_OPT_A) { |
| 283 | lines_after = strtoul(slines_after, &junk, 10); |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 284 | if(*junk != '\0') |
| 285 | bb_error_msg_and_die("invalid context length argument"); |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 286 | } |
| 287 | if(opt & GREP_OPT_B) { |
| 288 | lines_before = strtoul(slines_before, &junk, 10); |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 289 | if(*junk != '\0') |
| 290 | bb_error_msg_and_die("invalid context length argument"); |
| 291 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 292 | /* sanity checks after parse may be invalid numbers ;-) */ |
Eric Andersen | dec7f81 | 2004-05-26 11:47:55 +0000 | [diff] [blame] | 293 | if ((opt & (GREP_OPT_c|GREP_OPT_q|GREP_OPT_l|GREP_OPT_L))) { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 294 | opt &= ~GREP_OPT_n; |
| 295 | lines_before = 0; |
| 296 | lines_after = 0; |
| 297 | } else if(lines_before > 0) |
| 298 | before_buf = (char **)xcalloc(lines_before, sizeof(char *)); |
| 299 | } |
| 300 | #else |
| 301 | /* with auto sanity checks */ |
"Vladimir N. Oleynik" | f704b27 | 2005-10-14 09:56:52 +0000 | [diff] [blame] | 302 | bb_opt_complementally = "H-h:e::f::c-n:q-n:l-n"; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 303 | opt = bb_getopt_ulflags(argc, argv, GREP_OPTS OPT_EGREP, |
| 304 | &pattern_head, &fopt); |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 305 | #endif |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 306 | invert_search = (opt & GREP_OPT_v) != 0; /* 0 | 1 */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 307 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 308 | if(opt & GREP_OPT_H) |
| 309 | print_filename++; |
| 310 | if(opt & GREP_OPT_h) |
| 311 | print_filename--; |
| 312 | if(opt & GREP_OPT_f) |
| 313 | load_regexes_from_file(fopt); |
| 314 | |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 315 | if(ENABLE_FEATURE_GREP_FGREP_ALIAS && bb_applet_name[0] == 'f') |
| 316 | opt |= GREP_OPT_F; |
Mike Frysinger | 15ca586 | 2005-07-31 22:41:05 +0000 | [diff] [blame] | 317 | |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 318 | if(ENABLE_FEATURE_GREP_EGREP_ALIAS && |
| 319 | (bb_applet_name[0] == 'e' || (opt & GREP_OPT_E))) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 320 | reflags = REG_EXTENDED | REG_NOSUB; |
| 321 | else |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 322 | reflags = REG_NOSUB; |
| 323 | |
| 324 | if(opt & GREP_OPT_i) |
| 325 | reflags |= REG_ICASE; |
| 326 | |
| 327 | argv += optind; |
| 328 | argc -= optind; |
Eric Andersen | 053b146 | 2000-06-13 06:24:53 +0000 | [diff] [blame] | 329 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 330 | /* if we didn't get a pattern from a -e and no command file was specified, |
| 331 | * argv[optind] should be the pattern. no pattern, no worky */ |
Glenn L McGrath | 26df70a | 2003-04-27 01:50:57 +0000 | [diff] [blame] | 332 | if (pattern_head == NULL) { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 333 | if (*argv == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 334 | bb_show_usage(); |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 335 | else { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 336 | pattern_head = llist_add_to(pattern_head, *argv++); |
| 337 | argc--; |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 338 | } |
| 339 | } |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 340 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 341 | /* 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] | 342 | * there is more than one file to grep, we will print the filenames */ |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 343 | if (argc > 1) { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 344 | print_filename++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 345 | |
Mark Whitley | 2e1148b | 2000-06-28 22:59:30 +0000 | [diff] [blame] | 346 | /* If no files were specified, or '-' was specified, take input from |
| 347 | * stdin. Otherwise, we grep through all the files specified. */ |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 348 | } else if (argc == 0) { |
| 349 | argc++; |
Mark Whitley | 2ef880b | 2000-07-18 21:02:06 +0000 | [diff] [blame] | 350 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 351 | matched = 0; |
| 352 | while (argc--) { |
| 353 | cur_file = *argv++; |
| 354 | if(!cur_file || (*cur_file == '-' && !cur_file[1])) { |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 355 | cur_file = "(standard input)"; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 356 | file = stdin; |
| 357 | } else { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 358 | file = fopen(cur_file, "r"); |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 359 | } |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 360 | if (file == NULL) { |
| 361 | if (!SUPPRESS_ERR_MSGS) |
| 362 | bb_perror_msg("%s", cur_file); |
| 363 | error_open_count++; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 364 | } else { |
| 365 | matched += grep_file(file); |
| 366 | if(matched < 0) { |
| 367 | /* we found a match but were told to be quiet, stop here and |
| 368 | * return success */ |
| 369 | break; |
Mark Whitley | 2ef880b | 2000-07-18 21:02:06 +0000 | [diff] [blame] | 370 | } |
"Vladimir N. Oleynik" | cf40d81 | 2005-09-23 13:23:15 +0000 | [diff] [blame] | 371 | fclose(file); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 372 | } |
"Vladimir N. Oleynik" | cf40d81 | 2005-09-23 13:23:15 +0000 | [diff] [blame] | 373 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 374 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 375 | /* destroy all the elments in the pattern list */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 376 | if (ENABLE_FEATURE_CLEAN_UP) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 377 | while (pattern_head) { |
| 378 | llist_t *pattern_head_ptr = pattern_head; |
| 379 | |
| 380 | pattern_head = pattern_head->link; |
| 381 | free(pattern_head_ptr); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 382 | } |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 383 | |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 384 | /* 0 = success, 1 = failed, 2 = error */ |
| 385 | /* If the -q option is specified, the exit status shall be zero |
| 386 | * if an input line is selected, even if an error was detected. */ |
| 387 | if(BE_QUIET && matched) |
"Vladimir N. Oleynik" | bf44974 | 2005-09-23 13:50:24 +0000 | [diff] [blame] | 388 | return 0; |
| 389 | if(error_open_count) |
| 390 | return 2; |
Mark Whitley | b5c2985 | 2001-02-01 21:02:41 +0000 | [diff] [blame] | 391 | return !matched; /* invert return value 0 = success, 1 = failed */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 392 | } |