xref: /openbsd-src/usr.bin/grep/grep.c (revision 1a8dbaac879b9f3335ad7fb25429ce63ac1d6bac)
1 /*	$OpenBSD: grep.c,v 1.65 2020/07/23 20:19:27 martijn Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999 James Howard and Dag-Erling Co�dan Sm�rgrav
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/queue.h>
32 
33 #include <ctype.h>
34 #include <err.h>
35 #include <errno.h>
36 #include <getopt.h>
37 #include <regex.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #include "grep.h"
44 
45 /* Flags passed to regcomp() and regexec() */
46 int	 cflags;
47 int	 eflags = REG_STARTEND;
48 
49 int	 matchall;	/* shortcut */
50 int	 patterns, pattern_sz;
51 char   **pattern;
52 regex_t	*r_pattern;
53 fastgrep_t *fg_pattern;
54 
55 /* For regex errors  */
56 char	 re_error[RE_ERROR_BUF + 1];
57 
58 /* Command-line flags */
59 int	 Aflag;		/* -A x: print x lines trailing each match */
60 int	 Bflag;		/* -B x: print x lines leading each match */
61 int	 Eflag;		/* -E: interpret pattern as extended regexp */
62 int	 Fflag;		/* -F: interpret pattern as list of fixed strings */
63 int	 Hflag;		/* -H: always print filename header */
64 int	 Lflag;		/* -L: only show names of files with no matches */
65 int	 Rflag;		/* -R: recursively search directory trees */
66 int	 Zflag;		/* -Z: decompress input before processing */
67 int	 bflag;		/* -b: show block numbers for each match */
68 int	 cflag;		/* -c: only show a count of matching lines */
69 int	 hflag;		/* -h: don't print filename headers */
70 int	 iflag;		/* -i: ignore case */
71 int	 lflag;		/* -l: only show names of files with matches */
72 int	 mflag;		/* -m x: stop reading the files after x matches */
73 long long mcount;	/* count for -m */
74 long long mlimit;	/* requested value for -m */
75 int	 nflag;		/* -n: show line numbers in front of matching lines */
76 int	 oflag;		/* -o: print each match */
77 int	 qflag;		/* -q: quiet mode (don't output anything) */
78 int	 sflag;		/* -s: silent mode (ignore errors) */
79 int	 vflag;		/* -v: only show non-matching lines */
80 int	 wflag;		/* -w: pattern must start and end on word boundaries */
81 int	 xflag;		/* -x: pattern must match entire line */
82 int	 lbflag;	/* --line-buffered */
83 const char *labelname;	/* --label=name */
84 
85 int binbehave = BIN_FILE_BIN;
86 
87 enum {
88 	BIN_OPT = CHAR_MAX + 1,
89 	HELP_OPT,
90 	MMAP_OPT,
91 	LINEBUF_OPT,
92 	LABEL_OPT,
93 };
94 
95 /* Housekeeping */
96 int	 first;		/* flag whether or not this is our first match */
97 int	 tail;		/* lines left to print */
98 int	 file_err;	/* file reading error */
99 
100 struct patfile {
101 	const char		*pf_file;
102 	SLIST_ENTRY(patfile)	 pf_next;
103 };
104 SLIST_HEAD(, patfile)		 patfilelh;
105 
106 extern char *__progname;
107 
108 static void
109 usage(void)
110 {
111 	fprintf(stderr,
112 #ifdef NOZ
113 	    "usage: %s [-abcEFGHhIiLlnoqRsUVvwx] [-A num] [-B num] [-C[num]]"
114 #else
115 	    "usage: %s [-abcEFGHhIiLlnoqRsUVvwxZ] [-A num] [-B num] [-C[num]]"
116 #endif
117 	    " [-e pattern]\n"
118 	    "\t[-f file] [-m num] [--binary-files=value] [--context[=num]]\n"
119 	    "\t[--label=name] [--line-buffered] [pattern] [file ...]\n",
120 	    __progname);
121 	exit(2);
122 }
123 
124 #ifdef NOZ
125 static const char optstr[] = "0123456789A:B:CEFGHILRUVabce:f:hilm:noqrsuvwxy";
126 #else
127 static const char optstr[] = "0123456789A:B:CEFGHILRUVZabce:f:hilm:noqrsuvwxy";
128 #endif
129 
130 static const struct option long_options[] =
131 {
132 	{"binary-files",	required_argument,	NULL, BIN_OPT},
133 	{"help",		no_argument,		NULL, HELP_OPT},
134 	{"mmap",		no_argument,		NULL, MMAP_OPT},
135 	{"label",		required_argument,	NULL, LABEL_OPT},
136 	{"line-buffered",	no_argument,		NULL, LINEBUF_OPT},
137 	{"after-context",	required_argument,	NULL, 'A'},
138 	{"before-context",	required_argument,	NULL, 'B'},
139 	{"context",		optional_argument,	NULL, 'C'},
140 	{"devices",		required_argument,	NULL, 'D'},
141 	{"extended-regexp",	no_argument,		NULL, 'E'},
142 	{"fixed-strings",	no_argument,		NULL, 'F'},
143 	{"basic-regexp",	no_argument,		NULL, 'G'},
144 	{"with-filename",	no_argument,		NULL, 'H'},
145 	{"binary",		no_argument,		NULL, 'U'},
146 	{"version",		no_argument,		NULL, 'V'},
147 	{"text",		no_argument,		NULL, 'a'},
148 	{"byte-offset",		no_argument,		NULL, 'b'},
149 	{"count",		no_argument,		NULL, 'c'},
150 	{"regexp",		required_argument,	NULL, 'e'},
151 	{"file",		required_argument,	NULL, 'f'},
152 	{"no-filename",		no_argument,		NULL, 'h'},
153 	{"ignore-case",		no_argument,		NULL, 'i'},
154 	{"files-without-match",	no_argument,		NULL, 'L'},
155 	{"files-with-matches",	no_argument,		NULL, 'l'},
156 	{"max-count",		required_argument,	NULL, 'm'},
157 	{"line-number",		no_argument,		NULL, 'n'},
158 	{"quiet",		no_argument,		NULL, 'q'},
159 	{"silent",		no_argument,		NULL, 'q'},
160 	{"recursive",		no_argument,		NULL, 'r'},
161 	{"no-messages",		no_argument,		NULL, 's'},
162 	{"revert-match",	no_argument,		NULL, 'v'},
163 	{"word-regexp",		no_argument,		NULL, 'w'},
164 	{"line-regexp",		no_argument,		NULL, 'x'},
165 	{"unix-byte-offsets",	no_argument,		NULL, 'u'},
166 #ifndef NOZ
167 	{"decompress",		no_argument,		NULL, 'Z'},
168 #endif
169 	{NULL,			no_argument,		NULL, 0}
170 };
171 
172 
173 static void
174 add_pattern(char *pat, size_t len)
175 {
176 	if (!xflag && (len == 0 || matchall)) {
177 		matchall = 1;
178 		return;
179 	}
180 	if (patterns == pattern_sz) {
181 		pattern_sz *= 2;
182 		pattern = grep_reallocarray(pattern, ++pattern_sz, sizeof(*pattern));
183 	}
184 	if (len > 0 && pat[len - 1] == '\n')
185 		--len;
186 	/* pat may not be NUL-terminated */
187 	if (wflag && !Fflag) {
188 		int bol = 0, eol = 0, extra;
189 		if (pat[0] == '^')
190 			bol = 1;
191 		if (len > 0 && pat[len - 1] == '$')
192 			eol = 1;
193 		extra = Eflag ? 2 : 4;
194 		pattern[patterns] = grep_malloc(len + 15 + extra);
195 		snprintf(pattern[patterns], len + 15 + extra,
196 		   "%s[[:<:]]%s%.*s%s[[:>:]]%s",
197 		    bol ? "^" : "",
198 		    Eflag ? "(" : "\\(",
199 		    (int)len - bol - eol, pat + bol,
200 		    Eflag ? ")" : "\\)",
201 		    eol ? "$" : "");
202 		len += 14 + extra;
203 	} else {
204 		pattern[patterns] = grep_malloc(len + 1);
205 		memcpy(pattern[patterns], pat, len);
206 		pattern[patterns][len] = '\0';
207 	}
208 	++patterns;
209 }
210 
211 static void
212 add_patterns(char *pats)
213 {
214 	char *nl;
215 
216 	while ((nl = strchr(pats, '\n')) != NULL) {
217 		add_pattern(pats, nl - pats);
218 		pats = nl + 1;
219 	}
220 	add_pattern(pats, strlen(pats));
221 }
222 
223 static void
224 read_patterns(const char *fn)
225 {
226 	FILE *f;
227 	char *line;
228 	ssize_t len;
229 	size_t linesize;
230 
231 	if ((f = fopen(fn, "r")) == NULL)
232 		err(2, "%s", fn);
233 	line = NULL;
234 	linesize = 0;
235 	while ((len = getline(&line, &linesize, f)) != -1)
236 		add_pattern(line, *line == '\n' ? 0 : len);
237 	if (ferror(f))
238 		err(2, "%s", fn);
239 	fclose(f);
240 	free(line);
241 }
242 
243 int
244 main(int argc, char *argv[])
245 {
246 	int c, lastc, prevoptind, newarg, i, needpattern, exprs, expr_sz;
247 	struct patfile *patfile, *pf_next;
248 	long l;
249 	char **expr;
250 	const char *errstr;
251 
252 	if (pledge("stdio rpath", NULL) == -1)
253 		err(2, "pledge");
254 
255 	SLIST_INIT(&patfilelh);
256 	switch (__progname[0]) {
257 	case 'e':
258 		Eflag = 1;
259 		break;
260 	case 'f':
261 		Fflag = 1;
262 		break;
263 #ifndef NOZ
264 	case 'z':
265 		Zflag = 1;
266 		switch(__progname[1]) {
267 		case 'e':
268 			Eflag = 1;
269 			break;
270 		case 'f':
271 			Fflag = 1;
272 			break;
273 		}
274 		break;
275 #endif
276 	}
277 
278 	lastc = '\0';
279 	newarg = 1;
280 	prevoptind = 1;
281 	needpattern = 1;
282 	expr_sz = exprs = 0;
283 	expr = NULL;
284 	while ((c = getopt_long(argc, argv, optstr,
285 				long_options, NULL)) != -1) {
286 		switch (c) {
287 		case '0': case '1': case '2': case '3': case '4':
288 		case '5': case '6': case '7': case '8': case '9':
289 			if (newarg || !isdigit(lastc))
290 				Aflag = 0;
291 			else if (Aflag > INT_MAX / 10)
292 				errx(2, "context out of range");
293 			Aflag = Bflag = (Aflag * 10) + (c - '0');
294 			break;
295 		case 'A':
296 		case 'B':
297 			l = strtonum(optarg, 1, INT_MAX, &errstr);
298 			if (errstr != NULL)
299 				errx(2, "context %s", errstr);
300 			if (c == 'A')
301 				Aflag = (int)l;
302 			else
303 				Bflag = (int)l;
304 			break;
305 		case 'C':
306 			if (optarg == NULL)
307 				Aflag = Bflag = 2;
308 			else {
309 				l = strtonum(optarg, 1, INT_MAX, &errstr);
310 				if (errstr != NULL)
311 					errx(2, "context %s", errstr);
312 				Aflag = Bflag = (int)l;
313 			}
314 			break;
315 		case 'E':
316 			Fflag = 0;
317 			Eflag = 1;
318 			break;
319 		case 'F':
320 			Eflag = 0;
321 			Fflag = 1;
322 			break;
323 		case 'G':
324 			Eflag = Fflag = 0;
325 			break;
326 		case 'H':
327 			Hflag = 1;
328 			break;
329 		case 'I':
330 			binbehave = BIN_FILE_SKIP;
331 			break;
332 		case 'L':
333 			lflag = 0;
334 			Lflag = qflag = 1;
335 			break;
336 		case 'R':
337 		case 'r':
338 			Rflag = 1;
339 			break;
340 		case 'U':
341 			binbehave = BIN_FILE_BIN;
342 			break;
343 		case 'V':
344 			fprintf(stderr, "grep version %u.%u\n", VER_MAJ, VER_MIN);
345 			exit(0);
346 			break;
347 #ifndef NOZ
348 		case 'Z':
349 			Zflag = 1;
350 			break;
351 #endif
352 		case 'a':
353 			binbehave = BIN_FILE_TEXT;
354 			break;
355 		case 'b':
356 			bflag = 1;
357 			break;
358 		case 'c':
359 			cflag = 1;
360 			break;
361 		case 'e':
362 			/* defer adding of expressions until all arguments are parsed */
363 			if (exprs == expr_sz) {
364 				expr_sz *= 2;
365 				expr = grep_reallocarray(expr, ++expr_sz,
366 				    sizeof(*expr));
367 			}
368 			needpattern = 0;
369 			expr[exprs] = optarg;
370 			++exprs;
371 			break;
372 		case 'f':
373 			patfile = grep_malloc(sizeof(*patfile));
374 			patfile->pf_file = optarg;
375 			SLIST_INSERT_HEAD(&patfilelh, patfile, pf_next);
376 			needpattern = 0;
377 			break;
378 		case 'h':
379 			hflag = 1;
380 			break;
381 		case 'i':
382 		case 'y':
383 			iflag = 1;
384 			cflags |= REG_ICASE;
385 			break;
386 		case 'l':
387 			Lflag = 0;
388 			lflag = qflag = 1;
389 			break;
390 		case 'm':
391 			mflag = 1;
392 			mlimit = mcount = strtonum(optarg, 0, LLONG_MAX,
393 			   &errstr);
394 			if (errstr != NULL)
395 				errx(2, "invalid max-count %s: %s",
396 				    optarg, errstr);
397 			break;
398 		case 'n':
399 			nflag = 1;
400 			break;
401 		case 'o':
402 			oflag = 1;
403 			break;
404 		case 'q':
405 			qflag = 1;
406 			break;
407 		case 's':
408 			sflag = 1;
409 			break;
410 		case 'v':
411 			vflag = 1;
412 			break;
413 		case 'w':
414 			wflag = 1;
415 			break;
416 		case 'x':
417 			xflag = 1;
418 			break;
419 		case BIN_OPT:
420 			if (strcmp("binary", optarg) == 0)
421 				binbehave = BIN_FILE_BIN;
422 			else if (strcmp("without-match", optarg) == 0)
423 				binbehave = BIN_FILE_SKIP;
424 			else if (strcmp("text", optarg) == 0)
425 				binbehave = BIN_FILE_TEXT;
426 			else
427 				errx(2, "Unknown binary-files option");
428 			break;
429 		case 'u':
430 		case MMAP_OPT:
431 			/* default, compatibility */
432 			break;
433 		case LABEL_OPT:
434 			labelname = optarg;
435 			break;
436 		case LINEBUF_OPT:
437 			lbflag = 1;
438 			break;
439 		case HELP_OPT:
440 		default:
441 			usage();
442 		}
443 		lastc = c;
444 		newarg = optind != prevoptind;
445 		prevoptind = optind;
446 	}
447 	argc -= optind;
448 	argv += optind;
449 
450 	for (i = 0; i < exprs; i++)
451 		add_patterns(expr[i]);
452 	free(expr);
453 	expr = NULL;
454 
455 	for (patfile = SLIST_FIRST(&patfilelh); patfile != NULL;
456 	    patfile = pf_next) {
457 		pf_next = SLIST_NEXT(patfile, pf_next);
458 		read_patterns(patfile->pf_file);
459 		free(patfile);
460 	}
461 
462 	if (argc == 0 && needpattern)
463 		usage();
464 
465 	if (argc != 0 && needpattern) {
466 		add_patterns(*argv);
467 		--argc;
468 		++argv;
469 	}
470 	if (argc == 1 && strcmp(*argv, "-") == 0) {
471 		/* stdin */
472 		--argc;
473 		++argv;
474 	}
475 
476 	if (Eflag)
477 		cflags |= REG_EXTENDED;
478 	if (Fflag)
479 		cflags |= REG_NOSPEC;
480 #ifdef SMALL
481 	/* Sorry, this won't work */
482 	if (Fflag && wflag)
483 		errx(1, "Can't use small fgrep with -w");
484 #endif
485 	fg_pattern = grep_calloc(patterns, sizeof(*fg_pattern));
486 	r_pattern = grep_calloc(patterns, sizeof(*r_pattern));
487 	for (i = 0; i < patterns; ++i) {
488 		/* Check if cheating is allowed (always is for fgrep). */
489 #ifndef SMALL
490 		if (Fflag) {
491 			fgrepcomp(&fg_pattern[i], pattern[i]);
492 		} else
493 #endif
494 		{
495 			if (fastcomp(&fg_pattern[i], pattern[i])) {
496 				/* Fall back to full regex library */
497 				c = regcomp(&r_pattern[i], pattern[i], cflags);
498 				if (c != 0) {
499 					regerror(c, &r_pattern[i], re_error,
500 					    RE_ERROR_BUF);
501 					errx(2, "%s", re_error);
502 				}
503 			}
504 		}
505 	}
506 
507 	if (lbflag)
508 		setvbuf(stdout, NULL, _IOLBF, 0);
509 
510 	if ((argc == 0 || argc == 1) && !Rflag && !Hflag)
511 		hflag = 1;
512 
513 	if (argc == 0 && !Rflag)
514 		exit(!procfile(NULL));
515 
516 	if (Rflag)
517 		c = grep_tree(argv);
518 	else
519 		for (c = 0; argc--; ++argv)
520 			c |= procfile(*argv);
521 
522 	exit(c ? (file_err ? (qflag ? 0 : 2) : 0) : (file_err ? 2 : 1));
523 }
524