Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/grep Redo context printing so that adjourning contex...



details:   https://anonhg.NetBSD.org/src/rev/10a2cb556939
branches:  trunk
changeset: 764386:10a2cb556939
user:      joerg <joerg%NetBSD.org@localhost>
date:      Mon Apr 18 17:18:03 2011 +0000

description:
Redo context printing so that adjourning contexts don't print the
separator, following GNU grep's behavior in this regard.

diffstat:

 usr.bin/grep/grep.c |   6 ++----
 usr.bin/grep/grep.h |   4 ++--
 usr.bin/grep/util.c |  39 +++++++++++++++++----------------------
 3 files changed, 21 insertions(+), 28 deletions(-)

diffs (138 lines):

diff -r e684b16ba4b9 -r 10a2cb556939 usr.bin/grep/grep.c
--- a/usr.bin/grep/grep.c       Mon Apr 18 15:59:09 2011 +0000
+++ b/usr.bin/grep/grep.c       Mon Apr 18 17:18:03 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: grep.c,v 1.6 2011/04/18 03:48:23 joerg Exp $   */
+/*     $NetBSD: grep.c,v 1.7 2011/04/18 17:18:03 joerg Exp $   */
 /*     $FreeBSD: head/usr.bin/grep/grep.c 211519 2010-08-19 22:55:17Z delphij $        */
 /*     $OpenBSD: grep.c,v 1.42 2010/07/02 22:18:03 tedu Exp $  */
 
@@ -34,7 +34,7 @@
 #endif
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: grep.c,v 1.6 2011/04/18 03:48:23 joerg Exp $");
+__RCSID("$NetBSD: grep.c,v 1.7 2011/04/18 17:18:03 joerg Exp $");
 
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -147,8 +147,6 @@
 static inline const char       *init_color(const char *);
 
 /* Housekeeping */
-bool    first = true;  /* flag whether we are processing the first match */
-bool    prev;          /* flag whether or not the previous line matched */
 int     tail;          /* lines left to print */
 bool    notfound;      /* file not found */
 
diff -r e684b16ba4b9 -r 10a2cb556939 usr.bin/grep/grep.h
--- a/usr.bin/grep/grep.h       Mon Apr 18 15:59:09 2011 +0000
+++ b/usr.bin/grep/grep.h       Mon Apr 18 17:18:03 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: grep.h,v 1.5 2011/02/27 17:33:37 joerg Exp $   */
+/*     $NetBSD: grep.h,v 1.6 2011/04/18 17:18:04 joerg Exp $   */
 /*     $OpenBSD: grep.h,v 1.15 2010/04/05 03:03:55 tedu Exp $  */
 /*     $FreeBSD: head/usr.bin/grep/grep.h 211496 2010-08-19 09:28:59Z des $    */
 
@@ -119,7 +119,7 @@
 extern const char *color;
 extern int      binbehave, devbehave, dirbehave, filebehave, grepbehave, linkbehave;
 
-extern bool     first, matchall, notfound, prev;
+extern bool     matchall, notfound;
 extern int      tail;
 extern unsigned int dpatterns, fpatterns, patterns;
 extern char    **pattern;
diff -r e684b16ba4b9 -r 10a2cb556939 usr.bin/grep/util.c
--- a/usr.bin/grep/util.c       Mon Apr 18 15:59:09 2011 +0000
+++ b/usr.bin/grep/util.c       Mon Apr 18 17:18:03 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: util.c,v 1.10 2011/04/18 03:27:40 joerg Exp $  */
+/*     $NetBSD: util.c,v 1.11 2011/04/18 17:18:04 joerg Exp $  */
 /*     $FreeBSD: head/usr.bin/grep/util.c 211496 2010-08-19 09:28:59Z des $    */
 /*     $OpenBSD: util.c,v 1.39 2010/07/02 22:18:03 tedu Exp $  */
 
@@ -34,7 +34,7 @@
 #endif
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: util.c,v 1.10 2011/04/18 03:27:40 joerg Exp $");
+__RCSID("$NetBSD: util.c,v 1.11 2011/04/18 17:18:04 joerg Exp $");
 
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -55,7 +55,9 @@
 
 #include "grep.h"
 
-static int      linesqueued;
+static bool     first, first_global = true;
+static unsigned long long since_printed;
+
 static int      procline(struct str *l, int);
 
 bool
@@ -218,11 +220,10 @@
        strcpy(ln.file, fn);
        ln.line_no = 0;
        ln.len = 0;
-       linesqueued = 0;
        tail = 0;
        ln.off = -1;
 
-       for (c = 0;  c == 0 || !(lflag || qflag); ) {
+       for (first = true, c = 0;  c == 0 || !(lflag || qflag); ) {
                ln.off += ln.len + 1;
                if ((ln.dat = grep_fgetln(f, &ln.len)) == NULL || ln.len == 0) {
                        if (ln.line_no == 0 && matchall)
@@ -242,10 +243,7 @@
                        return (0);
                }
                /* Process the file line-by-line */
-               if ((t = procline(&ln, f->binary)) == 0 && Bflag > 0) {
-                       enqueue(&ln);
-                       linesqueued++;
-               }
+               t = procline(&ln, f->binary);
                c += t;
 
                /* Count the matches if we have a match limit */
@@ -374,28 +372,25 @@
        /* Dealing with the context */
        if ((tail || c) && !cflag && !qflag && !lflag && !Lflag) {
                if (c) {
-                       if (!first && !prev && !tail && Aflag)
+                       if ((Aflag || Bflag) && !first_global &&
+                           (first || since_printed > Bflag))
                                printf("--\n");
                        tail = Aflag;
-                       if (Bflag > 0) {
-                               if (!first && !prev)
-                                       printf("--\n");
+                       if (Bflag > 0)
                                printqueue();
-                       }
-                       linesqueued = 0;
                        printline(l, ':', matches, m);
                } else {
                        printline(l, '-', matches, m);
                        tail--;
                }
+               first = false;
+               first_global = false;
+               since_printed = 0;
+       } else {
+               if (Bflag)
+                       enqueue(l);
+               since_printed++;
        }
-
-       if (c) {
-               prev = true;
-               first = false;
-       } else
-               prev = false;
-
        return (c);
 }
 



Home | Main Index | Thread Index | Old Index