Home | History | Annotate | Line # | Download | only in grep
util.c revision 1.2
      1  1.2   dsl /*	$NetBSD: util.c,v 1.2 2004/10/30 17:37:10 dsl Exp $	*/
      2  1.2   dsl 
      3  1.1  cjep /*-
      4  1.1  cjep  * Copyright (c) 1999 James Howard and Dag-Erling Codan Smrgrav
      5  1.1  cjep  * All rights reserved.
      6  1.1  cjep  *
      7  1.1  cjep  * Redistribution and use in source and binary forms, with or without
      8  1.1  cjep  * modification, are permitted provided that the following conditions
      9  1.1  cjep  * are met:
     10  1.1  cjep  * 1. Redistributions of source code must retain the above copyright
     11  1.1  cjep  *    notice, this list of conditions and the following disclaimer.
     12  1.1  cjep  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  cjep  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  cjep  *    documentation and/or other materials provided with the distribution.
     15  1.1  cjep  *
     16  1.1  cjep  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1  cjep  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1  cjep  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  cjep  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1  cjep  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  cjep  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1  cjep  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  cjep  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1  cjep  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  cjep  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  cjep  * SUCH DAMAGE.
     27  1.1  cjep  *
     28  1.1  cjep  */
     29  1.1  cjep 
     30  1.2   dsl #include <sys/cdefs.h>
     31  1.2   dsl #ifndef lint
     32  1.2   dsl __RCSID("$NetBSD: util.c,v 1.2 2004/10/30 17:37:10 dsl Exp $");
     33  1.2   dsl #endif /* not lint */
     34  1.2   dsl 
     35  1.1  cjep #include <sys/types.h>
     36  1.1  cjep #include <sys/stat.h>
     37  1.1  cjep 
     38  1.1  cjep #include <ctype.h>
     39  1.1  cjep #include <err.h>
     40  1.1  cjep #include <errno.h>
     41  1.1  cjep #include <fts.h>
     42  1.1  cjep #include <regex.h>
     43  1.1  cjep #include <stdio.h>
     44  1.1  cjep #include <stdlib.h>
     45  1.1  cjep #include <string.h>
     46  1.1  cjep #include <unistd.h>
     47  1.1  cjep #include <zlib.h>
     48  1.1  cjep 
     49  1.1  cjep #include "grep.h"
     50  1.1  cjep 
     51  1.1  cjep /*
     52  1.1  cjep  * Process a file line by line...
     53  1.1  cjep  */
     54  1.1  cjep 
     55  1.2   dsl static int linesqueued, newfile;
     56  1.2   dsl static int procline(str_t *l, int nottext);
     57  1.1  cjep 
     58  1.1  cjep int
     59  1.1  cjep grep_tree(char **argv)
     60  1.1  cjep {
     61  1.2   dsl 	FTS *fts;
     62  1.2   dsl 	FTSENT *p;
     63  1.2   dsl 	int c, fts_flags;
     64  1.1  cjep 
     65  1.1  cjep 	c = fts_flags = 0;
     66  1.1  cjep 
     67  1.2   dsl /* 	if (linkbehave == LINK_EXPLICIT)
     68  1.1  cjep 		fts_flags = FTS_COMFOLLOW;
     69  1.2   dsl 	if (linkbehave == LINK_SKIP)
     70  1.1  cjep 		fts_flags = FTS_PHYSICAL;
     71  1.2   dsl 	if (linkbehave == LINK_FOLLOW)
     72  1.2   dsl 		fts_flags = FTS_LOGICAL;*/
     73  1.1  cjep 
     74  1.2   dsl 	fts_flags |= FTS_NOSTAT | FTS_NOCHDIR | FTS_LOGICAL;
     75  1.1  cjep 
     76  1.2   dsl 	if ((fts = fts_open(argv, fts_flags, NULL)) == NULL)
     77  1.2   dsl 		err(2, NULL);
     78  1.1  cjep 	while ((p = fts_read(fts)) != NULL) {
     79  1.1  cjep 		switch (p->fts_info) {
     80  1.1  cjep 		case FTS_DNR:
     81  1.1  cjep 			break;
     82  1.1  cjep 		case FTS_ERR:
     83  1.2   dsl 			errx(2, "%s: %s", p->fts_path, strerror(p->fts_errno));
     84  1.1  cjep 			break;
     85  1.1  cjep 		case FTS_DP:
     86  1.2   dsl 		case FTS_D:
     87  1.2   dsl 			break;
     88  1.2   dsl 		case FTS_DC:
     89  1.2   dsl 			warnx("warning: %s: recursive directory loop\n",
     90  1.2   dsl 				p->fts_path);
     91  1.1  cjep 			break;
     92  1.1  cjep 		default:
     93  1.1  cjep 			c += procfile(p->fts_path);
     94  1.1  cjep 			break;
     95  1.1  cjep 		}
     96  1.1  cjep 	}
     97  1.1  cjep 
     98  1.1  cjep 	return c;
     99  1.1  cjep }
    100  1.1  cjep 
    101  1.1  cjep int
    102  1.1  cjep procfile(char *fn)
    103  1.1  cjep {
    104  1.1  cjep 	str_t ln;
    105  1.1  cjep 	file_t *f;
    106  1.2   dsl 	struct stat sb;
    107  1.2   dsl 	mode_t s;
    108  1.2   dsl 	int c, t, z, nottext, skip;
    109  1.2   dsl 
    110  1.2   dsl 	tail = 0;
    111  1.2   dsl 	newfile = 1;
    112  1.1  cjep 
    113  1.1  cjep 	if (fn == NULL) {
    114  1.2   dsl 		fn = stdin_label;
    115  1.1  cjep 		f = grep_fdopen(STDIN_FILENO, "r");
    116  1.1  cjep 	} else {
    117  1.2   dsl 		skip = 1;
    118  1.2   dsl 		if (dirbehave == GREP_SKIP || devbehave == GREP_SKIP) {
    119  1.2   dsl 			if (stat(fn, &sb)) {
    120  1.2   dsl 				fprintf(stderr, "Cannot stat %s %d\n",
    121  1.2   dsl 					fn, errno);
    122  1.2   dsl 				/* XXX record error variable */
    123  1.2   dsl 			} else {
    124  1.2   dsl 				s = sb.st_mode & S_IFMT;
    125  1.2   dsl 				if (s == S_IFDIR && dirbehave == GREP_SKIP)
    126  1.2   dsl 					skip = 0;
    127  1.2   dsl 				if (   (s == S_IFIFO || s == S_IFCHR ||
    128  1.2   dsl 					s == S_IFBLK || s == S_IFSOCK)
    129  1.2   dsl 					&& devbehave == GREP_SKIP)
    130  1.2   dsl 							skip = 0;
    131  1.2   dsl 			}
    132  1.2   dsl 		}
    133  1.2   dsl 		if (skip == 0)
    134  1.2   dsl 			return 0;
    135  1.2   dsl 
    136  1.1  cjep 		f = grep_open(fn, "r");
    137  1.1  cjep 	}
    138  1.1  cjep 	if (f == NULL) {
    139  1.1  cjep 		if (!sflag)
    140  1.1  cjep 			warn("%s", fn);
    141  1.1  cjep 		return 0;
    142  1.1  cjep 	}
    143  1.2   dsl 
    144  1.2   dsl 	nottext = grep_bin_file(f);
    145  1.2   dsl 
    146  1.2   dsl 	if (nottext && binbehave == BIN_FILE_SKIP) {
    147  1.2   dsl 		/* Skip this file as it is binary */
    148  1.1  cjep 		grep_close(f);
    149  1.1  cjep 		return 0;
    150  1.1  cjep 	}
    151  1.1  cjep 
    152  1.1  cjep 	ln.file = fn;
    153  1.1  cjep 	ln.line_no = 0;
    154  1.1  cjep 	linesqueued = 0;
    155  1.1  cjep 	ln.off = -1;
    156  1.1  cjep 
    157  1.1  cjep 	if (Bflag > 0)
    158  1.1  cjep 		initqueue();
    159  1.1  cjep 	for (c = 0; !(lflag && c);) {
    160  1.1  cjep 		ln.off += ln.len + 1;
    161  1.1  cjep 		if ((ln.dat = grep_fgetln(f, &ln.len)) == NULL)
    162  1.1  cjep 			break;
    163  1.2   dsl 		if (ln.len > 0 && ln.dat[ln.len - 1] == line_endchar)
    164  1.1  cjep 			--ln.len;
    165  1.1  cjep 		ln.line_no++;
    166  1.1  cjep 
    167  1.1  cjep 		z = tail;
    168  1.1  cjep 
    169  1.2   dsl 		if ((t = procline(&ln, nottext)) == 0 && Bflag > 0 && z == 0) {
    170  1.1  cjep 			enqueue(&ln);
    171  1.1  cjep 			linesqueued++;
    172  1.1  cjep 		}
    173  1.1  cjep 		c += t;
    174  1.2   dsl 
    175  1.2   dsl 		/* If we have a maximum number of matches, stop processing */
    176  1.2   dsl 		if (mflag && c >= maxcount)
    177  1.2   dsl 			break;
    178  1.1  cjep 	}
    179  1.1  cjep 	if (Bflag > 0)
    180  1.1  cjep 		clearqueue();
    181  1.1  cjep 	grep_close(f);
    182  1.1  cjep 
    183  1.1  cjep 	if (cflag) {
    184  1.2   dsl 		if (output_filenames)
    185  1.2   dsl 			printf("%s%c", ln.file, fn_colonchar);
    186  1.1  cjep 		printf("%u\n", c);
    187  1.2   dsl 	}
    188  1.2   dsl 
    189  1.1  cjep 	if (lflag && c != 0)
    190  1.2   dsl 		printf("%s%c", fn, fn_endchar);
    191  1.1  cjep 	if (Lflag && c == 0)
    192  1.2   dsl 		printf("%s%c", fn, fn_endchar);
    193  1.2   dsl 	if (c && !cflag && !lflag && !Lflag &&
    194  1.2   dsl 		binbehave == BIN_FILE_BIN && nottext && !qflag)
    195  1.2   dsl 			printf("Binary file %s matches\n", fn);
    196  1.2   dsl 
    197  1.1  cjep 	return c;
    198  1.1  cjep }
    199  1.1  cjep 
    200  1.1  cjep 
    201  1.1  cjep /*
    202  1.1  cjep  * Process an individual line in a file. Return non-zero if it matches.
    203  1.1  cjep  */
    204  1.1  cjep 
    205  1.2   dsl #define isword(x) (isalnum((unsigned char)(x)) || (x) == '_')
    206  1.1  cjep 
    207  1.1  cjep static int
    208  1.2   dsl procline(str_t *l, int nottext)
    209  1.1  cjep {
    210  1.2   dsl 	regmatch_t pmatch;
    211  1.2   dsl 	regmatch_t matches[MAX_LINE_MATCHES];
    212  1.2   dsl 	int c = 0, i, r, t, m = 0;
    213  1.2   dsl 	regoff_t st = 0;
    214  1.1  cjep 
    215  1.1  cjep 	if (matchall) {
    216  1.1  cjep 		c = !vflag;
    217  1.1  cjep 		goto print;
    218  1.1  cjep 	}
    219  1.1  cjep 
    220  1.1  cjep 	t = vflag ? REG_NOMATCH : 0;
    221  1.2   dsl 
    222  1.2   dsl 	while (st <= l->len) {
    223  1.2   dsl 		pmatch.rm_so = st;
    224  1.2   dsl 		pmatch.rm_eo = l->len;
    225  1.2   dsl 		for (i = 0; i < patterns; i++) {
    226  1.2   dsl 			r = regexec(&r_pattern[i], l->dat, 1, &pmatch, eflags);
    227  1.2   dsl 			if (r == REG_NOMATCH && t == 0)
    228  1.2   dsl 				continue;
    229  1.2   dsl 			if (r == 0) {
    230  1.2   dsl 				if (wflag) {
    231  1.2   dsl 					if ((pmatch.rm_so != 0 && isword(l->dat[pmatch.rm_so - 1]))
    232  1.2   dsl 					    || (pmatch.rm_eo != l->len && isword(l->dat[pmatch.rm_eo])))
    233  1.2   dsl 						r = REG_NOMATCH;
    234  1.2   dsl 				}
    235  1.2   dsl 				if (xflag) {
    236  1.2   dsl 					if (pmatch.rm_so != 0 || pmatch.rm_eo != l->len)
    237  1.2   dsl 						r = REG_NOMATCH;
    238  1.2   dsl 				}
    239  1.1  cjep 			}
    240  1.2   dsl 			if (r == t) {
    241  1.2   dsl 				if (m == 0)
    242  1.2   dsl 					c++;
    243  1.2   dsl 				if (m < MAX_LINE_MATCHES) {
    244  1.2   dsl 					matches[m] = pmatch;
    245  1.2   dsl 					m++;
    246  1.2   dsl 				}
    247  1.2   dsl 				st = pmatch.rm_eo;
    248  1.2   dsl 				break;
    249  1.1  cjep 			}
    250  1.1  cjep 		}
    251  1.2   dsl 
    252  1.2   dsl 		/* One pass if we are not recording matches */
    253  1.2   dsl 		if (!oflag && !colours)
    254  1.1  cjep 			break;
    255  1.2   dsl 
    256  1.2   dsl 		if (st == pmatch.rm_so)
    257  1.2   dsl 			break; 	/* No matches */
    258  1.2   dsl 
    259  1.1  cjep 	}
    260  1.1  cjep 
    261  1.1  cjep print:
    262  1.2   dsl 
    263  1.2   dsl 	if (c && binbehave == BIN_FILE_BIN && nottext)
    264  1.2   dsl 		return c;	/* Binary file */
    265  1.2   dsl 
    266  1.1  cjep 	if ((tail > 0 || c) && !cflag && !qflag) {
    267  1.1  cjep 		if (c) {
    268  1.2   dsl 
    269  1.2   dsl 			if ( (Aflag || Bflag) && first > 0 &&
    270  1.2   dsl 			   ( (Bflag <= linesqueued && tail == 0) || newfile) )
    271  1.2   dsl 						printf("--\n");
    272  1.2   dsl 
    273  1.1  cjep 			first = 1;
    274  1.2   dsl 			newfile = 0;
    275  1.1  cjep 			tail = Aflag;
    276  1.1  cjep 			if (Bflag > 0)
    277  1.1  cjep 				printqueue();
    278  1.1  cjep 			linesqueued = 0;
    279  1.2   dsl 			printline(l, fn_colonchar, matches, m);
    280  1.1  cjep 		} else {
    281  1.2   dsl 			printline(l, fn_dashchar, matches, m);
    282  1.1  cjep 			tail--;
    283  1.1  cjep 		}
    284  1.2   dsl 
    285  1.1  cjep 	}
    286  1.1  cjep 	return c;
    287  1.1  cjep }
    288  1.1  cjep 
    289  1.1  cjep void *
    290  1.1  cjep grep_malloc(size_t size)
    291  1.1  cjep {
    292  1.2   dsl 	void *ptr;
    293  1.1  cjep 
    294  1.1  cjep 	if ((ptr = malloc(size)) == NULL)
    295  1.2   dsl 		err(2, "malloc");
    296  1.1  cjep 	return ptr;
    297  1.1  cjep }
    298  1.1  cjep 
    299  1.1  cjep void *
    300  1.1  cjep grep_realloc(void *ptr, size_t size)
    301  1.1  cjep {
    302  1.1  cjep 	if ((ptr = realloc(ptr, size)) == NULL)
    303  1.2   dsl 		err(2, "realloc");
    304  1.1  cjep 	return ptr;
    305  1.1  cjep }
    306  1.1  cjep 
    307  1.1  cjep void
    308  1.2   dsl printline(str_t *line, int sep, regmatch_t *matches, int m)
    309  1.1  cjep {
    310  1.2   dsl 	int i, n = 0;
    311  1.2   dsl 	size_t a = 0;
    312  1.2   dsl 
    313  1.2   dsl 	if (output_filenames) {
    314  1.1  cjep 		fputs(line->file, stdout);
    315  1.1  cjep 		++n;
    316  1.1  cjep 	}
    317  1.1  cjep 	if (nflag) {
    318  1.1  cjep 		if (n)
    319  1.1  cjep 			putchar(sep);
    320  1.1  cjep 		printf("%d", line->line_no);
    321  1.1  cjep 		++n;
    322  1.1  cjep 	}
    323  1.1  cjep 	if (bflag) {
    324  1.1  cjep 		if (n)
    325  1.1  cjep 			putchar(sep);
    326  1.1  cjep 		printf("%lu", (unsigned long)line->off);
    327  1.1  cjep 	}
    328  1.1  cjep 	if (n)
    329  1.1  cjep 		putchar(sep);
    330  1.2   dsl 
    331  1.2   dsl 	if ((oflag || colours) && m > 0) {
    332  1.2   dsl 
    333  1.2   dsl 		for (i = 0; i < m; i++) {
    334  1.2   dsl 
    335  1.2   dsl 			if (!oflag)
    336  1.2   dsl 				fwrite(line->dat + a, matches[i].rm_so - a, 1, stdout);
    337  1.2   dsl 
    338  1.2   dsl 			if (colours)
    339  1.2   dsl 				fprintf(stdout, "\33[%sm", grep_colour);
    340  1.2   dsl 			fwrite(line->dat + matches[i].rm_so,
    341  1.2   dsl 				matches[i].rm_eo - matches[i].rm_so, 1, stdout);
    342  1.2   dsl 
    343  1.2   dsl 			if (colours)
    344  1.2   dsl 				fprintf(stdout, "\33[00m");
    345  1.2   dsl 			a = matches[i].rm_eo;
    346  1.2   dsl 			if (oflag)
    347  1.2   dsl 				putchar('\n');
    348  1.2   dsl 		}
    349  1.2   dsl 		if (!oflag) {
    350  1.2   dsl 			if (line->len - a > 0)
    351  1.2   dsl 				fwrite(line->dat + a, line->len - a, 1, stdout);
    352  1.2   dsl 			putchar('\n');
    353  1.2   dsl 		}
    354  1.2   dsl 
    355  1.2   dsl 
    356  1.2   dsl 	} else {
    357  1.2   dsl 		fwrite(line->dat, line->len, 1, stdout);
    358  1.2   dsl 		putchar(line_endchar);
    359  1.2   dsl 	}
    360  1.2   dsl 
    361  1.1  cjep }
    362