svn commit: r317051 - head/usr.bin/grep

Ed Maste emaste at FreeBSD.org
Mon Apr 17 13:36:31 UTC 2017


Author: emaste
Date: Mon Apr 17 13:36:30 2017
New Revision: 317051
URL: https://svnweb.freebsd.org/changeset/base/317051

Log:
  bsdgrep: remove output separators between overlapping segments
  
  Make bsdgrep more sensitive to context overlaps.  If it's printing
  context that either overlaps or is immediately adjacent to another bit
  of context, don't print a separator.
  
  - Non-overlapping segments no longer have two separators between them
  
  - Overlapping segments no longer have separators between them with
    overlapping sections repeated
  
  Submitted by:	Kyle Evans <kevans91 at ksu.edu>
  Reviewed by:	cem
  Differential Revision:	https://reviews.freebsd.org/D10105

Modified:
  head/usr.bin/grep/util.c

Modified: head/usr.bin/grep/util.c
==============================================================================
--- head/usr.bin/grep/util.c	Mon Apr 17 13:22:39 2017	(r317050)
+++ head/usr.bin/grep/util.c	Mon Apr 17 13:36:30 2017	(r317051)
@@ -55,6 +55,9 @@ __FBSDID("$FreeBSD$");
 static int	 linesqueued;
 static int	 procline(struct str *l, int);
 
+static int	 lasta;
+static bool	 ctxover;
+
 bool
 file_matching(const char *fname)
 {
@@ -212,8 +215,10 @@ procfile(const char *fn)
 	strcpy(ln.file, fn);
 	ln.line_no = 0;
 	ln.len = 0;
+	ctxover = false;
 	linesqueued = 0;
 	tail = 0;
+	lasta = 0;
 	ln.off = -1;
 
 	for (c = 0;  c == 0 || !(lflag || qflag); ) {
@@ -235,10 +240,24 @@ procfile(const char *fn)
 			free(f);
 			return (0);
 		}
-		/* Process the file line-by-line */
+
+		/* Process the file line-by-line, enqueue non-matching lines */
 		if ((t = procline(&ln, f->binary)) == 0 && Bflag > 0) {
-			enqueue(&ln);
-			linesqueued++;
+			/* Except don't enqueue lines that appear in -A ctx */
+			if (ln.line_no == 0 || lasta != ln.line_no) {
+				/* queue is maxed to Bflag number of lines */
+				enqueue(&ln);
+				linesqueued++;
+				ctxover = false;
+			} else {
+				/*
+				 * Indicate to procline() that we have ctx
+				 * overlap and make sure queue is empty.
+				 */
+				if (!ctxover)
+					clearqueue();
+				ctxover = true;
+			}
 		}
 		c += t;
 		if (mflag && mcount <= 0)
@@ -396,17 +415,19 @@ procline(struct str *l, int nottext)
 	/* Dealing with the context */
 	if ((tail || c) && !cflag && !qflag && !lflag && !Lflag) {
 		if (c) {
-			if (!first && !prev && !tail && Aflag)
+			if (!first && !prev && !tail && (Bflag || Aflag) &&
+			    !ctxover)
 				printf("--\n");
 			tail = Aflag;
 			if (Bflag > 0) {
-				if (!first && !prev)
-					printf("--\n");
 				printqueue();
+				ctxover = false;
 			}
 			linesqueued = 0;
 			printline(l, ':', matches, m);
 		} else {
+			/* Print -A lines following matches */
+			lasta = l->line_no;
 			printline(l, '-', matches, m);
 			tail--;
 		}


More information about the svn-src-head mailing list