From owner-svn-src-all@freebsd.org Mon Apr 17 13:14:20 2017 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id A1D1CD41674; Mon, 17 Apr 2017 13:14:20 +0000 (UTC) (envelope-from emaste@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 60172EE; Mon, 17 Apr 2017 13:14:20 +0000 (UTC) (envelope-from emaste@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v3HDEJ7R011985; Mon, 17 Apr 2017 13:14:19 GMT (envelope-from emaste@FreeBSD.org) Received: (from emaste@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v3HDEI7K011979; Mon, 17 Apr 2017 13:14:18 GMT (envelope-from emaste@FreeBSD.org) Message-Id: <201704171314.v3HDEI7K011979@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: emaste set sender to emaste@FreeBSD.org using -f From: Ed Maste Date: Mon, 17 Apr 2017 13:14:18 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r317049 - head/usr.bin/grep X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 17 Apr 2017 13:14:20 -0000 Author: emaste Date: Mon Apr 17 13:14:18 2017 New Revision: 317049 URL: https://svnweb.freebsd.org/changeset/base/317049 Log: bsdgrep: add -z/--null-data support -z treats input and output data as sequences of lines terminated by a zero byte instead of a newline. This brings it more in line with GNU grep and brings us closer to passing the current tests with BSD grep. Submitted by: Kyle Evans Reviewed by: cem Relnotes: Yes Differential Revision: https://reviews.freebsd.org/D10101 Modified: head/usr.bin/grep/file.c head/usr.bin/grep/grep.1 head/usr.bin/grep/grep.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 Mon Apr 17 13:01:42 2017 (r317048) +++ head/usr.bin/grep/file.c Mon Apr 17 13:14:18 2017 (r317049) @@ -197,7 +197,7 @@ grep_fgetln(struct file *f, size_t *lenp } /* Look for a newline in the remaining part of the buffer */ - if ((p = memchr(bufpos, '\n', bufrem)) != NULL) { + if ((p = memchr(bufpos, fileeol, bufrem)) != NULL) { ++p; /* advance over newline */ ret = bufpos; len = p - bufpos; @@ -219,7 +219,7 @@ grep_fgetln(struct file *f, size_t *lenp if (bufrem == 0) /* EOF: return partial line */ break; - if ((p = memchr(bufpos, '\n', bufrem)) == NULL && + if ((p = memchr(bufpos, fileeol, bufrem)) == NULL && filebehave != FILE_MMAP) continue; if (p == NULL) { @@ -322,7 +322,8 @@ grep_open(const char *path) goto error2; /* Check for binary stuff, if necessary */ - if (binbehave != BINFILE_TEXT && memchr(bufpos, '\0', bufrem) != NULL) + if (binbehave != BINFILE_TEXT && fileeol != '\0' && + memchr(bufpos, '\0', bufrem) != NULL) f->binary = true; return (f); Modified: head/usr.bin/grep/grep.1 ============================================================================== --- head/usr.bin/grep/grep.1 Mon Apr 17 13:01:42 2017 (r317048) +++ head/usr.bin/grep/grep.1 Mon Apr 17 13:14:18 2017 (r317049) @@ -30,7 +30,7 @@ .\" .\" @(#)grep.1 8.3 (Berkeley) 4/18/94 .\" -.Dd July 28, 2010 +.Dd April 17, 2017 .Dt GREP 1 .Os .Sh NAME @@ -40,7 +40,7 @@ .Sh SYNOPSIS .Nm grep .Bk -words -.Op Fl abcdDEFGHhIiJLlmnOopqRSsUVvwxZ +.Op Fl abcdDEFGHhIiJLlmnOopqRSsUVvwxZz .Op Fl A Ar num .Op Fl B Ar num .Op Fl C Ns Op Ar num @@ -378,7 +378,10 @@ expression are considered to be matching Equivalent to .Fl i . Obsoleted. -.It Fl Z , Fl z , Fl Fl decompress +.It Fl z , Fl Fl null-data +Treat input and output data as sequences of lines terminated by a +zero-byte instead of a newline. +.It Fl Z , Fl Fl decompress Force .Nm grep to behave as Modified: head/usr.bin/grep/grep.c ============================================================================== --- head/usr.bin/grep/grep.c Mon Apr 17 13:01:42 2017 (r317048) +++ head/usr.bin/grep/grep.c Mon Apr 17 13:14:18 2017 (r317049) @@ -66,7 +66,7 @@ const char *errstr[] = { /* 1*/ "(standard input)", /* 2*/ "cannot read bzip2 compressed file", /* 3*/ "unknown %s option", -/* 4*/ "usage: %s [-abcDEFGHhIiJLlmnOoPqRSsUVvwxZ] [-A num] [-B num] [-C[num]]\n", +/* 4*/ "usage: %s [-abcDEFGHhIiJLlmnOoPqRSsUVvwxZz] [-A num] [-B num] [-C[num]]\n", /* 5*/ "\t[-e pattern] [-f file] [--binary-files=value] [--color=when]\n", /* 6*/ "\t[--context[=num]] [--directories=action] [--label] [--line-buffered]\n", /* 7*/ "\t[--null] [pattern] [file ...]\n", @@ -109,6 +109,7 @@ bool lflag; /* -l: only show names of bool mflag; /* -m x: stop reading the files after x matches */ long long mcount; /* count for -m */ long long mlimit; /* requested value for -m */ +char fileeol; /* indicator for eol */ bool nflag; /* -n: show line numbers in front of matching lines */ bool oflag; /* -o: print only matching part */ bool qflag; /* -q: quiet mode (don't output anything) */ @@ -165,7 +166,7 @@ usage(void) exit(2); } -static const char *optstr = "0123456789A:B:C:D:EFGHIJMLOPSRUVZabcd:e:f:hilm:nopqrsuvwxXy"; +static const char *optstr = "0123456789A:B:C:D:EFGHIJMLOPSRUVZabcd:e:f:hilm:nopqrsuvwxXyz"; static const struct option long_options[] = { @@ -215,6 +216,7 @@ static const struct option long_options[ {"word-regexp", no_argument, NULL, 'w'}, {"line-regexp", no_argument, NULL, 'x'}, {"xz", no_argument, NULL, 'X'}, + {"null-data", no_argument, NULL, 'z'}, {"decompress", no_argument, NULL, 'Z'}, {NULL, no_argument, NULL, 0} }; @@ -384,6 +386,7 @@ main(int argc, char *argv[]) newarg = 1; prevoptind = 1; needpattern = 1; + fileeol = '\n'; eopts = getenv("GREP_OPTIONS"); @@ -605,6 +608,9 @@ main(int argc, char *argv[]) case 'X': filebehave = FILE_XZ; break; + case 'z': + fileeol = '\0'; + break; case 'Z': filebehave = FILE_GZIP; break; Modified: head/usr.bin/grep/grep.h ============================================================================== --- head/usr.bin/grep/grep.h Mon Apr 17 13:01:42 2017 (r317048) +++ head/usr.bin/grep/grep.h Mon Apr 17 13:14:18 2017 (r317049) @@ -116,6 +116,7 @@ extern bool dexclude, dinclude, fexclud extern unsigned long long Aflag, Bflag; extern long long mcount; extern long long mlimit; +extern char fileeol; extern char *label; extern const char *color; extern int binbehave, devbehave, dirbehave, filebehave, grepbehave, linkbehave; Modified: head/usr.bin/grep/util.c ============================================================================== --- head/usr.bin/grep/util.c Mon Apr 17 13:01:42 2017 (r317048) +++ head/usr.bin/grep/util.c Mon Apr 17 13:14:18 2017 (r317049) @@ -221,7 +221,7 @@ procfile(const char *fn) else break; } - if (ln.len > 0 && ln.dat[ln.len - 1] == '\n') + if (ln.len > 0 && ln.dat[ln.len - 1] == fileeol) --ln.len; ln.line_no++; @@ -530,6 +530,6 @@ printline(struct str *line, int sep, reg } } else { fwrite(line->dat, line->len, 1, stdout); - putchar('\n'); + putchar(fileeol); } }