aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNatanael Copa <ncopa@alpinelinux.org>2012-02-23 14:20:22 +0000
committerDenys Vlasenko <vda.linux@googlemail.com>2012-02-28 03:44:08 +0100
commitcd09e81520b7917adebcffd7c361671f913325eb (patch)
tree8b9bca5890d22a116511c5b098d08c6bbcd294d4
parent0b170e6a096b0d5010e29a39f3b270c3a7bc4945 (diff)
downloadbusybox-w32-cd09e81520b7917adebcffd7c361671f913325eb.tar.gz
busybox-w32-cd09e81520b7917adebcffd7c361671f913325eb.tar.bz2
busybox-w32-cd09e81520b7917adebcffd7c361671f913325eb.zip
grep: support for -x, match whole line
Specified in POSIX. http://pubs.opengroup.org/onlinepubs/009604499/utilities/grep.html Signed-off-by: Natanael Copa <ncopa@alpinelinux.org> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--findutils/grep.c12
1 files changed, 9 insertions, 3 deletions
diff --git a/findutils/grep.c b/findutils/grep.c
index 5f4224203..f14d6e6c1 100644
--- a/findutils/grep.c
+++ b/findutils/grep.c
@@ -85,6 +85,7 @@
85//usage: "\n -r Recurse" 85//usage: "\n -r Recurse"
86//usage: "\n -i Ignore case" 86//usage: "\n -i Ignore case"
87//usage: "\n -w Match whole words only" 87//usage: "\n -w Match whole words only"
88//usage: "\n -x Match whole lines only"
88//usage: "\n -F PATTERN is a literal (not regexp)" 89//usage: "\n -F PATTERN is a literal (not regexp)"
89//usage: IF_FEATURE_GREP_EGREP_ALIAS( 90//usage: IF_FEATURE_GREP_EGREP_ALIAS(
90//usage: "\n -E PATTERN is an extended regexp" 91//usage: "\n -E PATTERN is an extended regexp"
@@ -113,7 +114,7 @@
113//usage:#define fgrep_full_usage "" 114//usage:#define fgrep_full_usage ""
114 115
115#define OPTSTR_GREP \ 116#define OPTSTR_GREP \
116 "lnqvscFiHhe:f:Lorm:w" \ 117 "lnqvscFiHhe:f:Lorm:wx" \
117 IF_FEATURE_GREP_CONTEXT("A:B:C:") \ 118 IF_FEATURE_GREP_CONTEXT("A:B:C:") \
118 IF_FEATURE_GREP_EGREP_ALIAS("E") \ 119 IF_FEATURE_GREP_EGREP_ALIAS("E") \
119 IF_EXTRA_COMPAT("z") \ 120 IF_EXTRA_COMPAT("z") \
@@ -138,6 +139,7 @@ enum {
138 OPTBIT_r, /* recurse dirs */ 139 OPTBIT_r, /* recurse dirs */
139 OPTBIT_m, /* -m MAX_MATCHES */ 140 OPTBIT_m, /* -m MAX_MATCHES */
140 OPTBIT_w, /* -w whole word match */ 141 OPTBIT_w, /* -w whole word match */
142 OPTBIT_x, /* -x whole line match */
141 IF_FEATURE_GREP_CONTEXT( OPTBIT_A ,) /* -A NUM: after-match context */ 143 IF_FEATURE_GREP_CONTEXT( OPTBIT_A ,) /* -A NUM: after-match context */
142 IF_FEATURE_GREP_CONTEXT( OPTBIT_B ,) /* -B NUM: before-match context */ 144 IF_FEATURE_GREP_CONTEXT( OPTBIT_B ,) /* -B NUM: before-match context */
143 IF_FEATURE_GREP_CONTEXT( OPTBIT_C ,) /* -C NUM: -A and -B combined */ 145 IF_FEATURE_GREP_CONTEXT( OPTBIT_C ,) /* -C NUM: -A and -B combined */
@@ -160,6 +162,7 @@ enum {
160 OPT_r = 1 << OPTBIT_r, 162 OPT_r = 1 << OPTBIT_r,
161 OPT_m = 1 << OPTBIT_m, 163 OPT_m = 1 << OPTBIT_m,
162 OPT_w = 1 << OPTBIT_w, 164 OPT_w = 1 << OPTBIT_w,
165 OPT_x = 1 << OPTBIT_x,
163 OPT_A = IF_FEATURE_GREP_CONTEXT( (1 << OPTBIT_A)) + 0, 166 OPT_A = IF_FEATURE_GREP_CONTEXT( (1 << OPTBIT_A)) + 0,
164 OPT_B = IF_FEATURE_GREP_CONTEXT( (1 << OPTBIT_B)) + 0, 167 OPT_B = IF_FEATURE_GREP_CONTEXT( (1 << OPTBIT_B)) + 0,
165 OPT_C = IF_FEATURE_GREP_CONTEXT( (1 << OPTBIT_C)) + 0, 168 OPT_C = IF_FEATURE_GREP_CONTEXT( (1 << OPTBIT_C)) + 0,
@@ -370,9 +373,12 @@ static int grep_file(FILE *file)
370 &gl->matched_range) >= 0 373 &gl->matched_range) >= 0
371#endif 374#endif
372 ) { 375 ) {
373 if (!(option_mask32 & OPT_w)) 376 if (option_mask32 & OPT_x) {
377 found = (gl->matched_range.rm_so == 0
378 && line[gl->matched_range.rm_eo] == '\0');
379 } else if (!(option_mask32 & OPT_w)) {
374 found = 1; 380 found = 1;
375 else { 381 } else {
376 char c = ' '; 382 char c = ' ';
377 if (gl->matched_range.rm_so) 383 if (gl->matched_range.rm_so)
378 c = line[gl->matched_range.rm_so - 1]; 384 c = line[gl->matched_range.rm_so - 1];