1 /**	$MirOS: src/usr.bin/grep/grep.h,v 1.3 2013/08/06 16:59:33 tg Exp $ */
2 /*	$OpenBSD: grep.h,v 1.12 2004/10/03 19:23:02 otto Exp $	*/
3 
4 /*-
5  * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/types.h>
31 #include <sys/limits.h>
32 
33 #include <regex.h>
34 #include <stdio.h>
35 #include <zlib.h>
36 
37 #define VER_MAJ 0
38 #define VER_MIN 9
39 
40 #define BIN_FILE_BIN	0
41 #define BIN_FILE_SKIP	1
42 #define BIN_FILE_TEXT	2
43 
44 typedef struct {
45 	size_t		 len;
46 	int		 line_no;
47 	off_t		 off;
48 	const char	*file;
49 	char		*dat;
50 } str_t;
51 
52 typedef struct {
53 	const unsigned char *pattern;
54 	int		 patternLen;
55 	int		 qsBc[UCHAR_MAX + 1];
56 	/* flags */
57 	int		 bol;
58 	int		 eol;
59 	int		 wmatch;
60 	int		 reversedSearch;
61 } fastgrep_t;
62 
63 /* Flags passed to regcomp() and regexec() */
64 extern int	 cflags, eflags;
65 
66 /* Command line flags */
67 extern int	 Aflag, Bflag, Eflag, Fflag, Gflag, Hflag, Lflag, Pflag,
68 		 Sflag, Rflag, Zflag,
69 		 bflag, cflag, hflag, iflag, lflag, nflag, qflag, sflag,
70 		 vflag, wflag, xflag;
71 extern int	 binbehave;
72 
73 extern int	 first, lead, matchall, patterns, tail;
74 extern char    **pattern;
75 extern fastgrep_t *fg_pattern;
76 extern regex_t	*r_pattern;
77 
78 /* For regex errors  */
79 #define RE_ERROR_BUF 512
80 extern char	 re_error[RE_ERROR_BUF + 1];	/* Seems big enough */
81 
82 /* util.c */
83 int		 procfile(const char *fn);
84 int		 grep_tree(char **argv);
85 void		*grep_malloc(size_t size);
86 void		*grep_realloc(void *ptr, size_t size);
87 void		 printline(str_t *line, int sep);
88 int		 fastcomp(fastgrep_t *, const char *);
89 int		 fgrepcomp(fastgrep_t *, const char *);
90 
91 /* queue.c */
92 void		 initqueue(void);
93 void		 enqueue(str_t *x);
94 void		 printqueue(void);
95 void		 clearqueue(void);
96 int		 countqueue(void);
97 
98 /* mmfile.c */
99 typedef struct mmfile {
100 	int	 fd;
101 	size_t	 len;
102 	char	*base, *end, *ptr;
103 } mmf_t;
104 
105 mmf_t		*mmopen(char *fn, const char *mode);
106 void		 mmclose(mmf_t *mmf);
107 char		*mmfgetln(mmf_t *mmf, size_t *l);
108 long		 mmtell(mmf_t *mmf);
109 void		 mmrewind(mmf_t *mmf);
110 
111 /* file.c */
112 struct file;
113 typedef struct file file_t;
114 
115 file_t		*grep_fdopen(int fd, const char *mode);
116 file_t		*grep_open(const char *path, const char *mode);
117 int		 grep_bin_file(file_t *f);
118 long		 grep_tell(file_t *f);
119 char		*grep_fgetln(file_t *f, size_t *l);
120 void		 grep_close(file_t *f);
121 
122 /* binary.c */
123 int		 bin_file(FILE * f);
124 int		 gzbin_file(gzFile f);
125 int		 mmbin_file(mmf_t *f);
126