Norihiro Tanaka wrote:
The argument that a separater should also be output for zero might be reasonable

It sounds reasonable to me, too. Unfortunately that patch did not work for me. Also, the change should be documented in NEWS, and the documentation updated, and a test case added. I installed the attached patches.
From 7bee5eee0a719cd6ccd1e7ac2e36bf1e10ac123e Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
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

From 4f19f8df714c2eb654f89742fec0e71a809e7765 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Sat, 10 May 2014 01:25:08 -0700
Subject: [PATCH 2/2] tests: add test case for -C 0 change

* tests/context-0: New test.
* tests/Makefile.am (TESTS): Add it.
---
 tests/Makefile.am |  1 +
 tests/context-0   | 27 +++++++++++++++++++++++++++
 2 files changed, 28 insertions(+)
 create mode 100755 tests/context-0

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 91775bd..f3450f3 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -50,6 +50,7 @@ TESTS =                                               \
   case-fold-titlecase                          \
   char-class-multibyte                         \
   char-class-multibyte2                                \
+  context-0                                    \
   dfa-coverage                                 \
   dfa-heap-overrun                             \
   dfaexec-multibyte                            \
diff --git a/tests/context-0 b/tests/context-0
new file mode 100755
index 0000000..4b63305
--- /dev/null
+++ b/tests/context-0
@@ -0,0 +1,27 @@
+#!/bin/sh
+# 0 lines of context
+. "${srcdir=.}/init.sh"; path_prepend_ ../src
+
+cat <<EOF > in || framework_failure_
+needle
+1st line of context
+2nd line of context
+3rd line of context
+another needle
+5th line of context relative to first match
+6th line...
+EOF
+
+cat <<EOF > exp || framework_failure_
+needle
+--
+another needle
+EOF
+
+fail=0
+grep -C 0 needle in > out 2>err || fail=1
+
+compare exp out || fail=1
+compare /dev/null err || fail=1
+
+Exit $fail
-- 
1.9.0

Reply via email to