Home | History | Annotate | Line # | Download | only in grep
grep.c revision 1.1
      1  1.1  cjep /*-
      2  1.1  cjep  * Copyright (c) 1999 James Howard and Dag-Erling Codan Smrgrav
      3  1.1  cjep  * All rights reserved.
      4  1.1  cjep  *
      5  1.1  cjep  * Redistribution and use in source and binary forms, with or without
      6  1.1  cjep  * modification, are permitted provided that the following conditions
      7  1.1  cjep  * are met:
      8  1.1  cjep  * 1. Redistributions of source code must retain the above copyright
      9  1.1  cjep  *    notice, this list of conditions and the following disclaimer.
     10  1.1  cjep  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  cjep  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  cjep  *    documentation and/or other materials provided with the distribution.
     13  1.1  cjep  *
     14  1.1  cjep  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     15  1.1  cjep  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  1.1  cjep  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     17  1.1  cjep  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     18  1.1  cjep  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     19  1.1  cjep  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     20  1.1  cjep  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     21  1.1  cjep  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     22  1.1  cjep  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     23  1.1  cjep  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     24  1.1  cjep  * SUCH DAMAGE.
     25  1.1  cjep  *
     26  1.1  cjep  *	$Id: grep.c,v 1.1 2004/01/02 14:58:44 cjep Exp $
     27  1.1  cjep  */
     28  1.1  cjep 
     29  1.1  cjep #include <sys/types.h>
     30  1.1  cjep #include <sys/stat.h>
     31  1.1  cjep 
     32  1.1  cjep #include <err.h>
     33  1.1  cjep #include <errno.h>
     34  1.1  cjep #include <getopt.h>
     35  1.1  cjep #include <regex.h>
     36  1.1  cjep #include <stdio.h>
     37  1.1  cjep #include <stdlib.h>
     38  1.1  cjep #include <string.h>
     39  1.1  cjep #include <unistd.h>
     40  1.1  cjep 
     41  1.1  cjep #include "grep.h"
     42  1.1  cjep 
     43  1.1  cjep /* Flags passed to regcomp() and regexec() */
     44  1.1  cjep int	 cflags;
     45  1.1  cjep int	 eflags = REG_STARTEND;
     46  1.1  cjep 
     47  1.1  cjep int	 matchall;	/* shortcut */
     48  1.1  cjep int	 patterns, pattern_sz;
     49  1.1  cjep char   **pattern;
     50  1.1  cjep regex_t	*r_pattern;
     51  1.1  cjep 
     52  1.1  cjep /* For regex errors  */
     53  1.1  cjep char	 re_error[RE_ERROR_BUF + 1];
     54  1.1  cjep 
     55  1.1  cjep /* Command-line flags */
     56  1.1  cjep int	 Aflag;		/* -A x: print x lines trailing each match */
     57  1.1  cjep int	 Bflag;		/* -B x: print x lines leading each match */
     58  1.1  cjep int	 Eflag;		/* -E: interpret pattern as extended regexp */
     59  1.1  cjep int	 Fflag;		/* -F: interpret pattern as list of fixed strings */
     60  1.1  cjep int	 Gflag;		/* -G: interpret pattern as basic regexp */
     61  1.1  cjep int	 Hflag;		/* -H: if -R, follow explicitly listed symlinks */
     62  1.1  cjep int	 Lflag;		/* -L: only show names of files with no matches */
     63  1.1  cjep int	 Pflag;		/* -P: if -R, no symlinks are followed */
     64  1.1  cjep int	 Rflag;		/* -R: recursively search directory trees */
     65  1.1  cjep int	 Sflag;		/* -S: if -R, follow all symlinks */
     66  1.1  cjep int	 Vflag;		/* -V: display version information */
     67  1.1  cjep int	 Zflag;		/* -Z: decompress input before processing */
     68  1.1  cjep int	 aflag;		/* -a: only search ascii files */
     69  1.1  cjep int	 bflag;		/* -b: show block numbers for each match */
     70  1.1  cjep int	 cflag;		/* -c: only show a count of matching lines */
     71  1.1  cjep int	 hflag;		/* -h: don't print filename headers */
     72  1.1  cjep int	 iflag;		/* -i: ignore case */
     73  1.1  cjep int	 lflag;		/* -l: only show names of files with matches */
     74  1.1  cjep int	 nflag;		/* -n: show line numbers in front of matching lines */
     75  1.1  cjep int	 oflag;		/* -o: always print file name */
     76  1.1  cjep int	 qflag;		/* -q: quiet mode (don't output anything) */
     77  1.1  cjep int	 sflag;		/* -s: silent mode (ignore errors) */
     78  1.1  cjep int	 vflag;		/* -v: only show non-matching lines */
     79  1.1  cjep int	 wflag;		/* -w: pattern must start and end on word boundaries */
     80  1.1  cjep int	 xflag;		/* -x: pattern must match entire line */
     81  1.1  cjep 
     82  1.1  cjep /* Housekeeping */
     83  1.1  cjep int	 first;		/* flag whether or not this is our fist match */
     84  1.1  cjep int	 tail;		/* lines left to print */
     85  1.1  cjep int	 lead;		/* number of lines in leading context queue */
     86  1.1  cjep 
     87  1.1  cjep char	*progname;
     88  1.1  cjep 
     89  1.1  cjep static void
     90  1.1  cjep usage(void)
     91  1.1  cjep {
     92  1.1  cjep 	fprintf(stderr, "usage: %s %s %s\n",
     93  1.1  cjep 		progname,
     94  1.1  cjep 		"[-[AB] num] [-CEFGHLPRSVZabchilnoqsvwx]",
     95  1.1  cjep 		"[-e patttern] [-f file]");
     96  1.1  cjep 	exit(2);
     97  1.1  cjep }
     98  1.1  cjep 
     99  1.1  cjep static char *optstr = "0123456789A:B:CEFGHLPSRUVZabce:f:hilnoqrsuvwxy";
    100  1.1  cjep 
    101  1.1  cjep struct option long_options[] =
    102  1.1  cjep {
    103  1.1  cjep 	{"basic-regexp",        no_argument,       NULL, 'G'},
    104  1.1  cjep 	{"extended-regexp",     no_argument,       NULL, 'E'},
    105  1.1  cjep 	{"fixed-strings",       no_argument,       NULL, 'F'},
    106  1.1  cjep 	{"after-context",       required_argument, NULL, 'A'},
    107  1.1  cjep 	{"before-context",      required_argument, NULL, 'B'},
    108  1.1  cjep 	{"context",             optional_argument, NULL, 'C'},
    109  1.1  cjep 	{"version",             no_argument,       NULL, 'V'},
    110  1.1  cjep 	{"byte-offset",         no_argument,       NULL, 'b'},
    111  1.1  cjep 	{"count",               no_argument,       NULL, 'c'},
    112  1.1  cjep 	{"regexp",              required_argument, NULL, 'e'},
    113  1.1  cjep 	{"file",                required_argument, NULL, 'f'},
    114  1.1  cjep 	{"no-filename",         no_argument,       NULL, 'h'},
    115  1.1  cjep 	{"ignore-case",         no_argument,       NULL, 'i'},
    116  1.1  cjep 	{"files-without-match", no_argument,       NULL, 'L'},
    117  1.1  cjep 	{"files-with-matches",  no_argument,       NULL, 'l'},
    118  1.1  cjep 	{"line-number",         no_argument,       NULL, 'n'},
    119  1.1  cjep 	{"quiet",               no_argument,       NULL, 'q'},
    120  1.1  cjep 	{"silent",              no_argument,       NULL, 'q'},
    121  1.1  cjep 	{"recursive",           no_argument,       NULL, 'r'},
    122  1.1  cjep 	{"no-messages",         no_argument,       NULL, 's'},
    123  1.1  cjep 	{"text",                no_argument,       NULL, 'a'},
    124  1.1  cjep 	{"revert-match",        no_argument,       NULL, 'v'},
    125  1.1  cjep 	{"word-regexp",         no_argument,       NULL, 'w'},
    126  1.1  cjep 	{"line-regexp",         no_argument,       NULL, 'x'},
    127  1.1  cjep 	{"binary",              no_argument,       NULL, 'U'},
    128  1.1  cjep 	{"unix-byte-offsets",   no_argument,       NULL, 'u'},
    129  1.1  cjep 	{"decompress",          no_argument,       NULL, 'Z'},
    130  1.1  cjep 
    131  1.1  cjep 	{NULL,                  no_argument,       NULL, 0}
    132  1.1  cjep };
    133  1.1  cjep 
    134  1.1  cjep 
    135  1.1  cjep static void
    136  1.1  cjep add_pattern(char *pat, size_t len)
    137  1.1  cjep {
    138  1.1  cjep 	if (len == 0 || matchall) {
    139  1.1  cjep 		matchall = 1;
    140  1.1  cjep 		return;
    141  1.1  cjep 	}
    142  1.1  cjep 	if (patterns == pattern_sz) {
    143  1.1  cjep 		pattern_sz *= 2;
    144  1.1  cjep 		pattern = grep_realloc(pattern, ++pattern_sz);
    145  1.1  cjep 	}
    146  1.1  cjep 	if (pat[len-1] == '\n')
    147  1.1  cjep 		--len;
    148  1.1  cjep 	pattern[patterns] = grep_malloc(len+1);
    149  1.1  cjep 	strncpy(pattern[patterns], pat, len);
    150  1.1  cjep 	pattern[patterns][len] = '\0';
    151  1.1  cjep 	++patterns;
    152  1.1  cjep }
    153  1.1  cjep 
    154  1.1  cjep static void
    155  1.1  cjep read_patterns(char *fn)
    156  1.1  cjep {
    157  1.1  cjep 	FILE *f;
    158  1.1  cjep 	char *line;
    159  1.1  cjep 	size_t len;
    160  1.1  cjep 	int nl;
    161  1.1  cjep 
    162  1.1  cjep 	if ((f = fopen(fn, "r")) == NULL)
    163  1.1  cjep 		err(1, "%s", fn);
    164  1.1  cjep 	nl = 0;
    165  1.1  cjep 	while ((line = fgetln(f, &len)) != NULL) {
    166  1.1  cjep 		if (*line == '\n') {
    167  1.1  cjep 			++nl;
    168  1.1  cjep 			continue;
    169  1.1  cjep 		}
    170  1.1  cjep 		if (nl) {
    171  1.1  cjep 			matchall = 1;
    172  1.1  cjep 			break;
    173  1.1  cjep 		}
    174  1.1  cjep 		nl = 0;
    175  1.1  cjep 		add_pattern(line, len);
    176  1.1  cjep 	}
    177  1.1  cjep 	if (ferror(f))
    178  1.1  cjep 		err(1, "%s", fn);
    179  1.1  cjep 	fclose(f);
    180  1.1  cjep }
    181  1.1  cjep 
    182  1.1  cjep int
    183  1.1  cjep main(int argc, char *argv[])
    184  1.1  cjep {
    185  1.1  cjep 	char *tmp;
    186  1.1  cjep 	int c, i;
    187  1.1  cjep 
    188  1.1  cjep 	if ((progname = strrchr(*argv, '/')) != NULL)
    189  1.1  cjep 		++progname;
    190  1.1  cjep 	else
    191  1.1  cjep 		progname = *argv;
    192  1.1  cjep 
    193  1.1  cjep 	while ((c = getopt_long(argc, argv, optstr,
    194  1.1  cjep 				long_options, (int *)NULL)) != -1) {
    195  1.1  cjep 		switch (c) {
    196  1.1  cjep 		case '0': case '1': case '2': case '3': case '4':
    197  1.1  cjep 		case '5': case '6': case '7': case '8': case '9':
    198  1.1  cjep 			tmp = argv[optind - 1];
    199  1.1  cjep 			if (tmp[0] == '-' && tmp[1] == c && !tmp[2])
    200  1.1  cjep 				Aflag = Bflag = strtol(++tmp, (char **)NULL, 10);
    201  1.1  cjep 			else
    202  1.1  cjep 				Aflag = Bflag = strtol(argv[optind] + 1, (char **)NULL, 10);
    203  1.1  cjep 			break;
    204  1.1  cjep 		case 'A':
    205  1.1  cjep 			Aflag = strtol(optarg, (char **)NULL, 10);
    206  1.1  cjep 			break;
    207  1.1  cjep 		case 'B':
    208  1.1  cjep 			Bflag = strtol(optarg, (char **)NULL, 10);
    209  1.1  cjep 			break;
    210  1.1  cjep 		case 'C':
    211  1.1  cjep 			if (optarg == NULL)
    212  1.1  cjep 				Aflag = Bflag = 2;
    213  1.1  cjep 			else
    214  1.1  cjep 				Aflag = Bflag = strtol(optarg, (char **)NULL, 10);
    215  1.1  cjep 			break;
    216  1.1  cjep 		case 'E':
    217  1.1  cjep 			Eflag++;
    218  1.1  cjep 			break;
    219  1.1  cjep 		case 'F':
    220  1.1  cjep 			Fflag++;
    221  1.1  cjep 			break;
    222  1.1  cjep 		case 'G':
    223  1.1  cjep 			Gflag++;
    224  1.1  cjep 			break;
    225  1.1  cjep 		case 'H':
    226  1.1  cjep 			Hflag++;
    227  1.1  cjep 			break;
    228  1.1  cjep 		case 'L':
    229  1.1  cjep 			lflag = 0;
    230  1.1  cjep 			Lflag = qflag = 1;
    231  1.1  cjep 			break;
    232  1.1  cjep 		case 'P':
    233  1.1  cjep 			Pflag++;
    234  1.1  cjep 			break;
    235  1.1  cjep 		case 'S':
    236  1.1  cjep 			Sflag++;
    237  1.1  cjep 			break;
    238  1.1  cjep 		case 'R':
    239  1.1  cjep 		case 'r':
    240  1.1  cjep 			Rflag++;
    241  1.1  cjep 			oflag++;
    242  1.1  cjep 			break;
    243  1.1  cjep 		case 'U':
    244  1.1  cjep 		case 'u':
    245  1.1  cjep 			/* these are here for compatability */
    246  1.1  cjep 			break;
    247  1.1  cjep 		case 'V':
    248  1.1  cjep 			fprintf(stderr, "grep version %u.%u\n", VER_MAJ, VER_MIN);
    249  1.1  cjep 			fprintf(stderr, argv[0]);
    250  1.1  cjep 			usage();
    251  1.1  cjep 			break;
    252  1.1  cjep 		case 'Z':
    253  1.1  cjep 			Zflag++;
    254  1.1  cjep 			break;
    255  1.1  cjep 		case 'a':
    256  1.1  cjep 			aflag = 1;
    257  1.1  cjep 			break;
    258  1.1  cjep 		case 'b':
    259  1.1  cjep 			bflag = 1;
    260  1.1  cjep 			break;
    261  1.1  cjep 		case 'c':
    262  1.1  cjep 			cflag = 1;
    263  1.1  cjep 			break;
    264  1.1  cjep 		case 'e':
    265  1.1  cjep 			add_pattern(optarg, strlen(optarg));
    266  1.1  cjep 			break;
    267  1.1  cjep 		case 'f':
    268  1.1  cjep 			read_patterns(optarg);
    269  1.1  cjep 			break;
    270  1.1  cjep 		case 'h':
    271  1.1  cjep 			oflag = 0;
    272  1.1  cjep 			hflag = 1;
    273  1.1  cjep 			break;
    274  1.1  cjep 		case 'i':
    275  1.1  cjep 		case 'y':
    276  1.1  cjep 			cflags |= REG_ICASE;
    277  1.1  cjep 			break;
    278  1.1  cjep 		case 'l':
    279  1.1  cjep 			Lflag = 0;
    280  1.1  cjep 			lflag = qflag = 1;
    281  1.1  cjep 			break;
    282  1.1  cjep 		case 'n':
    283  1.1  cjep 			nflag = 1;
    284  1.1  cjep 			break;
    285  1.1  cjep 		case 'o':
    286  1.1  cjep 			hflag = 0;
    287  1.1  cjep 			oflag = 1;
    288  1.1  cjep 			break;
    289  1.1  cjep 		case 'q':
    290  1.1  cjep 			qflag = 1;
    291  1.1  cjep 			break;
    292  1.1  cjep 		case 's':
    293  1.1  cjep 			sflag = 1;
    294  1.1  cjep 			break;
    295  1.1  cjep 		case 'v':
    296  1.1  cjep 			vflag = 1;
    297  1.1  cjep 			break;
    298  1.1  cjep 		case 'w':
    299  1.1  cjep 			wflag = 1;
    300  1.1  cjep 			break;
    301  1.1  cjep 		case 'x':
    302  1.1  cjep 			xflag = 1;
    303  1.1  cjep 			break;
    304  1.1  cjep 		default:
    305  1.1  cjep 			usage();
    306  1.1  cjep 		}
    307  1.1  cjep 	}
    308  1.1  cjep 
    309  1.1  cjep 	argc -= optind;
    310  1.1  cjep 	argv += optind;
    311  1.1  cjep 
    312  1.1  cjep 	if (argc == 0 && patterns == 0)
    313  1.1  cjep 		usage();
    314  1.1  cjep 
    315  1.1  cjep 	if (patterns == 0) {
    316  1.1  cjep 		add_pattern(*argv, strlen(*argv));
    317  1.1  cjep 		--argc;
    318  1.1  cjep 		++argv;
    319  1.1  cjep 	}
    320  1.1  cjep 
    321  1.1  cjep 	switch (*progname) {
    322  1.1  cjep 	case 'e':
    323  1.1  cjep 		Eflag++;
    324  1.1  cjep 		break;
    325  1.1  cjep 	case 'f':
    326  1.1  cjep 		Fflag++;
    327  1.1  cjep 		break;
    328  1.1  cjep 	case 'g':
    329  1.1  cjep 		Gflag++;
    330  1.1  cjep 		break;
    331  1.1  cjep 	case 'z':
    332  1.1  cjep 		Zflag++;
    333  1.1  cjep 		break;
    334  1.1  cjep 	}
    335  1.1  cjep 
    336  1.1  cjep 	cflags |= Eflag ? REG_EXTENDED : REG_BASIC;
    337  1.1  cjep 	r_pattern = grep_malloc(patterns * sizeof(regex_t));
    338  1.1  cjep 	for (i = 0; i < patterns; ++i) {
    339  1.1  cjep 		if ((c = regcomp(&r_pattern[i], pattern[i], cflags))) {
    340  1.1  cjep 			regerror(c, &r_pattern[i], re_error, RE_ERROR_BUF);
    341  1.1  cjep 			errx(1, "%s", re_error);
    342  1.1  cjep 		}
    343  1.1  cjep 	}
    344  1.1  cjep 
    345  1.1  cjep 	if ((argc == 0 || argc == 1) && !oflag)
    346  1.1  cjep 		hflag = 1;
    347  1.1  cjep 
    348  1.1  cjep 	if (argc == 0)
    349  1.1  cjep 		exit(!procfile(NULL));
    350  1.1  cjep 
    351  1.1  cjep 	if (Rflag)
    352  1.1  cjep 		c = grep_tree(argv);
    353  1.1  cjep 	else
    354  1.1  cjep 		for (c = 0; argc--; ++argv)
    355  1.1  cjep 			c += procfile(*argv);
    356  1.1  cjep 
    357  1.1  cjep 	exit(!c);
    358  1.1  cjep }
    359