1/* $OpenBSD: grep.c,v 1.42 2010/07/02 22:18:03 tedu Exp $ */
2
3/*-
4 * Copyright (c) 1999 James Howard and Dag-Erling Co�dan Sm�rgrav
5 * Copyright (C) 2008-2009 Gabor Kovesdan <gabor@FreeBSD.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
--- 14 unchanged lines hidden (view full) ---
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/usr.bin/grep/grep.c 210461 2010-07-25 08:42:18Z gabor $");
32
33#include <sys/stat.h>
34#include <sys/types.h>
35
36#include <ctype.h>
37#include <err.h>
38#include <errno.h>
39#include <getopt.h>
--- 40 unchanged lines hidden (view full) ---
80
81/* Searching patterns */
82unsigned int patterns, pattern_sz;
83char **pattern;
84regex_t *r_pattern;
85fastgrep_t *fg_pattern;
86
87/* Filename exclusion/inclusion patterns */
88unsigned int epatterns, epattern_sz;
89struct epat *epattern;
90
91/* For regex errors */
92char re_error[RE_ERROR_BUF + 1];
93
94/* Command-line flags */
95unsigned long long Aflag; /* -A x: print x lines trailing each match */
96unsigned long long Bflag; /* -B x: print x lines leading each match */
97bool Hflag; /* -H: always print file name */
--- 9 unchanged lines hidden (view full) ---
107bool oflag; /* -o: print only matching part */
108bool qflag; /* -q: quiet mode (don't output anything) */
109bool sflag; /* -s: silent mode (ignore errors) */
110bool vflag; /* -v: only show non-matching lines */
111bool wflag; /* -w: pattern must start and end on word boundaries */
112bool xflag; /* -x: pattern must match entire line */
113bool lbflag; /* --line-buffered */
114bool nullflag; /* --null */
115bool exclflag; /* --exclude */
116char *label; /* --label */
117const char *color; /* --color */
118int grepbehave = GREP_BASIC; /* -EFGP: type of the regex */
119int binbehave = BINFILE_BIN; /* -aIU: handling of binary files */
120int filebehave = FILE_STDIO; /* -JZ: normal, gzip or bzip2 file */
121int devbehave = DEV_READ; /* -D: handling of devices */
122int dirbehave = DIR_READ; /* -dRr: handling of directories */
123int linkbehave = LINK_READ; /* -OpS: handling of symlinks */
124
125enum {
126 BIN_OPT = CHAR_MAX + 1,
127 COLOR_OPT,
128 HELP_OPT,
129 MMAP_OPT,
130 LINEBUF_OPT,
131 LABEL_OPT,
132 NULL_OPT,
--- 96 unchanged lines hidden (view full) ---
229 pattern_sz *= 2;
230 pattern = grep_realloc(pattern, ++pattern_sz *
231 sizeof(*pattern));
232 }
233 if (len > 0 && pat[len - 1] == '\n')
234 --len;
235 /* pat may not be NUL-terminated */
236 pattern[patterns] = grep_malloc(len + 1);
237 memcpy(pattern[patterns], pat, len);
238 pattern[patterns][len] = '\0';
239 ++patterns;
240}
241
242/*
243 * Adds an include/exclude pattern to the internal array.
244 */
245static void
246add_epattern(char *pat, size_t len, int type, int mode)
247{
248
249 /* Increase size if necessary */
250 if (epatterns == epattern_sz) {
251 epattern_sz *= 2;
252 epattern = grep_realloc(epattern, ++epattern_sz *
253 sizeof(struct epat));
254 }
255 if (len > 0 && pat[len - 1] == '\n')
256 --len;
257 epattern[epatterns].pat = grep_malloc(len + 1);
258 memcpy(epattern[epatterns].pat, pat, len);
259 epattern[epatterns].pat[len] = '\0';
260 epattern[epatterns].type = type;
261 epattern[epatterns].mode = mode;
262 ++epatterns;
263}
264
265/*
266 * Reads searching patterns from a file and adds them with add_pattern().
267 */
268static void
269read_patterns(const char *fn)
270{
271 FILE *f;
272 char *line;
273 size_t len;
--- 312 unchanged lines hidden (view full) ---
586 break;
587 case LINEBUF_OPT:
588 lbflag = true;
589 break;
590 case NULL_OPT:
591 nullflag = true;
592 break;
593 case R_INCLUDE_OPT:
594 exclflag = true;
595 add_epattern(basename(optarg), strlen(basename(optarg)),
596 FILE_PAT, INCL_PAT);
597 break;
598 case R_EXCLUDE_OPT:
599 exclflag = true;
600 add_epattern(basename(optarg), strlen(basename(optarg)),
601 FILE_PAT, EXCL_PAT);
602 break;
603 case R_DINCLUDE_OPT:
604 exclflag = true;
605 add_epattern(basename(optarg), strlen(basename(optarg)),
606 DIR_PAT, INCL_PAT);
607 break;
608 case R_DEXCLUDE_OPT:
609 exclflag = true;
610 add_epattern(basename(optarg), strlen(basename(optarg)),
611 DIR_PAT, EXCL_PAT);
612 break;
613 case HELP_OPT:
614 default:
615 usage();
616 }
617 lastc = c;
618 newarg = optind != prevoptind;
619 prevoptind = optind;
--- 55 unchanged lines hidden (view full) ---
675 hflag = true;
676
677 if (aargc == 0)
678 exit(!procfile("-"));
679
680 if (dirbehave == DIR_RECURSE)
681 c = grep_tree(aargv);
682 else
683 for (c = 0; aargc--; ++aargv)
684 c+= procfile(*aargv);
685
686#ifndef WITHOUT_NLS
687 catclose(catalog);
688#endif
689
690 /* Find out the correct return value according to the
691 results and the command line option. */
692 exit(c ? (notfound ? (qflag ? 0 : 2) : 0) : (notfound ? 2 : 1));
693}
2
3/*-
4 * Copyright (c) 1999 James Howard and Dag-Erling Co�dan Sm�rgrav
5 * Copyright (C) 2008-2009 Gabor Kovesdan <gabor@FreeBSD.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
--- 14 unchanged lines hidden (view full) ---
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/usr.bin/grep/grep.c 210461 2010-07-25 08:42:18Z gabor $");
32
33#include <sys/stat.h>
34#include <sys/types.h>
35
36#include <ctype.h>
37#include <err.h>
38#include <errno.h>
39#include <getopt.h>
--- 40 unchanged lines hidden (view full) ---
80
81/* Searching patterns */
82unsigned int patterns, pattern_sz;
83char **pattern;
84regex_t *r_pattern;
85fastgrep_t *fg_pattern;
86
87/* Filename exclusion/inclusion patterns */
88unsigned int epatterns, epattern_sz;
89struct epat *epattern;
90
91/* For regex errors */
92char re_error[RE_ERROR_BUF + 1];
93
94/* Command-line flags */
95unsigned long long Aflag; /* -A x: print x lines trailing each match */
96unsigned long long Bflag; /* -B x: print x lines leading each match */
97bool Hflag; /* -H: always print file name */
--- 9 unchanged lines hidden (view full) ---
107bool oflag; /* -o: print only matching part */
108bool qflag; /* -q: quiet mode (don't output anything) */
109bool sflag; /* -s: silent mode (ignore errors) */
110bool vflag; /* -v: only show non-matching lines */
111bool wflag; /* -w: pattern must start and end on word boundaries */
112bool xflag; /* -x: pattern must match entire line */
113bool lbflag; /* --line-buffered */
114bool nullflag; /* --null */
115bool exclflag; /* --exclude */
116char *label; /* --label */
117const char *color; /* --color */
118int grepbehave = GREP_BASIC; /* -EFGP: type of the regex */
119int binbehave = BINFILE_BIN; /* -aIU: handling of binary files */
120int filebehave = FILE_STDIO; /* -JZ: normal, gzip or bzip2 file */
121int devbehave = DEV_READ; /* -D: handling of devices */
122int dirbehave = DIR_READ; /* -dRr: handling of directories */
123int linkbehave = LINK_READ; /* -OpS: handling of symlinks */
124
125enum {
126 BIN_OPT = CHAR_MAX + 1,
127 COLOR_OPT,
128 HELP_OPT,
129 MMAP_OPT,
130 LINEBUF_OPT,
131 LABEL_OPT,
132 NULL_OPT,
--- 96 unchanged lines hidden (view full) ---
229 pattern_sz *= 2;
230 pattern = grep_realloc(pattern, ++pattern_sz *
231 sizeof(*pattern));
232 }
233 if (len > 0 && pat[len - 1] == '\n')
234 --len;
235 /* pat may not be NUL-terminated */
236 pattern[patterns] = grep_malloc(len + 1);
237 memcpy(pattern[patterns], pat, len);
238 pattern[patterns][len] = '\0';
239 ++patterns;
240}
241
242/*
243 * Adds an include/exclude pattern to the internal array.
244 */
245static void
246add_epattern(char *pat, size_t len, int type, int mode)
247{
248
249 /* Increase size if necessary */
250 if (epatterns == epattern_sz) {
251 epattern_sz *= 2;
252 epattern = grep_realloc(epattern, ++epattern_sz *
253 sizeof(struct epat));
254 }
255 if (len > 0 && pat[len - 1] == '\n')
256 --len;
257 epattern[epatterns].pat = grep_malloc(len + 1);
258 memcpy(epattern[epatterns].pat, pat, len);
259 epattern[epatterns].pat[len] = '\0';
260 epattern[epatterns].type = type;
261 epattern[epatterns].mode = mode;
262 ++epatterns;
263}
264
265/*
266 * Reads searching patterns from a file and adds them with add_pattern().
267 */
268static void
269read_patterns(const char *fn)
270{
271 FILE *f;
272 char *line;
273 size_t len;
--- 312 unchanged lines hidden (view full) ---
586 break;
587 case LINEBUF_OPT:
588 lbflag = true;
589 break;
590 case NULL_OPT:
591 nullflag = true;
592 break;
593 case R_INCLUDE_OPT:
594 exclflag = true;
595 add_epattern(basename(optarg), strlen(basename(optarg)),
596 FILE_PAT, INCL_PAT);
597 break;
598 case R_EXCLUDE_OPT:
599 exclflag = true;
600 add_epattern(basename(optarg), strlen(basename(optarg)),
601 FILE_PAT, EXCL_PAT);
602 break;
603 case R_DINCLUDE_OPT:
604 exclflag = true;
605 add_epattern(basename(optarg), strlen(basename(optarg)),
606 DIR_PAT, INCL_PAT);
607 break;
608 case R_DEXCLUDE_OPT:
609 exclflag = true;
610 add_epattern(basename(optarg), strlen(basename(optarg)),
611 DIR_PAT, EXCL_PAT);
612 break;
613 case HELP_OPT:
614 default:
615 usage();
616 }
617 lastc = c;
618 newarg = optind != prevoptind;
619 prevoptind = optind;
--- 55 unchanged lines hidden (view full) ---
675 hflag = true;
676
677 if (aargc == 0)
678 exit(!procfile("-"));
679
680 if (dirbehave == DIR_RECURSE)
681 c = grep_tree(aargv);
682 else
683 for (c = 0; aargc--; ++aargv)
684 c+= procfile(*aargv);
685
686#ifndef WITHOUT_NLS
687 catclose(catalog);
688#endif
689
690 /* Find out the correct return value according to the
691 results and the command line option. */
692 exit(c ? (notfound ? (qflag ? 0 : 2) : 0) : (notfound ? 2 : 1));
693}