[git commit] grep: support for -x, match whole line

Denys Vlasenko vda.linux at googlemail.com
Tue Feb 28 02:44:08 UTC 2012


commit: http://git.busybox.net/busybox/commit/?id=cd09e81520b7917adebcffd7c361671f913325eb
branch: http://git.busybox.net/busybox/commit/?id=refs/heads/master

Specified in POSIX.
http://pubs.opengroup.org/onlinepubs/009604499/utilities/grep.html

Signed-off-by: Natanael Copa <ncopa at alpinelinux.org>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 findutils/grep.c |   12 +++++++++---
 1 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/findutils/grep.c b/findutils/grep.c
index 5f42242..f14d6e6 100644
--- a/findutils/grep.c
+++ b/findutils/grep.c
@@ -85,6 +85,7 @@
 //usage:     "\n	-r	Recurse"
 //usage:     "\n	-i	Ignore case"
 //usage:     "\n	-w	Match whole words only"
+//usage:     "\n	-x	Match whole lines only"
 //usage:     "\n	-F	PATTERN is a literal (not regexp)"
 //usage:	IF_FEATURE_GREP_EGREP_ALIAS(
 //usage:     "\n	-E	PATTERN is an extended regexp"
@@ -113,7 +114,7 @@
 //usage:#define fgrep_full_usage ""
 
 #define OPTSTR_GREP \
-	"lnqvscFiHhe:f:Lorm:w" \
+	"lnqvscFiHhe:f:Lorm:wx" \
 	IF_FEATURE_GREP_CONTEXT("A:B:C:") \
 	IF_FEATURE_GREP_EGREP_ALIAS("E") \
 	IF_EXTRA_COMPAT("z") \
@@ -138,6 +139,7 @@ enum {
 	OPTBIT_r, /* recurse dirs */
 	OPTBIT_m, /* -m MAX_MATCHES */
 	OPTBIT_w, /* -w whole word match */
+	OPTBIT_x, /* -x whole line match */
 	IF_FEATURE_GREP_CONTEXT(    OPTBIT_A ,) /* -A NUM: after-match context */
 	IF_FEATURE_GREP_CONTEXT(    OPTBIT_B ,) /* -B NUM: before-match context */
 	IF_FEATURE_GREP_CONTEXT(    OPTBIT_C ,) /* -C NUM: -A and -B combined */
@@ -160,6 +162,7 @@ enum {
 	OPT_r = 1 << OPTBIT_r,
 	OPT_m = 1 << OPTBIT_m,
 	OPT_w = 1 << OPTBIT_w,
+	OPT_x = 1 << OPTBIT_x,
 	OPT_A = IF_FEATURE_GREP_CONTEXT(    (1 << OPTBIT_A)) + 0,
 	OPT_B = IF_FEATURE_GREP_CONTEXT(    (1 << OPTBIT_B)) + 0,
 	OPT_C = IF_FEATURE_GREP_CONTEXT(    (1 << OPTBIT_C)) + 0,
@@ -370,9 +373,12 @@ static int grep_file(FILE *file)
 							&gl->matched_range) >= 0
 #endif
 				) {
-					if (!(option_mask32 & OPT_w))
+					if (option_mask32 & OPT_x) {
+						found = (gl->matched_range.rm_so == 0
+						         && line[gl->matched_range.rm_eo] == '\0');
+					} else if (!(option_mask32 & OPT_w)) {
 						found = 1;
-					else {
+					} else {
 						char c = ' ';
 						if (gl->matched_range.rm_so)
 							c = line[gl->matched_range.rm_so - 1];


More information about the busybox-cvs mailing list