Module Name:    src
Committed By:   joerg
Date:           Mon Apr 18 17:18:04 UTC 2011

Modified Files:
        src/usr.bin/grep: grep.c grep.h util.c

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


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/usr.bin/grep/grep.c
cvs rdiff -u -r1.5 -r1.6 src/usr.bin/grep/grep.h
cvs rdiff -u -r1.10 -r1.11 src/usr.bin/grep/util.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/grep/grep.c
diff -u src/usr.bin/grep/grep.c:1.6 src/usr.bin/grep/grep.c:1.7
--- src/usr.bin/grep/grep.c:1.6	Mon Apr 18 03:48:23 2011
+++ src/usr.bin/grep/grep.c	Mon Apr 18 17:18:03 2011
@@ -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 */
 

Index: src/usr.bin/grep/grep.h
diff -u src/usr.bin/grep/grep.h:1.5 src/usr.bin/grep/grep.h:1.6
--- src/usr.bin/grep/grep.h:1.5	Sun Feb 27 17:33:37 2011
+++ src/usr.bin/grep/grep.h	Mon Apr 18 17:18:04 2011
@@ -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;

Index: src/usr.bin/grep/util.c
diff -u src/usr.bin/grep/util.c:1.10 src/usr.bin/grep/util.c:1.11
--- src/usr.bin/grep/util.c:1.10	Mon Apr 18 03:27:40 2011
+++ src/usr.bin/grep/util.c	Mon Apr 18 17:18:04 2011
@@ -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--;
 		}
-	}
-
-	if (c) {
-		prev = true;
 		first = false;
-	} else
-		prev = false;
-
+		first_global = false;
+		since_printed = 0;
+	} else {
+		if (Bflag)
+			enqueue(l);
+		since_printed++;
+	}
 	return (c);
 }
 

Reply via email to