xref: /freebsd/usr.bin/grep/grep.h (revision 499fe48de8938d4c7b0a91e20eb6c16db9d55633)
1 /*	$NetBSD: grep.h,v 1.5 2011/02/27 17:33:37 joerg Exp $	*/
2 /*	$OpenBSD: grep.h,v 1.15 2010/04/05 03:03:55 tedu Exp $	*/
3 /*	$FreeBSD$	*/
4 
5 /*-
6  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
7  *
8  * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
9  * Copyright (c) 2008-2009 Gabor Kovesdan <gabor@FreeBSD.org>
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <bzlib.h>
35 #include <limits.h>
36 #include <regex.h>
37 #include <stdbool.h>
38 #include <stdio.h>
39 #include <zlib.h>
40 
41 extern const char		*errstr[];
42 
43 #define	VERSION		"2.6.0-FreeBSD"
44 
45 #define	GREP_FIXED	0
46 #define	GREP_BASIC	1
47 #define	GREP_EXTENDED	2
48 
49 #if !defined(REG_NOSPEC) && !defined(REG_LITERAL)
50 #define WITH_INTERNAL_NOSPEC
51 #endif
52 
53 #define	BINFILE_BIN	0
54 #define	BINFILE_SKIP	1
55 #define	BINFILE_TEXT	2
56 
57 #define	FILE_STDIO	0
58 #define	FILE_MMAP	1
59 
60 #define	DIR_READ	0
61 #define	DIR_SKIP	1
62 #define	DIR_RECURSE	2
63 
64 #define	DEV_READ	0
65 #define	DEV_SKIP	1
66 
67 #define	LINK_READ	0
68 #define	LINK_EXPLICIT	1
69 #define	LINK_SKIP	2
70 
71 #define	EXCL_PAT	0
72 #define	INCL_PAT	1
73 
74 #define	MAX_MATCHES	32
75 
76 struct file {
77 	int		 fd;
78 	bool		 binary;
79 };
80 
81 struct str {
82 	off_t		 boff;
83 	off_t		 off;
84 	size_t		 len;
85 	char		*dat;
86 	char		*file;
87 	int		 line_no;
88 };
89 
90 struct pat {
91 	char		*pat;
92 	int		 len;
93 };
94 
95 struct epat {
96 	char		*pat;
97 	int		 mode;
98 };
99 
100 /*
101  * Parsing context; used to hold things like matches made and
102  * other useful bits
103  */
104 struct parsec {
105 	regmatch_t	matches[MAX_MATCHES];		/* Matches made */
106 	/* XXX TODO: This should be a chunk, not a line */
107 	struct str	ln;				/* Current line */
108 	size_t		lnstart;			/* Position in line */
109 	size_t		matchidx;			/* Latest match index */
110 	int		printed;			/* Metadata printed? */
111 	bool		binary;				/* Binary file? */
112 	bool		cntlines;			/* Count lines? */
113 };
114 
115 /* Flags passed to regcomp() and regexec() */
116 extern int	 cflags, eflags;
117 
118 /* Command line flags */
119 extern bool	 Eflag, Fflag, Gflag, Hflag, Lflag,
120 		 bflag, cflag, hflag, iflag, lflag, mflag, nflag, oflag,
121 		 qflag, sflag, vflag, wflag, xflag;
122 extern bool	 dexclude, dinclude, fexclude, finclude, lbflag, nullflag;
123 extern long long Aflag, Bflag;
124 extern long long mcount;
125 extern long long mlimit;
126 extern char	 fileeol;
127 extern char	*label;
128 extern const char *color;
129 extern int	 binbehave, devbehave, dirbehave, filebehave, grepbehave, linkbehave;
130 
131 extern bool	 file_err, matchall;
132 extern unsigned int dpatterns, fpatterns, patterns;
133 extern struct pat *pattern;
134 extern struct epat *dpattern, *fpattern;
135 extern regex_t	*er_pattern, *r_pattern;
136 
137 /* For regex errors  */
138 #define	RE_ERROR_BUF	512
139 extern char	 re_error[RE_ERROR_BUF + 1];	/* Seems big enough */
140 
141 /* util.c */
142 bool	 file_matching(const char *fname);
143 bool	 procfile(const char *fn);
144 bool	 grep_tree(char **argv);
145 void	*grep_malloc(size_t size);
146 void	*grep_calloc(size_t nmemb, size_t size);
147 void	*grep_realloc(void *ptr, size_t size);
148 char	*grep_strdup(const char *str);
149 void	 grep_printline(struct str *line, int sep);
150 
151 /* queue.c */
152 bool	 enqueue(struct str *x);
153 void	 printqueue(void);
154 void	 clearqueue(void);
155 
156 /* file.c */
157 void		 grep_close(struct file *f);
158 struct file	*grep_open(const char *path);
159 char		*grep_fgetln(struct file *f, struct parsec *pc);
160