From 7bee5eee0a719cd6ccd1e7ac2e36bf1e10ac123e Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sat, 10 May 2014 01:21:15 -0700 Subject: [PATCH 1/2] grep: -A 0, -B 0, -C 0 now output a separator Problem reported by Dan Jacobson in: http://bugs.gnu.org/17380 * NEWS: * doc/grep.texi (Context Line Control): Document this. * src/grep.c (prtext): Output a separator even if context is zero. (main): Default context is now -1, not 0. --- NEWS | 4 ++++ doc/grep.texi | 18 ++++++++---------- src/grep.c | 5 +++-- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/NEWS b/NEWS index 3a20096..685ce9b 100644 --- a/NEWS +++ b/NEWS @@ -11,6 +11,10 @@ GNU grep NEWS -*- outline -*- grep no longer mishandles an empty pattern at the end of a pattern list. [bug introduced in grep-2.5] + grep -C NUM now outputs separators consistently even when NUM is zero, + and similarly for grep -A NUM and grep -B NUM. + [bug present since "the beginning"] + grep -f no longer mishandles patterns containing NUL bytes. [bug introduced in grep-2.11] diff --git a/doc/grep.texi b/doc/grep.texi index 59d0d3c..feee0d8 100644 --- a/doc/grep.texi +++ b/doc/grep.texi @@ -532,14 +532,13 @@ Print @var{num} lines of leading and trailing output context. @opindex --group-separator @cindex group separator When @option{-A}, @option{-B} or @option{-C} are in use, -print @var{string} instead of @option{--} around disjoint groups -of lines. +print @var{string} instead of @option{--} between groups of lines. @item --no-group-separator @opindex --group-separator @cindex group separator When @option{-A}, @option{-B} or @option{-C} are in use, -print disjoint groups of lines adjacent to each other. +do not print a separator between groups of lines. @end table @@ -555,26 +554,25 @@ between prefix fields and actual line content. Context (i.e., non-matching) lines use @samp{-} instead. @item -When no context is specified, +When context is not specified, matching lines are simply output one right after another. @item -When nonzero context is specified, +When context is specified, lines that are adjacent in the input form a group and are output one right after another, while -a separator appears by default between disjoint groups on a line -of its own and without any prefix. +by default a separator appears between non-adjacent groups. @item The default separator -is @samp{--}, however whether to include it and its appearance +is a @samp{--} line; its presence and appearance can be changed with the options above. @item Each group may contain several matching lines when they are close enough to each other -that two otherwise adjacent but divided groups connect -and can just merge into a single contiguous one. +that two adjacent groups connect and can merge into a single +contiguous one. @end itemize @node File and Directory Selection diff --git a/src/grep.c b/src/grep.c index 1e3fc28..ec955d8 100644 --- a/src/grep.c +++ b/src/grep.c @@ -1002,7 +1002,8 @@ prtext (char const *beg, char const *lim) /* Print the group separator unless the output is adjacent to the previous output in the file. */ - if ((out_before || out_after) && used && p != lastout && group_separator) + if ((0 <= out_before || 0 <= out_after) && used + && p != lastout && group_separator) { pr_sgr_start_if (sep_color); fputs (group_separator, stdout); @@ -1961,7 +1962,7 @@ main (int argc, char **argv) /* The value -1 means to use DEFAULT_CONTEXT. */ out_after = out_before = -1; /* Default before/after context: changed by -C/-NUM options */ - default_context = 0; + default_context = -1; /* Changed by -o option */ only_matching = 0; -- 1.9.0