grep.h revision 210461
1210389Sgabor/*	$OpenBSD: grep.h,v 1.15 2010/04/05 03:03:55 tedu Exp $	*/
2210389Sgabor/*	$FreeBSD: head/usr.bin/grep/grep.h 210461 2010-07-25 08:42:18Z 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 FILE_PAT	0
75210389Sgabor#define DIR_PAT		1
76210389Sgabor#define EXCL_PAT	0
77210389Sgabor#define INCL_PAT	1
78210389Sgabor
79210389Sgabor#define MAX_LINE_MATCHES	32
80210389Sgabor
81210389Sgaborstruct file {
82210389Sgabor	struct mmfile	*mmf;
83210389Sgabor	BZFILE		*bzf;
84210389Sgabor	FILE		*f;
85210389Sgabor	gzFile		*gzf;
86210389Sgabor	bool		 binary;
87210389Sgabor	bool		 stdin;
88210389Sgabor};
89210389Sgabor
90210389Sgaborstruct str {
91210389Sgabor	off_t		 off;
92210389Sgabor	size_t		 len;
93210389Sgabor	char		*dat;
94210389Sgabor	char		*file;
95210389Sgabor	int		 line_no;
96210389Sgabor};
97210389Sgabor
98210389Sgaborstruct epat {
99210389Sgabor	char		*pat;
100210389Sgabor	int		 mode;
101210389Sgabor	int		 type;
102210389Sgabor};
103210389Sgabor
104210389Sgabortypedef struct {
105210389Sgabor	size_t		 len;
106210389Sgabor	unsigned char	*pattern;
107210389Sgabor	int		 qsBc[UCHAR_MAX + 1];
108210389Sgabor	/* flags */
109210389Sgabor	bool		 bol;
110210389Sgabor	bool		 eol;
111210389Sgabor	bool		 reversed;
112210389Sgabor} fastgrep_t;
113210389Sgabor
114210389Sgabor/* Flags passed to regcomp() and regexec() */
115210389Sgaborextern int	 cflags, eflags;
116210389Sgabor
117210389Sgabor/* Command line flags */
118210389Sgaborextern bool	 Eflag, Fflag, Gflag, Hflag, Lflag,
119210389Sgabor		 bflag, cflag, hflag, iflag, lflag, mflag, nflag, oflag,
120210389Sgabor		 qflag, sflag, vflag, wflag, xflag;
121210389Sgaborextern bool	 exclflag, nullflag;
122210389Sgaborextern unsigned long long Aflag, Bflag, mcount;
123210461Sgaborextern char	*label;
124210461Sgaborextern const char *color;
125210389Sgaborextern int	 binbehave, devbehave, dirbehave, filebehave, grepbehave, linkbehave;
126210389Sgabor
127210389Sgaborextern bool	 first, matchall, notfound, prev;
128210389Sgaborextern int	 tail;
129210389Sgaborextern unsigned int epatterns, patterns;
130210389Sgaborextern char    **pattern;
131210389Sgaborextern struct epat *epattern;
132210389Sgaborextern regex_t	*er_pattern, *r_pattern;
133210389Sgaborextern fastgrep_t *fg_pattern;
134210389Sgabor
135210389Sgabor/* For regex errors  */
136210389Sgabor#define RE_ERROR_BUF	512
137210389Sgaborextern char	 re_error[RE_ERROR_BUF + 1];	/* Seems big enough */
138210389Sgabor
139210389Sgabor/* util.c */
140210389Sgaborint	 procfile(const char *fn);
141210389Sgaborint	 grep_tree(char **argv);
142210389Sgaborvoid	*grep_malloc(size_t size);
143210389Sgaborvoid	*grep_calloc(size_t nmemb, size_t size);
144210389Sgaborvoid	*grep_realloc(void *ptr, size_t size);
145210389Sgaborvoid	 printline(struct str *line, int sep, regmatch_t *matches, int m);
146210389Sgabor
147210389Sgabor/* queue.c */
148210389Sgaborvoid	 enqueue(struct str *x);
149210389Sgaborvoid	 printqueue(void);
150210389Sgaborvoid	 clearqueue(void);
151210389Sgabor
152210389Sgabor/* file.c */
153210389Sgaborvoid		 grep_close(struct file *f);
154210389Sgaborstruct file	*grep_stdin_open(void);
155210389Sgaborstruct file	*grep_open(const char *path);
156210389Sgaborint		 grep_feof(struct file *f);
157210389Sgaborint		 grep_fgetc(struct file *f);
158210389Sgaborchar		*grep_fgetln(struct file *f, size_t *len);
159210389Sgabor
160210389Sgabor/* fastgrep.c */
161210389Sgaborint		 fastcomp(fastgrep_t *, const char *);
162210389Sgaborvoid		 fgrepcomp(fastgrep_t *, const char *);
163210389Sgaborint		 grep_search(fastgrep_t *, unsigned char *, size_t, regmatch_t *);
164