2018-02-06 01:14:26 +03:00
|
|
|
/* $NetBSD: util.c,v 1.19 2018/02/05 22:14:26 mrg Exp $ */
|
2011-02-16 04:31:33 +03:00
|
|
|
/* $FreeBSD: head/usr.bin/grep/util.c 211496 2010-08-19 09:28:59Z des $ */
|
|
|
|
/* $OpenBSD: util.c,v 1.39 2010/07/02 22:18:03 tedu Exp $ */
|
2004-01-02 18:00:29 +03:00
|
|
|
|
2004-01-02 17:58:43 +03:00
|
|
|
/*-
|
2011-02-16 04:31:33 +03:00
|
|
|
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
|
|
|
|
* Copyright (C) 2008-2010 Gabor Kovesdan <gabor@FreeBSD.org>
|
2004-01-02 17:58:43 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2011-04-18 07:27:40 +04:00
|
|
|
#if HAVE_NBTOOL_CONFIG_H
|
|
|
|
#include "nbtool_config.h"
|
|
|
|
#endif
|
|
|
|
|
2004-01-02 18:00:29 +03:00
|
|
|
#include <sys/cdefs.h>
|
2018-02-06 01:14:26 +03:00
|
|
|
__RCSID("$NetBSD: util.c,v 1.19 2018/02/05 22:14:26 mrg Exp $");
|
2004-01-02 18:00:29 +03:00
|
|
|
|
2004-01-02 17:58:43 +03:00
|
|
|
#include <sys/stat.h>
|
2011-02-16 04:31:33 +03:00
|
|
|
#include <sys/types.h>
|
2004-01-02 17:58:43 +03:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2011-02-16 04:31:33 +03:00
|
|
|
#include <fnmatch.h>
|
2004-01-02 17:58:43 +03:00
|
|
|
#include <fts.h>
|
2011-02-16 04:31:33 +03:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <stdbool.h>
|
2004-01-02 17:58:43 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2011-02-16 04:31:33 +03:00
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
2004-01-02 17:58:43 +03:00
|
|
|
|
|
|
|
#include "grep.h"
|
|
|
|
|
2011-04-18 21:18:03 +04:00
|
|
|
static bool first, first_global = true;
|
|
|
|
static unsigned long long since_printed;
|
|
|
|
|
2011-02-16 04:31:33 +03:00
|
|
|
static int procline(struct str *l, int);
|
|
|
|
|
|
|
|
bool
|
|
|
|
file_matching(const char *fname)
|
|
|
|
{
|
2011-02-16 21:35:39 +03:00
|
|
|
char *fname_base, *fname_copy;
|
2011-04-19 03:22:42 +04:00
|
|
|
unsigned int i;
|
2011-02-16 04:31:33 +03:00
|
|
|
bool ret;
|
|
|
|
|
|
|
|
ret = finclude ? false : true;
|
2011-02-16 21:35:39 +03:00
|
|
|
fname_copy = grep_strdup(fname);
|
|
|
|
fname_base = basename(fname_copy);
|
2011-02-16 04:31:33 +03:00
|
|
|
|
2011-04-19 03:22:42 +04:00
|
|
|
for (i = 0; i < fpatterns; ++i) {
|
2011-02-16 21:35:39 +03:00
|
|
|
if (fnmatch(fpattern[i].pat, fname, 0) == 0 ||
|
|
|
|
fnmatch(fpattern[i].pat, fname_base, 0) == 0) {
|
2013-01-21 07:24:43 +04:00
|
|
|
if (fpattern[i].mode == EXCL_PAT) {
|
|
|
|
free(fname_copy);
|
2011-02-16 04:31:33 +03:00
|
|
|
return (false);
|
2013-01-21 07:24:43 +04:00
|
|
|
} else
|
2011-02-16 04:31:33 +03:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
2011-02-16 21:35:39 +03:00
|
|
|
free(fname_copy);
|
2011-02-16 04:31:33 +03:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
dir_matching(const char *dname)
|
|
|
|
{
|
2011-04-19 03:22:42 +04:00
|
|
|
unsigned int i;
|
2011-02-16 04:31:33 +03:00
|
|
|
bool ret;
|
|
|
|
|
|
|
|
ret = dinclude ? false : true;
|
2004-01-02 17:58:43 +03:00
|
|
|
|
2011-04-19 03:22:42 +04:00
|
|
|
for (i = 0; i < dpatterns; ++i) {
|
2011-02-16 04:31:33 +03:00
|
|
|
if (dname != NULL &&
|
|
|
|
fnmatch(dname, dpattern[i].pat, 0) == 0) {
|
|
|
|
if (dpattern[i].mode == EXCL_PAT)
|
|
|
|
return (false);
|
|
|
|
else
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (ret);
|
|
|
|
}
|
2004-01-02 17:58:43 +03:00
|
|
|
|
2011-02-16 04:31:33 +03:00
|
|
|
/*
|
|
|
|
* Processes a directory when a recursive search is performed with
|
|
|
|
* the -R option. Each appropriate file is passed to procfile().
|
|
|
|
*/
|
|
|
|
int
|
2004-01-02 17:58:43 +03:00
|
|
|
grep_tree(char **argv)
|
|
|
|
{
|
2004-01-02 18:00:29 +03:00
|
|
|
FTS *fts;
|
|
|
|
FTSENT *p;
|
2011-02-16 04:31:33 +03:00
|
|
|
char *d, *dir = NULL;
|
2004-01-02 18:00:29 +03:00
|
|
|
int c, fts_flags;
|
2011-02-16 04:31:33 +03:00
|
|
|
bool ok;
|
2004-01-02 17:58:43 +03:00
|
|
|
|
|
|
|
c = fts_flags = 0;
|
|
|
|
|
2011-02-16 04:31:33 +03:00
|
|
|
switch(linkbehave) {
|
|
|
|
case LINK_EXPLICIT:
|
2004-01-02 17:58:43 +03:00
|
|
|
fts_flags = FTS_COMFOLLOW;
|
2011-02-16 04:31:33 +03:00
|
|
|
break;
|
|
|
|
case LINK_SKIP:
|
2004-01-02 17:58:43 +03:00
|
|
|
fts_flags = FTS_PHYSICAL;
|
2011-02-16 04:31:33 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fts_flags = FTS_LOGICAL;
|
|
|
|
|
|
|
|
}
|
2004-01-02 17:58:43 +03:00
|
|
|
|
2011-02-16 04:31:33 +03:00
|
|
|
fts_flags |= FTS_NOSTAT | FTS_NOCHDIR;
|
2004-01-02 17:58:43 +03:00
|
|
|
|
2011-02-16 04:31:33 +03:00
|
|
|
if (!(fts = fts_open(argv, fts_flags, NULL)))
|
|
|
|
err(2, "fts_open");
|
2004-01-02 17:58:43 +03:00
|
|
|
while ((p = fts_read(fts)) != NULL) {
|
|
|
|
switch (p->fts_info) {
|
|
|
|
case FTS_DNR:
|
2011-02-16 04:31:33 +03:00
|
|
|
/* FALLTHROUGH */
|
2004-01-02 17:58:43 +03:00
|
|
|
case FTS_ERR:
|
2004-01-02 18:00:29 +03:00
|
|
|
errx(2, "%s: %s", p->fts_path, strerror(p->fts_errno));
|
2004-01-02 17:58:43 +03:00
|
|
|
break;
|
2004-01-02 18:00:29 +03:00
|
|
|
case FTS_D:
|
2011-02-16 04:31:33 +03:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case FTS_DP:
|
2004-01-02 18:00:29 +03:00
|
|
|
break;
|
|
|
|
case FTS_DC:
|
2011-02-16 04:31:33 +03:00
|
|
|
/* Print a warning for recursive directory loop */
|
2006-01-13 23:30:40 +03:00
|
|
|
warnx("warning: %s: recursive directory loop",
|
2004-01-02 18:00:29 +03:00
|
|
|
p->fts_path);
|
2004-01-02 17:58:43 +03:00
|
|
|
break;
|
|
|
|
default:
|
2011-02-16 04:31:33 +03:00
|
|
|
/* Check for file exclusion/inclusion */
|
|
|
|
ok = true;
|
|
|
|
if (dexclude || dinclude) {
|
|
|
|
if ((d = strrchr(p->fts_path, '/')) != NULL) {
|
|
|
|
dir = grep_malloc(sizeof(char) *
|
|
|
|
(d - p->fts_path + 1));
|
|
|
|
memcpy(dir, p->fts_path,
|
|
|
|
d - p->fts_path);
|
|
|
|
dir[d - p->fts_path] = '\0';
|
|
|
|
}
|
|
|
|
ok = dir_matching(dir);
|
|
|
|
free(dir);
|
|
|
|
dir = NULL;
|
|
|
|
}
|
|
|
|
if (fexclude || finclude)
|
|
|
|
ok &= file_matching(p->fts_path);
|
|
|
|
|
|
|
|
if (ok)
|
|
|
|
c += procfile(p->fts_path);
|
2004-01-02 17:58:43 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-16 04:31:33 +03:00
|
|
|
fts_close(fts);
|
|
|
|
return (c);
|
2004-01-02 17:58:43 +03:00
|
|
|
}
|
|
|
|
|
2011-02-16 04:31:33 +03:00
|
|
|
/*
|
|
|
|
* Opens a file and processes it. Each file is processed line-by-line
|
|
|
|
* passing the lines to procline().
|
|
|
|
*/
|
2004-01-02 17:58:43 +03:00
|
|
|
int
|
2006-05-16 01:12:21 +04:00
|
|
|
procfile(const char *fn)
|
2004-01-02 17:58:43 +03:00
|
|
|
{
|
2011-02-16 04:31:33 +03:00
|
|
|
struct file *f;
|
2004-01-02 18:00:29 +03:00
|
|
|
struct stat sb;
|
2011-02-16 04:31:33 +03:00
|
|
|
struct str ln;
|
2004-01-02 18:00:29 +03:00
|
|
|
mode_t s;
|
2011-02-16 04:31:33 +03:00
|
|
|
int c, t;
|
2004-01-02 17:58:43 +03:00
|
|
|
|
2011-02-16 04:31:33 +03:00
|
|
|
if (mflag && (mcount <= 0))
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (strcmp(fn, "-") == 0) {
|
|
|
|
fn = label != NULL ? label : getstr(1);
|
|
|
|
f = grep_open(NULL);
|
2004-01-02 17:58:43 +03:00
|
|
|
} else {
|
2011-02-16 04:31:33 +03:00
|
|
|
if (!stat(fn, &sb)) {
|
|
|
|
/* Check if we need to process the file */
|
|
|
|
s = sb.st_mode & S_IFMT;
|
|
|
|
if (s == S_IFDIR && dirbehave == DIR_SKIP)
|
|
|
|
return (0);
|
|
|
|
if ((s == S_IFIFO || s == S_IFCHR || s == S_IFBLK
|
|
|
|
|| s == S_IFSOCK) && devbehave == DEV_SKIP)
|
|
|
|
return (0);
|
2004-01-02 18:00:29 +03:00
|
|
|
}
|
2011-02-16 04:31:33 +03:00
|
|
|
f = grep_open(fn);
|
2004-01-02 17:58:43 +03:00
|
|
|
}
|
|
|
|
if (f == NULL) {
|
|
|
|
if (!sflag)
|
|
|
|
warn("%s", fn);
|
2011-02-16 04:31:33 +03:00
|
|
|
if (errno == ENOENT)
|
|
|
|
notfound = true;
|
|
|
|
return (0);
|
2004-01-02 17:58:43 +03:00
|
|
|
}
|
2004-01-02 18:00:29 +03:00
|
|
|
|
2011-02-16 04:31:33 +03:00
|
|
|
ln.file = grep_malloc(strlen(fn) + 1);
|
|
|
|
strcpy(ln.file, fn);
|
2004-01-02 17:58:43 +03:00
|
|
|
ln.line_no = 0;
|
2011-02-16 04:31:33 +03:00
|
|
|
ln.len = 0;
|
|
|
|
tail = 0;
|
2004-01-02 17:58:43 +03:00
|
|
|
ln.off = -1;
|
|
|
|
|
2011-04-18 21:18:03 +04:00
|
|
|
for (first = true, c = 0; c == 0 || !(lflag || qflag); ) {
|
2004-01-02 17:58:43 +03:00
|
|
|
ln.off += ln.len + 1;
|
2012-05-07 02:27:00 +04:00
|
|
|
if ((ln.dat = grep_fgetln(f, &ln.len)) == NULL || ln.len == 0)
|
|
|
|
break;
|
2011-04-19 02:46:48 +04:00
|
|
|
if (ln.len > 0 && ln.dat[ln.len - 1] == line_sep)
|
2004-01-02 17:58:43 +03:00
|
|
|
--ln.len;
|
|
|
|
ln.line_no++;
|
|
|
|
|
2011-02-16 04:31:33 +03:00
|
|
|
/* Return if we need to skip a binary file */
|
|
|
|
if (f->binary && binbehave == BINFILE_SKIP) {
|
|
|
|
grep_close(f);
|
|
|
|
free(ln.file);
|
|
|
|
free(f);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
/* Process the file line-by-line */
|
2011-04-18 21:18:03 +04:00
|
|
|
t = procline(&ln, f->binary);
|
2004-01-02 17:58:43 +03:00
|
|
|
c += t;
|
2011-02-16 04:31:33 +03:00
|
|
|
|
|
|
|
/* Count the matches if we have a match limit */
|
|
|
|
if (mflag) {
|
|
|
|
mcount -= t;
|
|
|
|
if (mcount <= 0)
|
|
|
|
break;
|
|
|
|
}
|
2004-01-02 17:58:43 +03:00
|
|
|
}
|
|
|
|
if (Bflag > 0)
|
|
|
|
clearqueue();
|
|
|
|
grep_close(f);
|
|
|
|
|
|
|
|
if (cflag) {
|
2011-02-16 04:31:33 +03:00
|
|
|
if (!hflag)
|
|
|
|
printf("%s:", ln.file);
|
2011-04-19 02:46:48 +04:00
|
|
|
printf("%u%c", c, line_sep);
|
2011-02-16 04:31:33 +03:00
|
|
|
}
|
|
|
|
if (lflag && !qflag && c != 0)
|
2011-04-19 02:46:48 +04:00
|
|
|
printf("%s%c", fn, line_sep);
|
2011-02-16 04:31:33 +03:00
|
|
|
if (Lflag && !qflag && c == 0)
|
2011-04-19 02:46:48 +04:00
|
|
|
printf("%s%c", fn, line_sep);
|
2011-02-16 04:31:33 +03:00
|
|
|
if (c && !cflag && !lflag && !Lflag &&
|
|
|
|
binbehave == BINFILE_BIN && f->binary && !qflag)
|
|
|
|
printf(getstr(8), fn);
|
|
|
|
|
|
|
|
free(ln.file);
|
|
|
|
free(f);
|
|
|
|
return (c);
|
2004-01-02 17:58:43 +03:00
|
|
|
}
|
|
|
|
|
2011-02-16 04:31:33 +03:00
|
|
|
#define iswword(x) (iswalnum((x)) || (x) == L'_')
|
2004-01-02 17:58:43 +03:00
|
|
|
|
|
|
|
/*
|
2011-02-16 04:31:33 +03:00
|
|
|
* Processes a line comparing it with the specified patterns. Each pattern
|
|
|
|
* is looped to be compared along with the full string, saving each and every
|
|
|
|
* match, which is necessary to colorize the output and to count the
|
|
|
|
* matches. The matching lines are passed to printline() to display the
|
|
|
|
* appropriate output.
|
2004-01-02 17:58:43 +03:00
|
|
|
*/
|
2011-02-16 21:35:39 +03:00
|
|
|
static int
|
2011-02-16 04:31:33 +03:00
|
|
|
procline(struct str *l, int nottext)
|
2004-01-02 17:58:43 +03:00
|
|
|
{
|
2004-01-02 18:00:29 +03:00
|
|
|
regmatch_t matches[MAX_LINE_MATCHES];
|
2011-02-16 04:31:33 +03:00
|
|
|
regmatch_t pmatch;
|
|
|
|
size_t st = 0;
|
|
|
|
unsigned int i;
|
|
|
|
int c = 0, m = 0, r = 0;
|
|
|
|
|
2012-05-07 01:56:08 +04:00
|
|
|
/* Loop to process the whole line */
|
|
|
|
while (st <= l->len) {
|
|
|
|
pmatch.rm_so = st;
|
|
|
|
pmatch.rm_eo = l->len;
|
|
|
|
|
|
|
|
/* Loop to compare with all the patterns */
|
|
|
|
for (i = 0; i < patterns; i++) {
|
2011-02-16 04:31:33 +03:00
|
|
|
/*
|
|
|
|
* XXX: grep_search() is a workaround for speed up and should be
|
|
|
|
* removed in the future. See fastgrep.c.
|
|
|
|
*/
|
2012-05-07 01:56:08 +04:00
|
|
|
if (fg_pattern[i].pattern) {
|
|
|
|
r = grep_search(&fg_pattern[i],
|
|
|
|
(unsigned char *)l->dat,
|
|
|
|
l->len, &pmatch);
|
|
|
|
r = (r == 0) ? 0 : REG_NOMATCH;
|
|
|
|
st = pmatch.rm_eo;
|
|
|
|
} else {
|
|
|
|
r = regexec(&r_pattern[i], l->dat, 1,
|
|
|
|
&pmatch, eflags);
|
|
|
|
r = (r == 0) ? 0 : REG_NOMATCH;
|
|
|
|
st = pmatch.rm_eo;
|
2011-02-16 04:31:33 +03:00
|
|
|
}
|
2012-05-07 01:56:08 +04:00
|
|
|
if (r == REG_NOMATCH)
|
|
|
|
continue;
|
|
|
|
/* Check for full match */
|
2012-05-07 02:32:05 +04:00
|
|
|
if (xflag &&
|
|
|
|
(pmatch.rm_so != 0 ||
|
|
|
|
(size_t)pmatch.rm_eo != l->len))
|
|
|
|
continue;
|
2012-05-07 01:56:08 +04:00
|
|
|
/* Check for whole word match */
|
2012-05-07 02:32:05 +04:00
|
|
|
if (fg_pattern[i].word && pmatch.rm_so != 0) {
|
2014-07-15 01:56:03 +04:00
|
|
|
wchar_t wbegin, wend;
|
2012-05-07 01:56:08 +04:00
|
|
|
|
|
|
|
wbegin = wend = L' ';
|
|
|
|
if (pmatch.rm_so != 0 &&
|
|
|
|
sscanf(&l->dat[pmatch.rm_so - 1],
|
|
|
|
"%lc", &wbegin) != 1)
|
2012-05-07 02:32:05 +04:00
|
|
|
continue;
|
|
|
|
if ((size_t)pmatch.rm_eo != l->len &&
|
2012-05-07 01:56:08 +04:00
|
|
|
sscanf(&l->dat[pmatch.rm_eo],
|
|
|
|
"%lc", &wend) != 1)
|
2012-05-07 02:32:05 +04:00
|
|
|
continue;
|
|
|
|
if (iswword(wbegin) || iswword(wend))
|
|
|
|
continue;
|
2012-05-07 01:56:08 +04:00
|
|
|
}
|
2012-05-07 02:32:05 +04:00
|
|
|
c = 1;
|
|
|
|
if (m < MAX_LINE_MATCHES)
|
|
|
|
matches[m++] = pmatch;
|
|
|
|
/* matches - skip further patterns */
|
|
|
|
if ((color != NULL && !oflag) || qflag || lflag)
|
|
|
|
break;
|
2012-05-07 01:56:08 +04:00
|
|
|
}
|
2004-01-02 18:00:29 +03:00
|
|
|
|
2012-05-07 01:56:08 +04:00
|
|
|
if (vflag) {
|
|
|
|
c = !c;
|
|
|
|
break;
|
2011-02-16 04:31:33 +03:00
|
|
|
}
|
2012-05-07 01:56:08 +04:00
|
|
|
/* One pass if we are not recording matches */
|
|
|
|
if ((color != NULL && !oflag) || qflag || lflag)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (st == (size_t)pmatch.rm_so)
|
|
|
|
break; /* No matches */
|
|
|
|
}
|
2004-01-02 18:00:29 +03:00
|
|
|
|
2011-02-16 04:31:33 +03:00
|
|
|
if (c && binbehave == BINFILE_BIN && nottext)
|
|
|
|
return (c); /* Binary file */
|
2004-01-02 18:00:29 +03:00
|
|
|
|
2011-02-16 04:31:33 +03:00
|
|
|
/* Dealing with the context */
|
|
|
|
if ((tail || c) && !cflag && !qflag && !lflag && !Lflag) {
|
2004-01-02 17:58:43 +03:00
|
|
|
if (c) {
|
2011-04-18 21:18:03 +04:00
|
|
|
if ((Aflag || Bflag) && !first_global &&
|
|
|
|
(first || since_printed > Bflag))
|
2011-02-16 04:31:33 +03:00
|
|
|
printf("--\n");
|
2004-01-02 17:58:43 +03:00
|
|
|
tail = Aflag;
|
2011-04-18 21:18:03 +04:00
|
|
|
if (Bflag > 0)
|
2004-01-02 17:58:43 +03:00
|
|
|
printqueue();
|
2011-02-16 04:31:33 +03:00
|
|
|
printline(l, ':', matches, m);
|
2004-01-02 17:58:43 +03:00
|
|
|
} else {
|
2011-02-16 04:31:33 +03:00
|
|
|
printline(l, '-', matches, m);
|
2004-01-02 17:58:43 +03:00
|
|
|
tail--;
|
|
|
|
}
|
2011-02-16 04:31:33 +03:00
|
|
|
first = false;
|
2011-04-18 21:18:03 +04:00
|
|
|
first_global = false;
|
|
|
|
since_printed = 0;
|
|
|
|
} else {
|
|
|
|
if (Bflag)
|
|
|
|
enqueue(l);
|
|
|
|
since_printed++;
|
|
|
|
}
|
2011-02-16 04:31:33 +03:00
|
|
|
return (c);
|
2004-01-02 17:58:43 +03:00
|
|
|
}
|
|
|
|
|
2011-02-16 04:31:33 +03:00
|
|
|
/*
|
|
|
|
* Safe malloc() for internal use.
|
|
|
|
*/
|
2004-01-02 17:58:43 +03:00
|
|
|
void *
|
|
|
|
grep_malloc(size_t size)
|
|
|
|
{
|
2004-01-02 18:00:29 +03:00
|
|
|
void *ptr;
|
2004-01-02 17:58:43 +03:00
|
|
|
|
|
|
|
if ((ptr = malloc(size)) == NULL)
|
2004-01-02 18:00:29 +03:00
|
|
|
err(2, "malloc");
|
2011-02-16 04:31:33 +03:00
|
|
|
return (ptr);
|
2004-01-02 17:58:43 +03:00
|
|
|
}
|
|
|
|
|
2011-02-16 04:31:33 +03:00
|
|
|
/*
|
|
|
|
* Safe calloc() for internal use.
|
|
|
|
*/
|
|
|
|
void *
|
|
|
|
grep_calloc(size_t nmemb, size_t size)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
if ((ptr = calloc(nmemb, size)) == NULL)
|
|
|
|
err(2, "calloc");
|
|
|
|
return (ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Safe realloc() for internal use.
|
|
|
|
*/
|
2004-01-02 17:58:43 +03:00
|
|
|
void *
|
|
|
|
grep_realloc(void *ptr, size_t size)
|
|
|
|
{
|
2011-02-16 04:31:33 +03:00
|
|
|
|
2004-01-02 17:58:43 +03:00
|
|
|
if ((ptr = realloc(ptr, size)) == NULL)
|
2004-01-02 18:00:29 +03:00
|
|
|
err(2, "realloc");
|
2011-02-16 04:31:33 +03:00
|
|
|
return (ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Safe strdup() for internal use.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
grep_strdup(const char *str)
|
|
|
|
{
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
if ((ret = strdup(str)) == NULL)
|
|
|
|
err(2, "strdup");
|
|
|
|
return (ret);
|
2004-01-02 17:58:43 +03:00
|
|
|
}
|
|
|
|
|
2011-02-16 04:31:33 +03:00
|
|
|
/*
|
|
|
|
* Prints a matching line according to the command line options.
|
|
|
|
*/
|
2004-01-02 17:58:43 +03:00
|
|
|
void
|
2011-02-16 04:31:33 +03:00
|
|
|
printline(struct str *line, int sep, regmatch_t *matches, int m)
|
2004-01-02 17:58:43 +03:00
|
|
|
{
|
2004-01-02 18:00:29 +03:00
|
|
|
size_t a = 0;
|
2011-02-16 04:31:33 +03:00
|
|
|
int i, n = 0;
|
2004-01-02 18:00:29 +03:00
|
|
|
|
2011-02-16 04:31:33 +03:00
|
|
|
if (!hflag) {
|
|
|
|
if (nullflag == 0)
|
|
|
|
fputs(line->file, stdout);
|
|
|
|
else {
|
|
|
|
printf("%s", line->file);
|
|
|
|
putchar(0);
|
|
|
|
}
|
2004-01-02 17:58:43 +03:00
|
|
|
++n;
|
|
|
|
}
|
|
|
|
if (nflag) {
|
2011-02-16 04:31:33 +03:00
|
|
|
if (n > 0)
|
2004-01-02 17:58:43 +03:00
|
|
|
putchar(sep);
|
|
|
|
printf("%d", line->line_no);
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
if (bflag) {
|
2011-02-16 04:31:33 +03:00
|
|
|
if (n > 0)
|
2004-01-02 17:58:43 +03:00
|
|
|
putchar(sep);
|
2011-02-16 04:31:33 +03:00
|
|
|
printf("%lld", (long long)line->off);
|
|
|
|
++n;
|
2004-01-02 17:58:43 +03:00
|
|
|
}
|
|
|
|
if (n)
|
|
|
|
putchar(sep);
|
2011-02-16 04:31:33 +03:00
|
|
|
/* --color and -o */
|
|
|
|
if ((oflag || color) && m > 0) {
|
2004-01-02 18:00:29 +03:00
|
|
|
for (i = 0; i < m; i++) {
|
|
|
|
if (!oflag)
|
2011-02-16 04:31:33 +03:00
|
|
|
fwrite(line->dat + a, matches[i].rm_so - a, 1,
|
|
|
|
stdout);
|
|
|
|
if (color)
|
|
|
|
fprintf(stdout, "\33[%sm\33[K", color);
|
|
|
|
|
2018-02-06 01:14:26 +03:00
|
|
|
fwrite(line->dat + matches[i].rm_so,
|
|
|
|
matches[i].rm_eo - matches[i].rm_so, 1,
|
|
|
|
stdout);
|
|
|
|
|
2011-02-16 04:31:33 +03:00
|
|
|
if (color)
|
|
|
|
fprintf(stdout, "\33[m\33[K");
|
2004-01-02 18:00:29 +03:00
|
|
|
a = matches[i].rm_eo;
|
|
|
|
if (oflag)
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
if (!oflag) {
|
|
|
|
if (line->len - a > 0)
|
|
|
|
fwrite(line->dat + a, line->len - a, 1, stdout);
|
2011-04-19 02:46:48 +04:00
|
|
|
putchar(line_sep);
|
2004-01-02 18:00:29 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fwrite(line->dat, line->len, 1, stdout);
|
2011-04-19 02:46:48 +04:00
|
|
|
putchar(line_sep);
|
2004-01-02 18:00:29 +03:00
|
|
|
}
|
2004-01-02 17:58:43 +03:00
|
|
|
}
|