1 /* grep.c - main driver file for grep.
2 Copyright (C) 1992, 1997, 1998, 1999 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>. */
22 /* $FreeBSD: src/gnu/usr.bin/grep/grep.c,v 1.20.2.1 2000/06/13 07:17:27 ru Exp $ */
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>
39 #include "getpagesize.h"
44 #define MAX(A,B) ((A) > (B) ? (A) : (B))
52 /* base of chain of stat buffers, used to detect directory loops */
53 static struct stats stats_base;
55 /* if non-zero, display usage information and exit */
58 /* If non-zero, print the version on standard output and exit. */
59 static int show_version;
61 /* If nonzero, use mmap if possible. */
62 static int mmap_option;
64 /* If zero, output nulls after filenames. */
65 static int filename_mask;
68 static char const short_options[] =
69 "0123456789A:B:C::EFGHIRUVX:abcd:e:f:hiLlnqrsuvwxyZz";
71 /* Non-boolean long options that have no corresponding short equivalents. */
74 BINARY_FILES_OPTION = CHAR_MAX + 1
77 /* Long options equivalences. */
78 static struct option long_options[] =
80 {"after-context", required_argument, NULL, 'A'},
81 {"basic-regexp", no_argument, NULL, 'G'},
82 {"before-context", required_argument, NULL, 'B'},
83 {"binary-files", required_argument, NULL, BINARY_FILES_OPTION},
84 {"byte-offset", no_argument, NULL, 'b'},
85 {"context", optional_argument, NULL, 'C'},
86 {"count", no_argument, NULL, 'c'},
87 {"directories", required_argument, NULL, 'd'},
88 {"extended-regexp", no_argument, NULL, 'E'},
89 {"file", required_argument, NULL, 'f'},
90 {"files-with-matches", no_argument, NULL, 'l'},
91 {"files-without-match", no_argument, NULL, 'L'},
92 {"fixed-regexp", no_argument, NULL, 'F'},
93 {"fixed-strings", no_argument, NULL, 'F'},
94 {"help", no_argument, &show_help, 1},
95 {"ignore-case", no_argument, NULL, 'i'},
96 {"line-number", no_argument, NULL, 'n'},
97 {"line-regexp", no_argument, NULL, 'x'},
98 {"mmap", no_argument, &mmap_option, 1},
99 {"no-filename", no_argument, NULL, 'h'},
100 {"no-messages", no_argument, NULL, 's'},
102 {"decompress", no_argument, NULL, 'Z'},
103 {"null", no_argument, &filename_mask, 0},
105 {"null", no_argument, NULL, 'Z'},
107 {"null-data", no_argument, NULL, 'z'},
108 {"quiet", no_argument, NULL, 'q'},
109 {"recursive", no_argument, NULL, 'r'},
110 {"regexp", required_argument, NULL, 'e'},
111 {"invert-match", no_argument, NULL, 'v'},
112 {"silent", no_argument, NULL, 'q'},
113 {"text", no_argument, NULL, 'a'},
114 {"binary", no_argument, NULL, 'U'},
115 {"unix-byte-offsets", no_argument, NULL, 'u'},
116 {"version", no_argument, NULL, 'V'},
117 {"with-filename", no_argument, NULL, 'H'},
118 {"word-regexp", no_argument, NULL, 'w'},
122 /* Define flags declared in grep.h. */
127 unsigned char eolbyte;
129 /* For error messages. */
131 static char const *filename;
134 /* How to handle directories. */
142 static int ck_atoi PARAMS ((char const *, int *));
143 static void usage PARAMS ((int)) __attribute__((noreturn));
144 static void error PARAMS ((const char *, int));
145 static void setmatcher PARAMS ((char const *));
146 static int install_matcher PARAMS ((char const *));
147 static int prepend_args PARAMS ((char const *, char *, char **));
148 static void prepend_default_options PARAMS ((char const *, int *, char ***));
149 static char *page_alloc PARAMS ((size_t, char **));
150 static int reset PARAMS ((int, char const *, struct stats *));
151 static int fillbuf PARAMS ((size_t, struct stats *));
152 static int grepbuf PARAMS ((char *, char *));
153 static void prtext PARAMS ((char *, char *, int *));
154 static void prpending PARAMS ((char *));
155 static void prline PARAMS ((char *, char *, int));
156 static void print_offset_sep PARAMS ((off_t, int));
157 static void nlscan PARAMS ((char *));
158 static int grep PARAMS ((int, char const *, struct stats *));
159 static int grepdir PARAMS ((char const *, struct stats *));
160 static int grepfile PARAMS ((char const *, struct stats *));
162 static inline int undossify_input PARAMS ((register char *, size_t));
165 /* Functions we'll use to search. */
166 static void (*compile) PARAMS ((char *, size_t));
167 static char *(*execute) PARAMS ((char *, size_t, char **));
169 /* Print a message and possibly an error string. Remember
170 that something awful happened. */
172 error (const char *mesg, int errnum)
175 fprintf (stderr, "%s: %s: %s\n", prog, mesg, strerror (errnum));
177 fprintf (stderr, "%s: %s\n", prog, mesg);
181 /* Like error (), but die horribly after printing. */
183 fatal (const char *mesg, int errnum)
185 error (mesg, errnum);
189 /* Interface to handle errors and fix library lossage. */
191 xmalloc (size_t size)
195 result = malloc (size);
197 fatal (_("memory exhausted"), 0);
201 /* Interface to handle errors and fix some library lossage. */
203 xrealloc (char *ptr, size_t size)
208 result = realloc (ptr, size);
210 result = malloc (size);
212 fatal (_("memory exhausted"), 0);
216 /* Convert STR to a positive integer, storing the result in *OUT.
217 If STR is not a valid integer, return -1 (otherwise 0). */
219 ck_atoi (char const *str, int *out)
222 for (p = str; *p; p++)
223 if (*p < '0' || *p > '9')
226 *out = atoi (optarg);
231 /* Hairy buffering mechanism for grep. The intent is to keep
232 all reads aligned on a page boundary and multiples of the
235 static char *ubuffer; /* Unaligned base of buffer. */
236 static char *buffer; /* Base of buffer. */
237 static size_t bufsalloc; /* Allocated size of buffer save region. */
238 static size_t bufalloc; /* Total buffer size. */
239 #define PREFERRED_SAVE_FACTOR 5 /* Preferred value of bufalloc / bufsalloc. */
240 static int bufdesc; /* File descriptor. */
241 static char *bufbeg; /* Beginning of user-visible stuff. */
242 static char *buflim; /* Limit of user-visible stuff. */
243 static size_t pagesize; /* alignment of memory pages */
244 static off_t bufoffset; /* Read offset; defined on regular files. */
246 #if defined(HAVE_MMAP)
247 static int bufmapped; /* True if buffer is memory-mapped. */
248 static off_t initial_bufoffset; /* Initial value of bufoffset. */
253 static gzFile gzbufdesc; /* zlib file descriptor. */
254 static int Zflag; /* uncompress before searching. */
257 /* Return VAL aligned to the next multiple of ALIGNMENT. VAL can be
258 an integer or a pointer. Both args must be free of side effects. */
259 #define ALIGN_TO(val, alignment) \
260 ((size_t) (val) % (alignment) == 0 \
262 : (val) + ((alignment) - (size_t) (val) % (alignment)))
264 /* Return the address of a page-aligned buffer of size SIZE,
265 reallocating it from *UP. Set *UP to the newly allocated (but
266 possibly unaligned) buffer used to build the aligned buffer. To
267 free the buffer, free (*UP). */
269 page_alloc (size_t size, char **up)
271 size_t asize = size + pagesize - 1;
274 char *p = *up ? realloc (*up, asize) : malloc (asize);
278 return ALIGN_TO (p, pagesize);
284 /* Reset the buffer for a new file, returning zero if we should skip it.
285 Initialize on the first time through. */
287 reset (int fd, char const *file, struct stats *stats)
290 bufsalloc = ALIGN_TO (bufalloc / PREFERRED_SAVE_FACTOR, pagesize);
294 pagesize = getpagesize ();
298 ubufsalloc = MAX (8192, pagesize);
300 ubufsalloc = BUFSALLOC;
302 bufsalloc = ALIGN_TO (ubufsalloc, pagesize);
303 bufalloc = PREFERRED_SAVE_FACTOR * bufsalloc;
304 /* The 1 byte of overflow is a kludge for dfaexec(), which
305 inserts a sentinel newline at the end of the buffer
306 being searched. There's gotta be a better way... */
307 if (bufsalloc < ubufsalloc
308 || bufalloc / PREFERRED_SAVE_FACTOR != bufsalloc
309 || bufalloc + 1 < bufalloc
310 || ! (buffer = page_alloc (bufalloc + 1, &ubuffer)))
311 fatal (_("memory exhausted"), 0);
316 gzbufdesc = gzdopen(fd, "r");
317 if (gzbufdesc == NULL)
318 fatal(_("memory exhausted"), 0);
325 if (fstat (fd, &stats->stat) != 0)
327 error ("fstat", errno);
330 if (directories == SKIP_DIRECTORIES && S_ISDIR (stats->stat.st_mode))
336 S_ISREG (stats->stat.st_mode))
342 bufoffset = lseek (fd, 0, SEEK_CUR);
345 error ("lseek", errno);
350 initial_bufoffset = bufoffset;
351 bufmapped = mmap_option && bufoffset % pagesize == 0;
363 /* Read new stuff into the buffer, saving the specified
364 amount of old stuff. When we're done, 'bufbeg' points
365 to the beginning of the buffer contents, and 'buflim'
366 points just after the end. Return zero if there's an error. */
368 fillbuf (size_t save, struct stats *stats)
374 /* Offset from start of unaligned buffer to start of old stuff
375 that we want to save. */
376 size_t saved_offset = buflim - ubuffer - save;
378 if (bufsalloc < save)
380 size_t aligned_save = ALIGN_TO (save, pagesize);
381 size_t maxalloc = (size_t) -1;
384 if (S_ISREG (stats->stat.st_mode))
386 /* Calculate an upper bound on how much memory we should allocate.
387 We can't use ALIGN_TO here, since off_t might be longer than
388 size_t. Watch out for arithmetic overflow. */
389 off_t to_be_read = stats->stat.st_size - bufoffset;
390 size_t slop = to_be_read % pagesize;
391 off_t aligned_to_be_read = to_be_read + (slop ? pagesize - slop : 0);
392 off_t maxalloc_off = aligned_save + aligned_to_be_read;
393 if (0 <= maxalloc_off && maxalloc_off == (size_t) maxalloc_off)
394 maxalloc = maxalloc_off;
397 /* Grow bufsalloc until it is at least as great as `save'; but
398 if there is an overflow, just grow it to the next page boundary. */
399 while (bufsalloc < save)
400 if (bufsalloc < bufsalloc * 2)
404 bufsalloc = aligned_save;
408 /* Grow the buffer size to be PREFERRED_SAVE_FACTOR times
410 newalloc = PREFERRED_SAVE_FACTOR * bufsalloc;
411 if (maxalloc < newalloc)
413 /* ... except don't grow it more than a pagesize past the
414 file size, as that might cause unnecessary memory
415 exhaustion if the file is large. */
417 bufsalloc = aligned_save;
420 /* Check that the above calculations made progress, which might
421 not occur if there is arithmetic overflow. If there's no
422 progress, or if the new buffer size is larger than the old
423 and buffer reallocation fails, report memory exhaustion. */
424 if (bufsalloc < save || newalloc < save
425 || (newalloc == save && newalloc != maxalloc)
426 || (bufalloc < newalloc
428 = page_alloc ((bufalloc = newalloc) + 1, &ubuffer))))
429 fatal (_("memory exhausted"), 0);
432 bufbeg = buffer + bufsalloc - save;
433 memmove (bufbeg, ubuffer + saved_offset, save);
434 readsize = bufalloc - bufsalloc;
436 #if defined(HAVE_MMAP)
439 size_t mmapsize = readsize;
441 /* Don't mmap past the end of the file; some hosts don't allow this.
442 Use `read' on the last page. */
443 if (stats->stat.st_size - bufoffset < mmapsize)
445 mmapsize = stats->stat.st_size - bufoffset;
446 mmapsize -= mmapsize % pagesize;
450 && (mmap ((caddr_t) (buffer + bufsalloc), mmapsize,
451 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED,
455 /* Do not bother to use madvise with MADV_SEQUENTIAL or
456 MADV_WILLNEED on the mmapped memory. One might think it
457 would help, but it slows us down about 30% on SunOS 4.1. */
462 /* Stop using mmap on this file. Synchronize the file
463 offset. Do not warn about mmap failures. On some hosts
464 (e.g. Solaris 2.5) mmap can fail merely because some
465 other process has an advisory read lock on the file.
466 There's no point alarming the user about this misfeature. */
468 if (bufoffset != initial_bufoffset
469 && lseek (bufdesc, bufoffset, SEEK_SET) < 0)
471 error ("lseek", errno);
484 bytesread = gzread (gzbufdesc, buffer + bufsalloc, readsize);
487 bytesread = read (bufdesc, buffer + bufsalloc, readsize);
488 while (bytesread < 0 && errno == EINTR);
492 fillsize = bytesread;
495 bufoffset += fillsize;
498 fillsize = undossify_input (buffer + bufsalloc, fillsize);
500 buflim = buffer + bufsalloc + fillsize;
504 /* Flags controlling the style of output. */
509 WITHOUT_MATCH_BINARY_FILES
510 } binary_files; /* How to handle binary files. */
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 int suppress_errors; /* Suppress diagnostics. */
523 /* Internal variables to keep track of byte count, context, etc. */
524 static off_t totalcc; /* Total character count before bufbeg. */
525 static char *lastnl; /* Pointer after last newline counted. */
526 static char *lastout; /* Pointer after last character output;
527 NULL if no character has been output
528 or if it's conceptually before bufbeg. */
529 static off_t totalnl; /* Total newline count before lastnl. */
530 static int pending; /* Pending lines of output. */
531 static int done_on_match; /* Stop scanning file on first match */
541 for (beg = lastnl; (beg = memchr (beg, eolbyte, lim - beg)); beg++)
547 print_offset_sep (off_t pos, int sep)
549 /* Do not rely on printf to print pos, since off_t may be longer than long,
550 and long long is not portable. */
552 char buf[sizeof pos * CHAR_BIT];
553 char *p = buf + sizeof buf - 1;
557 *--p = '0' + pos % 10;
558 while ((pos /= 10) != 0);
560 fwrite (p, 1, buf + sizeof buf - p, stdout);
564 prline (char *beg, char *lim, int sep)
567 printf ("%s%c", filename, sep & filename_mask);
571 print_offset_sep (++totalnl, sep);
576 off_t pos = totalcc + (beg - bufbeg);
578 pos = dossified_pos (pos);
580 print_offset_sep (pos, sep);
582 fwrite (beg, 1, lim - beg, stdout);
584 error (_("writing output"), errno);
588 /* Print pending lines of trailing context prior to LIM. */
590 prpending (char *lim)
596 while (pending > 0 && lastout < lim)
599 if ((nl = memchr (lastout, eolbyte, lim - lastout)) != 0)
603 prline (lastout, nl, '-');
607 /* Print the lines between BEG and LIM. Deal with context crap.
608 If NLINESP is non-null, store a count of lines between BEG and LIM. */
610 prtext (char *beg, char *lim, int *nlinesp)
612 static int used; /* avoid printing "--" before any output */
617 if (!out_quiet && pending > 0)
624 /* Deal with leading context crap. */
626 bp = lastout ? lastout : bufbeg;
627 for (i = 0; i < out_before; ++i)
631 while (p > bp && p[-1] != eol);
633 /* We only print the "--" separator if our output is
634 discontiguous from the last output in the file. */
635 if ((out_before || out_after) && used && p != lastout)
640 nl = memchr (p, eol, beg - p);
641 prline (p, nl + 1, '-');
648 /* Caller wants a line count. */
649 for (n = 0; p < lim; ++n)
651 if ((nl = memchr (p, eol, lim - p)) != 0)
663 prline (beg, lim, ':');
665 pending = out_quiet ? 0 : out_after;
669 /* Scan the specified portion of the buffer, matching lines (or
670 between matching lines if OUT_INVERT is true). Return a count of
673 grepbuf (char *beg, char *lim)
676 register char *p, *b;
682 while ((b = (*execute)(p, lim - p, &endp)) != 0)
684 /* Avoid matching the empty line at the end of the buffer. */
685 if (b == lim && ((b > beg && b[-1] == eol) || b == beg))
689 prtext (b, endp, (int *) 0);
701 if (out_invert && p < lim)
709 /* Search a given file. Normally, return a count of lines printed;
710 but if the file is a directory and we search it recursively, then
711 return -2 if there was a match, and -1 otherwise. */
713 grep (int fd, char const *file, struct stats *stats)
717 size_t residue, save;
721 if (!reset (fd, file, stats))
724 if (file && directories == RECURSE_DIRECTORIES
725 && S_ISDIR (stats->stat.st_mode))
727 /* Close fd now, so that we don't open a lot of file descriptors
728 when we recurse deeply. */
736 return grepdir (file, stats) - 2;
748 if (! fillbuf (save, stats))
750 if (! (is_EISDIR (errno, file) && suppress_errors))
751 error (filename, errno);
755 not_text = (((binary_files == BINARY_BINARY_FILES && !out_quiet)
756 || binary_files == WITHOUT_MATCH_BINARY_FILES)
757 && memchr (bufbeg, eol ? '\0' : '\200', buflim - bufbeg));
758 if (not_text && binary_files == WITHOUT_MATCH_BINARY_FILES)
760 done_on_match += not_text;
761 out_quiet += not_text;
768 if (buflim - bufbeg == save)
770 beg = bufbeg + save - residue;
771 for (lim = buflim; lim > beg && lim[-1] != eol; --lim)
773 residue = buflim - lim;
776 nlines += grepbuf (beg, lim);
779 if (nlines && done_on_match && !out_invert)
784 while (i < out_before && beg > bufbeg && beg != lastout)
789 while (beg > bufbeg && beg[-1] != eol);
793 save = residue + lim - beg;
794 totalcc += buflim - bufbeg - save;
797 if (! fillbuf (save, stats))
799 if (! (is_EISDIR (errno, file) && suppress_errors))
800 error (filename, errno);
807 nlines += grepbuf (bufbeg + save - residue, buflim);
813 done_on_match -= not_text;
814 out_quiet -= not_text;
815 if ((not_text & ~out_quiet) && nlines != 0)
816 printf (_("Binary file %s matches\n"), filename);
821 grepfile (char const *file, struct stats *stats)
830 filename = _("(standard input)");
834 while ((desc = open (file, O_RDONLY)) < 0 && errno == EINTR)
841 if (is_EISDIR (e, file) && directories == RECURSE_DIRECTORIES)
843 if (stat (file, &stats->stat) != 0)
849 return grepdir (file, stats);
852 if (!suppress_errors)
854 if (directories == SKIP_DIRECTORIES)
862 /* When skipping directories, don't worry about
863 directories that can't be opened. */
864 if (stat (file, &stats->stat) == 0
865 && S_ISDIR (stats->stat.st_mode))
880 /* Set input to binary mode. Pipes are simulated with files
881 on DOS, so this includes the case of "foo | grep bar". */
886 count = grep (desc, file, stats);
894 printf ("%s%c", filename, ':' & filename_mask);
895 printf ("%d\n", count);
899 if (list_files == 1 - 2 * status)
900 printf ("%s%c", filename, '\n' & filename_mask);
908 while (close (desc) != 0)
920 grepdir (char const *dir, struct stats *stats)
923 struct stats *ancestor;
926 for (ancestor = stats; (ancestor = ancestor->parent) != 0; )
927 if (ancestor->stat.st_ino == stats->stat.st_ino
928 && ancestor->stat.st_dev == stats->stat.st_dev)
930 if (!suppress_errors)
931 fprintf (stderr, _("%s: warning: %s: %s\n"), prog, dir,
932 _("recursive directory loop"));
936 name_space = savedir (dir, (unsigned) stats->stat.st_size);
942 if (!suppress_errors)
946 fatal (_("Memory exhausted"), 0);
950 size_t dirlen = strlen (dir);
951 int needs_slash = ! (dirlen == FILESYSTEM_PREFIX_LEN (dir)
952 || IS_SLASH (dir[dirlen - 1]));
954 char *namep = name_space;
956 child.parent = stats;
957 out_file += !no_filenames;
960 size_t namelen = strlen (namep);
961 file = xrealloc (file, dirlen + 1 + namelen + 1);
964 strcpy (file + dirlen + needs_slash, namep);
965 namep += namelen + 1;
966 status &= grepfile (file, &child);
968 out_file -= !no_filenames;
982 fprintf (stderr, _("Usage: %s [OPTION]... PATTERN [FILE]...\n"), prog);
983 fprintf (stderr, _("Try `%s --help' for more information.\n"), prog);
987 printf (_("Usage: %s [OPTION]... PATTERN [FILE] ...\n"), prog);
989 Search for PATTERN in each FILE or standard input.\n\
990 Example: %s -i 'hello world' menu.h main.c\n\
992 Regexp selection and interpretation:\n"), prog);
994 -E, --extended-regexp PATTERN is an extended regular expression\n\
995 -F, --fixed-strings PATTERN is a set of newline-separated strings\n\
996 -G, --basic-regexp PATTERN is a basic regular expression\n"));
998 -e, --regexp=PATTERN use PATTERN as a regular expression\n\
999 -f, --file=FILE obtain PATTERN from FILE\n\
1000 -i, --ignore-case ignore case distinctions\n\
1001 -w, --word-regexp force PATTERN to match only whole words\n\
1002 -x, --line-regexp force PATTERN to match only whole lines\n\
1003 -z, --null-data a data line ends in 0 byte, not newline\n"));
1007 -s, --no-messages suppress error messages\n\
1008 -v, --invert-match select non-matching lines\n\
1009 -V, --version print version information and exit\n\
1010 --help display this help and exit\n\
1011 -Z, --decompress decompress input before searching (HAVE_LIBZ=1)\n\
1012 --mmap use memory-mapped input if possible\n"));
1016 -b, --byte-offset print the byte offset with output lines\n\
1017 -n, --line-number print line number with output lines\n\
1018 -H, --with-filename print the filename for each match\n\
1019 -h, --no-filename suppress the prefixing filename on output\n\
1020 -q, --quiet, --silent suppress all normal output\n\
1021 --binary-files=TYPE assume that binary files are TYPE\n\
1022 TYPE is 'binary', 'text', or 'without-match'.\n\
1023 -a, --text equivalent to --binary-files=text\n\
1024 -I equivalent to --binary-files=without-match\n\
1025 -d, --directories=ACTION how to handle directories\n\
1026 ACTION is 'read', 'recurse', or 'skip'.\n\
1027 -r, --recursive equivalent to --directories=recurse.\n\
1028 -L, --files-without-match only print FILE names containing no match\n\
1029 -l, --files-with-matches only print FILE names containing matches\n\
1030 -c, --count only print a count of matching lines per FILE\n\
1031 --null print 0 byte after FILE name\n"));
1035 -B, --before-context=NUM print NUM lines of leading context\n\
1036 -A, --after-context=NUM print NUM lines of trailing context\n\
1037 -C, --context[=NUM] print NUM (default 2) lines of output context\n\
1038 unless overridden by -A or -B\n\
1039 -NUM same as --context=NUM\n\
1040 -U, --binary do not strip CR characters at EOL (MSDOS)\n\
1041 -u, --unix-byte-offsets report offsets as if CRs were not there (MSDOS)\n\
1043 `egrep' means `grep -E'. `fgrep' means `grep -F'.\n\
1044 With no FILE, or when FILE is -, read standard input. If less than\n\
1045 two FILEs given, assume -h. Exit status is 0 if match, 1 if no match,\n\
1046 and 2 if trouble.\n"));
1047 printf (_("\nReport bugs to <bug-gnu-utils@gnu.org>.\n"));
1052 /* Set the matcher to M, reporting any conflicts. */
1054 setmatcher (char const *m)
1056 if (matcher && strcmp (matcher, m) != 0)
1057 fatal (_("conflicting matchers specified"), 0);
1061 /* Go through the matchers vector and look for the specified matcher.
1062 If we find it, install it in compile and execute, and return 1. */
1064 install_matcher (char const *name)
1067 #ifdef HAVE_SETRLIMIT
1071 for (i = 0; matchers[i].name; ++i)
1072 if (strcmp (name, matchers[i].name) == 0)
1074 compile = matchers[i].compile;
1075 execute = matchers[i].execute;
1076 #if HAVE_SETRLIMIT && defined(RLIMIT_STACK)
1077 /* I think every platform needs to do this, so that regex.c
1078 doesn't oveflow the stack. The default value of
1079 `re_max_failures' is too large for some platforms: it needs
1080 more than 3MB-large stack.
1082 The test for HAVE_SETRLIMIT should go into `configure'. */
1083 if (!getrlimit (RLIMIT_STACK, &rlim))
1086 extern long int re_max_failures; /* from regex.c */
1088 /* Approximate the amount regex.c needs, plus some more. */
1089 newlim = re_max_failures * 2 * 20 * sizeof (char *);
1090 if (newlim > rlim.rlim_max)
1092 newlim = rlim.rlim_max;
1093 re_max_failures = newlim / (2 * 20 * sizeof (char *));
1095 if (rlim.rlim_cur < newlim)
1096 rlim.rlim_cur = newlim;
1098 setrlimit (RLIMIT_STACK, &rlim);
1106 /* Find the white-space-separated options specified by OPTIONS, and
1107 using BUF to store copies of these options, set ARGV[0], ARGV[1],
1108 etc. to the option copies. Return the number N of options found.
1109 Do not set ARGV[N] to NULL. If ARGV is NULL, do not store ARGV[0]
1110 etc. Backslash can be used to escape whitespace (and backslashes). */
1112 prepend_args (char const *options, char *buf, char **argv)
1114 char const *o = options;
1120 while (ISSPACE ((unsigned char) *o))
1129 if ((*b++ = *o++) == '\\' && *o)
1131 while (*o && ! ISSPACE ((unsigned char) *o));
1137 /* Prepend the whitespace-separated options in OPTIONS to the argument
1138 vector of a main program with argument count *PARGC and argument
1141 prepend_default_options (char const *options, int *pargc, char ***pargv)
1145 char *buf = xmalloc (strlen (options) + 1);
1146 int prepended = prepend_args (options, buf, (char **) NULL);
1148 char * const *argv = *pargv;
1149 char **pp = (char **) xmalloc ((prepended + argc + 1) * sizeof *pp);
1150 *pargc = prepended + argc;
1153 pp += prepend_args (options, buf, pp);
1154 while ((*pp++ = *argv++))
1160 main (int argc, char **argv)
1163 size_t keycc, oldcc, keyalloc;
1165 int opt, cc, status;
1166 int default_context;
1167 unsigned digit_args_val;
1169 extern char *optarg;
1172 initialize_main (&argc, &argv);
1174 if (prog && strrchr (prog, '/'))
1175 prog = strrchr (prog, '/') + 1;
1178 if (prog[0] == 'z') {
1184 #if defined(__MSDOS__) || defined(_WIN32)
1185 /* DOS and MS-Windows use backslashes as directory separators, and usually
1186 have an .exe suffix. They also have case-insensitive filesystems. */
1190 char *bslash = strrchr (argv[0], '\\');
1192 if (bslash && bslash >= prog) /* for mixed forward/backslash case */
1194 else if (prog == argv[0]
1195 && argv[0][0] && argv[0][1] == ':') /* "c:progname" */
1198 /* Collapse the letter-case, so `strcmp' could be used hence. */
1200 if (*p >= 'A' && *p <= 'Z')
1203 /* Remove the .exe extension, if any. */
1204 if ((p = strrchr (prog, '.')) && strcmp (p, ".exe") == 0)
1215 /* The value -1 means to use DEFAULT_CONTEXT. */
1216 out_after = out_before = -1;
1217 /* Default before/after context: chaged by -C/-NUM options */
1218 default_context = 0;
1219 /* Accumulated value of individual digits in a -NUM option */
1223 /* Internationalization. */
1225 setlocale (LC_ALL, "");
1228 bindtextdomain (PACKAGE, LOCALEDIR);
1229 textdomain (PACKAGE);
1232 prepend_default_options (getenv ("GREP_OPTIONS"), &argc, &argv);
1234 while ((opt = getopt_long (argc, argv, short_options, long_options, NULL))
1248 digit_args_val = 10 * digit_args_val + opt - '0';
1249 default_context = digit_args_val;
1254 if (ck_atoi (optarg, &out_after))
1255 fatal (_("invalid context length argument"), 0);
1261 if (ck_atoi (optarg, &out_before))
1262 fatal (_("invalid context length argument"), 0);
1266 /* Set output match context, but let any explicit leading or
1267 trailing amount specified with -A or -B stand. */
1270 if (ck_atoi (optarg, &default_context))
1271 fatal (_("invalid context length argument"), 0);
1274 default_context = 2;
1277 setmatcher ("egrep");
1280 setmatcher ("fgrep");
1283 setmatcher ("grep");
1289 binary_files = WITHOUT_MATCH_BINARY_FILES;
1293 dos_use_file_type = DOS_BINARY;
1298 dos_report_unix_offset = 1;
1305 setmatcher (optarg);
1308 binary_files = TEXT_BINARY_FILES;
1318 if (strcmp (optarg, "read") == 0)
1319 directories = READ_DIRECTORIES;
1320 else if (strcmp (optarg, "skip") == 0)
1321 directories = SKIP_DIRECTORIES;
1322 else if (strcmp (optarg, "recurse") == 0)
1323 directories = RECURSE_DIRECTORIES;
1325 fatal (_("unknown directories method"), 0);
1328 cc = strlen (optarg);
1329 keys = xrealloc (keys, keycc + cc + 1);
1330 strcpy (&keys[keycc], optarg);
1332 keys[keycc++] = '\n';
1335 fp = strcmp (optarg, "-") != 0 ? fopen (optarg, "r") : stdin;
1337 fatal (optarg, errno);
1338 for (keyalloc = 1; keyalloc <= keycc + 1; keyalloc *= 2)
1340 keys = xrealloc (keys, keyalloc);
1343 && (cc = fread (keys + keycc, 1, keyalloc - 1 - keycc, fp)) > 0)
1346 if (keycc == keyalloc - 1)
1347 keys = xrealloc (keys, keyalloc *= 2);
1351 /* Append final newline if file ended in non-newline. */
1352 if (oldcc != keycc && keys[keycc - 1] != '\n')
1353 keys[keycc++] = '\n';
1359 case 'y': /* For old-timers . . . */
1363 /* Like -l, except list files that don't contain matches.
1364 Inspired by the same option in Hume's gre. */
1383 directories = RECURSE_DIRECTORIES;
1386 suppress_errors = 1;
1407 case BINARY_FILES_OPTION:
1408 if (strcmp (optarg, "binary") == 0)
1409 binary_files = BINARY_BINARY_FILES;
1410 else if (strcmp (optarg, "text") == 0)
1411 binary_files = TEXT_BINARY_FILES;
1412 else if (strcmp (optarg, "without-match") == 0)
1413 binary_files = WITHOUT_MATCH_BINARY_FILES;
1415 fatal (_("unknown binary-files type"), 0);
1426 out_after = default_context;
1428 out_before = default_context;
1435 printf (_("%s (GNU grep) %s\n"), matcher, VERSION);
1438 Copyright (C) 1988, 1992-1998, 1999 Free Software Foundation, Inc.\n"));
1440 This is free software; see the source for copying conditions. There is NO\n\
1441 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"));
1452 /* No keys were specified (e.g. -f /dev/null). Match nothing. */
1455 /* Strip trailing newline. */
1461 keys = argv[optind++];
1462 keycc = strlen (keys);
1467 if (!install_matcher (matcher) && !install_matcher ("default"))
1470 (*compile)(keys, keycc);
1472 if ((argc - optind > 1 && !no_filenames) || with_filenames)
1476 /* Output is set to binary mode because we shouldn't convert
1477 NL to CR-LF pairs, especially when grepping binary files. */
1488 char *file = argv[optind];
1489 status &= grepfile (strcmp (file, "-") == 0 ? (char *) NULL : file,
1492 while ( ++optind < argc);
1495 status = grepfile ((char *) NULL, &stats_base);
1497 if (fclose (stdout) == EOF)
1498 error (_("writing output"), errno);
1500 exit (errseen ? 2 : status);