pcre2grep: correctly handle multiple passes

When the -m option is used, pcre2grep is meant to exit after enough
matches are found but while leaving the stream pinned to the next position
after the last match.

Unfortunately, it wasn't tracking correctly the beginning of the stream
on subsequent passes, and therefore it will fail to use the right seek
value.

Grab the position of the stream at the beginning and while at it, make
sure that the stream passed hasn't been consumed already.
This commit is contained in:
Carlo Marcelo Arenas Belón 2021-10-31 19:13:09 -07:00
parent 242d267641
commit 7cef0f46bc
3 changed files with 27 additions and 3 deletions

View File

@ -678,6 +678,10 @@ echo "---------------------------- Test 132 -----------------------------" >>tes
echo "RC=$?" >>testtrygrep
echo "---------------------------- Test 133 -----------------------------" >>testtrygrep
(cd $srcdir; exec 3<$srcdir/testdata/grepinput; $valgrind $vjs $pcre2grep -m1 -A3 '^match' <&3; echo '---'; $valgrind $vjs $pcre2grep -m1 -A3 '^match' <&3; exec 3<&-) >>testtrygrep 2>&1
echo "RC=$?" >>testtrygrep
echo "---------------------------- Test 134 -----------------------------" >>testtrygrep
(cd $srcdir; $valgrind $vjs $pcre2grep -m1 -O '=$x{41}$x423$o{103}$o1045=' 'fox') <$srcdir/testdata/grepinputv >>testtrygrep 2>&1
echo "RC=$?" >>testtrygrep

View File

@ -2538,6 +2538,7 @@ BOOL endhyphenpending = FALSE;
BOOL lines_printed = FALSE;
BOOL input_line_buffered = line_buffered;
FILE *in = NULL; /* Ensure initialized */
long stream_start = -1; /* Only non-negative if relevant */
/* Do the first read into the start of the buffer and set up the pointer to end
of what we have. In the case of libz, a non-zipped .gz file will be read as a
@ -2547,7 +2548,15 @@ fail. */
if (frtype != FR_LIBZ && frtype != FR_LIBBZ2)
{
in = (FILE *)handle;
if (is_file_tty(in)) input_line_buffered = TRUE;
if (feof(in))
return 1;
if (is_file_tty(in))
input_line_buffered = TRUE;
else
{
if (count_limit >= 0 && filename == stdin_name)
stream_start = ftell(in);
}
}
else input_line_buffered = FALSE;
@ -2594,8 +2603,8 @@ while (ptr < endptr)
if (count_limit >= 0 && count_matched_lines >= count_limit)
{
if (frtype == FR_PLAIN && filename == stdin_name && !is_file_tty(handle))
(void)fseek(handle, (long int)filepos, SEEK_SET);
if (stream_start >= 0)
(void)fseek(handle, stream_start + (long int)filepos, SEEK_SET);
rc = (count_limit == 0)? 1 : 0;
break;
}

11
testdata/grepoutput vendored
View File

@ -978,5 +978,16 @@ match 2:
a
RC=0
---------------------------- Test 133 -----------------------------
match 1:
a
match 2:
b
---
match 2:
b
match 3:
c
RC=0
---------------------------- Test 134 -----------------------------
=AB3CD5=
RC=0