aboutsummaryrefslogtreecommitdiff
path: root/findutils/grep.c
diff options
context:
space:
mode:
Diffstat (limited to 'findutils/grep.c')
-rw-r--r--findutils/grep.c47
1 files changed, 42 insertions, 5 deletions
diff --git a/findutils/grep.c b/findutils/grep.c
index 318fb4b2f..d5fe10320 100644
--- a/findutils/grep.c
+++ b/findutils/grep.c
@@ -19,15 +19,49 @@
19 * (C) 2006 Jac Goudsmit added -o option 19 * (C) 2006 Jac Goudsmit added -o option
20 */ 20 */
21 21
22//kbuild:lib-$(CONFIG_GREP) += grep.o
23//config:
24//config:config GREP
25//config: bool "grep"
26//config: default n
27//config: help
28//config: grep is used to search files for a specified pattern.
29//config:
30//config:config FEATURE_GREP_EGREP_ALIAS
31//config: bool "Enable extended regular expressions (egrep & grep -E)"
32//config: default y
33//config: depends on GREP
34//config: help
35//config: Enabled support for extended regular expressions. Extended
36//config: regular expressions allow for alternation (foo|bar), grouping,
37//config: and various repetition operators.
38//config:
39//config:config FEATURE_GREP_FGREP_ALIAS
40//config: bool "Alias fgrep to grep -F"
41//config: default y
42//config: depends on GREP
43//config: help
44//config: fgrep sees the search pattern as a normal string rather than
45//config: regular expressions.
46//config: grep -F always works, this just creates the fgrep alias.
47//config:
48//config:config FEATURE_GREP_CONTEXT
49//config: bool "Enable before and after context flags (-A, -B and -C)"
50//config: default y
51//config: depends on GREP
52//config: help
53//config: Print the specified number of leading (-B) and/or trailing (-A)
54//config: context surrounding our matching lines.
55//config: Print the specified number of context lines (-C).
56
22#include "libbb.h" 57#include "libbb.h"
23#include "xregex.h" 58#include "xregex.h"
24 59
25/* options */ 60/* options */
26#define OPTSTR_GREP \ 61#define OPTSTR_GREP \
27 "lnqvscFiHhe:f:Lorm:" \ 62 "lnqvscFiHhe:f:Lorm:w" \
28 IF_FEATURE_GREP_CONTEXT("A:B:C:") \ 63 IF_FEATURE_GREP_CONTEXT("A:B:C:") \
29 IF_FEATURE_GREP_EGREP_ALIAS("E") \ 64 IF_FEATURE_GREP_EGREP_ALIAS("E") \
30 IF_DESKTOP("w") \
31 IF_EXTRA_COMPAT("z") \ 65 IF_EXTRA_COMPAT("z") \
32 "aI" 66 "aI"
33 67
@@ -51,11 +85,11 @@ enum {
51 OPTBIT_o, /* show only matching parts of lines */ 85 OPTBIT_o, /* show only matching parts of lines */
52 OPTBIT_r, /* recurse dirs */ 86 OPTBIT_r, /* recurse dirs */
53 OPTBIT_m, /* -m MAX_MATCHES */ 87 OPTBIT_m, /* -m MAX_MATCHES */
88 OPTBIT_w, /* -w whole word match */
54 IF_FEATURE_GREP_CONTEXT( OPTBIT_A ,) /* -A NUM: after-match context */ 89 IF_FEATURE_GREP_CONTEXT( OPTBIT_A ,) /* -A NUM: after-match context */
55 IF_FEATURE_GREP_CONTEXT( OPTBIT_B ,) /* -B NUM: before-match context */ 90 IF_FEATURE_GREP_CONTEXT( OPTBIT_B ,) /* -B NUM: before-match context */
56 IF_FEATURE_GREP_CONTEXT( OPTBIT_C ,) /* -C NUM: -A and -B combined */ 91 IF_FEATURE_GREP_CONTEXT( OPTBIT_C ,) /* -C NUM: -A and -B combined */
57 IF_FEATURE_GREP_EGREP_ALIAS(OPTBIT_E ,) /* extended regexp */ 92 IF_FEATURE_GREP_EGREP_ALIAS(OPTBIT_E ,) /* extended regexp */
58 IF_DESKTOP( OPTBIT_w ,) /* whole word match */
59 IF_EXTRA_COMPAT( OPTBIT_z ,) /* input is NUL terminated */ 93 IF_EXTRA_COMPAT( OPTBIT_z ,) /* input is NUL terminated */
60 OPT_l = 1 << OPTBIT_l, 94 OPT_l = 1 << OPTBIT_l,
61 OPT_n = 1 << OPTBIT_n, 95 OPT_n = 1 << OPTBIT_n,
@@ -73,11 +107,11 @@ enum {
73 OPT_o = 1 << OPTBIT_o, 107 OPT_o = 1 << OPTBIT_o,
74 OPT_r = 1 << OPTBIT_r, 108 OPT_r = 1 << OPTBIT_r,
75 OPT_m = 1 << OPTBIT_m, 109 OPT_m = 1 << OPTBIT_m,
110 OPT_w = 1 << OPTBIT_w,
76 OPT_A = IF_FEATURE_GREP_CONTEXT( (1 << OPTBIT_A)) + 0, 111 OPT_A = IF_FEATURE_GREP_CONTEXT( (1 << OPTBIT_A)) + 0,
77 OPT_B = IF_FEATURE_GREP_CONTEXT( (1 << OPTBIT_B)) + 0, 112 OPT_B = IF_FEATURE_GREP_CONTEXT( (1 << OPTBIT_B)) + 0,
78 OPT_C = IF_FEATURE_GREP_CONTEXT( (1 << OPTBIT_C)) + 0, 113 OPT_C = IF_FEATURE_GREP_CONTEXT( (1 << OPTBIT_C)) + 0,
79 OPT_E = IF_FEATURE_GREP_EGREP_ALIAS((1 << OPTBIT_E)) + 0, 114 OPT_E = IF_FEATURE_GREP_EGREP_ALIAS((1 << OPTBIT_E)) + 0,
80 OPT_w = IF_DESKTOP( (1 << OPTBIT_w)) + 0,
81 OPT_z = IF_EXTRA_COMPAT( (1 << OPTBIT_z)) + 0, 115 OPT_z = IF_EXTRA_COMPAT( (1 << OPTBIT_z)) + 0,
82}; 116};
83 117
@@ -263,7 +297,10 @@ static int grep_file(FILE *file)
263 while (pattern_ptr) { 297 while (pattern_ptr) {
264 gl = (grep_list_data_t *)pattern_ptr->data; 298 gl = (grep_list_data_t *)pattern_ptr->data;
265 if (FGREP_FLAG) { 299 if (FGREP_FLAG) {
266 found |= (strstr(line, gl->pattern) != NULL); 300 found |= (((option_mask32 & OPT_i)
301 ? strcasestr(line, gl->pattern)
302 : strstr(line, gl->pattern)
303 ) != NULL);
267 } else { 304 } else {
268 if (!(gl->flg_mem_alocated_compiled & COMPILED)) { 305 if (!(gl->flg_mem_alocated_compiled & COMPILED)) {
269 gl->flg_mem_alocated_compiled |= COMPILED; 306 gl->flg_mem_alocated_compiled |= COMPILED;