Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/grep Add support for --null-data. Change -Z to behav...



details:   https://anonhg.NetBSD.org/src/rev/30bbfefc805f
branches:  trunk
changeset: 764388:30bbfefc805f
user:      joerg <joerg%NetBSD.org@localhost>
date:      Mon Apr 18 22:46:48 2011 +0000

description:
Add support for --null-data. Change -Z to behave like GNU grep's -Z.

diffstat:

 usr.bin/grep/file.c                  |  11 ++++++-----
 usr.bin/grep/grep.1                  |  36 ++++++++++++++++++------------------
 usr.bin/grep/grep.c                  |  32 ++++++++++++++++++++------------
 usr.bin/grep/grep.h                  |   5 +++--
 usr.bin/grep/nls/C.msg               |   4 ++--
 usr.bin/grep/nls/es_ES.ISO8859-1.msg |   5 +++--
 usr.bin/grep/nls/gl_ES.ISO8859-1.msg |   5 +++--
 usr.bin/grep/nls/hu_HU.ISO8859-2.msg |   5 +++--
 usr.bin/grep/nls/ja_JP.SJIS.msg      |   5 +++--
 usr.bin/grep/nls/ja_JP.UTF-8.msg     |   5 +++--
 usr.bin/grep/nls/ja_JP.eucJP.msg     |   5 +++--
 usr.bin/grep/nls/pt_BR.ISO8859-1.msg |   5 +++--
 usr.bin/grep/nls/ru_RU.KOI8-R.msg    |   5 +++--
 usr.bin/grep/nls/uk_UA.UTF-8.msg     |   5 +++--
 usr.bin/grep/nls/zh_CN.UTF-8.msg     |   5 +++--
 usr.bin/grep/util.c                  |  16 ++++++++--------
 16 files changed, 87 insertions(+), 67 deletions(-)

diffs (truncated from 537 to 300 lines):

diff -r 6b97c725e4cc -r 30bbfefc805f usr.bin/grep/file.c
--- a/usr.bin/grep/file.c       Mon Apr 18 22:05:39 2011 +0000
+++ b/usr.bin/grep/file.c       Mon Apr 18 22:46:48 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: file.c,v 1.6 2011/04/18 03:27:40 joerg Exp $   */
+/*     $NetBSD: file.c,v 1.7 2011/04/18 22:46:48 joerg Exp $   */
 /*     $FreeBSD: head/usr.bin/grep/file.c 211496 2010-08-19 09:28:59Z des $    */
 /*     $OpenBSD: file.c,v 1.11 2010/07/02 20:48:48 nicm Exp $  */
 
@@ -35,7 +35,7 @@
 #endif
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: file.c,v 1.6 2011/04/18 03:27:40 joerg Exp $");
+__RCSID("$NetBSD: file.c,v 1.7 2011/04/18 22:46:48 joerg Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -147,7 +147,7 @@
        }
 
        /* Look for a newline in the remaining part of the buffer */
-       if ((p = memchr(bufpos, '\n', bufrem)) != NULL) {
+       if ((p = memchr(bufpos, line_sep, bufrem)) != NULL) {
                ++p; /* advance over newline */
                ret = (char *)bufpos;
                len = p - bufpos;
@@ -169,7 +169,7 @@
                if (bufrem == 0)
                        /* EOF: return partial line */
                        break;
-               if ((p = memchr(bufpos, '\n', bufrem)) == NULL)
+               if ((p = memchr(bufpos, line_sep, bufrem)) == NULL)
                        continue;
                /* got it: finish up the line (like code above) */
                ++p;
@@ -207,7 +207,8 @@
                goto error;
 
        /* Check for binary stuff, if necessary */
-       if (binbehave != BINFILE_TEXT && memchr(bufpos, '\0', bufrem) != NULL)
+       if (!nulldataflag && binbehave != BINFILE_TEXT &&
+           memchr(bufpos, '\0', bufrem) != NULL)
                f->binary = true;
 
        return (f);
diff -r 6b97c725e4cc -r 30bbfefc805f usr.bin/grep/grep.1
--- a/usr.bin/grep/grep.1       Mon Apr 18 22:05:39 2011 +0000
+++ b/usr.bin/grep/grep.1       Mon Apr 18 22:46:48 2011 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: grep.1,v 1.2 2011/02/16 01:31:33 joerg Exp $
+.\"    $NetBSD: grep.1,v 1.3 2011/04/18 22:46:48 joerg Exp $
 .\"    $FreeBSD: head/usr.bin/grep/grep.1 210652 2010-07-30 14:05:20Z joel $
 .\"    $OpenBSD: grep.1,v 1.38 2010/04/05 06:30:59 jmc Exp $
 .\" Copyright (c) 1980, 1990, 1993
@@ -30,7 +30,7 @@
 .\"
 .\"    @(#)grep.1      8.3 (Berkeley) 4/18/94
 .\"
-.Dd July 28, 2010
+.Dd April 19, 2011
 .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
@@ -50,9 +50,9 @@
 .Op Fl Fl color Ns Op = Ns Ar when
 .Op Fl Fl colour Ns Op = Ns Ar when
 .Op Fl Fl context Ns Op = Ns Ar num
+.Op Fl Fl decompress
 .Op Fl Fl label
 .Op Fl Fl line-buffered
-.Op Fl Fl null
 .Op Ar pattern
 .Op Ar
 .Ek
@@ -318,8 +318,6 @@
 .Fl q
 is
 specified.
-.It Fl Fl null
-Prints a zero-byte after the file name.
 .It Fl O
 If
 .Fl R
@@ -372,11 +370,10 @@
 Equivalent to
 .Fl i .
 Obsoleted.
-.It Fl Z , Fl z , Fl Fl decompress
-Force
-.Nm grep
-to behave as
-.Nm zgrep .
+.It Fl Z , Fl Fl null
+Prints a zero-byte after the file name.
+.It Fl z , Fl Fl null-data
+Use the zero byte (ASCII NUL) as line separator.
 .It Fl Fl binary-files Ns = Ns Ar value
 Controls searching and printing of binary files.
 Options are
@@ -387,13 +384,16 @@
 and
 .Ar text :
 treat all files as text.
-.Sm off
-.It Fl Fl context Op = Ar num
-.Sm on
-Print
-.Ar num
-lines of leading and trailing context.
-The default is 2.
+.It Fl Fl decompress
+Detect input files compressed with
+.Xr bzip2 1
+or
+.Xr gzip 1
+and decompress them dynamically.
+This makes
+.Nm grep
+behave like
+.Nm zgrep .
 .It Fl Fl line-buffered
 Force output to be line buffered.
 By default, output is line buffered when standard output is a terminal
diff -r 6b97c725e4cc -r 30bbfefc805f usr.bin/grep/grep.c
--- a/usr.bin/grep/grep.c       Mon Apr 18 22:05:39 2011 +0000
+++ b/usr.bin/grep/grep.c       Mon Apr 18 22:46:48 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: grep.c,v 1.7 2011/04/18 17:18:03 joerg Exp $   */
+/*     $NetBSD: grep.c,v 1.8 2011/04/18 22:46:48 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.7 2011/04/18 17:18:03 joerg Exp $");
+__RCSID("$NetBSD: grep.c,v 1.8 2011/04/18 22:46:48 joerg Exp $");
 
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -68,10 +68,10 @@
 /* 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",
+/* 7*/ "\t[pattern] [file ...]\n",
 /* 8*/ "Binary file %s matches\n",
 /* 9*/ "%s (BSD grep) %s\n",
 };
@@ -118,6 +118,8 @@
 bool    xflag;         /* -x: pattern must match entire line */
 bool    lbflag;        /* --line-buffered */
 bool    nullflag;      /* --null */
+bool    nulldataflag;  /* --null-data */
+unsigned char line_sep = '\n'; /* 0 for --null-data */
 char   *label;         /* --label */
 const char *color;     /* --color */
 int     grepbehave = GREP_BASIC;       /* -EFGP: type of the regex */
@@ -133,11 +135,11 @@
 enum {
        BIN_OPT = CHAR_MAX + 1,
        COLOR_OPT,
+       DECOMPRESS_OPT,
        HELP_OPT,
        MMAP_OPT,
        LINEBUF_OPT,
        LABEL_OPT,
-       NULL_OPT,
        R_EXCLUDE_OPT,
        R_INCLUDE_OPT,
        R_DEXCLUDE_OPT,
@@ -166,16 +168,17 @@
        exit(2);
 }
 
-static const char      *optstr = "0123456789A:B:C:D:EFGHIJLOPSRUVZabcd:e:f:hilm:nopqrsuvwxy";
+static const char optstr[] =
+    "0123456789A:B:C:D:EFGHIJLOPSRUVZabcd:e:f:hilm:nopqrsuvwxyz";
 
 struct option long_options[] =
 {
        {"binary-files",        required_argument,      NULL, BIN_OPT},
+       {"decompress",          no_argument,            NULL, DECOMPRESS_OPT},
        {"help",                no_argument,            NULL, HELP_OPT},
        {"mmap",                no_argument,            NULL, MMAP_OPT},
        {"line-buffered",       no_argument,            NULL, LINEBUF_OPT},
        {"label",               required_argument,      NULL, LABEL_OPT},
-       {"null",                no_argument,            NULL, NULL_OPT},
        {"color",               optional_argument,      NULL, COLOR_OPT},
        {"colour",              optional_argument,      NULL, COLOR_OPT},
        {"exclude",             required_argument,      NULL, R_EXCLUDE_OPT},
@@ -214,7 +217,8 @@
        {"version",             no_argument,            NULL, 'V'},
        {"word-regexp",         no_argument,            NULL, 'w'},
        {"line-regexp",         no_argument,            NULL, 'x'},
-       {"decompress",          no_argument,            NULL, 'Z'},
+       {"null",                no_argument,            NULL, 'Z'},
+       {"null-data",           no_argument,            NULL, 'z'},
        {NULL,                  no_argument,            NULL, 0}
 };
 
@@ -563,7 +567,11 @@
                        xflag = true;
                        break;
                case 'Z':
-                       filebehave = FILE_GZIP;
+                       nullflag = true;
+                       break;
+               case 'z':
+                       nulldataflag = true;
+                       line_sep = '\0';
                        break;
                case BIN_OPT:
                        if (strcasecmp("binary", optarg) == 0)
@@ -595,15 +603,15 @@
                            strcasecmp("no", optarg) != 0)
                                errx(2, getstr(3), "--color");
                        break;
+               case DECOMPRESS_OPT:
+                       filebehave = FILE_GZIP;
+                       break;
                case LABEL_OPT:
                        label = optarg;
                        break;
                case LINEBUF_OPT:
                        lbflag = true;
                        break;
-               case NULL_OPT:
-                       nullflag = true;
-                       break;
                case R_INCLUDE_OPT:
                        finclude = true;
                        add_fpattern(optarg, INCL_PAT);
diff -r 6b97c725e4cc -r 30bbfefc805f usr.bin/grep/grep.h
--- a/usr.bin/grep/grep.h       Mon Apr 18 22:05:39 2011 +0000
+++ b/usr.bin/grep/grep.h       Mon Apr 18 22:46:48 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: grep.h,v 1.6 2011/04/18 17:18:04 joerg Exp $   */
+/*     $NetBSD: grep.h,v 1.7 2011/04/18 22:46:48 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 $    */
 
@@ -113,7 +113,8 @@
 extern bool     Eflag, Fflag, Gflag, Hflag, Lflag,
                 bflag, cflag, hflag, iflag, lflag, mflag, nflag, oflag,
                 qflag, sflag, vflag, wflag, xflag;
-extern bool     dexclude, dinclude, fexclude, finclude, lbflag, nullflag;
+extern bool     dexclude, dinclude, fexclude, finclude, lbflag, nullflag, nulldataflag;
+extern unsigned char line_sep;
 extern unsigned long long Aflag, Bflag, mcount;
 extern char    *label;
 extern const char *color;
diff -r 6b97c725e4cc -r 30bbfefc805f usr.bin/grep/nls/C.msg
--- a/usr.bin/grep/nls/C.msg    Mon Apr 18 22:05:39 2011 +0000
+++ b/usr.bin/grep/nls/C.msg    Mon Apr 18 22:46:48 2011 +0000
@@ -5,9 +5,9 @@
 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"
+7 "\t[pattern] [file ...]\n"
 8 "Binary file %s matches\n"
 9 "%s (BSD grep) %s\n"
diff -r 6b97c725e4cc -r 30bbfefc805f usr.bin/grep/nls/es_ES.ISO8859-1.msg
--- a/usr.bin/grep/nls/es_ES.ISO8859-1.msg      Mon Apr 18 22:05:39 2011 +0000
+++ b/usr.bin/grep/nls/es_ES.ISO8859-1.msg      Mon Apr 18 22:46:48 2011 +0000
@@ -1,3 +1,4 @@
+$ $NetBSD: es_ES.ISO8859-1.msg,v 1.2 2011/04/18 22:46:48 joerg Exp $
 $ $FreeBSD: head/usr.bin/grep/nls/es_ES.ISO8859-1.msg 210622 2010-07-29 18:02:57Z gabor $
 $
 $set 1
@@ -5,9 +6,9 @@
 1 "(entrada estándar)"
 2 "no se puede leer el fichero comprimido bzip2"
 3 "opción desconocida de %s"
-4 "uso: %s [-abcDEFGHhIiJLlmnOoPqRSsUVvwxZ] [-A no] [-B no] [-C[no]]\n"
+4 "uso: %s [-abcDEFGHhIiJLlmnOoPqRSsUVvwxZz] [-A no] [-B no] [-C[no]]\n"
 5 "\t[-e pauta] [-f fichero] [--binary-files=valor] [--color=cuando]\n"
 6 "\t[--context[=no]] [--directories=acción] [--label] [--line-buffered]\n"
-7 "\t[--null] [pauta] [fichero ...]\n"
+7 "\t[pauta] [fichero ...]\n"



Home | Main Index | Thread Index | Old Index