The argument that a separater should also be output for zero might be
reasonable, because zero is allowed with -A, -B or -C option, though I
think it isn't necessary.
From 722e0a019a1e9c116874e94d249c04109b95f275 Mon Sep 17 00:00:00 2001
From: Norihiro Tanaka <[email protected]>
Date: Sat, 10 May 2014 14:41:57 +0900
Subject: [PATCH] grep: output a separator by explicit zero with -A, -B or -C
 option

* src/grep.c (out_before, out_after): Initialize to -1.
(prtext): output a separator if any of them isn't -1.
---
 src/grep.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/src/grep.c b/src/grep.c
index 1e3fc28..c402876 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -667,8 +667,8 @@ static bool out_invert;             /* Print nonmatching 
stuff. */
 static int out_file;           /* Print filenames. */
 static int out_line;           /* Print line numbers. */
 static int out_byte;           /* Print byte offsets. */
-static intmax_t out_before;    /* Lines of leading context. */
-static intmax_t out_after;     /* Lines of trailing context. */
+static intmax_t out_before = -1;/* Lines of leading context. */
+static intmax_t out_after = -1;        /* Lines of trailing context. */
 static int count_matches;      /* Count matching lines.  */
 static int list_files;         /* List matching files.  */
 static int no_filenames;       /* Suppress file names.  */
@@ -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 ((out_before >= 0 || out_after >= 0) && used && p != lastout
+          && group_separator)
         {
           pr_sgr_start_if (sep_color);
           fputs (group_separator, stdout);
-- 
1.9.3

Reply via email to