grep.h revision 211364
1210389Sgabor/*	$OpenBSD: grep.h,v 1.15 2010/04/05 03:03:55 tedu Exp $	*/
2210389Sgabor/*	$FreeBSD: head/usr.bin/grep/grep.h 211364 2010-08-15 22:15:04Z gabor $	*/
3210389Sgabor
4210389Sgabor/*-
5210389Sgabor * Copyright (c) 1999 James Howard and Dag-Erling Co�dan Sm�rgrav
6210389Sgabor * Copyright (c) 2008-2009 Gabor Kovesdan <gabor@FreeBSD.org>
7210389Sgabor * All rights reserved.
8210389Sgabor *
9210389Sgabor * Redistribution and use in source and binary forms, with or without
10210389Sgabor * modification, are permitted provided that the following conditions
11210389Sgabor * are met:
12210389Sgabor * 1. Redistributions of source code must retain the above copyright
13210389Sgabor *    notice, this list of conditions and the following disclaimer.
14210389Sgabor * 2. Redistributions in binary form must reproduce the above copyright
15210389Sgabor *    notice, this list of conditions and the following disclaimer in the
16210389Sgabor *    documentation and/or other materials provided with the distribution.
17210389Sgabor *
18210389Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19210389Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20210389Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21210389Sgabor * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22210389Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23210389Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24210389Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25210389Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26210389Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27210389Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28210389Sgabor * SUCH DAMAGE.
29210389Sgabor */
30210389Sgabor
31210389Sgabor#include <bzlib.h>
32210389Sgabor#include <limits.h>
33210389Sgabor#include <regex.h>
34210389Sgabor#include <stdbool.h>
35210389Sgabor#include <stdio.h>
36210389Sgabor#include <zlib.h>
37210389Sgabor
38210389Sgabor#ifdef WITHOUT_NLS
39210389Sgabor#define getstr(n)	 errstr[n]
40210389Sgabor#else
41210389Sgabor#include <nl_types.h>
42210389Sgabor
43210389Sgaborextern nl_catd		 catalog;
44210389Sgabor#define getstr(n)	 catgets(catalog, 1, n, errstr[n])
45210389Sgabor#endif
46210389Sgabor
47210389Sgaborextern const char		*errstr[];
48210389Sgabor
49210389Sgabor#define VERSION		"2.5.1-FreeBSD"
50210389Sgabor
51210389Sgabor#define GREP_FIXED	0
52210389Sgabor#define GREP_BASIC	1
53210389Sgabor#define GREP_EXTENDED	2
54210389Sgabor
55210389Sgabor#define BINFILE_BIN	0
56210389Sgabor#define BINFILE_SKIP	1
57210389Sgabor#define BINFILE_TEXT	2
58210389Sgabor
59210389Sgabor#define FILE_STDIO	0
60210389Sgabor#define FILE_GZIP	1
61210389Sgabor#define FILE_BZIP	2
62210389Sgabor
63210461Sgabor#define DIR_READ	0
64210389Sgabor#define DIR_SKIP	1
65210389Sgabor#define DIR_RECURSE	2
66210389Sgabor
67210461Sgabor#define DEV_READ	0
68210389Sgabor#define DEV_SKIP	1
69210389Sgabor
70210461Sgabor#define LINK_READ	0
71210389Sgabor#define LINK_EXPLICIT	1
72210389Sgabor#define LINK_SKIP	2
73210389Sgabor
74210389Sgabor#define EXCL_PAT	0
75210389Sgabor#define INCL_PAT	1
76210389Sgabor
77210389Sgabor#define MAX_LINE_MATCHES	32
78210389Sgabor
79210389Sgaborstruct file {
80210389Sgabor	struct mmfile	*mmf;
81210389Sgabor	BZFILE		*bzf;
82210389Sgabor	FILE		*f;
83210389Sgabor	gzFile		*gzf;
84210389Sgabor	bool		 binary;
85210389Sgabor	bool		 stdin;
86210389Sgabor};
87210389Sgabor
88210389Sgaborstruct str {
89210389Sgabor	off_t		 off;
90210389Sgabor	size_t		 len;
91210389Sgabor	char		*dat;
92210389Sgabor	char		*file;
93210389Sgabor	int		 line_no;
94210389Sgabor};
95210389Sgabor
96210389Sgaborstruct epat {
97210389Sgabor	char		*pat;
98210389Sgabor	int		 mode;
99210389Sgabor};
100210389Sgabor
101210389Sgabortypedef struct {
102210389Sgabor	size_t		 len;
103210389Sgabor	unsigned char	*pattern;
104210389Sgabor	int		 qsBc[UCHAR_MAX + 1];
105210389Sgabor	/* flags */
106210389Sgabor	bool		 bol;
107210389Sgabor	bool		 eol;
108210389Sgabor	bool		 reversed;
109210389Sgabor} fastgrep_t;
110210389Sgabor
111210389Sgabor/* Flags passed to regcomp() and regexec() */
112210389Sgaborextern int	 cflags, eflags;
113210389Sgabor
114210389Sgabor/* Command line flags */
115210389Sgaborextern bool	 Eflag, Fflag, Gflag, Hflag, Lflag,
116210389Sgabor		 bflag, cflag, hflag, iflag, lflag, mflag, nflag, oflag,
117210389Sgabor		 qflag, sflag, vflag, wflag, xflag;
118211364Sgaborextern bool	 dexclude, dinclude, fexclude, finclude, lbflag, nullflag;
119210389Sgaborextern unsigned long long Aflag, Bflag, mcount;
120210461Sgaborextern char	*label;
121210461Sgaborextern const char *color;
122210389Sgaborextern int	 binbehave, devbehave, dirbehave, filebehave, grepbehave, linkbehave;
123210389Sgabor
124210389Sgaborextern bool	 first, matchall, notfound, prev;
125210389Sgaborextern int	 tail;
126210578Sgaborextern unsigned int dpatterns, fpatterns, patterns;
127210389Sgaborextern char    **pattern;
128210578Sgaborextern struct epat *dpattern, *fpattern;
129210389Sgaborextern regex_t	*er_pattern, *r_pattern;
130210389Sgaborextern fastgrep_t *fg_pattern;
131210389Sgabor
132210389Sgabor/* For regex errors  */
133210389Sgabor#define RE_ERROR_BUF	512
134210389Sgaborextern char	 re_error[RE_ERROR_BUF + 1];	/* Seems big enough */
135210389Sgabor
136210389Sgabor/* util.c */
137210578Sgaborbool	 file_matching(const char *fname);
138210389Sgaborint	 procfile(const char *fn);
139210389Sgaborint	 grep_tree(char **argv);
140210389Sgaborvoid	*grep_malloc(size_t size);
141210389Sgaborvoid	*grep_calloc(size_t nmemb, size_t size);
142210389Sgaborvoid	*grep_realloc(void *ptr, size_t size);
143210578Sgaborchar	*grep_strdup(const char *str);
144210389Sgaborvoid	 printline(struct str *line, int sep, regmatch_t *matches, int m);
145210389Sgabor
146210389Sgabor/* queue.c */
147210389Sgaborvoid	 enqueue(struct str *x);
148210389Sgaborvoid	 printqueue(void);
149210389Sgaborvoid	 clearqueue(void);
150210389Sgabor
151210389Sgabor/* file.c */
152210389Sgaborvoid		 grep_close(struct file *f);
153210389Sgaborstruct file	*grep_stdin_open(void);
154210389Sgaborstruct file	*grep_open(const char *path);
155210389Sgaborchar		*grep_fgetln(struct file *f, size_t *len);
156210389Sgabor
157210389Sgabor/* fastgrep.c */
158210389Sgaborint		 fastcomp(fastgrep_t *, const char *);
159210389Sgaborvoid		 fgrepcomp(fastgrep_t *, const char *);
160210389Sgaborint		 grep_search(fastgrep_t *, unsigned char *, size_t, regmatch_t *);
161