1 /* grep.c - main driver file for grep.
2 Copyright 1992, 1997-1999, 2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 /* Written July 1992 by Mike Haertel. */
20 /* Builtin decompression 1997 by Wolfram Schneider <wosch@FreeBSD.org>. */
27 #include <sys/types.h>
29 #if defined(HAVE_MMAP)
30 # include <sys/mman.h>
32 #if defined(HAVE_SETRLIMIT)
33 # include <sys/time.h>
34 # include <sys/resource.h>
36 #if defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H && defined HAVE_MBRTOWC
37 /* We can handle multibyte string. */
45 #include "getpagesize.h"
55 #define MAX(A,B) ((A) > (B) ? (A) : (B))
59 struct stats const *parent;
63 /* base of chain of stat buffers, used to detect directory loops */
64 static struct stats stats_base;
66 /* if non-zero, display usage information and exit */
69 /* If non-zero, print the version on standard output and exit. */
70 static int show_version;
72 /* If nonzero, suppress diagnostics for nonexistent or unreadable files. */
73 static int suppress_errors;
75 /* If nonzero, use mmap if possible. */
76 static int mmap_option;
78 /* If zero, output nulls after filenames. */
79 static int filename_mask;
81 /* If nonzero, use grep_color marker. */
82 static int color_option;
84 /* If nonzero, show only the part of a line matching the expression. */
85 static int only_matching;
87 /* The color string used. The user can overwrite it using the environment
88 variable GREP_COLOR. The default is to print red. */
89 static const char *grep_color = "01;31";
91 static struct exclude *excluded_patterns;
92 static struct exclude *included_patterns;
94 static char const short_options[] =
95 "0123456789A:B:C:D:EFGHIJPUVX:abcd:e:f:hiKLlm:noqRrsuvwxyZz";
97 /* Non-boolean long options that have no corresponding short equivalents. */
100 BINARY_FILES_OPTION = CHAR_MAX + 1,
105 LINE_BUFFERED_OPTION,
109 /* Long options equivalences. */
110 static struct option const long_options[] =
112 {"after-context", required_argument, NULL, 'A'},
113 {"basic-regexp", no_argument, NULL, 'G'},
114 {"before-context", required_argument, NULL, 'B'},
115 {"binary-files", required_argument, NULL, BINARY_FILES_OPTION},
116 {"byte-offset", no_argument, NULL, 'b'},
117 {"context", required_argument, NULL, 'C'},
118 {"color", optional_argument, NULL, COLOR_OPTION},
119 {"colour", optional_argument, NULL, COLOR_OPTION},
120 {"count", no_argument, NULL, 'c'},
121 {"devices", required_argument, NULL, 'D'},
122 {"directories", required_argument, NULL, 'd'},
123 {"extended-regexp", no_argument, NULL, 'E'},
124 {"exclude", required_argument, NULL, EXCLUDE_OPTION},
125 {"exclude-from", required_argument, NULL, EXCLUDE_FROM_OPTION},
126 {"file", required_argument, NULL, 'f'},
127 {"files-with-matches", no_argument, NULL, 'l'},
128 {"files-without-match", no_argument, NULL, 'L'},
129 {"fixed-regexp", no_argument, NULL, 'F'},
130 {"fixed-strings", no_argument, NULL, 'F'},
131 {"help", no_argument, &show_help, 1},
132 {"include", required_argument, NULL, INCLUDE_OPTION},
133 {"ignore-case", no_argument, NULL, 'i'},
134 {"label", required_argument, NULL, LABEL_OPTION},
135 {"line-buffered", no_argument, NULL, LINE_BUFFERED_OPTION},
136 {"line-number", no_argument, NULL, 'n'},
137 {"line-regexp", no_argument, NULL, 'x'},
138 {"max-count", required_argument, NULL, 'm'},
139 {"mmap", no_argument, &mmap_option, 1},
140 {"no-filename", no_argument, NULL, 'h'},
141 {"no-messages", no_argument, NULL, 's'},
142 {"bz2decompress", no_argument, NULL, 'J'},
144 {"decompress", no_argument, NULL, 'Z'},
145 {"null", no_argument, &filename_mask, 0},
147 {"null", no_argument, NULL, 'Z'},
149 {"null-data", no_argument, NULL, 'z'},
150 {"only-matching", no_argument, NULL, 'o'},
151 {"perl-regexp", no_argument, NULL, 'P'},
152 {"quiet", no_argument, NULL, 'q'},
153 {"recursive", no_argument, NULL, 'r'},
154 {"recursive", no_argument, NULL, 'R'},
155 {"regexp", required_argument, NULL, 'e'},
156 {"invert-match", no_argument, NULL, 'v'},
157 {"silent", no_argument, NULL, 'q'},
158 {"text", no_argument, NULL, 'a'},
159 {"binary", no_argument, NULL, 'U'},
160 {"unix-byte-offsets", no_argument, NULL, 'u'},
161 {"version", no_argument, NULL, 'V'},
162 {"with-filename", no_argument, NULL, 'H'},
163 {"word-regexp", no_argument, NULL, 'w'},
167 /* Define flags declared in grep.h. */
171 unsigned char eolbyte;
173 /* For error messages. */
174 /* The name the program was run with, stripped of any leading path. */
176 static char const *filename;
179 /* How to handle directories. */
185 } directories = READ_DIRECTORIES;
187 /* How to handle devices. */
192 } devices = READ_DEVICES;
194 static int grepdir PARAMS ((char const *, struct stats const *));
195 #if defined(HAVE_DOS_FILE_CONTENTS)
196 static inline int undossify_input PARAMS ((register char *, size_t));
199 /* Functions we'll use to search. */
200 static void (*compile) PARAMS ((char const *, size_t));
201 static size_t (*execute) PARAMS ((char const *, size_t, size_t *, int));
203 /* Like error, but suppress the diagnostic if requested. */
205 suppressible_error (char const *mesg, int errnum)
207 if (! suppress_errors)
208 error (0, errnum, "%s", mesg);
212 /* Convert STR to a positive integer, storing the result in *OUT.
213 STR must be a valid context length argument; report an error if it
216 context_length_arg (char const *str, int *out)
219 if (! (xstrtoumax (str, 0, 10, &value, "") == LONGINT_OK
220 && 0 <= (*out = value)
223 error (2, 0, "%s: %s\n", str, _("invalid context length argument"));
228 /* Hairy buffering mechanism for grep. The intent is to keep
229 all reads aligned on a page boundary and multiples of the
230 page size, unless a read yields a partial page. */
232 static char *buffer; /* Base of buffer. */
233 static size_t bufalloc; /* Allocated buffer size, counting slop. */
234 #define INITIAL_BUFSIZE 32768 /* Initial buffer size, not counting slop. */
235 static int bufdesc; /* File descriptor. */
236 static char *bufbeg; /* Beginning of user-visible stuff. */
237 static char *buflim; /* Limit of user-visible stuff. */
238 static size_t pagesize; /* alignment of memory pages */
239 static off_t bufoffset; /* Read offset; defined on regular files. */
240 static off_t after_last_match; /* Pointer after last matching line that
241 would have been output if we were
242 outputting characters. */
244 #if defined(HAVE_MMAP)
245 static int bufmapped; /* True if buffer is memory-mapped. */
246 static off_t initial_bufoffset; /* Initial value of bufoffset. */
252 static BZFILE* bzbufdesc; /* libbz2 file handle. */
253 static int BZflag; /* uncompress before searching. */
256 static gzFile gzbufdesc; /* zlib file descriptor. */
257 static int Zflag; /* uncompress before searching. */
260 /* Return VAL aligned to the next multiple of ALIGNMENT. VAL can be
261 an integer or a pointer. Both args must be free of side effects. */
262 #define ALIGN_TO(val, alignment) \
263 ((size_t) (val) % (alignment) == 0 \
265 : (val) + ((alignment) - (size_t) (val) % (alignment)))
267 /* Reset the buffer for a new file, returning zero if we should skip it.
268 Initialize on the first time through. */
270 reset (int fd, char const *file, struct stats *stats)
274 pagesize = getpagesize ();
275 if (pagesize == 0 || 2 * pagesize + 1 <= pagesize)
277 bufalloc = ALIGN_TO (INITIAL_BUFSIZE, pagesize) + pagesize + 1;
278 buffer = xmalloc (bufalloc);
282 bzbufdesc = BZ2_bzdopen(fd, "r");
283 if (bzbufdesc == NULL)
284 error(2, 0, _("memory exhausted"));
289 gzbufdesc = gzdopen(fd, "r");
290 if (gzbufdesc == NULL)
291 error(2, 0, _("memory exhausted"));
295 bufbeg = buflim = ALIGN_TO (buffer + 1, pagesize);
296 bufbeg[-1] = eolbyte;
299 if (fstat (fd, &stats->stat) != 0)
301 error (0, errno, "fstat");
304 if (directories == SKIP_DIRECTORIES && S_ISDIR (stats->stat.st_mode))
307 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)))
309 if (devices == SKIP_DEVICES && (S_ISCHR(stats->stat.st_mode) || S_ISBLK(stats->stat.st_mode)))
317 S_ISREG (stats->stat.st_mode))
323 bufoffset = lseek (fd, 0, SEEK_CUR);
326 error (0, errno, "lseek");
330 #if defined(HAVE_MMAP)
331 initial_bufoffset = bufoffset;
332 bufmapped = mmap_option && bufoffset % pagesize == 0;
337 #if defined(HAVE_MMAP)
344 /* Read new stuff into the buffer, saving the specified
345 amount of old stuff. When we're done, 'bufbeg' points
346 to the beginning of the buffer contents, and 'buflim'
347 points just after the end. Return zero if there's an error. */
349 fillbuf (size_t save, struct stats const *stats)
356 /* Offset from start of buffer to start of old stuff
357 that we want to save. */
358 size_t saved_offset = buflim - save - buffer;
360 if (pagesize <= buffer + bufalloc - buflim)
363 bufbeg = buflim - save;
367 size_t minsize = save + pagesize;
372 /* Grow newsize until it is at least as great as minsize. */
373 for (newsize = bufalloc - pagesize - 1; newsize < minsize; newsize *= 2)
374 if (newsize * 2 < newsize || newsize * 2 + pagesize + 1 < newsize * 2)
377 /* Try not to allocate more memory than the file size indicates,
378 as that might cause unnecessary memory exhaustion if the file
379 is large. However, do not use the original file size as a
380 heuristic if we've already read past the file end, as most
381 likely the file is growing. */
382 if (S_ISREG (stats->stat.st_mode))
384 off_t to_be_read = stats->stat.st_size - bufoffset;
385 off_t maxsize_off = save + to_be_read;
386 if (0 <= to_be_read && to_be_read <= maxsize_off
387 && maxsize_off == (size_t) maxsize_off
388 && minsize <= (size_t) maxsize_off
389 && (size_t) maxsize_off < newsize)
390 newsize = maxsize_off;
393 /* Add enough room so that the buffer is aligned and has room
394 for byte sentinels fore and aft. */
395 newalloc = newsize + pagesize + 1;
397 newbuf = bufalloc < newalloc ? xmalloc (bufalloc = newalloc) : buffer;
398 readbuf = ALIGN_TO (newbuf + 1 + save, pagesize);
399 bufbeg = readbuf - save;
400 memmove (bufbeg, buffer + saved_offset, save);
401 bufbeg[-1] = eolbyte;
402 if (newbuf != buffer)
409 readsize = buffer + bufalloc - readbuf;
410 readsize -= readsize % pagesize;
412 #if defined(HAVE_MMAP)
415 size_t mmapsize = readsize;
417 /* Don't mmap past the end of the file; some hosts don't allow this.
418 Use `read' on the last page. */
419 if (stats->stat.st_size - bufoffset < mmapsize)
421 mmapsize = stats->stat.st_size - bufoffset;
422 mmapsize -= mmapsize % pagesize;
426 && (mmap ((caddr_t) readbuf, mmapsize,
427 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED,
431 /* Do not bother to use madvise with MADV_SEQUENTIAL or
432 MADV_WILLNEED on the mmapped memory. One might think it
433 would help, but it slows us down about 30% on SunOS 4.1. */
438 /* Stop using mmap on this file. Synchronize the file
439 offset. Do not warn about mmap failures. On some hosts
440 (e.g. Solaris 2.5) mmap can fail merely because some
441 other process has an advisory read lock on the file.
442 There's no point alarming the user about this misfeature. */
444 if (bufoffset != initial_bufoffset
445 && lseek (bufdesc, bufoffset, SEEK_SET) < 0)
447 error (0, errno, "lseek");
458 if (BZflag && bzbufdesc)
461 bytesread = BZ2_bzRead (&bzerr, bzbufdesc, readbuf, readsize);
469 case BZ_DATA_ERROR_MAGIC:
470 BZ2_bzReadClose (&bzerr, bzbufdesc); bzbufdesc = NULL;
471 lseek (bufdesc, 0, SEEK_SET);
472 bytesread = read (bufdesc, readbuf, readsize);
482 bytesread = gzread (gzbufdesc, readbuf, readsize);
485 bytesread = read (bufdesc, readbuf, readsize);
486 while (bytesread < 0 && errno == EINTR);
490 fillsize = bytesread;
493 bufoffset += fillsize;
494 #if defined(HAVE_DOS_FILE_CONTENTS)
496 fillsize = undossify_input (readbuf, fillsize);
498 buflim = readbuf + fillsize;
502 /* Flags controlling the style of output. */
507 WITHOUT_MATCH_BINARY_FILES
508 } binary_files; /* How to handle binary files. */
510 static int filename_mask; /* If zero, output nulls after filenames. */
511 static int out_quiet; /* Suppress all normal output. */
512 static int out_invert; /* Print nonmatching stuff. */
513 static int out_file; /* Print filenames. */
514 static int out_line; /* Print line numbers. */
515 static int out_byte; /* Print byte offsets. */
516 static int out_before; /* Lines of leading context. */
517 static int out_after; /* Lines of trailing context. */
518 static int count_matches; /* Count matching lines. */
519 static int list_files; /* List matching files. */
520 static int no_filenames; /* Suppress file names. */
521 static off_t max_count; /* Stop after outputting this many
522 lines from an input file. */
523 static int line_buffered; /* If nonzero, use line buffering, i.e.
524 fflush everyline out. */
525 static char *label = NULL; /* Fake filename for stdin */
528 /* Internal variables to keep track of byte count, context, etc. */
529 static uintmax_t totalcc; /* Total character count before bufbeg. */
530 static char const *lastnl; /* Pointer after last newline counted. */
531 static char const *lastout; /* Pointer after last character output;
532 NULL if no character has been output
533 or if it's conceptually before bufbeg. */
534 static uintmax_t totalnl; /* Total newline count before lastnl. */
535 static off_t outleft; /* Maximum number of lines to be output. */
536 static int pending; /* Pending lines of output.
537 Always kept 0 if out_quiet is true. */
538 static int done_on_match; /* Stop scanning file on first match. */
539 static int exit_on_match; /* Exit on first match. */
541 #if defined(HAVE_DOS_FILE_CONTENTS)
545 /* Add two numbers that count input bytes or lines, and report an
546 error if the addition overflows. */
548 add_count (uintmax_t a, uintmax_t b)
550 uintmax_t sum = a + b;
552 error (2, 0, _("input is too large to count"));
557 nlscan (char const *lim)
561 for (beg = lastnl; beg != lim; beg = memchr (beg, eolbyte, lim - beg), beg++)
563 totalnl = add_count (totalnl, newlines);
567 /* Print a byte offset, followed by a character separator. */
569 print_offset_sep (uintmax_t pos, char sep)
571 /* Do not rely on printf to print pos, since uintmax_t may be longer
572 than long, and long long is not portable. */
574 char buf[sizeof pos * CHAR_BIT];
575 char *p = buf + sizeof buf - 1;
579 *--p = '0' + pos % 10;
580 while ((pos /= 10) != 0);
582 fwrite (p, 1, buf + sizeof buf - p, stdout);
586 prline (char const *beg, char const *lim, int sep)
589 printf ("%s%c", filename, sep & filename_mask);
593 totalnl = add_count (totalnl, 1);
594 print_offset_sep (totalnl, sep);
599 uintmax_t pos = add_count (totalcc, beg - bufbeg);
600 #if defined(HAVE_DOS_FILE_CONTENTS)
601 pos = dossified_pos (pos);
603 print_offset_sep (pos, sep);
609 while ((match_offset = (*execute) (beg, lim - beg, &match_size, 1))
612 char const *b = beg + match_offset;
618 printf("\33[%sm", grep_color);
619 fwrite(b, sizeof (char), match_size, stdout);
621 fputs("\33[00m", stdout);
623 beg = b + match_size;
634 while (lim-beg && (match_offset = (*execute) (beg, lim - beg, &match_size, 1))
637 char const *b = beg + match_offset;
638 /* Avoid matching the empty line at the end of the buffer. */
641 /* Avoid hanging on grep --color "" foo */
644 fwrite (beg, sizeof (char), match_offset, stdout);
645 printf ("\33[%sm", grep_color);
646 fwrite (b, sizeof (char), match_size, stdout);
647 fputs ("\33[00m", stdout);
648 beg = b + match_size;
650 fputs ("\33[K", stdout);
652 fwrite (beg, 1, lim - beg, stdout);
654 error (0, errno, _("writing output"));
660 /* Print pending lines of trailing context prior to LIM. Trailing context ends
661 at the next matching line when OUTLEFT is 0. */
663 prpending (char const *lim)
667 while (pending > 0 && lastout < lim)
669 char const *nl = memchr (lastout, eolbyte, lim - lastout);
673 || (((*execute) (lastout, nl - lastout, &match_size, 0) == (size_t) -1)
675 prline (lastout, nl + 1, '-');
681 /* Print the lines between BEG and LIM. Deal with context crap.
682 If NLINESP is non-null, store a count of lines between BEG and LIM. */
684 prtext (char const *beg, char const *lim, int *nlinesp)
686 static int used; /* avoid printing "--" before any output */
691 if (!out_quiet && pending > 0)
698 /* Deal with leading context crap. */
700 bp = lastout ? lastout : bufbeg;
701 for (i = 0; i < out_before; ++i)
705 while (p[-1] != eol);
707 /* We only print the "--" separator if our output is
708 discontiguous from the last output in the file. */
709 if ((out_before || out_after) && used && p != lastout)
714 char const *nl = memchr (p, eol, beg - p);
723 /* Caller wants a line count. */
724 for (n = 0; p < lim && n < outleft; n++)
726 char const *nl = memchr (p, eol, lim - p);
734 /* relying on it that this function is never called when outleft = 0. */
735 after_last_match = bufoffset - (buflim - p);
739 prline (beg, lim, ':');
741 pending = out_quiet ? 0 : out_after;
745 /* Scan the specified portion of the buffer, matching lines (or
746 between matching lines if OUT_INVERT is true). Return a count of
749 grepbuf (char const *beg, char const *lim)
752 register char const *p;
758 while ((match_offset = (*execute) (p, lim - p, &match_size, 0)) != (size_t) -1)
760 char const *b = p + match_offset;
761 char const *endp = b + match_size;
762 /* Avoid matching the empty line at the end of the buffer. */
767 prtext (b, endp, (int *) 0);
770 if (!outleft || done_on_match)
774 after_last_match = bufoffset - (buflim - endp);
788 if (out_invert && p < lim)
797 /* Search a given file. Normally, return a count of lines printed;
798 but if the file is a directory and we search it recursively, then
799 return -2 if there was a match, and -1 otherwise. */
801 grep (int fd, char const *file, struct stats *stats)
805 size_t residue, save;
811 if (!reset (fd, file, stats))
814 if (file && directories == RECURSE_DIRECTORIES
815 && S_ISDIR (stats->stat.st_mode))
817 /* Close fd now, so that we don't open a lot of file descriptors
818 when we recurse deeply. */
819 if (BZflag && bzbufdesc)
820 BZ2_bzclose(bzbufdesc);
828 error (0, errno, "%s", file);
829 return grepdir (file, stats) - 2;
836 after_last_match = 0;
843 if (! fillbuf (save, stats))
845 if (! is_EISDIR (errno, file))
846 suppressible_error (filename, errno);
850 not_text = (((binary_files == BINARY_BINARY_FILES && !out_quiet)
851 || binary_files == WITHOUT_MATCH_BINARY_FILES)
852 && memchr (bufbeg, eol ? '\0' : '\200', buflim - bufbeg));
853 if (not_text && binary_files == WITHOUT_MATCH_BINARY_FILES)
855 done_on_match += not_text;
856 out_quiet += not_text;
866 /* no more data to scan (eof) except for maybe a residue -> break */
870 /* Determine new residue (the length of an incomplete line at the end of
871 the buffer, 0 means there is no incomplete last line). */
874 for (lim = buflim; lim[-1] != eol; lim--)
880 residue = buflim - lim;
885 nlines += grepbuf (beg, lim);
888 if((!outleft && !pending) || (nlines && done_on_match && !out_invert))
892 /* The last OUT_BEFORE lines at the end of the buffer will be needed as
893 leading context if there is a matching line at the begin of the
894 next data. Make beg point to their begin. */
897 while (i < out_before && beg > bufbeg && beg != lastout)
902 while (beg[-1] != eol);
905 /* detect if leading context is discontinuous from last printed line. */
909 /* Handle some details and read more data to scan. */
910 save = residue + lim - beg;
912 totalcc = add_count (totalcc, buflim - bufbeg - save);
915 if (! fillbuf (save, stats))
917 if (! is_EISDIR (errno, file))
918 suppressible_error (filename, errno);
926 nlines += grepbuf (bufbeg + save - residue, buflim);
932 done_on_match -= not_text;
933 out_quiet -= not_text;
934 if ((not_text & ~out_quiet) && nlines != 0)
935 printf (_("Binary file %s matches\n"), filename);
940 grepfile (char const *file, struct stats *stats)
950 filename = label ? label : _("(standard input)");
954 while ((desc = open (file, O_RDONLY | O_NONBLOCK)) < 0 && errno == EINTR)
961 if (is_EISDIR (e, file) && directories == RECURSE_DIRECTORIES)
963 if (stat (file, &stats->stat) != 0)
965 error (0, errno, "%s", file);
969 return grepdir (file, stats);
972 if (!suppress_errors)
974 if (directories == SKIP_DIRECTORIES)
982 /* When skipping directories, don't worry about
983 directories that can't be opened. */
990 suppressible_error (file, e);
994 flags = fcntl(desc, F_GETFL);
995 flags &= ~O_NONBLOCK;
996 fcntl(desc, F_SETFL, flags);
1000 #if defined(SET_BINARY)
1001 /* Set input to binary mode. Pipes are simulated with files
1002 on DOS, so this includes the case of "foo | grep bar". */
1007 count = grep (desc, file, stats);
1015 printf ("%s%c", filename, ':' & filename_mask);
1016 printf ("%d\n", count);
1020 if (list_files == 1 - 2 * status)
1021 printf ("%s%c", filename, '\n' & filename_mask);
1023 if (BZflag && bzbufdesc)
1024 BZ2_bzclose(bzbufdesc);
1033 off_t required_offset = outleft ? bufoffset : after_last_match;
1034 if ((bufmapped || required_offset != bufoffset)
1035 && lseek (desc, required_offset, SEEK_SET) < 0
1036 && S_ISREG (stats->stat.st_mode))
1037 error (0, errno, "%s", filename);
1040 while (close (desc) != 0)
1043 error (0, errno, "%s", file);
1052 grepdir (char const *dir, struct stats const *stats)
1055 struct stats const *ancestor;
1058 /* Mingw32 does not support st_ino. No known working hosts use zero
1059 for st_ino, so assume that the Mingw32 bug applies if it's zero. */
1060 if (stats->stat.st_ino)
1061 for (ancestor = stats; (ancestor = ancestor->parent) != 0; )
1062 if (ancestor->stat.st_ino == stats->stat.st_ino
1063 && ancestor->stat.st_dev == stats->stat.st_dev)
1065 if (!suppress_errors)
1066 error (0, 0, _("warning: %s: %s"), dir,
1067 _("recursive directory loop"));
1071 name_space = savedir (dir, stats->stat.st_size, included_patterns,
1077 suppressible_error (dir, errno);
1083 size_t dirlen = strlen (dir);
1084 int needs_slash = ! (dirlen == FILESYSTEM_PREFIX_LEN (dir)
1085 || IS_SLASH (dir[dirlen - 1]));
1087 char const *namep = name_space;
1089 child.parent = stats;
1090 out_file += !no_filenames;
1093 size_t namelen = strlen (namep);
1094 file = xrealloc (file, dirlen + 1 + namelen + 1);
1097 strcpy (file + dirlen + needs_slash, namep);
1098 namep += namelen + 1;
1099 status &= grepfile (file, &child);
1101 out_file -= !no_filenames;
1115 fprintf (stderr, _("Usage: %s [OPTION]... PATTERN [FILE]...\n"),
1117 fprintf (stderr, _("Try `%s --help' for more information.\n"),
1122 printf (_("Usage: %s [OPTION]... PATTERN [FILE] ...\n"), program_name);
1124 Search for PATTERN in each FILE or standard input.\n\
1125 Example: %s -i 'hello world' menu.h main.c\n\
1127 Regexp selection and interpretation:\n"), program_name);
1129 -E, --extended-regexp PATTERN is an extended regular expression\n\
1130 -F, --fixed-strings PATTERN is a set of newline-separated strings\n\
1131 -G, --basic-regexp PATTERN is a basic regular expression\n\
1132 -P, --perl-regexp PATTERN is a Perl regular expression\n"));
1134 -e, --regexp=PATTERN use PATTERN as a regular expression\n\
1135 -f, --file=FILE obtain PATTERN from FILE\n\
1136 -i, --ignore-case ignore case distinctions\n\
1137 -w, --word-regexp force PATTERN to match only whole words\n\
1138 -x, --line-regexp force PATTERN to match only whole lines\n\
1139 -z, --null-data a data line ends in 0 byte, not newline\n"));
1143 -s, --no-messages suppress error messages\n\
1144 -v, --invert-match select non-matching lines\n\
1145 -V, --version print version information and exit\n\
1146 --help display this help and exit\n\
1147 -J, --bz2decompress decompress bzip2'ed input before searching\n\
1148 -Z, --decompress decompress input before searching (HAVE_LIBZ=1)\n\
1149 --mmap use memory-mapped input if possible\n"));
1153 -m, --max-count=NUM stop after NUM matches\n\
1154 -b, --byte-offset print the byte offset with output lines\n\
1155 -n, --line-number print line number with output lines\n\
1156 --line-buffered flush output on every line\n\
1157 -H, --with-filename print the filename for each match\n\
1158 -h, --no-filename suppress the prefixing filename on output\n\
1159 --label=LABEL print LABEL as filename for standard input\n\
1160 -o, --only-matching show only the part of a line matching PATTERN\n\
1161 -q, --quiet, --silent suppress all normal output\n\
1162 --binary-files=TYPE assume that binary files are TYPE\n\
1163 TYPE is 'binary', 'text', or 'without-match'\n\
1164 -a, --text equivalent to --binary-files=text\n\
1165 -I equivalent to --binary-files=without-match\n\
1166 -d, --directories=ACTION how to handle directories\n\
1167 ACTION is 'read', 'recurse', or 'skip'\n\
1168 -D, --devices=ACTION how to handle devices, FIFOs and sockets\n\
1169 ACTION is 'read' or 'skip'\n\
1170 -R, -r, --recursive equivalent to --directories=recurse\n\
1171 --include=PATTERN files that match PATTERN will be examined\n\
1172 --exclude=PATTERN files that match PATTERN will be skipped.\n\
1173 --exclude-from=FILE files that match PATTERN in FILE will be skipped.\n\
1174 -L, --files-without-match only print FILE names containing no match\n\
1175 -l, --files-with-matches only print FILE names containing matches\n\
1176 -c, --count only print a count of matching lines per FILE\n\
1177 --null print 0 byte after FILE name\n"));
1181 -B, --before-context=NUM print NUM lines of leading context\n\
1182 -A, --after-context=NUM print NUM lines of trailing context\n\
1183 -C, --context=NUM print NUM lines of output context\n\
1184 -NUM same as --context=NUM\n\
1186 --colour[=WHEN] use markers to distinguish the matching string\n\
1187 WHEN may be `always', `never' or `auto'.\n\
1188 -U, --binary do not strip CR characters at EOL (MSDOS)\n\
1189 -u, --unix-byte-offsets report offsets as if CRs were not there (MSDOS)\n\
1191 `egrep' means `grep -E'. `fgrep' means `grep -F'.\n\
1192 With no FILE, or when FILE is -, read standard input. If less than\n\
1193 two FILEs given, assume -h. Exit status is 0 if match, 1 if no match,\n\
1194 and 2 if trouble.\n"));
1195 printf (_("\nReport bugs to <bug-gnu-utils@gnu.org>.\n"));
1200 /* Set the matcher to M, reporting any conflicts. */
1202 setmatcher (char const *m)
1204 if (matcher && strcmp (matcher, m) != 0)
1205 error (2, 0, _("conflicting matchers specified"));
1209 /* Go through the matchers vector and look for the specified matcher.
1210 If we find it, install it in compile and execute, and return 1. */
1212 install_matcher (char const *name)
1215 #if defined(HAVE_SETRLIMIT)
1219 for (i = 0; matchers[i].compile; i++)
1220 if (strcmp (name, matchers[i].name) == 0)
1222 compile = matchers[i].compile;
1223 execute = matchers[i].execute;
1224 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_STACK)
1225 /* I think every platform needs to do this, so that regex.c
1226 doesn't oveflow the stack. The default value of
1227 `re_max_failures' is too large for some platforms: it needs
1228 more than 3MB-large stack.
1230 The test for HAVE_SETRLIMIT should go into `configure'. */
1231 if (!getrlimit (RLIMIT_STACK, &rlim))
1234 extern long int re_max_failures; /* from regex.c */
1236 /* Approximate the amount regex.c needs, plus some more. */
1237 newlim = re_max_failures * 2 * 20 * sizeof (char *);
1238 if (newlim > rlim.rlim_max)
1240 newlim = rlim.rlim_max;
1241 re_max_failures = newlim / (2 * 20 * sizeof (char *));
1243 if (rlim.rlim_cur < newlim)
1245 rlim.rlim_cur = newlim;
1246 setrlimit (RLIMIT_STACK, &rlim);
1255 /* Find the white-space-separated options specified by OPTIONS, and
1256 using BUF to store copies of these options, set ARGV[0], ARGV[1],
1257 etc. to the option copies. Return the number N of options found.
1258 Do not set ARGV[N] to NULL. If ARGV is NULL, do not store ARGV[0]
1259 etc. Backslash can be used to escape whitespace (and backslashes). */
1261 prepend_args (char const *options, char *buf, char **argv)
1263 char const *o = options;
1269 while (ISSPACE ((unsigned char) *o))
1278 if ((*b++ = *o++) == '\\' && *o)
1280 while (*o && ! ISSPACE ((unsigned char) *o));
1286 /* Prepend the whitespace-separated options in OPTIONS to the argument
1287 vector of a main program with argument count *PARGC and argument
1290 prepend_default_options (char const *options, int *pargc, char ***pargv)
1294 char *buf = xmalloc (strlen (options) + 1);
1295 int prepended = prepend_args (options, buf, (char **) NULL);
1297 char * const *argv = *pargv;
1298 char **pp = (char **) xmalloc ((prepended + argc + 1) * sizeof *pp);
1299 *pargc = prepended + argc;
1302 pp += prepend_args (options, buf, pp);
1303 while ((*pp++ = *argv++))
1308 /* Get the next non-digit option from ARGC and ARGV.
1309 Return -1 if there are no more options.
1310 Process any digit options that were encountered on the way,
1311 and store the resulting integer into *DEFAULT_CONTEXT. */
1313 get_nondigit_option (int argc, char *const *argv, int *default_context)
1316 char buf[sizeof (uintmax_t) * CHAR_BIT + 4];
1319 /* Set buf[0] to anything but '0', for the leading-zero test below. */
1322 while (opt = getopt_long (argc, argv, short_options, long_options, NULL),
1323 '0' <= opt && opt <= '9')
1325 /* Suppress trivial leading zeros, to avoid incorrect
1326 diagnostic on strings like 00000000000. */
1330 if (p == buf + sizeof buf - 4)
1332 /* Too many digits. Append "..." to make context_length_arg
1333 complain about "X...", where X contains the digits seen
1343 context_length_arg (buf, default_context);
1350 main (int argc, char **argv)
1353 size_t keycc, oldcc, keyalloc;
1355 int opt, cc, status;
1356 int default_context;
1358 extern char *optarg;
1361 initialize_main (&argc, &argv);
1362 program_name = argv[0];
1363 if (program_name && strrchr (program_name, '/'))
1364 program_name = strrchr (program_name, '/') + 1;
1366 if (program_name[0] == 'b' && program_name[1] == 'z') {
1371 else if (program_name[0] == 'z') {
1377 #if defined(__MSDOS__) || defined(_WIN32)
1378 /* DOS and MS-Windows use backslashes as directory separators, and usually
1379 have an .exe suffix. They also have case-insensitive filesystems. */
1382 char *p = program_name;
1383 char *bslash = strrchr (argv[0], '\\');
1385 if (bslash && bslash >= program_name) /* for mixed forward/backslash case */
1386 program_name = bslash + 1;
1387 else if (program_name == argv[0]
1388 && argv[0][0] && argv[0][1] == ':') /* "c:progname" */
1389 program_name = argv[0] + 2;
1391 /* Collapse the letter-case, so `strcmp' could be used hence. */
1393 if (*p >= 'A' && *p <= 'Z')
1396 /* Remove the .exe extension, if any. */
1397 if ((p = strrchr (program_name, '.')) && strcmp (p, ".exe") == 0)
1408 max_count = TYPE_MAXIMUM (off_t);
1410 /* The value -1 means to use DEFAULT_CONTEXT. */
1411 out_after = out_before = -1;
1412 /* Default before/after context: chaged by -C/-NUM options */
1413 default_context = 0;
1414 /* Changed by -o option */
1417 /* Internationalization. */
1418 #if defined(HAVE_SETLOCALE)
1419 setlocale (LC_ALL, "");
1421 #if defined(ENABLE_NLS)
1422 bindtextdomain (PACKAGE, LOCALEDIR);
1423 textdomain (PACKAGE);
1426 atexit (close_stdout);
1428 prepend_default_options (getenv ("GREP_OPTIONS"), &argc, &argv);
1430 while ((opt = get_nondigit_option (argc, argv, &default_context)) != -1)
1434 context_length_arg (optarg, &out_after);
1438 context_length_arg (optarg, &out_before);
1442 /* Set output match context, but let any explicit leading or
1443 trailing amount specified with -A or -B stand. */
1444 context_length_arg (optarg, &default_context);
1448 if (strcmp (optarg, "read") == 0)
1449 devices = READ_DEVICES;
1450 else if (strcmp (optarg, "skip") == 0)
1451 devices = SKIP_DEVICES;
1453 error (2, 0, _("unknown devices method"));
1457 setmatcher ("egrep");
1461 setmatcher ("fgrep");
1465 setmatcher ("perl");
1469 setmatcher ("grep");
1477 binary_files = WITHOUT_MATCH_BINARY_FILES;
1482 printf (_("Cannot mix -Z and -J.\n"));
1489 #if defined(HAVE_DOS_FILE_CONTENTS)
1490 dos_use_file_type = DOS_BINARY;
1495 #if defined(HAVE_DOS_FILE_CONTENTS)
1496 dos_report_unix_offset = 1;
1505 setmatcher (optarg);
1509 binary_files = TEXT_BINARY_FILES;
1521 if (strcmp (optarg, "read") == 0)
1522 directories = READ_DIRECTORIES;
1523 else if (strcmp (optarg, "skip") == 0)
1524 directories = SKIP_DIRECTORIES;
1525 else if (strcmp (optarg, "recurse") == 0)
1526 directories = RECURSE_DIRECTORIES;
1528 error (2, 0, _("unknown directories method"));
1532 cc = strlen (optarg);
1533 keys = xrealloc (keys, keycc + cc + 1);
1534 strcpy (&keys[keycc], optarg);
1536 keys[keycc++] = '\n';
1540 fp = strcmp (optarg, "-") != 0 ? fopen (optarg, "r") : stdin;
1542 error (2, errno, "%s", optarg);
1543 for (keyalloc = 1; keyalloc <= keycc + 1; keyalloc *= 2)
1545 keys = xrealloc (keys, keyalloc);
1548 && (cc = fread (keys + keycc, 1, keyalloc - 1 - keycc, fp)) > 0)
1551 if (keycc == keyalloc - 1)
1552 keys = xrealloc (keys, keyalloc *= 2);
1556 /* Append final newline if file ended in non-newline. */
1557 if (oldcc != keycc && keys[keycc - 1] != '\n')
1558 keys[keycc++] = '\n';
1566 case 'y': /* For old-timers . . . */
1571 /* Like -l, except list files that don't contain matches.
1572 Inspired by the same option in Hume's gre. */
1583 switch (xstrtoumax (optarg, 0, 10, &value, ""))
1587 if (0 <= max_count && max_count == value)
1590 case LONGINT_OVERFLOW:
1591 max_count = TYPE_MAXIMUM (off_t);
1595 error (2, 0, _("invalid max count"));
1610 close_stdout_set_status(0);
1615 directories = RECURSE_DIRECTORIES;
1619 suppress_errors = 1;
1638 printf (_("Cannot mix -J and -Z.\n"));
1651 case BINARY_FILES_OPTION:
1652 if (strcmp (optarg, "binary") == 0)
1653 binary_files = BINARY_BINARY_FILES;
1654 else if (strcmp (optarg, "text") == 0)
1655 binary_files = TEXT_BINARY_FILES;
1656 else if (strcmp (optarg, "without-match") == 0)
1657 binary_files = WITHOUT_MATCH_BINARY_FILES;
1659 error (2, 0, _("unknown binary-files type"));
1664 if(!strcasecmp(optarg, "always") || !strcasecmp(optarg, "yes") ||
1665 !strcasecmp(optarg, "force"))
1667 else if(!strcasecmp(optarg, "never") || !strcasecmp(optarg, "no") ||
1668 !strcasecmp(optarg, "none"))
1670 else if(!strcasecmp(optarg, "auto") || !strcasecmp(optarg, "tty") ||
1671 !strcasecmp(optarg, "if-tty"))
1677 if(color_option == 2) {
1678 if(isatty(STDOUT_FILENO) && getenv("TERM") &&
1679 strcmp(getenv("TERM"), "dumb"))
1686 case EXCLUDE_OPTION:
1687 if (!excluded_patterns)
1688 excluded_patterns = new_exclude ();
1689 add_exclude (excluded_patterns, optarg);
1692 case EXCLUDE_FROM_OPTION:
1693 if (!excluded_patterns)
1694 excluded_patterns = new_exclude ();
1695 if (add_exclude_file (add_exclude, excluded_patterns, optarg, '\n')
1698 error (2, errno, "%s", optarg);
1702 case INCLUDE_OPTION:
1703 if (!included_patterns)
1704 included_patterns = new_exclude ();
1705 add_exclude (included_patterns, optarg);
1708 case LINE_BUFFERED_OPTION:
1726 /* POSIX.2 says that -q overrides -l, which in turn overrides the
1727 other output options. */
1730 if (exit_on_match | list_files)
1735 out_quiet = count_matches | done_on_match;
1738 out_after = default_context;
1740 out_before = default_context;
1744 char *userval = getenv ("GREP_COLOR");
1745 if (userval != NULL && *userval != '\0')
1746 grep_color = userval;
1750 matcher = program_name;
1754 printf (_("%s (GNU grep) %s\n"), matcher, VERSION);
1757 Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc.\n"));
1759 This is free software; see the source for copying conditions. There is NO\n\
1760 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"));
1772 /* No keys were specified (e.g. -f /dev/null). Match nothing. */
1774 match_lines = match_words = 0;
1777 /* Strip trailing newline. */
1783 keys = argv[optind++];
1784 keycc = strlen (keys);
1789 if (!install_matcher (matcher) && !install_matcher ("default"))
1793 if (MB_CUR_MAX != 1 && match_icase)
1796 mbstate_t cur_state, prev_state;
1797 int i, len = strlen(keys);
1799 memset(&cur_state, 0, sizeof(mbstate_t));
1800 for (i = 0; i <= len ;)
1803 mbclen = mbrtowc(&wc, keys + i, len - i, &cur_state);
1804 if (mbclen == (size_t) -1 || mbclen == (size_t) -2 || mbclen == 0)
1806 /* An invalid sequence, or a truncated multibyte character.
1807 We treat it as a singlebyte character. */
1812 if (iswupper((wint_t)wc))
1814 wc = towlower((wint_t)wc);
1815 wcrtomb(keys + i, wc, &cur_state);
1821 #endif /* MBS_SUPPORT */
1823 (*compile)(keys, keycc);
1825 if ((argc - optind > 1 && !no_filenames) || with_filenames)
1829 /* Output is set to binary mode because we shouldn't convert
1830 NL to CR-LF pairs, especially when grepping binary files. */
1843 char *file = argv[optind];
1844 if ((included_patterns || excluded_patterns)
1847 if (included_patterns &&
1848 ! excluded_filename (included_patterns, file, 0))
1850 if (excluded_patterns &&
1851 excluded_filename (excluded_patterns, file, 0))
1854 status &= grepfile (strcmp (file, "-") == 0 ? (char *) NULL : file,
1857 while ( ++optind < argc);
1860 status = grepfile ((char *) NULL, &stats_base);
1862 /* We register via atexit() to test stdout. */
1863 exit (errseen ? 2 : status);
1865 /* vim:set shiftwidth=2: */