build: avoid another warning
[grep/ericb.git] / src / main.c
blob3fdcfb9b3145cb8248c654cd9b36ef17b93048a4
1 /* grep.c - main driver file for grep.
2 Copyright (C) 1992, 1997-2002, 2004-2010 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 3, or (at your option)
7 any later version.
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., 51 Franklin Street - Fifth Floor, Boston, MA
17 02110-1301, USA. */
19 /* Written July 1992 by Mike Haertel. */
21 #include <config.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #if defined(HAVE_SETRLIMIT)
25 # include <sys/time.h>
26 # include <sys/resource.h>
27 #endif
28 #include "mbsupport.h"
29 #ifdef MBS_SUPPORT
30 # include <wchar.h>
31 # include <wctype.h>
32 #endif
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include "system.h"
37 #include "closeout.h"
38 #include "error.h"
39 #include "exclude.h"
40 #include "exitfail.h"
41 #include "getopt.h"
42 #include "grep.h"
43 #include "intprops.h"
44 #include "isdir.h"
45 #include "progname.h"
46 #include "savedir.h"
47 #include "xalloc.h"
48 #include "xstrtol.h"
50 #define SEP_CHAR_SELECTED ':'
51 #define SEP_CHAR_REJECTED '-'
52 #define SEP_STR_GROUP "--"
54 struct stats
56 struct stats const *parent;
57 struct stat stat;
60 /* base of chain of stat buffers, used to detect directory loops */
61 static struct stats stats_base;
63 /* if non-zero, display usage information and exit */
64 static int show_help;
66 /* If non-zero, print the version on standard output and exit. */
67 static int show_version;
69 /* If nonzero, suppress diagnostics for nonexistent or unreadable files. */
70 static int suppress_errors;
72 /* If nonzero, use color markers. */
73 static int color_option;
75 /* If nonzero, show only the part of a line matching the expression. */
76 static int only_matching;
78 /* If nonzero, make sure first content char in a line is on a tab stop. */
79 static int align_tabs;
81 /* The group separator used when context is requested. */
82 static const char *group_separator = SEP_STR_GROUP;
84 /* The context and logic for choosing default --color screen attributes
85 (foreground and background colors, etc.) are the following.
86 -- There are eight basic colors available, each with its own
87 nominal luminosity to the human eye and foreground/background
88 codes (black [0 %, 30/40], blue [11 %, 34/44], red [30 %, 31/41],
89 magenta [41 %, 35/45], green [59 %, 32/42], cyan [70 %, 36/46],
90 yellow [89 %, 33/43], and white [100 %, 37/47]).
91 -- Sometimes, white as a background is actually implemented using
92 a shade of light gray, so that a foreground white can be visible
93 on top of it (but most often not).
94 -- Sometimes, black as a foreground is actually implemented using
95 a shade of dark gray, so that it can be visible on top of a
96 background black (but most often not).
97 -- Sometimes, more colors are available, as extensions.
98 -- Other attributes can be selected/deselected (bold [1/22],
99 underline [4/24], standout/inverse [7/27], blink [5/25], and
100 invisible/hidden [8/28]). They are sometimes implemented by
101 using colors instead of what their names imply; e.g., bold is
102 often achieved by using brighter colors. In practice, only bold
103 is really available to us, underline sometimes being mapped by
104 the terminal to some strange color choice, and standout best
105 being left for use by downstream programs such as less(1).
106 -- We cannot assume that any of the extensions or special features
107 are available for the purpose of choosing defaults for everyone.
108 -- The most prevalent default terminal backgrounds are pure black
109 and pure white, and are not necessarily the same shades of
110 those as if they were selected explicitly with SGR sequences.
111 Some terminals use dark or light pictures as default background,
112 but those are covered over by an explicit selection of background
113 color with an SGR sequence; their users will appreciate their
114 background pictures not be covered like this, if possible.
115 -- Some uses of colors attributes is to make some output items
116 more understated (e.g., context lines); this cannot be achieved
117 by changing the background color.
118 -- For these reasons, the grep color defaults should strive not
119 to change the background color from its default, unless it's
120 for a short item that should be highlighted, not understated.
121 -- The grep foreground color defaults (without an explicitly set
122 background) should provide enough contrast to be readable on any
123 terminal with either a black (dark) or white (light) background.
124 This only leaves red, magenta, green, and cyan (and their bold
125 counterparts) and possibly bold blue. */
126 /* The color strings used for matched text.
127 The user can overwrite them using the deprecated
128 environment variable GREP_COLOR or the new GREP_COLORS. */
129 static const char *selected_match_color = "01;31"; /* bold red */
130 static const char *context_match_color = "01;31"; /* bold red */
132 /* Other colors. Defaults look damn good. */
133 static const char *filename_color = "35"; /* magenta */
134 static const char *line_num_color = "32"; /* green */
135 static const char *byte_num_color = "32"; /* green */
136 static const char *sep_color = "36"; /* cyan */
137 static const char *selected_line_color = ""; /* default color pair */
138 static const char *context_line_color = ""; /* default color pair */
140 /* Select Graphic Rendition (SGR, "\33[...m") strings. */
141 /* Also Erase in Line (EL) to Right ("\33[K") by default. */
142 /* Why have EL to Right after SGR?
143 -- The behavior of line-wrapping when at the bottom of the
144 terminal screen and at the end of the current line is often
145 such that a new line is introduced, entirely cleared with
146 the current background color which may be different from the
147 default one (see the boolean back_color_erase terminfo(5)
148 capability), thus scrolling the display by one line.
149 The end of this new line will stay in this background color
150 even after reverting to the default background color with
151 "\33[m', unless it is explicitly cleared again with "\33[K"
152 (which is the behavior the user would instinctively expect
153 from the whole thing). There may be some unavoidable
154 background-color flicker at the end of this new line because
155 of this (when timing with the monitor's redraw is just right).
156 -- The behavior of HT (tab, "\t") is usually the same as that of
157 Cursor Forward Tabulation (CHT) with a default parameter
158 of 1 ("\33[I"), i.e., it performs pure movement to the next
159 tab stop, without any clearing of either content or screen
160 attributes (including background color); try
161 echo -ne 'asdfqwerzxcv\rASDF\tZXCV\n'
162 in a bash(1) shell to demonstrate this. This is not what the
163 user would instinctively expect of HT (but is ok for CHT).
164 The instinctive behavior would include clearing the terminal
165 cells that are skipped over by HT with blank cells in the
166 current screen attributes, including background color;
167 the boolean dest_tabs_magic_smso terminfo(5) capability
168 indicates this saner behavior for HT, but only some rare
169 terminals have it (although it also indicates a special
170 glitch with standout mode in the Teleray terminal for which
171 it was initially introduced). The remedy is to add "\33K"
172 after each SGR sequence, be it START (to fix the behavior
173 of any HT after that before another SGR) or END (to fix the
174 behavior of an HT in default background color that would
175 follow a line-wrapping at the bottom of the screen in another
176 background color, and to complement doing it after START).
177 Piping grep's output through a pager such as less(1) avoids
178 any HT problems since the pager performs tab expansion.
180 Generic disadvantages of this remedy are:
181 -- Some very rare terminals might support SGR but not EL (nobody
182 will use "grep --color" on a terminal that does not support
183 SGR in the first place).
184 -- Having these extra control sequences might somewhat complicate
185 the task of any program trying to parse "grep --color"
186 output in order to extract structuring information from it.
187 A specific disadvantage to doing it after SGR START is:
188 -- Even more possible background color flicker (when timing
189 with the monitor's redraw is just right), even when not at the
190 bottom of the screen.
191 There are no additional disadvantages specific to doing it after
192 SGR END.
194 It would be impractical for GNU grep to become a full-fledged
195 terminal program linked against ncurses or the like, so it will
196 not detect terminfo(5) capabilities. */
197 static const char *sgr_start = "\33[%sm\33[K";
198 #define SGR_START sgr_start
199 static const char *sgr_end = "\33[m\33[K";
200 #define SGR_END sgr_end
202 /* SGR utility macros. */
203 #define PR_SGR_FMT(fmt, s) do { if (*(s)) printf((fmt), (s)); } while (0)
204 #define PR_SGR_FMT_IF(fmt, s) \
205 do { if (color_option && *(s)) printf((fmt), (s)); } while (0)
206 #define PR_SGR_START(s) PR_SGR_FMT( SGR_START, (s))
207 #define PR_SGR_END(s) PR_SGR_FMT( SGR_END, (s))
208 #define PR_SGR_START_IF(s) PR_SGR_FMT_IF(SGR_START, (s))
209 #define PR_SGR_END_IF(s) PR_SGR_FMT_IF(SGR_END, (s))
211 struct color_cap
213 const char *name;
214 const char **var;
215 const char *(*fct)(void);
218 static const char *
219 color_cap_mt_fct(void)
221 /* Our caller just set selected_match_color. */
222 context_match_color = selected_match_color;
224 return NULL;
227 static const char *
228 color_cap_rv_fct(void)
230 /* By this point, it was 1 (or already -1). */
231 color_option = -1; /* That's still != 0. */
233 return NULL;
236 static const char *
237 color_cap_ne_fct(void)
239 sgr_start = "\33[%sm";
240 sgr_end = "\33[m";
242 return NULL;
245 /* For GREP_COLORS. */
246 static struct color_cap color_dict[] =
248 { "mt", &selected_match_color, color_cap_mt_fct }, /* both ms/mc */
249 { "ms", &selected_match_color, NULL }, /* selected matched text */
250 { "mc", &context_match_color, NULL }, /* context matched text */
251 { "fn", &filename_color, NULL }, /* filename */
252 { "ln", &line_num_color, NULL }, /* line number */
253 { "bn", &byte_num_color, NULL }, /* byte (sic) offset */
254 { "se", &sep_color, NULL }, /* separator */
255 { "sl", &selected_line_color, NULL }, /* selected lines */
256 { "cx", &context_line_color, NULL }, /* context lines */
257 { "rv", NULL, color_cap_rv_fct }, /* -v reverses sl/cx */
258 { "ne", NULL, color_cap_ne_fct }, /* no EL on SGR_* */
259 { NULL, NULL, NULL }
262 static struct exclude *excluded_patterns;
263 static struct exclude *included_patterns;
264 static struct exclude *excluded_directory_patterns;
265 /* Short options. */
266 static char const short_options[] =
267 "0123456789A:B:C:D:EFGHIPTUVX:abcd:e:f:hiKLlm:noqRrsuvwxyZz";
269 /* Non-boolean long options that have no corresponding short equivalents. */
270 enum
272 BINARY_FILES_OPTION = CHAR_MAX + 1,
273 COLOR_OPTION,
274 INCLUDE_OPTION,
275 EXCLUDE_OPTION,
276 EXCLUDE_FROM_OPTION,
277 LINE_BUFFERED_OPTION,
278 LABEL_OPTION,
279 EXCLUDE_DIRECTORY_OPTION,
280 GROUP_SEPARATOR_OPTION,
281 MMAP_OPTION
284 /* Long options equivalences. */
285 static struct option const long_options[] =
287 {"basic-regexp", no_argument, NULL, 'G'},
288 {"extended-regexp", no_argument, NULL, 'E'},
289 {"fixed-regexp", no_argument, NULL, 'F'},
290 {"fixed-strings", no_argument, NULL, 'F'},
291 {"perl-regexp", no_argument, NULL, 'P'},
292 {"after-context", required_argument, NULL, 'A'},
293 {"before-context", required_argument, NULL, 'B'},
294 {"binary-files", required_argument, NULL, BINARY_FILES_OPTION},
295 {"byte-offset", no_argument, NULL, 'b'},
296 {"context", required_argument, NULL, 'C'},
297 {"color", optional_argument, NULL, COLOR_OPTION},
298 {"colour", optional_argument, NULL, COLOR_OPTION},
299 {"count", no_argument, NULL, 'c'},
300 {"devices", required_argument, NULL, 'D'},
301 {"directories", required_argument, NULL, 'd'},
302 {"exclude", required_argument, NULL, EXCLUDE_OPTION},
303 {"exclude-from", required_argument, NULL, EXCLUDE_FROM_OPTION},
304 {"exclude-dir", required_argument, NULL, EXCLUDE_DIRECTORY_OPTION},
305 {"file", required_argument, NULL, 'f'},
306 {"files-with-matches", no_argument, NULL, 'l'},
307 {"files-without-match", no_argument, NULL, 'L'},
308 {"group-separator", required_argument, NULL, GROUP_SEPARATOR_OPTION},
309 {"help", no_argument, &show_help, 1},
310 {"include", required_argument, NULL, INCLUDE_OPTION},
311 {"ignore-case", no_argument, NULL, 'i'},
312 {"initial-tab", no_argument, NULL, 'T'},
313 {"label", required_argument, NULL, LABEL_OPTION},
314 {"line-buffered", no_argument, NULL, LINE_BUFFERED_OPTION},
315 {"line-number", no_argument, NULL, 'n'},
316 {"line-regexp", no_argument, NULL, 'x'},
317 {"max-count", required_argument, NULL, 'm'},
319 /* FIXME: disabled in Mar 2010; warn towards end of 2011; remove in 2013. */
320 {"mmap", no_argument, NULL, MMAP_OPTION},
321 {"no-filename", no_argument, NULL, 'h'},
322 {"no-group-separator", no_argument, NULL, GROUP_SEPARATOR_OPTION},
323 {"no-messages", no_argument, NULL, 's'},
324 {"null", no_argument, NULL, 'Z'},
325 {"null-data", no_argument, NULL, 'z'},
326 {"only-matching", no_argument, NULL, 'o'},
327 {"quiet", no_argument, NULL, 'q'},
328 {"recursive", no_argument, NULL, 'r'},
329 {"recursive", no_argument, NULL, 'R'},
330 {"regexp", required_argument, NULL, 'e'},
331 {"invert-match", no_argument, NULL, 'v'},
332 {"silent", no_argument, NULL, 'q'},
333 {"text", no_argument, NULL, 'a'},
334 {"binary", no_argument, NULL, 'U'},
335 {"unix-byte-offsets", no_argument, NULL, 'u'},
336 {"version", no_argument, NULL, 'V'},
337 {"with-filename", no_argument, NULL, 'H'},
338 {"word-regexp", no_argument, NULL, 'w'},
339 {0, 0, 0, 0}
342 /* Define flags declared in grep.h. */
343 int match_icase;
344 int match_words;
345 int match_lines;
346 unsigned char eolbyte;
348 /* For error messages. */
349 /* The name the program was run with, stripped of any leading path. */
350 static char const *filename;
351 static int errseen;
353 /* How to handle directories. */
354 static enum
356 READ_DIRECTORIES,
357 RECURSE_DIRECTORIES,
358 SKIP_DIRECTORIES
359 } directories = READ_DIRECTORIES;
361 /* How to handle devices. */
362 static enum
364 READ_DEVICES,
365 SKIP_DEVICES
366 } devices = READ_DEVICES;
368 static int grepdir (char const *, struct stats const *);
369 #if defined(HAVE_DOS_FILE_CONTENTS)
370 static inline int undossify_input (char *, size_t);
371 #endif
373 /* Functions we'll use to search. */
374 static compile_fp_t compile;
375 static execute_fp_t execute;
377 /* Like error, but suppress the diagnostic if requested. */
378 static void
379 suppressible_error (char const *mesg, int errnum)
381 if (! suppress_errors)
382 error (0, errnum, "%s", mesg);
383 errseen = 1;
386 /* Convert STR to a positive integer, storing the result in *OUT.
387 STR must be a valid context length argument; report an error if it
388 isn't. */
389 static void
390 context_length_arg (char const *str, int *out)
392 uintmax_t value;
393 if (! (xstrtoumax (str, 0, 10, &value, "") == LONGINT_OK
394 && 0 <= (*out = value)
395 && *out == value))
397 error (EXIT_TROUBLE, 0, "%s: %s\n", str,
398 _("invalid context length argument"));
403 /* Hairy buffering mechanism for grep. The intent is to keep
404 all reads aligned on a page boundary and multiples of the
405 page size, unless a read yields a partial page. */
407 static char *buffer; /* Base of buffer. */
408 static size_t bufalloc; /* Allocated buffer size, counting slop. */
409 #define INITIAL_BUFSIZE 32768 /* Initial buffer size, not counting slop. */
410 static int bufdesc; /* File descriptor. */
411 static char *bufbeg; /* Beginning of user-visible stuff. */
412 static char *buflim; /* Limit of user-visible stuff. */
413 static size_t pagesize; /* alignment of memory pages */
414 static off_t bufoffset; /* Read offset; defined on regular files. */
415 static off_t after_last_match; /* Pointer after last matching line that
416 would have been output if we were
417 outputting characters. */
419 /* Return VAL aligned to the next multiple of ALIGNMENT. VAL can be
420 an integer or a pointer. Both args must be free of side effects. */
421 #define ALIGN_TO(val, alignment) \
422 ((size_t) (val) % (alignment) == 0 \
423 ? (val) \
424 : (val) + ((alignment) - (size_t) (val) % (alignment)))
426 /* Reset the buffer for a new file, returning zero if we should skip it.
427 Initialize on the first time through. */
428 static int
429 reset (int fd, char const *file, struct stats *stats)
431 if (! pagesize)
433 pagesize = getpagesize ();
434 if (pagesize == 0 || 2 * pagesize + 1 <= pagesize)
435 abort ();
436 bufalloc = ALIGN_TO (INITIAL_BUFSIZE, pagesize) + pagesize + 1;
437 buffer = xmalloc (bufalloc);
440 bufbeg = buflim = ALIGN_TO (buffer + 1, pagesize);
441 bufbeg[-1] = eolbyte;
442 bufdesc = fd;
444 if (S_ISREG (stats->stat.st_mode))
446 if (file)
447 bufoffset = 0;
448 else
450 bufoffset = lseek (fd, 0, SEEK_CUR);
451 if (bufoffset < 0)
453 error (0, errno, _("lseek failed"));
454 return 0;
458 return 1;
461 /* Read new stuff into the buffer, saving the specified
462 amount of old stuff. When we're done, 'bufbeg' points
463 to the beginning of the buffer contents, and 'buflim'
464 points just after the end. Return zero if there's an error. */
465 static int
466 fillbuf (size_t save, struct stats const *stats)
468 size_t fillsize = 0;
469 int cc = 1;
470 char *readbuf;
471 size_t readsize;
473 /* Offset from start of buffer to start of old stuff
474 that we want to save. */
475 size_t saved_offset = buflim - save - buffer;
477 if (pagesize <= buffer + bufalloc - buflim)
479 readbuf = buflim;
480 bufbeg = buflim - save;
482 else
484 size_t minsize = save + pagesize;
485 size_t newsize;
486 size_t newalloc;
487 char *newbuf;
489 /* Grow newsize until it is at least as great as minsize. */
490 for (newsize = bufalloc - pagesize - 1; newsize < minsize; newsize *= 2)
491 if (newsize * 2 < newsize || newsize * 2 + pagesize + 1 < newsize * 2)
492 xalloc_die ();
494 /* Try not to allocate more memory than the file size indicates,
495 as that might cause unnecessary memory exhaustion if the file
496 is large. However, do not use the original file size as a
497 heuristic if we've already read past the file end, as most
498 likely the file is growing. */
499 if (S_ISREG (stats->stat.st_mode))
501 off_t to_be_read = stats->stat.st_size - bufoffset;
502 off_t maxsize_off = save + to_be_read;
503 if (0 <= to_be_read && to_be_read <= maxsize_off
504 && maxsize_off == (size_t) maxsize_off
505 && minsize <= (size_t) maxsize_off
506 && (size_t) maxsize_off < newsize)
507 newsize = maxsize_off;
510 /* Add enough room so that the buffer is aligned and has room
511 for byte sentinels fore and aft. */
512 newalloc = newsize + pagesize + 1;
514 newbuf = bufalloc < newalloc ? xmalloc (bufalloc = newalloc) : buffer;
515 readbuf = ALIGN_TO (newbuf + 1 + save, pagesize);
516 bufbeg = readbuf - save;
517 memmove (bufbeg, buffer + saved_offset, save);
518 bufbeg[-1] = eolbyte;
519 if (newbuf != buffer)
521 free (buffer);
522 buffer = newbuf;
526 readsize = buffer + bufalloc - readbuf;
527 readsize -= readsize % pagesize;
529 if (! fillsize)
531 ssize_t bytesread;
532 while ((bytesread = read (bufdesc, readbuf, readsize)) < 0
533 && errno == EINTR)
534 continue;
535 if (bytesread < 0)
536 cc = 0;
537 else
538 fillsize = bytesread;
541 bufoffset += fillsize;
542 #if defined(HAVE_DOS_FILE_CONTENTS)
543 if (fillsize)
544 fillsize = undossify_input (readbuf, fillsize);
545 #endif
546 buflim = readbuf + fillsize;
547 return cc;
550 /* Flags controlling the style of output. */
551 static enum
553 BINARY_BINARY_FILES,
554 TEXT_BINARY_FILES,
555 WITHOUT_MATCH_BINARY_FILES
556 } binary_files; /* How to handle binary files. */
558 static int filename_mask; /* If zero, output nulls after filenames. */
559 static int out_quiet; /* Suppress all normal output. */
560 static int out_invert; /* Print nonmatching stuff. */
561 static int out_file; /* Print filenames. */
562 static int out_line; /* Print line numbers. */
563 static int out_byte; /* Print byte offsets. */
564 static int out_before; /* Lines of leading context. */
565 static int out_after; /* Lines of trailing context. */
566 static int count_matches; /* Count matching lines. */
567 static int list_files; /* List matching files. */
568 static int no_filenames; /* Suppress file names. */
569 static off_t max_count; /* Stop after outputting this many
570 lines from an input file. */
571 static int line_buffered; /* If nonzero, use line buffering, i.e.
572 fflush everyline out. */
573 static char *label = NULL; /* Fake filename for stdin */
576 /* Internal variables to keep track of byte count, context, etc. */
577 static uintmax_t totalcc; /* Total character count before bufbeg. */
578 static char const *lastnl; /* Pointer after last newline counted. */
579 static char const *lastout; /* Pointer after last character output;
580 NULL if no character has been output
581 or if it's conceptually before bufbeg. */
582 static uintmax_t totalnl; /* Total newline count before lastnl. */
583 static off_t outleft; /* Maximum number of lines to be output. */
584 static int pending; /* Pending lines of output.
585 Always kept 0 if out_quiet is true. */
586 static int done_on_match; /* Stop scanning file on first match. */
587 static int exit_on_match; /* Exit on first match. */
589 #if defined(HAVE_DOS_FILE_CONTENTS)
590 # include "dosbuf.c"
591 #endif
593 /* Add two numbers that count input bytes or lines, and report an
594 error if the addition overflows. */
595 static uintmax_t
596 add_count (uintmax_t a, uintmax_t b)
598 uintmax_t sum = a + b;
599 if (sum < a)
600 error (EXIT_TROUBLE, 0, _("input is too large to count"));
601 return sum;
604 static void
605 nlscan (char const *lim)
607 size_t newlines = 0;
608 char const *beg;
609 for (beg = lastnl; beg < lim; beg++)
611 beg = memchr (beg, eolbyte, lim - beg);
612 if (!beg)
613 break;
614 newlines++;
616 totalnl = add_count (totalnl, newlines);
617 lastnl = lim;
620 /* Print the current filename. */
621 static void
622 print_filename (void)
624 PR_SGR_START_IF(filename_color);
625 fputs(filename, stdout);
626 PR_SGR_END_IF(filename_color);
629 /* Print a character separator. */
630 static void
631 print_sep (char sep)
633 PR_SGR_START_IF(sep_color);
634 fputc(sep, stdout);
635 PR_SGR_END_IF(sep_color);
638 /* Print a line number or a byte offset. */
639 static void
640 print_offset (uintmax_t pos, int min_width, const char *color)
642 /* Do not rely on printf to print pos, since uintmax_t may be longer
643 than long, and long long is not portable. */
645 char buf[sizeof pos * CHAR_BIT];
646 char *p = buf + sizeof buf;
650 *--p = '0' + pos % 10;
651 --min_width;
653 while ((pos /= 10) != 0);
655 /* Do this to maximize the probability of alignment across lines. */
656 if (align_tabs)
657 while (--min_width >= 0)
658 *--p = ' ';
660 PR_SGR_START_IF(color);
661 fwrite (p, 1, buf + sizeof buf - p, stdout);
662 PR_SGR_END_IF(color);
665 /* Print a whole line head (filename, line, byte). */
666 static void
667 print_line_head (char const *beg, char const *lim, int sep)
669 int pending_sep = 0;
671 if (out_file)
673 print_filename();
674 if (filename_mask)
675 pending_sep = 1;
676 else
677 fputc(0, stdout);
680 if (out_line)
682 if (lastnl < lim)
684 nlscan (beg);
685 totalnl = add_count (totalnl, 1);
686 lastnl = lim;
688 if (pending_sep)
689 print_sep(sep);
690 print_offset (totalnl, 4, line_num_color);
691 pending_sep = 1;
694 if (out_byte)
696 uintmax_t pos = add_count (totalcc, beg - bufbeg);
697 #if defined(HAVE_DOS_FILE_CONTENTS)
698 pos = dossified_pos (pos);
699 #endif
700 if (pending_sep)
701 print_sep(sep);
702 print_offset (pos, 6, byte_num_color);
703 pending_sep = 1;
706 if (pending_sep)
708 /* This assumes sep is one column wide.
709 Try doing this any other way with Unicode
710 (and its combining and wide characters)
711 filenames and you're wasting your efforts. */
712 if (align_tabs)
713 fputs("\t\b", stdout);
715 print_sep(sep);
719 static const char *
720 print_line_middle (const char *beg, const char *lim,
721 const char *line_color, const char *match_color)
723 size_t match_size;
724 size_t match_offset;
725 const char *cur = beg;
726 const char *mid = NULL;
727 char *buf;
728 const char *ibeg;
730 /* XXX - should not be needed anymore now that we use RE_ICASE.
731 Revisit after 2.6.x stabilizes. */
732 if (match_icase
733 #ifdef MBS_SUPPORT
734 && MB_CUR_MAX == 1
735 #endif
738 int i = lim - beg;
740 ibeg = buf = xmalloc(i);
741 while (--i >= 0)
742 buf[i] = tolower((unsigned char) beg[i]);
744 else
746 buf = NULL;
747 ibeg = beg;
750 while ( lim > cur
751 && ((match_offset = execute(ibeg, lim - beg, &match_size,
752 ibeg + (cur - beg))) != (size_t) -1))
754 char const *b = beg + match_offset;
756 /* Avoid matching the empty line at the end of the buffer. */
757 if (b == lim)
758 break;
760 /* Avoid hanging on grep --color "" foo */
761 if (match_size == 0)
763 /* Make minimal progress; there may be further non-empty matches. */
764 /* XXX - Could really advance by one whole multi-octet character. */
765 match_size = 1;
766 if (!mid)
767 mid = cur;
769 else
771 /* This function is called on a matching line only,
772 but is it selected or rejected/context? */
773 if (only_matching)
774 print_line_head(b, lim, out_invert ? SEP_CHAR_REJECTED
775 : SEP_CHAR_SELECTED);
776 else
778 PR_SGR_START(line_color);
779 if (mid)
781 cur = mid;
782 mid = NULL;
784 fwrite (cur, sizeof (char), b - cur, stdout);
787 PR_SGR_START_IF(match_color);
788 fwrite (b, sizeof (char), match_size, stdout);
789 PR_SGR_END_IF(match_color);
790 if (only_matching)
791 fputs("\n", stdout);
793 cur = b + match_size;
796 free (buf); /* XXX */
798 if (only_matching)
799 cur = lim;
800 else if (mid)
801 cur = mid;
803 return cur;
806 static const char *
807 print_line_tail (const char *beg, const char *lim, const char *line_color)
809 size_t eol_size;
810 size_t tail_size;
812 eol_size = (lim > beg && lim[-1] == eolbyte);
813 eol_size += (lim - eol_size > beg && lim[-(1 + eol_size)] == '\r');
814 tail_size = lim - eol_size - beg;
816 if (tail_size > 0)
818 PR_SGR_START(line_color);
819 fwrite(beg, 1, tail_size, stdout);
820 beg += tail_size;
821 PR_SGR_END(line_color);
824 return beg;
827 static void
828 prline (char const *beg, char const *lim, int sep)
830 int matching;
831 const char *line_color;
832 const char *match_color;
834 if (!only_matching)
835 print_line_head(beg, lim, sep);
837 matching = (sep == SEP_CHAR_SELECTED) ^ !!out_invert;
839 if (color_option)
841 line_color = ( (sep == SEP_CHAR_SELECTED)
842 ^ (out_invert && (color_option < 0)))
843 ? selected_line_color : context_line_color;
844 match_color = (sep == SEP_CHAR_SELECTED)
845 ? selected_match_color : context_match_color;
847 else
848 line_color = match_color = NULL; /* Shouldn't be used. */
850 if ( (only_matching && matching)
851 || (color_option && (*line_color || *match_color)))
853 /* We already know that non-matching lines have no match (to colorize). */
854 if (matching && (only_matching || *match_color))
855 beg = print_line_middle(beg, lim, line_color, match_color);
857 /* FIXME: this test may be removable. */
858 if (!only_matching && *line_color)
859 beg = print_line_tail(beg, lim, line_color);
862 if (!only_matching && lim > beg)
863 fwrite (beg, 1, lim - beg, stdout);
865 if (ferror (stdout))
866 error (0, errno, _("writing output"));
868 lastout = lim;
870 if (line_buffered)
871 fflush (stdout);
874 /* Print pending lines of trailing context prior to LIM. Trailing context ends
875 at the next matching line when OUTLEFT is 0. */
876 static void
877 prpending (char const *lim)
879 if (!lastout)
880 lastout = bufbeg;
881 while (pending > 0 && lastout < lim)
883 char const *nl = memchr (lastout, eolbyte, lim - lastout);
884 size_t match_size;
885 --pending;
886 if (outleft
887 || ((execute(lastout, nl + 1 - lastout,
888 &match_size, NULL) == (size_t) -1)
889 == !out_invert))
890 prline (lastout, nl + 1, SEP_CHAR_REJECTED);
891 else
892 pending = 0;
896 /* Print the lines between BEG and LIM. Deal with context crap.
897 If NLINESP is non-null, store a count of lines between BEG and LIM. */
898 static void
899 prtext (char const *beg, char const *lim, int *nlinesp)
901 static int used; /* avoid printing SEP_STR_GROUP before any output */
902 char const *bp, *p;
903 char eol = eolbyte;
904 int i, n;
906 if (!out_quiet && pending > 0)
907 prpending (beg);
909 p = beg;
911 if (!out_quiet)
913 /* Deal with leading context crap. */
915 bp = lastout ? lastout : bufbeg;
916 for (i = 0; i < out_before; ++i)
917 if (p > bp)
919 --p;
920 while (p[-1] != eol);
922 /* We print the SEP_STR_GROUP separator only if our output is
923 discontiguous from the last output in the file. */
924 if ((out_before || out_after) && used && p != lastout && group_separator)
926 PR_SGR_START_IF(sep_color);
927 fputs (group_separator, stdout);
928 PR_SGR_END_IF(sep_color);
929 fputc('\n', stdout);
932 while (p < beg)
934 char const *nl = memchr (p, eol, beg - p);
935 nl++;
936 prline (p, nl, SEP_CHAR_REJECTED);
937 p = nl;
941 if (nlinesp)
943 /* Caller wants a line count. */
944 for (n = 0; p < lim && n < outleft; n++)
946 char const *nl = memchr (p, eol, lim - p);
947 nl++;
948 if (!out_quiet)
949 prline (p, nl, SEP_CHAR_SELECTED);
950 p = nl;
952 *nlinesp = n;
954 /* relying on it that this function is never called when outleft = 0. */
955 after_last_match = bufoffset - (buflim - p);
957 else
958 if (!out_quiet)
959 prline (beg, lim, SEP_CHAR_SELECTED);
961 pending = out_quiet ? 0 : out_after;
962 used = 1;
965 static size_t
966 do_execute (char const *buf, size_t size, size_t *match_size, char const *start_ptr)
968 size_t result;
969 const char *line_next;
971 /* With the current implementation, using --ignore-case with a multi-byte
972 character set is very inefficient when applied to a large buffer
973 containing many matches. We can avoid much of the wasted effort
974 by matching line-by-line.
976 FIXME: this is just an ugly workaround, and it doesn't really
977 belong here. Also, PCRE is always using this same per-line
978 matching algorithm. Either we fix -i, or we should refactor
979 this code---for example, we could add another function pointer
980 to struct matcher to split the buffer passed to execute. It would
981 perform the memchr if line-by-line matching is necessary, or just
982 return buf + size otherwise. */
983 if (MB_CUR_MAX == 1 || !match_icase)
984 return execute(buf, size, match_size, start_ptr);
986 for (line_next = buf; line_next < buf + size; )
988 const char *line_buf = line_next;
989 const char *line_end = memchr (line_buf, eolbyte, (buf + size) - line_buf);
990 if (line_end == NULL)
991 line_next = line_end = buf + size;
992 else
993 line_next = line_end + 1;
995 if (start_ptr && start_ptr >= line_end)
996 continue;
998 result = execute (line_buf, line_next - line_buf, match_size, start_ptr);
999 if (result != (size_t) -1)
1000 return (line_buf - buf) + result;
1003 return (size_t) -1;
1006 /* Scan the specified portion of the buffer, matching lines (or
1007 between matching lines if OUT_INVERT is true). Return a count of
1008 lines printed. */
1009 static int
1010 grepbuf (char const *beg, char const *lim)
1012 int nlines, n;
1013 char const *p;
1014 size_t match_offset;
1015 size_t match_size;
1017 nlines = 0;
1018 p = beg;
1019 while ((match_offset = do_execute(p, lim - p, &match_size,
1020 NULL)) != (size_t) -1)
1022 char const *b = p + match_offset;
1023 char const *endp = b + match_size;
1024 /* Avoid matching the empty line at the end of the buffer. */
1025 if (b == lim)
1026 break;
1027 if (!out_invert)
1029 prtext (b, endp, (int *) 0);
1030 nlines++;
1031 outleft--;
1032 if (!outleft || done_on_match)
1034 if (exit_on_match)
1035 exit (EXIT_SUCCESS);
1036 after_last_match = bufoffset - (buflim - endp);
1037 return nlines;
1040 else if (p < b)
1042 prtext (p, b, &n);
1043 nlines += n;
1044 outleft -= n;
1045 if (!outleft)
1046 return nlines;
1048 p = endp;
1050 if (out_invert && p < lim)
1052 prtext (p, lim, &n);
1053 nlines += n;
1054 outleft -= n;
1056 return nlines;
1059 /* Search a given file. Normally, return a count of lines printed;
1060 but if the file is a directory and we search it recursively, then
1061 return -2 if there was a match, and -1 otherwise. */
1062 static int
1063 grep (int fd, char const *file, struct stats *stats)
1065 int nlines, i;
1066 int not_text;
1067 size_t residue, save;
1068 char oldc;
1069 char *beg;
1070 char *lim;
1071 char eol = eolbyte;
1073 if (!reset (fd, file, stats))
1074 return 0;
1076 if (file && directories == RECURSE_DIRECTORIES
1077 && S_ISDIR (stats->stat.st_mode))
1079 /* Close fd now, so that we don't open a lot of file descriptors
1080 when we recurse deeply. */
1081 if (close (fd) != 0)
1082 error (0, errno, "%s", file);
1083 return grepdir (file, stats) - 2;
1086 totalcc = 0;
1087 lastout = 0;
1088 totalnl = 0;
1089 outleft = max_count;
1090 after_last_match = 0;
1091 pending = 0;
1093 nlines = 0;
1094 residue = 0;
1095 save = 0;
1097 if (! fillbuf (save, stats))
1099 if (! is_EISDIR (errno, file))
1100 suppressible_error (filename, errno);
1101 return 0;
1104 not_text = (((binary_files == BINARY_BINARY_FILES && !out_quiet)
1105 || binary_files == WITHOUT_MATCH_BINARY_FILES)
1106 && memchr (bufbeg, eol ? '\0' : '\200', buflim - bufbeg));
1107 if (not_text && binary_files == WITHOUT_MATCH_BINARY_FILES)
1108 return 0;
1109 done_on_match += not_text;
1110 out_quiet += not_text;
1112 for (;;)
1114 lastnl = bufbeg;
1115 if (lastout)
1116 lastout = bufbeg;
1118 beg = bufbeg + save;
1120 /* no more data to scan (eof) except for maybe a residue -> break */
1121 if (beg == buflim)
1122 break;
1124 /* Determine new residue (the length of an incomplete line at the end of
1125 the buffer, 0 means there is no incomplete last line). */
1126 oldc = beg[-1];
1127 beg[-1] = eol;
1128 for (lim = buflim; lim[-1] != eol; lim--)
1129 continue;
1130 beg[-1] = oldc;
1131 if (lim == beg)
1132 lim = beg - residue;
1133 beg -= residue;
1134 residue = buflim - lim;
1136 if (beg < lim)
1138 if (outleft)
1139 nlines += grepbuf (beg, lim);
1140 if (pending)
1141 prpending (lim);
1142 if((!outleft && !pending) || (nlines && done_on_match && !out_invert))
1143 goto finish_grep;
1146 /* The last OUT_BEFORE lines at the end of the buffer will be needed as
1147 leading context if there is a matching line at the begin of the
1148 next data. Make beg point to their begin. */
1149 i = 0;
1150 beg = lim;
1151 while (i < out_before && beg > bufbeg && beg != lastout)
1153 ++i;
1155 --beg;
1156 while (beg[-1] != eol);
1159 /* detect if leading context is discontinuous from last printed line. */
1160 if (beg != lastout)
1161 lastout = 0;
1163 /* Handle some details and read more data to scan. */
1164 save = residue + lim - beg;
1165 if (out_byte)
1166 totalcc = add_count (totalcc, buflim - bufbeg - save);
1167 if (out_line)
1168 nlscan (beg);
1169 if (! fillbuf (save, stats))
1171 if (! is_EISDIR (errno, file))
1172 suppressible_error (filename, errno);
1173 goto finish_grep;
1176 if (residue)
1178 *buflim++ = eol;
1179 if (outleft)
1180 nlines += grepbuf (bufbeg + save - residue, buflim);
1181 if (pending)
1182 prpending (buflim);
1185 finish_grep:
1186 done_on_match -= not_text;
1187 out_quiet -= not_text;
1188 if ((not_text & ~out_quiet) && nlines != 0)
1189 printf (_("Binary file %s matches\n"), filename);
1190 return nlines;
1193 static int
1194 grepfile (char const *file, struct stats *stats)
1196 int desc;
1197 int count;
1198 int status;
1200 if (! file)
1202 desc = 0;
1203 filename = label ? label : _("(standard input)");
1205 else
1207 if (stat (file, &stats->stat) != 0)
1209 suppressible_error (file, errno);
1210 return 1;
1212 if (directories == SKIP_DIRECTORIES && S_ISDIR (stats->stat.st_mode))
1213 return 1;
1214 if (devices == SKIP_DEVICES && (S_ISCHR (stats->stat.st_mode)
1215 || S_ISBLK (stats->stat.st_mode)
1216 || S_ISSOCK (stats->stat.st_mode)
1217 || S_ISFIFO (stats->stat.st_mode)))
1218 return 1;
1219 while ((desc = open (file, O_RDONLY)) < 0 && errno == EINTR)
1220 continue;
1222 if (desc < 0)
1224 int e = errno;
1226 if (is_EISDIR (e, file) && directories == RECURSE_DIRECTORIES)
1228 if (stat (file, &stats->stat) != 0)
1230 error (0, errno, "%s", file);
1231 return 1;
1234 return grepdir (file, stats);
1237 if (!suppress_errors)
1239 if (directories == SKIP_DIRECTORIES)
1240 switch (e)
1242 #if defined(EISDIR)
1243 case EISDIR:
1244 return 1;
1245 #endif
1246 case EACCES:
1247 /* When skipping directories, don't worry about
1248 directories that can't be opened. */
1249 if (isdir (file))
1250 return 1;
1251 break;
1255 suppressible_error (file, e);
1256 return 1;
1259 filename = file;
1262 #if defined(SET_BINARY)
1263 /* Set input to binary mode. Pipes are simulated with files
1264 on DOS, so this includes the case of "foo | grep bar". */
1265 if (!isatty (desc))
1266 SET_BINARY (desc);
1267 #endif
1269 count = grep (desc, file, stats);
1270 if (count < 0)
1271 status = count + 2;
1272 else
1274 if (count_matches)
1276 if (out_file)
1278 print_filename();
1279 if (filename_mask)
1280 print_sep(SEP_CHAR_SELECTED);
1281 else
1282 fputc(0, stdout);
1284 printf ("%d\n", count);
1287 status = !count;
1288 if (list_files == 1 - 2 * status)
1290 print_filename();
1291 fputc('\n' & filename_mask, stdout);
1294 if (! file)
1296 off_t required_offset = outleft ? bufoffset : after_last_match;
1297 if (required_offset != bufoffset
1298 && lseek (desc, required_offset, SEEK_SET) < 0
1299 && S_ISREG (stats->stat.st_mode))
1300 error (0, errno, "%s", filename);
1302 else
1303 while (close (desc) != 0)
1304 if (errno != EINTR)
1306 error (0, errno, "%s", file);
1307 break;
1311 return status;
1314 static int
1315 grepdir (char const *dir, struct stats const *stats)
1317 struct stats const *ancestor;
1318 char *name_space;
1319 int status = 1;
1320 if ( excluded_directory_patterns &&
1321 excluded_file_name (excluded_directory_patterns, dir) ) {
1322 return 1;
1326 /* Mingw32 does not support st_ino. No known working hosts use zero
1327 for st_ino, so assume that the Mingw32 bug applies if it's zero. */
1328 if (stats->stat.st_ino)
1329 for (ancestor = stats; (ancestor = ancestor->parent) != 0; )
1330 if (ancestor->stat.st_ino == stats->stat.st_ino
1331 && ancestor->stat.st_dev == stats->stat.st_dev)
1333 if (!suppress_errors)
1334 error (0, 0, _("warning: %s: %s\n"), dir,
1335 _("recursive directory loop"));
1336 return 1;
1339 name_space = savedir (dir, stats->stat.st_size, included_patterns,
1340 excluded_patterns, excluded_directory_patterns);
1342 if (! name_space)
1344 if (errno)
1345 suppressible_error (dir, errno);
1346 else
1347 xalloc_die ();
1349 else
1351 size_t dirlen = strlen (dir);
1352 int needs_slash = ! (dirlen == FILE_SYSTEM_PREFIX_LEN (dir)
1353 || ISSLASH (dir[dirlen - 1]));
1354 char *file = NULL;
1355 char const *namep = name_space;
1356 struct stats child;
1357 child.parent = stats;
1358 out_file += !no_filenames;
1359 while (*namep)
1361 size_t namelen = strlen (namep);
1362 file = xrealloc (file, dirlen + 1 + namelen + 1);
1363 strcpy (file, dir);
1364 file[dirlen] = '/';
1365 strcpy (file + dirlen + needs_slash, namep);
1366 namep += namelen + 1;
1367 status &= grepfile (file, &child);
1369 out_file -= !no_filenames;
1370 free (file);
1371 free (name_space);
1374 return status;
1377 static void usage (int status) __attribute__ ((noreturn));
1378 static void
1379 usage (int status)
1381 if (status != 0)
1383 fprintf (stderr, _("Usage: %s [OPTION]... PATTERN [FILE]...\n"),
1384 program_name);
1385 fprintf (stderr, _("Try `%s --help' for more information.\n"),
1386 program_name);
1388 else
1390 printf (_("Usage: %s [OPTION]... PATTERN [FILE]...\n"), program_name);
1391 printf (_("\
1392 Search for PATTERN in each FILE or standard input.\n"));
1393 printf ("%s", gettext (before_options));
1394 printf (_("\
1395 Example: %s -i 'hello world' menu.h main.c\n\
1397 Regexp selection and interpretation:\n"), program_name);
1398 if (matchers[1].name)
1399 printf (_("\
1400 -E, --extended-regexp PATTERN is an extended regular expression (ERE)\n\
1401 -F, --fixed-strings PATTERN is a set of newline-separated fixed strings\n\
1402 -G, --basic-regexp PATTERN is a basic regular expression (BRE)\n\
1403 -P, --perl-regexp PATTERN is a Perl regular expression\n"));
1404 /* -X is undocumented on purpose. */
1405 printf (_("\
1406 -e, --regexp=PATTERN use PATTERN for matching\n\
1407 -f, --file=FILE obtain PATTERN from FILE\n\
1408 -i, --ignore-case ignore case distinctions\n\
1409 -w, --word-regexp force PATTERN to match only whole words\n\
1410 -x, --line-regexp force PATTERN to match only whole lines\n\
1411 -z, --null-data a data line ends in 0 byte, not newline\n"));
1412 printf (_("\
1414 Miscellaneous:\n\
1415 -s, --no-messages suppress error messages\n\
1416 -v, --invert-match select non-matching lines\n\
1417 -V, --version print version information and exit\n\
1418 --help display this help and exit\n\
1419 --mmap ignored for backwards compatibility\n"));
1420 printf (_("\
1422 Output control:\n\
1423 -m, --max-count=NUM stop after NUM matches\n\
1424 -b, --byte-offset print the byte offset with output lines\n\
1425 -n, --line-number print line number with output lines\n\
1426 --line-buffered flush output on every line\n\
1427 -H, --with-filename print the filename for each match\n\
1428 -h, --no-filename suppress the prefixing filename on output\n\
1429 --label=LABEL print LABEL as filename for standard input\n\
1430 "));
1431 printf (_("\
1432 -o, --only-matching show only the part of a line matching PATTERN\n\
1433 -q, --quiet, --silent suppress all normal output\n\
1434 --binary-files=TYPE assume that binary files are TYPE;\n\
1435 TYPE is `binary', `text', or `without-match'\n\
1436 -a, --text equivalent to --binary-files=text\n\
1437 "));
1438 printf (_("\
1439 -I equivalent to --binary-files=without-match\n\
1440 -d, --directories=ACTION how to handle directories;\n\
1441 ACTION is `read', `recurse', or `skip'\n\
1442 -D, --devices=ACTION how to handle devices, FIFOs and sockets;\n\
1443 ACTION is `read' or `skip'\n\
1444 -R, -r, --recursive equivalent to --directories=recurse\n\
1445 "));
1446 printf (_("\
1447 --include=FILE_PATTERN search only files that match FILE_PATTERN\n\
1448 --exclude=FILE_PATTERN skip files and directories matching FILE_PATTERN\n\
1449 --exclude-from=FILE skip files matching any file pattern from FILE\n\
1450 --exclude-dir=PATTERN directories that match PATTERN will be skipped.\n\
1451 "));
1452 printf (_("\
1453 -L, --files-without-match print only names of FILEs containing no match\n\
1454 -l, --files-with-matches print only names of FILEs containing matches\n\
1455 -c, --count print only a count of matching lines per FILE\n\
1456 -T, --initial-tab make tabs line up (if needed)\n\
1457 -Z, --null print 0 byte after FILE name\n"));
1458 printf (_("\
1460 Context control:\n\
1461 -B, --before-context=NUM print NUM lines of leading context\n\
1462 -A, --after-context=NUM print NUM lines of trailing context\n\
1463 -C, --context=NUM print NUM lines of output context\n\
1464 "));
1465 printf (_("\
1466 -NUM same as --context=NUM\n\
1467 --color[=WHEN],\n\
1468 --colour[=WHEN] use markers to highlight the matching strings;\n\
1469 WHEN is `always', `never', or `auto'\n\
1470 -U, --binary do not strip CR characters at EOL (MSDOS)\n\
1471 -u, --unix-byte-offsets report offsets as if CRs were not there (MSDOS)\n\
1472 \n"));
1473 printf ("%s", _(after_options));
1474 printf (_("\
1475 With no FILE, or when FILE is -, read standard input. If less than two FILEs\n\
1476 are given, assume -h. Exit status is 0 if any line was selected, 1 otherwise;\n\
1477 if any error occurs and -q was not given, the exit status is 2.\n"));
1478 printf (_("\nReport bugs to: %s\n"), PACKAGE_BUGREPORT);
1479 printf (_("GNU Grep home page: <%s>\n"),
1480 "http://www.gnu.org/software/grep/");
1481 fputs (_("General help using GNU software: <http://www.gnu.org/gethelp/>\n"),
1482 stdout);
1485 exit (status);
1488 /* If M is NULL, initialize the matcher to the default. Otherwise set the
1489 matcher to M if available. Exit in case of conflicts or if M is not
1490 available. */
1491 static void
1492 setmatcher (char const *m)
1494 static char const *matcher;
1495 unsigned int i;
1497 if (!m)
1499 compile = matchers[0].compile;
1500 execute = matchers[0].execute;
1501 if (!matchers[1].name)
1502 matcher = matchers[0].name;
1505 else if (matcher)
1507 if (matcher && strcmp (matcher, m) == 0)
1510 else if (!matchers[1].name)
1511 error (EXIT_TROUBLE, 0, _("%s can only use the %s pattern syntax"),
1512 program_name, matcher);
1513 else
1514 error (EXIT_TROUBLE, 0, _("conflicting matchers specified"));
1517 else
1519 for (i = 0; matchers[i].name; i++)
1520 if (strcmp (m, matchers[i].name) == 0)
1522 compile = matchers[i].compile;
1523 execute = matchers[i].execute;
1524 matcher = m;
1525 return;
1528 error (EXIT_TROUBLE, 0, _("invalid matcher %s"), m);
1532 static void
1533 set_limits(void)
1535 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_STACK)
1536 struct rlimit rlim;
1538 /* I think every platform needs to do this, so that regex.c
1539 doesn't oveflow the stack. The default value of
1540 `re_max_failures' is too large for some platforms: it needs
1541 more than 3MB-large stack.
1543 The test for HAVE_SETRLIMIT should go into `configure'. */
1544 if (!getrlimit (RLIMIT_STACK, &rlim))
1546 long newlim;
1547 extern long int re_max_failures; /* from regex.c */
1549 /* Approximate the amount regex.c needs, plus some more. */
1550 newlim = re_max_failures * 2 * 20 * sizeof (char *);
1551 if (newlim > rlim.rlim_max)
1553 newlim = rlim.rlim_max;
1554 re_max_failures = newlim / (2 * 20 * sizeof (char *));
1556 if (rlim.rlim_cur < newlim)
1558 rlim.rlim_cur = newlim;
1559 setrlimit (RLIMIT_STACK, &rlim);
1562 #endif
1565 /* Find the white-space-separated options specified by OPTIONS, and
1566 using BUF to store copies of these options, set ARGV[0], ARGV[1],
1567 etc. to the option copies. Return the number N of options found.
1568 Do not set ARGV[N] to NULL. If ARGV is NULL, do not store ARGV[0]
1569 etc. Backslash can be used to escape whitespace (and backslashes). */
1570 static int
1571 prepend_args (char const *options, char *buf, char **argv)
1573 char const *o = options;
1574 char *b = buf;
1575 int n = 0;
1577 for (;;)
1579 while (ISSPACE ((unsigned char) *o))
1580 o++;
1581 if (!*o)
1582 return n;
1583 if (argv)
1584 argv[n] = b;
1585 n++;
1588 if ((*b++ = *o++) == '\\' && *o)
1589 b[-1] = *o++;
1590 while (*o && ! ISSPACE ((unsigned char) *o));
1592 *b++ = '\0';
1596 /* Prepend the whitespace-separated options in OPTIONS to the argument
1597 vector of a main program with argument count *PARGC and argument
1598 vector *PARGV. */
1599 static void
1600 prepend_default_options (char const *options, int *pargc, char ***pargv)
1602 if (options && *options)
1604 char *buf = xmalloc (strlen (options) + 1);
1605 int prepended = prepend_args (options, buf, (char **) NULL);
1606 int argc = *pargc;
1607 char * const *argv = *pargv;
1608 char **pp = xmalloc ((prepended + argc + 1) * sizeof *pp);
1609 *pargc = prepended + argc;
1610 *pargv = pp;
1611 *pp++ = *argv++;
1612 pp += prepend_args (options, buf, pp);
1613 while ((*pp++ = *argv++))
1614 continue;
1618 /* Get the next non-digit option from ARGC and ARGV.
1619 Return -1 if there are no more options.
1620 Process any digit options that were encountered on the way,
1621 and store the resulting integer into *DEFAULT_CONTEXT. */
1622 static int
1623 get_nondigit_option (int argc, char *const *argv, int *default_context)
1625 static int prev_digit_optind = -1;
1626 int opt, this_digit_optind, was_digit;
1627 char buf[sizeof (uintmax_t) * CHAR_BIT + 4];
1628 char *p = buf;
1630 was_digit = 0;
1631 this_digit_optind = optind;
1632 while (opt = getopt_long (argc, (char **) argv, short_options, long_options,
1633 NULL),
1634 '0' <= opt && opt <= '9')
1636 if (prev_digit_optind != this_digit_optind || !was_digit)
1638 /* Reset to start another context length argument. */
1639 p = buf;
1641 else
1643 /* Suppress trivial leading zeros, to avoid incorrect
1644 diagnostic on strings like 00000000000. */
1645 p -= buf[0] == '0';
1648 if (p == buf + sizeof buf - 4)
1650 /* Too many digits. Append "..." to make context_length_arg
1651 complain about "X...", where X contains the digits seen
1652 so far. */
1653 strcpy (p, "...");
1654 p += 3;
1655 break;
1657 *p++ = opt;
1659 was_digit = 1;
1660 prev_digit_optind = this_digit_optind;
1661 this_digit_optind = optind;
1663 if (p != buf)
1665 *p = '\0';
1666 context_length_arg (buf, default_context);
1669 return opt;
1672 /* Parse GREP_COLORS. The default would look like:
1673 GREP_COLORS='ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36'
1674 with boolean capabilities (ne and rv) unset (i.e., omitted).
1675 No character escaping is needed or supported. */
1676 static void
1677 parse_grep_colors (void)
1679 const char *p;
1680 char *q;
1681 char *name;
1682 char *val;
1684 p = getenv("GREP_COLORS"); /* Plural! */
1685 if (p == NULL || *p == '\0')
1686 return;
1688 /* Work off a writable copy. */
1689 q = xmalloc(strlen(p) + 1);
1690 if (q == NULL)
1691 return;
1692 strcpy(q, p);
1694 name = q;
1695 val = NULL;
1696 /* From now on, be well-formed or you're gone. */
1697 for (;;)
1698 if (*q == ':' || *q == '\0')
1700 char c = *q;
1701 struct color_cap *cap;
1703 *q++ = '\0'; /* Terminate name or val. */
1704 /* Empty name without val (empty cap)
1705 * won't match and will be ignored. */
1706 for (cap = color_dict; cap->name; cap++)
1707 if (strcmp(cap->name, name) == 0)
1708 break;
1709 /* If name unknown, go on for forward compatibility. */
1710 if (cap->name)
1712 if (cap->var)
1714 if (val)
1715 *(cap->var) = val;
1716 else
1717 error(0, 0, _("In GREP_COLORS=\"%s\", the \"%s\" capacity "
1718 "needs a value (\"=...\"); skipped."), p, name);
1720 else if (val)
1721 error(0, 0, _("In GREP_COLORS=\"%s\", the \"%s\" capacity "
1722 "is boolean and cannot take a value (\"=%s\"); "
1723 "skipped."), p, name, val);
1725 if (cap->fct)
1727 const char *err_str = cap->fct();
1729 if (err_str)
1730 error(0, 0, _("In GREP_COLORS=\"%s\", the \"%s\" capacity %s."),
1731 p, name, err_str);
1733 if (c == '\0')
1734 return;
1735 name = q;
1736 val = NULL;
1738 else if (*q == '=')
1740 if (q == name || val)
1741 goto ill_formed;
1742 *q++ = '\0'; /* Terminate name. */
1743 val = q; /* Can be the empty string. */
1745 else if (val == NULL)
1746 q++; /* Accumulate name. */
1747 else if (*q == ';' || (*q >= '0' && *q <= '9'))
1748 q++; /* Accumulate val. Protect the terminal from being sent crap. */
1749 else
1750 goto ill_formed;
1752 ill_formed:
1753 error(0, 0, _("Stopped processing of ill-formed GREP_COLORS=\"%s\" "
1754 "at remaining substring \"%s\"."), p, q);
1758 main (int argc, char **argv)
1760 char *keys;
1761 size_t keycc, oldcc, keyalloc;
1762 int with_filenames;
1763 int opt, cc, status;
1764 int default_context;
1765 FILE *fp;
1767 initialize_main (&argc, &argv);
1768 set_program_name (argv[0]);
1769 program_name = argv[0];
1771 keys = NULL;
1772 keycc = 0;
1773 with_filenames = 0;
1774 eolbyte = '\n';
1775 filename_mask = ~0;
1777 max_count = TYPE_MAXIMUM (off_t);
1779 /* The value -1 means to use DEFAULT_CONTEXT. */
1780 out_after = out_before = -1;
1781 /* Default before/after context: chaged by -C/-NUM options */
1782 default_context = 0;
1783 /* Changed by -o option */
1784 only_matching = 0;
1786 /* Internationalization. */
1787 #if defined(HAVE_SETLOCALE)
1788 setlocale (LC_ALL, "");
1789 #endif
1790 #if defined(ENABLE_NLS)
1791 bindtextdomain (PACKAGE, LOCALEDIR);
1792 textdomain (PACKAGE);
1793 #endif
1795 exit_failure = EXIT_TROUBLE;
1796 atexit (close_stdout);
1798 prepend_default_options (getenv ("GREP_OPTIONS"), &argc, &argv);
1799 setmatcher (NULL);
1801 while ((opt = get_nondigit_option (argc, argv, &default_context)) != -1)
1802 switch (opt)
1804 case 'A':
1805 context_length_arg (optarg, &out_after);
1806 break;
1808 case 'B':
1809 context_length_arg (optarg, &out_before);
1810 break;
1812 case 'C':
1813 /* Set output match context, but let any explicit leading or
1814 trailing amount specified with -A or -B stand. */
1815 context_length_arg (optarg, &default_context);
1816 break;
1818 case 'D':
1819 if (strcmp (optarg, "read") == 0)
1820 devices = READ_DEVICES;
1821 else if (strcmp (optarg, "skip") == 0)
1822 devices = SKIP_DEVICES;
1823 else
1824 error (EXIT_TROUBLE, 0, _("unknown devices method"));
1825 break;
1827 case 'E':
1828 setmatcher ("egrep");
1829 break;
1831 case 'F':
1832 setmatcher ("fgrep");
1833 break;
1835 case 'P':
1836 setmatcher ("perl");
1837 break;
1839 case 'G':
1840 setmatcher ("grep");
1841 break;
1843 case 'X': /* undocumented on purpose */
1844 setmatcher (optarg);
1845 break;
1847 case 'H':
1848 with_filenames = 1;
1849 no_filenames = 0;
1850 break;
1852 case 'I':
1853 binary_files = WITHOUT_MATCH_BINARY_FILES;
1854 break;
1856 case 'T':
1857 align_tabs = 1;
1858 break;
1860 case 'U':
1861 #if defined(HAVE_DOS_FILE_CONTENTS)
1862 dos_use_file_type = DOS_BINARY;
1863 #endif
1864 break;
1866 case 'u':
1867 #if defined(HAVE_DOS_FILE_CONTENTS)
1868 dos_report_unix_offset = 1;
1869 #endif
1870 break;
1872 case 'V':
1873 show_version = 1;
1874 break;
1876 case 'a':
1877 binary_files = TEXT_BINARY_FILES;
1878 break;
1880 case 'b':
1881 out_byte = 1;
1882 break;
1884 case 'c':
1885 count_matches = 1;
1886 break;
1888 case 'd':
1889 if (strcmp (optarg, "read") == 0)
1890 directories = READ_DIRECTORIES;
1891 else if (strcmp (optarg, "skip") == 0)
1892 directories = SKIP_DIRECTORIES;
1893 else if (strcmp (optarg, "recurse") == 0)
1894 directories = RECURSE_DIRECTORIES;
1895 else
1896 error (EXIT_TROUBLE, 0, _("unknown directories method"));
1897 break;
1899 case 'e':
1900 cc = strlen (optarg);
1901 keys = xrealloc (keys, keycc + cc + 1);
1902 strcpy (&keys[keycc], optarg);
1903 keycc += cc;
1904 keys[keycc++] = '\n';
1905 break;
1907 case 'f':
1908 fp = strcmp (optarg, "-") != 0 ? fopen (optarg, "r") : stdin;
1909 if (!fp)
1910 error (EXIT_TROUBLE, errno, "%s", optarg);
1911 for (keyalloc = 1; keyalloc <= keycc + 1; keyalloc *= 2)
1913 keys = xrealloc (keys, keyalloc);
1914 oldcc = keycc;
1915 while (!feof (fp)
1916 && (cc = fread (keys + keycc, 1, keyalloc - 1 - keycc, fp)) > 0)
1918 keycc += cc;
1919 if (keycc == keyalloc - 1)
1920 keys = xrealloc (keys, keyalloc *= 2);
1922 if (fp != stdin)
1923 fclose(fp);
1924 /* Append final newline if file ended in non-newline. */
1925 if (oldcc != keycc && keys[keycc - 1] != '\n')
1926 keys[keycc++] = '\n';
1927 break;
1929 case 'h':
1930 with_filenames = 0;
1931 no_filenames = 1;
1932 break;
1934 case 'i':
1935 case 'y': /* For old-timers . . . */
1936 match_icase = 1;
1937 break;
1939 case 'L':
1940 /* Like -l, except list files that don't contain matches.
1941 Inspired by the same option in Hume's gre. */
1942 list_files = -1;
1943 break;
1945 case 'l':
1946 list_files = 1;
1947 break;
1949 case 'm':
1951 uintmax_t value;
1952 switch (xstrtoumax (optarg, 0, 10, &value, ""))
1954 case LONGINT_OK:
1955 max_count = value;
1956 if (0 <= max_count && max_count == value)
1957 break;
1958 /* Fall through. */
1959 case LONGINT_OVERFLOW:
1960 max_count = TYPE_MAXIMUM (off_t);
1961 break;
1963 default:
1964 error (EXIT_TROUBLE, 0, _("invalid max count"));
1967 break;
1969 case 'n':
1970 out_line = 1;
1971 break;
1973 case 'o':
1974 only_matching = 1;
1975 break;
1977 case 'q':
1978 exit_on_match = 1;
1979 exit_failure = 0;
1980 break;
1982 case 'R':
1983 case 'r':
1984 directories = RECURSE_DIRECTORIES;
1985 break;
1987 case 's':
1988 suppress_errors = 1;
1989 break;
1991 case 'v':
1992 out_invert = 1;
1993 break;
1995 case 'w':
1996 match_words = 1;
1997 break;
1999 case 'x':
2000 match_lines = 1;
2001 break;
2003 case 'Z':
2004 filename_mask = 0;
2005 break;
2007 case 'z':
2008 eolbyte = '\0';
2009 break;
2011 case BINARY_FILES_OPTION:
2012 if (strcmp (optarg, "binary") == 0)
2013 binary_files = BINARY_BINARY_FILES;
2014 else if (strcmp (optarg, "text") == 0)
2015 binary_files = TEXT_BINARY_FILES;
2016 else if (strcmp (optarg, "without-match") == 0)
2017 binary_files = WITHOUT_MATCH_BINARY_FILES;
2018 else
2019 error (EXIT_TROUBLE, 0, _("unknown binary-files type"));
2020 break;
2022 case COLOR_OPTION:
2023 if(optarg) {
2024 if(!strcasecmp(optarg, "always") || !strcasecmp(optarg, "yes") ||
2025 !strcasecmp(optarg, "force"))
2026 color_option = 1;
2027 else if(!strcasecmp(optarg, "never") || !strcasecmp(optarg, "no") ||
2028 !strcasecmp(optarg, "none"))
2029 color_option = 0;
2030 else if(!strcasecmp(optarg, "auto") || !strcasecmp(optarg, "tty") ||
2031 !strcasecmp(optarg, "if-tty"))
2032 color_option = 2;
2033 else
2034 show_help = 1;
2035 } else
2036 color_option = 2;
2037 if(color_option == 2) {
2038 if(isatty(STDOUT_FILENO) && getenv("TERM") &&
2039 strcmp(getenv("TERM"), "dumb"))
2040 color_option = 1;
2041 else
2042 color_option = 0;
2044 break;
2046 case EXCLUDE_OPTION:
2047 if (!excluded_patterns)
2048 excluded_patterns = new_exclude ();
2049 add_exclude (excluded_patterns, optarg, EXCLUDE_WILDCARDS);
2050 break;
2051 case EXCLUDE_FROM_OPTION:
2052 if (!excluded_patterns)
2053 excluded_patterns = new_exclude ();
2054 if (add_exclude_file (add_exclude, excluded_patterns, optarg,
2055 EXCLUDE_WILDCARDS, '\n') != 0)
2057 error (EXIT_TROUBLE, errno, "%s", optarg);
2059 break;
2061 case EXCLUDE_DIRECTORY_OPTION:
2062 if (!excluded_directory_patterns)
2063 excluded_directory_patterns = new_exclude ();
2064 add_exclude (excluded_directory_patterns, optarg, EXCLUDE_WILDCARDS);
2065 break;
2067 case INCLUDE_OPTION:
2068 if (!included_patterns)
2069 included_patterns = new_exclude ();
2070 add_exclude (included_patterns, optarg,
2071 EXCLUDE_WILDCARDS | EXCLUDE_INCLUDE);
2072 break;
2074 case GROUP_SEPARATOR_OPTION:
2075 group_separator = optarg;
2076 break;
2078 case LINE_BUFFERED_OPTION:
2079 line_buffered = 1;
2080 break;
2082 case LABEL_OPTION:
2083 label = optarg;
2084 break;
2086 case 0:
2087 /* long options */
2088 break;
2090 default:
2091 usage (EXIT_TROUBLE);
2092 break;
2096 /* POSIX.2 says that -q overrides -l, which in turn overrides the
2097 other output options. */
2098 if (exit_on_match)
2099 list_files = 0;
2100 if (exit_on_match | list_files)
2102 count_matches = 0;
2103 done_on_match = 1;
2105 out_quiet = count_matches | done_on_match;
2107 if (out_after < 0)
2108 out_after = default_context;
2109 if (out_before < 0)
2110 out_before = default_context;
2112 if (color_option)
2114 /* Legacy. */
2115 char *userval = getenv ("GREP_COLOR");
2116 if (userval != NULL && *userval != '\0')
2117 selected_match_color = context_match_color = userval;
2119 /* New GREP_COLORS has priority. */
2120 parse_grep_colors();
2123 if (show_version)
2125 printf ("%s\n\n", PACKAGE_STRING);
2126 printf (_("\
2127 Copyright (C) %s Free Software Foundation, Inc.\n\
2128 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n\
2129 This is free software: you are free to change and redistribute it.\n\
2130 There is NO WARRANTY, to the extent permitted by law.\n"),
2131 "2009");
2132 printf ("\n");
2133 exit (EXIT_SUCCESS);
2136 if (show_help)
2137 usage (EXIT_SUCCESS);
2139 if (keys)
2141 if (keycc == 0)
2143 /* No keys were specified (e.g. -f /dev/null). Match nothing. */
2144 out_invert ^= 1;
2145 match_lines = match_words = 0;
2147 else
2148 /* Strip trailing newline. */
2149 --keycc;
2151 else
2152 if (optind < argc)
2154 /* A copy must be made in case of an xrealloc() or free() later. */
2155 keycc = strlen(argv[optind]);
2156 keys = xmalloc(keycc + 1);
2157 strcpy(keys, argv[optind++]);
2159 else
2160 usage (EXIT_TROUBLE);
2162 set_limits();
2163 compile(keys, keycc);
2164 free (keys);
2166 if ((argc - optind > 1 && !no_filenames) || with_filenames)
2167 out_file = 1;
2169 #ifdef SET_BINARY
2170 /* Output is set to binary mode because we shouldn't convert
2171 NL to CR-LF pairs, especially when grepping binary files. */
2172 if (!isatty (1))
2173 SET_BINARY (1);
2174 #endif
2176 if (max_count == 0)
2177 exit (EXIT_FAILURE);
2179 if (optind < argc)
2181 status = 1;
2184 char *file = argv[optind];
2185 if ((included_patterns || excluded_patterns)
2186 && !isdir (file))
2188 if (included_patterns &&
2189 ! excluded_file_name (included_patterns, file))
2190 continue;
2191 if (excluded_patterns &&
2192 excluded_file_name (excluded_patterns, file))
2193 continue;
2195 status &= grepfile (strcmp (file, "-") == 0 ? (char *) NULL : file,
2196 &stats_base);
2198 while ( ++optind < argc);
2200 else
2201 status = grepfile ((char *) NULL, &stats_base);
2203 /* We register via atexit() to test stdout. */
2204 exit (errseen ? EXIT_TROUBLE : status);
2206 /* vim:set shiftwidth=2: */