Skip to content

pcre2grep: correctly handle multiple passes #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion RunGrepTest
Original file line number Diff line number Diff line change
Expand Up @@ -674,10 +674,14 @@ echo "---------------------------- Test 131 -----------------------------" >>tes
echo "RC=$?" >>testtrygrep

echo "---------------------------- Test 132 -----------------------------" >>testtrygrep
(cd $srcdir; $valgrind $vjs $pcre2grep -m1 -A3 '^match'; echo '---'; head -1) <$srcdir/testdata/grepinput >>testtrygrep 2>&1
(cd $srcdir; exec 3<$srcdir/testdata/grepinput; $valgrind $vjs $pcre2grep -m1 -A3 '^match' <&3; echo '---'; head -1 <&3; exec 3<&-) >>testtrygrep 2>&1
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

Expand Down
15 changes: 12 additions & 3 deletions src/pcre2grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
11 changes: 11 additions & 0 deletions testdata/grepoutput
Original file line number Diff line number Diff line change
Expand Up @@ -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