Annotation of src/usr.bin/grep/grep.c, Revision 1.62
1.62 ! tedu 1: /* $OpenBSD: grep.c,v 1.61 2019/10/07 17:47:32 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>
30: #include <sys/stat.h>
1.33 jaredy 31: #include <sys/queue.h>
1.1 deraadt 32:
1.18 millert 33: #include <ctype.h>
1.3 deraadt 34: #include <err.h>
35: #include <errno.h>
36: #include <getopt.h>
37: #include <regex.h>
1.1 deraadt 38: #include <stdio.h>
39: #include <stdlib.h>
1.3 deraadt 40: #include <string.h>
1.1 deraadt 41: #include <unistd.h>
42:
1.3 deraadt 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;
1.13 tedu 53: fastgrep_t *fg_pattern;
1.3 deraadt 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 */
1.43 tedu 63: int Hflag; /* -H: always print filename header */
1.3 deraadt 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 */
1.56 pirofti 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 */
1.3 deraadt 75: int nflag; /* -n: show line numbers in front of matching lines */
1.44 tedu 76: int oflag; /* -o: print each match */
1.3 deraadt 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 */
1.27 otto 82: int lbflag; /* --line-buffered */
1.61 tedu 83: const char *labelname; /* --label=name */
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,
1.61 tedu 91: LINEBUF_OPT,
92: LABEL_OPT,
1.9 tedu 93: };
94:
1.3 deraadt 95: /* Housekeeping */
1.10 tedu 96: int first; /* flag whether or not this is our first match */
1.3 deraadt 97: int tail; /* lines left to print */
1.45 millert 98: int file_err; /* file reading error */
1.3 deraadt 99:
1.33 jaredy 100: struct patfile {
101: const char *pf_file;
102: SLIST_ENTRY(patfile) pf_next;
103: };
104: SLIST_HEAD(, patfile) patfilelh;
105:
1.8 tedu 106: extern char *__progname;
1.3 deraadt 107:
108: static void
109: usage(void)
110: {
1.5 deraadt 111: fprintf(stderr,
112: #ifdef NOZ
1.57 jmc 113: "usage: %s [-abcEFGHhIiLlnoqRsUVvwx] [-A num] [-B num] [-C[num]]"
1.5 deraadt 114: #else
1.57 jmc 115: "usage: %s [-abcEFGHhIiLlnoqRsUVvwxZ] [-A num] [-B num] [-C[num]]"
1.5 deraadt 116: #endif
1.57 jmc 117: " [-e pattern]\n"
118: "\t[-f file] [-m num] [--binary-files=value] [--context[=num]]\n"
1.62 ! tedu 119: "\t[--label=name] [--line-buffered] [pattern] [file ...]\n",
1.56 pirofti 120: __progname);
1.3 deraadt 121: exit(2);
122: }
123:
1.5 deraadt 124: #ifdef NOZ
1.56 pirofti 125: static const char optstr[] = "0123456789A:B:CEFGHILRUVabce:f:hilm:noqrsuvwxy";
1.5 deraadt 126: #else
1.56 pirofti 127: static const char optstr[] = "0123456789A:B:CEFGHILRUVZabce:f:hilm:noqrsuvwxy";
1.5 deraadt 128: #endif
1.3 deraadt 129:
1.46 millert 130: static const struct option long_options[] =
1.3 deraadt 131: {
1.19 deraadt 132: {"binary-files", required_argument, NULL, BIN_OPT},
133: {"help", no_argument, NULL, HELP_OPT},
134: {"mmap", no_argument, NULL, MMAP_OPT},
1.61 tedu 135: {"label", required_argument, NULL, LABEL_OPT},
1.27 otto 136: {"line-buffered", no_argument, NULL, LINEBUF_OPT},
1.19 deraadt 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'},
1.43 tedu 144: {"with-filename", no_argument, NULL, 'H'},
1.19 deraadt 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'},
1.56 pirofti 156: {"max-count", required_argument, NULL, 'm'},
1.19 deraadt 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'},
1.5 deraadt 166: #ifndef NOZ
1.19 deraadt 167: {"decompress", no_argument, NULL, 'Z'},
1.5 deraadt 168: #endif
1.19 deraadt 169: {NULL, no_argument, NULL, 0}
1.3 deraadt 170: };
171:
1.1 deraadt 172:
1.3 deraadt 173: static void
174: add_pattern(char *pat, size_t len)
175: {
1.36 jaredy 176: if (!xflag && (len == 0 || matchall)) {
1.3 deraadt 177: matchall = 1;
178: return;
179: }
180: if (patterns == pattern_sz) {
181: pattern_sz *= 2;
1.48 deraadt 182: pattern = grep_reallocarray(pattern, ++pattern_sz, sizeof(*pattern));
1.3 deraadt 183: }
1.37 ray 184: if (len > 0 && pat[len - 1] == '\n')
1.3 deraadt 185: --len;
1.15 tedu 186: /* pat may not be NUL-terminated */
1.28 millert 187: if (wflag && !Fflag) {
1.32 otto 188: int bol = 0, eol = 0, extra;
1.26 millert 189: if (pat[0] == '^')
190: bol = 1;
1.36 jaredy 191: if (len > 0 && pat[len - 1] == '$')
1.26 millert 192: eol = 1;
1.32 otto 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 ? ")" : "\\)",
1.26 millert 201: eol ? "$" : "");
1.32 otto 202: len += 14 + extra;
1.25 millert 203: } else {
204: pattern[patterns] = grep_malloc(len + 1);
205: memcpy(pattern[patterns], pat, len);
206: pattern[patterns][len] = '\0';
207: }
1.3 deraadt 208: ++patterns;
209: }
1.1 deraadt 210:
1.3 deraadt 211: static void
1.35 otto 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
1.33 jaredy 224: read_patterns(const char *fn)
1.3 deraadt 225: {
226: FILE *f;
227: char *line;
1.59 tedu 228: ssize_t len;
229: size_t linesize;
1.3 deraadt 230:
231: if ((f = fopen(fn, "r")) == NULL)
1.20 millert 232: err(2, "%s", fn);
1.59 tedu 233: line = NULL;
234: linesize = 0;
235: while ((len = getline(&line, &linesize, f)) != -1)
1.38 kili 236: add_pattern(line, *line == '\n' ? 0 : len);
1.3 deraadt 237: if (ferror(f))
1.20 millert 238: err(2, "%s", fn);
1.3 deraadt 239: fclose(f);
1.59 tedu 240: free(line);
1.3 deraadt 241: }
1.1 deraadt 242:
243: int
1.3 deraadt 244: main(int argc, char *argv[])
1.1 deraadt 245: {
1.41 jacekm 246: int c, lastc, prevoptind, newarg, i, needpattern, exprs, expr_sz;
1.33 jaredy 247: struct patfile *patfile, *pf_next;
1.21 millert 248: long l;
1.52 dlg 249: char **expr;
250: const char *errstr;
1.53 deraadt 251:
1.54 deraadt 252: if (pledge("stdio rpath", NULL) == -1)
1.55 gsoares 253: err(2, "pledge");
1.3 deraadt 254:
1.33 jaredy 255: SLIST_INIT(&patfilelh);
1.16 tedu 256: switch (__progname[0]) {
257: case 'e':
1.49 tedu 258: Eflag = 1;
1.16 tedu 259: break;
260: case 'f':
1.49 tedu 261: Fflag = 1;
1.16 tedu 262: break;
263: #ifndef NOZ
264: case 'z':
1.49 tedu 265: Zflag = 1;
1.16 tedu 266: switch(__progname[1]) {
267: case 'e':
1.49 tedu 268: Eflag = 1;
1.16 tedu 269: break;
270: case 'f':
1.49 tedu 271: Fflag = 1;
1.16 tedu 272: break;
273: }
274: break;
275: #endif
276: }
277:
1.18 millert 278: lastc = '\0';
1.23 millert 279: newarg = 1;
280: prevoptind = 1;
1.38 kili 281: needpattern = 1;
1.41 jacekm 282: expr_sz = exprs = 0;
283: expr = NULL;
1.5 deraadt 284: while ((c = getopt_long(argc, argv, optstr,
1.17 millert 285: long_options, NULL)) != -1) {
1.3 deraadt 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':
1.23 millert 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');
1.3 deraadt 294: break;
295: case 'A':
296: case 'B':
1.52 dlg 297: l = strtonum(optarg, 1, INT_MAX, &errstr);
298: if (errstr != NULL)
299: errx(2, "context %s", errstr);
1.21 millert 300: if (c == 'A')
301: Aflag = (int)l;
302: else
303: Bflag = (int)l;
1.3 deraadt 304: break;
305: case 'C':
1.5 deraadt 306: if (optarg == NULL)
1.3 deraadt 307: Aflag = Bflag = 2;
1.21 millert 308: else {
1.52 dlg 309: l = strtonum(optarg, 1, INT_MAX, &errstr);
310: if (errstr != NULL)
311: errx(2, "context %s", errstr);
1.21 millert 312: Aflag = Bflag = (int)l;
313: }
1.1 deraadt 314: break;
315: case 'E':
1.49 tedu 316: Fflag = 0;
317: Eflag = 1;
1.1 deraadt 318: break;
319: case 'F':
1.49 tedu 320: Eflag = 0;
321: Fflag = 1;
1.3 deraadt 322: break;
323: case 'G':
1.16 tedu 324: Eflag = Fflag = 0;
1.1 deraadt 325: break;
1.43 tedu 326: case 'H':
1.49 tedu 327: Hflag = 1;
1.43 tedu 328: break;
1.9 tedu 329: case 'I':
330: binbehave = BIN_FILE_SKIP;
331: break;
1.1 deraadt 332: case 'L':
1.3 deraadt 333: lflag = 0;
334: Lflag = qflag = 1;
1.1 deraadt 335: break;
336: case 'R':
1.3 deraadt 337: case 'r':
1.49 tedu 338: Rflag = 1;
1.3 deraadt 339: break;
340: case 'U':
1.9 tedu 341: binbehave = BIN_FILE_BIN;
1.3 deraadt 342: break;
343: case 'V':
344: fprintf(stderr, "grep version %u.%u\n", VER_MAJ, VER_MIN);
1.9 tedu 345: exit(0);
1.1 deraadt 346: break;
1.5 deraadt 347: #ifndef NOZ
1.3 deraadt 348: case 'Z':
1.49 tedu 349: Zflag = 1;
1.1 deraadt 350: break;
1.5 deraadt 351: #endif
1.1 deraadt 352: case 'a':
1.9 tedu 353: binbehave = BIN_FILE_TEXT;
1.1 deraadt 354: break;
355: case 'b':
1.3 deraadt 356: bflag = 1;
1.1 deraadt 357: break;
358: case 'c':
1.3 deraadt 359: cflag = 1;
1.1 deraadt 360: break;
361: case 'e':
1.41 jacekm 362: /* defer adding of expressions until all arguments are parsed */
363: if (exprs == expr_sz) {
364: expr_sz *= 2;
1.48 deraadt 365: expr = grep_reallocarray(expr, ++expr_sz,
366: sizeof(*expr));
1.41 jacekm 367: }
1.38 kili 368: needpattern = 0;
1.41 jacekm 369: expr[exprs] = optarg;
370: ++exprs;
1.1 deraadt 371: break;
372: case 'f':
1.33 jaredy 373: patfile = grep_malloc(sizeof(*patfile));
374: patfile->pf_file = optarg;
375: SLIST_INSERT_HEAD(&patfilelh, patfile, pf_next);
1.38 kili 376: needpattern = 0;
1.1 deraadt 377: break;
378: case 'h':
1.3 deraadt 379: hflag = 1;
1.1 deraadt 380: break;
381: case 'i':
1.3 deraadt 382: case 'y':
1.13 tedu 383: iflag = 1;
1.1 deraadt 384: cflags |= REG_ICASE;
385: break;
386: case 'l':
1.3 deraadt 387: Lflag = 0;
388: lflag = qflag = 1;
1.56 pirofti 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);
1.1 deraadt 397: break;
398: case 'n':
1.3 deraadt 399: nflag = 1;
1.44 tedu 400: break;
401: case 'o':
402: oflag = 1;
1.3 deraadt 403: break;
1.1 deraadt 404: case 'q':
1.3 deraadt 405: qflag = 1;
1.1 deraadt 406: break;
407: case 's':
1.3 deraadt 408: sflag = 1;
1.1 deraadt 409: break;
410: case 'v':
1.3 deraadt 411: vflag = 1;
1.1 deraadt 412: break;
413: case 'w':
1.3 deraadt 414: wflag = 1;
1.1 deraadt 415: break;
416: case 'x':
1.3 deraadt 417: xflag = 1;
1.1 deraadt 418: break;
1.9 tedu 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;
1.61 tedu 433: case LABEL_OPT:
434: labelname = optarg;
435: break;
1.27 otto 436: case LINEBUF_OPT:
437: lbflag = 1;
438: break;
1.9 tedu 439: case HELP_OPT:
1.1 deraadt 440: default:
441: usage();
442: }
1.18 millert 443: lastc = c;
1.23 millert 444: newarg = optind != prevoptind;
1.18 millert 445: prevoptind = optind;
1.1 deraadt 446: }
1.3 deraadt 447: argc -= optind;
448: argv += optind;
1.41 jacekm 449:
450: for (i = 0; i < exprs; i++)
451: add_patterns(expr[i]);
452: free(expr);
453: expr = NULL;
1.33 jaredy 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: }
1.3 deraadt 461:
1.38 kili 462: if (argc == 0 && needpattern)
1.1 deraadt 463: usage();
464:
1.38 kili 465: if (argc != 0 && needpattern) {
1.35 otto 466: add_patterns(*argv);
1.61 tedu 467: --argc;
468: ++argv;
469: }
470: if (argc == 1 && strcmp(*argv, "-") == 0) {
471: /* stdin */
1.3 deraadt 472: --argc;
473: ++argv;
474: }
1.5 deraadt 475:
1.51 millert 476: if (Rflag && argc == 0)
477: warnx("warning: recursive search of stdin");
1.16 tedu 478: if (Eflag)
479: cflags |= REG_EXTENDED;
1.42 tedu 480: if (Fflag)
481: cflags |= REG_NOSPEC;
482: #ifdef SMALL
483: /* Sorry, this won't work */
484: if (Fflag && wflag)
485: errx(1, "Can't use small fgrep with -w");
486: #endif
1.39 deraadt 487: fg_pattern = grep_calloc(patterns, sizeof(*fg_pattern));
488: r_pattern = grep_calloc(patterns, sizeof(*r_pattern));
1.3 deraadt 489: for (i = 0; i < patterns; ++i) {
1.28 millert 490: /* Check if cheating is allowed (always is for fgrep). */
1.42 tedu 491: #ifndef SMALL
1.28 millert 492: if (Fflag) {
493: fgrepcomp(&fg_pattern[i], pattern[i]);
1.42 tedu 494: } else
495: #endif
496: {
1.28 millert 497: if (fastcomp(&fg_pattern[i], pattern[i])) {
498: /* Fall back to full regex library */
499: c = regcomp(&r_pattern[i], pattern[i], cflags);
500: if (c != 0) {
501: regerror(c, &r_pattern[i], re_error,
502: RE_ERROR_BUF);
503: errx(2, "%s", re_error);
504: }
1.13 tedu 505: }
1.1 deraadt 506: }
507: }
1.29 deraadt 508:
1.27 otto 509: if (lbflag)
1.47 millert 510: setvbuf(stdout, NULL, _IOLBF, 0);
1.1 deraadt 511:
1.43 tedu 512: if ((argc == 0 || argc == 1) && !Rflag && !Hflag)
1.3 deraadt 513: hflag = 1;
1.1 deraadt 514:
1.3 deraadt 515: if (argc == 0)
516: exit(!procfile(NULL));
1.5 deraadt 517:
1.3 deraadt 518: if (Rflag)
519: c = grep_tree(argv);
1.1 deraadt 520: else
1.3 deraadt 521: for (c = 0; argc--; ++argv)
522: c += procfile(*argv);
1.1 deraadt 523:
1.45 millert 524: exit(c ? (file_err ? (qflag ? 0 : 2) : 0) : (file_err ? 2 : 1));
1.1 deraadt 525: }