xref: /netbsd-src/usr.bin/grep/grep.h (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: grep.h,v 1.2 2004/05/05 15:06:33 cjep 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 
31 #include <regex.h>
32 #include <stdio.h>
33 #include <zlib.h>
34 
35 #define VERSION "20040505"
36 
37 #define	GREP_READ	0
38 #define GREP_SKIP	1
39 #define GREP_RECURSE	2
40 
41 #define BIN_FILE_BIN	0
42 #define BIN_FILE_SKIP	1
43 #define BIN_FILE_TEXT	2
44 
45 #define LINK_EXPLICIT	0
46 #define LINK_FOLLOW	1
47 #define LINK_SKIP	2
48 
49 #define MAX_LINE_MATCHES	30
50 
51 typedef struct {
52 	size_t len;
53 	int line_no;
54 	int off;
55 	char *file;
56 	char *dat;
57 } str_t;
58 
59 /* Flags passed to regcomp() and regexec() */
60 extern int cflags, eflags;
61 
62 /* Command line flags */
63 extern int Aflag, Bflag, Fflag, Lflag, bflag, cflag, lflag, mflag,
64 	nflag, oflag, qflag, sflag, vflag, wflag, xflag;
65 
66 extern int colours;
67 extern const char *grep_colour;
68 extern char fn_endchar, fn_colonchar, fn_dashchar;
69 extern char line_endchar;
70 
71 extern int output_filenames;
72 
73 extern int maxcount;
74 
75 /* Argv[0] flags */
76 extern int zgrep;
77 
78 extern int binbehave, dirbehave, devbehave;
79 /* extern int linkbehave; */
80 extern char *stdin_label;
81 
82 extern int first, matchall, patterns, tail;
83 extern char **pattern;
84 extern regex_t *r_pattern;
85 
86 /*
87  * For regex errors, 512 seems to be big enough
88  */
89 #define RE_ERROR_BUF 	512
90 
91 extern char re_error[RE_ERROR_BUF + 1];
92 
93 /* util.c */
94 int procfile(char *fn);
95 int grep_tree(char **argv);
96 
97 void *grep_malloc(size_t size);
98 void *grep_realloc(void *ptr, size_t size);
99 void printline(str_t *line, int sep, regmatch_t *matches, int m);
100 
101 /* queue.c */
102 void initqueue(void);
103 void enqueue(str_t *x);
104 void printqueue(void);
105 void clearqueue(void);
106 
107 /* mmfile.c */
108 typedef struct mmfile {
109 	int fd;
110 	size_t len;
111 	char *base, *end, *ptr;
112 } mmf_t;
113 
114 mmf_t *mmopen(char *fn, char *mode);
115 void mmclose(mmf_t *mmf);
116 char *mmfgetln(mmf_t *mmf, size_t *l);
117 void mmrewind(mmf_t *mmf);
118 
119 /* file.c */
120 struct file;
121 typedef struct file file_t;
122 
123 file_t *grep_fdopen(int fd, char *mode);
124 file_t *grep_open(char *path, char *mode);
125 
126 int grep_bin_file(file_t *f);
127 char *grep_fgetln(file_t *f, size_t *l);
128 void grep_close(file_t *f);
129 
130 /* binary.c */
131 int bin_file(FILE * f);
132 int gzbin_file(gzFile * f);
133 int mmbin_file(mmf_t *f);
134 
135