Upgrade grep from 2.14 => 2.20 on the vendor branch
[dragonfly.git] / contrib / grep / src / grep.c
1 /* grep.c - main driver file for grep.
2    Copyright (C) 1992, 1997-2002, 2004-2014 Free Software Foundation, Inc.
3
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.
8
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.
13
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.  */
18
19 /* Written July 1992 by Mike Haertel.  */
20
21 #include <config.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <wchar.h>
25 #include <wctype.h>
26 #include <fcntl.h>
27 #include <inttypes.h>
28 #include <stdio.h>
29 #include "system.h"
30
31 #include "argmatch.h"
32 #include "c-ctype.h"
33 #include "closeout.h"
34 #include "colorize.h"
35 #include "error.h"
36 #include "exclude.h"
37 #include "exitfail.h"
38 #include "fcntl-safer.h"
39 #include "fts_.h"
40 #include "getopt.h"
41 #include "grep.h"
42 #include "intprops.h"
43 #include "progname.h"
44 #include "propername.h"
45 #include "quote.h"
46 #include "safe-read.h"
47 #include "search.h"
48 #include "version-etc.h"
49 #include "xalloc.h"
50 #include "xstrtol.h"
51
52 #define SEP_CHAR_SELECTED ':'
53 #define SEP_CHAR_REJECTED '-'
54 #define SEP_STR_GROUP    "--"
55
56 #define AUTHORS \
57   proper_name ("Mike Haertel"), \
58   _("others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>")
59
60 /* When stdout is connected to a regular file, save its stat
61    information here, so that we can automatically skip it, thus
62    avoiding a potential (racy) infinite loop.  */
63 static struct stat out_stat;
64
65 /* if non-zero, display usage information and exit */
66 static int show_help;
67
68 /* If non-zero, print the version on standard output and exit.  */
69 static int show_version;
70
71 /* If nonzero, suppress diagnostics for nonexistent or unreadable files.  */
72 static int suppress_errors;
73
74 /* If nonzero, use color markers.  */
75 static int color_option;
76
77 /* If nonzero, show only the part of a line matching the expression. */
78 static int only_matching;
79
80 /* If nonzero, make sure first content char in a line is on a tab stop. */
81 static int align_tabs;
82
83 /* The group separator used when context is requested. */
84 static const char *group_separator = SEP_STR_GROUP;
85
86 /* The context and logic for choosing default --color screen attributes
87    (foreground and background colors, etc.) are the following.
88       -- There are eight basic colors available, each with its own
89          nominal luminosity to the human eye and foreground/background
90          codes (black [0 %, 30/40], blue [11 %, 34/44], red [30 %, 31/41],
91          magenta [41 %, 35/45], green [59 %, 32/42], cyan [70 %, 36/46],
92          yellow [89 %, 33/43], and white [100 %, 37/47]).
93       -- Sometimes, white as a background is actually implemented using
94          a shade of light gray, so that a foreground white can be visible
95          on top of it (but most often not).
96       -- Sometimes, black as a foreground is actually implemented using
97          a shade of dark gray, so that it can be visible on top of a
98          background black (but most often not).
99       -- Sometimes, more colors are available, as extensions.
100       -- Other attributes can be selected/deselected (bold [1/22],
101          underline [4/24], standout/inverse [7/27], blink [5/25], and
102          invisible/hidden [8/28]).  They are sometimes implemented by
103          using colors instead of what their names imply; e.g., bold is
104          often achieved by using brighter colors.  In practice, only bold
105          is really available to us, underline sometimes being mapped by
106          the terminal to some strange color choice, and standout best
107          being left for use by downstream programs such as less(1).
108       -- We cannot assume that any of the extensions or special features
109          are available for the purpose of choosing defaults for everyone.
110       -- The most prevalent default terminal backgrounds are pure black
111          and pure white, and are not necessarily the same shades of
112          those as if they were selected explicitly with SGR sequences.
113          Some terminals use dark or light pictures as default background,
114          but those are covered over by an explicit selection of background
115          color with an SGR sequence; their users will appreciate their
116          background pictures not be covered like this, if possible.
117       -- Some uses of colors attributes is to make some output items
118          more understated (e.g., context lines); this cannot be achieved
119          by changing the background color.
120       -- For these reasons, the grep color defaults should strive not
121          to change the background color from its default, unless it's
122          for a short item that should be highlighted, not understated.
123       -- The grep foreground color defaults (without an explicitly set
124          background) should provide enough contrast to be readable on any
125          terminal with either a black (dark) or white (light) background.
126          This only leaves red, magenta, green, and cyan (and their bold
127          counterparts) and possibly bold blue.  */
128 /* The color strings used for matched text.
129    The user can overwrite them using the deprecated
130    environment variable GREP_COLOR or the new GREP_COLORS.  */
131 static const char *selected_match_color = "01;31";      /* bold red */
132 static const char *context_match_color  = "01;31";      /* bold red */
133
134 /* Other colors.  Defaults look damn good.  */
135 static const char *filename_color = "35";       /* magenta */
136 static const char *line_num_color = "32";       /* green */
137 static const char *byte_num_color = "32";       /* green */
138 static const char *sep_color      = "36";       /* cyan */
139 static const char *selected_line_color = "";    /* default color pair */
140 static const char *context_line_color  = "";    /* default color pair */
141
142 /* Select Graphic Rendition (SGR, "\33[...m") strings.  */
143 /* Also Erase in Line (EL) to Right ("\33[K") by default.  */
144 /*    Why have EL to Right after SGR?
145          -- The behavior of line-wrapping when at the bottom of the
146             terminal screen and at the end of the current line is often
147             such that a new line is introduced, entirely cleared with
148             the current background color which may be different from the
149             default one (see the boolean back_color_erase terminfo(5)
150             capability), thus scrolling the display by one line.
151             The end of this new line will stay in this background color
152             even after reverting to the default background color with
153             "\33[m', unless it is explicitly cleared again with "\33[K"
154             (which is the behavior the user would instinctively expect
155             from the whole thing).  There may be some unavoidable
156             background-color flicker at the end of this new line because
157             of this (when timing with the monitor's redraw is just right).
158          -- The behavior of HT (tab, "\t") is usually the same as that of
159             Cursor Forward Tabulation (CHT) with a default parameter
160             of 1 ("\33[I"), i.e., it performs pure movement to the next
161             tab stop, without any clearing of either content or screen
162             attributes (including background color); try
163                printf 'asdfqwerzxcv\rASDF\tZXCV\n'
164             in a bash(1) shell to demonstrate this.  This is not what the
165             user would instinctively expect of HT (but is ok for CHT).
166             The instinctive behavior would include clearing the terminal
167             cells that are skipped over by HT with blank cells in the
168             current screen attributes, including background color;
169             the boolean dest_tabs_magic_smso terminfo(5) capability
170             indicates this saner behavior for HT, but only some rare
171             terminals have it (although it also indicates a special
172             glitch with standout mode in the Teleray terminal for which
173             it was initially introduced).  The remedy is to add "\33K"
174             after each SGR sequence, be it START (to fix the behavior
175             of any HT after that before another SGR) or END (to fix the
176             behavior of an HT in default background color that would
177             follow a line-wrapping at the bottom of the screen in another
178             background color, and to complement doing it after START).
179             Piping grep's output through a pager such as less(1) avoids
180             any HT problems since the pager performs tab expansion.
181
182       Generic disadvantages of this remedy are:
183          -- Some very rare terminals might support SGR but not EL (nobody
184             will use "grep --color" on a terminal that does not support
185             SGR in the first place).
186          -- Having these extra control sequences might somewhat complicate
187             the task of any program trying to parse "grep --color"
188             output in order to extract structuring information from it.
189       A specific disadvantage to doing it after SGR START is:
190          -- Even more possible background color flicker (when timing
191             with the monitor's redraw is just right), even when not at the
192             bottom of the screen.
193       There are no additional disadvantages specific to doing it after
194       SGR END.
195
196       It would be impractical for GNU grep to become a full-fledged
197       terminal program linked against ncurses or the like, so it will
198       not detect terminfo(5) capabilities.  */
199 static const char *sgr_start = "\33[%sm\33[K";
200 static const char *sgr_end   = "\33[m\33[K";
201
202 /* SGR utility functions.  */
203 static void
204 pr_sgr_start (char const *s)
205 {
206   if (*s)
207     print_start_colorize (sgr_start, s);
208 }
209 static void
210 pr_sgr_end (char const *s)
211 {
212   if (*s)
213     print_end_colorize (sgr_end);
214 }
215 static void
216 pr_sgr_start_if (char const *s)
217 {
218   if (color_option)
219     pr_sgr_start (s);
220 }
221 static void
222 pr_sgr_end_if (char const *s)
223 {
224   if (color_option)
225     pr_sgr_end (s);
226 }
227
228 struct color_cap
229   {
230     const char *name;
231     const char **var;
232     void (*fct) (void);
233   };
234
235 static void
236 color_cap_mt_fct (void)
237 {
238   /* Our caller just set selected_match_color.  */
239   context_match_color = selected_match_color;
240 }
241
242 static void
243 color_cap_rv_fct (void)
244 {
245   /* By this point, it was 1 (or already -1).  */
246   color_option = -1;  /* That's still != 0.  */
247 }
248
249 static void
250 color_cap_ne_fct (void)
251 {
252   sgr_start = "\33[%sm";
253   sgr_end   = "\33[m";
254 }
255
256 /* For GREP_COLORS.  */
257 static const struct color_cap color_dict[] =
258   {
259     { "mt", &selected_match_color, color_cap_mt_fct }, /* both ms/mc */
260     { "ms", &selected_match_color, NULL }, /* selected matched text */
261     { "mc", &context_match_color,  NULL }, /* context matched text */
262     { "fn", &filename_color,       NULL }, /* filename */
263     { "ln", &line_num_color,       NULL }, /* line number */
264     { "bn", &byte_num_color,       NULL }, /* byte (sic) offset */
265     { "se", &sep_color,            NULL }, /* separator */
266     { "sl", &selected_line_color,  NULL }, /* selected lines */
267     { "cx", &context_line_color,   NULL }, /* context lines */
268     { "rv", NULL,                  color_cap_rv_fct }, /* -v reverses sl/cx */
269     { "ne", NULL,                  color_cap_ne_fct }, /* no EL on SGR_* */
270     { NULL, NULL,                  NULL }
271   };
272
273 static struct exclude *excluded_patterns;
274 static struct exclude *excluded_directory_patterns;
275 /* Short options.  */
276 static char const short_options[] =
277 "0123456789A:B:C:D:EFGHIPTUVX:abcd:e:f:hiLlm:noqRrsuvwxyZz";
278
279 /* Non-boolean long options that have no corresponding short equivalents.  */
280 enum
281 {
282   BINARY_FILES_OPTION = CHAR_MAX + 1,
283   COLOR_OPTION,
284   INCLUDE_OPTION,
285   EXCLUDE_OPTION,
286   EXCLUDE_FROM_OPTION,
287   LINE_BUFFERED_OPTION,
288   LABEL_OPTION,
289   EXCLUDE_DIRECTORY_OPTION,
290   GROUP_SEPARATOR_OPTION
291 };
292
293 /* Long options equivalences. */
294 static struct option const long_options[] =
295 {
296   {"basic-regexp",    no_argument, NULL, 'G'},
297   {"extended-regexp", no_argument, NULL, 'E'},
298   {"fixed-regexp",    no_argument, NULL, 'F'},
299   {"fixed-strings",   no_argument, NULL, 'F'},
300   {"perl-regexp",     no_argument, NULL, 'P'},
301   {"after-context", required_argument, NULL, 'A'},
302   {"before-context", required_argument, NULL, 'B'},
303   {"binary-files", required_argument, NULL, BINARY_FILES_OPTION},
304   {"byte-offset", no_argument, NULL, 'b'},
305   {"context", required_argument, NULL, 'C'},
306   {"color", optional_argument, NULL, COLOR_OPTION},
307   {"colour", optional_argument, NULL, COLOR_OPTION},
308   {"count", no_argument, NULL, 'c'},
309   {"devices", required_argument, NULL, 'D'},
310   {"directories", required_argument, NULL, 'd'},
311   {"exclude", required_argument, NULL, EXCLUDE_OPTION},
312   {"exclude-from", required_argument, NULL, EXCLUDE_FROM_OPTION},
313   {"exclude-dir", required_argument, NULL, EXCLUDE_DIRECTORY_OPTION},
314   {"file", required_argument, NULL, 'f'},
315   {"files-with-matches", no_argument, NULL, 'l'},
316   {"files-without-match", no_argument, NULL, 'L'},
317   {"group-separator", required_argument, NULL, GROUP_SEPARATOR_OPTION},
318   {"help", no_argument, &show_help, 1},
319   {"include", required_argument, NULL, INCLUDE_OPTION},
320   {"ignore-case", no_argument, NULL, 'i'},
321   {"initial-tab", no_argument, NULL, 'T'},
322   {"label", required_argument, NULL, LABEL_OPTION},
323   {"line-buffered", no_argument, NULL, LINE_BUFFERED_OPTION},
324   {"line-number", no_argument, NULL, 'n'},
325   {"line-regexp", no_argument, NULL, 'x'},
326   {"max-count", required_argument, NULL, 'm'},
327
328   {"no-filename", no_argument, NULL, 'h'},
329   {"no-group-separator", no_argument, NULL, GROUP_SEPARATOR_OPTION},
330   {"no-messages", no_argument, NULL, 's'},
331   {"null", no_argument, NULL, 'Z'},
332   {"null-data", no_argument, NULL, 'z'},
333   {"only-matching", no_argument, NULL, 'o'},
334   {"quiet", no_argument, NULL, 'q'},
335   {"recursive", no_argument, NULL, 'r'},
336   {"dereference-recursive", no_argument, NULL, 'R'},
337   {"regexp", required_argument, NULL, 'e'},
338   {"invert-match", no_argument, NULL, 'v'},
339   {"silent", no_argument, NULL, 'q'},
340   {"text", no_argument, NULL, 'a'},
341   {"binary", no_argument, NULL, 'U'},
342   {"unix-byte-offsets", no_argument, NULL, 'u'},
343   {"version", no_argument, NULL, 'V'},
344   {"with-filename", no_argument, NULL, 'H'},
345   {"word-regexp", no_argument, NULL, 'w'},
346   {0, 0, 0, 0}
347 };
348
349 /* Define flags declared in grep.h. */
350 int match_icase;
351 int match_words;
352 int match_lines;
353 unsigned char eolbyte;
354
355 static char const *matcher;
356
357 /* For error messages. */
358 /* The input file name, or (if standard input) "-" or a --label argument.  */
359 static char const *filename;
360 static size_t filename_prefix_len;
361 static int errseen;
362 static int write_error_seen;
363
364 enum directories_type
365   {
366     READ_DIRECTORIES = 2,
367     RECURSE_DIRECTORIES,
368     SKIP_DIRECTORIES
369   };
370
371 /* How to handle directories.  */
372 static char const *const directories_args[] =
373 {
374   "read", "recurse", "skip", NULL
375 };
376 static enum directories_type const directories_types[] =
377 {
378   READ_DIRECTORIES, RECURSE_DIRECTORIES, SKIP_DIRECTORIES
379 };
380 ARGMATCH_VERIFY (directories_args, directories_types);
381
382 static enum directories_type directories = READ_DIRECTORIES;
383
384 enum { basic_fts_options = FTS_CWDFD | FTS_NOSTAT | FTS_TIGHT_CYCLE_CHECK };
385 static int fts_options = basic_fts_options | FTS_COMFOLLOW | FTS_PHYSICAL;
386
387 /* How to handle devices. */
388 static enum
389   {
390     READ_COMMAND_LINE_DEVICES,
391     READ_DEVICES,
392     SKIP_DEVICES
393   } devices = READ_COMMAND_LINE_DEVICES;
394
395 static int grepfile (int, char const *, int, int);
396 static int grepdesc (int, int);
397
398 static void dos_binary (void);
399 static void dos_unix_byte_offsets (void);
400 static int undossify_input (char *, size_t);
401
402 static int
403 is_device_mode (mode_t m)
404 {
405   return S_ISCHR (m) || S_ISBLK (m) || S_ISSOCK (m) || S_ISFIFO (m);
406 }
407
408 /* Return nonzero if ST->st_size is defined.  Assume the file is not a
409    symbolic link.  */
410 static int
411 usable_st_size (struct stat const *st)
412 {
413   return S_ISREG (st->st_mode) || S_TYPEISSHM (st) || S_TYPEISTMO (st);
414 }
415
416 /* Functions we'll use to search. */
417 typedef void (*compile_fp_t) (char const *, size_t);
418 typedef size_t (*execute_fp_t) (char const *, size_t, size_t *, char const *);
419 static compile_fp_t compile;
420 static execute_fp_t execute;
421
422 /* Like error, but suppress the diagnostic if requested.  */
423 static void
424 suppressible_error (char const *mesg, int errnum)
425 {
426   if (! suppress_errors)
427     error (0, errnum, "%s", mesg);
428   errseen = 1;
429 }
430
431 /* If there has already been a write error, don't bother closing
432    standard output, as that might elicit a duplicate diagnostic.  */
433 static void
434 clean_up_stdout (void)
435 {
436   if (! write_error_seen)
437     close_stdout ();
438 }
439
440 /* Return 1 if a file is known to be binary for the purpose of 'grep'.
441    BUF, of size BUFSIZE, is the initial buffer read from the file with
442    descriptor FD and status ST.  */
443 static int
444 file_is_binary (char const *buf, size_t bufsize, int fd, struct stat const *st)
445 {
446   #ifndef SEEK_HOLE
447   enum { SEEK_HOLE = SEEK_END };
448   #endif
449
450   /* If -z, test only whether the initial buffer contains '\200';
451      knowing about holes won't help.  */
452   if (! eolbyte)
453     return memchr (buf, '\200', bufsize) != 0;
454
455   /* If the initial buffer contains a null byte, guess that the file
456      is binary.  */
457   if (memchr (buf, '\0', bufsize))
458     return 1;
459
460   /* If the file has holes, it must contain a null byte somewhere.  */
461   if (SEEK_HOLE != SEEK_END && usable_st_size (st))
462     {
463       off_t cur = bufsize;
464       if (O_BINARY || fd == STDIN_FILENO)
465         {
466           cur = lseek (fd, 0, SEEK_CUR);
467           if (cur < 0)
468             return 0;
469         }
470
471       /* Look for a hole after the current location.  */
472       off_t hole_start = lseek (fd, cur, SEEK_HOLE);
473       if (0 <= hole_start)
474         {
475           if (lseek (fd, cur, SEEK_SET) < 0)
476             suppressible_error (filename, errno);
477           if (hole_start < st->st_size)
478             return 1;
479         }
480     }
481
482   /* Guess that the file does not contain binary data.  */
483   return 0;
484 }
485
486 /* Convert STR to a nonnegative integer, storing the result in *OUT.
487    STR must be a valid context length argument; report an error if it
488    isn't.  Silently ceiling *OUT at the maximum value, as that is
489    practically equivalent to infinity for grep's purposes.  */
490 static void
491 context_length_arg (char const *str, intmax_t *out)
492 {
493   switch (xstrtoimax (str, 0, 10, out, ""))
494     {
495     case LONGINT_OK:
496     case LONGINT_OVERFLOW:
497       if (0 <= *out)
498         break;
499       /* Fall through.  */
500     default:
501       error (EXIT_TROUBLE, 0, "%s: %s", str,
502              _("invalid context length argument"));
503     }
504 }
505
506 /* Return nonzero if the file with NAME should be skipped.
507    If COMMAND_LINE is nonzero, it is a command-line argument.
508    If IS_DIR is nonzero, it is a directory.  */
509 static int
510 skipped_file (char const *name, int command_line, int is_dir)
511 {
512   return (is_dir
513           ? (directories == SKIP_DIRECTORIES
514              || (! (command_line && filename_prefix_len != 0)
515                  && excluded_directory_patterns
516                  && excluded_file_name (excluded_directory_patterns, name)))
517           : (excluded_patterns
518              && excluded_file_name (excluded_patterns, name)));
519 }
520
521 /* Hairy buffering mechanism for grep.  The intent is to keep
522    all reads aligned on a page boundary and multiples of the
523    page size, unless a read yields a partial page.  */
524
525 static char *buffer;            /* Base of buffer. */
526 static size_t bufalloc;         /* Allocated buffer size, counting slop. */
527 #define INITIAL_BUFSIZE 32768   /* Initial buffer size, not counting slop. */
528 static int bufdesc;             /* File descriptor. */
529 static char *bufbeg;            /* Beginning of user-visible stuff. */
530 static char *buflim;            /* Limit of user-visible stuff. */
531 static size_t pagesize;         /* alignment of memory pages */
532 static off_t bufoffset;         /* Read offset; defined on regular files.  */
533 static off_t after_last_match;  /* Pointer after last matching line that
534                                    would have been output if we were
535                                    outputting characters. */
536
537 /* Return VAL aligned to the next multiple of ALIGNMENT.  VAL can be
538    an integer or a pointer.  Both args must be free of side effects.  */
539 #define ALIGN_TO(val, alignment) \
540   ((size_t) (val) % (alignment) == 0 \
541    ? (val) \
542    : (val) + ((alignment) - (size_t) (val) % (alignment)))
543
544 /* Reset the buffer for a new file, returning zero if we should skip it.
545    Initialize on the first time through. */
546 static int
547 reset (int fd, struct stat const *st)
548 {
549   if (! pagesize)
550     {
551       pagesize = getpagesize ();
552       if (pagesize == 0 || 2 * pagesize + 1 <= pagesize)
553         abort ();
554       bufalloc = ALIGN_TO (INITIAL_BUFSIZE, pagesize) + pagesize + 1;
555       buffer = xmalloc (bufalloc);
556     }
557
558   bufbeg = buflim = ALIGN_TO (buffer + 1, pagesize);
559   bufbeg[-1] = eolbyte;
560   bufdesc = fd;
561
562   if (S_ISREG (st->st_mode))
563     {
564       if (fd != STDIN_FILENO)
565         bufoffset = 0;
566       else
567         {
568           bufoffset = lseek (fd, 0, SEEK_CUR);
569           if (bufoffset < 0)
570             {
571               suppressible_error (_("lseek failed"), errno);
572               return 0;
573             }
574         }
575     }
576   return 1;
577 }
578
579 /* Read new stuff into the buffer, saving the specified
580    amount of old stuff.  When we're done, 'bufbeg' points
581    to the beginning of the buffer contents, and 'buflim'
582    points just after the end.  Return zero if there's an error.  */
583 static int
584 fillbuf (size_t save, struct stat const *st)
585 {
586   ssize_t fillsize;
587   int cc = 1;
588   char *readbuf;
589   size_t readsize;
590
591   /* Offset from start of buffer to start of old stuff
592      that we want to save.  */
593   size_t saved_offset = buflim - save - buffer;
594
595   if (pagesize <= buffer + bufalloc - buflim)
596     {
597       readbuf = buflim;
598       bufbeg = buflim - save;
599     }
600   else
601     {
602       size_t minsize = save + pagesize;
603       size_t newsize;
604       size_t newalloc;
605       char *newbuf;
606
607       /* Grow newsize until it is at least as great as minsize.  */
608       for (newsize = bufalloc - pagesize - 1; newsize < minsize; newsize *= 2)
609         if (newsize * 2 < newsize || newsize * 2 + pagesize + 1 < newsize * 2)
610           xalloc_die ();
611
612       /* Try not to allocate more memory than the file size indicates,
613          as that might cause unnecessary memory exhaustion if the file
614          is large.  However, do not use the original file size as a
615          heuristic if we've already read past the file end, as most
616          likely the file is growing.  */
617       if (usable_st_size (st))
618         {
619           off_t to_be_read = st->st_size - bufoffset;
620           off_t maxsize_off = save + to_be_read;
621           if (0 <= to_be_read && to_be_read <= maxsize_off
622               && maxsize_off == (size_t) maxsize_off
623               && minsize <= (size_t) maxsize_off
624               && (size_t) maxsize_off < newsize)
625             newsize = maxsize_off;
626         }
627
628       /* Add enough room so that the buffer is aligned and has room
629          for byte sentinels fore and aft.  */
630       newalloc = newsize + pagesize + 1;
631
632       newbuf = bufalloc < newalloc ? xmalloc (bufalloc = newalloc) : buffer;
633       readbuf = ALIGN_TO (newbuf + 1 + save, pagesize);
634       bufbeg = readbuf - save;
635       memmove (bufbeg, buffer + saved_offset, save);
636       bufbeg[-1] = eolbyte;
637       if (newbuf != buffer)
638         {
639           free (buffer);
640           buffer = newbuf;
641         }
642     }
643
644   readsize = buffer + bufalloc - readbuf;
645   readsize -= readsize % pagesize;
646
647   fillsize = safe_read (bufdesc, readbuf, readsize);
648   if (fillsize < 0)
649     fillsize = cc = 0;
650   bufoffset += fillsize;
651   fillsize = undossify_input (readbuf, fillsize);
652   buflim = readbuf + fillsize;
653   return cc;
654 }
655
656 /* Flags controlling the style of output. */
657 static enum
658 {
659   BINARY_BINARY_FILES,
660   TEXT_BINARY_FILES,
661   WITHOUT_MATCH_BINARY_FILES
662 } binary_files;         /* How to handle binary files.  */
663
664 static int filename_mask;       /* If zero, output nulls after filenames.  */
665 static int out_quiet;           /* Suppress all normal output. */
666 static bool out_invert;         /* Print nonmatching stuff. */
667 static int out_file;            /* Print filenames. */
668 static int out_line;            /* Print line numbers. */
669 static int out_byte;            /* Print byte offsets. */
670 static intmax_t out_before;     /* Lines of leading context. */
671 static intmax_t out_after;      /* Lines of trailing context. */
672 static int count_matches;       /* Count matching lines.  */
673 static int list_files;          /* List matching files.  */
674 static int no_filenames;        /* Suppress file names.  */
675 static intmax_t max_count;      /* Stop after outputting this many
676                                    lines from an input file.  */
677 static int line_buffered;       /* If nonzero, use line buffering, i.e.
678                                    fflush everyline out.  */
679 static char *label = NULL;      /* Fake filename for stdin */
680
681
682 /* Internal variables to keep track of byte count, context, etc. */
683 static uintmax_t totalcc;       /* Total character count before bufbeg. */
684 static char const *lastnl;      /* Pointer after last newline counted. */
685 static char const *lastout;     /* Pointer after last character output;
686                                    NULL if no character has been output
687                                    or if it's conceptually before bufbeg. */
688 static uintmax_t totalnl;       /* Total newline count before lastnl. */
689 static intmax_t outleft;        /* Maximum number of lines to be output.  */
690 static intmax_t pending;        /* Pending lines of output.
691                                    Always kept 0 if out_quiet is true.  */
692 static int done_on_match;       /* Stop scanning file on first match.  */
693 static int exit_on_match;       /* Exit on first match.  */
694
695 #include "dosbuf.c"
696
697 /* Add two numbers that count input bytes or lines, and report an
698    error if the addition overflows.  */
699 static uintmax_t
700 add_count (uintmax_t a, uintmax_t b)
701 {
702   uintmax_t sum = a + b;
703   if (sum < a)
704     error (EXIT_TROUBLE, 0, _("input is too large to count"));
705   return sum;
706 }
707
708 static void
709 nlscan (char const *lim)
710 {
711   size_t newlines = 0;
712   char const *beg;
713   for (beg = lastnl; beg < lim; beg++)
714     {
715       beg = memchr (beg, eolbyte, lim - beg);
716       if (!beg)
717         break;
718       newlines++;
719     }
720   totalnl = add_count (totalnl, newlines);
721   lastnl = lim;
722 }
723
724 /* Print the current filename.  */
725 static void
726 print_filename (void)
727 {
728   pr_sgr_start_if (filename_color);
729   fputs (filename, stdout);
730   pr_sgr_end_if (filename_color);
731 }
732
733 /* Print a character separator.  */
734 static void
735 print_sep (char sep)
736 {
737   pr_sgr_start_if (sep_color);
738   fputc (sep, stdout);
739   pr_sgr_end_if (sep_color);
740 }
741
742 /* Print a line number or a byte offset.  */
743 static void
744 print_offset (uintmax_t pos, int min_width, const char *color)
745 {
746   /* Do not rely on printf to print pos, since uintmax_t may be longer
747      than long, and long long is not portable.  */
748
749   char buf[sizeof pos * CHAR_BIT];
750   char *p = buf + sizeof buf;
751
752   do
753     {
754       *--p = '0' + pos % 10;
755       --min_width;
756     }
757   while ((pos /= 10) != 0);
758
759   /* Do this to maximize the probability of alignment across lines.  */
760   if (align_tabs)
761     while (--min_width >= 0)
762       *--p = ' ';
763
764   pr_sgr_start_if (color);
765   fwrite (p, 1, buf + sizeof buf - p, stdout);
766   pr_sgr_end_if (color);
767 }
768
769 /* Print a whole line head (filename, line, byte).  */
770 static void
771 print_line_head (char const *beg, char const *lim, int sep)
772 {
773   int pending_sep = 0;
774
775   if (out_file)
776     {
777       print_filename ();
778       if (filename_mask)
779         pending_sep = 1;
780       else
781         fputc (0, stdout);
782     }
783
784   if (out_line)
785     {
786       if (lastnl < lim)
787         {
788           nlscan (beg);
789           totalnl = add_count (totalnl, 1);
790           lastnl = lim;
791         }
792       if (pending_sep)
793         print_sep (sep);
794       print_offset (totalnl, 4, line_num_color);
795       pending_sep = 1;
796     }
797
798   if (out_byte)
799     {
800       uintmax_t pos = add_count (totalcc, beg - bufbeg);
801       pos = dossified_pos (pos);
802       if (pending_sep)
803         print_sep (sep);
804       print_offset (pos, 6, byte_num_color);
805       pending_sep = 1;
806     }
807
808   if (pending_sep)
809     {
810       /* This assumes sep is one column wide.
811          Try doing this any other way with Unicode
812          (and its combining and wide characters)
813          filenames and you're wasting your efforts.  */
814       if (align_tabs)
815         fputs ("\t\b", stdout);
816
817       print_sep (sep);
818     }
819 }
820
821 static const char *
822 print_line_middle (const char *beg, const char *lim,
823                    const char *line_color, const char *match_color)
824 {
825   size_t match_size;
826   size_t match_offset;
827   const char *cur = beg;
828   const char *mid = NULL;
829
830   while (cur < lim
831          && ((match_offset = execute (beg, lim - beg, &match_size,
832                                       beg + (cur - beg))) != (size_t) -1))
833     {
834       char const *b = beg + match_offset;
835
836       /* Avoid matching the empty line at the end of the buffer. */
837       if (b == lim)
838         break;
839
840       /* Avoid hanging on grep --color "" foo */
841       if (match_size == 0)
842         {
843           /* Make minimal progress; there may be further non-empty matches.  */
844           /* XXX - Could really advance by one whole multi-octet character.  */
845           match_size = 1;
846           if (!mid)
847             mid = cur;
848         }
849       else
850         {
851           /* This function is called on a matching line only,
852              but is it selected or rejected/context?  */
853           if (only_matching)
854             print_line_head (b, lim, (out_invert ? SEP_CHAR_REJECTED
855                                       : SEP_CHAR_SELECTED));
856           else
857             {
858               pr_sgr_start (line_color);
859               if (mid)
860                 {
861                   cur = mid;
862                   mid = NULL;
863                 }
864               fwrite (cur, sizeof (char), b - cur, stdout);
865             }
866
867           pr_sgr_start_if (match_color);
868           fwrite (b, sizeof (char), match_size, stdout);
869           pr_sgr_end_if (match_color);
870           if (only_matching)
871             fputs ("\n", stdout);
872         }
873       cur = b + match_size;
874     }
875
876   if (only_matching)
877     cur = lim;
878   else if (mid)
879     cur = mid;
880
881   return cur;
882 }
883
884 static const char *
885 print_line_tail (const char *beg, const char *lim, const char *line_color)
886 {
887   size_t eol_size;
888   size_t tail_size;
889
890   eol_size   = (lim > beg && lim[-1] == eolbyte);
891   eol_size  += (lim - eol_size > beg && lim[-(1 + eol_size)] == '\r');
892   tail_size  =  lim - eol_size - beg;
893
894   if (tail_size > 0)
895     {
896       pr_sgr_start (line_color);
897       fwrite (beg, 1, tail_size, stdout);
898       beg += tail_size;
899       pr_sgr_end (line_color);
900     }
901
902   return beg;
903 }
904
905 static void
906 prline (char const *beg, char const *lim, int sep)
907 {
908   int matching;
909   const char *line_color;
910   const char *match_color;
911
912   if (!only_matching)
913     print_line_head (beg, lim, sep);
914
915   matching = (sep == SEP_CHAR_SELECTED) ^ out_invert;
916
917   if (color_option)
918     {
919       line_color = (((sep == SEP_CHAR_SELECTED)
920                      ^ (out_invert && (color_option < 0)))
921                     ? selected_line_color  : context_line_color);
922       match_color = (sep == SEP_CHAR_SELECTED
923                      ? selected_match_color : context_match_color);
924     }
925   else
926     line_color = match_color = NULL; /* Shouldn't be used.  */
927
928   if ((only_matching && matching)
929       || (color_option && (*line_color || *match_color)))
930     {
931       /* We already know that non-matching lines have no match (to colorize). */
932       if (matching && (only_matching || *match_color))
933         beg = print_line_middle (beg, lim, line_color, match_color);
934
935       if (!only_matching && *line_color)
936         {
937           /* This code is exercised at least when grep is invoked like this:
938              echo k| GREP_COLORS='sl=01;32' src/grep k --color=always  */
939           beg = print_line_tail (beg, lim, line_color);
940         }
941     }
942
943   if (!only_matching && lim > beg)
944     fwrite (beg, 1, lim - beg, stdout);
945
946   if (ferror (stdout))
947     {
948       write_error_seen = 1;
949       error (EXIT_TROUBLE, 0, _("write error"));
950     }
951
952   lastout = lim;
953
954   if (line_buffered)
955     fflush (stdout);
956 }
957
958 /* Print pending lines of trailing context prior to LIM. Trailing context ends
959    at the next matching line when OUTLEFT is 0.  */
960 static void
961 prpending (char const *lim)
962 {
963   if (!lastout)
964     lastout = bufbeg;
965   while (pending > 0 && lastout < lim)
966     {
967       char const *nl = memchr (lastout, eolbyte, lim - lastout);
968       size_t match_size;
969       --pending;
970       if (outleft
971           || ((execute (lastout, nl + 1 - lastout,
972                         &match_size, NULL) == (size_t) -1)
973               == !out_invert))
974         prline (lastout, nl + 1, SEP_CHAR_REJECTED);
975       else
976         pending = 0;
977     }
978 }
979
980 /* Output the lines between BEG and LIM.  Deal with context.  */
981 static void
982 prtext (char const *beg, char const *lim)
983 {
984   static bool used;     /* Avoid printing SEP_STR_GROUP before any output.  */
985   char eol = eolbyte;
986
987   if (!out_quiet && pending > 0)
988     prpending (beg);
989
990   char const *p = beg;
991
992   if (!out_quiet)
993     {
994       /* Deal with leading context.  */
995       char const *bp = lastout ? lastout : bufbeg;
996       intmax_t i;
997       for (i = 0; i < out_before; ++i)
998         if (p > bp)
999           do
1000             --p;
1001           while (p[-1] != eol);
1002
1003       /* Print the group separator unless the output is adjacent to
1004          the previous output in the file.  */
1005       if ((0 <= out_before || 0 <= out_after) && used
1006           && p != lastout && group_separator)
1007         {
1008           pr_sgr_start_if (sep_color);
1009           fputs (group_separator, stdout);
1010           pr_sgr_end_if (sep_color);
1011           fputc ('\n', stdout);
1012         }
1013
1014       while (p < beg)
1015         {
1016           char const *nl = memchr (p, eol, beg - p);
1017           nl++;
1018           prline (p, nl, SEP_CHAR_REJECTED);
1019           p = nl;
1020         }
1021     }
1022
1023   intmax_t n;
1024   if (out_invert)
1025     {
1026       /* One or more lines are output.  */
1027       for (n = 0; p < lim && n < outleft; n++)
1028         {
1029           char const *nl = memchr (p, eol, lim - p);
1030           nl++;
1031           if (!out_quiet)
1032             prline (p, nl, SEP_CHAR_SELECTED);
1033           p = nl;
1034         }
1035     }
1036   else
1037     {
1038       /* Just one line is output.  */
1039       if (!out_quiet)
1040         prline (beg, lim, SEP_CHAR_SELECTED);
1041       n = 1;
1042       p = lim;
1043     }
1044
1045   after_last_match = bufoffset - (buflim - p);
1046   pending = out_quiet ? 0 : MAX (0, out_after);
1047   used = true;
1048   outleft -= n;
1049 }
1050
1051 /* Invoke the matcher, EXECUTE, on buffer BUF of SIZE bytes.  If there
1052    is no match, return (size_t) -1.  Otherwise, set *MATCH_SIZE to the
1053    length of the match and return the offset of the start of the match.  */
1054 static size_t
1055 do_execute (char const *buf, size_t size, size_t *match_size)
1056 {
1057   size_t result;
1058   const char *line_next;
1059
1060   /* With the current implementation, using --ignore-case with a multi-byte
1061      character set is very inefficient when applied to a large buffer
1062      containing many matches.  We can avoid much of the wasted effort
1063      by matching line-by-line.
1064
1065      FIXME: this is just an ugly workaround, and it doesn't really
1066      belong here.  Also, PCRE is always using this same per-line
1067      matching algorithm.  Either we fix -i, or we should refactor
1068      this code---for example, we could add another function pointer
1069      to struct matcher to split the buffer passed to execute.  It would
1070      perform the memchr if line-by-line matching is necessary, or just
1071      return buf + size otherwise.  */
1072   if (! (execute == Fexecute || execute == Pexecute)
1073       || MB_CUR_MAX == 1 || !match_icase)
1074     return execute (buf, size, match_size, NULL);
1075
1076   for (line_next = buf; line_next < buf + size; )
1077     {
1078       const char *line_buf = line_next;
1079       const char *line_end = memchr (line_buf, eolbyte,
1080                                      (buf + size) - line_buf);
1081       if (line_end == NULL)
1082         line_next = line_end = buf + size;
1083       else
1084         line_next = line_end + 1;
1085
1086       result = execute (line_buf, line_next - line_buf, match_size, NULL);
1087       if (result != (size_t) -1)
1088         return (line_buf - buf) + result;
1089     }
1090
1091   return (size_t) -1;
1092 }
1093
1094 /* Scan the specified portion of the buffer, matching lines (or
1095    between matching lines if OUT_INVERT is true).  Return a count of
1096    lines printed. */
1097 static intmax_t
1098 grepbuf (char const *beg, char const *lim)
1099 {
1100   intmax_t outleft0 = outleft;
1101   char const *p;
1102   char const *endp;
1103
1104   for (p = beg; p < lim; p = endp)
1105     {
1106       size_t match_size;
1107       size_t match_offset = do_execute (p, lim - p, &match_size);
1108       if (match_offset == (size_t) -1)
1109         {
1110           if (!out_invert)
1111             break;
1112           match_offset = lim - p;
1113           match_size = 0;
1114         }
1115       char const *b = p + match_offset;
1116       endp = b + match_size;
1117       /* Avoid matching the empty line at the end of the buffer. */
1118       if (!out_invert && b == lim)
1119         break;
1120       if (!out_invert || p < b)
1121         {
1122           char const *prbeg = out_invert ? p : b;
1123           char const *prend = out_invert ? b : endp;
1124           prtext (prbeg, prend);
1125           if (!outleft || done_on_match)
1126             {
1127               if (exit_on_match)
1128                 exit (EXIT_SUCCESS);
1129               break;
1130             }
1131         }
1132     }
1133
1134   return outleft0 - outleft;
1135 }
1136
1137 /* Search a given file.  Normally, return a count of lines printed;
1138    but if the file is a directory and we search it recursively, then
1139    return -2 if there was a match, and -1 otherwise.  */
1140 static intmax_t
1141 grep (int fd, struct stat const *st)
1142 {
1143   intmax_t nlines, i;
1144   int not_text;
1145   size_t residue, save;
1146   char oldc;
1147   char *beg;
1148   char *lim;
1149   char eol = eolbyte;
1150
1151   if (! reset (fd, st))
1152     return 0;
1153
1154   totalcc = 0;
1155   lastout = 0;
1156   totalnl = 0;
1157   outleft = max_count;
1158   after_last_match = 0;
1159   pending = 0;
1160
1161   nlines = 0;
1162   residue = 0;
1163   save = 0;
1164
1165   if (! fillbuf (save, st))
1166     {
1167       suppressible_error (filename, errno);
1168       return 0;
1169     }
1170
1171   not_text = (((binary_files == BINARY_BINARY_FILES && !out_quiet)
1172                || binary_files == WITHOUT_MATCH_BINARY_FILES)
1173               && file_is_binary (bufbeg, buflim - bufbeg, fd, st));
1174   if (not_text && binary_files == WITHOUT_MATCH_BINARY_FILES)
1175     return 0;
1176   done_on_match += not_text;
1177   out_quiet += not_text;
1178
1179   for (;;)
1180     {
1181       lastnl = bufbeg;
1182       if (lastout)
1183         lastout = bufbeg;
1184
1185       beg = bufbeg + save;
1186
1187       /* no more data to scan (eof) except for maybe a residue -> break */
1188       if (beg == buflim)
1189         break;
1190
1191       /* Determine new residue (the length of an incomplete line at the end of
1192          the buffer, 0 means there is no incomplete last line).  */
1193       oldc = beg[-1];
1194       beg[-1] = eol;
1195       /* FIXME: use rawmemrchr if/when it exists, since we have ensured
1196          that this use of memrchr is guaranteed never to return NULL.  */
1197       lim = memrchr (beg - 1, eol, buflim - beg + 1);
1198       ++lim;
1199       beg[-1] = oldc;
1200       if (lim == beg)
1201         lim = beg - residue;
1202       beg -= residue;
1203       residue = buflim - lim;
1204
1205       if (beg < lim)
1206         {
1207           if (outleft)
1208             nlines += grepbuf (beg, lim);
1209           if (pending)
1210             prpending (lim);
1211           if ((!outleft && !pending) || (nlines && done_on_match))
1212             goto finish_grep;
1213         }
1214
1215       /* The last OUT_BEFORE lines at the end of the buffer will be needed as
1216          leading context if there is a matching line at the begin of the
1217          next data. Make beg point to their begin.  */
1218       i = 0;
1219       beg = lim;
1220       while (i < out_before && beg > bufbeg && beg != lastout)
1221         {
1222           ++i;
1223           do
1224             --beg;
1225           while (beg[-1] != eol);
1226         }
1227
1228       /* Detect whether leading context is adjacent to previous output.  */
1229       if (beg != lastout)
1230         lastout = 0;
1231
1232       /* Handle some details and read more data to scan.  */
1233       save = residue + lim - beg;
1234       if (out_byte)
1235         totalcc = add_count (totalcc, buflim - bufbeg - save);
1236       if (out_line)
1237         nlscan (beg);
1238       if (! fillbuf (save, st))
1239         {
1240           suppressible_error (filename, errno);
1241           goto finish_grep;
1242         }
1243     }
1244   if (residue)
1245     {
1246       *buflim++ = eol;
1247       if (outleft)
1248         nlines += grepbuf (bufbeg + save - residue, buflim);
1249       if (pending)
1250         prpending (buflim);
1251     }
1252
1253  finish_grep:
1254   done_on_match -= not_text;
1255   out_quiet -= not_text;
1256   if ((not_text & ~out_quiet) && nlines != 0)
1257     printf (_("Binary file %s matches\n"), filename);
1258   return nlines;
1259 }
1260
1261 static int
1262 grepdirent (FTS *fts, FTSENT *ent, int command_line)
1263 {
1264   int follow, dirdesc;
1265   struct stat *st = ent->fts_statp;
1266   command_line &= ent->fts_level == FTS_ROOTLEVEL;
1267
1268   if (ent->fts_info == FTS_DP)
1269     {
1270       if (directories == RECURSE_DIRECTORIES && command_line)
1271         out_file &= ~ (2 * !no_filenames);
1272       return 1;
1273     }
1274
1275   if (skipped_file (ent->fts_name, command_line,
1276                     (ent->fts_info == FTS_D || ent->fts_info == FTS_DC
1277                      || ent->fts_info == FTS_DNR)))
1278     {
1279       fts_set (fts, ent, FTS_SKIP);
1280       return 1;
1281     }
1282
1283   filename = ent->fts_path + filename_prefix_len;
1284   follow = (fts->fts_options & FTS_LOGICAL
1285             || (fts->fts_options & FTS_COMFOLLOW && command_line));
1286
1287   switch (ent->fts_info)
1288     {
1289     case FTS_D:
1290       if (directories == RECURSE_DIRECTORIES)
1291         {
1292           out_file |= 2 * !no_filenames;
1293           return 1;
1294         }
1295       fts_set (fts, ent, FTS_SKIP);
1296       break;
1297
1298     case FTS_DC:
1299       if (!suppress_errors)
1300         error (0, 0, _("warning: %s: %s"), filename,
1301                _("recursive directory loop"));
1302       return 1;
1303
1304     case FTS_DNR:
1305     case FTS_ERR:
1306     case FTS_NS:
1307       suppressible_error (filename, ent->fts_errno);
1308       return 1;
1309
1310     case FTS_DEFAULT:
1311     case FTS_NSOK:
1312       if (devices == SKIP_DEVICES
1313           || (devices == READ_COMMAND_LINE_DEVICES && !command_line))
1314         {
1315           struct stat st1;
1316           if (! st->st_mode)
1317             {
1318               /* The file type is not already known.  Get the file status
1319                  before opening, since opening might have side effects
1320                  on a device.  */
1321               int flag = follow ? 0 : AT_SYMLINK_NOFOLLOW;
1322               if (fstatat (fts->fts_cwd_fd, ent->fts_accpath, &st1, flag) != 0)
1323                 {
1324                   suppressible_error (filename, errno);
1325                   return 1;
1326                 }
1327               st = &st1;
1328             }
1329           if (is_device_mode (st->st_mode))
1330             return 1;
1331         }
1332       break;
1333
1334     case FTS_F:
1335     case FTS_SLNONE:
1336       break;
1337
1338     case FTS_SL:
1339     case FTS_W:
1340       return 1;
1341
1342     default:
1343       abort ();
1344     }
1345
1346   dirdesc = ((fts->fts_options & (FTS_NOCHDIR | FTS_CWDFD)) == FTS_CWDFD
1347              ? fts->fts_cwd_fd
1348              : AT_FDCWD);
1349   return grepfile (dirdesc, ent->fts_accpath, follow, command_line);
1350 }
1351
1352 static int
1353 grepfile (int dirdesc, char const *name, int follow, int command_line)
1354 {
1355   int desc = openat_safer (dirdesc, name, O_RDONLY | (follow ? 0 : O_NOFOLLOW));
1356   if (desc < 0)
1357     {
1358       if (follow || (errno != ELOOP && errno != EMLINK))
1359         suppressible_error (filename, errno);
1360       return 1;
1361     }
1362   return grepdesc (desc, command_line);
1363 }
1364
1365 static int
1366 grepdesc (int desc, int command_line)
1367 {
1368   intmax_t count;
1369   int status = 1;
1370   struct stat st;
1371
1372   /* Get the file status, possibly for the second time.  This catches
1373      a race condition if the directory entry changes after the
1374      directory entry is read and before the file is opened.  For
1375      example, normally DESC is a directory only at the top level, but
1376      there is an exception if some other process substitutes a
1377      directory for a non-directory while 'grep' is running.  */
1378   if (fstat (desc, &st) != 0)
1379     {
1380       suppressible_error (filename, errno);
1381       goto closeout;
1382     }
1383
1384   if (desc != STDIN_FILENO && command_line
1385       && skipped_file (filename, 1, S_ISDIR (st.st_mode)))
1386     goto closeout;
1387
1388   if (desc != STDIN_FILENO
1389       && directories == RECURSE_DIRECTORIES && S_ISDIR (st.st_mode))
1390     {
1391       /* Traverse the directory starting with its full name, because
1392          unfortunately fts provides no way to traverse the directory
1393          starting from its file descriptor.  */
1394
1395       FTS *fts;
1396       FTSENT *ent;
1397       int opts = fts_options & ~(command_line ? 0 : FTS_COMFOLLOW);
1398       char *fts_arg[2];
1399
1400       /* Close DESC now, to conserve file descriptors if the race
1401          condition occurs many times in a deep recursion.  */
1402       if (close (desc) != 0)
1403         suppressible_error (filename, errno);
1404
1405       fts_arg[0] = (char *) filename;
1406       fts_arg[1] = NULL;
1407       fts = fts_open (fts_arg, opts, NULL);
1408
1409       if (!fts)
1410         xalloc_die ();
1411       while ((ent = fts_read (fts)))
1412         status &= grepdirent (fts, ent, command_line);
1413       if (errno)
1414         suppressible_error (filename, errno);
1415       if (fts_close (fts) != 0)
1416         suppressible_error (filename, errno);
1417       return status;
1418     }
1419   if (desc != STDIN_FILENO
1420       && ((directories == SKIP_DIRECTORIES && S_ISDIR (st.st_mode))
1421           || ((devices == SKIP_DEVICES
1422                || (devices == READ_COMMAND_LINE_DEVICES && !command_line))
1423               && is_device_mode (st.st_mode))))
1424     goto closeout;
1425
1426   /* If there is a regular file on stdout and the current file refers
1427      to the same i-node, we have to report the problem and skip it.
1428      Otherwise when matching lines from some other input reach the
1429      disk before we open this file, we can end up reading and matching
1430      those lines and appending them to the file from which we're reading.
1431      Then we'd have what appears to be an infinite loop that'd terminate
1432      only upon filling the output file system or reaching a quota.
1433      However, there is no risk of an infinite loop if grep is generating
1434      no output, i.e., with --silent, --quiet, -q.
1435      Similarly, with any of these:
1436        --max-count=N (-m) (for N >= 2)
1437        --files-with-matches (-l)
1438        --files-without-match (-L)
1439      there is no risk of trouble.
1440      For --max-count=1, grep stops after printing the first match,
1441      so there is no risk of malfunction.  But even --max-count=2, with
1442      input==output, while there is no risk of infloop, there is a race
1443      condition that could result in "alternate" output.  */
1444   if (!out_quiet && list_files == 0 && 1 < max_count
1445       && S_ISREG (out_stat.st_mode) && out_stat.st_ino
1446       && SAME_INODE (st, out_stat))
1447     {
1448       if (! suppress_errors)
1449         error (0, 0, _("input file %s is also the output"), quote (filename));
1450       errseen = 1;
1451       goto closeout;
1452     }
1453
1454 #if defined SET_BINARY
1455   /* Set input to binary mode.  Pipes are simulated with files
1456      on DOS, so this includes the case of "foo | grep bar".  */
1457   if (!isatty (desc))
1458     SET_BINARY (desc);
1459 #endif
1460
1461   count = grep (desc, &st);
1462   if (count < 0)
1463     status = count + 2;
1464   else
1465     {
1466       if (count_matches)
1467         {
1468           if (out_file)
1469             {
1470               print_filename ();
1471               if (filename_mask)
1472                 print_sep (SEP_CHAR_SELECTED);
1473               else
1474                 fputc (0, stdout);
1475             }
1476           printf ("%" PRIdMAX "\n", count);
1477         }
1478
1479       status = !count;
1480       if (list_files == 1 - 2 * status)
1481         {
1482           print_filename ();
1483           fputc ('\n' & filename_mask, stdout);
1484         }
1485
1486       if (desc == STDIN_FILENO)
1487         {
1488           off_t required_offset = outleft ? bufoffset : after_last_match;
1489           if (required_offset != bufoffset
1490               && lseek (desc, required_offset, SEEK_SET) < 0
1491               && S_ISREG (st.st_mode))
1492             suppressible_error (filename, errno);
1493         }
1494     }
1495
1496  closeout:
1497   if (desc != STDIN_FILENO && close (desc) != 0)
1498     suppressible_error (filename, errno);
1499   return status;
1500 }
1501
1502 static int
1503 grep_command_line_arg (char const *arg)
1504 {
1505   if (STREQ (arg, "-"))
1506     {
1507       filename = label ? label : _("(standard input)");
1508       return grepdesc (STDIN_FILENO, 1);
1509     }
1510   else
1511     {
1512       filename = arg;
1513       return grepfile (AT_FDCWD, arg, 1, 1);
1514     }
1515 }
1516
1517 _Noreturn void usage (int);
1518 void
1519 usage (int status)
1520 {
1521   if (status != 0)
1522     {
1523       fprintf (stderr, _("Usage: %s [OPTION]... PATTERN [FILE]...\n"),
1524                program_name);
1525       fprintf (stderr, _("Try '%s --help' for more information.\n"),
1526                program_name);
1527     }
1528   else
1529     {
1530       printf (_("Usage: %s [OPTION]... PATTERN [FILE]...\n"), program_name);
1531       printf (_("Search for PATTERN in each FILE or standard input.\n"));
1532       printf (_("PATTERN is, by default, a basic regular expression (BRE).\n"));
1533       printf (_("\
1534 Example: %s -i 'hello world' menu.h main.c\n\
1535 \n\
1536 Regexp selection and interpretation:\n"), program_name);
1537       printf (_("\
1538   -E, --extended-regexp     PATTERN is an extended regular expression (ERE)\n\
1539   -F, --fixed-strings       PATTERN is a set of newline-separated fixed strings\n\
1540   -G, --basic-regexp        PATTERN is a basic regular expression (BRE)\n\
1541   -P, --perl-regexp         PATTERN is a Perl regular expression\n"));
1542   /* -X is undocumented on purpose. */
1543       printf (_("\
1544   -e, --regexp=PATTERN      use PATTERN for matching\n\
1545   -f, --file=FILE           obtain PATTERN from FILE\n\
1546   -i, --ignore-case         ignore case distinctions\n\
1547   -w, --word-regexp         force PATTERN to match only whole words\n\
1548   -x, --line-regexp         force PATTERN to match only whole lines\n\
1549   -z, --null-data           a data line ends in 0 byte, not newline\n"));
1550       printf (_("\
1551 \n\
1552 Miscellaneous:\n\
1553   -s, --no-messages         suppress error messages\n\
1554   -v, --invert-match        select non-matching lines\n\
1555   -V, --version             display version information and exit\n\
1556       --help                display this help text and exit\n"));
1557       printf (_("\
1558 \n\
1559 Output control:\n\
1560   -m, --max-count=NUM       stop after NUM matches\n\
1561   -b, --byte-offset         print the byte offset with output lines\n\
1562   -n, --line-number         print line number with output lines\n\
1563       --line-buffered       flush output on every line\n\
1564   -H, --with-filename       print the file name for each match\n\
1565   -h, --no-filename         suppress the file name prefix on output\n\
1566       --label=LABEL         use LABEL as the standard input file name prefix\n\
1567 "));
1568       printf (_("\
1569   -o, --only-matching       show only the part of a line matching PATTERN\n\
1570   -q, --quiet, --silent     suppress all normal output\n\
1571       --binary-files=TYPE   assume that binary files are TYPE;\n\
1572                             TYPE is 'binary', 'text', or 'without-match'\n\
1573   -a, --text                equivalent to --binary-files=text\n\
1574 "));
1575       printf (_("\
1576   -I                        equivalent to --binary-files=without-match\n\
1577   -d, --directories=ACTION  how to handle directories;\n\
1578                             ACTION is 'read', 'recurse', or 'skip'\n\
1579   -D, --devices=ACTION      how to handle devices, FIFOs and sockets;\n\
1580                             ACTION is 'read' or 'skip'\n\
1581   -r, --recursive           like --directories=recurse\n\
1582   -R, --dereference-recursive  likewise, but follow all symlinks\n\
1583 "));
1584       printf (_("\
1585       --include=FILE_PATTERN  search only files that match FILE_PATTERN\n\
1586       --exclude=FILE_PATTERN  skip files and directories matching FILE_PATTERN\n\
1587       --exclude-from=FILE   skip files matching any file pattern from FILE\n\
1588       --exclude-dir=PATTERN  directories that match PATTERN will be skipped.\n\
1589 "));
1590       printf (_("\
1591   -L, --files-without-match  print only names of FILEs containing no match\n\
1592   -l, --files-with-matches  print only names of FILEs containing matches\n\
1593   -c, --count               print only a count of matching lines per FILE\n\
1594   -T, --initial-tab         make tabs line up (if needed)\n\
1595   -Z, --null                print 0 byte after FILE name\n"));
1596       printf (_("\
1597 \n\
1598 Context control:\n\
1599   -B, --before-context=NUM  print NUM lines of leading context\n\
1600   -A, --after-context=NUM   print NUM lines of trailing context\n\
1601   -C, --context=NUM         print NUM lines of output context\n\
1602 "));
1603       printf (_("\
1604   -NUM                      same as --context=NUM\n\
1605       --color[=WHEN],\n\
1606       --colour[=WHEN]       use markers to highlight the matching strings;\n\
1607                             WHEN is 'always', 'never', or 'auto'\n\
1608   -U, --binary              do not strip CR characters at EOL (MSDOS/Windows)\n\
1609   -u, --unix-byte-offsets   report offsets as if CRs were not there\n\
1610                             (MSDOS/Windows)\n\
1611 \n"));
1612       printf (_("\
1613 'egrep' means 'grep -E'.  'fgrep' means 'grep -F'.\n\
1614 Direct invocation as either 'egrep' or 'fgrep' is deprecated.\n"));
1615       printf (_("\
1616 When FILE is -, read standard input.  With no FILE, read . if a command-line\n\
1617 -r is given, - otherwise.  If fewer than two FILEs are given, assume -h.\n\
1618 Exit status is 0 if any line is selected, 1 otherwise;\n\
1619 if any error occurs and -q is not given, the exit status is 2.\n"));
1620       printf (_("\nReport bugs to: %s\n"), PACKAGE_BUGREPORT);
1621       printf (_("GNU Grep home page: <%s>\n"),
1622               "http://www.gnu.org/software/grep/");
1623       fputs (_("General help using GNU software: <http://www.gnu.org/gethelp/>\n"),
1624              stdout);
1625
1626     }
1627   exit (status);
1628 }
1629
1630 /* Pattern compilers and matchers.  */
1631
1632 static void
1633 Gcompile (char const *pattern, size_t size)
1634 {
1635   GEAcompile (pattern, size, RE_SYNTAX_GREP | RE_NO_EMPTY_RANGES);
1636 }
1637
1638 static void
1639 Ecompile (char const *pattern, size_t size)
1640 {
1641   GEAcompile (pattern, size, RE_SYNTAX_POSIX_EGREP | RE_NO_EMPTY_RANGES);
1642 }
1643
1644 static void
1645 Acompile (char const *pattern, size_t size)
1646 {
1647   GEAcompile (pattern, size, RE_SYNTAX_AWK);
1648 }
1649
1650 static void
1651 GAcompile (char const *pattern, size_t size)
1652 {
1653   GEAcompile (pattern, size, RE_SYNTAX_GNU_AWK);
1654 }
1655
1656 static void
1657 PAcompile (char const *pattern, size_t size)
1658 {
1659   GEAcompile (pattern, size, RE_SYNTAX_POSIX_AWK);
1660 }
1661
1662 struct matcher
1663 {
1664   char const name[16];
1665   compile_fp_t compile;
1666   execute_fp_t execute;
1667 };
1668 static struct matcher const matchers[] = {
1669   { "grep",      Gcompile, EGexecute },
1670   { "egrep",     Ecompile, EGexecute },
1671   { "fgrep",     Fcompile,  Fexecute },
1672   { "awk",       Acompile, EGexecute },
1673   { "gawk",     GAcompile, EGexecute },
1674   { "posixawk", PAcompile, EGexecute },
1675   { "perl",      Pcompile,  Pexecute },
1676   { "", NULL, NULL },
1677 };
1678
1679 /* Set the matcher to M if available.  Exit in case of conflicts or if
1680    M is not available.  */
1681 static void
1682 setmatcher (char const *m)
1683 {
1684   struct matcher const *p;
1685
1686   if (matcher && !STREQ (matcher, m))
1687     error (EXIT_TROUBLE, 0, _("conflicting matchers specified"));
1688
1689   for (p = matchers; p->compile; p++)
1690     if (STREQ (m, p->name))
1691       {
1692         matcher = p->name;
1693         compile = p->compile;
1694         execute = p->execute;
1695         return;
1696       }
1697
1698   error (EXIT_TROUBLE, 0, _("invalid matcher %s"), m);
1699 }
1700
1701 /* Find the white-space-separated options specified by OPTIONS, and
1702    using BUF to store copies of these options, set ARGV[0], ARGV[1],
1703    etc. to the option copies.  Return the number N of options found.
1704    Do not set ARGV[N] to NULL.  If ARGV is NULL, do not store ARGV[0]
1705    etc.  Backslash can be used to escape whitespace (and backslashes).  */
1706 static size_t
1707 prepend_args (char const *options, char *buf, char **argv)
1708 {
1709   char const *o = options;
1710   char *b = buf;
1711   size_t n = 0;
1712
1713   for (;;)
1714     {
1715       while (c_isspace (to_uchar (*o)))
1716         o++;
1717       if (!*o)
1718         return n;
1719       if (argv)
1720         argv[n] = b;
1721       n++;
1722
1723       do
1724         if ((*b++ = *o++) == '\\' && *o)
1725           b[-1] = *o++;
1726       while (*o && ! c_isspace (to_uchar (*o)));
1727
1728       *b++ = '\0';
1729     }
1730 }
1731
1732 /* Prepend the whitespace-separated options in OPTIONS to the argument
1733    vector of a main program with argument count *PARGC and argument
1734    vector *PARGV.  Return the number of options prepended.  */
1735 static int
1736 prepend_default_options (char const *options, int *pargc, char ***pargv)
1737 {
1738   if (options && *options)
1739     {
1740       char *buf = xmalloc (strlen (options) + 1);
1741       size_t prepended = prepend_args (options, buf, NULL);
1742       int argc = *pargc;
1743       char *const *argv = *pargv;
1744       char **pp;
1745       enum { MAX_ARGS = MIN (INT_MAX, SIZE_MAX / sizeof *pp - 1) };
1746       if (MAX_ARGS - argc < prepended)
1747         xalloc_die ();
1748       pp = xmalloc ((prepended + argc + 1) * sizeof *pp);
1749       *pargc = prepended + argc;
1750       *pargv = pp;
1751       *pp++ = *argv++;
1752       pp += prepend_args (options, buf, pp);
1753       while ((*pp++ = *argv++))
1754         continue;
1755       return prepended;
1756     }
1757
1758   return 0;
1759 }
1760
1761 /* Get the next non-digit option from ARGC and ARGV.
1762    Return -1 if there are no more options.
1763    Process any digit options that were encountered on the way,
1764    and store the resulting integer into *DEFAULT_CONTEXT.  */
1765 static int
1766 get_nondigit_option (int argc, char *const *argv, intmax_t *default_context)
1767 {
1768   static int prev_digit_optind = -1;
1769   int this_digit_optind, was_digit;
1770   char buf[INT_BUFSIZE_BOUND (intmax_t) + 4];
1771   char *p = buf;
1772   int opt;
1773
1774   was_digit = 0;
1775   this_digit_optind = optind;
1776   while (1)
1777     {
1778       opt = getopt_long (argc, (char **) argv, short_options,
1779                          long_options, NULL);
1780       if ( ! ('0' <= opt && opt <= '9'))
1781         break;
1782
1783       if (prev_digit_optind != this_digit_optind || !was_digit)
1784         {
1785           /* Reset to start another context length argument.  */
1786           p = buf;
1787         }
1788       else
1789         {
1790           /* Suppress trivial leading zeros, to avoid incorrect
1791              diagnostic on strings like 00000000000.  */
1792           p -= buf[0] == '0';
1793         }
1794
1795       if (p == buf + sizeof buf - 4)
1796         {
1797           /* Too many digits.  Append "..." to make context_length_arg
1798              complain about "X...", where X contains the digits seen
1799              so far.  */
1800           strcpy (p, "...");
1801           p += 3;
1802           break;
1803         }
1804       *p++ = opt;
1805
1806       was_digit = 1;
1807       prev_digit_optind = this_digit_optind;
1808       this_digit_optind = optind;
1809     }
1810   if (p != buf)
1811     {
1812       *p = '\0';
1813       context_length_arg (buf, default_context);
1814     }
1815
1816   return opt;
1817 }
1818
1819 /* Parse GREP_COLORS.  The default would look like:
1820      GREP_COLORS='ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36'
1821    with boolean capabilities (ne and rv) unset (i.e., omitted).
1822    No character escaping is needed or supported.  */
1823 static void
1824 parse_grep_colors (void)
1825 {
1826   const char *p;
1827   char *q;
1828   char *name;
1829   char *val;
1830
1831   p = getenv ("GREP_COLORS"); /* Plural! */
1832   if (p == NULL || *p == '\0')
1833     return;
1834
1835   /* Work off a writable copy.  */
1836   q = xstrdup (p);
1837
1838   name = q;
1839   val = NULL;
1840   /* From now on, be well-formed or you're gone.  */
1841   for (;;)
1842     if (*q == ':' || *q == '\0')
1843       {
1844         char c = *q;
1845         struct color_cap const *cap;
1846
1847         *q++ = '\0'; /* Terminate name or val.  */
1848         /* Empty name without val (empty cap)
1849          * won't match and will be ignored.  */
1850         for (cap = color_dict; cap->name; cap++)
1851           if (STREQ (cap->name, name))
1852             break;
1853         /* If name unknown, go on for forward compatibility.  */
1854         if (cap->var && val)
1855           *(cap->var) = val;
1856         if (cap->fct)
1857           cap->fct ();
1858         if (c == '\0')
1859           return;
1860         name = q;
1861         val = NULL;
1862       }
1863     else if (*q == '=')
1864       {
1865         if (q == name || val)
1866           return;
1867         *q++ = '\0'; /* Terminate name.  */
1868         val = q; /* Can be the empty string.  */
1869       }
1870     else if (val == NULL)
1871       q++; /* Accumulate name.  */
1872     else if (*q == ';' || (*q >= '0' && *q <= '9'))
1873       q++; /* Accumulate val.  Protect the terminal from being sent crap.  */
1874     else
1875       return;
1876 }
1877
1878 /* Return true if PAT (of length PATLEN) contains an encoding error.  */
1879 static bool
1880 contains_encoding_error (char const *pat, size_t patlen)
1881 {
1882   mbstate_t mbs = { 0 };
1883   size_t i, charlen;
1884
1885   for (i = 0; i < patlen; i += charlen + (charlen == 0))
1886     {
1887       charlen = mbrlen (pat + i, patlen - i, &mbs);
1888       if ((size_t) -2 <= charlen)
1889         return true;
1890     }
1891   return false;
1892 }
1893
1894 /* Change a pattern for fgrep into grep.  */
1895 static void
1896 fgrep_to_grep_pattern (size_t len, char const *keys,
1897                        size_t *new_len, char **new_keys)
1898 {
1899   char *p = *new_keys = xnmalloc (len + 1, 2);
1900   mbstate_t mb_state = { 0 };
1901   size_t n;
1902
1903   for (; len; keys += n, len -= n)
1904     {
1905       wchar_t wc;
1906       n = mbrtowc (&wc, keys, len, &mb_state);
1907       switch (n)
1908         {
1909         case (size_t) -2:
1910           n = len;
1911           /* Fall through.  */
1912         default:
1913           p = mempcpy (p, keys, n);
1914           break;
1915
1916         case (size_t) -1:
1917           memset (&mb_state, 0, sizeof mb_state);
1918           /* Fall through.  */
1919         case 1:
1920           *p = '\\';
1921           p += strchr ("$*.[\\^", *keys) != NULL;
1922           /* Fall through.  */
1923         case 0:
1924           *p++ = *keys;
1925           n = 1;
1926           break;
1927         }
1928     }
1929
1930   *new_len = p - *new_keys;
1931 }
1932
1933 int
1934 main (int argc, char **argv)
1935 {
1936   char *keys;
1937   size_t keycc, oldcc, keyalloc;
1938   int with_filenames;
1939   size_t cc;
1940   int opt, status, prepended;
1941   int prev_optind, last_recursive;
1942   int fread_errno;
1943   intmax_t default_context;
1944   FILE *fp;
1945   exit_failure = EXIT_TROUBLE;
1946   initialize_main (&argc, &argv);
1947   set_program_name (argv[0]);
1948   program_name = argv[0];
1949
1950   keys = NULL;
1951   keycc = 0;
1952   with_filenames = 0;
1953   eolbyte = '\n';
1954   filename_mask = ~0;
1955
1956   max_count = INTMAX_MAX;
1957
1958   /* The value -1 means to use DEFAULT_CONTEXT. */
1959   out_after = out_before = -1;
1960   /* Default before/after context: changed by -C/-NUM options */
1961   default_context = -1;
1962   /* Changed by -o option */
1963   only_matching = 0;
1964
1965   /* Internationalization. */
1966 #if defined HAVE_SETLOCALE
1967   setlocale (LC_ALL, "");
1968 #endif
1969 #if defined ENABLE_NLS
1970   bindtextdomain (PACKAGE, LOCALEDIR);
1971   textdomain (PACKAGE);
1972 #endif
1973
1974   exit_failure = EXIT_TROUBLE;
1975   atexit (clean_up_stdout);
1976
1977   last_recursive = 0;
1978   prepended = prepend_default_options (getenv ("GREP_OPTIONS"), &argc, &argv);
1979   compile = matchers[0].compile;
1980   execute = matchers[0].execute;
1981
1982   while (prev_optind = optind,
1983          (opt = get_nondigit_option (argc, argv, &default_context)) != -1)
1984     switch (opt)
1985       {
1986       case 'A':
1987         context_length_arg (optarg, &out_after);
1988         break;
1989
1990       case 'B':
1991         context_length_arg (optarg, &out_before);
1992         break;
1993
1994       case 'C':
1995         /* Set output match context, but let any explicit leading or
1996            trailing amount specified with -A or -B stand. */
1997         context_length_arg (optarg, &default_context);
1998         break;
1999
2000       case 'D':
2001         if (STREQ (optarg, "read"))
2002           devices = READ_DEVICES;
2003         else if (STREQ (optarg, "skip"))
2004           devices = SKIP_DEVICES;
2005         else
2006           error (EXIT_TROUBLE, 0, _("unknown devices method"));
2007         break;
2008
2009       case 'E':
2010         setmatcher ("egrep");
2011         break;
2012
2013       case 'F':
2014         setmatcher ("fgrep");
2015         break;
2016
2017       case 'P':
2018         setmatcher ("perl");
2019         break;
2020
2021       case 'G':
2022         setmatcher ("grep");
2023         break;
2024
2025       case 'X': /* undocumented on purpose */
2026         setmatcher (optarg);
2027         break;
2028
2029       case 'H':
2030         with_filenames = 1;
2031         no_filenames = 0;
2032         break;
2033
2034       case 'I':
2035         binary_files = WITHOUT_MATCH_BINARY_FILES;
2036         break;
2037
2038       case 'T':
2039         align_tabs = 1;
2040         break;
2041
2042       case 'U':
2043         dos_binary ();
2044         break;
2045
2046       case 'u':
2047         dos_unix_byte_offsets ();
2048         break;
2049
2050       case 'V':
2051         show_version = 1;
2052         break;
2053
2054       case 'a':
2055         binary_files = TEXT_BINARY_FILES;
2056         break;
2057
2058       case 'b':
2059         out_byte = 1;
2060         break;
2061
2062       case 'c':
2063         count_matches = 1;
2064         break;
2065
2066       case 'd':
2067         directories = XARGMATCH ("--directories", optarg,
2068                                  directories_args, directories_types);
2069         if (directories == RECURSE_DIRECTORIES)
2070           last_recursive = prev_optind;
2071         break;
2072
2073       case 'e':
2074         cc = strlen (optarg);
2075         keys = xrealloc (keys, keycc + cc + 1);
2076         strcpy (&keys[keycc], optarg);
2077         keycc += cc;
2078         keys[keycc++] = '\n';
2079         break;
2080
2081       case 'f':
2082         fp = STREQ (optarg, "-") ? stdin : fopen (optarg, O_TEXT ? "rt" : "r");
2083         if (!fp)
2084           error (EXIT_TROUBLE, errno, "%s", optarg);
2085         for (keyalloc = 1; keyalloc <= keycc + 1; keyalloc *= 2)
2086           ;
2087         keys = xrealloc (keys, keyalloc);
2088         oldcc = keycc;
2089         while ((cc = fread (keys + keycc, 1, keyalloc - 1 - keycc, fp)) != 0)
2090           {
2091             keycc += cc;
2092             if (keycc == keyalloc - 1)
2093               keys = x2nrealloc (keys, &keyalloc, sizeof *keys);
2094           }
2095         fread_errno = errno;
2096         if (ferror (fp))
2097           error (EXIT_TROUBLE, fread_errno, "%s", optarg);
2098         if (fp != stdin)
2099           fclose (fp);
2100         /* Append final newline if file ended in non-newline. */
2101         if (oldcc != keycc && keys[keycc - 1] != '\n')
2102           keys[keycc++] = '\n';
2103         break;
2104
2105       case 'h':
2106         with_filenames = 0;
2107         no_filenames = 1;
2108         break;
2109
2110       case 'i':
2111       case 'y':                 /* For old-timers . . . */
2112         match_icase = 1;
2113         break;
2114
2115       case 'L':
2116         /* Like -l, except list files that don't contain matches.
2117            Inspired by the same option in Hume's gre. */
2118         list_files = -1;
2119         break;
2120
2121       case 'l':
2122         list_files = 1;
2123         break;
2124
2125       case 'm':
2126         switch (xstrtoimax (optarg, 0, 10, &max_count, ""))
2127           {
2128           case LONGINT_OK:
2129           case LONGINT_OVERFLOW:
2130             break;
2131
2132           default:
2133             error (EXIT_TROUBLE, 0, _("invalid max count"));
2134           }
2135         break;
2136
2137       case 'n':
2138         out_line = 1;
2139         break;
2140
2141       case 'o':
2142         only_matching = 1;
2143         break;
2144
2145       case 'q':
2146         exit_on_match = 1;
2147         exit_failure = 0;
2148         break;
2149
2150       case 'R':
2151         fts_options = basic_fts_options | FTS_LOGICAL;
2152         /* Fall through.  */
2153       case 'r':
2154         directories = RECURSE_DIRECTORIES;
2155         last_recursive = prev_optind;
2156         break;
2157
2158       case 's':
2159         suppress_errors = 1;
2160         break;
2161
2162       case 'v':
2163         out_invert = true;
2164         break;
2165
2166       case 'w':
2167         match_words = 1;
2168         break;
2169
2170       case 'x':
2171         match_lines = 1;
2172         break;
2173
2174       case 'Z':
2175         filename_mask = 0;
2176         break;
2177
2178       case 'z':
2179         eolbyte = '\0';
2180         break;
2181
2182       case BINARY_FILES_OPTION:
2183         if (STREQ (optarg, "binary"))
2184           binary_files = BINARY_BINARY_FILES;
2185         else if (STREQ (optarg, "text"))
2186           binary_files = TEXT_BINARY_FILES;
2187         else if (STREQ (optarg, "without-match"))
2188           binary_files = WITHOUT_MATCH_BINARY_FILES;
2189         else
2190           error (EXIT_TROUBLE, 0, _("unknown binary-files type"));
2191         break;
2192
2193       case COLOR_OPTION:
2194         if (optarg)
2195           {
2196             if (!strcasecmp (optarg, "always") || !strcasecmp (optarg, "yes")
2197                 || !strcasecmp (optarg, "force"))
2198               color_option = 1;
2199             else if (!strcasecmp (optarg, "never") || !strcasecmp (optarg, "no")
2200                      || !strcasecmp (optarg, "none"))
2201               color_option = 0;
2202             else if (!strcasecmp (optarg, "auto") || !strcasecmp (optarg, "tty")
2203                      || !strcasecmp (optarg, "if-tty"))
2204               color_option = 2;
2205             else
2206               show_help = 1;
2207           }
2208         else
2209           color_option = 2;
2210         break;
2211
2212       case EXCLUDE_OPTION:
2213       case INCLUDE_OPTION:
2214         if (!excluded_patterns)
2215           excluded_patterns = new_exclude ();
2216         add_exclude (excluded_patterns, optarg,
2217                      (EXCLUDE_WILDCARDS
2218                       | (opt == INCLUDE_OPTION ? EXCLUDE_INCLUDE : 0)));
2219         break;
2220       case EXCLUDE_FROM_OPTION:
2221         if (!excluded_patterns)
2222           excluded_patterns = new_exclude ();
2223         if (add_exclude_file (add_exclude, excluded_patterns, optarg,
2224                               EXCLUDE_WILDCARDS, '\n') != 0)
2225           {
2226             error (EXIT_TROUBLE, errno, "%s", optarg);
2227           }
2228         break;
2229
2230       case EXCLUDE_DIRECTORY_OPTION:
2231         if (!excluded_directory_patterns)
2232           excluded_directory_patterns = new_exclude ();
2233         strip_trailing_slashes (optarg);
2234         add_exclude (excluded_directory_patterns, optarg, EXCLUDE_WILDCARDS);
2235         break;
2236
2237       case GROUP_SEPARATOR_OPTION:
2238         group_separator = optarg;
2239         break;
2240
2241       case LINE_BUFFERED_OPTION:
2242         line_buffered = 1;
2243         break;
2244
2245       case LABEL_OPTION:
2246         label = optarg;
2247         break;
2248
2249       case 0:
2250         /* long options */
2251         break;
2252
2253       default:
2254         usage (EXIT_TROUBLE);
2255         break;
2256
2257       }
2258
2259   if (color_option == 2)
2260     color_option = isatty (STDOUT_FILENO) && should_colorize ();
2261   init_colorize ();
2262
2263   /* POSIX says that -q overrides -l, which in turn overrides the
2264      other output options.  */
2265   if (exit_on_match)
2266     list_files = 0;
2267   if (exit_on_match | list_files)
2268     {
2269       count_matches = 0;
2270       done_on_match = 1;
2271     }
2272   out_quiet = count_matches | done_on_match;
2273
2274   if (out_after < 0)
2275     out_after = default_context;
2276   if (out_before < 0)
2277     out_before = default_context;
2278
2279   if (color_option)
2280     {
2281       /* Legacy.  */
2282       char *userval = getenv ("GREP_COLOR");
2283       if (userval != NULL && *userval != '\0')
2284         selected_match_color = context_match_color = userval;
2285
2286       /* New GREP_COLORS has priority.  */
2287       parse_grep_colors ();
2288     }
2289
2290   if (show_version)
2291     {
2292       version_etc (stdout, program_name, PACKAGE_NAME, VERSION, AUTHORS,
2293                    (char *) NULL);
2294       exit (EXIT_SUCCESS);
2295     }
2296
2297   if (show_help)
2298     usage (EXIT_SUCCESS);
2299
2300   struct stat tmp_stat;
2301   if (fstat (STDOUT_FILENO, &tmp_stat) == 0 && S_ISREG (tmp_stat.st_mode))
2302     out_stat = tmp_stat;
2303
2304   if (keys)
2305     {
2306       if (keycc == 0)
2307         {
2308           /* No keys were specified (e.g. -f /dev/null).  Match nothing.  */
2309           out_invert ^= true;
2310           match_lines = match_words = 0;
2311         }
2312       else
2313         /* Strip trailing newline. */
2314         --keycc;
2315     }
2316   else if (optind < argc)
2317     {
2318       /* A copy must be made in case of an xrealloc() or free() later.  */
2319       keycc = strlen (argv[optind]);
2320       keys = xmemdup (argv[optind++], keycc + 1);
2321     }
2322   else
2323     usage (EXIT_TROUBLE);
2324
2325   /* If fgrep in a multibyte locale, then use grep if either
2326      (1) case is ignored (where grep is typically faster), or
2327      (2) the pattern has an encoding error (where fgrep might not work).  */
2328   if (compile == Fcompile && MB_CUR_MAX > 1
2329       && (match_icase || contains_encoding_error (keys, keycc)))
2330     {
2331       size_t new_keycc;
2332       char *new_keys;
2333       fgrep_to_grep_pattern (keycc, keys, &new_keycc, &new_keys);
2334       free (keys);
2335       keys = new_keys;
2336       keycc = new_keycc;
2337       matcher = "grep";
2338       compile = Gcompile;
2339       execute = EGexecute;
2340     }
2341
2342   if (MB_CUR_MAX > 1)
2343     build_mbclen_cache ();
2344
2345   compile (keys, keycc);
2346   free (keys);
2347
2348   if ((argc - optind > 1 && !no_filenames) || with_filenames)
2349     out_file = 1;
2350
2351 #ifdef SET_BINARY
2352   /* Output is set to binary mode because we shouldn't convert
2353      NL to CR-LF pairs, especially when grepping binary files.  */
2354   if (!isatty (1))
2355     SET_BINARY (1);
2356 #endif
2357
2358   if (max_count == 0)
2359     exit (EXIT_FAILURE);
2360
2361   if (fts_options & FTS_LOGICAL && devices == READ_COMMAND_LINE_DEVICES)
2362     devices = READ_DEVICES;
2363
2364   if (optind < argc)
2365     {
2366       status = 1;
2367       do
2368         status &= grep_command_line_arg (argv[optind]);
2369       while (++optind < argc);
2370     }
2371   else if (directories == RECURSE_DIRECTORIES && prepended < last_recursive)
2372     {
2373       /* Grep through ".", omitting leading "./" from diagnostics.  */
2374       filename_prefix_len = 2;
2375       status = grep_command_line_arg (".");
2376     }
2377   else
2378     status = grep_command_line_arg ("-");
2379
2380   /* We register via atexit() to test stdout.  */
2381   exit (errseen ? EXIT_TROUBLE : status);
2382 }
2383 /* vim:set shiftwidth=2: */