bug-grep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] add --group-separator=FOO and --no-group-separator


From: Paolo Bonzini
Subject: [PATCH] add --group-separator=FOO and --no-group-separator
Date: Fri, 19 Feb 2010 14:47:46 +0100

I took inspiration from bug #28906 to implement this.  Improvements
to the wording in the manual are welcome.  Otherwise okay?

* src/grep.c (group_separator): New.
(long_options): Add --group-separator=FOO and --no-group-separator.
(prtext): Print group_separator instead of SEP_STR_GROUP.  Optionally
suppress the separator altogether.
(main) Handle GROUP_SEPARATOR_OPTION.
* doc/grep.texi (Context control): Document it.
* tests/yesno.sh: Add testcases.
---
 doc/grep.texi  |   22 +++++++++++++++++-----
 src/grep.c     |   16 ++++++++++++----
 tests/yesno.sh |    9 ++++++++-
 3 files changed, 37 insertions(+), 10 deletions(-)

diff --git a/doc/grep.texi b/doc/grep.texi
index 3c2849e..44d68be 100644
--- a/doc/grep.texi
+++ b/doc/grep.texi
@@ -513,6 +513,17 @@ Print @var{num} lines of leading context before matching 
lines.
 @cindex context
 Print @var{num} lines of leading and trailing output context.
 
address@hidden address@hidden
address@hidden --group-separator
address@hidden group separator
+Print @var{string} instead of @samp{--} around disjoint groups
+of lines.
+
address@hidden --no-group-separator
address@hidden --group-separator
address@hidden group separator
+Print disjoint groups of lines adjacent to each other.
+
 @end table
 
 Matching lines normally use @samp{:} as a separator
@@ -522,11 +533,12 @@ When no context is specified,
 matching lines are simply output one right after another.
 When nonzero context is specified,
 lines that are adjacent in the input form a group
-and are output one right after another,
-but disjoint groups of lines are separated by a @samp{--}
-without any prefix and on a line of its own.
-Each group may contain several matching lines
-when they are close enough to each other
+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.  The default separator
+is @samp{--}, however whether to include it and its appearance
+can be changed with the options above.  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.
 
diff --git a/src/grep.c b/src/grep.c
index bef60b8..66242ba 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -81,6 +81,9 @@ static int only_matching;
 /* If nonzero, make sure first content char in a line is on a tab stop. */
 static int align_tabs;
 
+/* The group separator used when context is requested. */
+static const char *group_separator = SEP_STR_GROUP;
+
 /* The context and logic for choosing default --color screen attributes
    (foreground and background colors, etc.) are the following.
       -- There are eight basic colors available, each with its own
@@ -280,7 +283,8 @@ enum
   EXCLUDE_FROM_OPTION,
   LINE_BUFFERED_OPTION,
   LABEL_OPTION,
-  EXCLUDE_DIRECTORY_OPTION
+  EXCLUDE_DIRECTORY_OPTION,
+  GROUP_SEPARATOR_OPTION
 };
 
 /* Long options equivalences. */
@@ -309,6 +313,7 @@ static struct option const long_options[] =
   {"file", required_argument, NULL, 'f'},
   {"files-with-matches", no_argument, NULL, 'l'},
   {"files-without-match", no_argument, NULL, 'L'},
+  {"group-separator", required_argument, NULL, GROUP_SEPARATOR_OPTION},
   {"help", no_argument, &show_help, 1},
   {"include", required_argument, NULL, INCLUDE_OPTION},
   {"ignore-case", no_argument, NULL, 'i'},
@@ -320,6 +325,7 @@ static struct option const long_options[] =
   {"max-count", required_argument, NULL, 'm'},
   {"mmap", no_argument, &mmap_option, 1},
   {"no-filename", no_argument, NULL, 'h'},
+  {"no-group-separator", no_argument, NULL, GROUP_SEPARATOR_OPTION},
   {"no-messages", no_argument, NULL, 's'},
   {"null", no_argument, NULL, 'Z'},
   {"null-data", no_argument, NULL, 'z'},
@@ -977,10 +983,10 @@ prtext (char const *beg, char const *lim, int *nlinesp)
 
       /* We print the SEP_STR_GROUP separator only if our output is
         discontiguous from the last output in the file. */
-      if ((out_before || out_after) && used && p != lastout)
+      if ((out_before || out_after) && used && p != lastout && group_separator)
        {
          PR_SGR_START_IF(sep_color);
-         fputs (SEP_STR_GROUP, stdout);
+         fputs (group_separator, stdout);
          PR_SGR_END_IF(sep_color);
          fputc('\n', stdout);
        }
@@ -2156,7 +2162,9 @@ main (int argc, char **argv)
        add_exclude (included_patterns, optarg, EXCLUDE_INCLUDE);
        break;
 
-
+      case GROUP_SEPARATOR_OPTION:
+        group_separator = optarg;
+        break;
 
       case LINE_BUFFERED_OPTION:
        line_buffered = 1;
diff --git a/tests/yesno.sh b/tests/yesno.sh
index f774b74..88415d3 100755
--- a/tests/yesno.sh
+++ b/tests/yesno.sh
@@ -50,8 +50,9 @@ K='11:100:[K11 no ]/'; rK='11-100-[K11 no ]/'; XK='X[K11 no 
]/';
 L='12:110:[L12 no ]/'; rL='12-110-[L12 no ]/'; XL='X[L12 no ]/';
 M='13:120:[M13 yes]/'; rM='13-120-[M13 yes]/'; XM='X[M13 yes]/'; 
m='13:125:yes/'; rm='13-125-yes/'
 N='14:130:[N14 yes]/'; rN='14-130-[N14 yes]/'; XN='X[N14 yes]/'; 
n='14:135:yes/'; rn='14-135-yes/'
-# Group separator.
+# Group separators.
 s='--/'
+S='XYZ/'
 # Exit statuses.
 z0='?0/'
 z1='?1/'
@@ -64,7 +65,11 @@ set x \
   '-o'              "$c$d$e$h$i$m$n$z0" \
   '-C,1'            "$rB$C$D$E$rF$rG$H$I$rJ$s$rL$M$N$z0" \
   '-C,1,-o'         "$c$d$e$h$i$s$m$n$z0" \
+  '-C,1,-o,--group=XYZ' "$c$d$e$h$i$S$m$n$z0" \
+  '-C,1,-o,--no-gr' "$c$d$e$h$i$m$n$z0" \
   '-C,4,-1'         "$rB$C$D$E$rF$rG$H$I$rJ$s$rL$M$N$z0" \
+  '-C,1,--group=XYZ' "$rB$C$D$E$rF$rG$H$I$rJ$S$rL$M$N$z0" \
+  '-C,1,--no-gr'    "$rB$C$D$E$rF$rG$H$I$rJ$rL$M$N$z0" \
   '-m,4'            "$C$D$E$H$z0$XI$XJ$XK$XL$XM$XN" \
   '-m,4,-o'         "$c$d$e$h$z0$XI$XJ$XK$XL$XM$XN" \
   '-m,4,-C,1'       "$rB$C$D$E$rF$rG$H$z0$XI$XJ$XK$XL$XM$XN" \
@@ -80,6 +85,8 @@ set x \
   '-v,-o'           "$z0" \
   '-v,-C,1'         "$A$B$rC$s$rE$F$G$rH$rI$J$K$L$rM$z0" \
   '-v,-C,1,-o'      "$rc$s$re$rh$ri$rm$z0" \
+  '-v,-C,1,--group=XYZ' "$A$B$rC$S$rE$F$G$rH$rI$J$K$L$rM$z0" \
+  '-v,-C,1,--no-gr' "$A$B$rC$rE$F$G$rH$rI$J$K$L$rM$z0" \
   '-4,-1'           "$rB$C$D$E$rF$rG$H$I$rJ$s$rL$M$N$z0" \
   '-4,-v,-1'        "$A$B$rC$s$rE$F$G$rH$rI$J$K$L$rM$z0" \
   '-m,1,-v'         "$A$z0$XB$XC$XD$XE$XF$XG$XH$XI$XJ$XK$XL$XM$XN" \
-- 
1.6.6





reply via email to

[Prev in Thread] Current Thread [Next in Thread]