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: stable/11/usr.bin/grep/grep.h 354628 2019-11-11 19:54:08Z kevans $	*/
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#ifndef WITHOUT_FASTMATCH
42#include "fastmatch.h"
43#endif
44
45#ifdef WITHOUT_NLS
46#define	getstr(n)	 errstr[n]
47#else
48#include <nl_types.h>
49
50extern nl_catd		 catalog;
51#define	getstr(n)	 catgets(catalog, 1, n, errstr[n])
52#endif
53
54extern const char		*errstr[];
55
56#define	VERSION		"2.6.0-FreeBSD"
57
58#define	GREP_FIXED	0
59#define	GREP_BASIC	1
60#define	GREP_EXTENDED	2
61
62#if !defined(REG_NOSPEC) && !defined(REG_LITERAL)
63#define WITH_INTERNAL_NOSPEC
64#endif
65
66#define	BINFILE_BIN	0
67#define	BINFILE_SKIP	1
68#define	BINFILE_TEXT	2
69
70#define	FILE_STDIO	0
71#define	FILE_MMAP	1
72#define	FILE_GZIP	2
73#define	FILE_BZIP	3
74#define	FILE_XZ		4
75#define	FILE_LZMA	5
76
77#define	DIR_READ	0
78#define	DIR_SKIP	1
79#define	DIR_RECURSE	2
80
81#define	DEV_READ	0
82#define	DEV_SKIP	1
83
84#define	LINK_READ	0
85#define	LINK_EXPLICIT	1
86#define	LINK_SKIP	2
87
88#define	EXCL_PAT	0
89#define	INCL_PAT	1
90
91#define	MAX_MATCHES	32
92
93struct file {
94	int		 fd;
95	bool		 binary;
96};
97
98struct str {
99	off_t		 boff;
100	off_t		 off;
101	size_t		 len;
102	char		*dat;
103	char		*file;
104	int		 line_no;
105};
106
107struct pat {
108	char		*pat;
109	int		 len;
110};
111
112struct epat {
113	char		*pat;
114	int		 mode;
115};
116
117/*
118 * Parsing context; used to hold things like matches made and
119 * other useful bits
120 */
121struct parsec {
122	regmatch_t	matches[MAX_MATCHES];		/* Matches made */
123	/* XXX TODO: This should be a chunk, not a line */
124	struct str	ln;				/* Current line */
125	size_t		lnstart;			/* Position in line */
126	size_t		matchidx;			/* Latest match index */
127	int		printed;			/* Metadata printed? */
128	bool		binary;				/* Binary file? */
129	bool		cntlines;			/* Count lines? */
130};
131
132/* Flags passed to regcomp() and regexec() */
133extern int	 cflags, eflags;
134
135/* Command line flags */
136extern bool	 Eflag, Fflag, Gflag, Hflag, Lflag,
137		 bflag, cflag, hflag, iflag, lflag, mflag, nflag, oflag,
138		 qflag, sflag, vflag, wflag, xflag;
139extern bool	 dexclude, dinclude, fexclude, finclude, lbflag, nullflag;
140extern long long Aflag, Bflag;
141extern long long mcount;
142extern long long mlimit;
143extern char	 fileeol;
144extern char	*label;
145extern const char *color;
146extern int	 binbehave, devbehave, dirbehave, filebehave, grepbehave, linkbehave;
147
148extern bool	 file_err, matchall;
149extern unsigned int dpatterns, fpatterns, patterns;
150extern struct pat *pattern;
151extern struct epat *dpattern, *fpattern;
152extern regex_t	*er_pattern, *r_pattern;
153#ifndef WITHOUT_FASTMATCH
154extern fastmatch_t *fg_pattern;
155#endif
156
157/* For regex errors  */
158#define	RE_ERROR_BUF	512
159extern char	 re_error[RE_ERROR_BUF + 1];	/* Seems big enough */
160
161/* util.c */
162bool	 file_matching(const char *fname);
163bool	 procfile(const char *fn);
164bool	 grep_tree(char **argv);
165void	*grep_malloc(size_t size);
166void	*grep_calloc(size_t nmemb, size_t size);
167void	*grep_realloc(void *ptr, size_t size);
168char	*grep_strdup(const char *str);
169void	 grep_printline(struct str *line, int sep);
170
171/* queue.c */
172bool	 enqueue(struct str *x);
173void	 printqueue(void);
174void	 clearqueue(void);
175
176/* file.c */
177void		 grep_close(struct file *f);
178struct file	*grep_open(const char *path);
179char		*grep_fgetln(struct file *f, struct parsec *pc);
180