xref: /freebsd/usr.bin/grep/grep.c (revision 4c14980baab5aa678fa8a1ca046892cf30e92d71)
1f3f50de6SPedro F. Giffuni /*	$NetBSD: grep.c,v 1.6 2011/04/18 03:48:23 joerg Exp $	*/
2b66a823bSGabor Kovesdan /* 	$FreeBSD$	*/
34dc88ebeSGabor Kovesdan /*	$OpenBSD: grep.c,v 1.42 2010/07/02 22:18:03 tedu Exp $	*/
44dc88ebeSGabor Kovesdan 
54dc88ebeSGabor Kovesdan /*-
61de7b4b8SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
71de7b4b8SPedro F. Giffuni  *
8a0ef9ad6SDag-Erling Smørgrav  * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
94dc88ebeSGabor Kovesdan  * Copyright (C) 2008-2009 Gabor Kovesdan <gabor@FreeBSD.org>
104dc88ebeSGabor Kovesdan  * All rights reserved.
114dc88ebeSGabor Kovesdan  *
124dc88ebeSGabor Kovesdan  * Redistribution and use in source and binary forms, with or without
134dc88ebeSGabor Kovesdan  * modification, are permitted provided that the following conditions
144dc88ebeSGabor Kovesdan  * are met:
154dc88ebeSGabor Kovesdan  * 1. Redistributions of source code must retain the above copyright
164dc88ebeSGabor Kovesdan  *    notice, this list of conditions and the following disclaimer.
174dc88ebeSGabor Kovesdan  * 2. Redistributions in binary form must reproduce the above copyright
184dc88ebeSGabor Kovesdan  *    notice, this list of conditions and the following disclaimer in the
194dc88ebeSGabor Kovesdan  *    documentation and/or other materials provided with the distribution.
204dc88ebeSGabor Kovesdan  *
214dc88ebeSGabor Kovesdan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
224dc88ebeSGabor Kovesdan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
234dc88ebeSGabor Kovesdan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
244dc88ebeSGabor Kovesdan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
254dc88ebeSGabor Kovesdan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264dc88ebeSGabor Kovesdan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
274dc88ebeSGabor Kovesdan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
284dc88ebeSGabor Kovesdan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
294dc88ebeSGabor Kovesdan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
304dc88ebeSGabor Kovesdan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
314dc88ebeSGabor Kovesdan  * SUCH DAMAGE.
324dc88ebeSGabor Kovesdan  */
334dc88ebeSGabor Kovesdan 
344dc88ebeSGabor Kovesdan #include <sys/cdefs.h>
354dc88ebeSGabor Kovesdan __FBSDID("$FreeBSD$");
364dc88ebeSGabor Kovesdan 
374dc88ebeSGabor Kovesdan #include <sys/stat.h>
384dc88ebeSGabor Kovesdan #include <sys/types.h>
394dc88ebeSGabor Kovesdan 
404dc88ebeSGabor Kovesdan #include <ctype.h>
414dc88ebeSGabor Kovesdan #include <err.h>
424dc88ebeSGabor Kovesdan #include <errno.h>
43f20f6f3fSGabor Kovesdan #include <fcntl.h>
444dc88ebeSGabor Kovesdan #include <getopt.h>
454dc88ebeSGabor Kovesdan #include <limits.h>
464dc88ebeSGabor Kovesdan #include <libgen.h>
474dc88ebeSGabor Kovesdan #include <locale.h>
484dc88ebeSGabor Kovesdan #include <stdbool.h>
494dc88ebeSGabor Kovesdan #include <stdio.h>
504dc88ebeSGabor Kovesdan #include <stdlib.h>
514dc88ebeSGabor Kovesdan #include <string.h>
524dc88ebeSGabor Kovesdan #include <unistd.h>
534dc88ebeSGabor Kovesdan 
544dc88ebeSGabor Kovesdan #include "grep.h"
554dc88ebeSGabor Kovesdan 
564dc88ebeSGabor Kovesdan const char	*errstr[] = {
574dc88ebeSGabor Kovesdan 	"",
584dc88ebeSGabor Kovesdan /* 1*/	"(standard input)",
59a1852807SKyle Evans /* 2*/	"unknown %s option",
60*4c14980bSKyle Evans /* 3*/	"usage: %s [-abcDEFGHhIiLlmnOopqRSsUVvwxz] [-A num] [-B num] [-C num]\n",
61a1852807SKyle Evans /* 4*/	"\t[-e pattern] [-f file] [--binary-files=value] [--color=when]\n",
62be6b8b7aSMateusz Piotrowski /* 5*/	"\t[--context=num] [--directories=action] [--label] [--line-buffered]\n",
63a1852807SKyle Evans /* 6*/	"\t[--null] [pattern] [file ...]\n",
64a1852807SKyle Evans /* 7*/	"Binary file %s matches\n",
657c2f310fSKyle Evans /* 8*/	"%s (BSD grep, GNU compatible) %s\n",
664dc88ebeSGabor Kovesdan };
674dc88ebeSGabor Kovesdan 
684dc88ebeSGabor Kovesdan /* Flags passed to regcomp() and regexec() */
698bf46064SEd Maste int		 cflags = REG_NOSUB | REG_NEWLINE;
704dc88ebeSGabor Kovesdan int		 eflags = REG_STARTEND;
714dc88ebeSGabor Kovesdan 
724dc88ebeSGabor Kovesdan bool		 matchall;
734dc88ebeSGabor Kovesdan 
744dc88ebeSGabor Kovesdan /* Searching patterns */
75bf70beceSEd Schouten unsigned int	 patterns;
76bf70beceSEd Schouten static unsigned int pattern_sz;
77f20f6f3fSGabor Kovesdan struct pat	*pattern;
784dc88ebeSGabor Kovesdan regex_t		*r_pattern;
794dc88ebeSGabor Kovesdan 
804dc88ebeSGabor Kovesdan /* Filename exclusion/inclusion patterns */
81bf70beceSEd Schouten unsigned int	fpatterns, dpatterns;
82bf70beceSEd Schouten static unsigned int fpattern_sz, dpattern_sz;
8355e44f51SGabor Kovesdan struct epat	*dpattern, *fpattern;
844dc88ebeSGabor Kovesdan 
854dc88ebeSGabor Kovesdan /* For regex errors  */
864dc88ebeSGabor Kovesdan char	 re_error[RE_ERROR_BUF + 1];
874dc88ebeSGabor Kovesdan 
884dc88ebeSGabor Kovesdan /* Command-line flags */
89b5fc583cSEd Maste long long Aflag;	/* -A x: print x lines trailing each match */
90b5fc583cSEd Maste long long Bflag;	/* -B x: print x lines leading each match */
914dc88ebeSGabor Kovesdan bool	 Hflag;		/* -H: always print file name */
924dc88ebeSGabor Kovesdan bool	 Lflag;		/* -L: only show names of files with no matches */
934dc88ebeSGabor Kovesdan bool	 bflag;		/* -b: show block numbers for each match */
944dc88ebeSGabor Kovesdan bool	 cflag;		/* -c: only show a count of matching lines */
954dc88ebeSGabor Kovesdan bool	 hflag;		/* -h: don't print filename headers */
964dc88ebeSGabor Kovesdan bool	 iflag;		/* -i: ignore case */
974dc88ebeSGabor Kovesdan bool	 lflag;		/* -l: only show names of files with matches */
984dc88ebeSGabor Kovesdan bool	 mflag;		/* -m x: stop reading the files after x matches */
99f20f6f3fSGabor Kovesdan long long mcount;	/* count for -m */
100924500b7SEitan Adler long long mlimit;	/* requested value for -m */
1015ee1ea02SEd Maste char	 fileeol;	/* indicator for eol */
1024dc88ebeSGabor Kovesdan bool	 nflag;		/* -n: show line numbers in front of matching lines */
1034dc88ebeSGabor Kovesdan bool	 oflag;		/* -o: print only matching part */
1044dc88ebeSGabor Kovesdan bool	 qflag;		/* -q: quiet mode (don't output anything) */
1054dc88ebeSGabor Kovesdan bool	 sflag;		/* -s: silent mode (ignore errors) */
1064dc88ebeSGabor Kovesdan bool	 vflag;		/* -v: only show non-matching lines */
1074dc88ebeSGabor Kovesdan bool	 wflag;		/* -w: pattern must start and end on word boundaries */
1084dc88ebeSGabor Kovesdan bool	 xflag;		/* -x: pattern must match entire line */
1094dc88ebeSGabor Kovesdan bool	 lbflag;	/* --line-buffered */
1104dc88ebeSGabor Kovesdan bool	 nullflag;	/* --null */
1114dc88ebeSGabor Kovesdan char	*label;		/* --label */
11227116286SGabor Kovesdan const char *color;	/* --color */
113*4c14980bSKyle Evans int	 grepbehave = GREP_BASIC;	/* -EFG: type of the regex */
1144dc88ebeSGabor Kovesdan int	 binbehave = BINFILE_BIN;	/* -aIU: handling of binary files */
1154a5b4207SBaptiste Daroussin int	 filebehave = FILE_STDIO;
11627116286SGabor Kovesdan int	 devbehave = DEV_READ;		/* -D: handling of devices */
11727116286SGabor Kovesdan int	 dirbehave = DIR_READ;		/* -dRr: handling of directories */
11827116286SGabor Kovesdan int	 linkbehave = LINK_READ;	/* -OpS: handling of symlinks */
1194dc88ebeSGabor Kovesdan 
12059218eb7SGabor Kovesdan bool	 dexclude, dinclude;	/* --exclude-dir and --include-dir */
12159218eb7SGabor Kovesdan bool	 fexclude, finclude;	/* --exclude and --include */
12255e44f51SGabor Kovesdan 
1234dc88ebeSGabor Kovesdan enum {
1244dc88ebeSGabor Kovesdan 	BIN_OPT = CHAR_MAX + 1,
1254dc88ebeSGabor Kovesdan 	COLOR_OPT,
1264dc88ebeSGabor Kovesdan 	HELP_OPT,
1274dc88ebeSGabor Kovesdan 	MMAP_OPT,
1284dc88ebeSGabor Kovesdan 	LINEBUF_OPT,
1294dc88ebeSGabor Kovesdan 	LABEL_OPT,
1304dc88ebeSGabor Kovesdan 	NULL_OPT,
1314dc88ebeSGabor Kovesdan 	R_EXCLUDE_OPT,
1324dc88ebeSGabor Kovesdan 	R_INCLUDE_OPT,
1334dc88ebeSGabor Kovesdan 	R_DEXCLUDE_OPT,
1344dc88ebeSGabor Kovesdan 	R_DINCLUDE_OPT
1354dc88ebeSGabor Kovesdan };
1364dc88ebeSGabor Kovesdan 
13727116286SGabor Kovesdan static inline const char	*init_color(const char *);
13827116286SGabor Kovesdan 
1394dc88ebeSGabor Kovesdan /* Housekeeping */
1406f4cbf7cSGabor Kovesdan bool	 file_err;	/* file reading error */
1414dc88ebeSGabor Kovesdan 
1424dc88ebeSGabor Kovesdan /*
1434dc88ebeSGabor Kovesdan  * Prints usage information and returns 2.
1444dc88ebeSGabor Kovesdan  */
1454dc88ebeSGabor Kovesdan static void
1464dc88ebeSGabor Kovesdan usage(void)
1474dc88ebeSGabor Kovesdan {
14830dc9502SBaptiste Daroussin 	fprintf(stderr, errstr[3], getprogname());
14930dc9502SBaptiste Daroussin 	fprintf(stderr, "%s", errstr[4]);
15030dc9502SBaptiste Daroussin 	fprintf(stderr, "%s", errstr[5]);
15130dc9502SBaptiste Daroussin 	fprintf(stderr, "%s", errstr[6]);
1524dc88ebeSGabor Kovesdan 	exit(2);
1534dc88ebeSGabor Kovesdan }
1544dc88ebeSGabor Kovesdan 
155*4c14980bSKyle Evans static const char	*optstr = "0123456789A:B:C:D:EFGHILOSRUVabcd:e:f:hilm:nopqrsuvwxyz";
1564dc88ebeSGabor Kovesdan 
1578375d512SEd Schouten static const struct option long_options[] =
1584dc88ebeSGabor Kovesdan {
1594dc88ebeSGabor Kovesdan 	{"binary-files",	required_argument,	NULL, BIN_OPT},
1604dc88ebeSGabor Kovesdan 	{"help",		no_argument,		NULL, HELP_OPT},
1614dc88ebeSGabor Kovesdan 	{"mmap",		no_argument,		NULL, MMAP_OPT},
1624dc88ebeSGabor Kovesdan 	{"line-buffered",	no_argument,		NULL, LINEBUF_OPT},
1634dc88ebeSGabor Kovesdan 	{"label",		required_argument,	NULL, LABEL_OPT},
1644dc88ebeSGabor Kovesdan 	{"null",		no_argument,		NULL, NULL_OPT},
1654dc88ebeSGabor Kovesdan 	{"color",		optional_argument,	NULL, COLOR_OPT},
1664dc88ebeSGabor Kovesdan 	{"colour",		optional_argument,	NULL, COLOR_OPT},
1674dc88ebeSGabor Kovesdan 	{"exclude",		required_argument,	NULL, R_EXCLUDE_OPT},
1684dc88ebeSGabor Kovesdan 	{"include",		required_argument,	NULL, R_INCLUDE_OPT},
1694dc88ebeSGabor Kovesdan 	{"exclude-dir",		required_argument,	NULL, R_DEXCLUDE_OPT},
1704dc88ebeSGabor Kovesdan 	{"include-dir",		required_argument,	NULL, R_DINCLUDE_OPT},
1714dc88ebeSGabor Kovesdan 	{"after-context",	required_argument,	NULL, 'A'},
1724dc88ebeSGabor Kovesdan 	{"text",		no_argument,		NULL, 'a'},
1734dc88ebeSGabor Kovesdan 	{"before-context",	required_argument,	NULL, 'B'},
1744dc88ebeSGabor Kovesdan 	{"byte-offset",		no_argument,		NULL, 'b'},
1754dc88ebeSGabor Kovesdan 	{"context",		optional_argument,	NULL, 'C'},
1764dc88ebeSGabor Kovesdan 	{"count",		no_argument,		NULL, 'c'},
1774dc88ebeSGabor Kovesdan 	{"devices",		required_argument,	NULL, 'D'},
1784dc88ebeSGabor Kovesdan         {"directories",		required_argument,	NULL, 'd'},
1794dc88ebeSGabor Kovesdan 	{"extended-regexp",	no_argument,		NULL, 'E'},
1804dc88ebeSGabor Kovesdan 	{"regexp",		required_argument,	NULL, 'e'},
1814dc88ebeSGabor Kovesdan 	{"fixed-strings",	no_argument,		NULL, 'F'},
1824dc88ebeSGabor Kovesdan 	{"file",		required_argument,	NULL, 'f'},
1834dc88ebeSGabor Kovesdan 	{"basic-regexp",	no_argument,		NULL, 'G'},
1844dc88ebeSGabor Kovesdan 	{"no-filename",		no_argument,		NULL, 'h'},
1854dc88ebeSGabor Kovesdan 	{"with-filename",	no_argument,		NULL, 'H'},
1864dc88ebeSGabor Kovesdan 	{"ignore-case",		no_argument,		NULL, 'i'},
1874dc88ebeSGabor Kovesdan 	{"files-with-matches",	no_argument,		NULL, 'l'},
1884dc88ebeSGabor Kovesdan 	{"files-without-match", no_argument,            NULL, 'L'},
1894dc88ebeSGabor Kovesdan 	{"max-count",		required_argument,	NULL, 'm'},
1904dc88ebeSGabor Kovesdan 	{"line-number",		no_argument,		NULL, 'n'},
1914dc88ebeSGabor Kovesdan 	{"only-matching",	no_argument,		NULL, 'o'},
1924dc88ebeSGabor Kovesdan 	{"quiet",		no_argument,		NULL, 'q'},
1934dc88ebeSGabor Kovesdan 	{"silent",		no_argument,		NULL, 'q'},
1944dc88ebeSGabor Kovesdan 	{"recursive",		no_argument,		NULL, 'r'},
1954dc88ebeSGabor Kovesdan 	{"no-messages",		no_argument,		NULL, 's'},
1964dc88ebeSGabor Kovesdan 	{"binary",		no_argument,		NULL, 'U'},
1974dc88ebeSGabor Kovesdan 	{"unix-byte-offsets",	no_argument,		NULL, 'u'},
1984dc88ebeSGabor Kovesdan 	{"invert-match",	no_argument,		NULL, 'v'},
1994dc88ebeSGabor Kovesdan 	{"version",		no_argument,		NULL, 'V'},
2004dc88ebeSGabor Kovesdan 	{"word-regexp",		no_argument,		NULL, 'w'},
2014dc88ebeSGabor Kovesdan 	{"line-regexp",		no_argument,		NULL, 'x'},
2025ee1ea02SEd Maste 	{"null-data",		no_argument,		NULL, 'z'},
2034dc88ebeSGabor Kovesdan 	{NULL,			no_argument,		NULL, 0}
2044dc88ebeSGabor Kovesdan };
2054dc88ebeSGabor Kovesdan 
2064dc88ebeSGabor Kovesdan /*
2074dc88ebeSGabor Kovesdan  * Adds a searching pattern to the internal array.
2084dc88ebeSGabor Kovesdan  */
2094dc88ebeSGabor Kovesdan static void
2104dc88ebeSGabor Kovesdan add_pattern(char *pat, size_t len)
2114dc88ebeSGabor Kovesdan {
2124dc88ebeSGabor Kovesdan 
2134dc88ebeSGabor Kovesdan 	/* Check if we can do a shortcut */
214f20f6f3fSGabor Kovesdan 	if (len == 0) {
2154dc88ebeSGabor Kovesdan 		matchall = true;
2164dc88ebeSGabor Kovesdan 		return;
2174dc88ebeSGabor Kovesdan 	}
2184dc88ebeSGabor Kovesdan 	/* Increase size if necessary */
2194dc88ebeSGabor Kovesdan 	if (patterns == pattern_sz) {
2204dc88ebeSGabor Kovesdan 		pattern_sz *= 2;
2214dc88ebeSGabor Kovesdan 		pattern = grep_realloc(pattern, ++pattern_sz *
222f20f6f3fSGabor Kovesdan 		    sizeof(struct pat));
2234dc88ebeSGabor Kovesdan 	}
2244dc88ebeSGabor Kovesdan 	if (len > 0 && pat[len - 1] == '\n')
2254dc88ebeSGabor Kovesdan 		--len;
2264dc88ebeSGabor Kovesdan 	/* pat may not be NUL-terminated */
227f20f6f3fSGabor Kovesdan 	pattern[patterns].pat = grep_malloc(len + 1);
228f20f6f3fSGabor Kovesdan 	memcpy(pattern[patterns].pat, pat, len);
229f20f6f3fSGabor Kovesdan 	pattern[patterns].len = len;
230f20f6f3fSGabor Kovesdan 	pattern[patterns].pat[len] = '\0';
2314dc88ebeSGabor Kovesdan 	++patterns;
2324dc88ebeSGabor Kovesdan }
2334dc88ebeSGabor Kovesdan 
2344dc88ebeSGabor Kovesdan /*
23555e44f51SGabor Kovesdan  * Adds a file include/exclude pattern to the internal array.
2364dc88ebeSGabor Kovesdan  */
2374dc88ebeSGabor Kovesdan static void
23855e44f51SGabor Kovesdan add_fpattern(const char *pat, int mode)
2394dc88ebeSGabor Kovesdan {
2404dc88ebeSGabor Kovesdan 
2414dc88ebeSGabor Kovesdan 	/* Increase size if necessary */
24255e44f51SGabor Kovesdan 	if (fpatterns == fpattern_sz) {
24355e44f51SGabor Kovesdan 		fpattern_sz *= 2;
24455e44f51SGabor Kovesdan 		fpattern = grep_realloc(fpattern, ++fpattern_sz *
2454dc88ebeSGabor Kovesdan 		    sizeof(struct epat));
2464dc88ebeSGabor Kovesdan 	}
24755e44f51SGabor Kovesdan 	fpattern[fpatterns].pat = grep_strdup(pat);
24855e44f51SGabor Kovesdan 	fpattern[fpatterns].mode = mode;
24955e44f51SGabor Kovesdan 	++fpatterns;
25055e44f51SGabor Kovesdan }
25155e44f51SGabor Kovesdan 
25255e44f51SGabor Kovesdan /*
25355e44f51SGabor Kovesdan  * Adds a directory include/exclude pattern to the internal array.
25455e44f51SGabor Kovesdan  */
25555e44f51SGabor Kovesdan static void
25655e44f51SGabor Kovesdan add_dpattern(const char *pat, int mode)
25755e44f51SGabor Kovesdan {
25855e44f51SGabor Kovesdan 
25955e44f51SGabor Kovesdan 	/* Increase size if necessary */
26055e44f51SGabor Kovesdan 	if (dpatterns == dpattern_sz) {
26155e44f51SGabor Kovesdan 		dpattern_sz *= 2;
26255e44f51SGabor Kovesdan 		dpattern = grep_realloc(dpattern, ++dpattern_sz *
26355e44f51SGabor Kovesdan 		    sizeof(struct epat));
26455e44f51SGabor Kovesdan 	}
26555e44f51SGabor Kovesdan 	dpattern[dpatterns].pat = grep_strdup(pat);
26655e44f51SGabor Kovesdan 	dpattern[dpatterns].mode = mode;
26755e44f51SGabor Kovesdan 	++dpatterns;
2684dc88ebeSGabor Kovesdan }
2694dc88ebeSGabor Kovesdan 
2704dc88ebeSGabor Kovesdan /*
2714dc88ebeSGabor Kovesdan  * Reads searching patterns from a file and adds them with add_pattern().
2724dc88ebeSGabor Kovesdan  */
2734dc88ebeSGabor Kovesdan static void
2744dc88ebeSGabor Kovesdan read_patterns(const char *fn)
2754dc88ebeSGabor Kovesdan {
276f20f6f3fSGabor Kovesdan 	struct stat st;
2774dc88ebeSGabor Kovesdan 	FILE *f;
2784dc88ebeSGabor Kovesdan 	char *line;
2794dc88ebeSGabor Kovesdan 	size_t len;
280f3f50de6SPedro F. Giffuni 	ssize_t rlen;
2814dc88ebeSGabor Kovesdan 
28224a656c2SKyle Evans 	if (strcmp(fn, "-") == 0)
28324a656c2SKyle Evans 		f = stdin;
28424a656c2SKyle Evans 	else if ((f = fopen(fn, "r")) == NULL)
2854dc88ebeSGabor Kovesdan 		err(2, "%s", fn);
286f20f6f3fSGabor Kovesdan 	if ((fstat(fileno(f), &st) == -1) || (S_ISDIR(st.st_mode))) {
287f20f6f3fSGabor Kovesdan 		fclose(f);
288f20f6f3fSGabor Kovesdan 		return;
289f20f6f3fSGabor Kovesdan 	}
290f3f50de6SPedro F. Giffuni 	len = 0;
291f3f50de6SPedro F. Giffuni 	line = NULL;
292d204af1eSEd Maste 	while ((rlen = getline(&line, &len, f)) != -1) {
293d204af1eSEd Maste 		if (line[0] == '\0')
294d204af1eSEd Maste 			continue;
2952fa7a2afSPedro F. Giffuni 		add_pattern(line, line[0] == '\n' ? 0 : (size_t)rlen);
296d204af1eSEd Maste 	}
297d204af1eSEd Maste 
298f3f50de6SPedro F. Giffuni 	free(line);
2994dc88ebeSGabor Kovesdan 	if (ferror(f))
3004dc88ebeSGabor Kovesdan 		err(2, "%s", fn);
30124a656c2SKyle Evans 	if (strcmp(fn, "-") != 0)
3024dc88ebeSGabor Kovesdan 		fclose(f);
3034dc88ebeSGabor Kovesdan }
3044dc88ebeSGabor Kovesdan 
30527116286SGabor Kovesdan static inline const char *
30627116286SGabor Kovesdan init_color(const char *d)
30727116286SGabor Kovesdan {
30827116286SGabor Kovesdan 	char *c;
30927116286SGabor Kovesdan 
31027116286SGabor Kovesdan 	c = getenv("GREP_COLOR");
311dab19f30SGabor Kovesdan 	return (c != NULL && c[0] != '\0' ? c : d);
31227116286SGabor Kovesdan }
31327116286SGabor Kovesdan 
3144dc88ebeSGabor Kovesdan int
3154dc88ebeSGabor Kovesdan main(int argc, char *argv[])
3164dc88ebeSGabor Kovesdan {
3174dc88ebeSGabor Kovesdan 	char **aargv, **eargv, *eopts;
318afbbd357SGabor Kovesdan 	char *ep;
319afbbd357SGabor Kovesdan 	const char *pn;
320b5fc583cSEd Maste 	long long l;
3214dc88ebeSGabor Kovesdan 	unsigned int aargc, eargc, i;
3224dc88ebeSGabor Kovesdan 	int c, lastc, needpattern, newarg, prevoptind;
323cbfff13fSKyle Evans 	bool matched;
3244dc88ebeSGabor Kovesdan 
3254dc88ebeSGabor Kovesdan 	setlocale(LC_ALL, "");
3264dc88ebeSGabor Kovesdan 
32725385eb3SKyle Evans 	/*
32825385eb3SKyle Evans 	 * Check how we've bene invoked to determine the behavior we should
32925385eb3SKyle Evans 	 * exhibit. In this way we can have all the functionalities in one
33025385eb3SKyle Evans 	 * binary without the need of scripting and using ugly hacks.
33125385eb3SKyle Evans 	 */
332afbbd357SGabor Kovesdan 	pn = getprogname();
333f20f6f3fSGabor Kovesdan 	switch (pn[0]) {
3344dc88ebeSGabor Kovesdan 	case 'e':
3354dc88ebeSGabor Kovesdan 		grepbehave = GREP_EXTENDED;
3364dc88ebeSGabor Kovesdan 		break;
3374dc88ebeSGabor Kovesdan 	case 'f':
3384dc88ebeSGabor Kovesdan 		grepbehave = GREP_FIXED;
3394dc88ebeSGabor Kovesdan 		break;
34025385eb3SKyle Evans 	case 'r':
34125385eb3SKyle Evans 		dirbehave = DIR_RECURSE;
34225385eb3SKyle Evans 		Hflag = true;
34325385eb3SKyle Evans 		break;
3444dc88ebeSGabor Kovesdan 	}
3454dc88ebeSGabor Kovesdan 
3464dc88ebeSGabor Kovesdan 	lastc = '\0';
3474dc88ebeSGabor Kovesdan 	newarg = 1;
3484dc88ebeSGabor Kovesdan 	prevoptind = 1;
3494dc88ebeSGabor Kovesdan 	needpattern = 1;
3505ee1ea02SEd Maste 	fileeol = '\n';
3514dc88ebeSGabor Kovesdan 
3524dc88ebeSGabor Kovesdan 	eopts = getenv("GREP_OPTIONS");
3534dc88ebeSGabor Kovesdan 
35459218eb7SGabor Kovesdan 	/* support for extra arguments in GREP_OPTIONS */
35559218eb7SGabor Kovesdan 	eargc = 0;
356dab19f30SGabor Kovesdan 	if (eopts != NULL && eopts[0] != '\0') {
3574dc88ebeSGabor Kovesdan 		char *str;
3584dc88ebeSGabor Kovesdan 
35959218eb7SGabor Kovesdan 		/* make an estimation of how many extra arguments we have */
36059218eb7SGabor Kovesdan 		for (unsigned int j = 0; j < strlen(eopts); j++)
36159218eb7SGabor Kovesdan 			if (eopts[j] == ' ')
3624dc88ebeSGabor Kovesdan 				eargc++;
3634dc88ebeSGabor Kovesdan 
3644dc88ebeSGabor Kovesdan 		eargv = (char **)grep_malloc(sizeof(char *) * (eargc + 1));
3654dc88ebeSGabor Kovesdan 
3664dc88ebeSGabor Kovesdan 		eargc = 0;
36759218eb7SGabor Kovesdan 		/* parse extra arguments */
36859218eb7SGabor Kovesdan 		while ((str = strsep(&eopts, " ")) != NULL)
369dab19f30SGabor Kovesdan 			if (str[0] != '\0')
37059218eb7SGabor Kovesdan 				eargv[eargc++] = grep_strdup(str);
3714dc88ebeSGabor Kovesdan 
3720c41ffb3SXin LI 		aargv = (char **)grep_calloc(eargc + argc + 1,
3730c41ffb3SXin LI 		    sizeof(char *));
37459218eb7SGabor Kovesdan 
3754dc88ebeSGabor Kovesdan 		aargv[0] = argv[0];
37659218eb7SGabor Kovesdan 		for (i = 0; i < eargc; i++)
37759218eb7SGabor Kovesdan 			aargv[i + 1] = eargv[i];
37859218eb7SGabor Kovesdan 		for (int j = 1; j < argc; j++, i++)
37959218eb7SGabor Kovesdan 			aargv[i + 1] = argv[j];
3804dc88ebeSGabor Kovesdan 
38159218eb7SGabor Kovesdan 		aargc = eargc + argc;
3824dc88ebeSGabor Kovesdan 	} else {
3834dc88ebeSGabor Kovesdan 		aargv = argv;
3844dc88ebeSGabor Kovesdan 		aargc = argc;
3854dc88ebeSGabor Kovesdan 	}
3864dc88ebeSGabor Kovesdan 
3874dc88ebeSGabor Kovesdan 	while (((c = getopt_long(aargc, aargv, optstr, long_options, NULL)) !=
3884dc88ebeSGabor Kovesdan 	    -1)) {
3894dc88ebeSGabor Kovesdan 		switch (c) {
3904dc88ebeSGabor Kovesdan 		case '0': case '1': case '2': case '3': case '4':
3914dc88ebeSGabor Kovesdan 		case '5': case '6': case '7': case '8': case '9':
3924dc88ebeSGabor Kovesdan 			if (newarg || !isdigit(lastc))
3934dc88ebeSGabor Kovesdan 				Aflag = 0;
394b5fc583cSEd Maste 			else if (Aflag > LLONG_MAX / 10 - 1) {
3954dc88ebeSGabor Kovesdan 				errno = ERANGE;
3964dc88ebeSGabor Kovesdan 				err(2, NULL);
3974dc88ebeSGabor Kovesdan 			}
398b5fc583cSEd Maste 
3994dc88ebeSGabor Kovesdan 			Aflag = Bflag = (Aflag * 10) + (c - '0');
4004dc88ebeSGabor Kovesdan 			break;
4014dc88ebeSGabor Kovesdan 		case 'C':
4024dc88ebeSGabor Kovesdan 			if (optarg == NULL) {
4034dc88ebeSGabor Kovesdan 				Aflag = Bflag = 2;
4044dc88ebeSGabor Kovesdan 				break;
4054dc88ebeSGabor Kovesdan 			}
4064dc88ebeSGabor Kovesdan 			/* FALLTHROUGH */
4074dc88ebeSGabor Kovesdan 		case 'A':
4084dc88ebeSGabor Kovesdan 			/* FALLTHROUGH */
4094dc88ebeSGabor Kovesdan 		case 'B':
4104dc88ebeSGabor Kovesdan 			errno = 0;
411b5fc583cSEd Maste 			l = strtoll(optarg, &ep, 10);
412b5fc583cSEd Maste 			if (errno == ERANGE || errno == EINVAL)
4134dc88ebeSGabor Kovesdan 				err(2, NULL);
4144dc88ebeSGabor Kovesdan 			else if (ep[0] != '\0') {
4154dc88ebeSGabor Kovesdan 				errno = EINVAL;
4164dc88ebeSGabor Kovesdan 				err(2, NULL);
417b5fc583cSEd Maste 			} else if (l < 0) {
418b5fc583cSEd Maste 				errno = EINVAL;
419b5fc583cSEd Maste 				err(2, "context argument must be non-negative");
4204dc88ebeSGabor Kovesdan 			}
421b5fc583cSEd Maste 
4224dc88ebeSGabor Kovesdan 			if (c == 'A')
4234dc88ebeSGabor Kovesdan 				Aflag = l;
4244dc88ebeSGabor Kovesdan 			else if (c == 'B')
4254dc88ebeSGabor Kovesdan 				Bflag = l;
4264dc88ebeSGabor Kovesdan 			else
4274dc88ebeSGabor Kovesdan 				Aflag = Bflag = l;
4284dc88ebeSGabor Kovesdan 			break;
4294dc88ebeSGabor Kovesdan 		case 'a':
4304dc88ebeSGabor Kovesdan 			binbehave = BINFILE_TEXT;
4314dc88ebeSGabor Kovesdan 			break;
4324dc88ebeSGabor Kovesdan 		case 'b':
4334dc88ebeSGabor Kovesdan 			bflag = true;
4344dc88ebeSGabor Kovesdan 			break;
4354dc88ebeSGabor Kovesdan 		case 'c':
4364dc88ebeSGabor Kovesdan 			cflag = true;
4374dc88ebeSGabor Kovesdan 			break;
4384dc88ebeSGabor Kovesdan 		case 'D':
43927116286SGabor Kovesdan 			if (strcasecmp(optarg, "skip") == 0)
4404dc88ebeSGabor Kovesdan 				devbehave = DEV_SKIP;
44127116286SGabor Kovesdan 			else if (strcasecmp(optarg, "read") == 0)
44227116286SGabor Kovesdan 				devbehave = DEV_READ;
44397a012f2SGabor Kovesdan 			else
44430dc9502SBaptiste Daroussin 				errx(2, errstr[2], "--devices");
4454dc88ebeSGabor Kovesdan 			break;
4464dc88ebeSGabor Kovesdan 		case 'd':
44727116286SGabor Kovesdan 			if (strcasecmp("recurse", optarg) == 0) {
4484dc88ebeSGabor Kovesdan 				Hflag = true;
4494dc88ebeSGabor Kovesdan 				dirbehave = DIR_RECURSE;
45027116286SGabor Kovesdan 			} else if (strcasecmp("skip", optarg) == 0)
4514dc88ebeSGabor Kovesdan 				dirbehave = DIR_SKIP;
45227116286SGabor Kovesdan 			else if (strcasecmp("read", optarg) == 0)
45327116286SGabor Kovesdan 				dirbehave = DIR_READ;
45497a012f2SGabor Kovesdan 			else
45530dc9502SBaptiste Daroussin 				errx(2, errstr[2], "--directories");
4564dc88ebeSGabor Kovesdan 			break;
4574dc88ebeSGabor Kovesdan 		case 'E':
4584dc88ebeSGabor Kovesdan 			grepbehave = GREP_EXTENDED;
4594dc88ebeSGabor Kovesdan 			break;
4604dc88ebeSGabor Kovesdan 		case 'e':
461e411593dSGabor Kovesdan 			{
462e411593dSGabor Kovesdan 				char *token;
463cd64c588SGabor Kovesdan 				char *string = optarg;
464e411593dSGabor Kovesdan 
465e411593dSGabor Kovesdan 				while ((token = strsep(&string, "\n")) != NULL)
466e411593dSGabor Kovesdan 					add_pattern(token, strlen(token));
467e411593dSGabor Kovesdan 			}
4684dc88ebeSGabor Kovesdan 			needpattern = 0;
4694dc88ebeSGabor Kovesdan 			break;
4704dc88ebeSGabor Kovesdan 		case 'F':
4714dc88ebeSGabor Kovesdan 			grepbehave = GREP_FIXED;
4724dc88ebeSGabor Kovesdan 			break;
4734dc88ebeSGabor Kovesdan 		case 'f':
4744dc88ebeSGabor Kovesdan 			read_patterns(optarg);
4754dc88ebeSGabor Kovesdan 			needpattern = 0;
4764dc88ebeSGabor Kovesdan 			break;
4774dc88ebeSGabor Kovesdan 		case 'G':
4784dc88ebeSGabor Kovesdan 			grepbehave = GREP_BASIC;
4794dc88ebeSGabor Kovesdan 			break;
4804dc88ebeSGabor Kovesdan 		case 'H':
4814dc88ebeSGabor Kovesdan 			Hflag = true;
4824dc88ebeSGabor Kovesdan 			break;
4834dc88ebeSGabor Kovesdan 		case 'h':
4844dc88ebeSGabor Kovesdan 			Hflag = false;
4854dc88ebeSGabor Kovesdan 			hflag = true;
4864dc88ebeSGabor Kovesdan 			break;
4874dc88ebeSGabor Kovesdan 		case 'I':
4884dc88ebeSGabor Kovesdan 			binbehave = BINFILE_SKIP;
4894dc88ebeSGabor Kovesdan 			break;
4904dc88ebeSGabor Kovesdan 		case 'i':
4914dc88ebeSGabor Kovesdan 		case 'y':
4924dc88ebeSGabor Kovesdan 			iflag =  true;
4934dc88ebeSGabor Kovesdan 			cflags |= REG_ICASE;
4944dc88ebeSGabor Kovesdan 			break;
4954dc88ebeSGabor Kovesdan 		case 'L':
4964dc88ebeSGabor Kovesdan 			lflag = false;
49727116286SGabor Kovesdan 			Lflag = true;
4984dc88ebeSGabor Kovesdan 			break;
4994dc88ebeSGabor Kovesdan 		case 'l':
5004dc88ebeSGabor Kovesdan 			Lflag = false;
50127116286SGabor Kovesdan 			lflag = true;
5024dc88ebeSGabor Kovesdan 			break;
5034dc88ebeSGabor Kovesdan 		case 'm':
5044dc88ebeSGabor Kovesdan 			mflag = true;
5054dc88ebeSGabor Kovesdan 			errno = 0;
506924500b7SEitan Adler 			mlimit = mcount = strtoll(optarg, &ep, 10);
507f20f6f3fSGabor Kovesdan 			if (((errno == ERANGE) && (mcount == LLONG_MAX)) ||
5084dc88ebeSGabor Kovesdan 			    ((errno == EINVAL) && (mcount == 0)))
5094dc88ebeSGabor Kovesdan 				err(2, NULL);
5104dc88ebeSGabor Kovesdan 			else if (ep[0] != '\0') {
5114dc88ebeSGabor Kovesdan 				errno = EINVAL;
5124dc88ebeSGabor Kovesdan 				err(2, NULL);
5134dc88ebeSGabor Kovesdan 			}
5144dc88ebeSGabor Kovesdan 			break;
5154dc88ebeSGabor Kovesdan 		case 'n':
5164dc88ebeSGabor Kovesdan 			nflag = true;
5174dc88ebeSGabor Kovesdan 			break;
5184dc88ebeSGabor Kovesdan 		case 'O':
5194dc88ebeSGabor Kovesdan 			linkbehave = LINK_EXPLICIT;
5204dc88ebeSGabor Kovesdan 			break;
5214dc88ebeSGabor Kovesdan 		case 'o':
5224dc88ebeSGabor Kovesdan 			oflag = true;
52369a6d198SGabor Kovesdan 			cflags &= ~REG_NOSUB;
5244dc88ebeSGabor Kovesdan 			break;
5254dc88ebeSGabor Kovesdan 		case 'p':
5264dc88ebeSGabor Kovesdan 			linkbehave = LINK_SKIP;
5274dc88ebeSGabor Kovesdan 			break;
5284dc88ebeSGabor Kovesdan 		case 'q':
5294dc88ebeSGabor Kovesdan 			qflag = true;
5304dc88ebeSGabor Kovesdan 			break;
5314dc88ebeSGabor Kovesdan 		case 'S':
53227116286SGabor Kovesdan 			linkbehave = LINK_READ;
5334dc88ebeSGabor Kovesdan 			break;
5344dc88ebeSGabor Kovesdan 		case 'R':
5354dc88ebeSGabor Kovesdan 		case 'r':
5364dc88ebeSGabor Kovesdan 			dirbehave = DIR_RECURSE;
5374dc88ebeSGabor Kovesdan 			Hflag = true;
5384dc88ebeSGabor Kovesdan 			break;
5394dc88ebeSGabor Kovesdan 		case 's':
5404dc88ebeSGabor Kovesdan 			sflag = true;
5414dc88ebeSGabor Kovesdan 			break;
5424dc88ebeSGabor Kovesdan 		case 'U':
5434dc88ebeSGabor Kovesdan 			binbehave = BINFILE_BIN;
5444dc88ebeSGabor Kovesdan 			break;
5454dc88ebeSGabor Kovesdan 		case 'u':
5464dc88ebeSGabor Kovesdan 		case MMAP_OPT:
547f20f6f3fSGabor Kovesdan 			filebehave = FILE_MMAP;
5484dc88ebeSGabor Kovesdan 			break;
5494dc88ebeSGabor Kovesdan 		case 'V':
55030dc9502SBaptiste Daroussin 			printf(errstr[8], getprogname(), VERSION);
5514dc88ebeSGabor Kovesdan 			exit(0);
5524dc88ebeSGabor Kovesdan 		case 'v':
5534dc88ebeSGabor Kovesdan 			vflag = true;
5544dc88ebeSGabor Kovesdan 			break;
5554dc88ebeSGabor Kovesdan 		case 'w':
5564dc88ebeSGabor Kovesdan 			wflag = true;
55769a6d198SGabor Kovesdan 			cflags &= ~REG_NOSUB;
5584dc88ebeSGabor Kovesdan 			break;
5594dc88ebeSGabor Kovesdan 		case 'x':
5604dc88ebeSGabor Kovesdan 			xflag = true;
56169a6d198SGabor Kovesdan 			cflags &= ~REG_NOSUB;
5624dc88ebeSGabor Kovesdan 			break;
5635ee1ea02SEd Maste 		case 'z':
5645ee1ea02SEd Maste 			fileeol = '\0';
5655ee1ea02SEd Maste 			break;
5664dc88ebeSGabor Kovesdan 		case BIN_OPT:
56727116286SGabor Kovesdan 			if (strcasecmp("binary", optarg) == 0)
5684dc88ebeSGabor Kovesdan 				binbehave = BINFILE_BIN;
56927116286SGabor Kovesdan 			else if (strcasecmp("without-match", optarg) == 0)
5704dc88ebeSGabor Kovesdan 				binbehave = BINFILE_SKIP;
57127116286SGabor Kovesdan 			else if (strcasecmp("text", optarg) == 0)
5724dc88ebeSGabor Kovesdan 				binbehave = BINFILE_TEXT;
5734dc88ebeSGabor Kovesdan 			else
57430dc9502SBaptiste Daroussin 				errx(2, errstr[2], "--binary-files");
5754dc88ebeSGabor Kovesdan 			break;
5764dc88ebeSGabor Kovesdan 		case COLOR_OPT:
5774dc88ebeSGabor Kovesdan 			color = NULL;
57827116286SGabor Kovesdan 			if (optarg == NULL || strcasecmp("auto", optarg) == 0 ||
57927116286SGabor Kovesdan 			    strcasecmp("tty", optarg) == 0 ||
58027116286SGabor Kovesdan 			    strcasecmp("if-tty", optarg) == 0) {
58127116286SGabor Kovesdan 				char *term;
58227116286SGabor Kovesdan 
58327116286SGabor Kovesdan 				term = getenv("TERM");
58427116286SGabor Kovesdan 				if (isatty(STDOUT_FILENO) && term != NULL &&
58527116286SGabor Kovesdan 				    strcasecmp(term, "dumb") != 0)
58627116286SGabor Kovesdan 					color = init_color("01;31");
58727116286SGabor Kovesdan 			} else if (strcasecmp("always", optarg) == 0 ||
58827116286SGabor Kovesdan 			    strcasecmp("yes", optarg) == 0 ||
58927116286SGabor Kovesdan 			    strcasecmp("force", optarg) == 0) {
59027116286SGabor Kovesdan 				color = init_color("01;31");
59127116286SGabor Kovesdan 			} else if (strcasecmp("never", optarg) != 0 &&
59227116286SGabor Kovesdan 			    strcasecmp("none", optarg) != 0 &&
59327116286SGabor Kovesdan 			    strcasecmp("no", optarg) != 0)
59430dc9502SBaptiste Daroussin 				errx(2, errstr[2], "--color");
59569a6d198SGabor Kovesdan 			cflags &= ~REG_NOSUB;
5964dc88ebeSGabor Kovesdan 			break;
5974dc88ebeSGabor Kovesdan 		case LABEL_OPT:
5984dc88ebeSGabor Kovesdan 			label = optarg;
5994dc88ebeSGabor Kovesdan 			break;
6004dc88ebeSGabor Kovesdan 		case LINEBUF_OPT:
6014dc88ebeSGabor Kovesdan 			lbflag = true;
6024dc88ebeSGabor Kovesdan 			break;
6034dc88ebeSGabor Kovesdan 		case NULL_OPT:
6044dc88ebeSGabor Kovesdan 			nullflag = true;
6054dc88ebeSGabor Kovesdan 			break;
6064dc88ebeSGabor Kovesdan 		case R_INCLUDE_OPT:
60755e44f51SGabor Kovesdan 			finclude = true;
60855e44f51SGabor Kovesdan 			add_fpattern(optarg, INCL_PAT);
6094dc88ebeSGabor Kovesdan 			break;
6104dc88ebeSGabor Kovesdan 		case R_EXCLUDE_OPT:
61155e44f51SGabor Kovesdan 			fexclude = true;
61255e44f51SGabor Kovesdan 			add_fpattern(optarg, EXCL_PAT);
6134dc88ebeSGabor Kovesdan 			break;
6144dc88ebeSGabor Kovesdan 		case R_DINCLUDE_OPT:
61559218eb7SGabor Kovesdan 			dinclude = true;
61655e44f51SGabor Kovesdan 			add_dpattern(optarg, INCL_PAT);
6174dc88ebeSGabor Kovesdan 			break;
6184dc88ebeSGabor Kovesdan 		case R_DEXCLUDE_OPT:
61959218eb7SGabor Kovesdan 			dexclude = true;
62055e44f51SGabor Kovesdan 			add_dpattern(optarg, EXCL_PAT);
6214dc88ebeSGabor Kovesdan 			break;
6224dc88ebeSGabor Kovesdan 		case HELP_OPT:
6234dc88ebeSGabor Kovesdan 		default:
6244dc88ebeSGabor Kovesdan 			usage();
6254dc88ebeSGabor Kovesdan 		}
6264dc88ebeSGabor Kovesdan 		lastc = c;
6274dc88ebeSGabor Kovesdan 		newarg = optind != prevoptind;
6284dc88ebeSGabor Kovesdan 		prevoptind = optind;
6294dc88ebeSGabor Kovesdan 	}
6304dc88ebeSGabor Kovesdan 	aargc -= optind;
6314dc88ebeSGabor Kovesdan 	aargv += optind;
6324dc88ebeSGabor Kovesdan 
6332373acbbSKyle Evans 	/* xflag takes precedence, don't confuse the matching bits. */
6342373acbbSKyle Evans 	if (wflag && xflag)
6352373acbbSKyle Evans 		wflag = false;
6362373acbbSKyle Evans 
6374dc88ebeSGabor Kovesdan 	/* Fail if we don't have any pattern */
6384dc88ebeSGabor Kovesdan 	if (aargc == 0 && needpattern)
6394dc88ebeSGabor Kovesdan 		usage();
6404dc88ebeSGabor Kovesdan 
6414dc88ebeSGabor Kovesdan 	/* Process patterns from command line */
6424dc88ebeSGabor Kovesdan 	if (aargc != 0 && needpattern) {
643e411593dSGabor Kovesdan 		char *token;
644cd64c588SGabor Kovesdan 		char *string = *aargv;
645e411593dSGabor Kovesdan 
646e411593dSGabor Kovesdan 		while ((token = strsep(&string, "\n")) != NULL)
647e411593dSGabor Kovesdan 			add_pattern(token, strlen(token));
6484dc88ebeSGabor Kovesdan 		--aargc;
6494dc88ebeSGabor Kovesdan 		++aargv;
6504dc88ebeSGabor Kovesdan 	}
6514dc88ebeSGabor Kovesdan 
6524dc88ebeSGabor Kovesdan 	switch (grepbehave) {
6534dc88ebeSGabor Kovesdan 	case GREP_BASIC:
6544dc88ebeSGabor Kovesdan 		break;
655f20f6f3fSGabor Kovesdan 	case GREP_FIXED:
65605ad8215SKyle Evans 		/*
65705ad8215SKyle Evans 		 * regex(3) implementations that support fixed-string searches generally
65805ad8215SKyle Evans 		 * define either REG_NOSPEC or REG_LITERAL. Set the appropriate flag
65905ad8215SKyle Evans 		 * here. If neither are defined, GREP_FIXED later implies that the
66005ad8215SKyle Evans 		 * internal literal matcher should be used. Other cflags that have
66105ad8215SKyle Evans 		 * the same interpretation as REG_NOSPEC and REG_LITERAL should be
66205ad8215SKyle Evans 		 * similarly added here, and grep.h should be amended to take this into
66305ad8215SKyle Evans 		 * consideration when defining WITH_INTERNAL_NOSPEC.
66405ad8215SKyle Evans 		 */
665476d2098SEd Maste #if defined(REG_NOSPEC)
666476d2098SEd Maste 		cflags |= REG_NOSPEC;
667476d2098SEd Maste #elif defined(REG_LITERAL)
668476d2098SEd Maste 		cflags |= REG_LITERAL;
669476d2098SEd Maste #endif
670f20f6f3fSGabor Kovesdan 		break;
6714dc88ebeSGabor Kovesdan 	case GREP_EXTENDED:
6724dc88ebeSGabor Kovesdan 		cflags |= REG_EXTENDED;
6734dc88ebeSGabor Kovesdan 		break;
6744dc88ebeSGabor Kovesdan 	default:
6754dc88ebeSGabor Kovesdan 		/* NOTREACHED */
6764dc88ebeSGabor Kovesdan 		usage();
6774dc88ebeSGabor Kovesdan 	}
6784dc88ebeSGabor Kovesdan 
6794dc88ebeSGabor Kovesdan 	r_pattern = grep_calloc(patterns, sizeof(*r_pattern));
680f20f6f3fSGabor Kovesdan 
68105ad8215SKyle Evans #ifdef WITH_INTERNAL_NOSPEC
68238325e2aSKyle Evans 	if (grepbehave != GREP_FIXED) {
68305ad8215SKyle Evans #else
68438325e2aSKyle Evans 	{
68505ad8215SKyle Evans #endif
6864dc88ebeSGabor Kovesdan 		/* Check if cheating is allowed (always is for fgrep). */
6874dc88ebeSGabor Kovesdan 		for (i = 0; i < patterns; ++i) {
688f20f6f3fSGabor Kovesdan 			c = regcomp(&r_pattern[i], pattern[i].pat, cflags);
6894dc88ebeSGabor Kovesdan 			if (c != 0) {
6904dc88ebeSGabor Kovesdan 				regerror(c, &r_pattern[i], re_error,
6914dc88ebeSGabor Kovesdan 				    RE_ERROR_BUF);
6924dc88ebeSGabor Kovesdan 				errx(2, "%s", re_error);
6934dc88ebeSGabor Kovesdan 			}
6944dc88ebeSGabor Kovesdan 		}
695a4f3f02bSEd Maste 	}
6964dc88ebeSGabor Kovesdan 
6974dc88ebeSGabor Kovesdan 	if (lbflag)
6984dc88ebeSGabor Kovesdan 		setlinebuf(stdout);
6994dc88ebeSGabor Kovesdan 
7004dc88ebeSGabor Kovesdan 	if ((aargc == 0 || aargc == 1) && !Hflag)
7014dc88ebeSGabor Kovesdan 		hflag = true;
7024dc88ebeSGabor Kovesdan 
703df546c3bSKyle Evans 	initqueue();
704df546c3bSKyle Evans 
705a461896aSEd Maste 	if (aargc == 0 && dirbehave != DIR_RECURSE)
7064dc88ebeSGabor Kovesdan 		exit(!procfile("-"));
7074dc88ebeSGabor Kovesdan 
7084dc88ebeSGabor Kovesdan 	if (dirbehave == DIR_RECURSE)
709cbfff13fSKyle Evans 		matched = grep_tree(aargv);
710c38208adSXin LI 	else
711cbfff13fSKyle Evans 		for (matched = false; aargc--; ++aargv) {
71255e44f51SGabor Kovesdan 			if ((finclude || fexclude) && !file_matching(*aargv))
71355e44f51SGabor Kovesdan 				continue;
714cbfff13fSKyle Evans 			if (procfile(*aargv))
715cbfff13fSKyle Evans 				matched = true;
71655e44f51SGabor Kovesdan 		}
7174dc88ebeSGabor Kovesdan 
71838325e2aSKyle Evans 	if (Lflag)
71938325e2aSKyle Evans 		matched = !matched;
72038325e2aSKyle Evans 
72138325e2aSKyle Evans 	/*
72238325e2aSKyle Evans 	 * Calculate the correct return value according to the
72338325e2aSKyle Evans 	 * results and the command line option.
72438325e2aSKyle Evans 	 */
725cbfff13fSKyle Evans 	exit(matched ? (file_err ? (qflag ? 0 : 2) : 0) : (file_err ? 2 : 1));
7264dc88ebeSGabor Kovesdan }
727