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