Home | History | Annotate | Line # | Download | only in grep
util.c revision 1.6.4.2
      1  1.6.4.2  bouyer /*	$NetBSD: util.c,v 1.6.4.2 2011/03/05 15:10:59 bouyer Exp $	*/
      2  1.6.4.1  bouyer /*	$FreeBSD: head/usr.bin/grep/util.c 211496 2010-08-19 09:28:59Z des $	*/
      3  1.6.4.1  bouyer /*	$OpenBSD: util.c,v 1.39 2010/07/02 22:18:03 tedu Exp $	*/
      4      1.2     dsl 
      5      1.1    cjep /*-
      6  1.6.4.1  bouyer  * Copyright (c) 1999 James Howard and Dag-Erling Codan Smrgrav
      7  1.6.4.1  bouyer  * Copyright (C) 2008-2010 Gabor Kovesdan <gabor (at) FreeBSD.org>
      8      1.1    cjep  * All rights reserved.
      9      1.1    cjep  *
     10      1.1    cjep  * Redistribution and use in source and binary forms, with or without
     11      1.1    cjep  * modification, are permitted provided that the following conditions
     12      1.1    cjep  * are met:
     13      1.1    cjep  * 1. Redistributions of source code must retain the above copyright
     14      1.1    cjep  *    notice, this list of conditions and the following disclaimer.
     15      1.1    cjep  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1    cjep  *    notice, this list of conditions and the following disclaimer in the
     17      1.1    cjep  *    documentation and/or other materials provided with the distribution.
     18      1.1    cjep  *
     19      1.1    cjep  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     20      1.1    cjep  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21      1.1    cjep  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22      1.1    cjep  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     23      1.1    cjep  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24      1.1    cjep  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25      1.1    cjep  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26      1.1    cjep  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27      1.1    cjep  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28      1.1    cjep  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29      1.1    cjep  * SUCH DAMAGE.
     30      1.1    cjep  */
     31      1.1    cjep 
     32      1.2     dsl #include <sys/cdefs.h>
     33  1.6.4.2  bouyer __RCSID("$NetBSD: util.c,v 1.6.4.2 2011/03/05 15:10:59 bouyer Exp $");
     34      1.2     dsl 
     35      1.1    cjep #include <sys/stat.h>
     36  1.6.4.1  bouyer #include <sys/types.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.6.4.1  bouyer #include <fnmatch.h>
     42      1.1    cjep #include <fts.h>
     43  1.6.4.1  bouyer #include <libgen.h>
     44  1.6.4.1  bouyer #include <stdbool.h>
     45      1.1    cjep #include <stdio.h>
     46      1.1    cjep #include <stdlib.h>
     47      1.1    cjep #include <string.h>
     48      1.1    cjep #include <unistd.h>
     49  1.6.4.1  bouyer #include <wchar.h>
     50  1.6.4.1  bouyer #include <wctype.h>
     51      1.1    cjep 
     52      1.1    cjep #include "grep.h"
     53      1.1    cjep 
     54  1.6.4.1  bouyer static int	 linesqueued;
     55  1.6.4.1  bouyer static int	 procline(struct str *l, int);
     56  1.6.4.1  bouyer 
     57  1.6.4.1  bouyer bool
     58  1.6.4.1  bouyer file_matching(const char *fname)
     59  1.6.4.1  bouyer {
     60  1.6.4.1  bouyer 	char *fname_base, *fname_copy;
     61  1.6.4.1  bouyer 	bool ret;
     62  1.6.4.1  bouyer 
     63  1.6.4.1  bouyer 	ret = finclude ? false : true;
     64  1.6.4.1  bouyer 	fname_copy = grep_strdup(fname);
     65  1.6.4.1  bouyer 	fname_base = basename(fname_copy);
     66  1.6.4.1  bouyer 
     67  1.6.4.1  bouyer 	for (unsigned int i = 0; i < fpatterns; ++i) {
     68  1.6.4.1  bouyer 		if (fnmatch(fpattern[i].pat, fname, 0) == 0 ||
     69  1.6.4.1  bouyer 		    fnmatch(fpattern[i].pat, fname_base, 0) == 0) {
     70  1.6.4.1  bouyer 			if (fpattern[i].mode == EXCL_PAT)
     71  1.6.4.1  bouyer 				return (false);
     72  1.6.4.1  bouyer 			else
     73  1.6.4.1  bouyer 				ret = true;
     74  1.6.4.1  bouyer 		}
     75  1.6.4.1  bouyer 	}
     76  1.6.4.1  bouyer 	free(fname_copy);
     77  1.6.4.1  bouyer 	return (ret);
     78  1.6.4.1  bouyer }
     79  1.6.4.1  bouyer 
     80  1.6.4.1  bouyer static inline bool
     81  1.6.4.1  bouyer dir_matching(const char *dname)
     82  1.6.4.1  bouyer {
     83  1.6.4.1  bouyer 	bool ret;
     84      1.1    cjep 
     85  1.6.4.1  bouyer 	ret = dinclude ? false : true;
     86      1.1    cjep 
     87  1.6.4.1  bouyer 	for (unsigned int i = 0; i < dpatterns; ++i) {
     88  1.6.4.1  bouyer 		if (dname != NULL &&
     89  1.6.4.1  bouyer 		    fnmatch(dname, dpattern[i].pat, 0) == 0) {
     90  1.6.4.1  bouyer 			if (dpattern[i].mode == EXCL_PAT)
     91  1.6.4.1  bouyer 				return (false);
     92  1.6.4.1  bouyer 			else
     93  1.6.4.1  bouyer 				ret = true;
     94  1.6.4.1  bouyer 		}
     95  1.6.4.1  bouyer 	}
     96  1.6.4.1  bouyer 	return (ret);
     97  1.6.4.1  bouyer }
     98  1.6.4.1  bouyer 
     99  1.6.4.1  bouyer /*
    100  1.6.4.1  bouyer  * Processes a directory when a recursive search is performed with
    101  1.6.4.1  bouyer  * the -R option.  Each appropriate file is passed to procfile().
    102  1.6.4.1  bouyer  */
    103  1.6.4.1  bouyer int
    104      1.1    cjep grep_tree(char **argv)
    105      1.1    cjep {
    106      1.2     dsl 	FTS *fts;
    107      1.2     dsl 	FTSENT *p;
    108  1.6.4.1  bouyer 	char *d, *dir = NULL;
    109      1.2     dsl 	int c, fts_flags;
    110  1.6.4.1  bouyer 	bool ok;
    111      1.1    cjep 
    112      1.1    cjep 	c = fts_flags = 0;
    113      1.1    cjep 
    114  1.6.4.1  bouyer 	switch(linkbehave) {
    115  1.6.4.1  bouyer 	case LINK_EXPLICIT:
    116      1.1    cjep 		fts_flags = FTS_COMFOLLOW;
    117  1.6.4.1  bouyer 		break;
    118  1.6.4.1  bouyer 	case LINK_SKIP:
    119      1.1    cjep 		fts_flags = FTS_PHYSICAL;
    120  1.6.4.1  bouyer 		break;
    121  1.6.4.1  bouyer 	default:
    122  1.6.4.1  bouyer 		fts_flags = FTS_LOGICAL;
    123  1.6.4.1  bouyer 
    124  1.6.4.1  bouyer 	}
    125      1.1    cjep 
    126  1.6.4.1  bouyer 	fts_flags |= FTS_NOSTAT | FTS_NOCHDIR;
    127      1.1    cjep 
    128  1.6.4.1  bouyer 	if (!(fts = fts_open(argv, fts_flags, NULL)))
    129  1.6.4.1  bouyer 		err(2, "fts_open");
    130      1.1    cjep 	while ((p = fts_read(fts)) != NULL) {
    131      1.1    cjep 		switch (p->fts_info) {
    132      1.1    cjep 		case FTS_DNR:
    133  1.6.4.1  bouyer 			/* FALLTHROUGH */
    134      1.1    cjep 		case FTS_ERR:
    135      1.2     dsl 			errx(2, "%s: %s", p->fts_path, strerror(p->fts_errno));
    136      1.1    cjep 			break;
    137      1.2     dsl 		case FTS_D:
    138  1.6.4.1  bouyer 			/* FALLTHROUGH */
    139  1.6.4.1  bouyer 		case FTS_DP:
    140      1.2     dsl 			break;
    141      1.2     dsl 		case FTS_DC:
    142  1.6.4.1  bouyer 			/* Print a warning for recursive directory loop */
    143      1.3     wiz 			warnx("warning: %s: recursive directory loop",
    144      1.2     dsl 				p->fts_path);
    145      1.1    cjep 			break;
    146      1.1    cjep 		default:
    147  1.6.4.1  bouyer 			/* Check for file exclusion/inclusion */
    148  1.6.4.1  bouyer 			ok = true;
    149  1.6.4.1  bouyer 			if (dexclude || dinclude) {
    150  1.6.4.1  bouyer 				if ((d = strrchr(p->fts_path, '/')) != NULL) {
    151  1.6.4.1  bouyer 					dir = grep_malloc(sizeof(char) *
    152  1.6.4.1  bouyer 					    (d - p->fts_path + 1));
    153  1.6.4.1  bouyer 					memcpy(dir, p->fts_path,
    154  1.6.4.1  bouyer 					    d - p->fts_path);
    155  1.6.4.1  bouyer 					dir[d - p->fts_path] = '\0';
    156  1.6.4.1  bouyer 				}
    157  1.6.4.1  bouyer 				ok = dir_matching(dir);
    158  1.6.4.1  bouyer 				free(dir);
    159  1.6.4.1  bouyer 				dir = NULL;
    160  1.6.4.1  bouyer 			}
    161  1.6.4.1  bouyer 			if (fexclude || finclude)
    162  1.6.4.1  bouyer 				ok &= file_matching(p->fts_path);
    163  1.6.4.1  bouyer 
    164  1.6.4.1  bouyer 			if (ok)
    165  1.6.4.1  bouyer 				c += procfile(p->fts_path);
    166      1.1    cjep 			break;
    167      1.1    cjep 		}
    168      1.1    cjep 	}
    169      1.1    cjep 
    170  1.6.4.1  bouyer 	fts_close(fts);
    171  1.6.4.1  bouyer 	return (c);
    172      1.1    cjep }
    173      1.1    cjep 
    174  1.6.4.1  bouyer /*
    175  1.6.4.1  bouyer  * Opens a file and processes it.  Each file is processed line-by-line
    176  1.6.4.1  bouyer  * passing the lines to procline().
    177  1.6.4.1  bouyer  */
    178      1.1    cjep int
    179      1.4  rillig procfile(const char *fn)
    180      1.1    cjep {
    181  1.6.4.1  bouyer 	struct file *f;
    182      1.2     dsl 	struct stat sb;
    183  1.6.4.1  bouyer 	struct str ln;
    184      1.2     dsl 	mode_t s;
    185  1.6.4.1  bouyer 	int c, t;
    186  1.6.4.1  bouyer 
    187  1.6.4.1  bouyer 	if (mflag && (mcount <= 0))
    188  1.6.4.1  bouyer 		return (0);
    189      1.1    cjep 
    190  1.6.4.1  bouyer 	if (strcmp(fn, "-") == 0) {
    191  1.6.4.1  bouyer 		fn = label != NULL ? label : getstr(1);
    192  1.6.4.1  bouyer 		f = grep_open(NULL);
    193      1.1    cjep 	} else {
    194  1.6.4.1  bouyer 		if (!stat(fn, &sb)) {
    195  1.6.4.1  bouyer 			/* Check if we need to process the file */
    196  1.6.4.1  bouyer 			s = sb.st_mode & S_IFMT;
    197  1.6.4.1  bouyer 			if (s == S_IFDIR && dirbehave == DIR_SKIP)
    198  1.6.4.1  bouyer 				return (0);
    199  1.6.4.1  bouyer 			if ((s == S_IFIFO || s == S_IFCHR || s == S_IFBLK
    200  1.6.4.1  bouyer 				|| s == S_IFSOCK) && devbehave == DEV_SKIP)
    201  1.6.4.1  bouyer 					return (0);
    202      1.2     dsl 		}
    203  1.6.4.1  bouyer 		f = grep_open(fn);
    204      1.1    cjep 	}
    205      1.1    cjep 	if (f == NULL) {
    206      1.1    cjep 		if (!sflag)
    207      1.1    cjep 			warn("%s", fn);
    208  1.6.4.1  bouyer 		if (errno == ENOENT)
    209  1.6.4.1  bouyer 			notfound = true;
    210  1.6.4.1  bouyer 		return (0);
    211      1.1    cjep 	}
    212      1.2     dsl 
    213  1.6.4.1  bouyer 	ln.file = grep_malloc(strlen(fn) + 1);
    214  1.6.4.1  bouyer 	strcpy(ln.file, fn);
    215      1.1    cjep 	ln.line_no = 0;
    216  1.6.4.1  bouyer 	ln.len = 0;
    217      1.1    cjep 	linesqueued = 0;
    218  1.6.4.1  bouyer 	tail = 0;
    219      1.1    cjep 	ln.off = -1;
    220      1.1    cjep 
    221  1.6.4.1  bouyer 	for (c = 0;  c == 0 || !(lflag || qflag); ) {
    222      1.1    cjep 		ln.off += ln.len + 1;
    223  1.6.4.1  bouyer 		if ((ln.dat = grep_fgetln(f, &ln.len)) == NULL || ln.len == 0) {
    224  1.6.4.1  bouyer 			if (ln.line_no == 0 && matchall)
    225  1.6.4.1  bouyer 				exit(0);
    226  1.6.4.1  bouyer 			else
    227  1.6.4.1  bouyer 				break;
    228  1.6.4.1  bouyer 		}
    229  1.6.4.1  bouyer 		if (ln.len > 0 && ln.dat[ln.len - 1] == '\n')
    230      1.1    cjep 			--ln.len;
    231      1.1    cjep 		ln.line_no++;
    232      1.1    cjep 
    233  1.6.4.1  bouyer 		/* Return if we need to skip a binary file */
    234  1.6.4.1  bouyer 		if (f->binary && binbehave == BINFILE_SKIP) {
    235  1.6.4.1  bouyer 			grep_close(f);
    236  1.6.4.1  bouyer 			free(ln.file);
    237  1.6.4.1  bouyer 			free(f);
    238  1.6.4.1  bouyer 			return (0);
    239  1.6.4.1  bouyer 		}
    240  1.6.4.1  bouyer 		/* Process the file line-by-line */
    241  1.6.4.1  bouyer 		if ((t = procline(&ln, f->binary)) == 0 && Bflag > 0) {
    242      1.1    cjep 			enqueue(&ln);
    243      1.1    cjep 			linesqueued++;
    244      1.1    cjep 		}
    245      1.1    cjep 		c += t;
    246  1.6.4.1  bouyer 
    247  1.6.4.1  bouyer 		/* Count the matches if we have a match limit */
    248  1.6.4.1  bouyer 		if (mflag) {
    249  1.6.4.1  bouyer 			mcount -= t;
    250  1.6.4.1  bouyer 			if (mcount <= 0)
    251  1.6.4.1  bouyer 				break;
    252  1.6.4.1  bouyer 		}
    253      1.1    cjep 	}
    254      1.1    cjep 	if (Bflag > 0)
    255      1.1    cjep 		clearqueue();
    256      1.1    cjep 	grep_close(f);
    257      1.1    cjep 
    258      1.1    cjep 	if (cflag) {
    259  1.6.4.1  bouyer 		if (!hflag)
    260  1.6.4.1  bouyer 			printf("%s:", ln.file);
    261      1.1    cjep 		printf("%u\n", c);
    262  1.6.4.1  bouyer 	}
    263  1.6.4.1  bouyer 	if (lflag && !qflag && c != 0)
    264  1.6.4.1  bouyer 		printf("%s\n", fn);
    265  1.6.4.1  bouyer 	if (Lflag && !qflag && c == 0)
    266  1.6.4.1  bouyer 		printf("%s\n", fn);
    267  1.6.4.1  bouyer 	if (c && !cflag && !lflag && !Lflag &&
    268  1.6.4.1  bouyer 	    binbehave == BINFILE_BIN && f->binary && !qflag)
    269  1.6.4.1  bouyer 		printf(getstr(8), fn);
    270  1.6.4.1  bouyer 
    271  1.6.4.1  bouyer 	free(ln.file);
    272  1.6.4.1  bouyer 	free(f);
    273  1.6.4.1  bouyer 	return (c);
    274      1.1    cjep }
    275      1.1    cjep 
    276  1.6.4.1  bouyer #define iswword(x)	(iswalnum((x)) || (x) == L'_')
    277      1.1    cjep 
    278      1.1    cjep /*
    279  1.6.4.1  bouyer  * Processes a line comparing it with the specified patterns.  Each pattern
    280  1.6.4.1  bouyer  * is looped to be compared along with the full string, saving each and every
    281  1.6.4.1  bouyer  * match, which is necessary to colorize the output and to count the
    282  1.6.4.1  bouyer  * matches.  The matching lines are passed to printline() to display the
    283  1.6.4.1  bouyer  * appropriate output.
    284      1.1    cjep  */
    285      1.1    cjep static int
    286  1.6.4.1  bouyer procline(struct str *l, int nottext)
    287      1.1    cjep {
    288      1.2     dsl 	regmatch_t matches[MAX_LINE_MATCHES];
    289  1.6.4.1  bouyer 	regmatch_t pmatch;
    290  1.6.4.1  bouyer 	size_t st = 0;
    291  1.6.4.1  bouyer 	unsigned int i;
    292  1.6.4.1  bouyer 	int c = 0, m = 0, r = 0;
    293  1.6.4.1  bouyer 
    294  1.6.4.1  bouyer 	if (!matchall) {
    295  1.6.4.1  bouyer 		/* Loop to process the whole line */
    296  1.6.4.1  bouyer 		while (st <= l->len) {
    297  1.6.4.1  bouyer 			pmatch.rm_so = st;
    298  1.6.4.1  bouyer 			pmatch.rm_eo = l->len;
    299      1.2     dsl 
    300  1.6.4.1  bouyer 			/* Loop to compare with all the patterns */
    301  1.6.4.1  bouyer 			for (i = 0; i < patterns; i++) {
    302  1.6.4.1  bouyer /*
    303  1.6.4.1  bouyer  * XXX: grep_search() is a workaround for speed up and should be
    304  1.6.4.1  bouyer  * removed in the future.  See fastgrep.c.
    305  1.6.4.1  bouyer  */
    306  1.6.4.1  bouyer 				if (fg_pattern[i].pattern) {
    307  1.6.4.1  bouyer 					r = grep_search(&fg_pattern[i],
    308  1.6.4.1  bouyer 					    (unsigned char *)l->dat,
    309  1.6.4.1  bouyer 					    l->len, &pmatch);
    310  1.6.4.1  bouyer 					r = (r == 0) ? 0 : REG_NOMATCH;
    311  1.6.4.1  bouyer 					st = pmatch.rm_eo;
    312  1.6.4.1  bouyer 				} else {
    313  1.6.4.1  bouyer 					r = regexec(&r_pattern[i], l->dat, 1,
    314  1.6.4.1  bouyer 					    &pmatch, eflags);
    315  1.6.4.1  bouyer 					r = (r == 0) ? 0 : REG_NOMATCH;
    316  1.6.4.1  bouyer 					st = pmatch.rm_eo;
    317      1.2     dsl 				}
    318  1.6.4.1  bouyer 				if (r == REG_NOMATCH)
    319  1.6.4.1  bouyer 					continue;
    320  1.6.4.1  bouyer 				/* Check for full match */
    321  1.6.4.1  bouyer 				if (r == 0 && xflag)
    322  1.6.4.1  bouyer 					if (pmatch.rm_so != 0 ||
    323  1.6.4.1  bouyer 					    (size_t)pmatch.rm_eo != l->len)
    324  1.6.4.1  bouyer 						r = REG_NOMATCH;
    325  1.6.4.1  bouyer 				/* Check for whole word match */
    326  1.6.4.2  bouyer 				if (r == 0 && fg_pattern[i].word &&
    327  1.6.4.2  bouyer 				    pmatch.rm_so != 0) {
    328  1.6.4.1  bouyer 					wint_t wbegin, wend;
    329  1.6.4.1  bouyer 
    330  1.6.4.1  bouyer 					wbegin = wend = L' ';
    331  1.6.4.1  bouyer 					if (pmatch.rm_so != 0 &&
    332  1.6.4.1  bouyer 					    sscanf(&l->dat[pmatch.rm_so - 1],
    333  1.6.4.1  bouyer 					    "%lc", &wbegin) != 1)
    334  1.6.4.1  bouyer 						r = REG_NOMATCH;
    335  1.6.4.1  bouyer 					else if ((size_t)pmatch.rm_eo != l->len &&
    336  1.6.4.1  bouyer 					    sscanf(&l->dat[pmatch.rm_eo],
    337  1.6.4.1  bouyer 					    "%lc", &wend) != 1)
    338  1.6.4.1  bouyer 						r = REG_NOMATCH;
    339  1.6.4.1  bouyer 					else if (iswword(wbegin) || iswword(wend))
    340      1.2     dsl 						r = REG_NOMATCH;
    341      1.2     dsl 				}
    342  1.6.4.1  bouyer 				if (r == 0) {
    343  1.6.4.1  bouyer 					if (m == 0)
    344  1.6.4.1  bouyer 						c++;
    345  1.6.4.1  bouyer 					if (m < MAX_LINE_MATCHES)
    346  1.6.4.1  bouyer 						matches[m++] = pmatch;
    347  1.6.4.1  bouyer 					/* matches - skip further patterns */
    348  1.6.4.1  bouyer 					if ((color != NULL && !oflag) || qflag || lflag)
    349  1.6.4.1  bouyer 						break;
    350      1.2     dsl 				}
    351  1.6.4.1  bouyer 			}
    352  1.6.4.1  bouyer 
    353  1.6.4.1  bouyer 			if (vflag) {
    354  1.6.4.1  bouyer 				c = !c;
    355      1.2     dsl 				break;
    356      1.1    cjep 			}
    357  1.6.4.1  bouyer 			/* One pass if we are not recording matches */
    358  1.6.4.1  bouyer 			if ((color != NULL && !oflag) || qflag || lflag)
    359  1.6.4.1  bouyer 				break;
    360  1.6.4.1  bouyer 
    361  1.6.4.1  bouyer 			if (st == (size_t)pmatch.rm_so)
    362  1.6.4.1  bouyer 				break; 	/* No matches */
    363      1.1    cjep 		}
    364  1.6.4.1  bouyer 	} else
    365  1.6.4.1  bouyer 		c = !vflag;
    366      1.2     dsl 
    367  1.6.4.1  bouyer 	if (c && binbehave == BINFILE_BIN && nottext)
    368  1.6.4.1  bouyer 		return (c); /* Binary file */
    369      1.2     dsl 
    370  1.6.4.1  bouyer 	/* Dealing with the context */
    371  1.6.4.1  bouyer 	if ((tail || c) && !cflag && !qflag && !lflag && !Lflag) {
    372      1.1    cjep 		if (c) {
    373  1.6.4.1  bouyer 			if (!first && !prev && !tail && Aflag)
    374  1.6.4.1  bouyer 				printf("--\n");
    375      1.1    cjep 			tail = Aflag;
    376  1.6.4.1  bouyer 			if (Bflag > 0) {
    377  1.6.4.1  bouyer 				if (!first && !prev)
    378  1.6.4.1  bouyer 					printf("--\n");
    379      1.1    cjep 				printqueue();
    380  1.6.4.1  bouyer 			}
    381      1.1    cjep 			linesqueued = 0;
    382  1.6.4.1  bouyer 			printline(l, ':', matches, m);
    383      1.1    cjep 		} else {
    384  1.6.4.1  bouyer 			printline(l, '-', matches, m);
    385      1.1    cjep 			tail--;
    386      1.1    cjep 		}
    387      1.1    cjep 	}
    388  1.6.4.1  bouyer 
    389  1.6.4.1  bouyer 	if (c) {
    390  1.6.4.1  bouyer 		prev = true;
    391  1.6.4.1  bouyer 		first = false;
    392  1.6.4.1  bouyer 	} else
    393  1.6.4.1  bouyer 		prev = false;
    394  1.6.4.1  bouyer 
    395  1.6.4.1  bouyer 	return (c);
    396      1.1    cjep }
    397      1.1    cjep 
    398  1.6.4.1  bouyer /*
    399  1.6.4.1  bouyer  * Safe malloc() for internal use.
    400  1.6.4.1  bouyer  */
    401      1.1    cjep void *
    402      1.1    cjep grep_malloc(size_t size)
    403      1.1    cjep {
    404      1.2     dsl 	void *ptr;
    405      1.1    cjep 
    406      1.1    cjep 	if ((ptr = malloc(size)) == NULL)
    407      1.2     dsl 		err(2, "malloc");
    408  1.6.4.1  bouyer 	return (ptr);
    409      1.1    cjep }
    410      1.1    cjep 
    411  1.6.4.1  bouyer /*
    412  1.6.4.1  bouyer  * Safe calloc() for internal use.
    413  1.6.4.1  bouyer  */
    414  1.6.4.1  bouyer void *
    415  1.6.4.1  bouyer grep_calloc(size_t nmemb, size_t size)
    416  1.6.4.1  bouyer {
    417  1.6.4.1  bouyer 	void *ptr;
    418  1.6.4.1  bouyer 
    419  1.6.4.1  bouyer 	if ((ptr = calloc(nmemb, size)) == NULL)
    420  1.6.4.1  bouyer 		err(2, "calloc");
    421  1.6.4.1  bouyer 	return (ptr);
    422  1.6.4.1  bouyer }
    423  1.6.4.1  bouyer 
    424  1.6.4.1  bouyer /*
    425  1.6.4.1  bouyer  * Safe realloc() for internal use.
    426  1.6.4.1  bouyer  */
    427      1.1    cjep void *
    428      1.1    cjep grep_realloc(void *ptr, size_t size)
    429      1.1    cjep {
    430  1.6.4.1  bouyer 
    431      1.1    cjep 	if ((ptr = realloc(ptr, size)) == NULL)
    432      1.2     dsl 		err(2, "realloc");
    433  1.6.4.1  bouyer 	return (ptr);
    434  1.6.4.1  bouyer }
    435  1.6.4.1  bouyer 
    436  1.6.4.1  bouyer /*
    437  1.6.4.1  bouyer  * Safe strdup() for internal use.
    438  1.6.4.1  bouyer  */
    439  1.6.4.1  bouyer char *
    440  1.6.4.1  bouyer grep_strdup(const char *str)
    441  1.6.4.1  bouyer {
    442  1.6.4.1  bouyer 	char *ret;
    443  1.6.4.1  bouyer 
    444  1.6.4.1  bouyer 	if ((ret = strdup(str)) == NULL)
    445  1.6.4.1  bouyer 		err(2, "strdup");
    446  1.6.4.1  bouyer 	return (ret);
    447      1.1    cjep }
    448      1.1    cjep 
    449  1.6.4.1  bouyer /*
    450  1.6.4.1  bouyer  * Prints a matching line according to the command line options.
    451  1.6.4.1  bouyer  */
    452      1.1    cjep void
    453  1.6.4.1  bouyer printline(struct str *line, int sep, regmatch_t *matches, int m)
    454      1.1    cjep {
    455      1.2     dsl 	size_t a = 0;
    456  1.6.4.1  bouyer 	int i, n = 0;
    457      1.2     dsl 
    458  1.6.4.1  bouyer 	if (!hflag) {
    459  1.6.4.1  bouyer 		if (nullflag == 0)
    460  1.6.4.1  bouyer 			fputs(line->file, stdout);
    461  1.6.4.1  bouyer 		else {
    462  1.6.4.1  bouyer 			printf("%s", line->file);
    463  1.6.4.1  bouyer 			putchar(0);
    464  1.6.4.1  bouyer 		}
    465      1.1    cjep 		++n;
    466      1.1    cjep 	}
    467      1.1    cjep 	if (nflag) {
    468  1.6.4.1  bouyer 		if (n > 0)
    469      1.1    cjep 			putchar(sep);
    470      1.1    cjep 		printf("%d", line->line_no);
    471      1.1    cjep 		++n;
    472      1.1    cjep 	}
    473      1.1    cjep 	if (bflag) {
    474  1.6.4.1  bouyer 		if (n > 0)
    475      1.1    cjep 			putchar(sep);
    476  1.6.4.1  bouyer 		printf("%lld", (long long)line->off);
    477  1.6.4.1  bouyer 		++n;
    478      1.1    cjep 	}
    479      1.1    cjep 	if (n)
    480      1.1    cjep 		putchar(sep);
    481  1.6.4.1  bouyer 	/* --color and -o */
    482  1.6.4.1  bouyer 	if ((oflag || color) && m > 0) {
    483      1.2     dsl 		for (i = 0; i < m; i++) {
    484      1.2     dsl 			if (!oflag)
    485  1.6.4.1  bouyer 				fwrite(line->dat + a, matches[i].rm_so - a, 1,
    486  1.6.4.1  bouyer 				    stdout);
    487  1.6.4.1  bouyer 			if (color)
    488  1.6.4.1  bouyer 				fprintf(stdout, "\33[%sm\33[K", color);
    489  1.6.4.1  bouyer 
    490  1.6.4.1  bouyer 				fwrite(line->dat + matches[i].rm_so,
    491  1.6.4.1  bouyer 				    matches[i].rm_eo - matches[i].rm_so, 1,
    492  1.6.4.1  bouyer 				    stdout);
    493  1.6.4.1  bouyer 			if (color)
    494  1.6.4.1  bouyer 				fprintf(stdout, "\33[m\33[K");
    495      1.2     dsl 			a = matches[i].rm_eo;
    496      1.2     dsl 			if (oflag)
    497      1.2     dsl 				putchar('\n');
    498      1.2     dsl 		}
    499      1.2     dsl 		if (!oflag) {
    500      1.2     dsl 			if (line->len - a > 0)
    501      1.2     dsl 				fwrite(line->dat + a, line->len - a, 1, stdout);
    502      1.2     dsl 			putchar('\n');
    503      1.2     dsl 		}
    504      1.2     dsl 	} else {
    505      1.2     dsl 		fwrite(line->dat, line->len, 1, stdout);
    506  1.6.4.1  bouyer 		putchar('\n');
    507      1.2     dsl 	}
    508      1.1    cjep }
    509