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 | 7fee0c4 | 2006-09-13 16:39:19 +0000 | [diff] [blame] | 8 | * Licensed under the GPL v2 or later, 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 | /* |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 14 | * 2004,2006 (C) Vladimir Oleynik <dzo@simtreas.ru> - |
Eric Andersen | 7f164cd | 2004-05-26 09:46:41 +0000 | [diff] [blame] | 15 | * correction "-e pattern1 -e pattern2" logic and more optimizations. |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 16 | * precompiled regex |
Denis Vlasenko | 5193753 | 2006-09-29 20:58:53 +0000 | [diff] [blame] | 17 | */ |
| 18 | /* |
| 19 | * (C) 2006 Jac Goudsmit added -o option |
| 20 | */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 21 | |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 22 | #include "busybox.h" |
"Vladimir N. Oleynik" | 23f62fc | 2005-09-14 16:59:11 +0000 | [diff] [blame] | 23 | #include "xregex.h" |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 24 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 25 | /* options */ |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 26 | #define OPTSTR_GREP \ |
| 27 | "lnqvscFiHhe:f:Lor" \ |
| 28 | USE_FEATURE_GREP_CONTEXT("A:B:C:") \ |
| 29 | USE_FEATURE_GREP_EGREP_ALIAS("E") \ |
| 30 | USE_DESKTOP("w") \ |
Denis Vlasenko | f8ea0f3 | 2007-02-25 02:38:54 +0000 | [diff] [blame] | 31 | "aI" |
| 32 | /* ignored: -a "assume all files to be text" */ |
| 33 | /* ignored: -I "assume binary files have no matches" */ |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 34 | |
| 35 | enum { |
| 36 | OPTBIT_l, |
| 37 | OPTBIT_n, |
| 38 | OPTBIT_q, |
| 39 | OPTBIT_v, |
| 40 | OPTBIT_s, |
| 41 | OPTBIT_c, |
| 42 | OPTBIT_F, |
| 43 | OPTBIT_i, |
| 44 | OPTBIT_H, |
| 45 | OPTBIT_h, |
| 46 | OPTBIT_e, |
| 47 | OPTBIT_f, |
| 48 | OPTBIT_L, |
| 49 | OPTBIT_o, |
| 50 | OPTBIT_r, |
| 51 | USE_FEATURE_GREP_CONTEXT(OPTBIT_A ,) |
| 52 | USE_FEATURE_GREP_CONTEXT(OPTBIT_B ,) |
| 53 | USE_FEATURE_GREP_CONTEXT(OPTBIT_C ,) |
| 54 | USE_FEATURE_GREP_CONTEXT(OPTBIT_E ,) |
| 55 | USE_DESKTOP( OPTBIT_w ,) |
| 56 | OPT_l = 1 << OPTBIT_l, |
| 57 | OPT_n = 1 << OPTBIT_n, |
| 58 | OPT_q = 1 << OPTBIT_q, |
| 59 | OPT_v = 1 << OPTBIT_v, |
| 60 | OPT_s = 1 << OPTBIT_s, |
| 61 | OPT_c = 1 << OPTBIT_c, |
| 62 | OPT_F = 1 << OPTBIT_F, |
| 63 | OPT_i = 1 << OPTBIT_i, |
| 64 | OPT_H = 1 << OPTBIT_H, |
| 65 | OPT_h = 1 << OPTBIT_h, |
| 66 | OPT_e = 1 << OPTBIT_e, |
| 67 | OPT_f = 1 << OPTBIT_f, |
| 68 | OPT_L = 1 << OPTBIT_L, |
| 69 | OPT_o = 1 << OPTBIT_o, |
| 70 | OPT_r = 1 << OPTBIT_r, |
| 71 | OPT_A = USE_FEATURE_GREP_CONTEXT((1 << OPTBIT_A)) + 0, |
| 72 | OPT_B = USE_FEATURE_GREP_CONTEXT((1 << OPTBIT_B)) + 0, |
| 73 | OPT_C = USE_FEATURE_GREP_CONTEXT((1 << OPTBIT_C)) + 0, |
| 74 | OPT_E = USE_FEATURE_GREP_CONTEXT((1 << OPTBIT_E)) + 0, |
| 75 | OPT_w = USE_DESKTOP( (1 << OPTBIT_w)) + 0, |
| 76 | }; |
| 77 | |
| 78 | #define PRINT_FILES_WITH_MATCHES (option_mask32 & OPT_l) |
| 79 | #define PRINT_LINE_NUM (option_mask32 & OPT_n) |
| 80 | #define BE_QUIET (option_mask32 & OPT_q) |
| 81 | #define SUPPRESS_ERR_MSGS (option_mask32 & OPT_s) |
| 82 | #define PRINT_MATCH_COUNTS (option_mask32 & OPT_c) |
| 83 | #define FGREP_FLAG (option_mask32 & OPT_F) |
| 84 | #define PRINT_FILES_WITHOUT_MATCHES (option_mask32 & OPT_L) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 85 | |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 86 | typedef unsigned char byte_t; |
| 87 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 88 | static int reflags; |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 89 | static byte_t invert_search; |
| 90 | static byte_t print_filename; |
| 91 | static byte_t open_errors; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 92 | |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 93 | #if ENABLE_FEATURE_GREP_CONTEXT |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 94 | static int lines_before; |
| 95 | static int lines_after; |
| 96 | static char **before_buf; |
| 97 | static int last_line_printed; |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 98 | #endif /* ENABLE_FEATURE_GREP_CONTEXT */ |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 99 | |
| 100 | /* globals used internally */ |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 101 | static llist_t *pattern_head; /* growable list of patterns to match */ |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 102 | static const char *cur_file; /* the current file we are reading */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 103 | |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 104 | typedef struct grep_list_data_t { |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 105 | char *pattern; |
| 106 | regex_t preg; |
| 107 | #define PATTERN_MEM_A 1 |
| 108 | #define COMPILED 2 |
| 109 | int flg_mem_alocated_compiled; |
| 110 | } grep_list_data_t; |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 111 | |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 112 | static void print_line(const char *line, int linenum, char decoration) |
| 113 | { |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 114 | #if ENABLE_FEATURE_GREP_CONTEXT |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 115 | /* possibly print the little '--' separator */ |
Matt Kraai | edc8065 | 2001-05-22 14:29:27 +0000 | [diff] [blame] | 116 | if ((lines_before || lines_after) && last_line_printed && |
| 117 | last_line_printed < linenum - 1) { |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 118 | puts("--"); |
| 119 | } |
| 120 | last_line_printed = linenum; |
| 121 | #endif |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 122 | if (print_filename) |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 123 | printf("%s%c", cur_file, decoration); |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 124 | if (PRINT_LINE_NUM) |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 125 | printf("%i%c", linenum, decoration); |
Denis Vlasenko | 5193753 | 2006-09-29 20:58:53 +0000 | [diff] [blame] | 126 | /* Emulate weird GNU grep behavior with -ov */ |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 127 | if ((option_mask32 & (OPT_v+OPT_o)) != (OPT_v+OPT_o)) |
Denis Vlasenko | 5193753 | 2006-09-29 20:58:53 +0000 | [diff] [blame] | 128 | puts(line); |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 129 | } |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 130 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 131 | static int grep_file(FILE *file) |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 132 | { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 133 | char *line; |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 134 | byte_t ret; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 135 | int linenum = 0; |
Matt Kraai | deb95f6 | 2000-08-06 15:25:53 +0000 | [diff] [blame] | 136 | int nmatches = 0; |
Denis Vlasenko | 5193753 | 2006-09-29 20:58:53 +0000 | [diff] [blame] | 137 | regmatch_t regmatch; |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 138 | #if ENABLE_FEATURE_GREP_CONTEXT |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 139 | int print_n_lines_after = 0; |
| 140 | int curpos = 0; /* track where we are in the circular 'before' buffer */ |
| 141 | int idx = 0; /* used for iteration through the circular buffer */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 142 | #endif /* ENABLE_FEATURE_GREP_CONTEXT */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 143 | |
Denis Vlasenko | 2d5ca60 | 2006-10-12 22:43:20 +0000 | [diff] [blame] | 144 | while ((line = xmalloc_getline(file)) != NULL) { |
Glenn L McGrath | 26df70a | 2003-04-27 01:50:57 +0000 | [diff] [blame] | 145 | llist_t *pattern_ptr = pattern_head; |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 146 | grep_list_data_t * gl; |
Glenn L McGrath | 26df70a | 2003-04-27 01:50:57 +0000 | [diff] [blame] | 147 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 148 | linenum++; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 149 | ret = 0; |
Glenn L McGrath | 26df70a | 2003-04-27 01:50:57 +0000 | [diff] [blame] | 150 | while (pattern_ptr) { |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 151 | gl = (grep_list_data_t *)pattern_ptr->data; |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 152 | if (FGREP_FLAG) { |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 153 | ret = strstr(line, gl->pattern) != NULL; |
Glenn L McGrath | 26df70a | 2003-04-27 01:50:57 +0000 | [diff] [blame] | 154 | } else { |
| 155 | /* |
| 156 | * test for a postitive-assertion match (regexec returns success (0) |
| 157 | * and the user did not specify invert search), or a negative-assertion |
| 158 | * match (regexec returns failure (REG_NOMATCH) and the user specified |
| 159 | * invert search) |
| 160 | */ |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 161 | if (!(gl->flg_mem_alocated_compiled & COMPILED)) { |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 162 | gl->flg_mem_alocated_compiled |= COMPILED; |
| 163 | xregcomp(&(gl->preg), gl->pattern, reflags); |
| 164 | } |
Denis Vlasenko | 5193753 | 2006-09-29 20:58:53 +0000 | [diff] [blame] | 165 | regmatch.rm_so = 0; |
| 166 | regmatch.rm_eo = 0; |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 167 | if (regexec(&(gl->preg), line, 1, ®match, 0) == 0) { |
| 168 | if (!(option_mask32 & OPT_w)) |
| 169 | ret = 1; |
| 170 | else { |
| 171 | char c = ' '; |
| 172 | if (regmatch.rm_so) |
| 173 | c = line[regmatch.rm_so - 1]; |
| 174 | if (!isalnum(c) && c != '_') { |
| 175 | c = line[regmatch.rm_eo]; |
| 176 | if (!c || (!isalnum(c) && c != '_')) |
| 177 | ret = 1; |
| 178 | } |
| 179 | } |
| 180 | } |
Glenn L McGrath | 26df70a | 2003-04-27 01:50:57 +0000 | [diff] [blame] | 181 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 182 | pattern_ptr = pattern_ptr->link; |
| 183 | } /* while (pattern_ptr) */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 184 | |
Denis Vlasenko | 5193753 | 2006-09-29 20:58:53 +0000 | [diff] [blame] | 185 | if (ret ^ invert_search) { |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 186 | if (PRINT_FILES_WITH_MATCHES || BE_QUIET) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 187 | free(line); |
| 188 | |
| 189 | /* 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] | 190 | if (BE_QUIET || PRINT_FILES_WITHOUT_MATCHES) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 191 | return -1; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 192 | |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 193 | /* keep track of matches */ |
| 194 | nmatches++; |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 195 | |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 196 | /* if we're just printing filenames, we stop after the first match */ |
| 197 | if (PRINT_FILES_WITH_MATCHES) |
| 198 | break; |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 199 | |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 200 | /* print the matched line */ |
| 201 | if (PRINT_MATCH_COUNTS == 0) { |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 202 | #if ENABLE_FEATURE_GREP_CONTEXT |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 203 | int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1; |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 204 | |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 205 | /* if we were told to print 'before' lines and there is at least |
| 206 | * one line in the circular buffer, print them */ |
| 207 | if (lines_before && before_buf[prevpos] != NULL) { |
| 208 | int first_buf_entry_line_num = linenum - lines_before; |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 209 | |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 210 | /* advance to the first entry in the circular buffer, and |
| 211 | * figure out the line number is of the first line in the |
| 212 | * buffer */ |
| 213 | idx = curpos; |
| 214 | while (before_buf[idx] == NULL) { |
| 215 | idx = (idx + 1) % lines_before; |
| 216 | first_buf_entry_line_num++; |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 219 | /* now print each line in the buffer, clearing them as we go */ |
| 220 | while (before_buf[idx] != NULL) { |
| 221 | print_line(before_buf[idx], first_buf_entry_line_num, '-'); |
| 222 | free(before_buf[idx]); |
| 223 | before_buf[idx] = NULL; |
| 224 | idx = (idx + 1) % lines_before; |
| 225 | first_buf_entry_line_num++; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /* make a note that we need to print 'after' lines */ |
| 230 | print_n_lines_after = lines_after; |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 231 | #endif |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 232 | if (option_mask32 & OPT_o) { |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 233 | line[regmatch.rm_eo] = '\0'; |
| 234 | print_line(line + regmatch.rm_so, linenum, ':'); |
| 235 | } else { |
| 236 | print_line(line, linenum, ':'); |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 237 | } |
Matt Kraai | 0810f72 | 2001-01-04 15:11:52 +0000 | [diff] [blame] | 238 | } |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 239 | } |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 240 | #if ENABLE_FEATURE_GREP_CONTEXT |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 241 | else { /* no match */ |
| 242 | /* Add the line to the circular 'before' buffer */ |
| 243 | if (lines_before) { |
| 244 | free(before_buf[curpos]); |
| 245 | before_buf[curpos] = xstrdup(line); |
| 246 | curpos = (curpos + 1) % lines_before; |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 247 | } |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 248 | } |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 249 | |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 250 | /* if we need to print some context lines after the last match, do so */ |
| 251 | if (print_n_lines_after && (last_line_printed != linenum)) { |
| 252 | print_line(line, linenum, '-'); |
| 253 | print_n_lines_after--; |
| 254 | } |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 255 | #endif /* ENABLE_FEATURE_GREP_CONTEXT */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 256 | free(line); |
| 257 | } |
Mark Whitley | 8f12243 | 2000-07-18 18:37:01 +0000 | [diff] [blame] | 258 | |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 259 | /* 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] | 260 | * matches, just filenames and possibly match counts */ |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 261 | |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 262 | /* grep -c: print [filename:]count, even if count is zero */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 263 | if (PRINT_MATCH_COUNTS) { |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 264 | if (print_filename) |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 265 | printf("%s:", cur_file); |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 266 | printf("%d\n", nmatches); |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 267 | } |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 268 | |
| 269 | /* 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] | 270 | if (PRINT_FILES_WITH_MATCHES && nmatches > 0) { |
Matt Kraai | 59df6f7 | 2001-05-16 14:21:09 +0000 | [diff] [blame] | 271 | puts(cur_file); |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Eric Andersen | dec7f81 | 2004-05-26 11:47:55 +0000 | [diff] [blame] | 274 | /* 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] | 275 | if (PRINT_FILES_WITHOUT_MATCHES && nmatches == 0) { |
Eric Andersen | dec7f81 | 2004-05-26 11:47:55 +0000 | [diff] [blame] | 276 | puts(cur_file); |
| 277 | } |
| 278 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 279 | return nmatches; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 280 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 281 | |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 282 | #if ENABLE_FEATURE_CLEAN_UP |
| 283 | #define new_grep_list_data(p, m) add_grep_list_data(p, m) |
| 284 | static char * add_grep_list_data(char *pattern, int flg_used_mem) |
| 285 | #else |
| 286 | #define new_grep_list_data(p, m) add_grep_list_data(p) |
| 287 | static char * add_grep_list_data(char *pattern) |
| 288 | #endif |
| 289 | { |
| 290 | grep_list_data_t *gl = xmalloc(sizeof(grep_list_data_t)); |
| 291 | gl->pattern = pattern; |
| 292 | #if ENABLE_FEATURE_CLEAN_UP |
| 293 | gl->flg_mem_alocated_compiled = flg_used_mem; |
| 294 | #else |
| 295 | gl->flg_mem_alocated_compiled = 0; |
| 296 | #endif |
| 297 | return (char *)gl; |
| 298 | } |
| 299 | |
| 300 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 301 | static void load_regexes_from_file(llist_t *fopt) |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 302 | { |
| 303 | char *line; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 304 | FILE *f; |
| 305 | |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 306 | while (fopt) { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 307 | llist_t *cur = fopt; |
| 308 | char *ffile = cur->data; |
| 309 | |
| 310 | fopt = cur->link; |
| 311 | free(cur); |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 312 | f = xfopen(ffile, "r"); |
Denis Vlasenko | 2d5ca60 | 2006-10-12 22:43:20 +0000 | [diff] [blame] | 313 | while ((line = xmalloc_getline(f)) != NULL) { |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 314 | llist_add_to(&pattern_head, |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 315 | new_grep_list_data(line, PATTERN_MEM_A)); |
Eric Andersen | 31c27a9 | 2004-10-08 08:10:57 +0000 | [diff] [blame] | 316 | } |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 317 | } |
| 318 | } |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 319 | |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 320 | static int file_action_grep(const char *filename, struct stat *statbuf, void* matched, int depth) |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 321 | { |
| 322 | FILE *file = fopen(filename, "r"); |
| 323 | if (file == NULL) { |
| 324 | if (!SUPPRESS_ERR_MSGS) |
| 325 | bb_perror_msg("%s", cur_file); |
| 326 | open_errors = 1; |
| 327 | return 0; |
| 328 | } |
| 329 | cur_file = filename; |
| 330 | *(int*)matched += grep_file(file); |
Denis Vlasenko | bf39216 | 2006-10-15 18:38:01 +0000 | [diff] [blame] | 331 | fclose(file); |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 332 | return 1; |
| 333 | } |
| 334 | |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 335 | static int grep_dir(const char *dir) |
| 336 | { |
| 337 | int matched = 0; |
| 338 | recursive_action(dir, |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 339 | /* recurse=yes */ ACTION_RECURSE | |
| 340 | /* followLinks=no */ |
| 341 | /* depthFirst=yes */ ACTION_DEPTHFIRST, |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 342 | /* fileAction= */ file_action_grep, |
| 343 | /* dirAction= */ NULL, |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 344 | /* userData= */ &matched, |
| 345 | /* depth= */ 0); |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 346 | return matched; |
| 347 | } |
| 348 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 349 | int grep_main(int argc, char **argv); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 350 | int grep_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 351 | { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 352 | FILE *file; |
| 353 | int matched; |
Eric Andersen | 31c27a9 | 2004-10-08 08:10:57 +0000 | [diff] [blame] | 354 | llist_t *fopt = NULL; |
Matt Kraai | 999623e | 2001-10-29 15:49:03 +0000 | [diff] [blame] | 355 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 356 | /* do normal option parsing */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 357 | #if ENABLE_FEATURE_GREP_CONTEXT |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 358 | char *slines_after; |
| 359 | char *slines_before; |
| 360 | char *Copt; |
| 361 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 362 | opt_complementary = "H-h:e::f::C-AB"; |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 363 | getopt32(argc, argv, |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 364 | OPTSTR_GREP, |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 365 | &pattern_head, &fopt, |
| 366 | &slines_after, &slines_before, &Copt); |
| 367 | |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 368 | if (option_mask32 & OPT_C) { |
Denis Vlasenko | 2295b49 | 2006-10-22 11:42:51 +0000 | [diff] [blame] | 369 | /* -C unsets prev -A and -B, but following -A or -B |
| 370 | may override it */ |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 371 | if (!(option_mask32 & OPT_A)) /* not overridden */ |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 372 | slines_after = Copt; |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 373 | if (!(option_mask32 & OPT_B)) /* not overridden */ |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 374 | slines_before = Copt; |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 375 | option_mask32 |= OPT_A|OPT_B; /* for parser */ |
Eric Andersen | 053b146 | 2000-06-13 06:24:53 +0000 | [diff] [blame] | 376 | } |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 377 | if (option_mask32 & OPT_A) { |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 378 | lines_after = xatoi_u(slines_after); |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 379 | } |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 380 | if (option_mask32 & OPT_B) { |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 381 | lines_before = xatoi_u(slines_before); |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 382 | } |
Denis Vlasenko | 2295b49 | 2006-10-22 11:42:51 +0000 | [diff] [blame] | 383 | /* sanity checks */ |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 384 | if (option_mask32 & (OPT_c|OPT_q|OPT_l|OPT_L)) { |
| 385 | option_mask32 &= ~OPT_n; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 386 | lines_before = 0; |
| 387 | lines_after = 0; |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 388 | } else if (lines_before > 0) |
Denis Vlasenko | 4cccc03 | 2006-12-22 18:37:07 +0000 | [diff] [blame] | 389 | before_buf = xzalloc(lines_before * sizeof(char *)); |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 390 | #else |
| 391 | /* with auto sanity checks */ |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 392 | opt_complementary = "H-h:e::f::c-n:q-n:l-n"; |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 393 | getopt32(argc, argv, OPTSTR_GREP, |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 394 | &pattern_head, &fopt); |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 395 | #endif |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 396 | invert_search = ((option_mask32 & OPT_v) != 0); /* 0 | 1 */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 397 | |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 398 | if (pattern_head != NULL) { |
Bernhard Reutner-Fischer | febe3c4 | 2007-04-04 20:52:03 +0000 | [diff] [blame] | 399 | /* convert char **argv to grep_list_data_t */ |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 400 | llist_t *cur; |
| 401 | |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 402 | for (cur = pattern_head; cur; cur = cur->link) |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 403 | cur->data = new_grep_list_data(cur->data, 0); |
| 404 | } |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 405 | if (option_mask32 & OPT_f) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 406 | load_regexes_from_file(fopt); |
| 407 | |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 408 | if (ENABLE_FEATURE_GREP_FGREP_ALIAS && applet_name[0] == 'f') |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 409 | option_mask32 |= OPT_F; |
Mike Frysinger | 15ca586 | 2005-07-31 22:41:05 +0000 | [diff] [blame] | 410 | |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 411 | if (!(option_mask32 & (OPT_o | OPT_w))) |
Denis Vlasenko | 5193753 | 2006-09-29 20:58:53 +0000 | [diff] [blame] | 412 | reflags = REG_NOSUB; |
| 413 | |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 414 | if (ENABLE_FEATURE_GREP_EGREP_ALIAS && |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 415 | (applet_name[0] == 'e' || (option_mask32 & OPT_E))) |
Denis Vlasenko | 5193753 | 2006-09-29 20:58:53 +0000 | [diff] [blame] | 416 | reflags |= REG_EXTENDED; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 417 | |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 418 | if (option_mask32 & OPT_i) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 419 | reflags |= REG_ICASE; |
| 420 | |
| 421 | argv += optind; |
| 422 | argc -= optind; |
Eric Andersen | 053b146 | 2000-06-13 06:24:53 +0000 | [diff] [blame] | 423 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 424 | /* if we didn't get a pattern from a -e and no command file was specified, |
| 425 | * argv[optind] should be the pattern. no pattern, no worky */ |
Glenn L McGrath | 26df70a | 2003-04-27 01:50:57 +0000 | [diff] [blame] | 426 | if (pattern_head == NULL) { |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 427 | char *pattern; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 428 | if (*argv == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 429 | bb_show_usage(); |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 430 | pattern = new_grep_list_data(*argv++, 0); |
| 431 | llist_add_to(&pattern_head, pattern); |
| 432 | argc--; |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 433 | } |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 434 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 435 | /* argv[(optind)..(argc-1)] should be names of file to grep through. If |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 436 | * there is more than one file to grep, we will print the filenames. */ |
Denis Vlasenko | 2295b49 | 2006-10-22 11:42:51 +0000 | [diff] [blame] | 437 | if (argc > 1) |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 438 | print_filename = 1; |
Denis Vlasenko | 2295b49 | 2006-10-22 11:42:51 +0000 | [diff] [blame] | 439 | /* -H / -h of course override */ |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 440 | if (option_mask32 & OPT_H) |
Denis Vlasenko | 2295b49 | 2006-10-22 11:42:51 +0000 | [diff] [blame] | 441 | print_filename = 1; |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 442 | if (option_mask32 & OPT_h) |
Denis Vlasenko | 2295b49 | 2006-10-22 11:42:51 +0000 | [diff] [blame] | 443 | print_filename = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 444 | |
Mark Whitley | 2e1148b | 2000-06-28 22:59:30 +0000 | [diff] [blame] | 445 | /* If no files were specified, or '-' was specified, take input from |
| 446 | * stdin. Otherwise, we grep through all the files specified. */ |
Denis Vlasenko | 2295b49 | 2006-10-22 11:42:51 +0000 | [diff] [blame] | 447 | if (argc == 0) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 448 | argc++; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 449 | matched = 0; |
| 450 | while (argc--) { |
| 451 | cur_file = *argv++; |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 452 | file = stdin; |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 453 | if (!cur_file || (*cur_file == '-' && !cur_file[1])) { |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 454 | cur_file = "(standard input)"; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 455 | } else { |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 456 | if (option_mask32 & OPT_r) { |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 457 | struct stat st; |
| 458 | if (stat(cur_file, &st) == 0 && S_ISDIR(st.st_mode)) { |
Denis Vlasenko | 385304d | 2007-02-25 02:38:20 +0000 | [diff] [blame] | 459 | if (!(option_mask32 & OPT_h)) |
Denis Vlasenko | 3544ae6 | 2006-10-14 14:51:59 +0000 | [diff] [blame] | 460 | print_filename = 1; |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 461 | matched += grep_dir(cur_file); |
| 462 | goto grep_done; |
| 463 | } |
Mark Whitley | 2ef880b | 2000-07-18 21:02:06 +0000 | [diff] [blame] | 464 | } |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 465 | /* else: fopen(dir) will succeed, but reading won't */ |
| 466 | file = fopen(cur_file, "r"); |
| 467 | if (file == NULL) { |
| 468 | if (!SUPPRESS_ERR_MSGS) |
| 469 | bb_perror_msg("%s", cur_file); |
| 470 | open_errors = 1; |
| 471 | continue; |
| 472 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 473 | } |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 474 | matched += grep_file(file); |
Denis Vlasenko | ddec5af | 2006-10-26 23:25:17 +0000 | [diff] [blame] | 475 | fclose_if_not_stdin(file); |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 476 | grep_done: |
| 477 | if (matched < 0) { |
| 478 | /* we found a match but were told to be quiet, stop here and |
| 479 | * return success */ |
| 480 | break; |
| 481 | } |
"Vladimir N. Oleynik" | cf40d81 | 2005-09-23 13:23:15 +0000 | [diff] [blame] | 482 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 483 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 484 | /* destroy all the elments in the pattern list */ |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 485 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 486 | while (pattern_head) { |
| 487 | llist_t *pattern_head_ptr = pattern_head; |
| 488 | grep_list_data_t *gl = |
| 489 | (grep_list_data_t *)pattern_head_ptr->data; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 490 | |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 491 | pattern_head = pattern_head->link; |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 492 | if ((gl->flg_mem_alocated_compiled & PATTERN_MEM_A)) |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 493 | free(gl->pattern); |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 494 | if ((gl->flg_mem_alocated_compiled & COMPILED)) |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 495 | regfree(&(gl->preg)); |
Mike Frysinger | 3a62a73 | 2007-04-12 18:29:27 +0000 | [diff] [blame^] | 496 | free(gl); |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 497 | free(pattern_head_ptr); |
| 498 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 499 | } |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 500 | /* 0 = success, 1 = failed, 2 = error */ |
| 501 | /* If the -q option is specified, the exit status shall be zero |
| 502 | * if an input line is selected, even if an error was detected. */ |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 503 | if (BE_QUIET && matched) |
"Vladimir N. Oleynik" | bf44974 | 2005-09-23 13:50:24 +0000 | [diff] [blame] | 504 | return 0; |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 505 | if (open_errors) |
"Vladimir N. Oleynik" | bf44974 | 2005-09-23 13:50:24 +0000 | [diff] [blame] | 506 | return 2; |
Mark Whitley | b5c2985 | 2001-02-01 21:02:41 +0000 | [diff] [blame] | 507 | return !matched; /* invert return value 0 = success, 1 = failed */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 508 | } |