1999-11-20 09:40:28 +00:00
|
|
|
/* grep.c - main driver file for grep.
|
2004-03-01 08:37:20 +00:00
|
|
|
Copyright 1992, 1997-1999, 2000 Free Software Foundation, Inc.
|
1999-11-20 09:40:28 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
1999-11-21 01:02:48 +00:00
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
02111-1307, USA. */
|
1999-11-20 09:40:28 +00:00
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
/* Written July 1992 by Mike Haertel. */
|
|
|
|
/* Builtin decompression 1997 by Wolfram Schneider <wosch@FreeBSD.org>. */
|
1999-11-20 09:40:28 +00:00
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
/* $FreeBSD$ */
|
1999-11-20 09:40:28 +00:00
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
1999-11-20 09:40:28 +00:00
|
|
|
#endif
|
|
|
|
#include <sys/types.h>
|
1999-11-21 01:02:48 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#if defined(HAVE_MMAP)
|
|
|
|
# include <sys/mman.h>
|
1999-11-20 09:40:28 +00:00
|
|
|
#endif
|
1999-11-21 01:02:48 +00:00
|
|
|
#if defined(HAVE_SETRLIMIT)
|
|
|
|
# include <sys/time.h>
|
|
|
|
# include <sys/resource.h>
|
1999-11-20 09:40:28 +00:00
|
|
|
#endif
|
2004-07-04 11:58:10 +00:00
|
|
|
#if defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H && defined HAVE_MBRTOWC
|
|
|
|
/* We can handle multibyte string. */
|
|
|
|
# define MBS_SUPPORT
|
|
|
|
# include <wchar.h>
|
|
|
|
# include <wctype.h>
|
|
|
|
#endif
|
1999-11-21 01:02:48 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "system.h"
|
|
|
|
#include "getopt.h"
|
1999-11-20 09:40:28 +00:00
|
|
|
#include "getpagesize.h"
|
|
|
|
#include "grep.h"
|
1999-11-21 01:02:48 +00:00
|
|
|
#include "savedir.h"
|
2004-07-04 10:02:03 +00:00
|
|
|
#include "xstrtol.h"
|
|
|
|
#include "xalloc.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "exclude.h"
|
|
|
|
#include "closeout.h"
|
1999-11-20 09:40:28 +00:00
|
|
|
|
|
|
|
#undef MAX
|
|
|
|
#define MAX(A,B) ((A) > (B) ? (A) : (B))
|
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
struct stats
|
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
struct stats const *parent;
|
1999-11-21 01:02:48 +00:00
|
|
|
struct stat stat;
|
|
|
|
};
|
1999-11-20 09:40:28 +00:00
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
/* base of chain of stat buffers, used to detect directory loops */
|
|
|
|
static struct stats stats_base;
|
1999-11-20 09:40:28 +00:00
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
/* if non-zero, display usage information and exit */
|
|
|
|
static int show_help;
|
1999-11-20 09:40:28 +00:00
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
/* If non-zero, print the version on standard output and exit. */
|
|
|
|
static int show_version;
|
1999-11-20 09:40:28 +00:00
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
/* If nonzero, suppress diagnostics for nonexistent or unreadable files. */
|
|
|
|
static int suppress_errors;
|
|
|
|
|
2000-01-04 03:25:40 +00:00
|
|
|
/* If nonzero, use mmap if possible. */
|
|
|
|
static int mmap_option;
|
|
|
|
|
2000-01-04 10:32:55 +00:00
|
|
|
/* If zero, output nulls after filenames. */
|
|
|
|
static int filename_mask;
|
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
/* If nonzero, use grep_color marker. */
|
|
|
|
static int color_option;
|
|
|
|
|
|
|
|
/* If nonzero, show only the part of a line matching the expression. */
|
|
|
|
static int only_matching;
|
|
|
|
|
|
|
|
/* The color string used. The user can overwrite it using the environment
|
|
|
|
variable GREP_COLOR. The default is to print red. */
|
|
|
|
static const char *grep_color = "01;31";
|
|
|
|
|
|
|
|
static struct exclude *excluded_patterns;
|
|
|
|
static struct exclude *included_patterns;
|
2000-01-04 03:25:40 +00:00
|
|
|
/* Short options. */
|
|
|
|
static char const short_options[] =
|
2004-07-04 10:02:03 +00:00
|
|
|
"0123456789A:B:C:D:EFGHIJPUVX:abcd:e:f:hiKLlm:noqRrsuvwxyZz";
|
2000-01-04 03:25:40 +00:00
|
|
|
|
2000-01-18 09:46:39 +00:00
|
|
|
/* Non-boolean long options that have no corresponding short equivalents. */
|
|
|
|
enum
|
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
BINARY_FILES_OPTION = CHAR_MAX + 1,
|
|
|
|
COLOR_OPTION,
|
|
|
|
INCLUDE_OPTION,
|
|
|
|
EXCLUDE_OPTION,
|
|
|
|
EXCLUDE_FROM_OPTION,
|
|
|
|
LINE_BUFFERED_OPTION,
|
|
|
|
LABEL_OPTION
|
2000-01-18 09:46:39 +00:00
|
|
|
};
|
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
/* Long options equivalences. */
|
2004-07-04 10:02:03 +00:00
|
|
|
static struct option const long_options[] =
|
1999-11-21 01:02:48 +00:00
|
|
|
{
|
|
|
|
{"after-context", required_argument, NULL, 'A'},
|
|
|
|
{"basic-regexp", no_argument, NULL, 'G'},
|
|
|
|
{"before-context", required_argument, NULL, 'B'},
|
2000-01-18 09:46:39 +00:00
|
|
|
{"binary-files", required_argument, NULL, BINARY_FILES_OPTION},
|
1999-11-21 01:02:48 +00:00
|
|
|
{"byte-offset", no_argument, NULL, 'b'},
|
2004-07-04 10:02:03 +00:00
|
|
|
{"context", required_argument, NULL, 'C'},
|
|
|
|
{"color", optional_argument, NULL, COLOR_OPTION},
|
|
|
|
{"colour", optional_argument, NULL, COLOR_OPTION},
|
1999-11-21 01:02:48 +00:00
|
|
|
{"count", no_argument, NULL, 'c'},
|
2004-07-04 10:02:03 +00:00
|
|
|
{"devices", required_argument, NULL, 'D'},
|
1999-11-21 01:02:48 +00:00
|
|
|
{"directories", required_argument, NULL, 'd'},
|
|
|
|
{"extended-regexp", no_argument, NULL, 'E'},
|
2004-07-04 10:02:03 +00:00
|
|
|
{"exclude", required_argument, NULL, EXCLUDE_OPTION},
|
|
|
|
{"exclude-from", required_argument, NULL, EXCLUDE_FROM_OPTION},
|
1999-11-21 01:02:48 +00:00
|
|
|
{"file", required_argument, NULL, 'f'},
|
|
|
|
{"files-with-matches", no_argument, NULL, 'l'},
|
|
|
|
{"files-without-match", no_argument, NULL, 'L'},
|
|
|
|
{"fixed-regexp", no_argument, NULL, 'F'},
|
|
|
|
{"fixed-strings", no_argument, NULL, 'F'},
|
|
|
|
{"help", no_argument, &show_help, 1},
|
2004-07-04 10:02:03 +00:00
|
|
|
{"include", required_argument, NULL, INCLUDE_OPTION},
|
1999-11-21 01:02:48 +00:00
|
|
|
{"ignore-case", no_argument, NULL, 'i'},
|
2004-07-04 10:02:03 +00:00
|
|
|
{"label", required_argument, NULL, LABEL_OPTION},
|
|
|
|
{"line-buffered", no_argument, NULL, LINE_BUFFERED_OPTION},
|
1999-11-21 01:02:48 +00:00
|
|
|
{"line-number", no_argument, NULL, 'n'},
|
|
|
|
{"line-regexp", no_argument, NULL, 'x'},
|
2004-07-04 10:02:03 +00:00
|
|
|
{"max-count", required_argument, NULL, 'm'},
|
2000-01-04 03:25:40 +00:00
|
|
|
{"mmap", no_argument, &mmap_option, 1},
|
1999-11-21 01:02:48 +00:00
|
|
|
{"no-filename", no_argument, NULL, 'h'},
|
|
|
|
{"no-messages", no_argument, NULL, 's'},
|
2002-09-16 04:27:29 +00:00
|
|
|
{"bz2decompress", no_argument, NULL, 'J'},
|
2000-01-04 03:25:40 +00:00
|
|
|
#if HAVE_LIBZ > 0
|
2000-01-04 10:32:55 +00:00
|
|
|
{"decompress", no_argument, NULL, 'Z'},
|
|
|
|
{"null", no_argument, &filename_mask, 0},
|
2000-01-04 03:25:40 +00:00
|
|
|
#else
|
|
|
|
{"null", no_argument, NULL, 'Z'},
|
|
|
|
#endif
|
|
|
|
{"null-data", no_argument, NULL, 'z'},
|
2004-07-04 10:02:03 +00:00
|
|
|
{"only-matching", no_argument, NULL, 'o'},
|
|
|
|
{"perl-regexp", no_argument, NULL, 'P'},
|
1999-11-21 01:02:48 +00:00
|
|
|
{"quiet", no_argument, NULL, 'q'},
|
|
|
|
{"recursive", no_argument, NULL, 'r'},
|
2004-07-04 10:02:03 +00:00
|
|
|
{"recursive", no_argument, NULL, 'R'},
|
1999-11-21 01:02:48 +00:00
|
|
|
{"regexp", required_argument, NULL, 'e'},
|
2000-01-04 03:25:40 +00:00
|
|
|
{"invert-match", no_argument, NULL, 'v'},
|
1999-11-21 01:02:48 +00:00
|
|
|
{"silent", no_argument, NULL, 'q'},
|
|
|
|
{"text", no_argument, NULL, 'a'},
|
|
|
|
{"binary", no_argument, NULL, 'U'},
|
|
|
|
{"unix-byte-offsets", no_argument, NULL, 'u'},
|
|
|
|
{"version", no_argument, NULL, 'V'},
|
|
|
|
{"with-filename", no_argument, NULL, 'H'},
|
|
|
|
{"word-regexp", no_argument, NULL, 'w'},
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
1999-11-20 09:40:28 +00:00
|
|
|
|
|
|
|
/* Define flags declared in grep.h. */
|
|
|
|
int match_icase;
|
|
|
|
int match_words;
|
|
|
|
int match_lines;
|
2000-01-04 03:25:40 +00:00
|
|
|
unsigned char eolbyte;
|
1999-11-20 09:40:28 +00:00
|
|
|
|
|
|
|
/* For error messages. */
|
2004-07-04 10:02:03 +00:00
|
|
|
/* The name the program was run with, stripped of any leading path. */
|
|
|
|
char *program_name;
|
1999-11-21 01:02:48 +00:00
|
|
|
static char const *filename;
|
1999-11-20 09:40:28 +00:00
|
|
|
static int errseen;
|
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
/* How to handle directories. */
|
|
|
|
static enum
|
|
|
|
{
|
|
|
|
READ_DIRECTORIES,
|
|
|
|
RECURSE_DIRECTORIES,
|
|
|
|
SKIP_DIRECTORIES
|
2004-07-04 10:02:03 +00:00
|
|
|
} directories = READ_DIRECTORIES;
|
|
|
|
|
|
|
|
/* How to handle devices. */
|
|
|
|
static enum
|
|
|
|
{
|
|
|
|
READ_DEVICES,
|
|
|
|
SKIP_DEVICES
|
|
|
|
} devices = READ_DEVICES;
|
|
|
|
|
|
|
|
static int grepdir PARAMS ((char const *, struct stats const *));
|
|
|
|
#if defined(HAVE_DOS_FILE_CONTENTS)
|
1999-11-21 01:02:48 +00:00
|
|
|
static inline int undossify_input PARAMS ((register char *, size_t));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Functions we'll use to search. */
|
2004-07-04 10:02:03 +00:00
|
|
|
static void (*compile) PARAMS ((char const *, size_t));
|
2005-05-14 03:02:22 +00:00
|
|
|
static size_t (*execute) PARAMS ((char const *, size_t, size_t *, int));
|
1999-11-21 01:02:48 +00:00
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
/* Like error, but suppress the diagnostic if requested. */
|
1999-11-20 09:40:28 +00:00
|
|
|
static void
|
2004-07-04 10:02:03 +00:00
|
|
|
suppressible_error (char const *mesg, int errnum)
|
1999-11-20 09:40:28 +00:00
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
if (! suppress_errors)
|
|
|
|
error (0, errnum, "%s", mesg);
|
1999-11-20 09:40:28 +00:00
|
|
|
errseen = 1;
|
|
|
|
}
|
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
/* Convert STR to a positive integer, storing the result in *OUT.
|
2004-07-04 10:02:03 +00:00
|
|
|
STR must be a valid context length argument; report an error if it
|
|
|
|
isn't. */
|
|
|
|
static void
|
|
|
|
context_length_arg (char const *str, int *out)
|
1999-11-21 01:02:48 +00:00
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
uintmax_t value;
|
|
|
|
if (! (xstrtoumax (str, 0, 10, &value, "") == LONGINT_OK
|
|
|
|
&& 0 <= (*out = value)
|
|
|
|
&& *out == value))
|
|
|
|
{
|
|
|
|
error (2, 0, "%s: %s\n", str, _("invalid context length argument"));
|
|
|
|
}
|
1999-11-21 01:02:48 +00:00
|
|
|
}
|
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
|
|
|
|
/* Hairy buffering mechanism for grep. The intent is to keep
|
|
|
|
all reads aligned on a page boundary and multiples of the
|
2004-07-04 10:02:03 +00:00
|
|
|
page size, unless a read yields a partial page. */
|
1999-11-20 09:40:28 +00:00
|
|
|
|
|
|
|
static char *buffer; /* Base of buffer. */
|
2004-07-04 10:02:03 +00:00
|
|
|
static size_t bufalloc; /* Allocated buffer size, counting slop. */
|
|
|
|
#define INITIAL_BUFSIZE 32768 /* Initial buffer size, not counting slop. */
|
1999-11-20 09:40:28 +00:00
|
|
|
static int bufdesc; /* File descriptor. */
|
|
|
|
static char *bufbeg; /* Beginning of user-visible stuff. */
|
|
|
|
static char *buflim; /* Limit of user-visible stuff. */
|
1999-11-21 01:02:48 +00:00
|
|
|
static size_t pagesize; /* alignment of memory pages */
|
2000-01-04 03:25:40 +00:00
|
|
|
static off_t bufoffset; /* Read offset; defined on regular files. */
|
2004-07-04 10:02:03 +00:00
|
|
|
static off_t after_last_match; /* Pointer after last matching line that
|
|
|
|
would have been output if we were
|
|
|
|
outputting characters. */
|
1999-11-20 09:40:28 +00:00
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
#if defined(HAVE_MMAP)
|
2000-01-04 03:25:40 +00:00
|
|
|
static int bufmapped; /* True if buffer is memory-mapped. */
|
1999-11-21 01:02:48 +00:00
|
|
|
static off_t initial_bufoffset; /* Initial value of bufoffset. */
|
2004-07-04 10:02:03 +00:00
|
|
|
#else
|
|
|
|
# define bufmapped 0
|
1999-11-20 09:40:28 +00:00
|
|
|
#endif
|
|
|
|
|
2002-09-16 04:27:29 +00:00
|
|
|
#include <bzlib.h>
|
|
|
|
static BZFILE* bzbufdesc; /* libbz2 file handle. */
|
|
|
|
static int BZflag; /* uncompress before searching. */
|
1999-11-20 09:40:28 +00:00
|
|
|
#if HAVE_LIBZ > 0
|
|
|
|
#include <zlib.h>
|
1999-11-21 01:02:48 +00:00
|
|
|
static gzFile gzbufdesc; /* zlib file descriptor. */
|
|
|
|
static int Zflag; /* uncompress before searching. */
|
1999-11-20 09:40:28 +00:00
|
|
|
#endif
|
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
/* Return VAL aligned to the next multiple of ALIGNMENT. VAL can be
|
|
|
|
an integer or a pointer. Both args must be free of side effects. */
|
|
|
|
#define ALIGN_TO(val, alignment) \
|
|
|
|
((size_t) (val) % (alignment) == 0 \
|
|
|
|
? (val) \
|
|
|
|
: (val) + ((alignment) - (size_t) (val) % (alignment)))
|
|
|
|
|
|
|
|
/* Reset the buffer for a new file, returning zero if we should skip it.
|
|
|
|
Initialize on the first time through. */
|
|
|
|
static int
|
2000-01-31 13:28:57 +00:00
|
|
|
reset (int fd, char const *file, struct stats *stats)
|
1999-11-21 01:02:48 +00:00
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
if (! pagesize)
|
1999-11-20 09:40:28 +00:00
|
|
|
{
|
1999-11-21 01:02:48 +00:00
|
|
|
pagesize = getpagesize ();
|
2004-07-04 10:02:03 +00:00
|
|
|
if (pagesize == 0 || 2 * pagesize + 1 <= pagesize)
|
1999-11-21 01:02:48 +00:00
|
|
|
abort ();
|
2004-07-04 10:02:03 +00:00
|
|
|
bufalloc = ALIGN_TO (INITIAL_BUFSIZE, pagesize) + pagesize + 1;
|
|
|
|
buffer = xmalloc (bufalloc);
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
2002-09-16 04:27:29 +00:00
|
|
|
if (BZflag)
|
|
|
|
{
|
|
|
|
bzbufdesc = BZ2_bzdopen(fd, "r");
|
|
|
|
if (bzbufdesc == NULL)
|
2004-07-04 10:02:03 +00:00
|
|
|
error(2, 0, _("memory exhausted"));
|
2002-09-16 04:27:29 +00:00
|
|
|
}
|
1999-11-20 09:40:28 +00:00
|
|
|
#if HAVE_LIBZ > 0
|
2000-01-04 03:25:40 +00:00
|
|
|
if (Zflag)
|
|
|
|
{
|
1999-11-20 09:40:28 +00:00
|
|
|
gzbufdesc = gzdopen(fd, "r");
|
1999-11-21 01:02:48 +00:00
|
|
|
if (gzbufdesc == NULL)
|
2004-07-04 10:02:03 +00:00
|
|
|
error(2, 0, _("memory exhausted"));
|
2000-01-04 03:25:40 +00:00
|
|
|
}
|
1999-11-20 09:40:28 +00:00
|
|
|
#endif
|
2000-01-04 03:25:40 +00:00
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
bufbeg = buflim = ALIGN_TO (buffer + 1, pagesize);
|
|
|
|
bufbeg[-1] = eolbyte;
|
1999-11-21 01:02:48 +00:00
|
|
|
bufdesc = fd;
|
|
|
|
|
2000-01-04 03:25:40 +00:00
|
|
|
if (fstat (fd, &stats->stat) != 0)
|
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
error (0, errno, "fstat");
|
2000-01-04 03:25:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
1999-11-21 01:02:48 +00:00
|
|
|
if (directories == SKIP_DIRECTORIES && S_ISDIR (stats->stat.st_mode))
|
|
|
|
return 0;
|
2004-07-04 10:02:03 +00:00
|
|
|
#ifndef DJGPP
|
2013-01-22 03:23:14 +00:00
|
|
|
if (devices == SKIP_DEVICES && (S_ISCHR(stats->stat.st_mode) || S_ISBLK(stats->stat.st_mode) || S_ISSOCK(stats->stat.st_mode) || S_ISFIFO(stats->stat.st_mode)))
|
2004-07-04 10:02:03 +00:00
|
|
|
#else
|
|
|
|
if (devices == SKIP_DEVICES && (S_ISCHR(stats->stat.st_mode) || S_ISBLK(stats->stat.st_mode)))
|
|
|
|
#endif
|
|
|
|
return 0;
|
1999-11-20 09:40:28 +00:00
|
|
|
if (
|
2002-09-16 04:27:29 +00:00
|
|
|
BZflag ||
|
1999-11-20 09:40:28 +00:00
|
|
|
#if HAVE_LIBZ > 0
|
1999-11-21 01:02:48 +00:00
|
|
|
Zflag ||
|
1999-11-20 09:40:28 +00:00
|
|
|
#endif
|
2000-01-04 03:25:40 +00:00
|
|
|
S_ISREG (stats->stat.st_mode))
|
1999-11-20 09:40:28 +00:00
|
|
|
{
|
2000-01-04 03:25:40 +00:00
|
|
|
if (file)
|
|
|
|
bufoffset = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bufoffset = lseek (fd, 0, SEEK_CUR);
|
|
|
|
if (bufoffset < 0)
|
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
error (0, errno, "lseek");
|
2000-01-04 03:25:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2004-07-04 10:02:03 +00:00
|
|
|
#if defined(HAVE_MMAP)
|
2000-01-04 03:25:40 +00:00
|
|
|
initial_bufoffset = bufoffset;
|
|
|
|
bufmapped = mmap_option && bufoffset % pagesize == 0;
|
|
|
|
#endif
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
2000-01-04 03:25:40 +00:00
|
|
|
else
|
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
#if defined(HAVE_MMAP)
|
2000-01-04 03:25:40 +00:00
|
|
|
bufmapped = 0;
|
1999-11-20 09:40:28 +00:00
|
|
|
#endif
|
2000-01-04 03:25:40 +00:00
|
|
|
}
|
1999-11-21 01:02:48 +00:00
|
|
|
return 1;
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read new stuff into the buffer, saving the specified
|
|
|
|
amount of old stuff. When we're done, 'bufbeg' points
|
|
|
|
to the beginning of the buffer contents, and 'buflim'
|
2000-01-04 03:25:40 +00:00
|
|
|
points just after the end. Return zero if there's an error. */
|
1999-11-20 09:40:28 +00:00
|
|
|
static int
|
2004-07-04 10:02:03 +00:00
|
|
|
fillbuf (size_t save, struct stats const *stats)
|
1999-11-20 09:40:28 +00:00
|
|
|
{
|
2000-01-04 03:25:40 +00:00
|
|
|
size_t fillsize = 0;
|
|
|
|
int cc = 1;
|
2004-07-04 10:02:03 +00:00
|
|
|
char *readbuf;
|
2000-01-04 03:25:40 +00:00
|
|
|
size_t readsize;
|
1999-11-20 09:40:28 +00:00
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
/* Offset from start of buffer to start of old stuff
|
2000-01-04 03:25:40 +00:00
|
|
|
that we want to save. */
|
2004-07-04 10:02:03 +00:00
|
|
|
size_t saved_offset = buflim - save - buffer;
|
1999-11-21 01:02:48 +00:00
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
if (pagesize <= buffer + bufalloc - buflim)
|
1999-11-21 01:02:48 +00:00
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
readbuf = buflim;
|
|
|
|
bufbeg = buflim - save;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t minsize = save + pagesize;
|
|
|
|
size_t newsize;
|
2000-01-04 03:25:40 +00:00
|
|
|
size_t newalloc;
|
2004-07-04 10:02:03 +00:00
|
|
|
char *newbuf;
|
|
|
|
|
|
|
|
/* Grow newsize until it is at least as great as minsize. */
|
|
|
|
for (newsize = bufalloc - pagesize - 1; newsize < minsize; newsize *= 2)
|
|
|
|
if (newsize * 2 < newsize || newsize * 2 + pagesize + 1 < newsize * 2)
|
|
|
|
xalloc_die ();
|
|
|
|
|
|
|
|
/* Try not to allocate more memory than the file size indicates,
|
|
|
|
as that might cause unnecessary memory exhaustion if the file
|
|
|
|
is large. However, do not use the original file size as a
|
|
|
|
heuristic if we've already read past the file end, as most
|
|
|
|
likely the file is growing. */
|
2000-01-04 03:25:40 +00:00
|
|
|
if (S_ISREG (stats->stat.st_mode))
|
|
|
|
{
|
|
|
|
off_t to_be_read = stats->stat.st_size - bufoffset;
|
2004-07-04 10:02:03 +00:00
|
|
|
off_t maxsize_off = save + to_be_read;
|
|
|
|
if (0 <= to_be_read && to_be_read <= maxsize_off
|
|
|
|
&& maxsize_off == (size_t) maxsize_off
|
|
|
|
&& minsize <= (size_t) maxsize_off
|
|
|
|
&& (size_t) maxsize_off < newsize)
|
|
|
|
newsize = maxsize_off;
|
2000-01-04 03:25:40 +00:00
|
|
|
}
|
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
/* Add enough room so that the buffer is aligned and has room
|
|
|
|
for byte sentinels fore and aft. */
|
|
|
|
newalloc = newsize + pagesize + 1;
|
2000-01-04 03:25:40 +00:00
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
newbuf = bufalloc < newalloc ? xmalloc (bufalloc = newalloc) : buffer;
|
|
|
|
readbuf = ALIGN_TO (newbuf + 1 + save, pagesize);
|
|
|
|
bufbeg = readbuf - save;
|
|
|
|
memmove (bufbeg, buffer + saved_offset, save);
|
|
|
|
bufbeg[-1] = eolbyte;
|
|
|
|
if (newbuf != buffer)
|
2000-01-04 03:25:40 +00:00
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
free (buffer);
|
|
|
|
buffer = newbuf;
|
2000-01-04 03:25:40 +00:00
|
|
|
}
|
1999-11-21 01:02:48 +00:00
|
|
|
}
|
1999-11-20 09:40:28 +00:00
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
readsize = buffer + bufalloc - readbuf;
|
|
|
|
readsize -= readsize % pagesize;
|
2000-01-04 03:25:40 +00:00
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
#if defined(HAVE_MMAP)
|
2000-01-04 03:25:40 +00:00
|
|
|
if (bufmapped)
|
1999-11-20 09:40:28 +00:00
|
|
|
{
|
2000-01-04 03:25:40 +00:00
|
|
|
size_t mmapsize = readsize;
|
|
|
|
|
|
|
|
/* Don't mmap past the end of the file; some hosts don't allow this.
|
|
|
|
Use `read' on the last page. */
|
|
|
|
if (stats->stat.st_size - bufoffset < mmapsize)
|
1999-11-20 09:40:28 +00:00
|
|
|
{
|
2000-01-04 03:25:40 +00:00
|
|
|
mmapsize = stats->stat.st_size - bufoffset;
|
|
|
|
mmapsize -= mmapsize % pagesize;
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
2000-01-04 03:25:40 +00:00
|
|
|
|
|
|
|
if (mmapsize
|
2004-07-04 10:02:03 +00:00
|
|
|
&& (mmap ((caddr_t) readbuf, mmapsize,
|
2000-01-04 03:25:40 +00:00
|
|
|
PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED,
|
|
|
|
bufdesc, bufoffset)
|
|
|
|
!= (caddr_t) -1))
|
1999-11-20 09:40:28 +00:00
|
|
|
{
|
2000-01-04 03:25:40 +00:00
|
|
|
/* Do not bother to use madvise with MADV_SEQUENTIAL or
|
|
|
|
MADV_WILLNEED on the mmapped memory. One might think it
|
|
|
|
would help, but it slows us down about 30% on SunOS 4.1. */
|
|
|
|
fillsize = mmapsize;
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
1999-11-21 01:02:48 +00:00
|
|
|
else
|
2000-01-04 03:25:40 +00:00
|
|
|
{
|
|
|
|
/* Stop using mmap on this file. Synchronize the file
|
|
|
|
offset. Do not warn about mmap failures. On some hosts
|
|
|
|
(e.g. Solaris 2.5) mmap can fail merely because some
|
|
|
|
other process has an advisory read lock on the file.
|
|
|
|
There's no point alarming the user about this misfeature. */
|
|
|
|
bufmapped = 0;
|
|
|
|
if (bufoffset != initial_bufoffset
|
|
|
|
&& lseek (bufdesc, bufoffset, SEEK_SET) < 0)
|
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
error (0, errno, "lseek");
|
2000-01-04 03:25:40 +00:00
|
|
|
cc = 0;
|
|
|
|
}
|
|
|
|
}
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
2000-01-04 03:25:40 +00:00
|
|
|
#endif /*HAVE_MMAP*/
|
|
|
|
|
|
|
|
if (! fillsize)
|
|
|
|
{
|
|
|
|
ssize_t bytesread;
|
|
|
|
do
|
2002-10-06 07:02:30 +00:00
|
|
|
if (BZflag && bzbufdesc)
|
2002-09-16 04:27:29 +00:00
|
|
|
{
|
2002-10-06 07:02:30 +00:00
|
|
|
int bzerr;
|
2004-07-04 10:02:03 +00:00
|
|
|
bytesread = BZ2_bzRead (&bzerr, bzbufdesc, readbuf, readsize);
|
2002-10-06 07:02:30 +00:00
|
|
|
|
|
|
|
switch (bzerr)
|
|
|
|
{
|
|
|
|
case BZ_OK:
|
|
|
|
case BZ_STREAM_END:
|
|
|
|
/* ok */
|
|
|
|
break;
|
|
|
|
case BZ_DATA_ERROR_MAGIC:
|
|
|
|
BZ2_bzReadClose (&bzerr, bzbufdesc); bzbufdesc = NULL;
|
|
|
|
lseek (bufdesc, 0, SEEK_SET);
|
2004-07-04 10:02:03 +00:00
|
|
|
bytesread = read (bufdesc, readbuf, readsize);
|
2002-10-06 07:02:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
bytesread = 0;
|
|
|
|
break;
|
|
|
|
}
|
2002-09-16 04:27:29 +00:00
|
|
|
}
|
|
|
|
else
|
1999-11-20 09:40:28 +00:00
|
|
|
#if HAVE_LIBZ > 0
|
2000-01-04 03:25:40 +00:00
|
|
|
if (Zflag)
|
2004-07-04 10:02:03 +00:00
|
|
|
bytesread = gzread (gzbufdesc, readbuf, readsize);
|
2000-01-04 03:25:40 +00:00
|
|
|
else
|
1999-11-20 09:40:28 +00:00
|
|
|
#endif
|
2004-07-04 10:02:03 +00:00
|
|
|
bytesread = read (bufdesc, readbuf, readsize);
|
2000-01-04 03:25:40 +00:00
|
|
|
while (bytesread < 0 && errno == EINTR);
|
|
|
|
if (bytesread < 0)
|
|
|
|
cc = 0;
|
|
|
|
else
|
|
|
|
fillsize = bytesread;
|
|
|
|
}
|
|
|
|
|
|
|
|
bufoffset += fillsize;
|
2004-07-04 10:02:03 +00:00
|
|
|
#if defined(HAVE_DOS_FILE_CONTENTS)
|
2000-01-04 03:25:40 +00:00
|
|
|
if (fillsize)
|
2004-07-04 10:02:03 +00:00
|
|
|
fillsize = undossify_input (readbuf, fillsize);
|
1999-11-20 09:40:28 +00:00
|
|
|
#endif
|
2004-07-04 10:02:03 +00:00
|
|
|
buflim = readbuf + fillsize;
|
1999-11-20 09:40:28 +00:00
|
|
|
return cc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Flags controlling the style of output. */
|
2000-01-18 09:46:39 +00:00
|
|
|
static enum
|
2004-07-04 10:02:03 +00:00
|
|
|
{
|
|
|
|
BINARY_BINARY_FILES,
|
|
|
|
TEXT_BINARY_FILES,
|
|
|
|
WITHOUT_MATCH_BINARY_FILES
|
|
|
|
} binary_files; /* How to handle binary files. */
|
|
|
|
|
|
|
|
static int filename_mask; /* If zero, output nulls after filenames. */
|
1999-11-20 09:40:28 +00:00
|
|
|
static int out_quiet; /* Suppress all normal output. */
|
|
|
|
static int out_invert; /* Print nonmatching stuff. */
|
|
|
|
static int out_file; /* Print filenames. */
|
|
|
|
static int out_line; /* Print line numbers. */
|
|
|
|
static int out_byte; /* Print byte offsets. */
|
|
|
|
static int out_before; /* Lines of leading context. */
|
|
|
|
static int out_after; /* Lines of trailing context. */
|
1999-11-21 01:02:48 +00:00
|
|
|
static int count_matches; /* Count matching lines. */
|
|
|
|
static int list_files; /* List matching files. */
|
|
|
|
static int no_filenames; /* Suppress file names. */
|
2004-07-04 10:02:03 +00:00
|
|
|
static off_t max_count; /* Stop after outputting this many
|
|
|
|
lines from an input file. */
|
|
|
|
static int line_buffered; /* If nonzero, use line buffering, i.e.
|
|
|
|
fflush everyline out. */
|
|
|
|
static char *label = NULL; /* Fake filename for stdin */
|
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
|
|
|
|
/* Internal variables to keep track of byte count, context, etc. */
|
2004-07-04 10:02:03 +00:00
|
|
|
static uintmax_t totalcc; /* Total character count before bufbeg. */
|
|
|
|
static char const *lastnl; /* Pointer after last newline counted. */
|
|
|
|
static char const *lastout; /* Pointer after last character output;
|
1999-11-20 09:40:28 +00:00
|
|
|
NULL if no character has been output
|
|
|
|
or if it's conceptually before bufbeg. */
|
2004-07-04 10:02:03 +00:00
|
|
|
static uintmax_t totalnl; /* Total newline count before lastnl. */
|
|
|
|
static off_t outleft; /* Maximum number of lines to be output. */
|
|
|
|
static int pending; /* Pending lines of output.
|
|
|
|
Always kept 0 if out_quiet is true. */
|
|
|
|
static int done_on_match; /* Stop scanning file on first match. */
|
|
|
|
static int exit_on_match; /* Exit on first match. */
|
|
|
|
|
|
|
|
#if defined(HAVE_DOS_FILE_CONTENTS)
|
1999-11-21 01:02:48 +00:00
|
|
|
# include "dosbuf.c"
|
|
|
|
#endif
|
1999-11-20 09:40:28 +00:00
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
/* Add two numbers that count input bytes or lines, and report an
|
|
|
|
error if the addition overflows. */
|
|
|
|
static uintmax_t
|
|
|
|
add_count (uintmax_t a, uintmax_t b)
|
|
|
|
{
|
|
|
|
uintmax_t sum = a + b;
|
|
|
|
if (sum < a)
|
|
|
|
error (2, 0, _("input is too large to count"));
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
static void
|
2004-07-04 10:02:03 +00:00
|
|
|
nlscan (char const *lim)
|
1999-11-20 09:40:28 +00:00
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
size_t newlines = 0;
|
|
|
|
char const *beg;
|
|
|
|
for (beg = lastnl; beg != lim; beg = memchr (beg, eolbyte, lim - beg), beg++)
|
|
|
|
newlines++;
|
|
|
|
totalnl = add_count (totalnl, newlines);
|
2000-01-04 03:25:40 +00:00
|
|
|
lastnl = lim;
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
/* Print a byte offset, followed by a character separator. */
|
1999-11-20 09:40:28 +00:00
|
|
|
static void
|
2004-07-04 10:02:03 +00:00
|
|
|
print_offset_sep (uintmax_t pos, char sep)
|
1999-11-21 01:02:48 +00:00
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
/* Do not rely on printf to print pos, since uintmax_t may be longer
|
|
|
|
than long, and long long is not portable. */
|
1999-11-21 01:02:48 +00:00
|
|
|
|
|
|
|
char buf[sizeof pos * CHAR_BIT];
|
|
|
|
char *p = buf + sizeof buf - 1;
|
|
|
|
*p = sep;
|
|
|
|
|
|
|
|
do
|
|
|
|
*--p = '0' + pos % 10;
|
|
|
|
while ((pos /= 10) != 0);
|
|
|
|
|
|
|
|
fwrite (p, 1, buf + sizeof buf - p, stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-05-14 03:02:22 +00:00
|
|
|
prline (char const *beg, char const *lim, int sep)
|
1999-11-20 09:40:28 +00:00
|
|
|
{
|
|
|
|
if (out_file)
|
2000-01-04 03:25:40 +00:00
|
|
|
printf ("%s%c", filename, sep & filename_mask);
|
1999-11-20 09:40:28 +00:00
|
|
|
if (out_line)
|
|
|
|
{
|
1999-11-21 01:02:48 +00:00
|
|
|
nlscan (beg);
|
2004-07-04 10:02:03 +00:00
|
|
|
totalnl = add_count (totalnl, 1);
|
|
|
|
print_offset_sep (totalnl, sep);
|
1999-11-20 09:40:28 +00:00
|
|
|
lastnl = lim;
|
|
|
|
}
|
|
|
|
if (out_byte)
|
1999-11-21 01:02:48 +00:00
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
uintmax_t pos = add_count (totalcc, beg - bufbeg);
|
|
|
|
#if defined(HAVE_DOS_FILE_CONTENTS)
|
1999-11-21 01:02:48 +00:00
|
|
|
pos = dossified_pos (pos);
|
|
|
|
#endif
|
|
|
|
print_offset_sep (pos, sep);
|
|
|
|
}
|
2004-07-04 10:02:03 +00:00
|
|
|
if (only_matching)
|
|
|
|
{
|
|
|
|
size_t match_size;
|
|
|
|
size_t match_offset;
|
2005-05-14 03:02:22 +00:00
|
|
|
while ((match_offset = (*execute) (beg, lim - beg, &match_size, 1))
|
2004-07-04 10:02:03 +00:00
|
|
|
!= (size_t) -1)
|
|
|
|
{
|
|
|
|
char const *b = beg + match_offset;
|
|
|
|
if (b == lim)
|
|
|
|
break;
|
|
|
|
if (match_size == 0)
|
|
|
|
break;
|
|
|
|
if(color_option)
|
|
|
|
printf("\33[%sm", grep_color);
|
|
|
|
fwrite(b, sizeof (char), match_size, stdout);
|
|
|
|
if(color_option)
|
|
|
|
fputs("\33[00m", stdout);
|
|
|
|
fputs("\n", stdout);
|
|
|
|
beg = b + match_size;
|
|
|
|
}
|
|
|
|
lastout = lim;
|
|
|
|
if(line_buffered)
|
|
|
|
fflush(stdout);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (color_option)
|
|
|
|
{
|
|
|
|
size_t match_size;
|
|
|
|
size_t match_offset;
|
2005-05-14 03:02:22 +00:00
|
|
|
while (lim-beg && (match_offset = (*execute) (beg, lim - beg, &match_size, 1))
|
2004-07-04 10:02:03 +00:00
|
|
|
!= (size_t) -1)
|
|
|
|
{
|
|
|
|
char const *b = beg + match_offset;
|
|
|
|
/* Avoid matching the empty line at the end of the buffer. */
|
|
|
|
if (b == lim)
|
|
|
|
break;
|
|
|
|
/* Avoid hanging on grep --color "" foo */
|
|
|
|
if (match_size == 0)
|
|
|
|
break;
|
|
|
|
fwrite (beg, sizeof (char), match_offset, stdout);
|
|
|
|
printf ("\33[%sm", grep_color);
|
|
|
|
fwrite (b, sizeof (char), match_size, stdout);
|
|
|
|
fputs ("\33[00m", stdout);
|
|
|
|
beg = b + match_size;
|
|
|
|
}
|
2005-05-14 05:26:48 +00:00
|
|
|
fputs ("\33[K", stdout);
|
2004-07-04 10:02:03 +00:00
|
|
|
}
|
1999-11-21 01:02:48 +00:00
|
|
|
fwrite (beg, 1, lim - beg, stdout);
|
|
|
|
if (ferror (stdout))
|
2004-07-04 10:02:03 +00:00
|
|
|
error (0, errno, _("writing output"));
|
1999-11-20 09:40:28 +00:00
|
|
|
lastout = lim;
|
2004-07-04 10:02:03 +00:00
|
|
|
if (line_buffered)
|
|
|
|
fflush (stdout);
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
/* Print pending lines of trailing context prior to LIM. Trailing context ends
|
|
|
|
at the next matching line when OUTLEFT is 0. */
|
1999-11-20 09:40:28 +00:00
|
|
|
static void
|
2005-05-14 03:02:22 +00:00
|
|
|
prpending (char const *lim)
|
1999-11-20 09:40:28 +00:00
|
|
|
{
|
|
|
|
if (!lastout)
|
|
|
|
lastout = bufbeg;
|
|
|
|
while (pending > 0 && lastout < lim)
|
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
char const *nl = memchr (lastout, eolbyte, lim - lastout);
|
|
|
|
size_t match_size;
|
1999-11-20 09:40:28 +00:00
|
|
|
--pending;
|
2004-07-04 10:02:03 +00:00
|
|
|
if (outleft
|
2005-05-14 03:02:22 +00:00
|
|
|
|| (((*execute) (lastout, nl - lastout, &match_size, 0) == (size_t) -1)
|
2004-07-04 10:02:03 +00:00
|
|
|
== !out_invert))
|
2005-05-14 03:02:22 +00:00
|
|
|
prline (lastout, nl + 1, '-');
|
1999-11-20 09:40:28 +00:00
|
|
|
else
|
2004-07-04 10:02:03 +00:00
|
|
|
pending = 0;
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print the lines between BEG and LIM. Deal with context crap.
|
2004-07-04 10:02:03 +00:00
|
|
|
If NLINESP is non-null, store a count of lines between BEG and LIM. */
|
1999-11-20 09:40:28 +00:00
|
|
|
static void
|
2005-05-14 03:02:22 +00:00
|
|
|
prtext (char const *beg, char const *lim, int *nlinesp)
|
1999-11-20 09:40:28 +00:00
|
|
|
{
|
|
|
|
static int used; /* avoid printing "--" before any output */
|
2004-07-04 10:02:03 +00:00
|
|
|
char const *bp, *p;
|
2000-01-04 03:25:40 +00:00
|
|
|
char eol = eolbyte;
|
1999-11-20 09:40:28 +00:00
|
|
|
int i, n;
|
|
|
|
|
|
|
|
if (!out_quiet && pending > 0)
|
2005-05-14 03:02:22 +00:00
|
|
|
prpending (beg);
|
1999-11-20 09:40:28 +00:00
|
|
|
|
|
|
|
p = beg;
|
|
|
|
|
|
|
|
if (!out_quiet)
|
|
|
|
{
|
|
|
|
/* Deal with leading context crap. */
|
|
|
|
|
|
|
|
bp = lastout ? lastout : bufbeg;
|
|
|
|
for (i = 0; i < out_before; ++i)
|
|
|
|
if (p > bp)
|
|
|
|
do
|
|
|
|
--p;
|
2004-07-04 10:02:03 +00:00
|
|
|
while (p[-1] != eol);
|
1999-11-20 09:40:28 +00:00
|
|
|
|
|
|
|
/* We only print the "--" separator if our output is
|
|
|
|
discontiguous from the last output in the file. */
|
|
|
|
if ((out_before || out_after) && used && p != lastout)
|
1999-11-21 01:02:48 +00:00
|
|
|
puts ("--");
|
1999-11-20 09:40:28 +00:00
|
|
|
|
|
|
|
while (p < beg)
|
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
char const *nl = memchr (p, eol, beg - p);
|
|
|
|
nl++;
|
2005-05-14 03:02:22 +00:00
|
|
|
prline (p, nl, '-');
|
2004-07-04 10:02:03 +00:00
|
|
|
p = nl;
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nlinesp)
|
|
|
|
{
|
|
|
|
/* Caller wants a line count. */
|
2004-07-04 10:02:03 +00:00
|
|
|
for (n = 0; p < lim && n < outleft; n++)
|
1999-11-20 09:40:28 +00:00
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
char const *nl = memchr (p, eol, lim - p);
|
|
|
|
nl++;
|
1999-11-20 09:40:28 +00:00
|
|
|
if (!out_quiet)
|
2005-05-14 03:02:22 +00:00
|
|
|
prline (p, nl, ':');
|
1999-11-20 09:40:28 +00:00
|
|
|
p = nl;
|
|
|
|
}
|
|
|
|
*nlinesp = n;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
|
|
|
/* relying on it that this function is never called when outleft = 0. */
|
|
|
|
after_last_match = bufoffset - (buflim - p);
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (!out_quiet)
|
2005-05-14 03:02:22 +00:00
|
|
|
prline (beg, lim, ':');
|
1999-11-20 09:40:28 +00:00
|
|
|
|
2000-01-04 03:25:40 +00:00
|
|
|
pending = out_quiet ? 0 : out_after;
|
1999-11-20 09:40:28 +00:00
|
|
|
used = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Scan the specified portion of the buffer, matching lines (or
|
|
|
|
between matching lines if OUT_INVERT is true). Return a count of
|
|
|
|
lines printed. */
|
|
|
|
static int
|
2005-05-14 03:02:22 +00:00
|
|
|
grepbuf (char const *beg, char const *lim)
|
1999-11-20 09:40:28 +00:00
|
|
|
{
|
|
|
|
int nlines, n;
|
2004-07-04 10:02:03 +00:00
|
|
|
register char const *p;
|
|
|
|
size_t match_offset;
|
|
|
|
size_t match_size;
|
1999-11-20 09:40:28 +00:00
|
|
|
|
|
|
|
nlines = 0;
|
|
|
|
p = beg;
|
2005-05-14 03:02:22 +00:00
|
|
|
while ((match_offset = (*execute) (p, lim - p, &match_size, 0)) != (size_t) -1)
|
1999-11-20 09:40:28 +00:00
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
char const *b = p + match_offset;
|
|
|
|
char const *endp = b + match_size;
|
1999-11-20 09:40:28 +00:00
|
|
|
/* Avoid matching the empty line at the end of the buffer. */
|
2004-07-04 10:02:03 +00:00
|
|
|
if (b == lim)
|
1999-11-20 09:40:28 +00:00
|
|
|
break;
|
|
|
|
if (!out_invert)
|
|
|
|
{
|
2005-05-14 03:02:22 +00:00
|
|
|
prtext (b, endp, (int *) 0);
|
2004-07-04 10:02:03 +00:00
|
|
|
nlines++;
|
|
|
|
outleft--;
|
|
|
|
if (!outleft || done_on_match)
|
|
|
|
{
|
|
|
|
if (exit_on_match)
|
|
|
|
exit (0);
|
|
|
|
after_last_match = bufoffset - (buflim - endp);
|
|
|
|
return nlines;
|
|
|
|
}
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
|
|
|
else if (p < b)
|
|
|
|
{
|
2005-05-14 03:02:22 +00:00
|
|
|
prtext (p, b, &n);
|
1999-11-20 09:40:28 +00:00
|
|
|
nlines += n;
|
2004-07-04 10:02:03 +00:00
|
|
|
outleft -= n;
|
|
|
|
if (!outleft)
|
|
|
|
return nlines;
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
|
|
|
p = endp;
|
|
|
|
}
|
|
|
|
if (out_invert && p < lim)
|
|
|
|
{
|
2005-05-14 03:02:22 +00:00
|
|
|
prtext (p, lim, &n);
|
1999-11-20 09:40:28 +00:00
|
|
|
nlines += n;
|
2004-07-04 10:02:03 +00:00
|
|
|
outleft -= n;
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
|
|
|
return nlines;
|
|
|
|
}
|
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
/* Search a given file. Normally, return a count of lines printed;
|
|
|
|
but if the file is a directory and we search it recursively, then
|
|
|
|
return -2 if there was a match, and -1 otherwise. */
|
1999-11-20 09:40:28 +00:00
|
|
|
static int
|
2000-01-31 13:28:57 +00:00
|
|
|
grep (int fd, char const *file, struct stats *stats)
|
1999-11-20 09:40:28 +00:00
|
|
|
{
|
|
|
|
int nlines, i;
|
1999-11-21 01:02:48 +00:00
|
|
|
int not_text;
|
1999-11-20 09:40:28 +00:00
|
|
|
size_t residue, save;
|
2004-07-04 10:02:03 +00:00
|
|
|
char oldc;
|
|
|
|
char *beg;
|
|
|
|
char *lim;
|
2000-01-04 03:25:40 +00:00
|
|
|
char eol = eolbyte;
|
1999-11-20 09:40:28 +00:00
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
if (!reset (fd, file, stats))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (file && directories == RECURSE_DIRECTORIES
|
|
|
|
&& S_ISDIR (stats->stat.st_mode))
|
|
|
|
{
|
|
|
|
/* Close fd now, so that we don't open a lot of file descriptors
|
|
|
|
when we recurse deeply. */
|
2002-10-06 07:02:30 +00:00
|
|
|
if (BZflag && bzbufdesc)
|
2002-09-16 04:27:29 +00:00
|
|
|
BZ2_bzclose(bzbufdesc);
|
|
|
|
else
|
1999-11-21 01:02:48 +00:00
|
|
|
#if HAVE_LIBZ > 0
|
|
|
|
if (Zflag)
|
|
|
|
gzclose(gzbufdesc);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (close (fd) != 0)
|
2004-07-04 10:02:03 +00:00
|
|
|
error (0, errno, "%s", file);
|
1999-11-21 01:02:48 +00:00
|
|
|
return grepdir (file, stats) - 2;
|
|
|
|
}
|
1999-11-20 09:40:28 +00:00
|
|
|
|
|
|
|
totalcc = 0;
|
|
|
|
lastout = 0;
|
|
|
|
totalnl = 0;
|
2004-07-04 10:02:03 +00:00
|
|
|
outleft = max_count;
|
|
|
|
after_last_match = 0;
|
1999-11-20 09:40:28 +00:00
|
|
|
pending = 0;
|
|
|
|
|
|
|
|
nlines = 0;
|
|
|
|
residue = 0;
|
|
|
|
save = 0;
|
|
|
|
|
2000-01-04 03:25:40 +00:00
|
|
|
if (! fillbuf (save, stats))
|
1999-11-20 09:40:28 +00:00
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
if (! is_EISDIR (errno, file))
|
|
|
|
suppressible_error (filename, errno);
|
2000-01-18 09:46:39 +00:00
|
|
|
return 0;
|
1999-11-21 01:02:48 +00:00
|
|
|
}
|
1999-11-20 09:40:28 +00:00
|
|
|
|
2000-01-18 09:46:39 +00:00
|
|
|
not_text = (((binary_files == BINARY_BINARY_FILES && !out_quiet)
|
|
|
|
|| binary_files == WITHOUT_MATCH_BINARY_FILES)
|
2000-01-04 03:25:40 +00:00
|
|
|
&& memchr (bufbeg, eol ? '\0' : '\200', buflim - bufbeg));
|
2000-01-18 09:46:39 +00:00
|
|
|
if (not_text && binary_files == WITHOUT_MATCH_BINARY_FILES)
|
|
|
|
return 0;
|
1999-11-21 01:02:48 +00:00
|
|
|
done_on_match += not_text;
|
|
|
|
out_quiet += not_text;
|
1999-11-20 09:40:28 +00:00
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
1999-11-20 09:40:28 +00:00
|
|
|
lastnl = bufbeg;
|
|
|
|
if (lastout)
|
|
|
|
lastout = bufbeg;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
|
|
|
beg = bufbeg + save;
|
|
|
|
|
|
|
|
/* no more data to scan (eof) except for maybe a residue -> break */
|
|
|
|
if (beg == buflim)
|
1999-11-20 09:40:28 +00:00
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
|
|
|
/* Determine new residue (the length of an incomplete line at the end of
|
|
|
|
the buffer, 0 means there is no incomplete last line). */
|
|
|
|
oldc = beg[-1];
|
|
|
|
beg[-1] = eol;
|
|
|
|
for (lim = buflim; lim[-1] != eol; lim--)
|
|
|
|
continue;
|
|
|
|
beg[-1] = oldc;
|
|
|
|
if (lim == beg)
|
|
|
|
lim = beg - residue;
|
|
|
|
beg -= residue;
|
1999-11-20 09:40:28 +00:00
|
|
|
residue = buflim - lim;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
if (beg < lim)
|
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
if (outleft)
|
2005-05-14 03:02:22 +00:00
|
|
|
nlines += grepbuf (beg, lim);
|
1999-11-20 09:40:28 +00:00
|
|
|
if (pending)
|
2005-05-14 03:02:22 +00:00
|
|
|
prpending (lim);
|
2004-07-04 10:02:03 +00:00
|
|
|
if((!outleft && !pending) || (nlines && done_on_match && !out_invert))
|
1999-11-21 01:02:48 +00:00
|
|
|
goto finish_grep;
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
2004-07-04 10:02:03 +00:00
|
|
|
|
|
|
|
/* The last OUT_BEFORE lines at the end of the buffer will be needed as
|
|
|
|
leading context if there is a matching line at the begin of the
|
|
|
|
next data. Make beg point to their begin. */
|
1999-11-20 09:40:28 +00:00
|
|
|
i = 0;
|
|
|
|
beg = lim;
|
|
|
|
while (i < out_before && beg > bufbeg && beg != lastout)
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
do
|
|
|
|
--beg;
|
2004-07-04 10:02:03 +00:00
|
|
|
while (beg[-1] != eol);
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
2004-07-04 10:02:03 +00:00
|
|
|
|
|
|
|
/* detect if leading context is discontinuous from last printed line. */
|
1999-11-20 09:40:28 +00:00
|
|
|
if (beg != lastout)
|
|
|
|
lastout = 0;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
|
|
|
/* Handle some details and read more data to scan. */
|
1999-11-20 09:40:28 +00:00
|
|
|
save = residue + lim - beg;
|
2004-07-04 10:02:03 +00:00
|
|
|
if (out_byte)
|
|
|
|
totalcc = add_count (totalcc, buflim - bufbeg - save);
|
1999-11-20 09:40:28 +00:00
|
|
|
if (out_line)
|
1999-11-21 01:02:48 +00:00
|
|
|
nlscan (beg);
|
2000-01-04 03:25:40 +00:00
|
|
|
if (! fillbuf (save, stats))
|
1999-11-21 01:02:48 +00:00
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
if (! is_EISDIR (errno, file))
|
|
|
|
suppressible_error (filename, errno);
|
1999-11-21 01:02:48 +00:00
|
|
|
goto finish_grep;
|
|
|
|
}
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
|
|
|
if (residue)
|
|
|
|
{
|
2000-01-31 13:28:57 +00:00
|
|
|
*buflim++ = eol;
|
2004-07-04 10:02:03 +00:00
|
|
|
if (outleft)
|
2005-05-14 03:02:22 +00:00
|
|
|
nlines += grepbuf (bufbeg + save - residue, buflim);
|
1999-11-20 09:40:28 +00:00
|
|
|
if (pending)
|
2005-05-14 03:02:22 +00:00
|
|
|
prpending (buflim);
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
1999-11-21 01:02:48 +00:00
|
|
|
|
|
|
|
finish_grep:
|
|
|
|
done_on_match -= not_text;
|
|
|
|
out_quiet -= not_text;
|
|
|
|
if ((not_text & ~out_quiet) && nlines != 0)
|
|
|
|
printf (_("Binary file %s matches\n"), filename);
|
1999-11-20 09:40:28 +00:00
|
|
|
return nlines;
|
|
|
|
}
|
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
static int
|
2000-01-31 13:28:57 +00:00
|
|
|
grepfile (char const *file, struct stats *stats)
|
1999-11-21 01:02:48 +00:00
|
|
|
{
|
|
|
|
int desc;
|
|
|
|
int count;
|
|
|
|
int status;
|
2013-01-22 03:23:14 +00:00
|
|
|
int flags;
|
1999-11-21 01:02:48 +00:00
|
|
|
|
|
|
|
if (! file)
|
|
|
|
{
|
|
|
|
desc = 0;
|
2004-07-04 10:02:03 +00:00
|
|
|
filename = label ? label : _("(standard input)");
|
1999-11-21 01:02:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-22 03:23:14 +00:00
|
|
|
while ((desc = open (file, O_RDONLY | O_NONBLOCK)) < 0 && errno == EINTR)
|
2000-01-04 03:25:40 +00:00
|
|
|
continue;
|
1999-11-21 01:02:48 +00:00
|
|
|
|
|
|
|
if (desc < 0)
|
|
|
|
{
|
|
|
|
int e = errno;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
if (is_EISDIR (e, file) && directories == RECURSE_DIRECTORIES)
|
|
|
|
{
|
|
|
|
if (stat (file, &stats->stat) != 0)
|
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
error (0, errno, "%s", file);
|
1999-11-21 01:02:48 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return grepdir (file, stats);
|
|
|
|
}
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
if (!suppress_errors)
|
|
|
|
{
|
|
|
|
if (directories == SKIP_DIRECTORIES)
|
|
|
|
switch (e)
|
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
#if defined(EISDIR)
|
1999-11-21 01:02:48 +00:00
|
|
|
case EISDIR:
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
case EACCES:
|
|
|
|
/* When skipping directories, don't worry about
|
|
|
|
directories that can't be opened. */
|
2004-07-04 10:02:03 +00:00
|
|
|
if (isdir (file))
|
1999-11-21 01:02:48 +00:00
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
suppressible_error (file, e);
|
1999-11-21 01:02:48 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-01-22 03:23:14 +00:00
|
|
|
flags = fcntl(desc, F_GETFL);
|
|
|
|
flags &= ~O_NONBLOCK;
|
|
|
|
fcntl(desc, F_SETFL, flags);
|
1999-11-21 01:02:48 +00:00
|
|
|
filename = file;
|
|
|
|
}
|
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
#if defined(SET_BINARY)
|
1999-11-21 01:02:48 +00:00
|
|
|
/* Set input to binary mode. Pipes are simulated with files
|
|
|
|
on DOS, so this includes the case of "foo | grep bar". */
|
|
|
|
if (!isatty (desc))
|
|
|
|
SET_BINARY (desc);
|
1999-11-20 09:40:28 +00:00
|
|
|
#endif
|
1999-11-21 01:02:48 +00:00
|
|
|
|
|
|
|
count = grep (desc, file, stats);
|
|
|
|
if (count < 0)
|
|
|
|
status = count + 2;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (count_matches)
|
|
|
|
{
|
|
|
|
if (out_file)
|
2000-01-04 03:25:40 +00:00
|
|
|
printf ("%s%c", filename, ':' & filename_mask);
|
1999-11-21 01:02:48 +00:00
|
|
|
printf ("%d\n", count);
|
|
|
|
}
|
|
|
|
|
2000-01-04 03:25:40 +00:00
|
|
|
status = !count;
|
|
|
|
if (list_files == 1 - 2 * status)
|
|
|
|
printf ("%s%c", filename, '\n' & filename_mask);
|
1999-11-21 01:02:48 +00:00
|
|
|
|
2002-10-06 07:02:30 +00:00
|
|
|
if (BZflag && bzbufdesc)
|
2002-09-16 04:27:29 +00:00
|
|
|
BZ2_bzclose(bzbufdesc);
|
|
|
|
else
|
1999-11-20 09:40:28 +00:00
|
|
|
#if HAVE_LIBZ > 0
|
1999-11-21 01:02:48 +00:00
|
|
|
if (Zflag)
|
|
|
|
gzclose(gzbufdesc);
|
|
|
|
else
|
1999-11-20 09:40:28 +00:00
|
|
|
#endif
|
2004-07-04 10:02:03 +00:00
|
|
|
if (! file)
|
|
|
|
{
|
|
|
|
off_t required_offset = outleft ? bufoffset : after_last_match;
|
|
|
|
if ((bufmapped || required_offset != bufoffset)
|
|
|
|
&& lseek (desc, required_offset, SEEK_SET) < 0
|
|
|
|
&& S_ISREG (stats->stat.st_mode))
|
|
|
|
error (0, errno, "%s", filename);
|
|
|
|
}
|
|
|
|
else
|
2000-01-04 03:25:40 +00:00
|
|
|
while (close (desc) != 0)
|
|
|
|
if (errno != EINTR)
|
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
error (0, errno, "%s", file);
|
2000-01-04 03:25:40 +00:00
|
|
|
break;
|
|
|
|
}
|
1999-11-21 01:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-07-04 10:02:03 +00:00
|
|
|
grepdir (char const *dir, struct stats const *stats)
|
1999-11-21 01:02:48 +00:00
|
|
|
{
|
|
|
|
int status = 1;
|
2004-07-04 10:02:03 +00:00
|
|
|
struct stats const *ancestor;
|
1999-11-21 01:02:48 +00:00
|
|
|
char *name_space;
|
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
/* Mingw32 does not support st_ino. No known working hosts use zero
|
|
|
|
for st_ino, so assume that the Mingw32 bug applies if it's zero. */
|
|
|
|
if (stats->stat.st_ino)
|
|
|
|
for (ancestor = stats; (ancestor = ancestor->parent) != 0; )
|
|
|
|
if (ancestor->stat.st_ino == stats->stat.st_ino
|
|
|
|
&& ancestor->stat.st_dev == stats->stat.st_dev)
|
|
|
|
{
|
|
|
|
if (!suppress_errors)
|
2005-02-13 23:07:30 +00:00
|
|
|
error (0, 0, _("warning: %s: %s"), dir,
|
1999-11-21 01:02:48 +00:00
|
|
|
_("recursive directory loop"));
|
2004-07-04 10:02:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
1999-11-21 01:02:48 +00:00
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
name_space = savedir (dir, stats->stat.st_size, included_patterns,
|
|
|
|
excluded_patterns);
|
1999-11-21 01:02:48 +00:00
|
|
|
|
|
|
|
if (! name_space)
|
|
|
|
{
|
|
|
|
if (errno)
|
2004-07-04 10:02:03 +00:00
|
|
|
suppressible_error (dir, errno);
|
1999-11-21 01:02:48 +00:00
|
|
|
else
|
2004-07-04 10:02:03 +00:00
|
|
|
xalloc_die ();
|
1999-11-21 01:02:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t dirlen = strlen (dir);
|
|
|
|
int needs_slash = ! (dirlen == FILESYSTEM_PREFIX_LEN (dir)
|
|
|
|
|| IS_SLASH (dir[dirlen - 1]));
|
|
|
|
char *file = NULL;
|
2004-07-04 10:02:03 +00:00
|
|
|
char const *namep = name_space;
|
1999-11-21 01:02:48 +00:00
|
|
|
struct stats child;
|
|
|
|
child.parent = stats;
|
|
|
|
out_file += !no_filenames;
|
|
|
|
while (*namep)
|
|
|
|
{
|
|
|
|
size_t namelen = strlen (namep);
|
|
|
|
file = xrealloc (file, dirlen + 1 + namelen + 1);
|
|
|
|
strcpy (file, dir);
|
|
|
|
file[dirlen] = '/';
|
|
|
|
strcpy (file + dirlen + needs_slash, namep);
|
|
|
|
namep += namelen + 1;
|
|
|
|
status &= grepfile (file, &child);
|
|
|
|
}
|
|
|
|
out_file -= !no_filenames;
|
|
|
|
if (file)
|
|
|
|
free (file);
|
|
|
|
free (name_space);
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
1999-11-20 09:40:28 +00:00
|
|
|
|
|
|
|
static void
|
2000-01-31 13:28:57 +00:00
|
|
|
usage (int status)
|
1999-11-20 09:40:28 +00:00
|
|
|
{
|
1999-11-21 01:02:48 +00:00
|
|
|
if (status != 0)
|
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
fprintf (stderr, _("Usage: %s [OPTION]... PATTERN [FILE]...\n"),
|
|
|
|
program_name);
|
|
|
|
fprintf (stderr, _("Try `%s --help' for more information.\n"),
|
|
|
|
program_name);
|
1999-11-21 01:02:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
printf (_("Usage: %s [OPTION]... PATTERN [FILE] ...\n"), program_name);
|
1999-11-21 01:02:48 +00:00
|
|
|
printf (_("\
|
|
|
|
Search for PATTERN in each FILE or standard input.\n\
|
2000-01-31 13:28:57 +00:00
|
|
|
Example: %s -i 'hello world' menu.h main.c\n\
|
1999-11-21 01:02:48 +00:00
|
|
|
\n\
|
2004-07-04 10:02:03 +00:00
|
|
|
Regexp selection and interpretation:\n"), program_name);
|
2000-01-04 03:25:40 +00:00
|
|
|
printf (_("\
|
1999-11-21 01:02:48 +00:00
|
|
|
-E, --extended-regexp PATTERN is an extended regular expression\n\
|
2000-01-04 03:25:40 +00:00
|
|
|
-F, --fixed-strings PATTERN is a set of newline-separated strings\n\
|
2004-07-04 10:02:03 +00:00
|
|
|
-G, --basic-regexp PATTERN is a basic regular expression\n\
|
|
|
|
-P, --perl-regexp PATTERN is a Perl regular expression\n"));
|
2000-01-04 03:25:40 +00:00
|
|
|
printf (_("\
|
1999-11-21 01:02:48 +00:00
|
|
|
-e, --regexp=PATTERN use PATTERN as a regular expression\n\
|
|
|
|
-f, --file=FILE obtain PATTERN from FILE\n\
|
|
|
|
-i, --ignore-case ignore case distinctions\n\
|
|
|
|
-w, --word-regexp force PATTERN to match only whole words\n\
|
2000-01-04 03:25:40 +00:00
|
|
|
-x, --line-regexp force PATTERN to match only whole lines\n\
|
|
|
|
-z, --null-data a data line ends in 0 byte, not newline\n"));
|
1999-11-21 01:02:48 +00:00
|
|
|
printf (_("\
|
|
|
|
\n\
|
|
|
|
Miscellaneous:\n\
|
|
|
|
-s, --no-messages suppress error messages\n\
|
2000-01-04 03:25:40 +00:00
|
|
|
-v, --invert-match select non-matching lines\n\
|
1999-11-21 01:02:48 +00:00
|
|
|
-V, --version print version information and exit\n\
|
2000-01-04 03:25:40 +00:00
|
|
|
--help display this help and exit\n\
|
2002-09-16 04:27:29 +00:00
|
|
|
-J, --bz2decompress decompress bzip2'ed input before searching\n\
|
1999-11-21 01:02:48 +00:00
|
|
|
-Z, --decompress decompress input before searching (HAVE_LIBZ=1)\n\
|
2000-01-04 03:25:40 +00:00
|
|
|
--mmap use memory-mapped input if possible\n"));
|
1999-11-21 01:02:48 +00:00
|
|
|
printf (_("\
|
|
|
|
\n\
|
|
|
|
Output control:\n\
|
2004-07-04 10:02:03 +00:00
|
|
|
-m, --max-count=NUM stop after NUM matches\n\
|
1999-11-21 01:02:48 +00:00
|
|
|
-b, --byte-offset print the byte offset with output lines\n\
|
|
|
|
-n, --line-number print line number with output lines\n\
|
2004-07-04 10:02:03 +00:00
|
|
|
--line-buffered flush output on every line\n\
|
1999-11-21 01:02:48 +00:00
|
|
|
-H, --with-filename print the filename for each match\n\
|
|
|
|
-h, --no-filename suppress the prefixing filename on output\n\
|
2004-07-04 10:02:03 +00:00
|
|
|
--label=LABEL print LABEL as filename for standard input\n\
|
|
|
|
-o, --only-matching show only the part of a line matching PATTERN\n\
|
1999-11-21 01:02:48 +00:00
|
|
|
-q, --quiet, --silent suppress all normal output\n\
|
2000-01-18 09:46:39 +00:00
|
|
|
--binary-files=TYPE assume that binary files are TYPE\n\
|
2004-07-04 10:02:03 +00:00
|
|
|
TYPE is 'binary', 'text', or 'without-match'\n\
|
2000-01-31 13:28:57 +00:00
|
|
|
-a, --text equivalent to --binary-files=text\n\
|
|
|
|
-I equivalent to --binary-files=without-match\n\
|
1999-11-21 01:02:48 +00:00
|
|
|
-d, --directories=ACTION how to handle directories\n\
|
2004-07-04 10:02:03 +00:00
|
|
|
ACTION is 'read', 'recurse', or 'skip'\n\
|
|
|
|
-D, --devices=ACTION how to handle devices, FIFOs and sockets\n\
|
|
|
|
ACTION is 'read' or 'skip'\n\
|
|
|
|
-R, -r, --recursive equivalent to --directories=recurse\n\
|
|
|
|
--include=PATTERN files that match PATTERN will be examined\n\
|
|
|
|
--exclude=PATTERN files that match PATTERN will be skipped.\n\
|
|
|
|
--exclude-from=FILE files that match PATTERN in FILE will be skipped.\n\
|
1999-11-21 01:02:48 +00:00
|
|
|
-L, --files-without-match only print FILE names containing no match\n\
|
|
|
|
-l, --files-with-matches only print FILE names containing matches\n\
|
2000-01-04 03:25:40 +00:00
|
|
|
-c, --count only print a count of matching lines per FILE\n\
|
|
|
|
--null print 0 byte after FILE name\n"));
|
1999-11-21 01:02:48 +00:00
|
|
|
printf (_("\
|
|
|
|
\n\
|
|
|
|
Context control:\n\
|
|
|
|
-B, --before-context=NUM print NUM lines of leading context\n\
|
|
|
|
-A, --after-context=NUM print NUM lines of trailing context\n\
|
2004-07-04 10:02:03 +00:00
|
|
|
-C, --context=NUM print NUM lines of output context\n\
|
1999-11-21 01:02:48 +00:00
|
|
|
-NUM same as --context=NUM\n\
|
2004-07-04 10:02:03 +00:00
|
|
|
--color[=WHEN],\n\
|
|
|
|
--colour[=WHEN] use markers to distinguish the matching string\n\
|
|
|
|
WHEN may be `always', `never' or `auto'.\n\
|
1999-11-21 01:02:48 +00:00
|
|
|
-U, --binary do not strip CR characters at EOL (MSDOS)\n\
|
|
|
|
-u, --unix-byte-offsets report offsets as if CRs were not there (MSDOS)\n\
|
|
|
|
\n\
|
2000-01-04 03:25:40 +00:00
|
|
|
`egrep' means `grep -E'. `fgrep' means `grep -F'.\n\
|
|
|
|
With no FILE, or when FILE is -, read standard input. If less than\n\
|
|
|
|
two FILEs given, assume -h. Exit status is 0 if match, 1 if no match,\n\
|
|
|
|
and 2 if trouble.\n"));
|
1999-11-21 01:02:48 +00:00
|
|
|
printf (_("\nReport bugs to <bug-gnu-utils@gnu.org>.\n"));
|
|
|
|
}
|
|
|
|
exit (status);
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
|
|
|
|
2000-01-04 03:25:40 +00:00
|
|
|
/* Set the matcher to M, reporting any conflicts. */
|
|
|
|
static void
|
2000-01-31 13:28:57 +00:00
|
|
|
setmatcher (char const *m)
|
2000-01-04 03:25:40 +00:00
|
|
|
{
|
|
|
|
if (matcher && strcmp (matcher, m) != 0)
|
2004-07-04 10:02:03 +00:00
|
|
|
error (2, 0, _("conflicting matchers specified"));
|
2000-01-04 03:25:40 +00:00
|
|
|
matcher = m;
|
|
|
|
}
|
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
/* Go through the matchers vector and look for the specified matcher.
|
|
|
|
If we find it, install it in compile and execute, and return 1. */
|
1999-11-21 01:02:48 +00:00
|
|
|
static int
|
2000-01-31 13:28:57 +00:00
|
|
|
install_matcher (char const *name)
|
1999-11-20 09:40:28 +00:00
|
|
|
{
|
|
|
|
int i;
|
2004-07-04 10:02:03 +00:00
|
|
|
#if defined(HAVE_SETRLIMIT)
|
1999-11-21 01:02:48 +00:00
|
|
|
struct rlimit rlim;
|
|
|
|
#endif
|
1999-11-20 09:40:28 +00:00
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
for (i = 0; matchers[i].compile; i++)
|
1999-11-21 01:02:48 +00:00
|
|
|
if (strcmp (name, matchers[i].name) == 0)
|
1999-11-20 09:40:28 +00:00
|
|
|
{
|
|
|
|
compile = matchers[i].compile;
|
|
|
|
execute = matchers[i].execute;
|
2004-07-04 10:02:03 +00:00
|
|
|
#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_STACK)
|
1999-11-21 01:02:48 +00:00
|
|
|
/* I think every platform needs to do this, so that regex.c
|
|
|
|
doesn't oveflow the stack. The default value of
|
|
|
|
`re_max_failures' is too large for some platforms: it needs
|
|
|
|
more than 3MB-large stack.
|
|
|
|
|
|
|
|
The test for HAVE_SETRLIMIT should go into `configure'. */
|
|
|
|
if (!getrlimit (RLIMIT_STACK, &rlim))
|
|
|
|
{
|
|
|
|
long newlim;
|
|
|
|
extern long int re_max_failures; /* from regex.c */
|
|
|
|
|
|
|
|
/* Approximate the amount regex.c needs, plus some more. */
|
|
|
|
newlim = re_max_failures * 2 * 20 * sizeof (char *);
|
|
|
|
if (newlim > rlim.rlim_max)
|
|
|
|
{
|
|
|
|
newlim = rlim.rlim_max;
|
|
|
|
re_max_failures = newlim / (2 * 20 * sizeof (char *));
|
|
|
|
}
|
|
|
|
if (rlim.rlim_cur < newlim)
|
2004-07-04 10:02:03 +00:00
|
|
|
{
|
|
|
|
rlim.rlim_cur = newlim;
|
|
|
|
setrlimit (RLIMIT_STACK, &rlim);
|
|
|
|
}
|
1999-11-21 01:02:48 +00:00
|
|
|
}
|
|
|
|
#endif
|
1999-11-20 09:40:28 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-11-26 03:26:59 +00:00
|
|
|
/* Find the white-space-separated options specified by OPTIONS, and
|
|
|
|
using BUF to store copies of these options, set ARGV[0], ARGV[1],
|
|
|
|
etc. to the option copies. Return the number N of options found.
|
|
|
|
Do not set ARGV[N] to NULL. If ARGV is NULL, do not store ARGV[0]
|
|
|
|
etc. Backslash can be used to escape whitespace (and backslashes). */
|
|
|
|
static int
|
2000-01-31 13:28:57 +00:00
|
|
|
prepend_args (char const *options, char *buf, char **argv)
|
1999-11-26 03:26:59 +00:00
|
|
|
{
|
|
|
|
char const *o = options;
|
|
|
|
char *b = buf;
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
while (ISSPACE ((unsigned char) *o))
|
|
|
|
o++;
|
|
|
|
if (!*o)
|
|
|
|
return n;
|
|
|
|
if (argv)
|
|
|
|
argv[n] = b;
|
|
|
|
n++;
|
|
|
|
|
|
|
|
do
|
|
|
|
if ((*b++ = *o++) == '\\' && *o)
|
|
|
|
b[-1] = *o++;
|
|
|
|
while (*o && ! ISSPACE ((unsigned char) *o));
|
|
|
|
|
|
|
|
*b++ = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prepend the whitespace-separated options in OPTIONS to the argument
|
|
|
|
vector of a main program with argument count *PARGC and argument
|
|
|
|
vector *PARGV. */
|
|
|
|
static void
|
2000-01-31 13:28:57 +00:00
|
|
|
prepend_default_options (char const *options, int *pargc, char ***pargv)
|
1999-11-26 03:26:59 +00:00
|
|
|
{
|
|
|
|
if (options)
|
|
|
|
{
|
|
|
|
char *buf = xmalloc (strlen (options) + 1);
|
|
|
|
int prepended = prepend_args (options, buf, (char **) NULL);
|
|
|
|
int argc = *pargc;
|
|
|
|
char * const *argv = *pargv;
|
|
|
|
char **pp = (char **) xmalloc ((prepended + argc + 1) * sizeof *pp);
|
|
|
|
*pargc = prepended + argc;
|
|
|
|
*pargv = pp;
|
|
|
|
*pp++ = *argv++;
|
|
|
|
pp += prepend_args (options, buf, pp);
|
|
|
|
while ((*pp++ = *argv++))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
/* Get the next non-digit option from ARGC and ARGV.
|
|
|
|
Return -1 if there are no more options.
|
|
|
|
Process any digit options that were encountered on the way,
|
|
|
|
and store the resulting integer into *DEFAULT_CONTEXT. */
|
|
|
|
static int
|
|
|
|
get_nondigit_option (int argc, char *const *argv, int *default_context)
|
|
|
|
{
|
|
|
|
int opt;
|
|
|
|
char buf[sizeof (uintmax_t) * CHAR_BIT + 4];
|
|
|
|
char *p = buf;
|
|
|
|
|
|
|
|
/* Set buf[0] to anything but '0', for the leading-zero test below. */
|
|
|
|
buf[0] = '\0';
|
|
|
|
|
|
|
|
while (opt = getopt_long (argc, argv, short_options, long_options, NULL),
|
|
|
|
'0' <= opt && opt <= '9')
|
|
|
|
{
|
|
|
|
/* Suppress trivial leading zeros, to avoid incorrect
|
|
|
|
diagnostic on strings like 00000000000. */
|
|
|
|
p -= buf[0] == '0';
|
|
|
|
|
|
|
|
*p++ = opt;
|
|
|
|
if (p == buf + sizeof buf - 4)
|
|
|
|
{
|
|
|
|
/* Too many digits. Append "..." to make context_length_arg
|
|
|
|
complain about "X...", where X contains the digits seen
|
|
|
|
so far. */
|
|
|
|
strcpy (p, "...");
|
|
|
|
p += 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p != buf)
|
|
|
|
{
|
|
|
|
*p = '\0';
|
|
|
|
context_length_arg (buf, default_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
return opt;
|
|
|
|
}
|
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
int
|
2000-01-31 13:28:57 +00:00
|
|
|
main (int argc, char **argv)
|
1999-11-20 09:40:28 +00:00
|
|
|
{
|
|
|
|
char *keys;
|
|
|
|
size_t keycc, oldcc, keyalloc;
|
1999-11-21 01:02:48 +00:00
|
|
|
int with_filenames;
|
|
|
|
int opt, cc, status;
|
2000-01-31 13:28:57 +00:00
|
|
|
int default_context;
|
1999-11-20 09:40:28 +00:00
|
|
|
FILE *fp;
|
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
1999-11-21 01:02:48 +00:00
|
|
|
|
|
|
|
initialize_main (&argc, &argv);
|
2004-07-04 10:02:03 +00:00
|
|
|
program_name = argv[0];
|
|
|
|
if (program_name && strrchr (program_name, '/'))
|
|
|
|
program_name = strrchr (program_name, '/') + 1;
|
1999-11-21 01:02:48 +00:00
|
|
|
|
2005-10-21 15:17:18 +00:00
|
|
|
if (program_name[0] == 'b' && program_name[1] == 'z') {
|
2005-10-20 21:50:44 +00:00
|
|
|
BZflag = 1;
|
|
|
|
program_name += 2;
|
|
|
|
}
|
1999-11-21 01:02:48 +00:00
|
|
|
#if HAVE_LIBZ > 0
|
2005-10-21 15:17:18 +00:00
|
|
|
else if (program_name[0] == 'z') {
|
1999-11-21 01:02:48 +00:00
|
|
|
Zflag = 1;
|
2004-07-04 10:02:03 +00:00
|
|
|
++program_name;
|
1999-11-21 01:02:48 +00:00
|
|
|
}
|
1999-11-20 09:40:28 +00:00
|
|
|
#endif
|
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
#if defined(__MSDOS__) || defined(_WIN32)
|
|
|
|
/* DOS and MS-Windows use backslashes as directory separators, and usually
|
|
|
|
have an .exe suffix. They also have case-insensitive filesystems. */
|
2004-07-04 10:02:03 +00:00
|
|
|
if (program_name)
|
1999-11-21 01:02:48 +00:00
|
|
|
{
|
2004-07-04 10:02:03 +00:00
|
|
|
char *p = program_name;
|
1999-11-21 01:02:48 +00:00
|
|
|
char *bslash = strrchr (argv[0], '\\');
|
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
if (bslash && bslash >= program_name) /* for mixed forward/backslash case */
|
|
|
|
program_name = bslash + 1;
|
|
|
|
else if (program_name == argv[0]
|
1999-11-21 01:02:48 +00:00
|
|
|
&& argv[0][0] && argv[0][1] == ':') /* "c:progname" */
|
2004-07-04 10:02:03 +00:00
|
|
|
program_name = argv[0] + 2;
|
1999-11-21 01:02:48 +00:00
|
|
|
|
|
|
|
/* Collapse the letter-case, so `strcmp' could be used hence. */
|
|
|
|
for ( ; *p; p++)
|
|
|
|
if (*p >= 'A' && *p <= 'Z')
|
|
|
|
*p += 'a' - 'A';
|
|
|
|
|
|
|
|
/* Remove the .exe extension, if any. */
|
2004-07-04 10:02:03 +00:00
|
|
|
if ((p = strrchr (program_name, '.')) && strcmp (p, ".exe") == 0)
|
1999-11-21 01:02:48 +00:00
|
|
|
*p = '\0';
|
|
|
|
}
|
1999-11-20 09:40:28 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
keys = NULL;
|
|
|
|
keycc = 0;
|
1999-11-21 01:02:48 +00:00
|
|
|
with_filenames = 0;
|
2000-01-04 03:25:40 +00:00
|
|
|
eolbyte = '\n';
|
|
|
|
filename_mask = ~0;
|
1999-11-21 01:02:48 +00:00
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
max_count = TYPE_MAXIMUM (off_t);
|
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
/* The value -1 means to use DEFAULT_CONTEXT. */
|
|
|
|
out_after = out_before = -1;
|
|
|
|
/* Default before/after context: chaged by -C/-NUM options */
|
|
|
|
default_context = 0;
|
2004-07-04 10:02:03 +00:00
|
|
|
/* Changed by -o option */
|
|
|
|
only_matching = 0;
|
1999-11-21 01:02:48 +00:00
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
/* Internationalization. */
|
|
|
|
#if defined(HAVE_SETLOCALE)
|
1999-11-21 01:02:48 +00:00
|
|
|
setlocale (LC_ALL, "");
|
1999-11-20 09:40:28 +00:00
|
|
|
#endif
|
2004-07-04 10:02:03 +00:00
|
|
|
#if defined(ENABLE_NLS)
|
1999-11-21 01:02:48 +00:00
|
|
|
bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
textdomain (PACKAGE);
|
1999-11-20 09:40:28 +00:00
|
|
|
#endif
|
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
atexit (close_stdout);
|
|
|
|
|
1999-11-26 03:26:59 +00:00
|
|
|
prepend_default_options (getenv ("GREP_OPTIONS"), &argc, &argv);
|
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
while ((opt = get_nondigit_option (argc, argv, &default_context)) != -1)
|
1999-11-20 09:40:28 +00:00
|
|
|
switch (opt)
|
|
|
|
{
|
|
|
|
case 'A':
|
2004-07-04 10:02:03 +00:00
|
|
|
context_length_arg (optarg, &out_after);
|
1999-11-20 09:40:28 +00:00
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
case 'B':
|
2004-07-04 10:02:03 +00:00
|
|
|
context_length_arg (optarg, &out_before);
|
1999-11-20 09:40:28 +00:00
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
case 'C':
|
1999-11-21 01:02:48 +00:00
|
|
|
/* Set output match context, but let any explicit leading or
|
|
|
|
trailing amount specified with -A or -B stand. */
|
2004-07-04 10:02:03 +00:00
|
|
|
context_length_arg (optarg, &default_context);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'D':
|
|
|
|
if (strcmp (optarg, "read") == 0)
|
|
|
|
devices = READ_DEVICES;
|
|
|
|
else if (strcmp (optarg, "skip") == 0)
|
|
|
|
devices = SKIP_DEVICES;
|
1999-11-21 01:02:48 +00:00
|
|
|
else
|
2004-07-04 10:02:03 +00:00
|
|
|
error (2, 0, _("unknown devices method"));
|
1999-11-20 09:40:28 +00:00
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
case 'E':
|
2000-01-04 03:25:40 +00:00
|
|
|
setmatcher ("egrep");
|
1999-11-20 09:40:28 +00:00
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
case 'F':
|
2000-01-04 03:25:40 +00:00
|
|
|
setmatcher ("fgrep");
|
1999-11-20 09:40:28 +00:00
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
|
|
|
case 'P':
|
|
|
|
setmatcher ("perl");
|
|
|
|
break;
|
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
case 'G':
|
2000-01-04 03:25:40 +00:00
|
|
|
setmatcher ("grep");
|
1999-11-20 09:40:28 +00:00
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
case 'H':
|
|
|
|
with_filenames = 1;
|
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
2000-01-31 13:28:57 +00:00
|
|
|
case 'I':
|
|
|
|
binary_files = WITHOUT_MATCH_BINARY_FILES;
|
|
|
|
break;
|
2002-09-16 04:27:29 +00:00
|
|
|
case 'J':
|
2002-10-06 07:02:30 +00:00
|
|
|
if (Zflag)
|
|
|
|
{
|
|
|
|
printf (_("Cannot mix -Z and -J.\n"));
|
|
|
|
usage (2);
|
|
|
|
}
|
2002-09-16 04:27:29 +00:00
|
|
|
BZflag = 1;
|
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
case 'U':
|
2004-07-04 10:02:03 +00:00
|
|
|
#if defined(HAVE_DOS_FILE_CONTENTS)
|
1999-11-21 01:02:48 +00:00
|
|
|
dos_use_file_type = DOS_BINARY;
|
2000-01-04 03:25:40 +00:00
|
|
|
#endif
|
1999-11-21 01:02:48 +00:00
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
case 'u':
|
2004-07-04 10:02:03 +00:00
|
|
|
#if defined(HAVE_DOS_FILE_CONTENTS)
|
1999-11-21 01:02:48 +00:00
|
|
|
dos_report_unix_offset = 1;
|
|
|
|
#endif
|
2000-01-04 03:25:40 +00:00
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
case 'V':
|
1999-11-21 01:02:48 +00:00
|
|
|
show_version = 1;
|
1999-11-20 09:40:28 +00:00
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
case 'X':
|
2000-01-04 03:25:40 +00:00
|
|
|
setmatcher (optarg);
|
1999-11-20 09:40:28 +00:00
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
case 'a':
|
2000-01-18 09:46:39 +00:00
|
|
|
binary_files = TEXT_BINARY_FILES;
|
1999-11-20 09:40:28 +00:00
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
case 'b':
|
|
|
|
out_byte = 1;
|
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
case 'c':
|
|
|
|
count_matches = 1;
|
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
case 'd':
|
|
|
|
if (strcmp (optarg, "read") == 0)
|
|
|
|
directories = READ_DIRECTORIES;
|
|
|
|
else if (strcmp (optarg, "skip") == 0)
|
|
|
|
directories = SKIP_DIRECTORIES;
|
|
|
|
else if (strcmp (optarg, "recurse") == 0)
|
|
|
|
directories = RECURSE_DIRECTORIES;
|
|
|
|
else
|
2004-07-04 10:02:03 +00:00
|
|
|
error (2, 0, _("unknown directories method"));
|
1999-11-21 01:02:48 +00:00
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
case 'e':
|
1999-11-21 01:02:48 +00:00
|
|
|
cc = strlen (optarg);
|
|
|
|
keys = xrealloc (keys, keycc + cc + 1);
|
|
|
|
strcpy (&keys[keycc], optarg);
|
1999-11-20 09:40:28 +00:00
|
|
|
keycc += cc;
|
1999-11-21 01:02:48 +00:00
|
|
|
keys[keycc++] = '\n';
|
1999-11-20 09:40:28 +00:00
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
case 'f':
|
1999-11-21 01:02:48 +00:00
|
|
|
fp = strcmp (optarg, "-") != 0 ? fopen (optarg, "r") : stdin;
|
1999-11-20 09:40:28 +00:00
|
|
|
if (!fp)
|
2004-07-04 10:02:03 +00:00
|
|
|
error (2, errno, "%s", optarg);
|
1999-11-21 01:02:48 +00:00
|
|
|
for (keyalloc = 1; keyalloc <= keycc + 1; keyalloc *= 2)
|
1999-11-20 09:40:28 +00:00
|
|
|
;
|
1999-11-21 01:02:48 +00:00
|
|
|
keys = xrealloc (keys, keyalloc);
|
1999-11-20 09:40:28 +00:00
|
|
|
oldcc = keycc;
|
1999-11-21 01:02:48 +00:00
|
|
|
while (!feof (fp)
|
|
|
|
&& (cc = fread (keys + keycc, 1, keyalloc - 1 - keycc, fp)) > 0)
|
1999-11-20 09:40:28 +00:00
|
|
|
{
|
|
|
|
keycc += cc;
|
1999-11-21 01:02:48 +00:00
|
|
|
if (keycc == keyalloc - 1)
|
|
|
|
keys = xrealloc (keys, keyalloc *= 2);
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
|
|
|
if (fp != stdin)
|
|
|
|
fclose(fp);
|
1999-11-21 01:02:48 +00:00
|
|
|
/* Append final newline if file ended in non-newline. */
|
|
|
|
if (oldcc != keycc && keys[keycc - 1] != '\n')
|
|
|
|
keys[keycc++] = '\n';
|
1999-11-20 09:40:28 +00:00
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
case 'h':
|
|
|
|
no_filenames = 1;
|
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
case 'i':
|
|
|
|
case 'y': /* For old-timers . . . */
|
|
|
|
match_icase = 1;
|
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
case 'L':
|
|
|
|
/* Like -l, except list files that don't contain matches.
|
|
|
|
Inspired by the same option in Hume's gre. */
|
|
|
|
list_files = -1;
|
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
case 'l':
|
|
|
|
list_files = 1;
|
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
|
|
|
case 'm':
|
|
|
|
{
|
|
|
|
uintmax_t value;
|
|
|
|
switch (xstrtoumax (optarg, 0, 10, &value, ""))
|
|
|
|
{
|
|
|
|
case LONGINT_OK:
|
|
|
|
max_count = value;
|
|
|
|
if (0 <= max_count && max_count == value)
|
|
|
|
break;
|
|
|
|
/* Fall through. */
|
|
|
|
case LONGINT_OVERFLOW:
|
|
|
|
max_count = TYPE_MAXIMUM (off_t);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error (2, 0, _("invalid max count"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
case 'n':
|
|
|
|
out_line = 1;
|
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
|
|
|
case 'o':
|
|
|
|
only_matching = 1;
|
|
|
|
break;
|
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
case 'q':
|
2004-07-04 10:02:03 +00:00
|
|
|
exit_on_match = 1;
|
|
|
|
close_stdout_set_status(0);
|
1999-11-20 09:40:28 +00:00
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-21 01:09:03 +00:00
|
|
|
case 'R':
|
1999-11-21 01:02:48 +00:00
|
|
|
case 'r':
|
|
|
|
directories = RECURSE_DIRECTORIES;
|
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
case 's':
|
|
|
|
suppress_errors = 1;
|
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
case 'v':
|
|
|
|
out_invert = 1;
|
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
case 'w':
|
|
|
|
match_words = 1;
|
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
case 'x':
|
|
|
|
match_lines = 1;
|
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
2000-01-04 03:25:40 +00:00
|
|
|
case 'Z':
|
|
|
|
#if HAVE_LIBZ > 0
|
2002-10-06 07:02:30 +00:00
|
|
|
if (BZflag)
|
|
|
|
{
|
|
|
|
printf (_("Cannot mix -J and -Z.\n"));
|
|
|
|
usage (2);
|
|
|
|
}
|
2000-01-04 03:25:40 +00:00
|
|
|
Zflag = 1;
|
|
|
|
#else
|
|
|
|
filename_mask = 0;
|
|
|
|
#endif
|
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
2000-01-04 03:25:40 +00:00
|
|
|
case 'z':
|
|
|
|
eolbyte = '\0';
|
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
2000-01-18 09:46:39 +00:00
|
|
|
case BINARY_FILES_OPTION:
|
|
|
|
if (strcmp (optarg, "binary") == 0)
|
|
|
|
binary_files = BINARY_BINARY_FILES;
|
|
|
|
else if (strcmp (optarg, "text") == 0)
|
|
|
|
binary_files = TEXT_BINARY_FILES;
|
|
|
|
else if (strcmp (optarg, "without-match") == 0)
|
|
|
|
binary_files = WITHOUT_MATCH_BINARY_FILES;
|
|
|
|
else
|
2004-07-04 10:02:03 +00:00
|
|
|
error (2, 0, _("unknown binary-files type"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case COLOR_OPTION:
|
|
|
|
if(optarg) {
|
|
|
|
if(!strcasecmp(optarg, "always") || !strcasecmp(optarg, "yes") ||
|
|
|
|
!strcasecmp(optarg, "force"))
|
|
|
|
color_option = 1;
|
|
|
|
else if(!strcasecmp(optarg, "never") || !strcasecmp(optarg, "no") ||
|
|
|
|
!strcasecmp(optarg, "none"))
|
|
|
|
color_option = 0;
|
|
|
|
else if(!strcasecmp(optarg, "auto") || !strcasecmp(optarg, "tty") ||
|
|
|
|
!strcasecmp(optarg, "if-tty"))
|
|
|
|
color_option = 2;
|
|
|
|
else
|
|
|
|
show_help = 1;
|
|
|
|
} else
|
|
|
|
color_option = 2;
|
|
|
|
if(color_option == 2) {
|
|
|
|
if(isatty(STDOUT_FILENO) && getenv("TERM") &&
|
|
|
|
strcmp(getenv("TERM"), "dumb"))
|
|
|
|
color_option = 1;
|
|
|
|
else
|
|
|
|
color_option = 0;
|
|
|
|
}
|
2000-01-18 09:46:39 +00:00
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
|
|
|
case EXCLUDE_OPTION:
|
|
|
|
if (!excluded_patterns)
|
|
|
|
excluded_patterns = new_exclude ();
|
|
|
|
add_exclude (excluded_patterns, optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EXCLUDE_FROM_OPTION:
|
|
|
|
if (!excluded_patterns)
|
|
|
|
excluded_patterns = new_exclude ();
|
|
|
|
if (add_exclude_file (add_exclude, excluded_patterns, optarg, '\n')
|
|
|
|
!= 0)
|
|
|
|
{
|
|
|
|
error (2, errno, "%s", optarg);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INCLUDE_OPTION:
|
|
|
|
if (!included_patterns)
|
|
|
|
included_patterns = new_exclude ();
|
|
|
|
add_exclude (included_patterns, optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LINE_BUFFERED_OPTION:
|
|
|
|
line_buffered = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LABEL_OPTION:
|
|
|
|
label = optarg;
|
|
|
|
break;
|
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
case 0:
|
|
|
|
/* long options */
|
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
default:
|
1999-11-21 01:02:48 +00:00
|
|
|
usage (2);
|
1999-11-20 09:40:28 +00:00
|
|
|
break;
|
2004-07-04 10:02:03 +00:00
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
/* POSIX.2 says that -q overrides -l, which in turn overrides the
|
|
|
|
other output options. */
|
|
|
|
if (exit_on_match)
|
|
|
|
list_files = 0;
|
|
|
|
if (exit_on_match | list_files)
|
|
|
|
{
|
|
|
|
count_matches = 0;
|
|
|
|
done_on_match = 1;
|
|
|
|
}
|
|
|
|
out_quiet = count_matches | done_on_match;
|
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
if (out_after < 0)
|
|
|
|
out_after = default_context;
|
|
|
|
if (out_before < 0)
|
|
|
|
out_before = default_context;
|
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
if (color_option)
|
|
|
|
{
|
|
|
|
char *userval = getenv ("GREP_COLOR");
|
|
|
|
if (userval != NULL && *userval != '\0')
|
|
|
|
grep_color = userval;
|
|
|
|
}
|
|
|
|
|
2000-01-04 03:25:40 +00:00
|
|
|
if (! matcher)
|
2004-07-04 10:02:03 +00:00
|
|
|
matcher = program_name;
|
2000-01-04 03:25:40 +00:00
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
if (show_version)
|
|
|
|
{
|
2000-01-04 03:25:40 +00:00
|
|
|
printf (_("%s (GNU grep) %s\n"), matcher, VERSION);
|
1999-11-21 01:02:48 +00:00
|
|
|
printf ("\n");
|
|
|
|
printf (_("\
|
2004-07-04 10:02:03 +00:00
|
|
|
Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc.\n"));
|
1999-11-21 01:02:48 +00:00
|
|
|
printf (_("\
|
|
|
|
This is free software; see the source for copying conditions. There is NO\n\
|
|
|
|
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"));
|
|
|
|
printf ("\n");
|
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (show_help)
|
|
|
|
usage (0);
|
|
|
|
|
|
|
|
if (keys)
|
|
|
|
{
|
|
|
|
if (keycc == 0)
|
2004-07-04 10:02:03 +00:00
|
|
|
{
|
|
|
|
/* No keys were specified (e.g. -f /dev/null). Match nothing. */
|
|
|
|
out_invert ^= 1;
|
|
|
|
match_lines = match_words = 0;
|
|
|
|
}
|
1999-11-21 01:02:48 +00:00
|
|
|
else
|
|
|
|
/* Strip trailing newline. */
|
|
|
|
--keycc;
|
|
|
|
}
|
|
|
|
else
|
1999-11-20 09:40:28 +00:00
|
|
|
if (optind < argc)
|
|
|
|
{
|
|
|
|
keys = argv[optind++];
|
1999-11-21 01:02:48 +00:00
|
|
|
keycc = strlen (keys);
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
|
|
|
else
|
1999-11-21 01:02:48 +00:00
|
|
|
usage (2);
|
1999-11-20 09:40:28 +00:00
|
|
|
|
2000-01-04 03:25:40 +00:00
|
|
|
if (!install_matcher (matcher) && !install_matcher ("default"))
|
1999-11-21 01:02:48 +00:00
|
|
|
abort ();
|
1999-11-20 09:40:28 +00:00
|
|
|
|
2004-07-04 11:58:10 +00:00
|
|
|
#ifdef MBS_SUPPORT
|
|
|
|
if (MB_CUR_MAX != 1 && match_icase)
|
|
|
|
{
|
|
|
|
wchar_t wc;
|
|
|
|
mbstate_t cur_state, prev_state;
|
|
|
|
int i, len = strlen(keys);
|
|
|
|
|
|
|
|
memset(&cur_state, 0, sizeof(mbstate_t));
|
|
|
|
for (i = 0; i <= len ;)
|
|
|
|
{
|
|
|
|
size_t mbclen;
|
|
|
|
mbclen = mbrtowc(&wc, keys + i, len - i, &cur_state);
|
|
|
|
if (mbclen == (size_t) -1 || mbclen == (size_t) -2 || mbclen == 0)
|
|
|
|
{
|
|
|
|
/* An invalid sequence, or a truncated multibyte character.
|
|
|
|
We treat it as a singlebyte character. */
|
|
|
|
mbclen = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (iswupper((wint_t)wc))
|
|
|
|
{
|
|
|
|
wc = towlower((wint_t)wc);
|
|
|
|
wcrtomb(keys + i, wc, &cur_state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i += mbclen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MBS_SUPPORT */
|
|
|
|
|
1999-11-20 09:40:28 +00:00
|
|
|
(*compile)(keys, keycc);
|
|
|
|
|
1999-11-21 01:02:48 +00:00
|
|
|
if ((argc - optind > 1 && !no_filenames) || with_filenames)
|
1999-11-20 09:40:28 +00:00
|
|
|
out_file = 1;
|
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
#ifdef SET_BINARY
|
1999-11-21 01:02:48 +00:00
|
|
|
/* Output is set to binary mode because we shouldn't convert
|
|
|
|
NL to CR-LF pairs, especially when grepping binary files. */
|
|
|
|
if (!isatty (1))
|
|
|
|
SET_BINARY (1);
|
1999-11-20 09:40:28 +00:00
|
|
|
#endif
|
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
if (max_count == 0)
|
|
|
|
exit (1);
|
1999-11-20 09:40:28 +00:00
|
|
|
|
|
|
|
if (optind < argc)
|
|
|
|
{
|
1999-11-21 01:02:48 +00:00
|
|
|
status = 1;
|
|
|
|
do
|
1999-11-20 09:40:28 +00:00
|
|
|
{
|
1999-11-21 01:02:48 +00:00
|
|
|
char *file = argv[optind];
|
2004-07-04 10:02:03 +00:00
|
|
|
if ((included_patterns || excluded_patterns)
|
|
|
|
&& !isdir (file))
|
|
|
|
{
|
|
|
|
if (included_patterns &&
|
|
|
|
! excluded_filename (included_patterns, file, 0))
|
|
|
|
continue;
|
|
|
|
if (excluded_patterns &&
|
|
|
|
excluded_filename (excluded_patterns, file, 0))
|
|
|
|
continue;
|
|
|
|
}
|
1999-11-21 01:02:48 +00:00
|
|
|
status &= grepfile (strcmp (file, "-") == 0 ? (char *) NULL : file,
|
|
|
|
&stats_base);
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
1999-11-21 01:02:48 +00:00
|
|
|
while ( ++optind < argc);
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
1999-11-21 01:02:48 +00:00
|
|
|
else
|
|
|
|
status = grepfile ((char *) NULL, &stats_base);
|
|
|
|
|
2004-07-04 10:02:03 +00:00
|
|
|
/* We register via atexit() to test stdout. */
|
1999-11-21 01:02:48 +00:00
|
|
|
exit (errseen ? 2 : status);
|
1999-11-20 09:40:28 +00:00
|
|
|
}
|
2004-07-04 10:02:03 +00:00
|
|
|
/* vim:set shiftwidth=2: */
|