1b66a823bSGabor Kovesdan /* $NetBSD: util.c,v 1.9 2011/02/27 17:33:37 joerg Exp $ */
24dc88ebeSGabor Kovesdan /* $OpenBSD: util.c,v 1.39 2010/07/02 22:18:03 tedu Exp $ */
34dc88ebeSGabor Kovesdan
44dc88ebeSGabor Kovesdan /*-
54d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
61de7b4b8SPedro F. Giffuni *
7e738085bSDag-Erling Smørgrav * Copyright (c) 1999 James Howard and Dag-Erling Smørgrav
84dc88ebeSGabor Kovesdan * Copyright (C) 2008-2010 Gabor Kovesdan <gabor@FreeBSD.org>
926e1c38fSKyle Evans * Copyright (C) 2017 Kyle Evans <kevans@FreeBSD.org>
104dc88ebeSGabor Kovesdan * All rights reserved.
114dc88ebeSGabor Kovesdan *
124dc88ebeSGabor Kovesdan * Redistribution and use in source and binary forms, with or without
134dc88ebeSGabor Kovesdan * modification, are permitted provided that the following conditions
144dc88ebeSGabor Kovesdan * are met:
154dc88ebeSGabor Kovesdan * 1. Redistributions of source code must retain the above copyright
164dc88ebeSGabor Kovesdan * notice, this list of conditions and the following disclaimer.
174dc88ebeSGabor Kovesdan * 2. Redistributions in binary form must reproduce the above copyright
184dc88ebeSGabor Kovesdan * notice, this list of conditions and the following disclaimer in the
194dc88ebeSGabor Kovesdan * documentation and/or other materials provided with the distribution.
204dc88ebeSGabor Kovesdan *
214dc88ebeSGabor Kovesdan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
224dc88ebeSGabor Kovesdan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
234dc88ebeSGabor Kovesdan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
244dc88ebeSGabor Kovesdan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
254dc88ebeSGabor Kovesdan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264dc88ebeSGabor Kovesdan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
274dc88ebeSGabor Kovesdan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
284dc88ebeSGabor Kovesdan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
294dc88ebeSGabor Kovesdan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
304dc88ebeSGabor Kovesdan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
314dc88ebeSGabor Kovesdan * SUCH DAMAGE.
324dc88ebeSGabor Kovesdan */
334dc88ebeSGabor Kovesdan
344dc88ebeSGabor Kovesdan #include <sys/cdefs.h>
354dc88ebeSGabor Kovesdan #include <sys/stat.h>
364dc88ebeSGabor Kovesdan #include <sys/types.h>
374dc88ebeSGabor Kovesdan
384dc88ebeSGabor Kovesdan #include <ctype.h>
394dc88ebeSGabor Kovesdan #include <err.h>
404dc88ebeSGabor Kovesdan #include <errno.h>
414dc88ebeSGabor Kovesdan #include <fnmatch.h>
424dc88ebeSGabor Kovesdan #include <fts.h>
434dc88ebeSGabor Kovesdan #include <libgen.h>
4455e44f51SGabor Kovesdan #include <stdbool.h>
454dc88ebeSGabor Kovesdan #include <stdio.h>
464dc88ebeSGabor Kovesdan #include <stdlib.h>
474dc88ebeSGabor Kovesdan #include <string.h>
484dc88ebeSGabor Kovesdan #include <unistd.h>
494dc88ebeSGabor Kovesdan #include <wchar.h>
504dc88ebeSGabor Kovesdan #include <wctype.h>
514dc88ebeSGabor Kovesdan
524dc88ebeSGabor Kovesdan #include "grep.h"
534dc88ebeSGabor Kovesdan
54a4f3f02bSEd Maste static bool first_match = true;
554dc88ebeSGabor Kovesdan
56a4f3f02bSEd Maste /*
5781c634e8SKyle Evans * Match printing context
5881c634e8SKyle Evans */
5981c634e8SKyle Evans struct mprintc {
6081c634e8SKyle Evans long long tail; /* Number of trailing lines to record */
6181c634e8SKyle Evans int last_outed; /* Number of lines since last output */
6281c634e8SKyle Evans bool doctx; /* Printing context? */
6381c634e8SKyle Evans bool printmatch; /* Printing matches? */
6481c634e8SKyle Evans bool same_file; /* Same file as previously printed? */
6581c634e8SKyle Evans };
6681c634e8SKyle Evans
67042db8e8SKyle Evans static void procmatch_match(struct mprintc *mc, struct parsec *pc);
68042db8e8SKyle Evans static void procmatch_nomatch(struct mprintc *mc, struct parsec *pc);
6981c634e8SKyle Evans static bool procmatches(struct mprintc *mc, struct parsec *pc, bool matched);
7005ad8215SKyle Evans #ifdef WITH_INTERNAL_NOSPEC
7105ad8215SKyle Evans static int litexec(const struct pat *pat, const char *string,
7205ad8215SKyle Evans size_t nmatch, regmatch_t pmatch[]);
7305ad8215SKyle Evans #endif
74be13c0f9SKyle Evans static bool procline(struct parsec *pc);
75a4f3f02bSEd Maste static void printline(struct parsec *pc, int sep);
76a4f3f02bSEd Maste static void printline_metadata(struct str *line, int sep);
7722130a21SEd Maste
7855e44f51SGabor Kovesdan bool
file_matching(const char * fname)7955e44f51SGabor Kovesdan file_matching(const char *fname)
8055e44f51SGabor Kovesdan {
8133f5799aSEd Schouten char *fname_base, *fname_buf;
8255e44f51SGabor Kovesdan bool ret;
8355e44f51SGabor Kovesdan
8455e44f51SGabor Kovesdan ret = finclude ? false : true;
8533f5799aSEd Schouten fname_buf = strdup(fname);
8633f5799aSEd Schouten if (fname_buf == NULL)
8733f5799aSEd Schouten err(2, "strdup");
8833f5799aSEd Schouten fname_base = basename(fname_buf);
8955e44f51SGabor Kovesdan
9055e44f51SGabor Kovesdan for (unsigned int i = 0; i < fpatterns; ++i) {
91d841ecb3SGabor Kovesdan if (fnmatch(fpattern[i].pat, fname, 0) == 0 ||
92ff415f05SKyle Evans fnmatch(fpattern[i].pat, fname_base, 0) == 0)
93ff415f05SKyle Evans /*
94ff415f05SKyle Evans * The last pattern matched wins exclusion/inclusion
95ff415f05SKyle Evans * rights, so we can't reasonably bail out early here.
96ff415f05SKyle Evans */
97e3a2abadSKyle Evans ret = (fpattern[i].mode != EXCL_PAT);
9855e44f51SGabor Kovesdan }
9933f5799aSEd Schouten free(fname_buf);
10055e44f51SGabor Kovesdan return (ret);
10155e44f51SGabor Kovesdan }
10255e44f51SGabor Kovesdan
10359218eb7SGabor Kovesdan static inline bool
dir_matching(const char * dname)10455e44f51SGabor Kovesdan dir_matching(const char *dname)
10555e44f51SGabor Kovesdan {
10655e44f51SGabor Kovesdan bool ret;
10755e44f51SGabor Kovesdan
10855e44f51SGabor Kovesdan ret = dinclude ? false : true;
10955e44f51SGabor Kovesdan
11055e44f51SGabor Kovesdan for (unsigned int i = 0; i < dpatterns; ++i) {
111e3a2abadSKyle Evans if (dname != NULL && fnmatch(dpattern[i].pat, dname, 0) == 0)
112ff415f05SKyle Evans /*
113ff415f05SKyle Evans * The last pattern matched wins exclusion/inclusion
114ff415f05SKyle Evans * rights, so we can't reasonably bail out early here.
115ff415f05SKyle Evans */
116ff415f05SKyle Evans ret = (dpattern[i].mode != EXCL_PAT);
11755e44f51SGabor Kovesdan }
11855e44f51SGabor Kovesdan return (ret);
11955e44f51SGabor Kovesdan }
12055e44f51SGabor Kovesdan
1214dc88ebeSGabor Kovesdan /*
1224dc88ebeSGabor Kovesdan * Processes a directory when a recursive search is performed with
1234dc88ebeSGabor Kovesdan * the -R option. Each appropriate file is passed to procfile().
1244dc88ebeSGabor Kovesdan */
125cbfff13fSKyle Evans bool
grep_tree(char ** argv)1264dc88ebeSGabor Kovesdan grep_tree(char **argv)
1274dc88ebeSGabor Kovesdan {
1284dc88ebeSGabor Kovesdan FTS *fts;
1294dc88ebeSGabor Kovesdan FTSENT *p;
13040f0e0b1SKyle Evans int fts_flags;
131cbfff13fSKyle Evans bool matched, ok;
132a461896aSEd Maste const char *wd[] = { ".", NULL };
1334dc88ebeSGabor Kovesdan
134cbfff13fSKyle Evans matched = false;
1354dc88ebeSGabor Kovesdan
13666f780aeSKyle Evans /* This switch effectively initializes 'fts_flags' */
1374dc88ebeSGabor Kovesdan switch(linkbehave) {
1384dc88ebeSGabor Kovesdan case LINK_EXPLICIT:
139*77eb8777SJohn Baldwin fts_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
1404dc88ebeSGabor Kovesdan break;
1414dc88ebeSGabor Kovesdan case LINK_SKIP:
1424dc88ebeSGabor Kovesdan fts_flags = FTS_PHYSICAL;
1434dc88ebeSGabor Kovesdan break;
1444dc88ebeSGabor Kovesdan default:
145*77eb8777SJohn Baldwin fts_flags = FTS_LOGICAL | FTS_NOSTAT;
1464dc88ebeSGabor Kovesdan }
1474dc88ebeSGabor Kovesdan
148*77eb8777SJohn Baldwin fts_flags |= FTS_NOCHDIR;
1494dc88ebeSGabor Kovesdan
150a461896aSEd Maste fts = fts_open((argv[0] == NULL) ?
151a461896aSEd Maste __DECONST(char * const *, wd) : argv, fts_flags, NULL);
152a461896aSEd Maste if (fts == NULL)
1530c41ffb3SXin LI err(2, "fts_open");
1542dfa4b66SBryan Drewery while (errno = 0, (p = fts_read(fts)) != NULL) {
1554dc88ebeSGabor Kovesdan switch (p->fts_info) {
1564dc88ebeSGabor Kovesdan case FTS_DNR:
1574dc88ebeSGabor Kovesdan case FTS_ERR:
158*77eb8777SJohn Baldwin case FTS_NS:
1596f4cbf7cSGabor Kovesdan file_err = true;
160ede01be2SGabor Kovesdan if(!sflag)
161*77eb8777SJohn Baldwin warnc(p->fts_errno, "%s", p->fts_path);
1624dc88ebeSGabor Kovesdan break;
1634dc88ebeSGabor Kovesdan case FTS_D:
164ad92276eSGabor Kovesdan if (dexclude || dinclude)
165ad92276eSGabor Kovesdan if (!dir_matching(p->fts_name) ||
166ad92276eSGabor Kovesdan !dir_matching(p->fts_path))
167ad92276eSGabor Kovesdan fts_set(fts, p, FTS_SKIP);
1684dc88ebeSGabor Kovesdan break;
1694dc88ebeSGabor Kovesdan case FTS_DC:
1704dc88ebeSGabor Kovesdan /* Print a warning for recursive directory loop */
1714dc88ebeSGabor Kovesdan warnx("warning: %s: recursive directory loop",
1724dc88ebeSGabor Kovesdan p->fts_path);
1734dc88ebeSGabor Kovesdan break;
174*77eb8777SJohn Baldwin case FTS_DP:
175*77eb8777SJohn Baldwin break;
176*77eb8777SJohn Baldwin case FTS_SL:
177*77eb8777SJohn Baldwin /*
178*77eb8777SJohn Baldwin * Skip symlinks for LINK_EXPLICIT and
179*77eb8777SJohn Baldwin * LINK_SKIP. Note that due to FTS_COMFOLLOW,
180*77eb8777SJohn Baldwin * symlinks on the command line are followed
181*77eb8777SJohn Baldwin * for LINK_EXPLICIT and not reported as
182*77eb8777SJohn Baldwin * symlinks.
183*77eb8777SJohn Baldwin */
184*77eb8777SJohn Baldwin break;
1854dc88ebeSGabor Kovesdan default:
1864dc88ebeSGabor Kovesdan /* Check for file exclusion/inclusion */
1874dc88ebeSGabor Kovesdan ok = true;
18855e44f51SGabor Kovesdan if (fexclude || finclude)
18955e44f51SGabor Kovesdan ok &= file_matching(p->fts_path);
1904dc88ebeSGabor Kovesdan
191cbfff13fSKyle Evans if (ok && procfile(p->fts_path))
192cbfff13fSKyle Evans matched = true;
1934dc88ebeSGabor Kovesdan break;
1944dc88ebeSGabor Kovesdan }
1954dc88ebeSGabor Kovesdan }
1962dfa4b66SBryan Drewery if (errno != 0)
1972dfa4b66SBryan Drewery err(2, "fts_read");
1984dc88ebeSGabor Kovesdan
1990c41ffb3SXin LI fts_close(fts);
200cbfff13fSKyle Evans return (matched);
2014dc88ebeSGabor Kovesdan }
2024dc88ebeSGabor Kovesdan
203042db8e8SKyle Evans static void
procmatch_match(struct mprintc * mc,struct parsec * pc)204042db8e8SKyle Evans procmatch_match(struct mprintc *mc, struct parsec *pc)
20581c634e8SKyle Evans {
20681c634e8SKyle Evans
2075ea3fdc7SKyle Evans if (mc->doctx) {
208042db8e8SKyle Evans if (!first_match && (!mc->same_file || mc->last_outed > 0))
20981c634e8SKyle Evans printf("--\n");
21081c634e8SKyle Evans if (Bflag > 0)
21181c634e8SKyle Evans printqueue();
21281c634e8SKyle Evans mc->tail = Aflag;
21381c634e8SKyle Evans }
21481c634e8SKyle Evans
21581c634e8SKyle Evans /* Print the matching line, but only if not quiet/binary */
2165ea3fdc7SKyle Evans if (mc->printmatch) {
21781c634e8SKyle Evans printline(pc, ':');
21881c634e8SKyle Evans while (pc->matchidx >= MAX_MATCHES) {
21981c634e8SKyle Evans /* Reset matchidx and try again */
22081c634e8SKyle Evans pc->matchidx = 0;
22138325e2aSKyle Evans if (procline(pc) == !vflag)
22281c634e8SKyle Evans printline(pc, ':');
22381c634e8SKyle Evans else
22481c634e8SKyle Evans break;
22581c634e8SKyle Evans }
22681c634e8SKyle Evans first_match = false;
22781c634e8SKyle Evans mc->same_file = true;
22881c634e8SKyle Evans mc->last_outed = 0;
22981c634e8SKyle Evans }
230042db8e8SKyle Evans }
231042db8e8SKyle Evans
232042db8e8SKyle Evans static void
procmatch_nomatch(struct mprintc * mc,struct parsec * pc)233042db8e8SKyle Evans procmatch_nomatch(struct mprintc *mc, struct parsec *pc)
234042db8e8SKyle Evans {
235042db8e8SKyle Evans
236042db8e8SKyle Evans /* Deal with any -A context as needed */
237042db8e8SKyle Evans if (mc->tail > 0) {
238042db8e8SKyle Evans grep_printline(&pc->ln, '-');
239042db8e8SKyle Evans mc->tail--;
240042db8e8SKyle Evans if (Bflag > 0)
241042db8e8SKyle Evans clearqueue();
242042db8e8SKyle Evans } else if (Bflag == 0 || (Bflag > 0 && enqueue(&pc->ln)))
243042db8e8SKyle Evans /*
244042db8e8SKyle Evans * Enqueue non-matching lines for -B context. If we're not
245042db8e8SKyle Evans * actually doing -B context or if the enqueue resulted in a
246042db8e8SKyle Evans * line being rotated out, then go ahead and increment
247042db8e8SKyle Evans * last_outed to signify a gap between context/match.
248042db8e8SKyle Evans */
249042db8e8SKyle Evans ++mc->last_outed;
250042db8e8SKyle Evans }
251042db8e8SKyle Evans
252042db8e8SKyle Evans /*
253042db8e8SKyle Evans * Process any matches in the current parsing context, return a boolean
254042db8e8SKyle Evans * indicating whether we should halt any further processing or not. 'true' to
255042db8e8SKyle Evans * continue processing, 'false' to halt.
256042db8e8SKyle Evans */
257042db8e8SKyle Evans static bool
procmatches(struct mprintc * mc,struct parsec * pc,bool matched)258042db8e8SKyle Evans procmatches(struct mprintc *mc, struct parsec *pc, bool matched)
259042db8e8SKyle Evans {
260042db8e8SKyle Evans
2613e2d96acSKyle Evans if (mflag && mcount <= 0) {
2623e2d96acSKyle Evans /*
2633e2d96acSKyle Evans * We already hit our match count, but we need to keep dumping
2643e2d96acSKyle Evans * lines until we've lost our tail.
2653e2d96acSKyle Evans */
2663e2d96acSKyle Evans grep_printline(&pc->ln, '-');
2673e2d96acSKyle Evans mc->tail--;
2683e2d96acSKyle Evans return (mc->tail != 0);
2693e2d96acSKyle Evans }
2703e2d96acSKyle Evans
271042db8e8SKyle Evans /*
272042db8e8SKyle Evans * XXX TODO: This should loop over pc->matches and handle things on a
273042db8e8SKyle Evans * line-by-line basis, setting up a `struct str` as needed.
274042db8e8SKyle Evans */
275042db8e8SKyle Evans /* Deal with any -B context or context separators */
276042db8e8SKyle Evans if (matched) {
277042db8e8SKyle Evans procmatch_match(mc, pc);
27881c634e8SKyle Evans
2795ea3fdc7SKyle Evans /* Count the matches if we have a match limit */
2805ea3fdc7SKyle Evans if (mflag) {
2815ea3fdc7SKyle Evans /* XXX TODO: Decrement by number of matched lines */
2825ea3fdc7SKyle Evans mcount -= 1;
283bd60b9b4SKyle Evans if (mcount <= 0)
2843e2d96acSKyle Evans return (mc->tail != 0);
2855ea3fdc7SKyle Evans }
286042db8e8SKyle Evans } else if (mc->doctx)
287042db8e8SKyle Evans procmatch_nomatch(mc, pc);
28881c634e8SKyle Evans
28981c634e8SKyle Evans return (true);
29081c634e8SKyle Evans }
29181c634e8SKyle Evans
29281c634e8SKyle Evans /*
2934dc88ebeSGabor Kovesdan * Opens a file and processes it. Each file is processed line-by-line
2944dc88ebeSGabor Kovesdan * passing the lines to procline().
2954dc88ebeSGabor Kovesdan */
296cbfff13fSKyle Evans bool
procfile(const char * fn)2974dc88ebeSGabor Kovesdan procfile(const char *fn)
2984dc88ebeSGabor Kovesdan {
299a4f3f02bSEd Maste struct parsec pc;
30081c634e8SKyle Evans struct mprintc mc;
3014dc88ebeSGabor Kovesdan struct file *f;
3024dc88ebeSGabor Kovesdan struct stat sb;
3034dc88ebeSGabor Kovesdan mode_t s;
304be13c0f9SKyle Evans int lines;
305be13c0f9SKyle Evans bool line_matched;
3064dc88ebeSGabor Kovesdan
3074dc88ebeSGabor Kovesdan if (strcmp(fn, "-") == 0) {
30830dc9502SBaptiste Daroussin fn = label != NULL ? label : errstr[1];
3093ed1008bSGabor Kovesdan f = grep_open(NULL);
3104dc88ebeSGabor Kovesdan } else {
311f3cf3e59SKyle Evans if (stat(fn, &sb) == 0) {
3124dc88ebeSGabor Kovesdan /* Check if we need to process the file */
3134dc88ebeSGabor Kovesdan s = sb.st_mode & S_IFMT;
314f3cf3e59SKyle Evans if (dirbehave == DIR_SKIP && s == S_IFDIR)
315cbfff13fSKyle Evans return (false);
316f3cf3e59SKyle Evans if (devbehave == DEV_SKIP && (s == S_IFIFO ||
317f3cf3e59SKyle Evans s == S_IFCHR || s == S_IFBLK || s == S_IFSOCK))
318cbfff13fSKyle Evans return (false);
3194dc88ebeSGabor Kovesdan }
3204dc88ebeSGabor Kovesdan f = grep_open(fn);
3214dc88ebeSGabor Kovesdan }
3224dc88ebeSGabor Kovesdan if (f == NULL) {
3236f4cbf7cSGabor Kovesdan file_err = true;
3244dc88ebeSGabor Kovesdan if (!sflag)
3254dc88ebeSGabor Kovesdan warn("%s", fn);
326cbfff13fSKyle Evans return (false);
3274dc88ebeSGabor Kovesdan }
3284dc88ebeSGabor Kovesdan
32966ab2983SKyle Evans pc.ln.file = grep_strdup(fn);
330a4f3f02bSEd Maste pc.ln.line_no = 0;
331a4f3f02bSEd Maste pc.ln.len = 0;
3326d635d3bSEd Maste pc.ln.boff = 0;
333a4f3f02bSEd Maste pc.ln.off = -1;
334a4f3f02bSEd Maste pc.binary = f->binary;
335bd60b9b4SKyle Evans pc.cntlines = false;
33681c634e8SKyle Evans memset(&mc, 0, sizeof(mc));
33781c634e8SKyle Evans mc.printmatch = true;
338e2127de8SEd Maste if ((pc.binary && binbehave == BINFILE_BIN) || cflag || qflag ||
339e2127de8SEd Maste lflag || Lflag)
34081c634e8SKyle Evans mc.printmatch = false;
34181c634e8SKyle Evans if (mc.printmatch && (Aflag != 0 || Bflag != 0))
34281c634e8SKyle Evans mc.doctx = true;
343bd60b9b4SKyle Evans if (mc.printmatch && (Aflag != 0 || Bflag != 0 || mflag || nflag))
344bd60b9b4SKyle Evans pc.cntlines = true;
34583fd8885SEd Maste mcount = mlimit;
34683fd8885SEd Maste
347cbfff13fSKyle Evans for (lines = 0; lines == 0 || !(lflag || qflag); ) {
348d83f17e5SKyle Evans /*
349d83f17e5SKyle Evans * XXX TODO: We need to revisit this in a chunking world. We're
350d83f17e5SKyle Evans * not going to be doing per-line statistics because of the
351d83f17e5SKyle Evans * overhead involved. procmatches can figure that stuff out as
352d83f17e5SKyle Evans * needed. */
3536d635d3bSEd Maste /* Reset per-line statistics */
3546d635d3bSEd Maste pc.printed = 0;
355a4f3f02bSEd Maste pc.matchidx = 0;
356fe8c9d5bSEd Maste pc.lnstart = 0;
3576d635d3bSEd Maste pc.ln.boff = 0;
358a4f3f02bSEd Maste pc.ln.off += pc.ln.len + 1;
359d83f17e5SKyle Evans /* XXX TODO: Grab a chunk */
360bd60b9b4SKyle Evans if ((pc.ln.dat = grep_fgetln(f, &pc)) == NULL ||
3610e957942SKyle Evans pc.ln.len == 0)
3624dc88ebeSGabor Kovesdan break;
363a4f3f02bSEd Maste
364a4f3f02bSEd Maste if (pc.ln.len > 0 && pc.ln.dat[pc.ln.len - 1] == fileeol)
365a4f3f02bSEd Maste --pc.ln.len;
366a4f3f02bSEd Maste pc.ln.line_no++;
3674dc88ebeSGabor Kovesdan
3684dc88ebeSGabor Kovesdan /* Return if we need to skip a binary file */
369a4f3f02bSEd Maste if (pc.binary && binbehave == BINFILE_SKIP) {
3704dc88ebeSGabor Kovesdan grep_close(f);
371a4f3f02bSEd Maste free(pc.ln.file);
3724dc88ebeSGabor Kovesdan free(f);
3734dc88ebeSGabor Kovesdan return (0);
3744dc88ebeSGabor Kovesdan }
37522130a21SEd Maste
3763e2d96acSKyle Evans if (mflag && mcount <= 0) {
3773e2d96acSKyle Evans /*
3783e2d96acSKyle Evans * Short-circuit, already hit match count and now we're
3793e2d96acSKyle Evans * just picking up any remaining pieces.
3803e2d96acSKyle Evans */
3813e2d96acSKyle Evans if (!procmatches(&mc, &pc, false))
3823e2d96acSKyle Evans break;
3833e2d96acSKyle Evans continue;
3843e2d96acSKyle Evans }
38538325e2aSKyle Evans line_matched = procline(&pc) == !vflag;
386be13c0f9SKyle Evans if (line_matched)
387cbfff13fSKyle Evans ++lines;
388a4f3f02bSEd Maste
38981c634e8SKyle Evans /* Halt processing if we hit our match limit */
390be13c0f9SKyle Evans if (!procmatches(&mc, &pc, line_matched))
391fe8c9d5bSEd Maste break;
392fe8c9d5bSEd Maste }
3934dc88ebeSGabor Kovesdan if (Bflag > 0)
3944dc88ebeSGabor Kovesdan clearqueue();
3954dc88ebeSGabor Kovesdan grep_close(f);
3964dc88ebeSGabor Kovesdan
39724c681a7SMariusz Zaborski if (cflag && !qflag) {
3984dc88ebeSGabor Kovesdan if (!hflag)
399a4f3f02bSEd Maste printf("%s:", pc.ln.file);
400cbfff13fSKyle Evans printf("%u\n", lines);
4014dc88ebeSGabor Kovesdan }
402cbfff13fSKyle Evans if (lflag && !qflag && lines != 0)
403f0c94259SGabor Kovesdan printf("%s%c", fn, nullflag ? 0 : '\n');
404cbfff13fSKyle Evans if (Lflag && !qflag && lines == 0)
405f0c94259SGabor Kovesdan printf("%s%c", fn, nullflag ? 0 : '\n');
406cbfff13fSKyle Evans if (lines != 0 && !cflag && !lflag && !Lflag &&
4074dc88ebeSGabor Kovesdan binbehave == BINFILE_BIN && f->binary && !qflag)
40830dc9502SBaptiste Daroussin printf(errstr[7], fn);
4094dc88ebeSGabor Kovesdan
410a4f3f02bSEd Maste free(pc.ln.file);
4114dc88ebeSGabor Kovesdan free(f);
412cbfff13fSKyle Evans return (lines != 0);
4134dc88ebeSGabor Kovesdan }
4144dc88ebeSGabor Kovesdan
41505ad8215SKyle Evans #ifdef WITH_INTERNAL_NOSPEC
41605ad8215SKyle Evans /*
41705ad8215SKyle Evans * Internal implementation of literal string search within a string, modeled
41805ad8215SKyle Evans * after regexec(3), for use when the regex(3) implementation doesn't offer
41905ad8215SKyle Evans * either REG_NOSPEC or REG_LITERAL. This does not apply in the default FreeBSD
42005ad8215SKyle Evans * config, but in other scenarios such as building against libgnuregex or on
42105ad8215SKyle Evans * some non-FreeBSD OSes.
42205ad8215SKyle Evans */
42305ad8215SKyle Evans static int
litexec(const struct pat * pat,const char * string,size_t nmatch,regmatch_t pmatch[])42405ad8215SKyle Evans litexec(const struct pat *pat, const char *string, size_t nmatch,
42505ad8215SKyle Evans regmatch_t pmatch[])
42605ad8215SKyle Evans {
42705ad8215SKyle Evans char *(*strstr_fn)(const char *, const char *);
42805ad8215SKyle Evans char *sub, *subject;
42905ad8215SKyle Evans const char *search;
43005ad8215SKyle Evans size_t idx, n, ofs, stringlen;
43105ad8215SKyle Evans
43205ad8215SKyle Evans if (cflags & REG_ICASE)
43305ad8215SKyle Evans strstr_fn = strcasestr;
43405ad8215SKyle Evans else
43505ad8215SKyle Evans strstr_fn = strstr;
43605ad8215SKyle Evans idx = 0;
43705ad8215SKyle Evans ofs = pmatch[0].rm_so;
43805ad8215SKyle Evans stringlen = pmatch[0].rm_eo;
43905ad8215SKyle Evans if (ofs >= stringlen)
44005ad8215SKyle Evans return (REG_NOMATCH);
44105ad8215SKyle Evans subject = strndup(string, stringlen);
44205ad8215SKyle Evans if (subject == NULL)
44305ad8215SKyle Evans return (REG_ESPACE);
44405ad8215SKyle Evans for (n = 0; ofs < stringlen;) {
44505ad8215SKyle Evans search = (subject + ofs);
44605ad8215SKyle Evans if ((unsigned long)pat->len > strlen(search))
44705ad8215SKyle Evans break;
44805ad8215SKyle Evans sub = strstr_fn(search, pat->pat);
44905ad8215SKyle Evans /*
45005ad8215SKyle Evans * Ignoring the empty string possibility due to context: grep optimizes
45105ad8215SKyle Evans * for empty patterns and will never reach this point.
45205ad8215SKyle Evans */
45305ad8215SKyle Evans if (sub == NULL)
45405ad8215SKyle Evans break;
45505ad8215SKyle Evans ++n;
45605ad8215SKyle Evans /* Fill in pmatch if necessary */
45705ad8215SKyle Evans if (nmatch > 0) {
45805ad8215SKyle Evans pmatch[idx].rm_so = ofs + (sub - search);
45905ad8215SKyle Evans pmatch[idx].rm_eo = pmatch[idx].rm_so + pat->len;
46005ad8215SKyle Evans if (++idx == nmatch)
46105ad8215SKyle Evans break;
46205ad8215SKyle Evans ofs = pmatch[idx].rm_so + 1;
46305ad8215SKyle Evans } else
46405ad8215SKyle Evans /* We only needed to know if we match or not */
46505ad8215SKyle Evans break;
46605ad8215SKyle Evans }
46705ad8215SKyle Evans free(subject);
46805ad8215SKyle Evans if (n > 0 && nmatch > 0)
46905ad8215SKyle Evans for (n = idx; n < nmatch; ++n)
47005ad8215SKyle Evans pmatch[n].rm_so = pmatch[n].rm_eo = -1;
47105ad8215SKyle Evans
47205ad8215SKyle Evans return (n > 0 ? 0 : REG_NOMATCH);
47305ad8215SKyle Evans }
47405ad8215SKyle Evans #endif /* WITH_INTERNAL_NOSPEC */
47505ad8215SKyle Evans
4764dc88ebeSGabor Kovesdan #define iswword(x) (iswalnum((x)) || (x) == L'_')
4774dc88ebeSGabor Kovesdan
4784dc88ebeSGabor Kovesdan /*
4794dc88ebeSGabor Kovesdan * Processes a line comparing it with the specified patterns. Each pattern
4804dc88ebeSGabor Kovesdan * is looped to be compared along with the full string, saving each and every
4814dc88ebeSGabor Kovesdan * match, which is necessary to colorize the output and to count the
4824dc88ebeSGabor Kovesdan * matches. The matching lines are passed to printline() to display the
4834dc88ebeSGabor Kovesdan * appropriate output.
4844dc88ebeSGabor Kovesdan */
485be13c0f9SKyle Evans static bool
procline(struct parsec * pc)486a4f3f02bSEd Maste procline(struct parsec *pc)
4874dc88ebeSGabor Kovesdan {
488a4f3f02bSEd Maste regmatch_t pmatch, lastmatch, chkmatch;
489a4f3f02bSEd Maste wchar_t wbegin, wend;
490fe8c9d5bSEd Maste size_t st, nst;
4914dc88ebeSGabor Kovesdan unsigned int i;
492be13c0f9SKyle Evans int r = 0, leflags = eflags;
493a4f3f02bSEd Maste size_t startm = 0, matchidx;
494fe8c9d5bSEd Maste unsigned int retry;
495be13c0f9SKyle Evans bool lastmatched, matched;
4964dc88ebeSGabor Kovesdan
497a4f3f02bSEd Maste matchidx = pc->matchidx;
498a4f3f02bSEd Maste
499f823c6dcSKyle Evans /* Null pattern shortcuts. */
50038325e2aSKyle Evans if (matchall) {
501f823c6dcSKyle Evans if (xflag && pc->ln.len == 0) {
502f823c6dcSKyle Evans /* Matches empty lines (-x). */
503be13c0f9SKyle Evans return (true);
504f823c6dcSKyle Evans } else if (!wflag && !xflag) {
505f823c6dcSKyle Evans /* Matches every line (no -w or -x). */
50638325e2aSKyle Evans return (true);
507f823c6dcSKyle Evans }
50838325e2aSKyle Evans
50938325e2aSKyle Evans /*
510f823c6dcSKyle Evans * If we only have the NULL pattern, whether we match or not
511f823c6dcSKyle Evans * depends on if we got here with -w or -x. If either is set,
512f823c6dcSKyle Evans * the answer is no. If we have other patterns, we'll defer
513f823c6dcSKyle Evans * to them.
51438325e2aSKyle Evans */
515f823c6dcSKyle Evans if (patterns == 0) {
516f823c6dcSKyle Evans return (!(wflag || xflag));
517f823c6dcSKyle Evans }
518f823c6dcSKyle Evans } else if (patterns == 0) {
519f823c6dcSKyle Evans /* Pattern file with no patterns. */
520be13c0f9SKyle Evans return (false);
52138325e2aSKyle Evans }
522a4f3f02bSEd Maste
523be13c0f9SKyle Evans matched = false;
524fe8c9d5bSEd Maste st = pc->lnstart;
525fe8c9d5bSEd Maste nst = 0;
526a734ae9cSEd Maste /* Initialize to avoid a false positive warning from GCC. */
527a734ae9cSEd Maste lastmatch.rm_so = lastmatch.rm_eo = 0;
528a734ae9cSEd Maste
5294dc88ebeSGabor Kovesdan /* Loop to process the whole line */
530a4f3f02bSEd Maste while (st <= pc->ln.len) {
531be13c0f9SKyle Evans lastmatched = false;
532a4f3f02bSEd Maste startm = matchidx;
533945fc991SEd Maste retry = 0;
5348bf46064SEd Maste if (st > 0 && pc->ln.dat[st - 1] != fileeol)
53587c485cfSEd Maste leflags |= REG_NOTBOL;
5364dc88ebeSGabor Kovesdan /* Loop to compare with all the patterns */
5374dc88ebeSGabor Kovesdan for (i = 0; i < patterns; i++) {
53887c485cfSEd Maste pmatch.rm_so = st;
539a4f3f02bSEd Maste pmatch.rm_eo = pc->ln.len;
54005ad8215SKyle Evans #ifdef WITH_INTERNAL_NOSPEC
54105ad8215SKyle Evans if (grepbehave == GREP_FIXED)
54205ad8215SKyle Evans r = litexec(&pattern[i], pc->ln.dat, 1, &pmatch);
54305ad8215SKyle Evans else
54405ad8215SKyle Evans #endif
545a2584d1bSKyle Evans r = regexec(&r_pattern[i], pc->ln.dat, 1, &pmatch,
546a2584d1bSKyle Evans leflags);
547a4f3f02bSEd Maste if (r != 0)
5484dc88ebeSGabor Kovesdan continue;
5494dc88ebeSGabor Kovesdan /* Check for full match */
550a4f3f02bSEd Maste if (xflag && (pmatch.rm_so != 0 ||
551a4f3f02bSEd Maste (size_t)pmatch.rm_eo != pc->ln.len))
552a4f3f02bSEd Maste continue;
5534dc88ebeSGabor Kovesdan /* Check for whole word match */
554a4f3f02bSEd Maste if (wflag) {
55559218eb7SGabor Kovesdan wbegin = wend = L' ';
55659218eb7SGabor Kovesdan if (pmatch.rm_so != 0 &&
557a4f3f02bSEd Maste sscanf(&pc->ln.dat[pmatch.rm_so - 1],
55859218eb7SGabor Kovesdan "%lc", &wbegin) != 1)
5594dc88ebeSGabor Kovesdan r = REG_NOMATCH;
560cbe6b9e5SGabor Kovesdan else if ((size_t)pmatch.rm_eo !=
561a4f3f02bSEd Maste pc->ln.len &&
562a4f3f02bSEd Maste sscanf(&pc->ln.dat[pmatch.rm_eo],
5634dc88ebeSGabor Kovesdan "%lc", &wend) != 1)
5644dc88ebeSGabor Kovesdan r = REG_NOMATCH;
565cbe6b9e5SGabor Kovesdan else if (iswword(wbegin) ||
566cbe6b9e5SGabor Kovesdan iswword(wend))
5674dc88ebeSGabor Kovesdan r = REG_NOMATCH;
568945fc991SEd Maste /*
569945fc991SEd Maste * If we're doing whole word matching and we
570945fc991SEd Maste * matched once, then we should try the pattern
571945fc991SEd Maste * again after advancing just past the start of
572945fc991SEd Maste * the earliest match. This allows the pattern
573945fc991SEd Maste * to match later on in the line and possibly
574945fc991SEd Maste * still match a whole word.
575945fc991SEd Maste */
576945fc991SEd Maste if (r == REG_NOMATCH &&
577fe8c9d5bSEd Maste (retry == pc->lnstart ||
578de3d7a82SKyle Evans (unsigned int)pmatch.rm_so + 1 < retry))
579945fc991SEd Maste retry = pmatch.rm_so + 1;
580a4f3f02bSEd Maste if (r == REG_NOMATCH)
581a4f3f02bSEd Maste continue;
5824dc88ebeSGabor Kovesdan }
583be13c0f9SKyle Evans lastmatched = true;
58487c485cfSEd Maste lastmatch = pmatch;
585a4f3f02bSEd Maste
586a4f3f02bSEd Maste if (matchidx == 0)
587be13c0f9SKyle Evans matched = true;
58887c485cfSEd Maste
589a4f3f02bSEd Maste /*
590a4f3f02bSEd Maste * Replace previous match if the new one is earlier
591a4f3f02bSEd Maste * and/or longer. This will lead to some amount of
592a4f3f02bSEd Maste * extra work if -o/--color are specified, but it's
593a4f3f02bSEd Maste * worth it from a correctness point of view.
594a4f3f02bSEd Maste */
595a4f3f02bSEd Maste if (matchidx > startm) {
596a4f3f02bSEd Maste chkmatch = pc->matches[matchidx - 1];
597a4f3f02bSEd Maste if (pmatch.rm_so < chkmatch.rm_so ||
598a4f3f02bSEd Maste (pmatch.rm_so == chkmatch.rm_so &&
599a4f3f02bSEd Maste (pmatch.rm_eo - pmatch.rm_so) >
600a4f3f02bSEd Maste (chkmatch.rm_eo - chkmatch.rm_so))) {
601a4f3f02bSEd Maste pc->matches[matchidx - 1] = pmatch;
60287c485cfSEd Maste nst = pmatch.rm_eo;
60387c485cfSEd Maste }
60487c485cfSEd Maste } else {
60587c485cfSEd Maste /* Advance as normal if not */
606a4f3f02bSEd Maste pc->matches[matchidx++] = pmatch;
60787c485cfSEd Maste nst = pmatch.rm_eo;
60887c485cfSEd Maste }
609a4f3f02bSEd Maste /* avoid excessive matching - skip further patterns */
610a4f3f02bSEd Maste if ((color == NULL && !oflag) || qflag || lflag ||
6118bf46064SEd Maste matchidx >= MAX_MATCHES) {
612fe8c9d5bSEd Maste pc->lnstart = nst;
613be13c0f9SKyle Evans lastmatched = false;
6144dc88ebeSGabor Kovesdan break;
6154dc88ebeSGabor Kovesdan }
616fe8c9d5bSEd Maste }
6174dc88ebeSGabor Kovesdan
618945fc991SEd Maste /*
619945fc991SEd Maste * Advance to just past the start of the earliest match, try
620945fc991SEd Maste * again just in case we still have a chance to match later in
621945fc991SEd Maste * the string.
622945fc991SEd Maste */
623be13c0f9SKyle Evans if (!lastmatched && retry > pc->lnstart) {
624945fc991SEd Maste st = retry;
625945fc991SEd Maste continue;
6264dc88ebeSGabor Kovesdan }
627f20f6f3fSGabor Kovesdan
628d83f17e5SKyle Evans /* XXX TODO: We will need to keep going, since we're chunky */
6294dc88ebeSGabor Kovesdan /* One pass if we are not recording matches */
6302ac5b1c7SGabor Kovesdan if (!wflag && ((color == NULL && !oflag) || qflag || lflag || Lflag))
6314dc88ebeSGabor Kovesdan break;
6324dc88ebeSGabor Kovesdan
63387c485cfSEd Maste /* If we didn't have any matches or REG_NOSUB set */
634be13c0f9SKyle Evans if (!lastmatched || (cflags & REG_NOSUB))
635a4f3f02bSEd Maste nst = pc->ln.len;
63687c485cfSEd Maste
637be13c0f9SKyle Evans if (!lastmatched)
63887c485cfSEd Maste /* No matches */
63987c485cfSEd Maste break;
64087c485cfSEd Maste else if (st == nst && lastmatch.rm_so == lastmatch.rm_eo)
64187c485cfSEd Maste /* Zero-length match -- advance one more so we don't get stuck */
64287c485cfSEd Maste nst++;
64387c485cfSEd Maste
64487c485cfSEd Maste /* Advance st based on previous matches */
64587c485cfSEd Maste st = nst;
646fe8c9d5bSEd Maste pc->lnstart = st;
6474dc88ebeSGabor Kovesdan }
648f20f6f3fSGabor Kovesdan
649a4f3f02bSEd Maste /* Reflect the new matchidx in the context */
650a4f3f02bSEd Maste pc->matchidx = matchidx;
651be13c0f9SKyle Evans return matched;
6524dc88ebeSGabor Kovesdan }
6534dc88ebeSGabor Kovesdan
6544dc88ebeSGabor Kovesdan /*
6554dc88ebeSGabor Kovesdan * Safe malloc() for internal use.
6564dc88ebeSGabor Kovesdan */
6574dc88ebeSGabor Kovesdan void *
grep_malloc(size_t size)6584dc88ebeSGabor Kovesdan grep_malloc(size_t size)
6594dc88ebeSGabor Kovesdan {
6604dc88ebeSGabor Kovesdan void *ptr;
6614dc88ebeSGabor Kovesdan
662e116e040SKyle Evans if (size == 0)
663e116e040SKyle Evans return (NULL);
6644dc88ebeSGabor Kovesdan if ((ptr = malloc(size)) == NULL)
6654dc88ebeSGabor Kovesdan err(2, "malloc");
6664dc88ebeSGabor Kovesdan return (ptr);
6674dc88ebeSGabor Kovesdan }
6684dc88ebeSGabor Kovesdan
6694dc88ebeSGabor Kovesdan /*
6704dc88ebeSGabor Kovesdan * Safe calloc() for internal use.
6714dc88ebeSGabor Kovesdan */
6724dc88ebeSGabor Kovesdan void *
grep_calloc(size_t nmemb,size_t size)6734dc88ebeSGabor Kovesdan grep_calloc(size_t nmemb, size_t size)
6744dc88ebeSGabor Kovesdan {
6754dc88ebeSGabor Kovesdan void *ptr;
6764dc88ebeSGabor Kovesdan
677e116e040SKyle Evans if (nmemb == 0 || size == 0)
678e116e040SKyle Evans return (NULL);
6794dc88ebeSGabor Kovesdan if ((ptr = calloc(nmemb, size)) == NULL)
6804dc88ebeSGabor Kovesdan err(2, "calloc");
6814dc88ebeSGabor Kovesdan return (ptr);
6824dc88ebeSGabor Kovesdan }
6834dc88ebeSGabor Kovesdan
6844dc88ebeSGabor Kovesdan /*
6854dc88ebeSGabor Kovesdan * Safe realloc() for internal use.
6864dc88ebeSGabor Kovesdan */
6874dc88ebeSGabor Kovesdan void *
grep_realloc(void * ptr,size_t size)6884dc88ebeSGabor Kovesdan grep_realloc(void *ptr, size_t size)
6894dc88ebeSGabor Kovesdan {
6904dc88ebeSGabor Kovesdan
6914dc88ebeSGabor Kovesdan if ((ptr = realloc(ptr, size)) == NULL)
6924dc88ebeSGabor Kovesdan err(2, "realloc");
6934dc88ebeSGabor Kovesdan return (ptr);
6944dc88ebeSGabor Kovesdan }
6954dc88ebeSGabor Kovesdan
6964dc88ebeSGabor Kovesdan /*
69755e44f51SGabor Kovesdan * Safe strdup() for internal use.
69855e44f51SGabor Kovesdan */
69955e44f51SGabor Kovesdan char *
grep_strdup(const char * str)70055e44f51SGabor Kovesdan grep_strdup(const char *str)
70155e44f51SGabor Kovesdan {
70255e44f51SGabor Kovesdan char *ret;
70355e44f51SGabor Kovesdan
70455e44f51SGabor Kovesdan if ((ret = strdup(str)) == NULL)
70555e44f51SGabor Kovesdan err(2, "strdup");
70655e44f51SGabor Kovesdan return (ret);
70755e44f51SGabor Kovesdan }
70855e44f51SGabor Kovesdan
70955e44f51SGabor Kovesdan /*
710a4f3f02bSEd Maste * Print an entire line as-is, there are no inline matches to consider. This is
711a4f3f02bSEd Maste * used for printing context.
7124dc88ebeSGabor Kovesdan */
grep_printline(struct str * line,int sep)713a4f3f02bSEd Maste void grep_printline(struct str *line, int sep) {
714a4f3f02bSEd Maste printline_metadata(line, sep);
715a4f3f02bSEd Maste fwrite(line->dat, line->len, 1, stdout);
716a4f3f02bSEd Maste putchar(fileeol);
717a4f3f02bSEd Maste }
718a4f3f02bSEd Maste
719a4f3f02bSEd Maste static void
printline_metadata(struct str * line,int sep)720a4f3f02bSEd Maste printline_metadata(struct str *line, int sep)
7214dc88ebeSGabor Kovesdan {
722a4f3f02bSEd Maste bool printsep;
7234dc88ebeSGabor Kovesdan
724a4f3f02bSEd Maste printsep = false;
7254dc88ebeSGabor Kovesdan if (!hflag) {
726f0c94259SGabor Kovesdan if (!nullflag) {
7274dc88ebeSGabor Kovesdan fputs(line->file, stdout);
728a4f3f02bSEd Maste printsep = true;
729f0c94259SGabor Kovesdan } else {
7304dc88ebeSGabor Kovesdan printf("%s", line->file);
7314dc88ebeSGabor Kovesdan putchar(0);
7324dc88ebeSGabor Kovesdan }
7334dc88ebeSGabor Kovesdan }
7344dc88ebeSGabor Kovesdan if (nflag) {
735a4f3f02bSEd Maste if (printsep)
7364dc88ebeSGabor Kovesdan putchar(sep);
7374dc88ebeSGabor Kovesdan printf("%d", line->line_no);
738a4f3f02bSEd Maste printsep = true;
7394dc88ebeSGabor Kovesdan }
7404dc88ebeSGabor Kovesdan if (bflag) {
741a4f3f02bSEd Maste if (printsep)
7424dc88ebeSGabor Kovesdan putchar(sep);
7436d635d3bSEd Maste printf("%lld", (long long)(line->off + line->boff));
744a4f3f02bSEd Maste printsep = true;
7454dc88ebeSGabor Kovesdan }
746a4f3f02bSEd Maste if (printsep)
7474dc88ebeSGabor Kovesdan putchar(sep);
748a4f3f02bSEd Maste }
749a4f3f02bSEd Maste
750a4f3f02bSEd Maste /*
751a4f3f02bSEd Maste * Prints a matching line according to the command line options.
752a4f3f02bSEd Maste */
753a4f3f02bSEd Maste static void
printline(struct parsec * pc,int sep)754a4f3f02bSEd Maste printline(struct parsec *pc, int sep)
755a4f3f02bSEd Maste {
756a4f3f02bSEd Maste size_t a = 0;
757a4f3f02bSEd Maste size_t i, matchidx;
758a4f3f02bSEd Maste regmatch_t match;
759a4f3f02bSEd Maste
760a4f3f02bSEd Maste /* If matchall, everything matches but don't actually print for -o */
761a4f3f02bSEd Maste if (oflag && matchall)
762a4f3f02bSEd Maste return;
763a4f3f02bSEd Maste
764a4f3f02bSEd Maste matchidx = pc->matchidx;
765a4f3f02bSEd Maste
7664dc88ebeSGabor Kovesdan /* --color and -o */
767a4f3f02bSEd Maste if ((oflag || color) && matchidx > 0) {
7686d635d3bSEd Maste /* Only print metadata once per line if --color */
7696d635d3bSEd Maste if (!oflag && pc->printed == 0)
770a4f3f02bSEd Maste printline_metadata(&pc->ln, sep);
771a4f3f02bSEd Maste for (i = 0; i < matchidx; i++) {
772a4f3f02bSEd Maste match = pc->matches[i];
773e06ffa32SEd Maste /* Don't output zero length matches */
774a4f3f02bSEd Maste if (match.rm_so == match.rm_eo)
775e06ffa32SEd Maste continue;
7766d635d3bSEd Maste /*
7776d635d3bSEd Maste * Metadata is printed on a per-line basis, so every
7786d635d3bSEd Maste * match gets file metadata with the -o flag.
7796d635d3bSEd Maste */
7806d635d3bSEd Maste if (oflag) {
7816d635d3bSEd Maste pc->ln.boff = match.rm_so;
7826d635d3bSEd Maste printline_metadata(&pc->ln, sep);
7836d635d3bSEd Maste } else
784a4f3f02bSEd Maste fwrite(pc->ln.dat + a, match.rm_so - a, 1,
7854dc88ebeSGabor Kovesdan stdout);
7864dc88ebeSGabor Kovesdan if (color)
787a5ed8685SEd Maste fprintf(stdout, "\33[%sm\33[K", color);
788a4f3f02bSEd Maste fwrite(pc->ln.dat + match.rm_so,
789a4f3f02bSEd Maste match.rm_eo - match.rm_so, 1, stdout);
7904dc88ebeSGabor Kovesdan if (color)
791a5ed8685SEd Maste fprintf(stdout, "\33[m\33[K");
792a4f3f02bSEd Maste a = match.rm_eo;
7934dc88ebeSGabor Kovesdan if (oflag)
7944dc88ebeSGabor Kovesdan putchar('\n');
7954dc88ebeSGabor Kovesdan }
7964dc88ebeSGabor Kovesdan if (!oflag) {
797a4f3f02bSEd Maste if (pc->ln.len - a > 0)
798a4f3f02bSEd Maste fwrite(pc->ln.dat + a, pc->ln.len - a, 1,
799a4f3f02bSEd Maste stdout);
8004dc88ebeSGabor Kovesdan putchar('\n');
8014dc88ebeSGabor Kovesdan }
802a4f3f02bSEd Maste } else
803a4f3f02bSEd Maste grep_printline(&pc->ln, sep);
8046d635d3bSEd Maste pc->printed++;
8054dc88ebeSGabor Kovesdan }
806