xref: /freebsd/usr.bin/grep/grep.h (revision 8d20be1e22095c27faf8fe8b2f0d089739cc742e)
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  * 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 #include <bzlib.h>
33 #include <limits.h>
34 #include <regex.h>
35 #include <stdbool.h>
36 #include <stdio.h>
37 #include <zlib.h>
38 
39 #include "fastmatch.h"
40 
41 #ifdef WITHOUT_NLS
42 #define getstr(n)	 errstr[n]
43 #else
44 #include <nl_types.h>
45 
46 extern nl_catd		 catalog;
47 #define getstr(n)	 catgets(catalog, 1, n, errstr[n])
48 #endif
49 
50 extern const char		*errstr[];
51 
52 #define VERSION		"2.5.1-FreeBSD"
53 
54 #define GREP_FIXED	0
55 #define GREP_BASIC	1
56 #define GREP_EXTENDED	2
57 
58 #define BINFILE_BIN	0
59 #define BINFILE_SKIP	1
60 #define BINFILE_TEXT	2
61 
62 #define FILE_STDIO	0
63 #define FILE_MMAP	1
64 #define FILE_GZIP	2
65 #define FILE_BZIP	3
66 #define FILE_XZ		4
67 #define FILE_LZMA	5
68 
69 #define DIR_READ	0
70 #define DIR_SKIP	1
71 #define DIR_RECURSE	2
72 
73 #define DEV_READ	0
74 #define DEV_SKIP	1
75 
76 #define LINK_READ	0
77 #define LINK_EXPLICIT	1
78 #define LINK_SKIP	2
79 
80 #define EXCL_PAT	0
81 #define INCL_PAT	1
82 
83 #define MAX_LINE_MATCHES	32
84 
85 struct file {
86 	int		 fd;
87 	bool		 binary;
88 };
89 
90 struct str {
91 	off_t		 off;
92 	size_t		 len;
93 	char		*dat;
94 	char		*file;
95 	int		 line_no;
96 };
97 
98 struct pat {
99 	char		*pat;
100 	int		 len;
101 };
102 
103 struct epat {
104 	char		*pat;
105 	int		 mode;
106 };
107 
108 /* Flags passed to regcomp() and regexec() */
109 extern int	 cflags, eflags;
110 
111 /* Command line flags */
112 extern bool	 Eflag, Fflag, Gflag, Hflag, Lflag,
113 		 bflag, cflag, hflag, iflag, lflag, mflag, nflag, oflag,
114 		 qflag, sflag, vflag, wflag, xflag;
115 extern bool	 dexclude, dinclude, fexclude, finclude, lbflag, nullflag;
116 extern unsigned long long Aflag, Bflag;
117 extern long long mcount;
118 extern long long mlimit;
119 extern char	*label;
120 extern const char *color;
121 extern int	 binbehave, devbehave, dirbehave, filebehave, grepbehave, linkbehave;
122 
123 extern bool	 file_err, first, matchall, prev;
124 extern int	 tail;
125 extern unsigned int dpatterns, fpatterns, patterns;
126 extern struct pat *pattern;
127 extern struct epat *dpattern, *fpattern;
128 extern regex_t	*er_pattern, *r_pattern;
129 extern fastmatch_t *fg_pattern;
130 
131 /* For regex errors  */
132 #define RE_ERROR_BUF	512
133 extern char	 re_error[RE_ERROR_BUF + 1];	/* Seems big enough */
134 
135 /* util.c */
136 bool	 file_matching(const char *fname);
137 int	 procfile(const char *fn);
138 int	 grep_tree(char **argv);
139 void	*grep_malloc(size_t size);
140 void	*grep_calloc(size_t nmemb, size_t size);
141 void	*grep_realloc(void *ptr, size_t size);
142 char	*grep_strdup(const char *str);
143 void	 printline(struct str *line, int sep, regmatch_t *matches, int m);
144 
145 /* queue.c */
146 void	 enqueue(struct str *x);
147 void	 printqueue(void);
148 void	 clearqueue(void);
149 
150 /* file.c */
151 void		 grep_close(struct file *f);
152 struct file	*grep_open(const char *path);
153 char		*grep_fgetln(struct file *f, size_t *len);
154