summaryrefslogtreecommitdiffstats
path: root/usr.bin/grep
diff options
context:
space:
mode:
authorgrehan <grehan@FreeBSD.org>2011-06-28 06:26:03 +0000
committergrehan <grehan@FreeBSD.org>2011-06-28 06:26:03 +0000
commit2c6741be0f59191f2283eb268e4f7690399d578a (patch)
treeb139c8c6dcca4fa284815daade405b75886ee360 /usr.bin/grep
parent3c35264f695e0a1f8a04dbcca1c93bb5159b2274 (diff)
parent19ae02bba572390c7299166228d31e54003e094a (diff)
downloadFreeBSD-src-2c6741be0f59191f2283eb268e4f7690399d578a.zip
FreeBSD-src-2c6741be0f59191f2283eb268e4f7690399d578a.tar.gz
IFC @ r222830
Diffstat (limited to 'usr.bin/grep')
-rw-r--r--usr.bin/grep/Makefile11
-rw-r--r--usr.bin/grep/fastgrep.c2
-rw-r--r--usr.bin/grep/grep.c6
-rw-r--r--usr.bin/grep/util.c27
4 files changed, 30 insertions, 16 deletions
diff --git a/usr.bin/grep/Makefile b/usr.bin/grep/Makefile
index 98fceeb..f09a7d6 100644
--- a/usr.bin/grep/Makefile
+++ b/usr.bin/grep/Makefile
@@ -2,9 +2,16 @@
# $FreeBSD$
# $OpenBSD: Makefile,v 1.6 2003/06/25 15:00:04 millert Exp $
+.include <bsd.own.mk>
+
+.if ${MK_BSD_GREP} == "yes"
PROG= grep
+.else
+PROG= bsdgrep
+.endif
SRCS= fastgrep.c file.c grep.c queue.c util.c
+.if ${MK_BSD_GREP} == "yes"
LINKS= ${BINDIR}/grep ${BINDIR}/egrep \
${BINDIR}/grep ${BINDIR}/fgrep \
${BINDIR}/grep ${BINDIR}/zgrep \
@@ -16,8 +23,10 @@ MLINKS= grep.1 egrep.1 \
grep.1 zgrep.1 \
grep.1 zegrep.1 \
grep.1 zfgrep.1
+.endif
-WARNS?= 6
+bsdgrep.1: grep.1
+ cp ${.ALLSRC} ${.TARGET}
LDADD= -lz -lbz2
DPADD= ${LIBZ} ${LIBBZ2}
diff --git a/usr.bin/grep/fastgrep.c b/usr.bin/grep/fastgrep.c
index d5c9d31..bc8a3af 100644
--- a/usr.bin/grep/fastgrep.c
+++ b/usr.bin/grep/fastgrep.c
@@ -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] == '$') {
diff --git a/usr.bin/grep/grep.c b/usr.bin/grep/grep.c
index 0047650..d03c13c 100644
--- a/usr.bin/grep/grep.c
+++ b/usr.bin/grep/grep.c
@@ -73,7 +73,7 @@ const char *errstr[] = {
};
/* Flags passed to regcomp() and regexec() */
-int cflags = 0;
+int cflags = REG_NOSUB;
int eflags = REG_STARTEND;
/* Shortcut for matching all cases like empty regex */
@@ -519,6 +519,7 @@ main(int argc, char *argv[])
break;
case 'o':
oflag = true;
+ cflags &= ~REG_NOSUB;
break;
case 'p':
linkbehave = LINK_SKIP;
@@ -552,9 +553,11 @@ main(int argc, char *argv[])
break;
case 'w':
wflag = true;
+ cflags &= ~REG_NOSUB;
break;
case 'x':
xflag = true;
+ cflags &= ~REG_NOSUB;
break;
case 'Z':
filebehave = FILE_GZIP;
@@ -588,6 +591,7 @@ main(int argc, char *argv[])
strcasecmp("none", optarg) != 0 &&
strcasecmp("no", optarg) != 0)
errx(2, getstr(3), "--color");
+ cflags &= ~REG_NOSUB;
break;
case LABEL_OPT:
label = optarg;
diff --git a/usr.bin/grep/util.c b/usr.bin/grep/util.c
index dece127..683a537 100644
--- a/usr.bin/grep/util.c
+++ b/usr.bin/grep/util.c
@@ -301,18 +301,17 @@ 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 = (cflags & REG_NOSUB)
+ ? (size_t)l->len
+ : (size_t)pmatch.rm_eo;
if (r == REG_NOMATCH)
continue;
/* Check for full match */
@@ -321,8 +320,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 +328,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 +343,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 +354,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)
OpenPOWER on IntegriCloud