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