1 /* $NetBSD: grep.c,v 1.6 2011/04/18 03:48:23 joerg Exp $ */
2 /* $OpenBSD: grep.c,v 1.42 2010/07/02 22:18:03 tedu Exp $ */
5 * SPDX-License-Identifier: BSD-2-Clause
7 * Copyright (c) 1999 James Howard and Dag-Erling Smørgrav
8 * Copyright (C) 2008-2009 Gabor Kovesdan <gabor@FreeBSD.org>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
35 #include <sys/types.h>
53 const char *errstr[] = {
55 /* 1*/ "(standard input)",
56 /* 2*/ "unknown %s option",
57 /* 3*/ "usage: %s [-abcDEFGHhIiLlmnOopqRSsUVvwxz] [-A num] [-B num] [-C num]\n",
58 /* 4*/ "\t[-e pattern] [-f file] [--binary-files=value] [--color=when]\n",
59 /* 5*/ "\t[--context=num] [--directories=action] [--label] [--line-buffered]\n",
60 /* 6*/ "\t[--null] [pattern] [file ...]\n",
61 /* 7*/ "Binary file %s matches\n",
62 /* 8*/ "%s (BSD grep, GNU compatible) %s\n",
65 /* Flags passed to regcomp() and regexec() */
66 int cflags = REG_NOSUB | REG_NEWLINE;
67 int eflags = REG_STARTEND;
71 /* Searching patterns */
72 unsigned int patterns;
73 static unsigned int pattern_sz;
77 /* Filename exclusion/inclusion patterns */
78 unsigned int fpatterns, dpatterns;
79 static unsigned int fpattern_sz, dpattern_sz;
80 struct epat *dpattern, *fpattern;
82 /* For regex errors */
83 char re_error[RE_ERROR_BUF + 1];
85 /* Command-line flags */
86 long long Aflag; /* -A x: print x lines trailing each match */
87 long long Bflag; /* -B x: print x lines leading each match */
88 bool Hflag; /* -H: always print file name */
89 bool Lflag; /* -L: only show names of files with no matches */
90 bool bflag; /* -b: show block numbers for each match */
91 bool cflag; /* -c: only show a count of matching lines */
92 bool hflag; /* -h: don't print filename headers */
93 bool iflag; /* -i: ignore case */
94 bool lflag; /* -l: only show names of files with matches */
95 bool mflag; /* -m x: stop reading the files after x matches */
96 long long mcount; /* count for -m */
97 long long mlimit; /* requested value for -m */
98 char fileeol; /* indicator for eol */
99 bool nflag; /* -n: show line numbers in front of matching lines */
100 bool oflag; /* -o: print only matching part */
101 bool qflag; /* -q: quiet mode (don't output anything) */
102 bool sflag; /* -s: silent mode (ignore errors) */
103 bool vflag; /* -v: only show non-matching lines */
104 bool wflag; /* -w: pattern must start and end on word boundaries */
105 bool xflag; /* -x: pattern must match entire line */
106 bool lbflag; /* --line-buffered */
107 bool nullflag; /* --null */
108 char *label; /* --label */
109 const char *color; /* --color */
110 int grepbehave = GREP_BASIC; /* -EFG: type of the regex */
111 int binbehave = BINFILE_BIN; /* -aIU: handling of binary files */
112 int filebehave = FILE_STDIO;
113 int devbehave = DEV_READ; /* -D: handling of devices */
114 int dirbehave = DIR_READ; /* -dRr: handling of directories */
115 int linkbehave = LINK_READ; /* -OpS: handling of symlinks */
117 bool dexclude, dinclude; /* --exclude-dir and --include-dir */
118 bool fexclude, finclude; /* --exclude and --include */
121 BIN_OPT = CHAR_MAX + 1,
134 static inline const char *init_color(const char *);
137 bool file_err; /* file reading error */
140 * Prints usage information and returns 2.
145 fprintf(stderr, errstr[3], getprogname());
146 fprintf(stderr, "%s", errstr[4]);
147 fprintf(stderr, "%s", errstr[5]);
148 fprintf(stderr, "%s", errstr[6]);
152 static const char *optstr = "0123456789A:B:C:D:EFGHILOSRUVabcd:e:f:hilm:nopqrsuvwxyz";
154 static const struct option long_options[] =
156 {"binary-files", required_argument, NULL, BIN_OPT},
157 {"help", no_argument, NULL, HELP_OPT},
158 {"mmap", no_argument, NULL, MMAP_OPT},
159 {"line-buffered", no_argument, NULL, LINEBUF_OPT},
160 {"label", required_argument, NULL, LABEL_OPT},
161 {"null", no_argument, NULL, NULL_OPT},
162 {"color", optional_argument, NULL, COLOR_OPT},
163 {"colour", optional_argument, NULL, COLOR_OPT},
164 {"exclude", required_argument, NULL, R_EXCLUDE_OPT},
165 {"include", required_argument, NULL, R_INCLUDE_OPT},
166 {"exclude-dir", required_argument, NULL, R_DEXCLUDE_OPT},
167 {"include-dir", required_argument, NULL, R_DINCLUDE_OPT},
168 {"after-context", required_argument, NULL, 'A'},
169 {"text", no_argument, NULL, 'a'},
170 {"before-context", required_argument, NULL, 'B'},
171 {"byte-offset", no_argument, NULL, 'b'},
172 {"context", optional_argument, NULL, 'C'},
173 {"count", no_argument, NULL, 'c'},
174 {"devices", required_argument, NULL, 'D'},
175 {"directories", required_argument, NULL, 'd'},
176 {"extended-regexp", no_argument, NULL, 'E'},
177 {"regexp", required_argument, NULL, 'e'},
178 {"fixed-strings", no_argument, NULL, 'F'},
179 {"file", required_argument, NULL, 'f'},
180 {"basic-regexp", no_argument, NULL, 'G'},
181 {"no-filename", no_argument, NULL, 'h'},
182 {"with-filename", no_argument, NULL, 'H'},
183 {"ignore-case", no_argument, NULL, 'i'},
184 {"files-with-matches", no_argument, NULL, 'l'},
185 {"files-without-match", no_argument, NULL, 'L'},
186 {"max-count", required_argument, NULL, 'm'},
187 {"line-number", no_argument, NULL, 'n'},
188 {"only-matching", no_argument, NULL, 'o'},
189 {"quiet", no_argument, NULL, 'q'},
190 {"silent", no_argument, NULL, 'q'},
191 {"recursive", no_argument, NULL, 'r'},
192 {"no-messages", no_argument, NULL, 's'},
193 {"binary", no_argument, NULL, 'U'},
194 {"unix-byte-offsets", no_argument, NULL, 'u'},
195 {"invert-match", no_argument, NULL, 'v'},
196 {"version", no_argument, NULL, 'V'},
197 {"word-regexp", no_argument, NULL, 'w'},
198 {"line-regexp", no_argument, NULL, 'x'},
199 {"null-data", no_argument, NULL, 'z'},
200 {NULL, no_argument, NULL, 0}
204 * Adds a searching pattern to the internal array.
207 add_pattern(char *pat, size_t len)
210 /* Check if we can do a shortcut */
215 /* Increase size if necessary */
216 if (patterns == pattern_sz) {
218 pattern = grep_realloc(pattern, ++pattern_sz *
221 if (len > 0 && pat[len - 1] == '\n')
223 /* pat may not be NUL-terminated */
224 pattern[patterns].pat = grep_malloc(len + 1);
225 memcpy(pattern[patterns].pat, pat, len);
226 pattern[patterns].len = len;
227 pattern[patterns].pat[len] = '\0';
232 * Adds a file include/exclude pattern to the internal array.
235 add_fpattern(const char *pat, int mode)
238 /* Increase size if necessary */
239 if (fpatterns == fpattern_sz) {
241 fpattern = grep_realloc(fpattern, ++fpattern_sz *
242 sizeof(struct epat));
244 fpattern[fpatterns].pat = grep_strdup(pat);
245 fpattern[fpatterns].mode = mode;
250 * Adds a directory include/exclude pattern to the internal array.
253 add_dpattern(const char *pat, int mode)
256 /* Increase size if necessary */
257 if (dpatterns == dpattern_sz) {
259 dpattern = grep_realloc(dpattern, ++dpattern_sz *
260 sizeof(struct epat));
262 dpattern[dpatterns].pat = grep_strdup(pat);
263 dpattern[dpatterns].mode = mode;
268 * Reads searching patterns from a file and adds them with add_pattern().
271 read_patterns(const char *fn)
279 if (strcmp(fn, "-") == 0)
281 else if ((f = fopen(fn, "r")) == NULL)
283 if ((fstat(fileno(f), &st) == -1) || (S_ISDIR(st.st_mode))) {
289 while ((rlen = getline(&line, &len, f)) != -1) {
292 add_pattern(line, line[0] == '\n' ? 0 : (size_t)rlen);
298 if (strcmp(fn, "-") != 0)
302 static inline const char *
303 init_color(const char *d)
307 c = getenv("GREP_COLOR");
308 return (c != NULL && c[0] != '\0' ? c : d);
312 main(int argc, char *argv[])
314 char **aargv, **eargv, *eopts;
318 unsigned int aargc, eargc, i;
319 int c, lastc, needpattern, newarg, prevoptind;
322 setlocale(LC_ALL, "");
325 * Check how we've bene invoked to determine the behavior we should
326 * exhibit. In this way we can have all the functionalities in one
327 * binary without the need of scripting and using ugly hacks.
332 grepbehave = GREP_EXTENDED;
335 grepbehave = GREP_FIXED;
338 dirbehave = DIR_RECURSE;
349 eopts = getenv("GREP_OPTIONS");
351 /* support for extra arguments in GREP_OPTIONS */
353 if (eopts != NULL && eopts[0] != '\0') {
356 /* make an estimation of how many extra arguments we have */
357 for (unsigned int j = 0; j < strlen(eopts); j++)
361 eargv = (char **)grep_malloc(sizeof(char *) * (eargc + 1));
364 /* parse extra arguments */
365 while ((str = strsep(&eopts, " ")) != NULL)
367 eargv[eargc++] = grep_strdup(str);
369 aargv = (char **)grep_calloc(eargc + argc + 1,
373 for (i = 0; i < eargc; i++)
374 aargv[i + 1] = eargv[i];
375 for (int j = 1; j < argc; j++, i++)
376 aargv[i + 1] = argv[j];
378 aargc = eargc + argc;
384 while (((c = getopt_long(aargc, aargv, optstr, long_options, NULL)) !=
387 case '0': case '1': case '2': case '3': case '4':
388 case '5': case '6': case '7': case '8': case '9':
389 if (newarg || !isdigit(lastc))
391 else if (Aflag > LLONG_MAX / 10 - 1) {
396 Aflag = Bflag = (Aflag * 10) + (c - '0');
399 if (optarg == NULL) {
408 l = strtoll(optarg, &ep, 10);
409 if (errno == ERANGE || errno == EINVAL)
411 else if (ep[0] != '\0') {
416 err(2, "context argument must be non-negative");
427 binbehave = BINFILE_TEXT;
436 if (strcasecmp(optarg, "skip") == 0)
437 devbehave = DEV_SKIP;
438 else if (strcasecmp(optarg, "read") == 0)
439 devbehave = DEV_READ;
441 errx(2, errstr[2], "--devices");
444 if (strcasecmp("recurse", optarg) == 0) {
446 dirbehave = DIR_RECURSE;
447 } else if (strcasecmp("skip", optarg) == 0)
448 dirbehave = DIR_SKIP;
449 else if (strcasecmp("read", optarg) == 0)
450 dirbehave = DIR_READ;
452 errx(2, errstr[2], "--directories");
455 grepbehave = GREP_EXTENDED;
460 char *string = optarg;
462 while ((token = strsep(&string, "\n")) != NULL)
463 add_pattern(token, strlen(token));
468 grepbehave = GREP_FIXED;
471 read_patterns(optarg);
475 grepbehave = GREP_BASIC;
485 binbehave = BINFILE_SKIP;
503 mlimit = mcount = strtoll(optarg, &ep, 10);
504 if (((errno == ERANGE) && (mcount == LLONG_MAX)) ||
505 ((errno == EINVAL) && (mcount == 0)))
507 else if (ep[0] != '\0') {
516 linkbehave = LINK_EXPLICIT;
520 cflags &= ~REG_NOSUB;
523 linkbehave = LINK_SKIP;
529 linkbehave = LINK_READ;
533 dirbehave = DIR_RECURSE;
540 binbehave = BINFILE_BIN;
544 filebehave = FILE_MMAP;
547 printf(errstr[8], getprogname(), VERSION);
554 cflags &= ~REG_NOSUB;
558 cflags &= ~REG_NOSUB;
562 cflags &= ~REG_NEWLINE;
565 if (strcasecmp("binary", optarg) == 0)
566 binbehave = BINFILE_BIN;
567 else if (strcasecmp("without-match", optarg) == 0)
568 binbehave = BINFILE_SKIP;
569 else if (strcasecmp("text", optarg) == 0)
570 binbehave = BINFILE_TEXT;
572 errx(2, errstr[2], "--binary-files");
576 if (optarg == NULL || strcasecmp("auto", optarg) == 0 ||
577 strcasecmp("tty", optarg) == 0 ||
578 strcasecmp("if-tty", optarg) == 0) {
581 term = getenv("TERM");
582 if (isatty(STDOUT_FILENO) && term != NULL &&
583 strcasecmp(term, "dumb") != 0)
584 color = init_color("01;31");
585 } else if (strcasecmp("always", optarg) == 0 ||
586 strcasecmp("yes", optarg) == 0 ||
587 strcasecmp("force", optarg) == 0) {
588 color = init_color("01;31");
589 } else if (strcasecmp("never", optarg) != 0 &&
590 strcasecmp("none", optarg) != 0 &&
591 strcasecmp("no", optarg) != 0)
592 errx(2, errstr[2], "--color");
593 cflags &= ~REG_NOSUB;
606 add_fpattern(optarg, INCL_PAT);
610 add_fpattern(optarg, EXCL_PAT);
614 add_dpattern(optarg, INCL_PAT);
618 add_dpattern(optarg, EXCL_PAT);
625 newarg = optind != prevoptind;
631 /* xflag takes precedence, don't confuse the matching bits. */
635 /* Fail if we don't have any pattern */
636 if (aargc == 0 && needpattern)
639 /* Process patterns from command line */
640 if (aargc != 0 && needpattern) {
642 char *string = *aargv;
644 while ((token = strsep(&string, "\n")) != NULL)
645 add_pattern(token, strlen(token));
650 switch (grepbehave) {
655 * regex(3) implementations that support fixed-string searches generally
656 * define either REG_NOSPEC or REG_LITERAL. Set the appropriate flag
657 * here. If neither are defined, GREP_FIXED later implies that the
658 * internal literal matcher should be used. Other cflags that have
659 * the same interpretation as REG_NOSPEC and REG_LITERAL should be
660 * similarly added here, and grep.h should be amended to take this into
661 * consideration when defining WITH_INTERNAL_NOSPEC.
663 #if defined(REG_NOSPEC)
664 cflags |= REG_NOSPEC;
665 #elif defined(REG_LITERAL)
666 cflags |= REG_LITERAL;
670 cflags |= REG_EXTENDED;
677 r_pattern = grep_calloc(patterns, sizeof(*r_pattern));
679 #ifdef WITH_INTERNAL_NOSPEC
680 if (grepbehave != GREP_FIXED) {
684 /* Check if cheating is allowed (always is for fgrep). */
685 for (i = 0; i < patterns; ++i) {
686 c = regcomp(&r_pattern[i], pattern[i].pat, cflags);
688 regerror(c, &r_pattern[i], re_error,
690 errx(2, "%s", re_error);
698 if ((aargc == 0 || aargc == 1) && !Hflag)
703 if (aargc == 0 && dirbehave != DIR_RECURSE)
704 exit(!procfile("-"));
706 if (dirbehave == DIR_RECURSE)
707 matched = grep_tree(aargv);
709 for (matched = false; aargc--; ++aargv) {
710 if ((finclude || fexclude) && !file_matching(*aargv))
712 if (procfile(*aargv))
720 * Calculate the correct return value according to the
721 * results and the command line option.
723 exit(matched ? (file_err ? (qflag ? 0 : 2) : 0) : (file_err ? 2 : 1));