2004-01-02 18:00:29 +03:00
|
|
|
|
/* $NetBSD: util.c,v 1.1.1.2 2004/01/02 15:00:34 cjep Exp $ */
|
|
|
|
|
|
2004-01-02 17:58:43 +03:00
|
|
|
|
/*-
|
|
|
|
|
* Copyright (c) 1999 James Howard and Dag-Erling Co<EFBFBD>dan Sm<EFBFBD>rgrav
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2004-01-02 18:00:29 +03:00
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
#ifndef lint
|
|
|
|
|
__RCSID("$NetBSD: util.c,v 1.1.1.2 2004/01/02 15:00:34 cjep Exp $");
|
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
2004-01-02 17:58:43 +03:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <err.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <fts.h>
|
|
|
|
|
#include <regex.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <zlib.h>
|
|
|
|
|
|
|
|
|
|
#include "grep.h"
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Process a file line by line...
|
|
|
|
|
*/
|
|
|
|
|
|
2004-01-02 18:00:29 +03:00
|
|
|
|
static int linesqueued, newfile;
|
|
|
|
|
static int procline(str_t *l, int nottext);
|
2004-01-02 17:58:43 +03:00
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
grep_tree(char **argv)
|
|
|
|
|
{
|
2004-01-02 18:00:29 +03:00
|
|
|
|
FTS *fts;
|
|
|
|
|
FTSENT *p;
|
|
|
|
|
int c, fts_flags;
|
2004-01-02 17:58:43 +03:00
|
|
|
|
|
|
|
|
|
c = fts_flags = 0;
|
|
|
|
|
|
2004-01-02 18:00:29 +03:00
|
|
|
|
/* if (linkbehave == LINK_EXPLICIT)
|
2004-01-02 17:58:43 +03:00
|
|
|
|
fts_flags = FTS_COMFOLLOW;
|
2004-01-02 18:00:29 +03:00
|
|
|
|
if (linkbehave == LINK_SKIP)
|
2004-01-02 17:58:43 +03:00
|
|
|
|
fts_flags = FTS_PHYSICAL;
|
2004-01-02 18:00:29 +03:00
|
|
|
|
if (linkbehave == LINK_FOLLOW)
|
|
|
|
|
fts_flags = FTS_LOGICAL;*/
|
2004-01-02 17:58:43 +03:00
|
|
|
|
|
2004-01-02 18:00:29 +03:00
|
|
|
|
fts_flags |= FTS_NOSTAT | FTS_NOCHDIR | FTS_LOGICAL;
|
2004-01-02 17:58:43 +03:00
|
|
|
|
|
2004-01-02 18:00:29 +03:00
|
|
|
|
if ((fts = fts_open(argv, fts_flags, NULL)) == NULL)
|
|
|
|
|
err(2, NULL);
|
2004-01-02 17:58:43 +03:00
|
|
|
|
while ((p = fts_read(fts)) != NULL) {
|
|
|
|
|
switch (p->fts_info) {
|
|
|
|
|
case FTS_DNR:
|
|
|
|
|
break;
|
|
|
|
|
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;
|
|
|
|
|
case FTS_DP:
|
2004-01-02 18:00:29 +03:00
|
|
|
|
case FTS_D:
|
|
|
|
|
break;
|
|
|
|
|
case FTS_DC:
|
|
|
|
|
warnx("warning: %s: recursive directory loop\n",
|
|
|
|
|
p->fts_path);
|
2004-01-02 17:58:43 +03:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
c += procfile(p->fts_path);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
procfile(char *fn)
|
|
|
|
|
{
|
|
|
|
|
str_t ln;
|
|
|
|
|
file_t *f;
|
2004-01-02 18:00:29 +03:00
|
|
|
|
struct stat sb;
|
|
|
|
|
mode_t s;
|
|
|
|
|
int c, t, z, nottext, skip;
|
|
|
|
|
|
|
|
|
|
tail = 0;
|
|
|
|
|
newfile = 1;
|
2004-01-02 17:58:43 +03:00
|
|
|
|
|
|
|
|
|
if (fn == NULL) {
|
2004-01-02 18:00:29 +03:00
|
|
|
|
fn = stdin_label;
|
2004-01-02 17:58:43 +03:00
|
|
|
|
f = grep_fdopen(STDIN_FILENO, "r");
|
|
|
|
|
} else {
|
2004-01-02 18:00:29 +03:00
|
|
|
|
skip = 1;
|
|
|
|
|
if (dirbehave == GREP_SKIP || devbehave == GREP_SKIP) {
|
|
|
|
|
if (stat(fn, &sb)) {
|
|
|
|
|
fprintf(stderr, "Cannot stat %s %d\n",
|
|
|
|
|
fn, errno);
|
|
|
|
|
/* XXX record error variable */
|
|
|
|
|
} else {
|
|
|
|
|
s = sb.st_mode & S_IFMT;
|
|
|
|
|
if (s == S_IFDIR && dirbehave == GREP_SKIP)
|
|
|
|
|
skip = 0;
|
|
|
|
|
if ( (s == S_IFIFO || s == S_IFCHR ||
|
|
|
|
|
s == S_IFBLK || s == S_IFSOCK)
|
|
|
|
|
&& devbehave == GREP_SKIP)
|
|
|
|
|
skip = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (skip == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2004-01-02 17:58:43 +03:00
|
|
|
|
f = grep_open(fn, "r");
|
|
|
|
|
}
|
|
|
|
|
if (f == NULL) {
|
|
|
|
|
if (!sflag)
|
|
|
|
|
warn("%s", fn);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2004-01-02 18:00:29 +03:00
|
|
|
|
|
|
|
|
|
nottext = grep_bin_file(f);
|
|
|
|
|
|
|
|
|
|
if (nottext && binbehave == BIN_FILE_SKIP) {
|
|
|
|
|
/* Skip this file as it is binary */
|
2004-01-02 17:58:43 +03:00
|
|
|
|
grep_close(f);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ln.file = fn;
|
|
|
|
|
ln.line_no = 0;
|
|
|
|
|
linesqueued = 0;
|
|
|
|
|
ln.off = -1;
|
|
|
|
|
|
|
|
|
|
if (Bflag > 0)
|
|
|
|
|
initqueue();
|
|
|
|
|
for (c = 0; !(lflag && c);) {
|
|
|
|
|
ln.off += ln.len + 1;
|
|
|
|
|
if ((ln.dat = grep_fgetln(f, &ln.len)) == NULL)
|
|
|
|
|
break;
|
2004-01-02 18:00:29 +03:00
|
|
|
|
if (ln.len > 0 && ln.dat[ln.len - 1] == line_endchar)
|
2004-01-02 17:58:43 +03:00
|
|
|
|
--ln.len;
|
|
|
|
|
ln.line_no++;
|
|
|
|
|
|
|
|
|
|
z = tail;
|
|
|
|
|
|
2004-01-02 18:00:29 +03:00
|
|
|
|
if ((t = procline(&ln, nottext)) == 0 && Bflag > 0 && z == 0) {
|
2004-01-02 17:58:43 +03:00
|
|
|
|
enqueue(&ln);
|
|
|
|
|
linesqueued++;
|
|
|
|
|
}
|
|
|
|
|
c += t;
|
2004-01-02 18:00:29 +03:00
|
|
|
|
|
|
|
|
|
/* If we have a maximum number of matches, stop processing */
|
|
|
|
|
if (mflag && c >= maxcount)
|
|
|
|
|
break;
|
2004-01-02 17:58:43 +03:00
|
|
|
|
}
|
|
|
|
|
if (Bflag > 0)
|
|
|
|
|
clearqueue();
|
|
|
|
|
grep_close(f);
|
|
|
|
|
|
|
|
|
|
if (cflag) {
|
2004-01-02 18:00:29 +03:00
|
|
|
|
if (output_filenames)
|
|
|
|
|
printf("%s%c", ln.file, fn_colonchar);
|
2004-01-02 17:58:43 +03:00
|
|
|
|
printf("%u\n", c);
|
2004-01-02 18:00:29 +03:00
|
|
|
|
}
|
|
|
|
|
|
2004-01-02 17:58:43 +03:00
|
|
|
|
if (lflag && c != 0)
|
2004-01-02 18:00:29 +03:00
|
|
|
|
printf("%s%c", fn, fn_endchar);
|
2004-01-02 17:58:43 +03:00
|
|
|
|
if (Lflag && c == 0)
|
2004-01-02 18:00:29 +03:00
|
|
|
|
printf("%s%c", fn, fn_endchar);
|
|
|
|
|
if (c && !cflag && !lflag && !Lflag &&
|
|
|
|
|
binbehave == BIN_FILE_BIN && nottext && !qflag)
|
|
|
|
|
printf("Binary file %s matches\n", fn);
|
|
|
|
|
|
2004-01-02 17:58:43 +03:00
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Process an individual line in a file. Return non-zero if it matches.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define isword(x) (isalnum(x) || (x) == '_')
|
|
|
|
|
|
|
|
|
|
static int
|
2004-01-02 18:00:29 +03:00
|
|
|
|
procline(str_t *l, int nottext)
|
2004-01-02 17:58:43 +03:00
|
|
|
|
{
|
2004-01-02 18:00:29 +03:00
|
|
|
|
regmatch_t pmatch;
|
|
|
|
|
regmatch_t matches[MAX_LINE_MATCHES];
|
|
|
|
|
int c = 0, i, r, t, m = 0;
|
|
|
|
|
regoff_t st = 0;
|
2004-01-02 17:58:43 +03:00
|
|
|
|
|
|
|
|
|
if (matchall) {
|
|
|
|
|
c = !vflag;
|
|
|
|
|
goto print;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t = vflag ? REG_NOMATCH : 0;
|
2004-01-02 18:00:29 +03:00
|
|
|
|
|
|
|
|
|
while (st <= l->len) {
|
|
|
|
|
pmatch.rm_so = st;
|
|
|
|
|
pmatch.rm_eo = l->len;
|
|
|
|
|
for (i = 0; i < patterns; i++) {
|
|
|
|
|
r = regexec(&r_pattern[i], l->dat, 1, &pmatch, eflags);
|
|
|
|
|
if (r == REG_NOMATCH && t == 0)
|
|
|
|
|
continue;
|
|
|
|
|
if (r == 0) {
|
|
|
|
|
if (wflag) {
|
|
|
|
|
if ((pmatch.rm_so != 0 && isword(l->dat[pmatch.rm_so - 1]))
|
|
|
|
|
|| (pmatch.rm_eo != l->len && isword(l->dat[pmatch.rm_eo])))
|
|
|
|
|
r = REG_NOMATCH;
|
|
|
|
|
}
|
|
|
|
|
if (xflag) {
|
|
|
|
|
if (pmatch.rm_so != 0 || pmatch.rm_eo != l->len)
|
|
|
|
|
r = REG_NOMATCH;
|
|
|
|
|
}
|
2004-01-02 17:58:43 +03:00
|
|
|
|
}
|
2004-01-02 18:00:29 +03:00
|
|
|
|
if (r == t) {
|
|
|
|
|
if (m == 0)
|
|
|
|
|
c++;
|
|
|
|
|
if (m < MAX_LINE_MATCHES) {
|
|
|
|
|
matches[m] = pmatch;
|
|
|
|
|
m++;
|
|
|
|
|
}
|
|
|
|
|
st = pmatch.rm_eo;
|
|
|
|
|
break;
|
2004-01-02 17:58:43 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2004-01-02 18:00:29 +03:00
|
|
|
|
|
|
|
|
|
/* One pass if we are not recording matches */
|
|
|
|
|
if (!oflag && !colours)
|
2004-01-02 17:58:43 +03:00
|
|
|
|
break;
|
2004-01-02 18:00:29 +03:00
|
|
|
|
|
|
|
|
|
if (st == pmatch.rm_so)
|
|
|
|
|
break; /* No matches */
|
|
|
|
|
|
2004-01-02 17:58:43 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print:
|
2004-01-02 18:00:29 +03:00
|
|
|
|
|
|
|
|
|
if (c && binbehave == BIN_FILE_BIN && nottext)
|
|
|
|
|
return c; /* Binary file */
|
|
|
|
|
|
2004-01-02 17:58:43 +03:00
|
|
|
|
if ((tail > 0 || c) && !cflag && !qflag) {
|
|
|
|
|
if (c) {
|
2004-01-02 18:00:29 +03:00
|
|
|
|
|
|
|
|
|
if ( (Aflag || Bflag) && first > 0 &&
|
|
|
|
|
( (Bflag <= linesqueued && tail == 0) || newfile) )
|
|
|
|
|
printf("--\n");
|
|
|
|
|
|
2004-01-02 17:58:43 +03:00
|
|
|
|
first = 1;
|
2004-01-02 18:00:29 +03:00
|
|
|
|
newfile = 0;
|
2004-01-02 17:58:43 +03:00
|
|
|
|
tail = Aflag;
|
|
|
|
|
if (Bflag > 0)
|
|
|
|
|
printqueue();
|
|
|
|
|
linesqueued = 0;
|
2004-01-02 18:00:29 +03:00
|
|
|
|
printline(l, fn_colonchar, matches, m);
|
2004-01-02 17:58:43 +03:00
|
|
|
|
} else {
|
2004-01-02 18:00:29 +03:00
|
|
|
|
printline(l, fn_dashchar, matches, m);
|
2004-01-02 17:58:43 +03:00
|
|
|
|
tail--;
|
|
|
|
|
}
|
2004-01-02 18:00:29 +03:00
|
|
|
|
|
2004-01-02 17:58:43 +03:00
|
|
|
|
}
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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");
|
2004-01-02 17:58:43 +03:00
|
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void *
|
|
|
|
|
grep_realloc(void *ptr, size_t size)
|
|
|
|
|
{
|
|
|
|
|
if ((ptr = realloc(ptr, size)) == NULL)
|
2004-01-02 18:00:29 +03:00
|
|
|
|
err(2, "realloc");
|
2004-01-02 17:58:43 +03:00
|
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2004-01-02 18:00:29 +03:00
|
|
|
|
printline(str_t *line, int sep, regmatch_t *matches, int m)
|
2004-01-02 17:58:43 +03:00
|
|
|
|
{
|
2004-01-02 18:00:29 +03:00
|
|
|
|
int i, n = 0;
|
|
|
|
|
size_t a = 0;
|
|
|
|
|
|
|
|
|
|
if (output_filenames) {
|
2004-01-02 17:58:43 +03:00
|
|
|
|
fputs(line->file, stdout);
|
|
|
|
|
++n;
|
|
|
|
|
}
|
|
|
|
|
if (nflag) {
|
|
|
|
|
if (n)
|
|
|
|
|
putchar(sep);
|
|
|
|
|
printf("%d", line->line_no);
|
|
|
|
|
++n;
|
|
|
|
|
}
|
|
|
|
|
if (bflag) {
|
|
|
|
|
if (n)
|
|
|
|
|
putchar(sep);
|
|
|
|
|
printf("%lu", (unsigned long)line->off);
|
|
|
|
|
}
|
|
|
|
|
if (n)
|
|
|
|
|
putchar(sep);
|
2004-01-02 18:00:29 +03:00
|
|
|
|
|
|
|
|
|
if ((oflag || colours) && m > 0) {
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < m; i++) {
|
|
|
|
|
|
|
|
|
|
if (!oflag)
|
|
|
|
|
fwrite(line->dat + a, matches[i].rm_so - a, 1, stdout);
|
|
|
|
|
|
|
|
|
|
if (colours)
|
|
|
|
|
fprintf(stdout, "\33[%sm", grep_colour);
|
|
|
|
|
fwrite(line->dat + matches[i].rm_so,
|
|
|
|
|
matches[i].rm_eo - matches[i].rm_so, 1, stdout);
|
|
|
|
|
|
|
|
|
|
if (colours)
|
|
|
|
|
fprintf(stdout, "\33[00m");
|
|
|
|
|
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);
|
|
|
|
|
putchar('\n');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
fwrite(line->dat, line->len, 1, stdout);
|
|
|
|
|
putchar(line_endchar);
|
|
|
|
|
}
|
|
|
|
|
|
2004-01-02 17:58:43 +03:00
|
|
|
|
}
|