Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/grep Import of our changes to grep developed in othe...



details:   https://anonhg.NetBSD.org/src/rev/929f125a6bcb
branches:  trunk
changeset: 557092:929f125a6bcb
user:      cjep <cjep%NetBSD.org@localhost>
date:      Fri Jan 02 15:00:29 2004 +0000

description:
Import of our changes to grep developed in othersrc. A very
brief summary:

* Add NetBSD RCS Ids. Change to use a date based version number.
* Remove unused variables and functions.
* Move towards NetBSD code style.
* Add missing GNU options (except for --include, --exclude and
        --line-buffered)
* Bug fixes
* Bug fixes and changes from OpenBSD's src/usr.bin/grep

A full list of changes can be viewed in the NetBSD CVS repository at
othersrc/usr.bin/grep. A ChangeLog is also available at:
        ftp://ftp.NetBSD.org/pub/NetBSD/misc/cjep/grep-ChangeLog.txt

If you want to help out, please let me (cjep@) know so that we can
organise our efforts efficiently.

diffstat:

 usr.bin/grep/grep.c   |  431 ++++++++++++++++++++++++++++++++++++-------------
 usr.bin/grep/grep.h   |  119 ++++++++----
 usr.bin/grep/mmfile.c |   26 +-
 usr.bin/grep/queue.c  |   30 +-
 usr.bin/grep/util.c   |  221 ++++++++++++++++++------
 5 files changed, 582 insertions(+), 245 deletions(-)

diffs (truncated from 1297 to 300 lines):

diff -r 6d491f7c811c -r 929f125a6bcb usr.bin/grep/grep.c
--- a/usr.bin/grep/grep.c       Fri Jan 02 15:00:28 2004 +0000
+++ b/usr.bin/grep/grep.c       Fri Jan 02 15:00:29 2004 +0000
@@ -1,3 +1,5 @@
+/*     $NetBSD: grep.c,v 1.1.1.2 2004/01/02 15:00:29 cjep Exp $        */
+
 /*-
  * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
  * All rights reserved.
@@ -23,8 +25,14 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- *     $Id: grep.c,v 1.1.1.1 2004/01/02 14:58:44 cjep Exp $
  */
+ 
+
+
+#include <sys/cdefs.h>
+#ifndef lint
+__RCSID("$NetBSD: grep.c,v 1.1.1.2 2004/01/02 15:00:29 cjep Exp $");
+#endif /* not lint */
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -32,6 +40,7 @@
 #include <err.h>
 #include <errno.h>
 #include <getopt.h>
+#include <limits.h>
 #include <regex.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -40,98 +49,141 @@
 
 #include "grep.h"
 
-/* Flags passed to regcomp() and regexec() */
-int     cflags;
-int     eflags = REG_STARTEND;
+/* 
+ * Upper bound of number of digits to represent an int in decimal
+ *  2^8n <= 10^3n. Allow a terminator.
+ */
+#define MAX_BUF_DIGITS (sizeof(int) * 3) + 1
 
-int     matchall;      /* shortcut */
-int     patterns, pattern_sz;
-char   **pattern;
+/* Flags passed to regcomp() and regexec() */
+int cflags = REG_BASIC;
+int eflags = REG_STARTEND;
+
+int matchall;  /* shortcut */
+int patterns, pattern_sz;
+char **pattern;
 regex_t        *r_pattern;
 
 /* For regex errors  */
-char    re_error[RE_ERROR_BUF + 1];
+char re_error[RE_ERROR_BUF + 1];
 
 /* Command-line flags */
-int     Aflag;         /* -A x: print x lines trailing each match */
-int     Bflag;         /* -B x: print x lines leading each match */
-int     Eflag;         /* -E: interpret pattern as extended regexp */
-int     Fflag;         /* -F: interpret pattern as list of fixed strings */
-int     Gflag;         /* -G: interpret pattern as basic regexp */
-int     Hflag;         /* -H: if -R, follow explicitly listed symlinks */
-int     Lflag;         /* -L: only show names of files with no matches */
-int     Pflag;         /* -P: if -R, no symlinks are followed */
-int     Rflag;         /* -R: recursively search directory trees */
-int     Sflag;         /* -S: if -R, follow all symlinks */
-int     Vflag;         /* -V: display version information */
-int     Zflag;         /* -Z: decompress input before processing */
-int     aflag;         /* -a: only search ascii files */
-int     bflag;         /* -b: show block numbers for each match */
-int     cflag;         /* -c: only show a count of matching lines */
-int     hflag;         /* -h: don't print filename headers */
-int     iflag;         /* -i: ignore case */
-int     lflag;         /* -l: only show names of files with matches */
-int     nflag;         /* -n: show line numbers in front of matching lines */
-int     oflag;         /* -o: always print file name */
-int     qflag;         /* -q: quiet mode (don't output anything) */
-int     sflag;         /* -s: silent mode (ignore errors) */
-int     vflag;         /* -v: only show non-matching lines */
-int     wflag;         /* -w: pattern must start and end on word boundaries */
-int     xflag;         /* -x: pattern must match entire line */
+int Aflag;             /* -A x: print x lines trailing each match */
+int Bflag;             /* -B x: print x lines leading each match */
+int Eflag;             /* -E: interpret pattern as extended regexp */
+int Fflag;             /* -F: interpret pattern as list of fixed strings */
+int Gflag;             /* -G: interpret pattern as basic regexp */
+int Hflag;             /* -H: Always print filenames */
+int Lflag;             /* -L: only show names of files with no matches */
+/*int Pflag;           *//* -P: if -r, no symlinks are followed */
+/*int Sflag;           *//* -S: if -r, follow all symlinks */
+int bflag;             /* -b: show block numbers for each match */
+int cflag;             /* -c: only show a count of matching lines */
+int hflag;             /* -h: Never print filenames. -H overrides */
+int lflag;             /* -l: only show names of files with matches */
+int mflag;             /* -m: specify maximum line matches (per file) */
+int nflag;             /* -n: show line numbers in front of matching lines */
+int oflag;             /* -o: only print out matches */
+int qflag;             /* -q: quiet mode (don't output anything) */
+int sflag;             /* -s: silent mode (ignore errors) */
+int vflag;             /* -v: only show non-matching lines */
+int wflag;             /* -w: pattern must start and end on word boundaries */
+int xflag;             /* -x: pattern must match entire line */
+
+int colours = 0;       /* Attempt to use terminal colours */
+const char *grep_colour = "01;32"; /* Default colour string, green */
+char *uc;
+
+/* Characters to print after filenames */
+char fn_endchar = '\n';
+char fn_colonchar = ':';
+char fn_dashchar = '-';
+char line_endchar = '\n';      /* End of line character */
+
+int maxcount = 0;              /* Maximum line matches per file */
+int output_filenames = 0;
+
+/* Argv[0] flags */
+int zgrep;             /* If we are invoked as zgrep */
+
+int binbehave = BIN_FILE_BIN;
+int dirbehave = GREP_READ;
+int devbehave = GREP_READ;
+/*int linkbehave = LINK_FOLLOW;*/
+char *stdin_label;
+
+enum {
+       BIN_OPT = CHAR_MAX + 1,
+       HELP_OPT,
+       LABEL_OPT,
+       MMAP_OPT,
+       LINK_OPT,
+       COLOUR_OPT
+};
 
 /* Housekeeping */
-int     first;         /* flag whether or not this is our fist match */
-int     tail;          /* lines left to print */
-int     lead;          /* number of lines in leading context queue */
-
-char   *progname;
+int first;             /* flag whether or not this is our first match */
+int tail;              /* lines left to print */
 
 static void
 usage(void)
 {
        fprintf(stderr, "usage: %s %s %s\n",
-               progname,
-               "[-[AB] num] [-CEFGHLPRSVZabchilnoqsvwx]",
-               "[-e patttern] [-f file]");
+               getprogname(),
+               "[-[ABC] num] [-EFGHILVZabcdhilnoqrsvwxz]",
+               "[-D action] [-d action] [-e pattern] [-f file]");
        exit(2);
 }
 
-static char *optstr = "0123456789A:B:CEFGHLPSRUVZabce:f:hilnoqrsuvwxy";
+static char *optstr = "0123456789A:B:C:D:EFGHILUVZabcd:e:f:hilm:noqrsuvwxyz";
 
-struct option long_options[] = 
+struct option long_options[] =
 {
-       {"basic-regexp",        no_argument,       NULL, 'G'},
+       {"binary-files",        required_argument, NULL, BIN_OPT},
+       {"help",                no_argument,       NULL, HELP_OPT},
+       {"label",               required_argument, NULL, LABEL_OPT},
+       {"mmap",                no_argument,       NULL, MMAP_OPT},
+/*     {"links",               required_argument, NULL, LINK_OPT},*/
+       {"after-context",       required_argument, NULL, 'A'},
+       {"before-context",      required_argument, NULL, 'B'},
+       {"color",               optional_argument, NULL, COLOUR_OPT},
+       {"colour",              optional_argument, NULL, COLOUR_OPT},
+       {"context",             optional_argument, NULL, 'C'},
+       {"devices",             required_argument, NULL, 'D'},
        {"extended-regexp",     no_argument,       NULL, 'E'},
        {"fixed-strings",       no_argument,       NULL, 'F'},
-       {"after-context",       required_argument, NULL, 'A'},
-       {"before-context",      required_argument, NULL, 'B'},
-       {"context",             optional_argument, NULL, 'C'},
+       {"fixed-regexp",        no_argument,       NULL, 'F'},
+       {"basic-regexp",        no_argument,       NULL, 'G'},
+       {"with-filename",       no_argument,       NULL, 'H'},
+       {"files-without-match", no_argument,       NULL, 'L'},
+       {"binary",              no_argument,       NULL, 'U'},
        {"version",             no_argument,       NULL, 'V'},
+       {"null",                no_argument,       NULL, 'Z'},
+       {"text",                no_argument,       NULL, 'a'},
        {"byte-offset",         no_argument,       NULL, 'b'},
        {"count",               no_argument,       NULL, 'c'},
+       {"directories",         required_argument, NULL, 'd'},
        {"regexp",              required_argument, NULL, 'e'},
        {"file",                required_argument, NULL, 'f'},
        {"no-filename",         no_argument,       NULL, 'h'},
        {"ignore-case",         no_argument,       NULL, 'i'},
-       {"files-without-match", no_argument,       NULL, 'L'},
        {"files-with-matches",  no_argument,       NULL, 'l'},
+       {"max-count",           required_argument, NULL, 'm'},
        {"line-number",         no_argument,       NULL, 'n'},
+       {"only-matching",       no_argument,       NULL, 'o'},
        {"quiet",               no_argument,       NULL, 'q'},
        {"silent",              no_argument,       NULL, 'q'},
        {"recursive",           no_argument,       NULL, 'r'},
        {"no-messages",         no_argument,       NULL, 's'},
-       {"text",                no_argument,       NULL, 'a'},
-       {"revert-match",        no_argument,       NULL, 'v'},
+       {"unix-byte-offsets",   no_argument,       NULL, 'u'},
+       {"invert-match",        no_argument,       NULL, 'v'},
        {"word-regexp",         no_argument,       NULL, 'w'},
        {"line-regexp",         no_argument,       NULL, 'x'},
-       {"binary",              no_argument,       NULL, 'U'},
-       {"unix-byte-offsets",   no_argument,       NULL, 'u'},
-       {"decompress",          no_argument,       NULL, 'Z'},
-       
+       {"null-data",           no_argument,       NULL, 'z'},
+               
        {NULL,                  no_argument,       NULL, 0}
 };
 
-
 static void
 add_pattern(char *pat, size_t len)
 {
@@ -141,11 +193,11 @@
        }
        if (patterns == pattern_sz) {
                pattern_sz *= 2;
-               pattern = grep_realloc(pattern, ++pattern_sz);
+               pattern = grep_realloc(pattern, ++pattern_sz * sizeof(*pattern));
        }
-       if (pat[len-1] == '\n')
+       if (pat[len - 1] == '\n')
                --len;
-       pattern[patterns] = grep_malloc(len+1);
+       pattern[patterns] = grep_malloc(len + 1);
        strncpy(pattern[patterns], pat, len);
        pattern[patterns][len] = '\0';
        ++patterns;
@@ -160,7 +212,7 @@
        int nl;
 
        if ((f = fopen(fn, "r")) == NULL)
-               err(1, "%s", fn);
+               err(2, "%s", fn);
        nl = 0;
        while ((line = fgetln(f, &len)) != NULL) {
                if (*line == '\n') {
@@ -175,85 +227,163 @@
                add_pattern(line, len);
        }
        if (ferror(f))
-               err(1, "%s", fn);
+               err(2, "%s", fn);
        fclose(f);
 }
 
+static int
+check_context_arg(char const *str) {
+       char *ep;
+       long lval;
+
+       errno = 0;
+       lval = strtol(str, &ep, 10);
+
+       if (str[0] == '\0' || *ep != '\0')
+               errx(2, "Invalid context argument");
+
+       if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
+           (lval > INT_MAX || lval < INT_MIN))
+               errx(2, "Context argument out of range");
+
+       return lval;
+
+}
+
+static int
+grep_getopt(int argc, char *const *argv) 
+{
+       int c, ptr;
+       char buffer[MAX_BUF_DIGITS];
+
+       ptr = 0;
+       while (c = getopt_long(argc, argv, optstr, long_options, 
+               (int *)NULL), '0' <= c && 
+               c <= '9' && ptr < MAX_BUF_DIGITS) {
+               
+               /* Avoid leading zeros */
+               if (ptr != 0 || (ptr == 0 && c != '0'))
+                       buffer[ptr++] = c;
+       }
+
+       if (ptr >= MAX_BUF_DIGITS)
+               errx(2, "Context argument out of range");
+



Home | Main Index | Thread Index | Old Index