summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorph10 <ph10@2f5784b3-3f2a-0410-8824-cb99058d5e15>2013-05-10 11:40:06 +0000
committerph10 <ph10@2f5784b3-3f2a-0410-8824-cb99058d5e15>2013-05-10 11:40:06 +0000
commit038a52f90a30d93c5688a882620bfd392f386076 (patch)
treec66428c92f380efeccdcdde87f10b1854ca4dc42
parent5d21bb98d579cb16675f5c2be3ee77f23b74f43e (diff)
downloadpcre-038a52f90a30d93c5688a882620bfd392f386076.tar.gz
Fix pcregrep so that it can find empty lines.
git-svn-id: svn://vcs.exim.org/pcre/code/trunk@1324 2f5784b3-3f2a-0410-8824-cb99058d5e15
-rw-r--r--ChangeLog10
-rwxr-xr-xRunGrepTest16
-rw-r--r--pcregrep.c18
-rw-r--r--testdata/grepoutput35
4 files changed, 72 insertions, 7 deletions
diff --git a/ChangeLog b/ChangeLog
index f1034d1..f232932 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -147,6 +147,13 @@ Version 8.33 28-April-2013
39. Try madvise first before posix_madvise.
+40. Change 7 for PCRE 7.9 made it impossible for pcregrep to find empty lines
+ with a pattern such as ^$. It has taken 4 years for anybody to notice! The
+ original change locked out all matches of empty strings. This has been
+ changed so that one match of an empty string per line is recognized.
+ Subsequent searches on the same line (for colouring or for --only-matching,
+ for example) do not recognize empty strings.
+
Version 8.32 30-November-2012
-----------------------------
@@ -1655,7 +1662,8 @@ Version 7.9 11-Apr-09
7. A pattern that could match an empty string could cause pcregrep to loop; it
doesn't make sense to accept an empty string match in pcregrep, so I have
locked it out (using PCRE's PCRE_NOTEMPTY option). By experiment, this
- seems to be how GNU grep behaves.
+ seems to be how GNU grep behaves. [But see later change 40 for release
+ 8.33.]
8. The pattern (?(?=.*b)b|^) was incorrectly compiled as "match must be at
start or after a newline", because the conditional assertion was not being
diff --git a/RunGrepTest b/RunGrepTest
index 94fd808..daaf8af 100755
--- a/RunGrepTest
+++ b/RunGrepTest
@@ -486,6 +486,22 @@ echo "---------------------------- Test 101 ------------------------------" >>te
(cd $srcdir; $valgrind $pcregrep -o3 -Ho2 -o12 --only-matching=1 -o3 --colour=always --om-separator='|' '(\w+) binary (\w+)(\.)?' ./testdata/grepinput) >>testtry
echo "RC=$?" >>testtry
+echo "---------------------------- Test 102 -----------------------------" >>testtry
+(cd $srcdir; $valgrind $pcregrep -n "^$" ./testdata/grepinput3) >>testtry 2>&1
+echo "RC=$?" >>testtry
+
+echo "---------------------------- Test 103 -----------------------------" >>testtry
+(cd $srcdir; $valgrind $pcregrep --only-matching "^$" ./testdata/grepinput3) >>testtry 2>&1
+echo "RC=$?" >>testtry
+
+echo "---------------------------- Test 104 -----------------------------" >>testtry
+(cd $srcdir; $valgrind $pcregrep -n --only-matching "^$" ./testdata/grepinput3) >>testtry 2>&1
+echo "RC=$?" >>testtry
+
+echo "---------------------------- Test 105 -----------------------------" >>testtry
+(cd $srcdir; $valgrind $pcregrep --colour=always "ipsum|" ./testdata/grepinput3) >>testtry 2>&1
+echo "RC=$?" >>testtry
+
# Now compare the results.
diff --git a/pcregrep.c b/pcregrep.c
index 2e0dc03..1d20733 100644
--- a/pcregrep.c
+++ b/pcregrep.c
@@ -1378,6 +1378,7 @@ to find all possible matches.
Arguments:
matchptr the start of the subject
length the length of the subject to match
+ options options for pcre_exec
startoffset where to start matching
offsets the offets vector to fill in
mrc address of where to put the result of pcre_exec()
@@ -1388,8 +1389,8 @@ Returns: TRUE if there was a match
*/
static BOOL
-match_patterns(char *matchptr, size_t length, int startoffset, int *offsets,
- int *mrc)
+match_patterns(char *matchptr, size_t length, unsigned int options,
+ int startoffset, int *offsets, int *mrc)
{
int i;
size_t slen = length;
@@ -1404,7 +1405,7 @@ if (slen > 200)
for (i = 1; p != NULL; p = p->next, i++)
{
*mrc = pcre_exec(p->compiled, p->hint, matchptr, (int)length,
- startoffset, PCRE_NOTEMPTY, offsets, OFFSET_SIZE);
+ startoffset, options, offsets, OFFSET_SIZE);
if (*mrc >= 0) return TRUE;
if (*mrc == PCRE_ERROR_NOMATCH) continue;
fprintf(stderr, "pcregrep: pcre_exec() gave error %d while matching ", *mrc);
@@ -1539,6 +1540,7 @@ while (ptr < endptr)
int endlinelength;
int mrc = 0;
int startoffset = 0;
+ unsigned int options = 0;
BOOL match;
char *matchptr = ptr;
char *t = ptr;
@@ -1628,9 +1630,12 @@ while (ptr < endptr)
/* Run through all the patterns until one matches or there is an error other
than NOMATCH. This code is in a subroutine so that it can be re-used for
- finding subsequent matches when colouring matched lines. */
+ finding subsequent matches when colouring matched lines. After finding one
+ match, set PCRE_NOTEMPTY to disable any further matches of null strings in
+ this line. */
- match = match_patterns(matchptr, length, startoffset, offsets, &mrc);
+ match = match_patterns(matchptr, length, options, startoffset, offsets, &mrc);
+ options = PCRE_NOTEMPTY;
/* If it's a match or a not-match (as required), do what's wanted. */
@@ -1871,7 +1876,8 @@ while (ptr < endptr)
{
startoffset = offsets[1];
if (startoffset >= (int)linelength + endlinelength ||
- !match_patterns(matchptr, length, startoffset, offsets, &mrc))
+ !match_patterns(matchptr, length, options, startoffset, offsets,
+ &mrc))
break;
FWRITE(matchptr + startoffset, 1, offsets[0] - startoffset, stdout);
fprintf(stdout, "%c[%sm", 0x1b, colour_string);
diff --git a/testdata/grepoutput b/testdata/grepoutput
index 733b9d6..cf04091 100644
--- a/testdata/grepoutput
+++ b/testdata/grepoutput
@@ -705,3 +705,38 @@ RC=0
./testdata/grepinput:zero|a
./testdata/grepinput:.|zero|the|.
RC=0
+---------------------------- Test 102 -----------------------------
+2:
+5:
+7:
+9:
+12:
+14:
+RC=0
+---------------------------- Test 103 -----------------------------
+RC=0
+---------------------------- Test 104 -----------------------------
+2:
+5:
+7:
+9:
+12:
+14:
+RC=0
+---------------------------- Test 105 -----------------------------
+triple: t1_txt s1_tag s_txt p_tag p_txt o_tag o_txt
+
+triple: t2_txt s1_tag s_txt p_tag p_txt o_tag
+Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+
+triple: t3_txt s2_tag s_txt p_tag p_txt o_tag o_txt
+
+triple: t4_txt s1_tag s_txt p_tag p_txt o_tag o_txt
+
+triple: t5_txt s1_tag s_txt p_tag p_txt o_tag
+o_txt
+
+triple: t6_txt s2_tag s_txt p_tag p_txt o_tag o_txt
+
+triple: t7_txt s1_tag s_txt p_tag p_txt o_tag o_txt
+RC=0