xref: /freebsd/usr.bin/grep/grep.c (revision 2f513db7)
1 /*	$NetBSD: grep.c,v 1.6 2011/04/18 03:48:23 joerg Exp $	*/
2 /* 	$FreeBSD$	*/
3 /*	$OpenBSD: grep.c,v 1.42 2010/07/02 22:18:03 tedu Exp $	*/
4 
5 /*-
6  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
7  *
8  * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
9  * Copyright (C) 2008-2009 Gabor Kovesdan <gabor@FreeBSD.org>
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/stat.h>
38 #include <sys/types.h>
39 
40 #include <ctype.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <getopt.h>
45 #include <limits.h>
46 #include <libgen.h>
47 #include <locale.h>
48 #include <stdbool.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 
54 #include "grep.h"
55 
56 const char	*errstr[] = {
57 	"",
58 /* 1*/	"(standard input)",
59 /* 2*/	"unknown %s option",
60 /* 3*/	"usage: %s [-abcDEFGHhIiLlmnOoPqRSsUVvwxz] [-A num] [-B num] [-C[num]]\n",
61 /* 4*/	"\t[-e pattern] [-f file] [--binary-files=value] [--color=when]\n",
62 /* 5*/	"\t[--context[=num]] [--directories=action] [--label] [--line-buffered]\n",
63 /* 6*/	"\t[--null] [pattern] [file ...]\n",
64 /* 7*/	"Binary file %s matches\n",
65 /* 8*/	"%s (BSD grep) %s\n",
66 /* 9*/	"%s (BSD grep, GNU compatible) %s\n",
67 };
68 
69 /* Flags passed to regcomp() and regexec() */
70 int		 cflags = REG_NOSUB | REG_NEWLINE;
71 int		 eflags = REG_STARTEND;
72 
73 /* XXX TODO: Get rid of this flag.
74  * matchall is a gross hack that means that an empty pattern was passed to us.
75  * It is a necessary evil at the moment because our regex(3) implementation
76  * does not allow for empty patterns, as supported by POSIX's definition of
77  * grammar for BREs/EREs. When libregex becomes available, it would be wise
78  * to remove this and let regex(3) handle the dirty details of empty patterns.
79  */
80 bool		 matchall;
81 
82 /* Searching patterns */
83 unsigned int	 patterns;
84 static unsigned int pattern_sz;
85 struct pat	*pattern;
86 regex_t		*r_pattern;
87 
88 /* Filename exclusion/inclusion patterns */
89 unsigned int	fpatterns, dpatterns;
90 static unsigned int fpattern_sz, dpattern_sz;
91 struct epat	*dpattern, *fpattern;
92 
93 /* For regex errors  */
94 char	 re_error[RE_ERROR_BUF + 1];
95 
96 /* Command-line flags */
97 long long Aflag;	/* -A x: print x lines trailing each match */
98 long long Bflag;	/* -B x: print x lines leading each match */
99 bool	 Hflag;		/* -H: always print file name */
100 bool	 Lflag;		/* -L: only show names of files with no matches */
101 bool	 bflag;		/* -b: show block numbers for each match */
102 bool	 cflag;		/* -c: only show a count of matching lines */
103 bool	 hflag;		/* -h: don't print filename headers */
104 bool	 iflag;		/* -i: ignore case */
105 bool	 lflag;		/* -l: only show names of files with matches */
106 bool	 mflag;		/* -m x: stop reading the files after x matches */
107 long long mcount;	/* count for -m */
108 long long mlimit;	/* requested value for -m */
109 char	 fileeol;	/* indicator for eol */
110 bool	 nflag;		/* -n: show line numbers in front of matching lines */
111 bool	 oflag;		/* -o: print only matching part */
112 bool	 qflag;		/* -q: quiet mode (don't output anything) */
113 bool	 sflag;		/* -s: silent mode (ignore errors) */
114 bool	 vflag;		/* -v: only show non-matching lines */
115 bool	 wflag;		/* -w: pattern must start and end on word boundaries */
116 bool	 xflag;		/* -x: pattern must match entire line */
117 bool	 lbflag;	/* --line-buffered */
118 bool	 nullflag;	/* --null */
119 char	*label;		/* --label */
120 const char *color;	/* --color */
121 int	 grepbehave = GREP_BASIC;	/* -EFGP: type of the regex */
122 int	 binbehave = BINFILE_BIN;	/* -aIU: handling of binary files */
123 int	 filebehave = FILE_STDIO;
124 int	 devbehave = DEV_READ;		/* -D: handling of devices */
125 int	 dirbehave = DIR_READ;		/* -dRr: handling of directories */
126 int	 linkbehave = LINK_READ;	/* -OpS: handling of symlinks */
127 
128 bool	 dexclude, dinclude;	/* --exclude-dir and --include-dir */
129 bool	 fexclude, finclude;	/* --exclude and --include */
130 
131 enum {
132 	BIN_OPT = CHAR_MAX + 1,
133 	COLOR_OPT,
134 	HELP_OPT,
135 	MMAP_OPT,
136 	LINEBUF_OPT,
137 	LABEL_OPT,
138 	NULL_OPT,
139 	R_EXCLUDE_OPT,
140 	R_INCLUDE_OPT,
141 	R_DEXCLUDE_OPT,
142 	R_DINCLUDE_OPT
143 };
144 
145 static inline const char	*init_color(const char *);
146 
147 /* Housekeeping */
148 bool	 file_err;	/* file reading error */
149 
150 /*
151  * Prints usage information and returns 2.
152  */
153 static void
154 usage(void)
155 {
156 	fprintf(stderr, errstr[3], getprogname());
157 	fprintf(stderr, "%s", errstr[4]);
158 	fprintf(stderr, "%s", errstr[5]);
159 	fprintf(stderr, "%s", errstr[6]);
160 	exit(2);
161 }
162 
163 static const char	*optstr = "0123456789A:B:C:D:EFGHILOPSRUVabcd:e:f:hilm:nopqrsuvwxyz";
164 
165 static const struct option long_options[] =
166 {
167 	{"binary-files",	required_argument,	NULL, BIN_OPT},
168 	{"help",		no_argument,		NULL, HELP_OPT},
169 	{"mmap",		no_argument,		NULL, MMAP_OPT},
170 	{"line-buffered",	no_argument,		NULL, LINEBUF_OPT},
171 	{"label",		required_argument,	NULL, LABEL_OPT},
172 	{"null",		no_argument,		NULL, NULL_OPT},
173 	{"color",		optional_argument,	NULL, COLOR_OPT},
174 	{"colour",		optional_argument,	NULL, COLOR_OPT},
175 	{"exclude",		required_argument,	NULL, R_EXCLUDE_OPT},
176 	{"include",		required_argument,	NULL, R_INCLUDE_OPT},
177 	{"exclude-dir",		required_argument,	NULL, R_DEXCLUDE_OPT},
178 	{"include-dir",		required_argument,	NULL, R_DINCLUDE_OPT},
179 	{"after-context",	required_argument,	NULL, 'A'},
180 	{"text",		no_argument,		NULL, 'a'},
181 	{"before-context",	required_argument,	NULL, 'B'},
182 	{"byte-offset",		no_argument,		NULL, 'b'},
183 	{"context",		optional_argument,	NULL, 'C'},
184 	{"count",		no_argument,		NULL, 'c'},
185 	{"devices",		required_argument,	NULL, 'D'},
186         {"directories",		required_argument,	NULL, 'd'},
187 	{"extended-regexp",	no_argument,		NULL, 'E'},
188 	{"regexp",		required_argument,	NULL, 'e'},
189 	{"fixed-strings",	no_argument,		NULL, 'F'},
190 	{"file",		required_argument,	NULL, 'f'},
191 	{"basic-regexp",	no_argument,		NULL, 'G'},
192 	{"no-filename",		no_argument,		NULL, 'h'},
193 	{"with-filename",	no_argument,		NULL, 'H'},
194 	{"ignore-case",		no_argument,		NULL, 'i'},
195 	{"files-with-matches",	no_argument,		NULL, 'l'},
196 	{"files-without-match", no_argument,            NULL, 'L'},
197 	{"max-count",		required_argument,	NULL, 'm'},
198 	{"line-number",		no_argument,		NULL, 'n'},
199 	{"only-matching",	no_argument,		NULL, 'o'},
200 	{"quiet",		no_argument,		NULL, 'q'},
201 	{"silent",		no_argument,		NULL, 'q'},
202 	{"recursive",		no_argument,		NULL, 'r'},
203 	{"no-messages",		no_argument,		NULL, 's'},
204 	{"binary",		no_argument,		NULL, 'U'},
205 	{"unix-byte-offsets",	no_argument,		NULL, 'u'},
206 	{"invert-match",	no_argument,		NULL, 'v'},
207 	{"version",		no_argument,		NULL, 'V'},
208 	{"word-regexp",		no_argument,		NULL, 'w'},
209 	{"line-regexp",		no_argument,		NULL, 'x'},
210 	{"null-data",		no_argument,		NULL, 'z'},
211 	{NULL,			no_argument,		NULL, 0}
212 };
213 
214 /*
215  * Adds a searching pattern to the internal array.
216  */
217 static void
218 add_pattern(char *pat, size_t len)
219 {
220 
221 	/* Check if we can do a shortcut */
222 	if (len == 0) {
223 		matchall = true;
224 		return;
225 	}
226 	/* Increase size if necessary */
227 	if (patterns == pattern_sz) {
228 		pattern_sz *= 2;
229 		pattern = grep_realloc(pattern, ++pattern_sz *
230 		    sizeof(struct pat));
231 	}
232 	if (len > 0 && pat[len - 1] == '\n')
233 		--len;
234 	/* pat may not be NUL-terminated */
235 	pattern[patterns].pat = grep_malloc(len + 1);
236 	memcpy(pattern[patterns].pat, pat, len);
237 	pattern[patterns].len = len;
238 	pattern[patterns].pat[len] = '\0';
239 	++patterns;
240 }
241 
242 /*
243  * Adds a file include/exclude pattern to the internal array.
244  */
245 static void
246 add_fpattern(const char *pat, int mode)
247 {
248 
249 	/* Increase size if necessary */
250 	if (fpatterns == fpattern_sz) {
251 		fpattern_sz *= 2;
252 		fpattern = grep_realloc(fpattern, ++fpattern_sz *
253 		    sizeof(struct epat));
254 	}
255 	fpattern[fpatterns].pat = grep_strdup(pat);
256 	fpattern[fpatterns].mode = mode;
257 	++fpatterns;
258 }
259 
260 /*
261  * Adds a directory include/exclude pattern to the internal array.
262  */
263 static void
264 add_dpattern(const char *pat, int mode)
265 {
266 
267 	/* Increase size if necessary */
268 	if (dpatterns == dpattern_sz) {
269 		dpattern_sz *= 2;
270 		dpattern = grep_realloc(dpattern, ++dpattern_sz *
271 		    sizeof(struct epat));
272 	}
273 	dpattern[dpatterns].pat = grep_strdup(pat);
274 	dpattern[dpatterns].mode = mode;
275 	++dpatterns;
276 }
277 
278 /*
279  * Reads searching patterns from a file and adds them with add_pattern().
280  */
281 static void
282 read_patterns(const char *fn)
283 {
284 	struct stat st;
285 	FILE *f;
286 	char *line;
287 	size_t len;
288 	ssize_t rlen;
289 
290 	if (strcmp(fn, "-") == 0)
291 		f = stdin;
292 	else if ((f = fopen(fn, "r")) == NULL)
293 		err(2, "%s", fn);
294 	if ((fstat(fileno(f), &st) == -1) || (S_ISDIR(st.st_mode))) {
295 		fclose(f);
296 		return;
297 	}
298 	len = 0;
299 	line = NULL;
300 	while ((rlen = getline(&line, &len, f)) != -1) {
301 		if (line[0] == '\0')
302 			continue;
303 		add_pattern(line, line[0] == '\n' ? 0 : (size_t)rlen);
304 	}
305 
306 	free(line);
307 	if (ferror(f))
308 		err(2, "%s", fn);
309 	if (strcmp(fn, "-") != 0)
310 		fclose(f);
311 }
312 
313 static inline const char *
314 init_color(const char *d)
315 {
316 	char *c;
317 
318 	c = getenv("GREP_COLOR");
319 	return (c != NULL && c[0] != '\0' ? c : d);
320 }
321 
322 int
323 main(int argc, char *argv[])
324 {
325 	char **aargv, **eargv, *eopts;
326 	char *ep;
327 	const char *pn;
328 	long long l;
329 	unsigned int aargc, eargc, i;
330 	int c, lastc, needpattern, newarg, prevoptind;
331 	bool matched;
332 
333 	setlocale(LC_ALL, "");
334 
335 	/*
336 	 * Check how we've bene invoked to determine the behavior we should
337 	 * exhibit. In this way we can have all the functionalities in one
338 	 * binary without the need of scripting and using ugly hacks.
339 	 */
340 	pn = getprogname();
341 	switch (pn[0]) {
342 	case 'e':
343 		grepbehave = GREP_EXTENDED;
344 		break;
345 	case 'f':
346 		grepbehave = GREP_FIXED;
347 		break;
348 	case 'r':
349 		dirbehave = DIR_RECURSE;
350 		Hflag = true;
351 		break;
352 	}
353 
354 	lastc = '\0';
355 	newarg = 1;
356 	prevoptind = 1;
357 	needpattern = 1;
358 	fileeol = '\n';
359 
360 	eopts = getenv("GREP_OPTIONS");
361 
362 	/* support for extra arguments in GREP_OPTIONS */
363 	eargc = 0;
364 	if (eopts != NULL && eopts[0] != '\0') {
365 		char *str;
366 
367 		/* make an estimation of how many extra arguments we have */
368 		for (unsigned int j = 0; j < strlen(eopts); j++)
369 			if (eopts[j] == ' ')
370 				eargc++;
371 
372 		eargv = (char **)grep_malloc(sizeof(char *) * (eargc + 1));
373 
374 		eargc = 0;
375 		/* parse extra arguments */
376 		while ((str = strsep(&eopts, " ")) != NULL)
377 			if (str[0] != '\0')
378 				eargv[eargc++] = grep_strdup(str);
379 
380 		aargv = (char **)grep_calloc(eargc + argc + 1,
381 		    sizeof(char *));
382 
383 		aargv[0] = argv[0];
384 		for (i = 0; i < eargc; i++)
385 			aargv[i + 1] = eargv[i];
386 		for (int j = 1; j < argc; j++, i++)
387 			aargv[i + 1] = argv[j];
388 
389 		aargc = eargc + argc;
390 	} else {
391 		aargv = argv;
392 		aargc = argc;
393 	}
394 
395 	while (((c = getopt_long(aargc, aargv, optstr, long_options, NULL)) !=
396 	    -1)) {
397 		switch (c) {
398 		case '0': case '1': case '2': case '3': case '4':
399 		case '5': case '6': case '7': case '8': case '9':
400 			if (newarg || !isdigit(lastc))
401 				Aflag = 0;
402 			else if (Aflag > LLONG_MAX / 10 - 1) {
403 				errno = ERANGE;
404 				err(2, NULL);
405 			}
406 
407 			Aflag = Bflag = (Aflag * 10) + (c - '0');
408 			break;
409 		case 'C':
410 			if (optarg == NULL) {
411 				Aflag = Bflag = 2;
412 				break;
413 			}
414 			/* FALLTHROUGH */
415 		case 'A':
416 			/* FALLTHROUGH */
417 		case 'B':
418 			errno = 0;
419 			l = strtoll(optarg, &ep, 10);
420 			if (errno == ERANGE || errno == EINVAL)
421 				err(2, NULL);
422 			else if (ep[0] != '\0') {
423 				errno = EINVAL;
424 				err(2, NULL);
425 			} else if (l < 0) {
426 				errno = EINVAL;
427 				err(2, "context argument must be non-negative");
428 			}
429 
430 			if (c == 'A')
431 				Aflag = l;
432 			else if (c == 'B')
433 				Bflag = l;
434 			else
435 				Aflag = Bflag = l;
436 			break;
437 		case 'a':
438 			binbehave = BINFILE_TEXT;
439 			break;
440 		case 'b':
441 			bflag = true;
442 			break;
443 		case 'c':
444 			cflag = true;
445 			break;
446 		case 'D':
447 			if (strcasecmp(optarg, "skip") == 0)
448 				devbehave = DEV_SKIP;
449 			else if (strcasecmp(optarg, "read") == 0)
450 				devbehave = DEV_READ;
451 			else
452 				errx(2, errstr[2], "--devices");
453 			break;
454 		case 'd':
455 			if (strcasecmp("recurse", optarg) == 0) {
456 				Hflag = true;
457 				dirbehave = DIR_RECURSE;
458 			} else if (strcasecmp("skip", optarg) == 0)
459 				dirbehave = DIR_SKIP;
460 			else if (strcasecmp("read", optarg) == 0)
461 				dirbehave = DIR_READ;
462 			else
463 				errx(2, errstr[2], "--directories");
464 			break;
465 		case 'E':
466 			grepbehave = GREP_EXTENDED;
467 			break;
468 		case 'e':
469 			{
470 				char *token;
471 				char *string = optarg;
472 
473 				while ((token = strsep(&string, "\n")) != NULL)
474 					add_pattern(token, strlen(token));
475 			}
476 			needpattern = 0;
477 			break;
478 		case 'F':
479 			grepbehave = GREP_FIXED;
480 			break;
481 		case 'f':
482 			read_patterns(optarg);
483 			needpattern = 0;
484 			break;
485 		case 'G':
486 			grepbehave = GREP_BASIC;
487 			break;
488 		case 'H':
489 			Hflag = true;
490 			break;
491 		case 'h':
492 			Hflag = false;
493 			hflag = true;
494 			break;
495 		case 'I':
496 			binbehave = BINFILE_SKIP;
497 			break;
498 		case 'i':
499 		case 'y':
500 			iflag =  true;
501 			cflags |= REG_ICASE;
502 			break;
503 		case 'L':
504 			lflag = false;
505 			Lflag = true;
506 			break;
507 		case 'l':
508 			Lflag = false;
509 			lflag = true;
510 			break;
511 		case 'm':
512 			mflag = true;
513 			errno = 0;
514 			mlimit = mcount = strtoll(optarg, &ep, 10);
515 			if (((errno == ERANGE) && (mcount == LLONG_MAX)) ||
516 			    ((errno == EINVAL) && (mcount == 0)))
517 				err(2, NULL);
518 			else if (ep[0] != '\0') {
519 				errno = EINVAL;
520 				err(2, NULL);
521 			}
522 			break;
523 		case 'n':
524 			nflag = true;
525 			break;
526 		case 'O':
527 			linkbehave = LINK_EXPLICIT;
528 			break;
529 		case 'o':
530 			oflag = true;
531 			cflags &= ~REG_NOSUB;
532 			break;
533 		case 'p':
534 			linkbehave = LINK_SKIP;
535 			break;
536 		case 'q':
537 			qflag = true;
538 			break;
539 		case 'S':
540 			linkbehave = LINK_READ;
541 			break;
542 		case 'R':
543 		case 'r':
544 			dirbehave = DIR_RECURSE;
545 			Hflag = true;
546 			break;
547 		case 's':
548 			sflag = true;
549 			break;
550 		case 'U':
551 			binbehave = BINFILE_BIN;
552 			break;
553 		case 'u':
554 		case MMAP_OPT:
555 			filebehave = FILE_MMAP;
556 			break;
557 		case 'V':
558 #ifdef WITH_GNU
559 			printf(errstr[9], getprogname(), VERSION);
560 #else
561 			printf(errstr[8], getprogname(), VERSION);
562 #endif
563 			exit(0);
564 		case 'v':
565 			vflag = true;
566 			break;
567 		case 'w':
568 			wflag = true;
569 			cflags &= ~REG_NOSUB;
570 			break;
571 		case 'x':
572 			xflag = true;
573 			cflags &= ~REG_NOSUB;
574 			break;
575 		case 'z':
576 			fileeol = '\0';
577 			break;
578 		case BIN_OPT:
579 			if (strcasecmp("binary", optarg) == 0)
580 				binbehave = BINFILE_BIN;
581 			else if (strcasecmp("without-match", optarg) == 0)
582 				binbehave = BINFILE_SKIP;
583 			else if (strcasecmp("text", optarg) == 0)
584 				binbehave = BINFILE_TEXT;
585 			else
586 				errx(2, errstr[2], "--binary-files");
587 			break;
588 		case COLOR_OPT:
589 			color = NULL;
590 			if (optarg == NULL || strcasecmp("auto", optarg) == 0 ||
591 			    strcasecmp("tty", optarg) == 0 ||
592 			    strcasecmp("if-tty", optarg) == 0) {
593 				char *term;
594 
595 				term = getenv("TERM");
596 				if (isatty(STDOUT_FILENO) && term != NULL &&
597 				    strcasecmp(term, "dumb") != 0)
598 					color = init_color("01;31");
599 			} else if (strcasecmp("always", optarg) == 0 ||
600 			    strcasecmp("yes", optarg) == 0 ||
601 			    strcasecmp("force", optarg) == 0) {
602 				color = init_color("01;31");
603 			} else if (strcasecmp("never", optarg) != 0 &&
604 			    strcasecmp("none", optarg) != 0 &&
605 			    strcasecmp("no", optarg) != 0)
606 				errx(2, errstr[2], "--color");
607 			cflags &= ~REG_NOSUB;
608 			break;
609 		case LABEL_OPT:
610 			label = optarg;
611 			break;
612 		case LINEBUF_OPT:
613 			lbflag = true;
614 			break;
615 		case NULL_OPT:
616 			nullflag = true;
617 			break;
618 		case R_INCLUDE_OPT:
619 			finclude = true;
620 			add_fpattern(optarg, INCL_PAT);
621 			break;
622 		case R_EXCLUDE_OPT:
623 			fexclude = true;
624 			add_fpattern(optarg, EXCL_PAT);
625 			break;
626 		case R_DINCLUDE_OPT:
627 			dinclude = true;
628 			add_dpattern(optarg, INCL_PAT);
629 			break;
630 		case R_DEXCLUDE_OPT:
631 			dexclude = true;
632 			add_dpattern(optarg, EXCL_PAT);
633 			break;
634 		case HELP_OPT:
635 		default:
636 			usage();
637 		}
638 		lastc = c;
639 		newarg = optind != prevoptind;
640 		prevoptind = optind;
641 	}
642 	aargc -= optind;
643 	aargv += optind;
644 
645 	/* Empty pattern file matches nothing */
646 	if (!needpattern && (patterns == 0) && !matchall)
647 		exit(1);
648 
649 	/* Fail if we don't have any pattern */
650 	if (aargc == 0 && needpattern)
651 		usage();
652 
653 	/* Process patterns from command line */
654 	if (aargc != 0 && needpattern) {
655 		char *token;
656 		char *string = *aargv;
657 
658 		while ((token = strsep(&string, "\n")) != NULL)
659 			add_pattern(token, strlen(token));
660 		--aargc;
661 		++aargv;
662 	}
663 
664 	switch (grepbehave) {
665 	case GREP_BASIC:
666 		break;
667 	case GREP_FIXED:
668 		/*
669 		 * regex(3) implementations that support fixed-string searches generally
670 		 * define either REG_NOSPEC or REG_LITERAL. Set the appropriate flag
671 		 * here. If neither are defined, GREP_FIXED later implies that the
672 		 * internal literal matcher should be used. Other cflags that have
673 		 * the same interpretation as REG_NOSPEC and REG_LITERAL should be
674 		 * similarly added here, and grep.h should be amended to take this into
675 		 * consideration when defining WITH_INTERNAL_NOSPEC.
676 		 */
677 #if defined(REG_NOSPEC)
678 		cflags |= REG_NOSPEC;
679 #elif defined(REG_LITERAL)
680 		cflags |= REG_LITERAL;
681 #endif
682 		break;
683 	case GREP_EXTENDED:
684 		cflags |= REG_EXTENDED;
685 		break;
686 	default:
687 		/* NOTREACHED */
688 		usage();
689 	}
690 
691 	r_pattern = grep_calloc(patterns, sizeof(*r_pattern));
692 
693 #ifdef WITH_INTERNAL_NOSPEC
694 	if (grepbehave != GREP_FIXED) {
695 #else
696 	{
697 #endif
698 		/* Check if cheating is allowed (always is for fgrep). */
699 		for (i = 0; i < patterns; ++i) {
700 			c = regcomp(&r_pattern[i], pattern[i].pat, cflags);
701 			if (c != 0) {
702 				regerror(c, &r_pattern[i], re_error,
703 				    RE_ERROR_BUF);
704 				errx(2, "%s", re_error);
705 			}
706 		}
707 	}
708 
709 	if (lbflag)
710 		setlinebuf(stdout);
711 
712 	if ((aargc == 0 || aargc == 1) && !Hflag)
713 		hflag = true;
714 
715 	if (aargc == 0 && dirbehave != DIR_RECURSE)
716 		exit(!procfile("-"));
717 
718 	if (dirbehave == DIR_RECURSE)
719 		matched = grep_tree(aargv);
720 	else
721 		for (matched = false; aargc--; ++aargv) {
722 			if ((finclude || fexclude) && !file_matching(*aargv))
723 				continue;
724 			if (procfile(*aargv))
725 				matched = true;
726 		}
727 
728 	if (Lflag)
729 		matched = !matched;
730 
731 	/*
732 	 * Calculate the correct return value according to the
733 	 * results and the command line option.
734 	 */
735 	exit(matched ? (file_err ? (qflag ? 0 : 2) : 0) : (file_err ? 2 : 1));
736 }
737