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)
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
19 /* Written July 1992 by Mike Haertel. */
22 #include <sys/types.h>
24 #if defined(HAVE_SETRLIMIT)
25 # include <sys/time.h>
26 # include <sys/resource.h>
28 #include "mbsupport.h"
50 #define SEP_CHAR_SELECTED ':'
51 #define SEP_CHAR_REJECTED '-'
52 #define SEP_STR_GROUP "--"
56 struct stats
const *parent
;
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 */
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
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))
215 const char *(*fct
)(void);
219 color_cap_mt_fct(void)
221 /* Our caller just set selected_match_color. */
222 context_match_color
= selected_match_color
;
228 color_cap_rv_fct(void)
230 /* By this point, it was 1 (or already -1). */
231 color_option
= -1; /* That's still != 0. */
237 color_cap_ne_fct(void)
239 sgr_start
= "\33[%sm";
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_* */
262 static struct exclude
*excluded_patterns
;
263 static struct exclude
*included_patterns
;
264 static struct exclude
*excluded_directory_patterns
;
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. */
272 BINARY_FILES_OPTION
= CHAR_MAX
+ 1,
277 LINE_BUFFERED_OPTION
,
279 EXCLUDE_DIRECTORY_OPTION
,
280 GROUP_SEPARATOR_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'},
342 /* Define flags declared in grep.h. */
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
;
353 /* How to handle directories. */
359 } directories
= READ_DIRECTORIES
;
361 /* How to handle 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);
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. */
379 suppressible_error (char const *mesg
, int errnum
)
381 if (! suppress_errors
)
382 error (0, errnum
, "%s", mesg
);
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
390 context_length_arg (char const *str
, int *out
)
393 if (! (xstrtoumax (str
, 0, 10, &value
, "") == LONGINT_OK
394 && 0 <= (*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 \
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. */
429 reset (int fd
, char const *file
, struct stats
*stats
)
433 pagesize
= getpagesize ();
434 if (pagesize
== 0 || 2 * pagesize
+ 1 <= pagesize
)
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
;
444 if (S_ISREG (stats
->stat
.st_mode
))
450 bufoffset
= lseek (fd
, 0, SEEK_CUR
);
453 error (0, errno
, _("lseek failed"));
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. */
466 fillbuf (size_t save
, struct stats
const *stats
)
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
)
480 bufbeg
= buflim
- save
;
484 size_t minsize
= save
+ pagesize
;
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)
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
)
526 readsize
= buffer
+ bufalloc
- readbuf
;
527 readsize
-= readsize
% pagesize
;
532 while ((bytesread
= read (bufdesc
, readbuf
, readsize
)) < 0
538 fillsize
= bytesread
;
541 bufoffset
+= fillsize
;
542 #if defined(HAVE_DOS_FILE_CONTENTS)
544 fillsize
= undossify_input (readbuf
, fillsize
);
546 buflim
= readbuf
+ fillsize
;
550 /* Flags controlling the style of output. */
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)
593 /* Add two numbers that count input bytes or lines, and report an
594 error if the addition overflows. */
596 add_count (uintmax_t a
, uintmax_t b
)
598 uintmax_t sum
= a
+ b
;
600 error (EXIT_TROUBLE
, 0, _("input is too large to count"));
605 nlscan (char const *lim
)
609 for (beg
= lastnl
; beg
< lim
; beg
++)
611 beg
= memchr (beg
, eolbyte
, lim
- beg
);
616 totalnl
= add_count (totalnl
, newlines
);
620 /* Print the current filename. */
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. */
633 PR_SGR_START_IF(sep_color
);
635 PR_SGR_END_IF(sep_color
);
638 /* Print a line number or a byte offset. */
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;
653 while ((pos
/= 10) != 0);
655 /* Do this to maximize the probability of alignment across lines. */
657 while (--min_width
>= 0)
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). */
667 print_line_head (char const *beg
, char const *lim
, int sep
)
685 totalnl
= add_count (totalnl
, 1);
690 print_offset (totalnl
, 4, line_num_color
);
696 uintmax_t pos
= add_count (totalcc
, beg
- bufbeg
);
697 #if defined(HAVE_DOS_FILE_CONTENTS)
698 pos
= dossified_pos (pos
);
702 print_offset (pos
, 6, byte_num_color
);
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. */
713 fputs("\t\b", stdout
);
720 print_line_middle (const char *beg
, const char *lim
,
721 const char *line_color
, const char *match_color
)
725 const char *cur
= beg
;
726 const char *mid
= NULL
;
730 /* XXX - should not be needed anymore now that we use RE_ICASE.
731 Revisit after 2.6.x stabilizes. */
740 ibeg
= buf
= xmalloc(i
);
742 buf
[i
] = tolower((unsigned char) beg
[i
]);
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. */
760 /* Avoid hanging on grep --color "" foo */
763 /* Make minimal progress; there may be further non-empty matches. */
764 /* XXX - Could really advance by one whole multi-octet character. */
771 /* This function is called on a matching line only,
772 but is it selected or rejected/context? */
774 print_line_head(b
, lim
, out_invert
? SEP_CHAR_REJECTED
775 : SEP_CHAR_SELECTED
);
778 PR_SGR_START(line_color
);
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
);
793 cur
= b
+ match_size
;
796 free (buf
); /* XXX */
807 print_line_tail (const char *beg
, const char *lim
, const char *line_color
)
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
;
818 PR_SGR_START(line_color
);
819 fwrite(beg
, 1, tail_size
, stdout
);
821 PR_SGR_END(line_color
);
828 prline (char const *beg
, char const *lim
, int sep
)
831 const char *line_color
;
832 const char *match_color
;
835 print_line_head(beg
, lim
, sep
);
837 matching
= (sep
== SEP_CHAR_SELECTED
) ^ !!out_invert
;
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
;
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
);
866 error (0, errno
, _("writing output"));
874 /* Print pending lines of trailing context prior to LIM. Trailing context ends
875 at the next matching line when OUTLEFT is 0. */
877 prpending (char const *lim
)
881 while (pending
> 0 && lastout
< lim
)
883 char const *nl
= memchr (lastout
, eolbyte
, lim
- lastout
);
887 || ((execute(lastout
, nl
+ 1 - lastout
,
888 &match_size
, NULL
) == (size_t) -1)
890 prline (lastout
, nl
+ 1, SEP_CHAR_REJECTED
);
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. */
899 prtext (char const *beg
, char const *lim
, int *nlinesp
)
901 static int used
; /* avoid printing SEP_STR_GROUP before any output */
906 if (!out_quiet
&& pending
> 0)
913 /* Deal with leading context crap. */
915 bp
= lastout
? lastout
: bufbeg
;
916 for (i
= 0; i
< out_before
; ++i
)
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
);
934 char const *nl
= memchr (p
, eol
, beg
- p
);
936 prline (p
, nl
, SEP_CHAR_REJECTED
);
943 /* Caller wants a line count. */
944 for (n
= 0; p
< lim
&& n
< outleft
; n
++)
946 char const *nl
= memchr (p
, eol
, lim
- p
);
949 prline (p
, nl
, SEP_CHAR_SELECTED
);
954 /* relying on it that this function is never called when outleft = 0. */
955 after_last_match
= bufoffset
- (buflim
- p
);
959 prline (beg
, lim
, SEP_CHAR_SELECTED
);
961 pending
= out_quiet
? 0 : out_after
;
966 do_execute (char const *buf
, size_t size
, size_t *match_size
, char const *start_ptr
)
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
;
993 line_next
= line_end
+ 1;
995 if (start_ptr
&& start_ptr
>= line_end
)
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
;
1006 /* Scan the specified portion of the buffer, matching lines (or
1007 between matching lines if OUT_INVERT is true). Return a count of
1010 grepbuf (char const *beg
, char const *lim
)
1014 size_t match_offset
;
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. */
1029 prtext (b
, endp
, (int *) 0);
1032 if (!outleft
|| done_on_match
)
1035 exit (EXIT_SUCCESS
);
1036 after_last_match
= bufoffset
- (buflim
- endp
);
1050 if (out_invert
&& p
< lim
)
1052 prtext (p
, lim
, &n
);
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. */
1063 grep (int fd
, char const *file
, struct stats
*stats
)
1067 size_t residue
, save
;
1073 if (!reset (fd
, file
, stats
))
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;
1089 outleft
= max_count
;
1090 after_last_match
= 0;
1097 if (! fillbuf (save
, stats
))
1099 if (! is_EISDIR (errno
, file
))
1100 suppressible_error (filename
, errno
);
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
)
1109 done_on_match
+= not_text
;
1110 out_quiet
+= not_text
;
1118 beg
= bufbeg
+ save
;
1120 /* no more data to scan (eof) except for maybe a residue -> 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). */
1128 for (lim
= buflim
; lim
[-1] != eol
; lim
--)
1132 lim
= beg
- residue
;
1134 residue
= buflim
- lim
;
1139 nlines
+= grepbuf (beg
, lim
);
1142 if((!outleft
&& !pending
) || (nlines
&& done_on_match
&& !out_invert
))
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. */
1151 while (i
< out_before
&& beg
> bufbeg
&& beg
!= lastout
)
1156 while (beg
[-1] != eol
);
1159 /* detect if leading context is discontinuous from last printed line. */
1163 /* Handle some details and read more data to scan. */
1164 save
= residue
+ lim
- beg
;
1166 totalcc
= add_count (totalcc
, buflim
- bufbeg
- save
);
1169 if (! fillbuf (save
, stats
))
1171 if (! is_EISDIR (errno
, file
))
1172 suppressible_error (filename
, errno
);
1180 nlines
+= grepbuf (bufbeg
+ save
- residue
, buflim
);
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
);
1194 grepfile (char const *file
, struct stats
*stats
)
1203 filename
= label
? label
: _("(standard input)");
1207 if (stat (file
, &stats
->stat
) != 0)
1209 suppressible_error (file
, errno
);
1212 if (directories
== SKIP_DIRECTORIES
&& S_ISDIR (stats
->stat
.st_mode
))
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
)))
1219 while ((desc
= open (file
, O_RDONLY
)) < 0 && errno
== EINTR
)
1226 if (is_EISDIR (e
, file
) && directories
== RECURSE_DIRECTORIES
)
1228 if (stat (file
, &stats
->stat
) != 0)
1230 error (0, errno
, "%s", file
);
1234 return grepdir (file
, stats
);
1237 if (!suppress_errors
)
1239 if (directories
== SKIP_DIRECTORIES
)
1247 /* When skipping directories, don't worry about
1248 directories that can't be opened. */
1255 suppressible_error (file
, e
);
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". */
1269 count
= grep (desc
, file
, stats
);
1280 print_sep(SEP_CHAR_SELECTED
);
1284 printf ("%d\n", count
);
1288 if (list_files
== 1 - 2 * status
)
1291 fputc('\n' & filename_mask
, stdout
);
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
);
1303 while (close (desc
) != 0)
1306 error (0, errno
, "%s", file
);
1315 grepdir (char const *dir
, struct stats
const *stats
)
1317 struct stats
const *ancestor
;
1320 if ( excluded_directory_patterns
&&
1321 excluded_file_name (excluded_directory_patterns
, dir
) ) {
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"));
1339 name_space
= savedir (dir
, stats
->stat
.st_size
, included_patterns
,
1340 excluded_patterns
, excluded_directory_patterns
);
1345 suppressible_error (dir
, errno
);
1351 size_t dirlen
= strlen (dir
);
1352 int needs_slash
= ! (dirlen
== FILE_SYSTEM_PREFIX_LEN (dir
)
1353 || ISSLASH (dir
[dirlen
- 1]));
1355 char const *namep
= name_space
;
1357 child
.parent
= stats
;
1358 out_file
+= !no_filenames
;
1361 size_t namelen
= strlen (namep
);
1362 file
= xrealloc (file
, dirlen
+ 1 + namelen
+ 1);
1365 strcpy (file
+ dirlen
+ needs_slash
, namep
);
1366 namep
+= namelen
+ 1;
1367 status
&= grepfile (file
, &child
);
1369 out_file
-= !no_filenames
;
1377 static void usage (int status
) __attribute__ ((noreturn
));
1383 fprintf (stderr
, _("Usage: %s [OPTION]... PATTERN [FILE]...\n"),
1385 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
1390 printf (_("Usage: %s [OPTION]... PATTERN [FILE]...\n"), program_name
);
1392 Search for PATTERN in each FILE or standard input.\n"));
1393 printf ("%s", gettext (before_options
));
1395 Example: %s -i 'hello world' menu.h main.c\n\
1397 Regexp selection and interpretation:\n"), program_name
);
1398 if (matchers
[1].name
)
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. */
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"));
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"));
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\
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\
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\
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\
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"));
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\
1466 -NUM same as --context=NUM\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\
1473 printf ("%s", _(after_options
));
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"),
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
1492 setmatcher (char const *m
)
1494 static char const *matcher
;
1499 compile
= matchers
[0].compile
;
1500 execute
= matchers
[0].execute
;
1501 if (!matchers
[1].name
)
1502 matcher
= matchers
[0].name
;
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
);
1514 error (EXIT_TROUBLE
, 0, _("conflicting matchers specified"));
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
;
1528 error (EXIT_TROUBLE
, 0, _("invalid matcher %s"), m
);
1535 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_STACK)
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
))
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
);
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). */
1571 prepend_args (char const *options
, char *buf
, char **argv
)
1573 char const *o
= options
;
1579 while (ISSPACE ((unsigned char) *o
))
1588 if ((*b
++ = *o
++) == '\\' && *o
)
1590 while (*o
&& ! ISSPACE ((unsigned char) *o
));
1596 /* Prepend the whitespace-separated options in OPTIONS to the argument
1597 vector of a main program with argument count *PARGC and argument
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
);
1607 char * const *argv
= *pargv
;
1608 char **pp
= xmalloc ((prepended
+ argc
+ 1) * sizeof *pp
);
1609 *pargc
= prepended
+ argc
;
1612 pp
+= prepend_args (options
, buf
, pp
);
1613 while ((*pp
++ = *argv
++))
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. */
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];
1631 this_digit_optind
= optind
;
1632 while (opt
= getopt_long (argc
, (char **) argv
, short_options
, long_options
,
1634 '0' <= opt
&& opt
<= '9')
1636 if (prev_digit_optind
!= this_digit_optind
|| !was_digit
)
1638 /* Reset to start another context length argument. */
1643 /* Suppress trivial leading zeros, to avoid incorrect
1644 diagnostic on strings like 00000000000. */
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
1660 prev_digit_optind
= this_digit_optind
;
1661 this_digit_optind
= optind
;
1666 context_length_arg (buf
, default_context
);
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. */
1677 parse_grep_colors (void)
1684 p
= getenv("GREP_COLORS"); /* Plural! */
1685 if (p
== NULL
|| *p
== '\0')
1688 /* Work off a writable copy. */
1689 q
= xmalloc(strlen(p
) + 1);
1696 /* From now on, be well-formed or you're gone. */
1698 if (*q
== ':' || *q
== '\0')
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)
1709 /* If name unknown, go on for forward compatibility. */
1717 error(0, 0, _("In GREP_COLORS=\"%s\", the \"%s\" capacity "
1718 "needs a value (\"=...\"); skipped."), p
, name
);
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
);
1727 const char *err_str
= cap
->fct();
1730 error(0, 0, _("In GREP_COLORS=\"%s\", the \"%s\" capacity %s."),
1740 if (q
== name
|| val
)
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. */
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
)
1761 size_t keycc
, oldcc
, keyalloc
;
1763 int opt
, cc
, status
;
1764 int default_context
;
1767 initialize_main (&argc
, &argv
);
1768 set_program_name (argv
[0]);
1769 program_name
= argv
[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 */
1786 /* Internationalization. */
1787 #if defined(HAVE_SETLOCALE)
1788 setlocale (LC_ALL
, "");
1790 #if defined(ENABLE_NLS)
1791 bindtextdomain (PACKAGE
, LOCALEDIR
);
1792 textdomain (PACKAGE
);
1795 exit_failure
= EXIT_TROUBLE
;
1796 atexit (close_stdout
);
1798 prepend_default_options (getenv ("GREP_OPTIONS"), &argc
, &argv
);
1801 while ((opt
= get_nondigit_option (argc
, argv
, &default_context
)) != -1)
1805 context_length_arg (optarg
, &out_after
);
1809 context_length_arg (optarg
, &out_before
);
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
);
1819 if (strcmp (optarg
, "read") == 0)
1820 devices
= READ_DEVICES
;
1821 else if (strcmp (optarg
, "skip") == 0)
1822 devices
= SKIP_DEVICES
;
1824 error (EXIT_TROUBLE
, 0, _("unknown devices method"));
1828 setmatcher ("egrep");
1832 setmatcher ("fgrep");
1836 setmatcher ("perl");
1840 setmatcher ("grep");
1843 case 'X': /* undocumented on purpose */
1844 setmatcher (optarg
);
1853 binary_files
= WITHOUT_MATCH_BINARY_FILES
;
1861 #if defined(HAVE_DOS_FILE_CONTENTS)
1862 dos_use_file_type
= DOS_BINARY
;
1867 #if defined(HAVE_DOS_FILE_CONTENTS)
1868 dos_report_unix_offset
= 1;
1877 binary_files
= TEXT_BINARY_FILES
;
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
;
1896 error (EXIT_TROUBLE
, 0, _("unknown directories method"));
1900 cc
= strlen (optarg
);
1901 keys
= xrealloc (keys
, keycc
+ cc
+ 1);
1902 strcpy (&keys
[keycc
], optarg
);
1904 keys
[keycc
++] = '\n';
1908 fp
= strcmp (optarg
, "-") != 0 ? fopen (optarg
, "r") : stdin
;
1910 error (EXIT_TROUBLE
, errno
, "%s", optarg
);
1911 for (keyalloc
= 1; keyalloc
<= keycc
+ 1; keyalloc
*= 2)
1913 keys
= xrealloc (keys
, keyalloc
);
1916 && (cc
= fread (keys
+ keycc
, 1, keyalloc
- 1 - keycc
, fp
)) > 0)
1919 if (keycc
== keyalloc
- 1)
1920 keys
= xrealloc (keys
, keyalloc
*= 2);
1924 /* Append final newline if file ended in non-newline. */
1925 if (oldcc
!= keycc
&& keys
[keycc
- 1] != '\n')
1926 keys
[keycc
++] = '\n';
1935 case 'y': /* For old-timers . . . */
1940 /* Like -l, except list files that don't contain matches.
1941 Inspired by the same option in Hume's gre. */
1952 switch (xstrtoumax (optarg
, 0, 10, &value
, ""))
1956 if (0 <= max_count
&& max_count
== value
)
1959 case LONGINT_OVERFLOW
:
1960 max_count
= TYPE_MAXIMUM (off_t
);
1964 error (EXIT_TROUBLE
, 0, _("invalid max count"));
1984 directories
= RECURSE_DIRECTORIES
;
1988 suppress_errors
= 1;
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
;
2019 error (EXIT_TROUBLE
, 0, _("unknown binary-files type"));
2024 if(!strcasecmp(optarg
, "always") || !strcasecmp(optarg
, "yes") ||
2025 !strcasecmp(optarg
, "force"))
2027 else if(!strcasecmp(optarg
, "never") || !strcasecmp(optarg
, "no") ||
2028 !strcasecmp(optarg
, "none"))
2030 else if(!strcasecmp(optarg
, "auto") || !strcasecmp(optarg
, "tty") ||
2031 !strcasecmp(optarg
, "if-tty"))
2037 if(color_option
== 2) {
2038 if(isatty(STDOUT_FILENO
) && getenv("TERM") &&
2039 strcmp(getenv("TERM"), "dumb"))
2046 case EXCLUDE_OPTION
:
2047 if (!excluded_patterns
)
2048 excluded_patterns
= new_exclude ();
2049 add_exclude (excluded_patterns
, optarg
, EXCLUDE_WILDCARDS
);
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
);
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
);
2067 case INCLUDE_OPTION
:
2068 if (!included_patterns
)
2069 included_patterns
= new_exclude ();
2070 add_exclude (included_patterns
, optarg
,
2071 EXCLUDE_WILDCARDS
| EXCLUDE_INCLUDE
);
2074 case GROUP_SEPARATOR_OPTION
:
2075 group_separator
= optarg
;
2078 case LINE_BUFFERED_OPTION
:
2091 usage (EXIT_TROUBLE
);
2096 /* POSIX.2 says that -q overrides -l, which in turn overrides the
2097 other output options. */
2100 if (exit_on_match
| list_files
)
2105 out_quiet
= count_matches
| done_on_match
;
2108 out_after
= default_context
;
2110 out_before
= default_context
;
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();
2125 printf ("%s\n\n", PACKAGE_STRING
);
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"),
2133 exit (EXIT_SUCCESS
);
2137 usage (EXIT_SUCCESS
);
2143 /* No keys were specified (e.g. -f /dev/null). Match nothing. */
2145 match_lines
= match_words
= 0;
2148 /* Strip trailing newline. */
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
++]);
2160 usage (EXIT_TROUBLE
);
2163 compile(keys
, keycc
);
2166 if ((argc
- optind
> 1 && !no_filenames
) || with_filenames
)
2170 /* Output is set to binary mode because we shouldn't convert
2171 NL to CR-LF pairs, especially when grepping binary files. */
2177 exit (EXIT_FAILURE
);
2184 char *file
= argv
[optind
];
2185 if ((included_patterns
|| excluded_patterns
)
2188 if (included_patterns
&&
2189 ! excluded_file_name (included_patterns
, file
))
2191 if (excluded_patterns
&&
2192 excluded_file_name (excluded_patterns
, file
))
2195 status
&= grepfile (strcmp (file
, "-") == 0 ? (char *) NULL
: file
,
2198 while ( ++optind
< argc
);
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: */