From nobody@FreeBSD.org  Thu May  5 08:48:06 2011
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 03EA81065675
	for <freebsd-gnats-submit@FreeBSD.org>; Thu,  5 May 2011 08:48:06 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22])
	by mx1.freebsd.org (Postfix) with ESMTP id E53608FC0A
	for <freebsd-gnats-submit@FreeBSD.org>; Thu,  5 May 2011 08:48:05 +0000 (UTC)
Received: from red.freebsd.org (localhost [127.0.0.1])
	by red.freebsd.org (8.14.4/8.14.4) with ESMTP id p458m52H071223
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 5 May 2011 08:48:05 GMT
	(envelope-from nobody@red.freebsd.org)
Received: (from nobody@localhost)
	by red.freebsd.org (8.14.4/8.14.4/Submit) id p458m5ot071222;
	Thu, 5 May 2011 08:48:05 GMT
	(envelope-from nobody)
Message-Id: <201105050848.p458m5ot071222@red.freebsd.org>
Date: Thu, 5 May 2011 08:48:05 GMT
From: Yuri Pankov <yuri.pankov@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: [patch] grep(1): fix BSD grep --color option, -w matching at the start of the line and -F -w combination problem
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         156826
>Category:       bin
>Synopsis:       [patch] grep(1): fix BSD grep --color option, -w matching at the start of the line and -F -w combination problem
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    gabor
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu May 05 08:50:06 UTC 2011
>Closed-Date:    Sun Jun 12 12:44:52 UTC 2011
>Last-Modified:  Sun Jun 12 12:50:05 UTC 2011
>Originator:     Yuri Pankov
>Release:        
>Organization:
>Environment:
>Description:
Several problems with BSD grep:
- -w 'foo' matches 'foobar' at the start of the line
- -F makes -w noop
- --color only colors first match in the line
>How-To-Repeat:
procyon:yuri:/data/src/freebsd/head/usr.bin/grep$ echo foobar | ./grep -w foo; echo $?
foobar
0
procyon:yuri:/data/src/freebsd/head/usr.bin/grep$ echo foobar | ./grep -w bar; echo $?
1
procyon:yuri:/data/src/freebsd/head/usr.bin/grep$ echo foobar | ./grep -F -w foo; echo $?
foobar
0
procyon:yuri:/data/src/freebsd/head/usr.bin/grep$ echo foobar | ./grep -F -w bar; echo $?
foobar
0

Can't show problem with --color here :-)
>Fix:


Patch attached with submission follows:

Index: fastgrep.c
===================================================================
--- fastgrep.c	(revision 221483)
+++ fastgrep.c	(working copy)
@@ -89,7 +89,7 @@
 	fg->bol = false;
 	fg->eol = false;
 	fg->reversed = false;
-	fg->word = wflag;
+	fg->word = false;
 
 	/* Remove end-of-line character ('$'). */
 	if (fg->len > 0 && pat[fg->len - 1] == '$') {
Index: util.c
===================================================================
--- util.c	(revision 221483)
+++ util.c	(working copy)
@@ -301,18 +301,15 @@
  * XXX: grep_search() is a workaround for speed up and should be
  * removed in the future.  See fastgrep.c.
  */
-				if (fg_pattern[i].pattern) {
+				if (fg_pattern[i].pattern)
 					r = grep_search(&fg_pattern[i],
 					    (unsigned char *)l->dat,
 					    l->len, &pmatch);
-					r = (r == 0) ? 0 : REG_NOMATCH;
-					st = pmatch.rm_eo;
-				} else {
+				else
 					r = regexec(&r_pattern[i], l->dat, 1,
 					    &pmatch, eflags);
-					r = (r == 0) ? 0 : REG_NOMATCH;
-					st = pmatch.rm_eo;
-				}
+				r = (r == 0) ? 0 : REG_NOMATCH;
+				st = pmatch.rm_eo;
 				if (r == REG_NOMATCH)
 					continue;
 				/* Check for full match */
@@ -321,8 +318,7 @@
 					    (size_t)pmatch.rm_eo != l->len)
 						r = REG_NOMATCH;
 				/* Check for whole word match */
-				if (r == 0 && fg_pattern[i].word &&
-				    pmatch.rm_so != 0) {
+				if (r == 0 && (wflag || fg_pattern[i].word)) {
 					wint_t wbegin, wend;
 
 					wbegin = wend = L' ';
@@ -330,11 +326,13 @@
 					    sscanf(&l->dat[pmatch.rm_so - 1],
 					    "%lc", &wbegin) != 1)
 						r = REG_NOMATCH;
-					else if ((size_t)pmatch.rm_eo != l->len &&
+					else if ((size_t)pmatch.rm_eo !=
+					    l->len &&
 					    sscanf(&l->dat[pmatch.rm_eo],
 					    "%lc", &wend) != 1)
 						r = REG_NOMATCH;
-					else if (iswword(wbegin) || iswword(wend))
+					else if (iswword(wbegin) ||
+					    iswword(wend))
 						r = REG_NOMATCH;
 				}
 				if (r == 0) {
@@ -343,7 +341,8 @@
 					if (m < MAX_LINE_MATCHES)
 						matches[m++] = pmatch;
 					/* matches - skip further patterns */
-					if ((color != NULL && !oflag) || qflag || lflag)
+					if ((color == NULL && !oflag) ||
+					    qflag || lflag)
 						break;
 				}
 			}
@@ -353,7 +352,7 @@
 				break;
 			}
 			/* One pass if we are not recording matches */
-			if ((color != NULL && !oflag) || qflag || lflag)
+			if ((color == NULL && !oflag) || qflag || lflag)
 				break;
 
 			if (st == (size_t)pmatch.rm_so)


>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->analyzed 
State-Changed-By: arundel 
State-Changed-When: Thu May 5 10:22:49 UTC 2011 
State-Changed-Why:  
Yuri's patch seems to take care of the problems. 


Responsible-Changed-From-To: freebsd-bugs->gordon 
Responsible-Changed-By: arundel 
Responsible-Changed-When: Thu May 5 10:22:49 UTC 2011 
Responsible-Changed-Why:  
BSD grep is Gordon's baby. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=156826 
Responsible-Changed-From-To: gordon->gabor 
Responsible-Changed-By: gordon 
Responsible-Changed-When: Thu May 5 08:59:18 PDT 2011 
Responsible-Changed-Why:  
It's actually Gabor's baby, not mine. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=156826 
State-Changed-From-To: analyzed->closed 
State-Changed-By: gabor 
State-Changed-When: Sun Jun 12 12:44:25 UTC 2011 
State-Changed-Why:  
Committed to HEAD. Thanks for your contribution! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=156826 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/156826: commit references a PR
Date: Sun, 12 Jun 2011 12:44:16 +0000 (UTC)

 Author: gabor
 Date: Sun Jun 12 12:44:02 2011
 New Revision: 223008
 URL: http://svn.freebsd.org/changeset/base/223008
 
 Log:
   - Fix -w behavior
   - Make -F and -w work together
   - Fix --color to colorize all of the matches
   
   PR:		bin/156826
   Submitted by:	Yuri Pankov <yuri.pankov@gmail.com>
   Approved by:	delphij (mentor)
 
 Modified:
   head/usr.bin/grep/fastgrep.c
   head/usr.bin/grep/util.c
 
 Modified: head/usr.bin/grep/fastgrep.c
 ==============================================================================
 --- head/usr.bin/grep/fastgrep.c	Sun Jun 12 12:27:17 2011	(r223007)
 +++ head/usr.bin/grep/fastgrep.c	Sun Jun 12 12:44:02 2011	(r223008)
 @@ -89,7 +89,7 @@ fastcomp(fastgrep_t *fg, const char *pat
  	fg->bol = false;
  	fg->eol = false;
  	fg->reversed = false;
 -	fg->word = wflag;
 +	fg->word = false;
  
  	/* Remove end-of-line character ('$'). */
  	if (fg->len > 0 && pat[fg->len - 1] == '$') {
 
 Modified: head/usr.bin/grep/util.c
 ==============================================================================
 --- head/usr.bin/grep/util.c	Sun Jun 12 12:27:17 2011	(r223007)
 +++ head/usr.bin/grep/util.c	Sun Jun 12 12:44:02 2011	(r223008)
 @@ -301,18 +301,15 @@ procline(struct str *l, int nottext)
   * XXX: grep_search() is a workaround for speed up and should be
   * removed in the future.  See fastgrep.c.
   */
 -				if (fg_pattern[i].pattern) {
 +				if (fg_pattern[i].pattern)
  					r = grep_search(&fg_pattern[i],
  					    (unsigned char *)l->dat,
  					    l->len, &pmatch);
 -					r = (r == 0) ? 0 : REG_NOMATCH;
 -					st = pmatch.rm_eo;
 -				} else {
 +				else
  					r = regexec(&r_pattern[i], l->dat, 1,
  					    &pmatch, eflags);
 -					r = (r == 0) ? 0 : REG_NOMATCH;
 -					st = pmatch.rm_eo;
 -				}
 +				r = (r == 0) ? 0 : REG_NOMATCH;
 +				st = pmatch.rm_eo;
  				if (r == REG_NOMATCH)
  					continue;
  				/* Check for full match */
 @@ -321,8 +318,7 @@ procline(struct str *l, int nottext)
  					    (size_t)pmatch.rm_eo != l->len)
  						r = REG_NOMATCH;
  				/* Check for whole word match */
 -				if (r == 0 && fg_pattern[i].word &&
 -				    pmatch.rm_so != 0) {
 +				if (r == 0 && (wflag || fg_pattern[i].word)) {
  					wint_t wbegin, wend;
  
  					wbegin = wend = L' ';
 @@ -330,11 +326,13 @@ procline(struct str *l, int nottext)
  					    sscanf(&l->dat[pmatch.rm_so - 1],
  					    "%lc", &wbegin) != 1)
  						r = REG_NOMATCH;
 -					else if ((size_t)pmatch.rm_eo != l->len &&
 +					else if ((size_t)pmatch.rm_eo !=
 +					    l->len &&
  					    sscanf(&l->dat[pmatch.rm_eo],
  					    "%lc", &wend) != 1)
  						r = REG_NOMATCH;
 -					else if (iswword(wbegin) || iswword(wend))
 +					else if (iswword(wbegin) ||
 +					    iswword(wend))
  						r = REG_NOMATCH;
  				}
  				if (r == 0) {
 @@ -343,7 +341,8 @@ procline(struct str *l, int nottext)
  					if (m < MAX_LINE_MATCHES)
  						matches[m++] = pmatch;
  					/* matches - skip further patterns */
 -					if ((color != NULL && !oflag) || qflag || lflag)
 +					if ((color == NULL && !oflag) ||
 +					    qflag || lflag)
  						break;
  				}
  			}
 @@ -353,7 +352,7 @@ procline(struct str *l, int nottext)
  				break;
  			}
  			/* One pass if we are not recording matches */
 -			if ((color != NULL && !oflag) || qflag || lflag)
 +			if ((color == NULL && !oflag) || qflag || lflag)
  				break;
  
  			if (st == (size_t)pmatch.rm_so)
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
>Unformatted:
