freebsd-dev/contrib/netbsd-tests/usr.bin/grep
Kyle Evans 3e2d96ac97 grep: fix -A handling in conjunction with -m match limitation
The basic issue here is that grep, when given -m 1, would stop all
line processing once it hit the match count and exit immediately.  The
problem with exiting immediately is that -A processing only happens when
subsequent lines are processed and do not match.

The fix here is relatively easy; when bsdgrep matches a line, it resets
the 'tail' of the matching context to the value supplied to -A and
dumps anything that's been queued up for -B. After the current line has
been printed and tail is reset, we check our mcount and do what's
needed. Therefore, at the time that we decide we're doing nothing, we
know that 'tail' of the context is correct and we can simply continue
on if there's still more to pick up.

With this change, we still bail out immediately if there's been no -A
flag. If -A was supplied, we signal that we should continue on. However,
subsequent lines will not even bothere to try and process the line.  We
have reached the match count, so even if the next line would match then
we must process it if it hadn't. Thus, the loop in procfile() can
short-circuit and just process the line as a non-match until
procmatches() indicates that it's safe to stop.

A test has been added to reflect both that we should be picking up the
next line and that the next line should be considered a non-match even
if it should have been.

PR:		253350
MFC-after:	3 days
2021-02-08 12:41:22 -06:00
..
d_basic.out
d_begin_end_a.out
d_begin_end_b.out
d_binary.out
d_color_a.in bsdgrep: create additional tests for coverage on recent fixes 2017-04-05 18:41:44 +00:00
d_color_a.out bsdgrep: create additional tests for coverage on recent fixes 2017-04-05 18:41:44 +00:00
d_color_b.in bsdgrep: create additional tests for coverage on recent fixes 2017-04-05 18:41:44 +00:00
d_color_b.out bsdgrep: create additional tests for coverage on recent fixes 2017-04-05 18:41:44 +00:00
d_color_c.out bsdgrep: create additional tests for coverage on recent fixes 2017-04-05 18:41:44 +00:00
d_context2_a.out
d_context2_b.out
d_context2_c.out
d_context_a.in
d_context_a.out
d_context_b.in
d_context_b.out
d_context_c.out
d_context_d.out
d_context_e.in bsdgrep: fix -w flag matching with an empty pattern 2017-05-02 20:39:33 +00:00
d_context_e.out bsdgrep: fix -w flag matching with an empty pattern 2017-05-02 20:39:33 +00:00
d_context_f.out bsdgrep: fix -w flag matching with an empty pattern 2017-05-02 20:39:33 +00:00
d_context_g.out bsdgrep: fix -w flag matching with an empty pattern 2017-05-02 20:39:33 +00:00
d_egrep.out
d_escmap.in bsdgrep: create additional tests for coverage on recent fixes 2017-04-05 18:41:44 +00:00
d_f_file_empty.in bsdgrep: create additional tests for coverage on recent fixes 2017-04-05 18:41:44 +00:00
d_file_exp.in
d_file_exp.out
d_ignore_case.out
d_input
d_invert.in
d_invert.out
d_oflag_zerolen_a.in bsdgrep: create additional tests for coverage on recent fixes 2017-04-05 18:41:44 +00:00
d_oflag_zerolen_a.out bsdgrep: create additional tests for coverage on recent fixes 2017-04-05 18:41:44 +00:00
d_oflag_zerolen_b.in bsdgrep: create additional tests for coverage on recent fixes 2017-04-05 18:41:44 +00:00
d_oflag_zerolen_b.out bsdgrep: create additional tests for coverage on recent fixes 2017-04-05 18:41:44 +00:00
d_oflag_zerolen_c.in bsdgrep: create additional tests for coverage on recent fixes 2017-04-05 18:41:44 +00:00
d_oflag_zerolen_c.out bsdgrep: create additional tests for coverage on recent fixes 2017-04-05 18:41:44 +00:00
d_oflag_zerolen_d.in bsdgrep: create additional tests for coverage on recent fixes 2017-04-05 18:41:44 +00:00
d_oflag_zerolen_e.in bsdgrep: create additional tests for coverage on recent fixes 2017-04-05 18:41:44 +00:00
d_oflag_zerolen_e.out bsdgrep: create additional tests for coverage on recent fixes 2017-04-05 18:41:44 +00:00
d_recurse_symlink.err
d_recurse_symlink.out
d_recurse.out
d_whole_line.out
d_word_regexps.out
d_zgrep.out
t_grep.sh grep: fix -A handling in conjunction with -m match limitation 2021-02-08 12:41:22 -06:00