svn commit: r220421 - head/usr.bin/grep

Gabor Kovesdan gabor at FreeBSD.org
Thu Apr 7 13:01:04 UTC 2011


Author: gabor
Date: Thu Apr  7 13:01:03 2011
New Revision: 220421
URL: http://svn.freebsd.org/changeset/base/220421

Log:
  - Simplify the fixed string pattern preprocessing code
  - Improve readability
  
  Approved by:	delphij (mentor)
  Obtained from:	The NetBSD Project

Modified:
  head/usr.bin/grep/fastgrep.c
  head/usr.bin/grep/grep.h
  head/usr.bin/grep/util.c

Modified: head/usr.bin/grep/fastgrep.c
==============================================================================
--- head/usr.bin/grep/fastgrep.c	Thu Apr  7 12:52:46 2011	(r220420)
+++ head/usr.bin/grep/fastgrep.c	Thu Apr  7 13:01:03 2011	(r220421)
@@ -81,27 +81,25 @@ fastcomp(fastgrep_t *fg, const char *pat
 	int hasDot = 0;
 	int lastHalfDot = 0;
 	int shiftPatternLen;
-	bool bol = false;
-	bool eol = false;
 
 	/* Initialize. */
 	fg->len = strlen(pat);
 	fg->bol = false;
 	fg->eol = false;
 	fg->reversed = false;
+	fg->word = wflag;
 
 	/* Remove end-of-line character ('$'). */
 	if (fg->len > 0 && pat[fg->len - 1] == '$') {
-		eol = true;
 		fg->eol = true;
 		fg->len--;
 	}
 
 	/* Remove beginning-of-line character ('^'). */
 	if (pat[0] == '^') {
-		bol = true;
 		fg->bol = true;
 		fg->len--;
+		pat++;
 	}
 
 	if (fg->len >= 14 &&
@@ -110,7 +108,7 @@ fastcomp(fastgrep_t *fg, const char *pat
 		fg->len -= 14;
 		pat += 7;
 		/* Word boundary is handled separately in util.c */
-		wflag = true;
+		fg->word = true;
 	}
 
 	/*
@@ -149,7 +147,7 @@ fastcomp(fastgrep_t *fg, const char *pat
 	 * Determine if a reverse search would be faster based on the placement
 	 * of the dots.
 	 */
-	if ((!(lflag || cflag)) && ((!(bol || eol)) &&
+	if ((!(lflag || cflag)) && ((!(fg->bol || fg->eol)) &&
 	    ((lastHalfDot) && ((firstHalfDot < 0) ||
 	    ((fg->len - (lastHalfDot + 1)) < (size_t)firstHalfDot)))) &&
 	    !oflag && !color) {

Modified: head/usr.bin/grep/grep.h
==============================================================================
--- head/usr.bin/grep/grep.h	Thu Apr  7 12:52:46 2011	(r220420)
+++ head/usr.bin/grep/grep.h	Thu Apr  7 13:01:03 2011	(r220421)
@@ -102,6 +102,7 @@ typedef struct {
 	bool		 bol;
 	bool		 eol;
 	bool		 reversed;
+	bool		 word;
 } fastgrep_t;
 
 /* Flags passed to regcomp() and regexec() */

Modified: head/usr.bin/grep/util.c
==============================================================================
--- head/usr.bin/grep/util.c	Thu Apr  7 12:52:46 2011	(r220420)
+++ head/usr.bin/grep/util.c	Thu Apr  7 13:01:03 2011	(r220421)
@@ -55,14 +55,15 @@ static int	 procline(struct str *l, int)
 bool
 file_matching(const char *fname)
 {
+	char *fname_base;
 	bool ret;
 
 	ret = finclude ? false : true;
+	fname_base = basename(fname);
 
 	for (unsigned int i = 0; i < fpatterns; ++i) {
-		if (fnmatch(fpattern[i].pat,
-		    fname, 0) == 0 || fnmatch(fpattern[i].pat,
-		    basename(fname), 0) == 0) {
+		if (fnmatch(fpattern[i].pat, fname, 0) == 0 ||
+		    fnmatch(fpattern[i].pat, fname_base, 0) == 0) {
 			if (fpattern[i].mode == EXCL_PAT)
 				return (false);
 			else
@@ -277,7 +278,7 @@ procfile(const char *fn)
  * matches.  The matching lines are passed to printline() to display the
  * appropriate output.
  */
-static inline int
+static int
 procline(struct str *l, int nottext)
 {
 	regmatch_t matches[MAX_LINE_MATCHES];
@@ -318,7 +319,8 @@ procline(struct str *l, int nottext)
 					    (size_t)pmatch.rm_eo != l->len)
 						r = REG_NOMATCH;
 				/* Check for whole word match */
-				if (r == 0 && wflag && pmatch.rm_so != 0) {
+				if (r == 0 && fg_pattern[i].word &&
+				    pmatch.rm_so != 0) {
 					wint_t wbegin, wend;
 
 					wbegin = wend = L' ';


More information about the svn-src-head mailing list