Author: kevans
Date: Fri Jun  8 01:25:07 2018
New Revision: 334821
URL: https://svnweb.freebsd.org/changeset/base/334821

Log:
  bsdgrep(1): Slooowly peel away the chunky onion
  
  (or peel off the band-aid, whatever floats your boat)
  
  This addresses two separate issues:
  
  1.) Nothing within bsdgrep actually knew whether it cared about line numbers
    or not.
  
  2.) The file layer knew nothing about the context in which it was being
    called.
  
  #1 is only important when we're *not* processing line-by-line. #2 is
  debatably a good idea; the parsing context is only handy because that's
  where we store current offset information and, as of this commit, whether or
  not it needs to be line-aware.

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

Modified: head/usr.bin/grep/file.c
==============================================================================
--- head/usr.bin/grep/file.c    Fri Jun  8 00:47:24 2018        (r334820)
+++ head/usr.bin/grep/file.c    Fri Jun  8 01:25:07 2018        (r334821)
@@ -95,7 +95,7 @@ grep_lnbufgrow(size_t newlen)
 }
 
 char *
-grep_fgetln(struct file *f, size_t *lenp)
+grep_fgetln(struct file *f, struct parsec *pc)
 {
        unsigned char *p;
        char *ret;
@@ -109,18 +109,18 @@ grep_fgetln(struct file *f, size_t *lenp)
 
        if (bufrem == 0) {
                /* Return zero length to indicate EOF */
-               *lenp = 0;
+               pc->ln.len= 0;
                return (bufpos);
        }
 
-       /* Look for a newline in the remaining part of the buffer */
+       /* Look for a newline in the remaining part of the [6rbuffer */
        if ((p = memchr(bufpos, fileeol, bufrem)) != NULL) {
                ++p; /* advance over newline */
                ret = bufpos;
                len = p - bufpos;
                bufrem -= len;
                bufpos = p;
-               *lenp = len;
+               pc->ln.len = len;
                return (ret);
        }
 
@@ -155,11 +155,11 @@ grep_fgetln(struct file *f, size_t *lenp)
                bufpos = p;
                break;
        }
-       *lenp = len;
+       pc->ln.len = len;
        return (lnbuf);
 
 error:
-       *lenp = 0;
+       pc->ln.len = 0;
        return (NULL);
 }
 

Modified: head/usr.bin/grep/grep.h
==============================================================================
--- head/usr.bin/grep/grep.h    Fri Jun  8 00:47:24 2018        (r334820)
+++ head/usr.bin/grep/grep.h    Fri Jun  8 01:25:07 2018        (r334821)
@@ -97,6 +97,21 @@ struct epat {
        int              mode;
 };
 
+/*
+ * Parsing context; used to hold things like matches made and
+ * other useful bits
+ */
+struct parsec {
+       regmatch_t      matches[MAX_MATCHES];           /* Matches made */
+       /* XXX TODO: This should be a chunk, not a line */
+       struct str      ln;                             /* Current line */
+       size_t          lnstart;                        /* Position in line */
+       size_t          matchidx;                       /* Latest match index */
+       int             printed;                        /* Metadata printed? */
+       bool            binary;                         /* Binary file? */
+       bool            cntlines;                       /* Count lines? */
+};
+
 /* Flags passed to regcomp() and regexec() */
 extern int      cflags, eflags;
 
@@ -141,4 +156,4 @@ void         clearqueue(void);
 /* file.c */
 void            grep_close(struct file *f);
 struct file    *grep_open(const char *path);
-char           *grep_fgetln(struct file *f, size_t *len);
+char           *grep_fgetln(struct file *f, struct parsec *pc);

Modified: head/usr.bin/grep/util.c
==============================================================================
--- head/usr.bin/grep/util.c    Fri Jun  8 00:47:24 2018        (r334820)
+++ head/usr.bin/grep/util.c    Fri Jun  8 01:25:07 2018        (r334821)
@@ -57,20 +57,6 @@ __FBSDID("$FreeBSD$");
 static bool     first_match = true;
 
 /*
- * Parsing context; used to hold things like matches made and
- * other useful bits
- */
-struct parsec {
-       regmatch_t      matches[MAX_MATCHES];           /* Matches made */
-       /* XXX TODO: This should be a chunk, not a line */
-       struct str      ln;                             /* Current line */
-       size_t          lnstart;                        /* Position in line */
-       size_t          matchidx;                       /* Latest match index */
-       int             printed;                        /* Metadata printed? */
-       bool            binary;                         /* Binary file? */
-};
-
-/*
  * Match printing context
  */
 struct mprintc {
@@ -276,7 +262,7 @@ procmatches(struct mprintc *mc, struct parsec *pc, boo
                if (mflag) {
                        /* XXX TODO: Decrement by number of matched lines */
                        mcount -= 1;
-                       if (mflag && mcount <= 0)
+                       if (mcount <= 0)
                                return (false);
                }
        } else if (mc->doctx)
@@ -327,6 +313,7 @@ procfile(const char *fn)
        pc.ln.boff = 0;
        pc.ln.off = -1;
        pc.binary = f->binary;
+       pc.cntlines = false;
        memset(&mc, 0, sizeof(mc));
        mc.printmatch = true;
        if ((pc.binary && binbehave == BINFILE_BIN) || cflag || qflag ||
@@ -334,6 +321,8 @@ procfile(const char *fn)
                mc.printmatch = false;
        if (mc.printmatch && (Aflag != 0 || Bflag != 0))
                mc.doctx = true;
+       if (mc.printmatch && (Aflag != 0 || Bflag != 0 || mflag || nflag))
+               pc.cntlines = true;
        mcount = mlimit;
 
        for (lines = 0; lines == 0 || !(lflag || qflag); ) {
@@ -349,7 +338,7 @@ procfile(const char *fn)
                pc.ln.boff = 0;
                pc.ln.off += pc.ln.len + 1;
                /* XXX TODO: Grab a chunk */
-               if ((pc.ln.dat = grep_fgetln(f, &pc.ln.len)) == NULL ||
+               if ((pc.ln.dat = grep_fgetln(f, &pc)) == NULL ||
                    pc.ln.len == 0)
                        break;
 
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to