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