Merge branch 'vendor/MPC'
[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\n<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       if (errno != EINVAL)
1168         suppressible_error (filename, errno);
1169       return 0;
1170     }
1171
1172   not_text = (((binary_files == BINARY_BINARY_FILES && !out_quiet)
1173                || binary_files == WITHOUT_MATCH_BINARY_FILES)
1174               && file_is_binary (bufbeg, buflim - bufbeg, fd, st));
1175   if (not_text && binary_files == WITHOUT_MATCH_BINARY_FILES)
1176     return 0;
1177   done_on_match += not_text;
1178   out_quiet += not_text;
1179
1180   for (;;)
1181     {
1182       lastnl = bufbeg;
1183       if (lastout)
1184         lastout = bufbeg;
1185
1186       beg = bufbeg + save;
1187
1188       /* no more data to scan (eof) except for maybe a residue -> break */
1189       if (beg == buflim)
1190         break;
1191
1192       /* Determine new residue (the length of an incomplete line at the end of
1193          the buffer, 0 means there is no incomplete last line).  */
1194       oldc = beg[-1];
1195       beg[-1] = eol;
1196       /* FIXME: use rawmemrchr if/when it exists, since we have ensured
1197          that this use of memrchr is guaranteed never to return NULL.  */
1198       lim = memrchr (beg - 1, eol, buflim - beg + 1);
1199       ++lim;
1200       beg[-1] = oldc;
1201       if (lim == beg)
1202         lim = beg - residue;
1203       beg -= residue;
1204       residue = buflim - lim;
1205
1206       if (beg < lim)
1207         {
1208           if (outleft)
1209             nlines += grepbuf (beg, lim);
1210           if (pending)
1211             prpending (lim);
1212           if ((!outleft && !pending) || (nlines && done_on_match))
1213             goto finish_grep;
1214         }
1215
1216       /* The last OUT_BEFORE lines at the end of the buffer will be needed as
1217          leading context if there is a matching line at the begin of the
1218          next data. Make beg point to their begin.  */
1219       i = 0;
1220       beg = lim;
1221       while (i < out_before && beg > bufbeg && beg != lastout)
1222         {
1223           ++i;
1224           do
1225             --beg;
1226           while (beg[-1] != eol);
1227         }
1228
1229       /* Detect whether leading context is adjacent to previous output.  */
1230       if (beg != lastout)
1231         lastout = 0;
1232
1233       /* Handle some details and read more data to scan.  */
1234       save = residue + lim - beg;
1235       if (out_byte)
1236         totalcc = add_count (totalcc, buflim - bufbeg - save);
1237       if (out_line)
1238         nlscan (beg);
1239       if (! fillbuf (save, st))
1240         {
1241           suppressible_error (filename, errno);
1242           goto finish_grep;
1243         }
1244     }
1245   if (residue)
1246     {
1247       *buflim++ = eol;
1248       if (outleft)
1249         nlines += grepbuf (bufbeg + save - residue, buflim);
1250       if (pending)
1251         prpending (buflim);
1252     }
1253
1254  finish_grep:
1255   done_on_match -= not_text;
1256   out_quiet -= not_text;
1257   if ((not_text & ~out_quiet) && nlines != 0)
1258     printf (_("Binary file %s matches\n"), filename);
1259   return nlines;
1260 }
1261
1262 static int
1263 grepdirent (FTS *fts, FTSENT *ent, int command_line)
1264 {
1265   int follow, dirdesc;
1266   struct stat *st = ent->fts_statp;
1267   command_line &= ent->fts_level == FTS_ROOTLEVEL;
1268
1269   if (ent->fts_info == FTS_DP)
1270     {
1271       if (directories == RECURSE_DIRECTORIES && command_line)
1272         out_file &= ~ (2 * !no_filenames);
1273       return 1;
1274     }
1275
1276   if (skipped_file (ent->fts_name, command_line,
1277                     (ent->fts_info == FTS_D || ent->fts_info == FTS_DC
1278                      || ent->fts_info == FTS_DNR)))
1279     {
1280       fts_set (fts, ent, FTS_SKIP);
1281       return 1;
1282     }
1283
1284   filename = ent->fts_path + filename_prefix_len;
1285   follow = (fts->fts_options & FTS_LOGICAL
1286             || (fts->fts_options & FTS_COMFOLLOW && command_line));
1287
1288   switch (ent->fts_info)
1289     {
1290     case FTS_D:
1291       if (directories == RECURSE_DIRECTORIES)
1292         {
1293           out_file |= 2 * !no_filenames;
1294           return 1;
1295         }
1296       fts_set (fts, ent, FTS_SKIP);
1297       break;
1298
1299     case FTS_DC:
1300       if (!suppress_errors)
1301         error (0, 0, _("warning: %s: %s"), filename,
1302                _("recursive directory loop"));
1303       return 1;
1304
1305     case FTS_DNR:
1306     case FTS_ERR:
1307     case FTS_NS:
1308       suppressible_error (filename, ent->fts_errno);
1309       return 1;
1310
1311     case FTS_DEFAULT:
1312     case FTS_NSOK:
1313       if (devices == SKIP_DEVICES
1314           || (devices == READ_COMMAND_LINE_DEVICES && !command_line))
1315         {
1316           struct stat st1;
1317           if (! st->st_mode)
1318             {
1319               /* The file type is not already known.  Get the file status
1320                  before opening, since opening might have side effects
1321                  on a device.  */
1322               int flag = follow ? 0 : AT_SYMLINK_NOFOLLOW;
1323               if (fstatat (fts->fts_cwd_fd, ent->fts_accpath, &st1, flag) != 0)
1324                 {
1325                   suppressible_error (filename, errno);
1326                   return 1;
1327                 }
1328               st = &st1;
1329             }
1330           if (is_device_mode (st->st_mode))
1331             return 1;
1332         }
1333       break;
1334
1335     case FTS_F:
1336     case FTS_SLNONE:
1337       break;
1338
1339     case FTS_SL:
1340     case FTS_W:
1341       return 1;
1342
1343     default:
1344       abort ();
1345     }
1346
1347   dirdesc = ((fts->fts_options & (FTS_NOCHDIR | FTS_CWDFD)) == FTS_CWDFD
1348              ? fts->fts_cwd_fd
1349              : AT_FDCWD);
1350   return grepfile (dirdesc, ent->fts_accpath, follow, command_line);
1351 }
1352
1353 static int
1354 grepfile (int dirdesc, char const *name, int follow, int command_line)
1355 {
1356   int desc = openat_safer (dirdesc, name, O_RDONLY | (follow ? 0 : O_NOFOLLOW));
1357   if (desc < 0)
1358     {
1359       if (follow || (errno != ELOOP && errno != EMLINK))
1360         suppressible_error (filename, errno);
1361       return 1;
1362     }
1363   return grepdesc (desc, command_line);
1364 }
1365
1366 static int
1367 grepdesc (int desc, int command_line)
1368 {
1369   intmax_t count;
1370   int status = 1;
1371   struct stat st;
1372
1373   /* Get the file status, possibly for the second time.  This catches
1374      a race condition if the directory entry changes after the
1375      directory entry is read and before the file is opened.  For
1376      example, normally DESC is a directory only at the top level, but
1377      there is an exception if some other process substitutes a
1378      directory for a non-directory while 'grep' is running.  */
1379   if (fstat (desc, &st) != 0)
1380     {
1381       suppressible_error (filename, errno);
1382       goto closeout;
1383     }
1384
1385   if (desc != STDIN_FILENO && command_line
1386       && skipped_file (filename, 1, S_ISDIR (st.st_mode)))
1387     goto closeout;
1388
1389   if (desc != STDIN_FILENO
1390       && directories == RECURSE_DIRECTORIES && S_ISDIR (st.st_mode))
1391     {
1392       /* Traverse the directory starting with its full name, because
1393          unfortunately fts provides no way to traverse the directory
1394          starting from its file descriptor.  */
1395
1396       FTS *fts;
1397       FTSENT *ent;
1398       int opts = fts_options & ~(command_line ? 0 : FTS_COMFOLLOW);
1399       char *fts_arg[2];
1400
1401       /* Close DESC now, to conserve file descriptors if the race
1402          condition occurs many times in a deep recursion.  */
1403       if (close (desc) != 0)
1404         suppressible_error (filename, errno);
1405
1406       fts_arg[0] = (char *) filename;
1407       fts_arg[1] = NULL;
1408       fts = fts_open (fts_arg, opts, NULL);
1409
1410       if (!fts)
1411         xalloc_die ();
1412       while ((ent = fts_read (fts)))
1413         status &= grepdirent (fts, ent, command_line);
1414       if (errno)
1415         suppressible_error (filename, errno);
1416       if (fts_close (fts) != 0)
1417         suppressible_error (filename, errno);
1418       return status;
1419     }
1420   if (desc != STDIN_FILENO
1421       && ((directories == SKIP_DIRECTORIES && S_ISDIR (st.st_mode))
1422           || ((devices == SKIP_DEVICES
1423                || (devices == READ_COMMAND_LINE_DEVICES && !command_line))
1424               && is_device_mode (st.st_mode))))
1425     goto closeout;
1426
1427   /* If there is a regular file on stdout and the current file refers
1428      to the same i-node, we have to report the problem and skip it.
1429      Otherwise when matching lines from some other input reach the
1430      disk before we open this file, we can end up reading and matching
1431      those lines and appending them to the file from which we're reading.
1432      Then we'd have what appears to be an infinite loop that'd terminate
1433      only upon filling the output file system or reaching a quota.
1434      However, there is no risk of an infinite loop if grep is generating
1435      no output, i.e., with --silent, --quiet, -q.
1436      Similarly, with any of these:
1437        --max-count=N (-m) (for N >= 2)
1438        --files-with-matches (-l)
1439        --files-without-match (-L)
1440      there is no risk of trouble.
1441      For --max-count=1, grep stops after printing the first match,
1442      so there is no risk of malfunction.  But even --max-count=2, with
1443      input==output, while there is no risk of infloop, there is a race
1444      condition that could result in "alternate" output.  */
1445   if (!out_quiet && list_files == 0 && 1 < max_count
1446       && S_ISREG (out_stat.st_mode) && out_stat.st_ino
1447       && SAME_INODE (st, out_stat))
1448     {
1449       if (! suppress_errors)
1450         error (0, 0, _("input file %s is also the output"), quote (filename));
1451       errseen = 1;
1452       goto closeout;
1453     }
1454
1455 #if defined SET_BINARY
1456   /* Set input to binary mode.  Pipes are simulated with files
1457      on DOS, so this includes the case of "foo | grep bar".  */
1458   if (!isatty (desc))
1459     SET_BINARY (desc);
1460 #endif
1461
1462   count = grep (desc, &st);
1463   if (count < 0)
1464     status = count + 2;
1465   else
1466     {
1467       if (count_matches)
1468         {
1469           if (out_file)
1470             {
1471               print_filename ();
1472               if (filename_mask)
1473                 print_sep (SEP_CHAR_SELECTED);
1474               else
1475                 fputc (0, stdout);
1476             }
1477           printf ("%" PRIdMAX "\n", count);
1478         }
1479
1480       status = !count;
1481       if (list_files == 1 - 2 * status)
1482         {
1483           print_filename ();
1484           fputc ('\n' & filename_mask, stdout);
1485         }
1486
1487       if (desc == STDIN_FILENO)
1488         {
1489           off_t required_offset = outleft ? bufoffset : after_last_match;
1490           if (required_offset != bufoffset
1491               && lseek (desc, required_offset, SEEK_SET) < 0
1492               && S_ISREG (st.st_mode))
1493             suppressible_error (filename, errno);
1494         }
1495     }
1496
1497  closeout:
1498   if (desc != STDIN_FILENO && close (desc) != 0)
1499     suppressible_error (filename, errno);
1500   return status;
1501 }
1502
1503 static int
1504 grep_command_line_arg (char const *arg)
1505 {
1506   if (STREQ (arg, "-"))
1507     {
1508       filename = label ? label : _("(standard input)");
1509       return grepdesc (STDIN_FILENO, 1);
1510     }
1511   else
1512     {
1513       filename = arg;
1514       return grepfile (AT_FDCWD, arg, 1, 1);
1515     }
1516 }
1517
1518 _Noreturn void usage (int);
1519 void
1520 usage (int status)
1521 {
1522   if (status != 0)
1523     {
1524       fprintf (stderr, _("Usage: %s [OPTION]... PATTERN [FILE]...\n"),
1525                program_name);
1526       fprintf (stderr, _("Try '%s --help' for more information.\n"),
1527                program_name);
1528     }
1529   else
1530     {
1531       printf (_("Usage: %s [OPTION]... PATTERN [FILE]...\n"), program_name);
1532       printf (_("Search for PATTERN in each FILE or standard input.\n"));
1533       printf (_("PATTERN is, by default, a basic regular expression (BRE).\n"));
1534       printf (_("\
1535 Example: %s -i 'hello world' menu.h main.c\n\
1536 \n\
1537 Regexp selection and interpretation:\n"), program_name);
1538       printf (_("\
1539   -E, --extended-regexp     PATTERN is an extended regular expression (ERE)\n\
1540   -F, --fixed-strings       PATTERN is a set of newline-separated fixed strings\n\
1541   -G, --basic-regexp        PATTERN is a basic regular expression (BRE)\n\
1542   -P, --perl-regexp         PATTERN is a Perl regular expression\n"));
1543   /* -X is undocumented on purpose. */
1544       printf (_("\
1545   -e, --regexp=PATTERN      use PATTERN for matching\n\
1546   -f, --file=FILE           obtain PATTERN from FILE\n\
1547   -i, --ignore-case         ignore case distinctions\n\
1548   -w, --word-regexp         force PATTERN to match only whole words\n\
1549   -x, --line-regexp         force PATTERN to match only whole lines\n\
1550   -z, --null-data           a data line ends in 0 byte, not newline\n"));
1551       printf (_("\
1552 \n\
1553 Miscellaneous:\n\
1554   -s, --no-messages         suppress error messages\n\
1555   -v, --invert-match        select non-matching lines\n\
1556   -V, --version             display version information and exit\n\
1557       --help                display this help text and exit\n"));
1558       printf (_("\
1559 \n\
1560 Output control:\n\
1561   -m, --max-count=NUM       stop after NUM matches\n\
1562   -b, --byte-offset         print the byte offset with output lines\n\
1563   -n, --line-number         print line number with output lines\n\
1564       --line-buffered       flush output on every line\n\
1565   -H, --with-filename       print the file name for each match\n\
1566   -h, --no-filename         suppress the file name prefix on output\n\
1567       --label=LABEL         use LABEL as the standard input file name prefix\n\
1568 "));
1569       printf (_("\
1570   -o, --only-matching       show only the part of a line matching PATTERN\n\
1571   -q, --quiet, --silent     suppress all normal output\n\
1572       --binary-files=TYPE   assume that binary files are TYPE;\n\
1573                             TYPE is 'binary', 'text', or 'without-match'\n\
1574   -a, --text                equivalent to --binary-files=text\n\
1575 "));
1576       printf (_("\
1577   -I                        equivalent to --binary-files=without-match\n\
1578   -d, --directories=ACTION  how to handle directories;\n\
1579                             ACTION is 'read', 'recurse', or 'skip'\n\
1580   -D, --devices=ACTION      how to handle devices, FIFOs and sockets;\n\
1581                             ACTION is 'read' or 'skip'\n\
1582   -r, --recursive           like --directories=recurse\n\
1583   -R, --dereference-recursive  likewise, but follow all symlinks\n\
1584 "));
1585       printf (_("\
1586       --include=FILE_PATTERN  search only files that match FILE_PATTERN\n\
1587       --exclude=FILE_PATTERN  skip files and directories matching FILE_PATTERN\n\
1588       --exclude-from=FILE   skip files matching any file pattern from FILE\n\
1589       --exclude-dir=PATTERN  directories that match PATTERN will be skipped.\n\
1590 "));
1591       printf (_("\
1592   -L, --files-without-match  print only names of FILEs containing no match\n\
1593   -l, --files-with-matches  print only names of FILEs containing matches\n\
1594   -c, --count               print only a count of matching lines per FILE\n\
1595   -T, --initial-tab         make tabs line up (if needed)\n\
1596   -Z, --null                print 0 byte after FILE name\n"));
1597       printf (_("\
1598 \n\
1599 Context control:\n\
1600   -B, --before-context=NUM  print NUM lines of leading context\n\
1601   -A, --after-context=NUM   print NUM lines of trailing context\n\
1602   -C, --context=NUM         print NUM lines of output context\n\
1603 "));
1604       printf (_("\
1605   -NUM                      same as --context=NUM\n\
1606       --color[=WHEN],\n\
1607       --colour[=WHEN]       use markers to highlight the matching strings;\n\
1608                             WHEN is 'always', 'never', or 'auto'\n\
1609   -U, --binary              do not strip CR characters at EOL (MSDOS/Windows)\n\
1610   -u, --unix-byte-offsets   report offsets as if CRs were not there\n\
1611                             (MSDOS/Windows)\n\
1612 \n"));
1613       printf (_("\
1614 'egrep' means 'grep -E'.  'fgrep' means 'grep -F'.\n\
1615 Direct invocation as either 'egrep' or 'fgrep' is deprecated.\n"));
1616       printf (_("\
1617 When FILE is -, read standard input.  With no FILE, read . if a command-line\n\
1618 -r is given, - otherwise.  If fewer than two FILEs are given, assume -h.\n\
1619 Exit status is 0 if any line is selected, 1 otherwise;\n\
1620 if any error occurs and -q is not given, the exit status is 2.\n"));
1621       printf (_("\nReport bugs to: %s\n"), PACKAGE_BUGREPORT);
1622       printf (_("GNU Grep home page: <%s>\n"),
1623               "http://www.gnu.org/software/grep/");
1624       fputs (_("General help using GNU software: <http://www.gnu.org/gethelp/>\n"),
1625              stdout);
1626
1627     }
1628   exit (status);
1629 }
1630
1631 /* Pattern compilers and matchers.  */
1632
1633 static void
1634 Gcompile (char const *pattern, size_t size)
1635 {
1636   GEAcompile (pattern, size, RE_SYNTAX_GREP | RE_NO_EMPTY_RANGES);
1637 }
1638
1639 static void
1640 Ecompile (char const *pattern, size_t size)
1641 {
1642   GEAcompile (pattern, size, RE_SYNTAX_POSIX_EGREP | RE_NO_EMPTY_RANGES);
1643 }
1644
1645 static void
1646 Acompile (char const *pattern, size_t size)
1647 {
1648   GEAcompile (pattern, size, RE_SYNTAX_AWK);
1649 }
1650
1651 static void
1652 GAcompile (char const *pattern, size_t size)
1653 {
1654   GEAcompile (pattern, size, RE_SYNTAX_GNU_AWK);
1655 }
1656
1657 static void
1658 PAcompile (char const *pattern, size_t size)
1659 {
1660   GEAcompile (pattern, size, RE_SYNTAX_POSIX_AWK);
1661 }
1662
1663 struct matcher
1664 {
1665   char const name[16];
1666   compile_fp_t compile;
1667   execute_fp_t execute;
1668 };
1669 static struct matcher const matchers[] = {
1670   { "grep",      Gcompile, EGexecute },
1671   { "egrep",     Ecompile, EGexecute },
1672   { "fgrep",     Fcompile,  Fexecute },
1673   { "awk",       Acompile, EGexecute },
1674   { "gawk",     GAcompile, EGexecute },
1675   { "posixawk", PAcompile, EGexecute },
1676   { "perl",      Pcompile,  Pexecute },
1677   { "", NULL, NULL },
1678 };
1679
1680 /* Set the matcher to M if available.  Exit in case of conflicts or if
1681    M is not available.  */
1682 static void
1683 setmatcher (char const *m)
1684 {
1685   struct matcher const *p;
1686
1687   if (matcher && !STREQ (matcher, m))
1688     error (EXIT_TROUBLE, 0, _("conflicting matchers specified"));
1689
1690   for (p = matchers; p->compile; p++)
1691     if (STREQ (m, p->name))
1692       {
1693         matcher = p->name;
1694         compile = p->compile;
1695         execute = p->execute;
1696         return;
1697       }
1698
1699   error (EXIT_TROUBLE, 0, _("invalid matcher %s"), m);
1700 }
1701
1702 /* Find the white-space-separated options specified by OPTIONS, and
1703    using BUF to store copies of these options, set ARGV[0], ARGV[1],
1704    etc. to the option copies.  Return the number N of options found.
1705    Do not set ARGV[N] to NULL.  If ARGV is NULL, do not store ARGV[0]
1706    etc.  Backslash can be used to escape whitespace (and backslashes).  */
1707 static size_t
1708 prepend_args (char const *options, char *buf, char **argv)
1709 {
1710   char const *o = options;
1711   char *b = buf;
1712   size_t n = 0;
1713
1714   for (;;)
1715     {
1716       while (c_isspace (to_uchar (*o)))
1717         o++;
1718       if (!*o)
1719         return n;
1720       if (argv)
1721         argv[n] = b;
1722       n++;
1723
1724       do
1725         if ((*b++ = *o++) == '\\' && *o)
1726           b[-1] = *o++;
1727       while (*o && ! c_isspace (to_uchar (*o)));
1728
1729       *b++ = '\0';
1730     }
1731 }
1732
1733 /* Prepend the whitespace-separated options in OPTIONS to the argument
1734    vector of a main program with argument count *PARGC and argument
1735    vector *PARGV.  Return the number of options prepended.  */
1736 static int
1737 prepend_default_options (char const *options, int *pargc, char ***pargv)
1738 {
1739   if (options && *options)
1740     {
1741       char *buf = xmalloc (strlen (options) + 1);
1742       size_t prepended = prepend_args (options, buf, NULL);
1743       int argc = *pargc;
1744       char *const *argv = *pargv;
1745       char **pp;
1746       enum { MAX_ARGS = MIN (INT_MAX, SIZE_MAX / sizeof *pp - 1) };
1747       if (MAX_ARGS - argc < prepended)
1748         xalloc_die ();
1749       pp = xmalloc ((prepended + argc + 1) * sizeof *pp);
1750       *pargc = prepended + argc;
1751       *pargv = pp;
1752       *pp++ = *argv++;
1753       pp += prepend_args (options, buf, pp);
1754       while ((*pp++ = *argv++))
1755         continue;
1756       return prepended;
1757     }
1758
1759   return 0;
1760 }
1761
1762 /* Get the next non-digit option from ARGC and ARGV.
1763    Return -1 if there are no more options.
1764    Process any digit options that were encountered on the way,
1765    and store the resulting integer into *DEFAULT_CONTEXT.  */
1766 static int
1767 get_nondigit_option (int argc, char *const *argv, intmax_t *default_context)
1768 {
1769   static int prev_digit_optind = -1;
1770   int this_digit_optind, was_digit;
1771   char buf[INT_BUFSIZE_BOUND (intmax_t) + 4];
1772   char *p = buf;
1773   int opt;
1774
1775   was_digit = 0;
1776   this_digit_optind = optind;
1777   while (1)
1778     {
1779       opt = getopt_long (argc, (char **) argv, short_options,
1780                          long_options, NULL);
1781       if ( ! ('0' <= opt && opt <= '9'))
1782         break;
1783
1784       if (prev_digit_optind != this_digit_optind || !was_digit)
1785         {
1786           /* Reset to start another context length argument.  */
1787           p = buf;
1788         }
1789       else
1790         {
1791           /* Suppress trivial leading zeros, to avoid incorrect
1792              diagnostic on strings like 00000000000.  */
1793           p -= buf[0] == '0';
1794         }
1795
1796       if (p == buf + sizeof buf - 4)
1797         {
1798           /* Too many digits.  Append "..." to make context_length_arg
1799              complain about "X...", where X contains the digits seen
1800              so far.  */
1801           strcpy (p, "...");
1802           p += 3;
1803           break;
1804         }
1805       *p++ = opt;
1806
1807       was_digit = 1;
1808       prev_digit_optind = this_digit_optind;
1809       this_digit_optind = optind;
1810     }
1811   if (p != buf)
1812     {
1813       *p = '\0';
1814       context_length_arg (buf, default_context);
1815     }
1816
1817   return opt;
1818 }
1819
1820 /* Parse GREP_COLORS.  The default would look like:
1821      GREP_COLORS='ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36'
1822    with boolean capabilities (ne and rv) unset (i.e., omitted).
1823    No character escaping is needed or supported.  */
1824 static void
1825 parse_grep_colors (void)
1826 {
1827   const char *p;
1828   char *q;
1829   char *name;
1830   char *val;
1831
1832   p = getenv ("GREP_COLORS"); /* Plural! */
1833   if (p == NULL || *p == '\0')
1834     return;
1835
1836   /* Work off a writable copy.  */
1837   q = xstrdup (p);
1838
1839   name = q;
1840   val = NULL;
1841   /* From now on, be well-formed or you're gone.  */
1842   for (;;)
1843     if (*q == ':' || *q == '\0')
1844       {
1845         char c = *q;
1846         struct color_cap const *cap;
1847
1848         *q++ = '\0'; /* Terminate name or val.  */
1849         /* Empty name without val (empty cap)
1850          * won't match and will be ignored.  */
1851         for (cap = color_dict; cap->name; cap++)
1852           if (STREQ (cap->name, name))
1853             break;
1854         /* If name unknown, go on for forward compatibility.  */
1855         if (cap->var && val)
1856           *(cap->var) = val;
1857         if (cap->fct)
1858           cap->fct ();
1859         if (c == '\0')
1860           return;
1861         name = q;
1862         val = NULL;
1863       }
1864     else if (*q == '=')
1865       {
1866         if (q == name || val)
1867           return;
1868         *q++ = '\0'; /* Terminate name.  */
1869         val = q; /* Can be the empty string.  */
1870       }
1871     else if (val == NULL)
1872       q++; /* Accumulate name.  */
1873     else if (*q == ';' || (*q >= '0' && *q <= '9'))
1874       q++; /* Accumulate val.  Protect the terminal from being sent crap.  */
1875     else
1876       return;
1877 }
1878
1879 /* Return true if PAT (of length PATLEN) contains an encoding error.  */
1880 static bool
1881 contains_encoding_error (char const *pat, size_t patlen)
1882 {
1883   mbstate_t mbs = { 0 };
1884   size_t i, charlen;
1885
1886   for (i = 0; i < patlen; i += charlen + (charlen == 0))
1887     {
1888       charlen = mbrlen (pat + i, patlen - i, &mbs);
1889       if ((size_t) -2 <= charlen)
1890         return true;
1891     }
1892   return false;
1893 }
1894
1895 /* Change a pattern for fgrep into grep.  */
1896 static void
1897 fgrep_to_grep_pattern (size_t len, char const *keys,
1898                        size_t *new_len, char **new_keys)
1899 {
1900   char *p = *new_keys = xnmalloc (len + 1, 2);
1901   mbstate_t mb_state = { 0 };
1902   size_t n;
1903
1904   for (; len; keys += n, len -= n)
1905     {
1906       wchar_t wc;
1907       n = mbrtowc (&wc, keys, len, &mb_state);
1908       switch (n)
1909         {
1910         case (size_t) -2:
1911           n = len;
1912           /* Fall through.  */
1913         default:
1914           p = mempcpy (p, keys, n);
1915           break;
1916
1917         case (size_t) -1:
1918           memset (&mb_state, 0, sizeof mb_state);
1919           /* Fall through.  */
1920         case 1:
1921           *p = '\\';
1922           p += strchr ("$*.[\\^", *keys) != NULL;
1923           /* Fall through.  */
1924         case 0:
1925           *p++ = *keys;
1926           n = 1;
1927           break;
1928         }
1929     }
1930
1931   *new_len = p - *new_keys;
1932 }
1933
1934 int
1935 main (int argc, char **argv)
1936 {
1937   char *keys;
1938   size_t keycc, oldcc, keyalloc;
1939   int with_filenames;
1940   size_t cc;
1941   int opt, status, prepended;
1942   int prev_optind, last_recursive;
1943   int fread_errno;
1944   intmax_t default_context;
1945   FILE *fp;
1946   exit_failure = EXIT_TROUBLE;
1947   initialize_main (&argc, &argv);
1948   set_program_name (argv[0]);
1949   program_name = argv[0];
1950
1951   keys = NULL;
1952   keycc = 0;
1953   with_filenames = 0;
1954   eolbyte = '\n';
1955   filename_mask = ~0;
1956
1957   max_count = INTMAX_MAX;
1958
1959   /* The value -1 means to use DEFAULT_CONTEXT. */
1960   out_after = out_before = -1;
1961   /* Default before/after context: changed by -C/-NUM options */
1962   default_context = -1;
1963   /* Changed by -o option */
1964   only_matching = 0;
1965
1966   /* Internationalization. */
1967 #if defined HAVE_SETLOCALE
1968   setlocale (LC_ALL, "");
1969 #endif
1970 #if defined ENABLE_NLS
1971   bindtextdomain (PACKAGE, LOCALEDIR);
1972   textdomain (PACKAGE);
1973 #endif
1974
1975   exit_failure = EXIT_TROUBLE;
1976   atexit (clean_up_stdout);
1977
1978   last_recursive = 0;
1979   prepended = prepend_default_options (getenv ("GREP_OPTIONS"), &argc, &argv);
1980   compile = matchers[0].compile;
1981   execute = matchers[0].execute;
1982
1983   while (prev_optind = optind,
1984          (opt = get_nondigit_option (argc, argv, &default_context)) != -1)
1985     switch (opt)
1986       {
1987       case 'A':
1988         context_length_arg (optarg, &out_after);
1989         break;
1990
1991       case 'B':
1992         context_length_arg (optarg, &out_before);
1993         break;
1994
1995       case 'C':
1996         /* Set output match context, but let any explicit leading or
1997            trailing amount specified with -A or -B stand. */
1998         context_length_arg (optarg, &default_context);
1999         break;
2000
2001       case 'D':
2002         if (STREQ (optarg, "read"))
2003           devices = READ_DEVICES;
2004         else if (STREQ (optarg, "skip"))
2005           devices = SKIP_DEVICES;
2006         else
2007           error (EXIT_TROUBLE, 0, _("unknown devices method"));
2008         break;
2009
2010       case 'E':
2011         setmatcher ("egrep");
2012         break;
2013
2014       case 'F':
2015         setmatcher ("fgrep");
2016         break;
2017
2018       case 'P':
2019         setmatcher ("perl");
2020         break;
2021
2022       case 'G':
2023         setmatcher ("grep");
2024         break;
2025
2026       case 'X': /* undocumented on purpose */
2027         setmatcher (optarg);
2028         break;
2029
2030       case 'H':
2031         with_filenames = 1;
2032         no_filenames = 0;
2033         break;
2034
2035       case 'I':
2036         binary_files = WITHOUT_MATCH_BINARY_FILES;
2037         break;
2038
2039       case 'T':
2040         align_tabs = 1;
2041         break;
2042
2043       case 'U':
2044         dos_binary ();
2045         break;
2046
2047       case 'u':
2048         dos_unix_byte_offsets ();
2049         break;
2050
2051       case 'V':
2052         show_version = 1;
2053         break;
2054
2055       case 'a':
2056         binary_files = TEXT_BINARY_FILES;
2057         break;
2058
2059       case 'b':
2060         out_byte = 1;
2061         break;
2062
2063       case 'c':
2064         count_matches = 1;
2065         break;
2066
2067       case 'd':
2068         directories = XARGMATCH ("--directories", optarg,
2069                                  directories_args, directories_types);
2070         if (directories == RECURSE_DIRECTORIES)
2071           last_recursive = prev_optind;
2072         break;
2073
2074       case 'e':
2075         cc = strlen (optarg);
2076         keys = xrealloc (keys, keycc + cc + 1);
2077         strcpy (&keys[keycc], optarg);
2078         keycc += cc;
2079         keys[keycc++] = '\n';
2080         break;
2081
2082       case 'f':
2083         fp = STREQ (optarg, "-") ? stdin : fopen (optarg, O_TEXT ? "rt" : "r");
2084         if (!fp)
2085           error (EXIT_TROUBLE, errno, "%s", optarg);
2086         for (keyalloc = 1; keyalloc <= keycc + 1; keyalloc *= 2)
2087           ;
2088         keys = xrealloc (keys, keyalloc);
2089         oldcc = keycc;
2090         while ((cc = fread (keys + keycc, 1, keyalloc - 1 - keycc, fp)) != 0)
2091           {
2092             keycc += cc;
2093             if (keycc == keyalloc - 1)
2094               keys = x2nrealloc (keys, &keyalloc, sizeof *keys);
2095           }
2096         fread_errno = errno;
2097         if (ferror (fp))
2098           error (EXIT_TROUBLE, fread_errno, "%s", optarg);
2099         if (fp != stdin)
2100           fclose (fp);
2101         /* Append final newline if file ended in non-newline. */
2102         if (oldcc != keycc && keys[keycc - 1] != '\n')
2103           keys[keycc++] = '\n';
2104         break;
2105
2106       case 'h':
2107         with_filenames = 0;
2108         no_filenames = 1;
2109         break;
2110
2111       case 'i':
2112       case 'y':                 /* For old-timers . . . */
2113         match_icase = 1;
2114         break;
2115
2116       case 'L':
2117         /* Like -l, except list files that don't contain matches.
2118            Inspired by the same option in Hume's gre. */
2119         list_files = -1;
2120         break;
2121
2122       case 'l':
2123         list_files = 1;
2124         break;
2125
2126       case 'm':
2127         switch (xstrtoimax (optarg, 0, 10, &max_count, ""))
2128           {
2129           case LONGINT_OK:
2130           case LONGINT_OVERFLOW:
2131             break;
2132
2133           default:
2134             error (EXIT_TROUBLE, 0, _("invalid max count"));
2135           }
2136         break;
2137
2138       case 'n':
2139         out_line = 1;
2140         break;
2141
2142       case 'o':
2143         only_matching = 1;
2144         break;
2145
2146       case 'q':
2147         exit_on_match = 1;
2148         exit_failure = 0;
2149         break;
2150
2151       case 'R':
2152         fts_options = basic_fts_options | FTS_LOGICAL;
2153         /* Fall through.  */
2154       case 'r':
2155         directories = RECURSE_DIRECTORIES;
2156         last_recursive = prev_optind;
2157         break;
2158
2159       case 's':
2160         suppress_errors = 1;
2161         break;
2162
2163       case 'v':
2164         out_invert = true;
2165         break;
2166
2167       case 'w':
2168         match_words = 1;
2169         break;
2170
2171       case 'x':
2172         match_lines = 1;
2173         break;
2174
2175       case 'Z':
2176         filename_mask = 0;
2177         break;
2178
2179       case 'z':
2180         eolbyte = '\0';
2181         break;
2182
2183       case BINARY_FILES_OPTION:
2184         if (STREQ (optarg, "binary"))
2185           binary_files = BINARY_BINARY_FILES;
2186         else if (STREQ (optarg, "text"))
2187           binary_files = TEXT_BINARY_FILES;
2188         else if (STREQ (optarg, "without-match"))
2189           binary_files = WITHOUT_MATCH_BINARY_FILES;
2190         else
2191           error (EXIT_TROUBLE, 0, _("unknown binary-files type"));
2192         break;
2193
2194       case COLOR_OPTION:
2195         if (optarg)
2196           {
2197             if (!strcasecmp (optarg, "always") || !strcasecmp (optarg, "yes")
2198                 || !strcasecmp (optarg, "force"))
2199               color_option = 1;
2200             else if (!strcasecmp (optarg, "never") || !strcasecmp (optarg, "no")
2201                      || !strcasecmp (optarg, "none"))
2202               color_option = 0;
2203             else if (!strcasecmp (optarg, "auto") || !strcasecmp (optarg, "tty")
2204                      || !strcasecmp (optarg, "if-tty"))
2205               color_option = 2;
2206             else
2207               show_help = 1;
2208           }
2209         else
2210           color_option = 2;
2211         break;
2212
2213       case EXCLUDE_OPTION:
2214       case INCLUDE_OPTION:
2215         if (!excluded_patterns)
2216           excluded_patterns = new_exclude ();
2217         add_exclude (excluded_patterns, optarg,
2218                      (EXCLUDE_WILDCARDS
2219                       | (opt == INCLUDE_OPTION ? EXCLUDE_INCLUDE : 0)));
2220         break;
2221       case EXCLUDE_FROM_OPTION:
2222         if (!excluded_patterns)
2223           excluded_patterns = new_exclude ();
2224         if (add_exclude_file (add_exclude, excluded_patterns, optarg,
2225                               EXCLUDE_WILDCARDS, '\n') != 0)
2226           {
2227             error (EXIT_TROUBLE, errno, "%s", optarg);
2228           }
2229         break;
2230
2231       case EXCLUDE_DIRECTORY_OPTION:
2232         if (!excluded_directory_patterns)
2233           excluded_directory_patterns = new_exclude ();
2234         strip_trailing_slashes (optarg);
2235         add_exclude (excluded_directory_patterns, optarg, EXCLUDE_WILDCARDS);
2236         break;
2237
2238       case GROUP_SEPARATOR_OPTION:
2239         group_separator = optarg;
2240         break;
2241
2242       case LINE_BUFFERED_OPTION:
2243         line_buffered = 1;
2244         break;
2245
2246       case LABEL_OPTION:
2247         label = optarg;
2248         break;
2249
2250       case 0:
2251         /* long options */
2252         break;
2253
2254       default:
2255         usage (EXIT_TROUBLE);
2256         break;
2257
2258       }
2259
2260   if (color_option == 2)
2261     color_option = isatty (STDOUT_FILENO) && should_colorize ();
2262   init_colorize ();
2263
2264   /* POSIX says that -q overrides -l, which in turn overrides the
2265      other output options.  */
2266   if (exit_on_match)
2267     list_files = 0;
2268   if (exit_on_match | list_files)
2269     {
2270       count_matches = 0;
2271       done_on_match = 1;
2272     }
2273   out_quiet = count_matches | done_on_match;
2274
2275   if (out_after < 0)
2276     out_after = default_context;
2277   if (out_before < 0)
2278     out_before = default_context;
2279
2280   if (color_option)
2281     {
2282       /* Legacy.  */
2283       char *userval = getenv ("GREP_COLOR");
2284       if (userval != NULL && *userval != '\0')
2285         selected_match_color = context_match_color = userval;
2286
2287       /* New GREP_COLORS has priority.  */
2288       parse_grep_colors ();
2289     }
2290
2291   if (show_version)
2292     {
2293       version_etc (stdout, program_name, PACKAGE_NAME, VERSION, AUTHORS,
2294                    (char *) NULL);
2295       exit (EXIT_SUCCESS);
2296     }
2297
2298   if (show_help)
2299     usage (EXIT_SUCCESS);
2300
2301   struct stat tmp_stat;
2302   if (fstat (STDOUT_FILENO, &tmp_stat) == 0 && S_ISREG (tmp_stat.st_mode))
2303     out_stat = tmp_stat;
2304
2305   if (keys)
2306     {
2307       if (keycc == 0)
2308         {
2309           /* No keys were specified (e.g. -f /dev/null).  Match nothing.  */
2310           out_invert ^= true;
2311           match_lines = match_words = 0;
2312         }
2313       else
2314         /* Strip trailing newline. */
2315         --keycc;
2316     }
2317   else if (optind < argc)
2318     {
2319       /* A copy must be made in case of an xrealloc() or free() later.  */
2320       keycc = strlen (argv[optind]);
2321       keys = xmemdup (argv[optind++], keycc + 1);
2322     }
2323   else
2324     usage (EXIT_TROUBLE);
2325
2326   /* If fgrep in a multibyte locale, then use grep if either
2327      (1) case is ignored (where grep is typically faster), or
2328      (2) the pattern has an encoding error (where fgrep might not work).  */
2329   if (compile == Fcompile && MB_CUR_MAX > 1
2330       && (match_icase || contains_encoding_error (keys, keycc)))
2331     {
2332       size_t new_keycc;
2333       char *new_keys;
2334       fgrep_to_grep_pattern (keycc, keys, &new_keycc, &new_keys);
2335       free (keys);
2336       keys = new_keys;
2337       keycc = new_keycc;
2338       matcher = "grep";
2339       compile = Gcompile;
2340       execute = EGexecute;
2341     }
2342
2343   if (MB_CUR_MAX > 1)
2344     build_mbclen_cache ();
2345
2346   compile (keys, keycc);
2347   free (keys);
2348
2349   if ((argc - optind > 1 && !no_filenames) || with_filenames)
2350     out_file = 1;
2351
2352 #ifdef SET_BINARY
2353   /* Output is set to binary mode because we shouldn't convert
2354      NL to CR-LF pairs, especially when grepping binary files.  */
2355   if (!isatty (1))
2356     SET_BINARY (1);
2357 #endif
2358
2359   if (max_count == 0)
2360     exit (EXIT_FAILURE);
2361
2362   if (fts_options & FTS_LOGICAL && devices == READ_COMMAND_LINE_DEVICES)
2363     devices = READ_DEVICES;
2364
2365   if (optind < argc)
2366     {
2367       status = 1;
2368       do
2369         status &= grep_command_line_arg (argv[optind]);
2370       while (++optind < argc);
2371     }
2372   else if (directories == RECURSE_DIRECTORIES && prepended < last_recursive)
2373     {
2374       /* Grep through ".", omitting leading "./" from diagnostics.  */
2375       filename_prefix_len = 2;
2376       status = grep_command_line_arg (".");
2377     }
2378   else
2379     status = grep_command_line_arg ("-");
2380
2381   /* We register via atexit() to test stdout.  */
2382   exit (errseen ? EXIT_TROUBLE : status);
2383 }
2384 /* vim:set shiftwidth=2: */