summaryrefslogtreecommitdiffstats
path: root/usr.bin/grep
diff options
context:
space:
mode:
authorgabor <gabor@FreeBSD.org>2011-04-07 13:01:03 +0000
committergabor <gabor@FreeBSD.org>2011-04-07 13:01:03 +0000
commit43d1fb6126cdbf87fca36832fc399143e021ad7c (patch)
tree2578a6e697c6781cfb44ecbbb602f38a14ae0fcf /usr.bin/grep
parenta39cca6aa44fb41dad43abc8314ab193828507dc (diff)
downloadFreeBSD-src-43d1fb6126cdbf87fca36832fc399143e021ad7c.zip
FreeBSD-src-43d1fb6126cdbf87fca36832fc399143e021ad7c.tar.gz
- Simplify the fixed string pattern preprocessing code
- Improve readability Approved by: delphij (mentor) Obtained from: The NetBSD Project
Diffstat (limited to 'usr.bin/grep')
-rw-r--r--usr.bin/grep/fastgrep.c10
-rw-r--r--usr.bin/grep/grep.h1
-rw-r--r--usr.bin/grep/util.c12
3 files changed, 12 insertions, 11 deletions
diff --git a/usr.bin/grep/fastgrep.c b/usr.bin/grep/fastgrep.c
index 3fa993c..442ff4c 100644
--- a/usr.bin/grep/fastgrep.c
+++ b/usr.bin/grep/fastgrep.c
@@ -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) {
diff --git a/usr.bin/grep/grep.h b/usr.bin/grep/grep.h
index 5235e021..1562930 100644
--- a/usr.bin/grep/grep.h
+++ b/usr.bin/grep/grep.h
@@ -102,6 +102,7 @@ typedef struct {
bool bol;
bool eol;
bool reversed;
+ bool word;
} fastgrep_t;
/* Flags passed to regcomp() and regexec() */
diff --git a/usr.bin/grep/util.c b/usr.bin/grep/util.c
index c65d8f2..3c0a5a6 100644
--- a/usr.bin/grep/util.c
+++ b/usr.bin/grep/util.c
@@ -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' ';
OpenPOWER on IntegriCloud