Annotation of src/usr.bin/grep/grep.c, Revision 1.45
1.45 ! millert 1: /* $OpenBSD: grep.c,v 1.44 2011/07/08 01:20:24 tedu Exp $ */
1.6 deraadt 2:
1.1 deraadt 3: /*-
1.3 deraadt 4: * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
5: * All rights reserved.
1.1 deraadt 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:
1.3 deraadt 29: #include <sys/types.h>
1.9 tedu 30: #include <sys/limits.h>
1.3 deraadt 31: #include <sys/stat.h>
1.33 jaredy 32: #include <sys/queue.h>
1.1 deraadt 33:
1.18 millert 34: #include <ctype.h>
1.3 deraadt 35: #include <err.h>
36: #include <errno.h>
37: #include <getopt.h>
38: #include <regex.h>
1.1 deraadt 39: #include <stdio.h>
40: #include <stdlib.h>
1.3 deraadt 41: #include <string.h>
1.1 deraadt 42: #include <unistd.h>
43:
1.3 deraadt 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;
1.13 tedu 54: fastgrep_t *fg_pattern;
1.3 deraadt 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 */
1.43 tedu 65: int Hflag; /* -H: always print filename header */
1.3 deraadt 66: int Lflag; /* -L: only show names of files with no matches */
67: int Rflag; /* -R: recursively search directory trees */
1.5 deraadt 68: #ifndef NOZ
1.3 deraadt 69: int Zflag; /* -Z: decompress input before processing */
1.5 deraadt 70: #endif
1.3 deraadt 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 */
1.44 tedu 77: int oflag; /* -o: print each match */
1.3 deraadt 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 */
1.27 otto 83: int lbflag; /* --line-buffered */
1.3 deraadt 84:
1.9 tedu 85: int binbehave = BIN_FILE_BIN;
86:
87: enum {
88: BIN_OPT = CHAR_MAX + 1,
89: HELP_OPT,
1.27 otto 90: MMAP_OPT,
91: LINEBUF_OPT
1.9 tedu 92: };
93:
1.3 deraadt 94: /* Housekeeping */
1.10 tedu 95: int first; /* flag whether or not this is our first match */
1.3 deraadt 96: int tail; /* lines left to print */
1.45 ! millert 97: int file_err; /* file reading error */
1.3 deraadt 98:
1.33 jaredy 99: struct patfile {
100: const char *pf_file;
101: SLIST_ENTRY(patfile) pf_next;
102: };
103: SLIST_HEAD(, patfile) patfilelh;
104:
1.8 tedu 105: extern char *__progname;
1.3 deraadt 106:
107: static void
108: usage(void)
109: {
1.5 deraadt 110: fprintf(stderr,
111: #ifdef NOZ
1.44 tedu 112: "usage: %s [-abcEFGHhIiLlnoqRsUVvwx] [-A num] [-B num] [-C[num]]\n"
1.5 deraadt 113: #else
1.44 tedu 114: "usage: %s [-abcEFGHhIiLlnoqRsUVvwxZ] [-A num] [-B num] [-C[num]]\n"
1.5 deraadt 115: #endif
1.30 jmc 116: "\t[-e pattern] [-f file] [--binary-files=value] [--context[=num]]\n"
117: "\t[--line-buffered] [pattern] [file ...]\n", __progname);
1.3 deraadt 118: exit(2);
119: }
120:
1.5 deraadt 121: #ifdef NOZ
1.44 tedu 122: static char *optstr = "0123456789A:B:CEFGHILRUVabce:f:hilnoqrsuvwxy";
1.5 deraadt 123: #else
1.44 tedu 124: static char *optstr = "0123456789A:B:CEFGHILRUVZabce:f:hilnoqrsuvwxy";
1.5 deraadt 125: #endif
1.3 deraadt 126:
1.5 deraadt 127: struct option long_options[] =
1.3 deraadt 128: {
1.19 deraadt 129: {"binary-files", required_argument, NULL, BIN_OPT},
130: {"help", no_argument, NULL, HELP_OPT},
131: {"mmap", no_argument, NULL, MMAP_OPT},
1.27 otto 132: {"line-buffered", no_argument, NULL, LINEBUF_OPT},
1.19 deraadt 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'},
1.43 tedu 140: {"with-filename", no_argument, NULL, 'H'},
1.19 deraadt 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'},
1.5 deraadt 161: #ifndef NOZ
1.19 deraadt 162: {"decompress", no_argument, NULL, 'Z'},
1.5 deraadt 163: #endif
1.19 deraadt 164: {NULL, no_argument, NULL, 0}
1.3 deraadt 165: };
166:
1.1 deraadt 167:
1.3 deraadt 168: static void
169: add_pattern(char *pat, size_t len)
170: {
1.36 jaredy 171: if (!xflag && (len == 0 || matchall)) {
1.3 deraadt 172: matchall = 1;
173: return;
174: }
175: if (patterns == pattern_sz) {
176: pattern_sz *= 2;
1.22 millert 177: pattern = grep_realloc(pattern, ++pattern_sz * sizeof(*pattern));
1.3 deraadt 178: }
1.37 ray 179: if (len > 0 && pat[len - 1] == '\n')
1.3 deraadt 180: --len;
1.15 tedu 181: /* pat may not be NUL-terminated */
1.28 millert 182: if (wflag && !Fflag) {
1.32 otto 183: int bol = 0, eol = 0, extra;
1.26 millert 184: if (pat[0] == '^')
185: bol = 1;
1.36 jaredy 186: if (len > 0 && pat[len - 1] == '$')
1.26 millert 187: eol = 1;
1.32 otto 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 ? ")" : "\\)",
1.26 millert 196: eol ? "$" : "");
1.32 otto 197: len += 14 + extra;
1.25 millert 198: } else {
199: pattern[patterns] = grep_malloc(len + 1);
200: memcpy(pattern[patterns], pat, len);
201: pattern[patterns][len] = '\0';
202: }
1.3 deraadt 203: ++patterns;
204: }
1.1 deraadt 205:
1.3 deraadt 206: static void
1.35 otto 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
1.33 jaredy 219: read_patterns(const char *fn)
1.3 deraadt 220: {
221: FILE *f;
222: char *line;
223: size_t len;
224:
225: if ((f = fopen(fn, "r")) == NULL)
1.20 millert 226: err(2, "%s", fn);
1.38 kili 227: while ((line = fgetln(f, &len)) != NULL)
228: add_pattern(line, *line == '\n' ? 0 : len);
1.3 deraadt 229: if (ferror(f))
1.20 millert 230: err(2, "%s", fn);
1.3 deraadt 231: fclose(f);
232: }
1.1 deraadt 233:
234: int
1.3 deraadt 235: main(int argc, char *argv[])
1.1 deraadt 236: {
1.41 jacekm 237: int c, lastc, prevoptind, newarg, i, needpattern, exprs, expr_sz;
1.33 jaredy 238: struct patfile *patfile, *pf_next;
1.21 millert 239: long l;
1.41 jacekm 240: char *ep, **expr;
1.3 deraadt 241:
1.33 jaredy 242: SLIST_INIT(&patfilelh);
1.16 tedu 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:
1.18 millert 271: lastc = '\0';
1.23 millert 272: newarg = 1;
273: prevoptind = 1;
1.38 kili 274: needpattern = 1;
1.41 jacekm 275: expr_sz = exprs = 0;
276: expr = NULL;
1.5 deraadt 277: while ((c = getopt_long(argc, argv, optstr,
1.17 millert 278: long_options, NULL)) != -1) {
1.3 deraadt 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':
1.23 millert 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');
1.3 deraadt 287: break;
288: case 'A':
289: case 'B':
1.21 millert 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;
1.3 deraadt 298: break;
299: case 'C':
1.5 deraadt 300: if (optarg == NULL)
1.3 deraadt 301: Aflag = Bflag = 2;
1.21 millert 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: }
1.1 deraadt 309: break;
310: case 'E':
1.16 tedu 311: Fflag = Gflag = 0;
1.3 deraadt 312: Eflag++;
1.1 deraadt 313: break;
314: case 'F':
1.16 tedu 315: Eflag = Gflag = 0;
1.3 deraadt 316: Fflag++;
317: break;
318: case 'G':
1.16 tedu 319: Eflag = Fflag = 0;
1.3 deraadt 320: Gflag++;
1.1 deraadt 321: break;
1.43 tedu 322: case 'H':
323: Hflag++;
324: break;
1.9 tedu 325: case 'I':
326: binbehave = BIN_FILE_SKIP;
327: break;
1.1 deraadt 328: case 'L':
1.3 deraadt 329: lflag = 0;
330: Lflag = qflag = 1;
1.1 deraadt 331: break;
332: case 'R':
1.3 deraadt 333: case 'r':
334: Rflag++;
335: break;
336: case 'U':
1.9 tedu 337: binbehave = BIN_FILE_BIN;
1.3 deraadt 338: break;
339: case 'V':
340: fprintf(stderr, "grep version %u.%u\n", VER_MAJ, VER_MIN);
1.9 tedu 341: exit(0);
1.1 deraadt 342: break;
1.5 deraadt 343: #ifndef NOZ
1.3 deraadt 344: case 'Z':
345: Zflag++;
1.1 deraadt 346: break;
1.5 deraadt 347: #endif
1.1 deraadt 348: case 'a':
1.9 tedu 349: binbehave = BIN_FILE_TEXT;
1.1 deraadt 350: break;
351: case 'b':
1.3 deraadt 352: bflag = 1;
1.1 deraadt 353: break;
354: case 'c':
1.3 deraadt 355: cflag = 1;
1.1 deraadt 356: break;
357: case 'e':
1.41 jacekm 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: }
1.38 kili 363: needpattern = 0;
1.41 jacekm 364: expr[exprs] = optarg;
365: ++exprs;
1.1 deraadt 366: break;
367: case 'f':
1.33 jaredy 368: patfile = grep_malloc(sizeof(*patfile));
369: patfile->pf_file = optarg;
370: SLIST_INSERT_HEAD(&patfilelh, patfile, pf_next);
1.38 kili 371: needpattern = 0;
1.1 deraadt 372: break;
373: case 'h':
1.3 deraadt 374: hflag = 1;
1.1 deraadt 375: break;
376: case 'i':
1.3 deraadt 377: case 'y':
1.13 tedu 378: iflag = 1;
1.1 deraadt 379: cflags |= REG_ICASE;
380: break;
381: case 'l':
1.3 deraadt 382: Lflag = 0;
383: lflag = qflag = 1;
1.1 deraadt 384: break;
385: case 'n':
1.3 deraadt 386: nflag = 1;
1.44 tedu 387: break;
388: case 'o':
389: oflag = 1;
1.3 deraadt 390: break;
1.1 deraadt 391: case 'q':
1.3 deraadt 392: qflag = 1;
1.1 deraadt 393: break;
394: case 's':
1.3 deraadt 395: sflag = 1;
1.1 deraadt 396: break;
397: case 'v':
1.3 deraadt 398: vflag = 1;
1.1 deraadt 399: break;
400: case 'w':
1.3 deraadt 401: wflag = 1;
1.1 deraadt 402: break;
403: case 'x':
1.3 deraadt 404: xflag = 1;
1.1 deraadt 405: break;
1.9 tedu 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;
1.27 otto 420: case LINEBUF_OPT:
421: lbflag = 1;
422: break;
1.9 tedu 423: case HELP_OPT:
1.1 deraadt 424: default:
425: usage();
426: }
1.18 millert 427: lastc = c;
1.23 millert 428: newarg = optind != prevoptind;
1.18 millert 429: prevoptind = optind;
1.1 deraadt 430: }
1.3 deraadt 431: argc -= optind;
432: argv += optind;
1.41 jacekm 433:
434: for (i = 0; i < exprs; i++)
435: add_patterns(expr[i]);
436: free(expr);
437: expr = NULL;
1.33 jaredy 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: }
1.3 deraadt 445:
1.38 kili 446: if (argc == 0 && needpattern)
1.1 deraadt 447: usage();
448:
1.38 kili 449: if (argc != 0 && needpattern) {
1.35 otto 450: add_patterns(*argv);
1.3 deraadt 451: --argc;
452: ++argv;
453: }
1.5 deraadt 454:
1.16 tedu 455: if (Eflag)
456: cflags |= REG_EXTENDED;
1.42 tedu 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
1.39 deraadt 464: fg_pattern = grep_calloc(patterns, sizeof(*fg_pattern));
465: r_pattern = grep_calloc(patterns, sizeof(*r_pattern));
1.3 deraadt 466: for (i = 0; i < patterns; ++i) {
1.28 millert 467: /* Check if cheating is allowed (always is for fgrep). */
1.42 tedu 468: #ifndef SMALL
1.28 millert 469: if (Fflag) {
470: fgrepcomp(&fg_pattern[i], pattern[i]);
1.42 tedu 471: } else
472: #endif
473: {
1.28 millert 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: }
1.13 tedu 482: }
1.1 deraadt 483: }
484: }
1.29 deraadt 485:
1.27 otto 486: if (lbflag)
487: setlinebuf(stdout);
1.1 deraadt 488:
1.43 tedu 489: if ((argc == 0 || argc == 1) && !Rflag && !Hflag)
1.3 deraadt 490: hflag = 1;
1.1 deraadt 491:
1.3 deraadt 492: if (argc == 0)
493: exit(!procfile(NULL));
1.5 deraadt 494:
1.3 deraadt 495: if (Rflag)
496: c = grep_tree(argv);
1.1 deraadt 497: else
1.3 deraadt 498: for (c = 0; argc--; ++argv)
499: c += procfile(*argv);
1.1 deraadt 500:
1.45 ! millert 501: exit(c ? (file_err ? (qflag ? 0 : 2) : 0) : (file_err ? 2 : 1));
1.1 deraadt 502: }