xref: /netbsd-src/usr.bin/grep/util.c (revision 4695f70791fc58b040bfeef848d9f2a612c33b9f)
1*4695f707Smrg /*	$NetBSD: util.c,v 1.19 2018/02/05 22:14:26 mrg Exp $	*/
20a117266Sjoerg /*	$FreeBSD: head/usr.bin/grep/util.c 211496 2010-08-19 09:28:59Z des $	*/
30a117266Sjoerg /*	$OpenBSD: util.c,v 1.39 2010/07/02 22:18:03 tedu Exp $	*/
426e45f0cScjep 
5232a750cScjep /*-
60a117266Sjoerg  * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
70a117266Sjoerg  * Copyright (C) 2008-2010 Gabor Kovesdan <gabor@FreeBSD.org>
8232a750cScjep  * All rights reserved.
9232a750cScjep  *
10232a750cScjep  * Redistribution and use in source and binary forms, with or without
11232a750cScjep  * modification, are permitted provided that the following conditions
12232a750cScjep  * are met:
13232a750cScjep  * 1. Redistributions of source code must retain the above copyright
14232a750cScjep  *    notice, this list of conditions and the following disclaimer.
15232a750cScjep  * 2. Redistributions in binary form must reproduce the above copyright
16232a750cScjep  *    notice, this list of conditions and the following disclaimer in the
17232a750cScjep  *    documentation and/or other materials provided with the distribution.
18232a750cScjep  *
19232a750cScjep  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20232a750cScjep  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21232a750cScjep  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22232a750cScjep  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23232a750cScjep  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24232a750cScjep  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25232a750cScjep  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26232a750cScjep  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27232a750cScjep  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28232a750cScjep  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29232a750cScjep  * SUCH DAMAGE.
30232a750cScjep  */
31232a750cScjep 
32f7d22212Sjoerg #if HAVE_NBTOOL_CONFIG_H
33f7d22212Sjoerg #include "nbtool_config.h"
34f7d22212Sjoerg #endif
35f7d22212Sjoerg 
3626e45f0cScjep #include <sys/cdefs.h>
37*4695f707Smrg __RCSID("$NetBSD: util.c,v 1.19 2018/02/05 22:14:26 mrg Exp $");
3826e45f0cScjep 
39232a750cScjep #include <sys/stat.h>
400a117266Sjoerg #include <sys/types.h>
41232a750cScjep 
42232a750cScjep #include <ctype.h>
43232a750cScjep #include <err.h>
44232a750cScjep #include <errno.h>
450a117266Sjoerg #include <fnmatch.h>
46232a750cScjep #include <fts.h>
470a117266Sjoerg #include <libgen.h>
480a117266Sjoerg #include <stdbool.h>
49232a750cScjep #include <stdio.h>
50232a750cScjep #include <stdlib.h>
51232a750cScjep #include <string.h>
52232a750cScjep #include <unistd.h>
530a117266Sjoerg #include <wchar.h>
540a117266Sjoerg #include <wctype.h>
55232a750cScjep 
56232a750cScjep #include "grep.h"
57232a750cScjep 
58ff9dcf43Sjoerg static bool	 first, first_global = true;
59ff9dcf43Sjoerg static unsigned long long since_printed;
60ff9dcf43Sjoerg 
610a117266Sjoerg static int	 procline(struct str *l, int);
620a117266Sjoerg 
630a117266Sjoerg bool
file_matching(const char * fname)640a117266Sjoerg file_matching(const char *fname)
650a117266Sjoerg {
66dc189fa2Sjoerg 	char *fname_base, *fname_copy;
676a4bfecdSjoerg 	unsigned int i;
680a117266Sjoerg 	bool ret;
690a117266Sjoerg 
700a117266Sjoerg 	ret = finclude ? false : true;
71dc189fa2Sjoerg 	fname_copy = grep_strdup(fname);
72dc189fa2Sjoerg 	fname_base = basename(fname_copy);
730a117266Sjoerg 
746a4bfecdSjoerg 	for (i = 0; i < fpatterns; ++i) {
75dc189fa2Sjoerg 		if (fnmatch(fpattern[i].pat, fname, 0) == 0 ||
76dc189fa2Sjoerg 		    fnmatch(fpattern[i].pat, fname_base, 0) == 0) {
77302f8680Smsaitoh 			if (fpattern[i].mode == EXCL_PAT) {
78302f8680Smsaitoh 				free(fname_copy);
790a117266Sjoerg 				return (false);
80302f8680Smsaitoh 			} else
810a117266Sjoerg 				ret = true;
820a117266Sjoerg 		}
830a117266Sjoerg 	}
84dc189fa2Sjoerg 	free(fname_copy);
850a117266Sjoerg 	return (ret);
860a117266Sjoerg }
870a117266Sjoerg 
880a117266Sjoerg static inline bool
dir_matching(const char * dname)890a117266Sjoerg dir_matching(const char *dname)
900a117266Sjoerg {
916a4bfecdSjoerg 	unsigned int i;
920a117266Sjoerg 	bool ret;
930a117266Sjoerg 
940a117266Sjoerg 	ret = dinclude ? false : true;
950a117266Sjoerg 
966a4bfecdSjoerg 	for (i = 0; i < dpatterns; ++i) {
970a117266Sjoerg 		if (dname != NULL &&
980a117266Sjoerg 		    fnmatch(dname, dpattern[i].pat, 0) == 0) {
990a117266Sjoerg 			if (dpattern[i].mode == EXCL_PAT)
1000a117266Sjoerg 				return (false);
1010a117266Sjoerg 			else
1020a117266Sjoerg 				ret = true;
1030a117266Sjoerg 		}
1040a117266Sjoerg 	}
1050a117266Sjoerg 	return (ret);
1060a117266Sjoerg }
1070a117266Sjoerg 
108232a750cScjep /*
1090a117266Sjoerg  * Processes a directory when a recursive search is performed with
1100a117266Sjoerg  * the -R option.  Each appropriate file is passed to procfile().
111232a750cScjep  */
112232a750cScjep int
grep_tree(char ** argv)113232a750cScjep grep_tree(char **argv)
114232a750cScjep {
115232a750cScjep 	FTS *fts;
116232a750cScjep 	FTSENT *p;
1170a117266Sjoerg 	char *d, *dir = NULL;
118232a750cScjep 	int c, fts_flags;
1190a117266Sjoerg 	bool ok;
120232a750cScjep 
121232a750cScjep 	c = fts_flags = 0;
122232a750cScjep 
1230a117266Sjoerg 	switch(linkbehave) {
1240a117266Sjoerg 	case LINK_EXPLICIT:
125232a750cScjep 		fts_flags = FTS_COMFOLLOW;
1260a117266Sjoerg 		break;
1270a117266Sjoerg 	case LINK_SKIP:
128232a750cScjep 		fts_flags = FTS_PHYSICAL;
1290a117266Sjoerg 		break;
1300a117266Sjoerg 	default:
1310a117266Sjoerg 		fts_flags = FTS_LOGICAL;
132232a750cScjep 
1330a117266Sjoerg 	}
134232a750cScjep 
1350a117266Sjoerg 	fts_flags |= FTS_NOSTAT | FTS_NOCHDIR;
1360a117266Sjoerg 
1370a117266Sjoerg 	if (!(fts = fts_open(argv, fts_flags, NULL)))
1380a117266Sjoerg 		err(2, "fts_open");
139232a750cScjep 	while ((p = fts_read(fts)) != NULL) {
140232a750cScjep 		switch (p->fts_info) {
141232a750cScjep 		case FTS_DNR:
1420a117266Sjoerg 			/* FALLTHROUGH */
143232a750cScjep 		case FTS_ERR:
14426e45f0cScjep 			errx(2, "%s: %s", p->fts_path, strerror(p->fts_errno));
145232a750cScjep 			break;
14626e45f0cScjep 		case FTS_D:
1470a117266Sjoerg 			/* FALLTHROUGH */
1480a117266Sjoerg 		case FTS_DP:
14926e45f0cScjep 			break;
15026e45f0cScjep 		case FTS_DC:
1510a117266Sjoerg 			/* Print a warning for recursive directory loop */
152a67c9be5Swiz 			warnx("warning: %s: recursive directory loop",
15326e45f0cScjep 				p->fts_path);
154232a750cScjep 			break;
155232a750cScjep 		default:
1560a117266Sjoerg 			/* Check for file exclusion/inclusion */
1570a117266Sjoerg 			ok = true;
1580a117266Sjoerg 			if (dexclude || dinclude) {
1590a117266Sjoerg 				if ((d = strrchr(p->fts_path, '/')) != NULL) {
1600a117266Sjoerg 					dir = grep_malloc(sizeof(char) *
1610a117266Sjoerg 					    (d - p->fts_path + 1));
1620a117266Sjoerg 					memcpy(dir, p->fts_path,
1630a117266Sjoerg 					    d - p->fts_path);
1640a117266Sjoerg 					dir[d - p->fts_path] = '\0';
1650a117266Sjoerg 				}
1660a117266Sjoerg 				ok = dir_matching(dir);
1670a117266Sjoerg 				free(dir);
1680a117266Sjoerg 				dir = NULL;
1690a117266Sjoerg 			}
1700a117266Sjoerg 			if (fexclude || finclude)
1710a117266Sjoerg 				ok &= file_matching(p->fts_path);
1720a117266Sjoerg 
1730a117266Sjoerg 			if (ok)
174232a750cScjep 				c += procfile(p->fts_path);
175232a750cScjep 			break;
176232a750cScjep 		}
177232a750cScjep 	}
178232a750cScjep 
1790a117266Sjoerg 	fts_close(fts);
1800a117266Sjoerg 	return (c);
181232a750cScjep }
182232a750cScjep 
1830a117266Sjoerg /*
1840a117266Sjoerg  * Opens a file and processes it.  Each file is processed line-by-line
1850a117266Sjoerg  * passing the lines to procline().
1860a117266Sjoerg  */
187232a750cScjep int
procfile(const char * fn)1886e294718Srillig procfile(const char *fn)
189232a750cScjep {
1900a117266Sjoerg 	struct file *f;
19126e45f0cScjep 	struct stat sb;
1920a117266Sjoerg 	struct str ln;
19326e45f0cScjep 	mode_t s;
1940a117266Sjoerg 	int c, t;
19526e45f0cScjep 
1960a117266Sjoerg 	if (mflag && (mcount <= 0))
1970a117266Sjoerg 		return (0);
198232a750cScjep 
1990a117266Sjoerg 	if (strcmp(fn, "-") == 0) {
2000a117266Sjoerg 		fn = label != NULL ? label : getstr(1);
2010a117266Sjoerg 		f = grep_open(NULL);
202232a750cScjep 	} else {
2030a117266Sjoerg 		if (!stat(fn, &sb)) {
2040a117266Sjoerg 			/* Check if we need to process the file */
20526e45f0cScjep 			s = sb.st_mode & S_IFMT;
2060a117266Sjoerg 			if (s == S_IFDIR && dirbehave == DIR_SKIP)
2070a117266Sjoerg 				return (0);
2080a117266Sjoerg 			if ((s == S_IFIFO || s == S_IFCHR || s == S_IFBLK
2090a117266Sjoerg 				|| s == S_IFSOCK) && devbehave == DEV_SKIP)
2100a117266Sjoerg 					return (0);
21126e45f0cScjep 		}
2120a117266Sjoerg 		f = grep_open(fn);
213232a750cScjep 	}
214232a750cScjep 	if (f == NULL) {
215232a750cScjep 		if (!sflag)
216232a750cScjep 			warn("%s", fn);
2170a117266Sjoerg 		if (errno == ENOENT)
2180a117266Sjoerg 			notfound = true;
2190a117266Sjoerg 		return (0);
220232a750cScjep 	}
22126e45f0cScjep 
2220a117266Sjoerg 	ln.file = grep_malloc(strlen(fn) + 1);
2230a117266Sjoerg 	strcpy(ln.file, fn);
224232a750cScjep 	ln.line_no = 0;
2250a117266Sjoerg 	ln.len = 0;
2260a117266Sjoerg 	tail = 0;
227232a750cScjep 	ln.off = -1;
228232a750cScjep 
229ff9dcf43Sjoerg 	for (first = true, c = 0;  c == 0 || !(lflag || qflag); ) {
230232a750cScjep 		ln.off += ln.len + 1;
2310fe4b4b0Sjoerg 		if ((ln.dat = grep_fgetln(f, &ln.len)) == NULL || ln.len == 0)
232232a750cScjep 			break;
23334bdd278Sjoerg 		if (ln.len > 0 && ln.dat[ln.len - 1] == line_sep)
234232a750cScjep 			--ln.len;
235232a750cScjep 		ln.line_no++;
236232a750cScjep 
2370a117266Sjoerg 		/* Return if we need to skip a binary file */
2380a117266Sjoerg 		if (f->binary && binbehave == BINFILE_SKIP) {
2390a117266Sjoerg 			grep_close(f);
2400a117266Sjoerg 			free(ln.file);
2410a117266Sjoerg 			free(f);
2420a117266Sjoerg 			return (0);
2430a117266Sjoerg 		}
2440a117266Sjoerg 		/* Process the file line-by-line */
245ff9dcf43Sjoerg 		t = procline(&ln, f->binary);
246232a750cScjep 		c += t;
24726e45f0cScjep 
2480a117266Sjoerg 		/* Count the matches if we have a match limit */
2490a117266Sjoerg 		if (mflag) {
2500a117266Sjoerg 			mcount -= t;
2510a117266Sjoerg 			if (mcount <= 0)
25226e45f0cScjep 				break;
253232a750cScjep 		}
2540a117266Sjoerg 	}
255232a750cScjep 	if (Bflag > 0)
256232a750cScjep 		clearqueue();
257232a750cScjep 	grep_close(f);
258232a750cScjep 
259232a750cScjep 	if (cflag) {
2600a117266Sjoerg 		if (!hflag)
2610a117266Sjoerg 			printf("%s:", ln.file);
26234bdd278Sjoerg 		printf("%u%c", c, line_sep);
263232a750cScjep 	}
2640a117266Sjoerg 	if (lflag && !qflag && c != 0)
26534bdd278Sjoerg 		printf("%s%c", fn, line_sep);
2660a117266Sjoerg 	if (Lflag && !qflag && c == 0)
26734bdd278Sjoerg 		printf("%s%c", fn, line_sep);
26826e45f0cScjep 	if (c && !cflag && !lflag && !Lflag &&
2690a117266Sjoerg 	    binbehave == BINFILE_BIN && f->binary && !qflag)
2700a117266Sjoerg 		printf(getstr(8), fn);
27126e45f0cScjep 
2720a117266Sjoerg 	free(ln.file);
2730a117266Sjoerg 	free(f);
2740a117266Sjoerg 	return (c);
275232a750cScjep }
276232a750cScjep 
2770a117266Sjoerg #define iswword(x)	(iswalnum((x)) || (x) == L'_')
278232a750cScjep 
279232a750cScjep /*
2800a117266Sjoerg  * Processes a line comparing it with the specified patterns.  Each pattern
2810a117266Sjoerg  * is looped to be compared along with the full string, saving each and every
2820a117266Sjoerg  * match, which is necessary to colorize the output and to count the
2830a117266Sjoerg  * matches.  The matching lines are passed to printline() to display the
2840a117266Sjoerg  * appropriate output.
285232a750cScjep  */
286dc189fa2Sjoerg static int
procline(struct str * l,int nottext)2870a117266Sjoerg procline(struct str *l, int nottext)
288232a750cScjep {
28926e45f0cScjep 	regmatch_t matches[MAX_LINE_MATCHES];
2900a117266Sjoerg 	regmatch_t pmatch;
2910a117266Sjoerg 	size_t st = 0;
2920a117266Sjoerg 	unsigned int i;
2930a117266Sjoerg 	int c = 0, m = 0, r = 0;
294232a750cScjep 
2950a117266Sjoerg 	/* Loop to process the whole line */
2960a117266Sjoerg 	while (st <= l->len) {
29726e45f0cScjep 		pmatch.rm_so = st;
298232a750cScjep 		pmatch.rm_eo = l->len;
2990a117266Sjoerg 
3000a117266Sjoerg 		/* Loop to compare with all the patterns */
30126e45f0cScjep 		for (i = 0; i < patterns; i++) {
3020a117266Sjoerg /*
3030a117266Sjoerg  * XXX: grep_search() is a workaround for speed up and should be
3040a117266Sjoerg  * removed in the future.  See fastgrep.c.
3050a117266Sjoerg  */
3060a117266Sjoerg 			if (fg_pattern[i].pattern) {
3070a117266Sjoerg 				r = grep_search(&fg_pattern[i],
3080a117266Sjoerg 				    (unsigned char *)l->dat,
3090a117266Sjoerg 				    l->len, &pmatch);
3100a117266Sjoerg 				r = (r == 0) ? 0 : REG_NOMATCH;
3110a117266Sjoerg 				st = pmatch.rm_eo;
3120a117266Sjoerg 			} else {
3130a117266Sjoerg 				r = regexec(&r_pattern[i], l->dat, 1,
3140a117266Sjoerg 				    &pmatch, eflags);
3150a117266Sjoerg 				r = (r == 0) ? 0 : REG_NOMATCH;
3160a117266Sjoerg 				st = pmatch.rm_eo;
3170a117266Sjoerg 			}
3180a117266Sjoerg 			if (r == REG_NOMATCH)
319232a750cScjep 				continue;
3200a117266Sjoerg 			/* Check for full match */
321a9992047Sjoerg 			if (xflag &&
322a9992047Sjoerg 			    (pmatch.rm_so != 0 ||
323a9992047Sjoerg 			     (size_t)pmatch.rm_eo != l->len))
324a9992047Sjoerg 				continue;
3250a117266Sjoerg 			/* Check for whole word match */
326a9992047Sjoerg 			if (fg_pattern[i].word && pmatch.rm_so != 0) {
327b415df1fSchristos 				wchar_t wbegin, wend;
3280a117266Sjoerg 
3290a117266Sjoerg 				wbegin = wend = L' ';
3300a117266Sjoerg 				if (pmatch.rm_so != 0 &&
3310a117266Sjoerg 				    sscanf(&l->dat[pmatch.rm_so - 1],
3320a117266Sjoerg 				    "%lc", &wbegin) != 1)
333a9992047Sjoerg 					continue;
334a9992047Sjoerg 				if ((size_t)pmatch.rm_eo != l->len &&
3350a117266Sjoerg 				    sscanf(&l->dat[pmatch.rm_eo],
3360a117266Sjoerg 				    "%lc", &wend) != 1)
337a9992047Sjoerg 					continue;
338a9992047Sjoerg 				if (iswword(wbegin) || iswword(wend))
339a9992047Sjoerg 					continue;
3400a117266Sjoerg 			}
341a9992047Sjoerg 			c = 1;
3420a117266Sjoerg 			if (m < MAX_LINE_MATCHES)
3430a117266Sjoerg 				matches[m++] = pmatch;
3440a117266Sjoerg 			/* matches - skip further patterns */
3450a117266Sjoerg 			if ((color != NULL && !oflag) || qflag || lflag)
346232a750cScjep 				break;
347232a750cScjep 		}
348232a750cScjep 
3490a117266Sjoerg 		if (vflag) {
3500a117266Sjoerg 			c = !c;
3510a117266Sjoerg 			break;
3520a117266Sjoerg 		}
35326e45f0cScjep 		/* One pass if we are not recording matches */
3540a117266Sjoerg 		if ((color != NULL && !oflag) || qflag || lflag)
35526e45f0cScjep 			break;
35626e45f0cScjep 
3570a117266Sjoerg 		if (st == (size_t)pmatch.rm_so)
35826e45f0cScjep 			break; 	/* No matches */
35926e45f0cScjep 	}
36026e45f0cScjep 
3610a117266Sjoerg 	if (c && binbehave == BINFILE_BIN && nottext)
3620a117266Sjoerg 		return (c); /* Binary file */
36326e45f0cScjep 
3640a117266Sjoerg 	/* Dealing with the context */
3650a117266Sjoerg 	if ((tail || c) && !cflag && !qflag && !lflag && !Lflag) {
366232a750cScjep 		if (c) {
367ff9dcf43Sjoerg 			if ((Aflag || Bflag) && !first_global &&
368ff9dcf43Sjoerg 			    (first || since_printed > Bflag))
369232a750cScjep 				printf("--\n");
370232a750cScjep 			tail = Aflag;
371ff9dcf43Sjoerg 			if (Bflag > 0)
372232a750cScjep 				printqueue();
3730a117266Sjoerg 			printline(l, ':', matches, m);
374232a750cScjep 		} else {
3750a117266Sjoerg 			printline(l, '-', matches, m);
376232a750cScjep 			tail--;
377232a750cScjep 		}
3780a117266Sjoerg 		first = false;
379ff9dcf43Sjoerg 		first_global = false;
380ff9dcf43Sjoerg 		since_printed = 0;
381ff9dcf43Sjoerg 	} else {
382ff9dcf43Sjoerg 		if (Bflag)
383ff9dcf43Sjoerg 			enqueue(l);
384ff9dcf43Sjoerg 		since_printed++;
385ff9dcf43Sjoerg 	}
3860a117266Sjoerg 	return (c);
3870a117266Sjoerg }
3880a117266Sjoerg 
3890a117266Sjoerg /*
3900a117266Sjoerg  * Safe malloc() for internal use.
3910a117266Sjoerg  */
392232a750cScjep void *
grep_malloc(size_t size)393232a750cScjep grep_malloc(size_t size)
394232a750cScjep {
395232a750cScjep 	void *ptr;
396232a750cScjep 
397232a750cScjep 	if ((ptr = malloc(size)) == NULL)
39826e45f0cScjep 		err(2, "malloc");
3990a117266Sjoerg 	return (ptr);
400232a750cScjep }
401232a750cScjep 
4020a117266Sjoerg /*
4030a117266Sjoerg  * Safe calloc() for internal use.
4040a117266Sjoerg  */
4050a117266Sjoerg void *
grep_calloc(size_t nmemb,size_t size)4060a117266Sjoerg grep_calloc(size_t nmemb, size_t size)
4070a117266Sjoerg {
4080a117266Sjoerg 	void *ptr;
4090a117266Sjoerg 
4100a117266Sjoerg 	if ((ptr = calloc(nmemb, size)) == NULL)
4110a117266Sjoerg 		err(2, "calloc");
4120a117266Sjoerg 	return (ptr);
4130a117266Sjoerg }
4140a117266Sjoerg 
4150a117266Sjoerg /*
4160a117266Sjoerg  * Safe realloc() for internal use.
4170a117266Sjoerg  */
418232a750cScjep void *
grep_realloc(void * ptr,size_t size)419232a750cScjep grep_realloc(void *ptr, size_t size)
420232a750cScjep {
4210a117266Sjoerg 
422232a750cScjep 	if ((ptr = realloc(ptr, size)) == NULL)
42326e45f0cScjep 		err(2, "realloc");
4240a117266Sjoerg 	return (ptr);
425232a750cScjep }
426232a750cScjep 
4270a117266Sjoerg /*
4280a117266Sjoerg  * Safe strdup() for internal use.
4290a117266Sjoerg  */
4300a117266Sjoerg char *
grep_strdup(const char * str)4310a117266Sjoerg grep_strdup(const char *str)
432232a750cScjep {
4330a117266Sjoerg 	char *ret;
434232a750cScjep 
4350a117266Sjoerg 	if ((ret = strdup(str)) == NULL)
4360a117266Sjoerg 		err(2, "strdup");
4370a117266Sjoerg 	return (ret);
4380a117266Sjoerg }
4390a117266Sjoerg 
4400a117266Sjoerg /*
4410a117266Sjoerg  * Prints a matching line according to the command line options.
4420a117266Sjoerg  */
4430a117266Sjoerg void
printline(struct str * line,int sep,regmatch_t * matches,int m)4440a117266Sjoerg printline(struct str *line, int sep, regmatch_t *matches, int m)
4450a117266Sjoerg {
4460a117266Sjoerg 	size_t a = 0;
4470a117266Sjoerg 	int i, n = 0;
4480a117266Sjoerg 
4490a117266Sjoerg 	if (!hflag) {
4500a117266Sjoerg 		if (nullflag == 0)
451232a750cScjep 			fputs(line->file, stdout);
4520a117266Sjoerg 		else {
4530a117266Sjoerg 			printf("%s", line->file);
4540a117266Sjoerg 			putchar(0);
4550a117266Sjoerg 		}
456232a750cScjep 		++n;
457232a750cScjep 	}
458232a750cScjep 	if (nflag) {
4590a117266Sjoerg 		if (n > 0)
460232a750cScjep 			putchar(sep);
461232a750cScjep 		printf("%d", line->line_no);
462232a750cScjep 		++n;
463232a750cScjep 	}
464232a750cScjep 	if (bflag) {
4650a117266Sjoerg 		if (n > 0)
466232a750cScjep 			putchar(sep);
4670a117266Sjoerg 		printf("%lld", (long long)line->off);
4680a117266Sjoerg 		++n;
469232a750cScjep 	}
470232a750cScjep 	if (n)
471232a750cScjep 		putchar(sep);
4720a117266Sjoerg 	/* --color and -o */
4730a117266Sjoerg 	if ((oflag || color) && m > 0) {
47426e45f0cScjep 		for (i = 0; i < m; i++) {
47526e45f0cScjep 			if (!oflag)
4760a117266Sjoerg 				fwrite(line->dat + a, matches[i].rm_so - a, 1,
4770a117266Sjoerg 				    stdout);
4780a117266Sjoerg 			if (color)
4790a117266Sjoerg 				fprintf(stdout, "\33[%sm\33[K", color);
48026e45f0cScjep 
48126e45f0cScjep 			fwrite(line->dat + matches[i].rm_so,
4820a117266Sjoerg 			    matches[i].rm_eo - matches[i].rm_so, 1,
4830a117266Sjoerg 			    stdout);
484*4695f707Smrg 
4850a117266Sjoerg 			if (color)
4860a117266Sjoerg 				fprintf(stdout, "\33[m\33[K");
48726e45f0cScjep 			a = matches[i].rm_eo;
48826e45f0cScjep 			if (oflag)
489232a750cScjep 				putchar('\n');
490232a750cScjep 		}
49126e45f0cScjep 		if (!oflag) {
49226e45f0cScjep 			if (line->len - a > 0)
49326e45f0cScjep 				fwrite(line->dat + a, line->len - a, 1, stdout);
49434bdd278Sjoerg 			putchar(line_sep);
49526e45f0cScjep 		}
49626e45f0cScjep 	} else {
49726e45f0cScjep 		fwrite(line->dat, line->len, 1, stdout);
49834bdd278Sjoerg 		putchar(line_sep);
49926e45f0cScjep 	}
50026e45f0cScjep }
501