blob: 6454f9362ab82cf0aeadb8c65fbf422638886b68 [file] [log] [blame]
Jeff Sharkey3e8b1582012-07-13 16:37:13 -07001/* $NetBSD: grep.h,v 1.8 2012/05/06 22:27:00 joerg Exp $ */
2/* $OpenBSD: grep.h,v 1.15 2010/04/05 03:03:55 tedu Exp $ */
3/* $FreeBSD: head/usr.bin/grep/grep.h 211496 2010-08-19 09:28:59Z des $ */
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#ifdef ANDROID
33#define WITHOUT_NLS
34#endif
35
36#ifndef ANDROID
37#include <bzlib.h>
38#endif
39#include <limits.h>
40#include <regex.h>
41#include <stdbool.h>
42#include <stdio.h>
43#ifndef ANDROID
44#include <zlib.h>
45#endif
46
47#ifdef WITHOUT_NLS
48#define getstr(n) errstr[n]
49#else
50#include <nl_types.h>
51
52extern nl_catd catalog;
53#define getstr(n) catgets(catalog, 1, n, errstr[n])
54#endif
55
56extern const char *errstr[];
57
58#define VERSION "2.5.1-FreeBSD"
59
60#define GREP_FIXED 0
61#define GREP_BASIC 1
62#define GREP_EXTENDED 2
63
64#define BINFILE_BIN 0
65#define BINFILE_SKIP 1
66#define BINFILE_TEXT 2
67
68#define FILE_STDIO 0
69#define FILE_GZIP 1
70#define FILE_BZIP 2
71
72#define DIR_READ 0
73#define DIR_SKIP 1
74#define DIR_RECURSE 2
75
76#define DEV_READ 0
77#define DEV_SKIP 1
78
79#define LINK_READ 0
80#define LINK_EXPLICIT 1
81#define LINK_SKIP 2
82
83#define EXCL_PAT 0
84#define INCL_PAT 1
85
86#define MAX_LINE_MATCHES 32
87
88struct file {
89 int fd;
90 bool binary;
91};
92
93struct str {
94 off_t off;
95 size_t len;
96 char *dat;
97 char *file;
98 int line_no;
99};
100
101struct epat {
102 char *pat;
103 int mode;
104};
105
106typedef struct {
107 size_t len;
108 unsigned char *pattern;
109 int qsBc[UCHAR_MAX + 1];
110 /* flags */
111 bool bol;
112 bool eol;
113 bool reversed;
114 bool word;
115} fastgrep_t;
116
117/* Flags passed to regcomp() and regexec() */
118extern int cflags, eflags;
119
120/* Command line flags */
121extern bool Eflag, Fflag, Gflag, Hflag, Lflag,
122 bflag, cflag, hflag, iflag, lflag, mflag, nflag, oflag,
123 qflag, sflag, vflag, wflag, xflag;
124extern bool dexclude, dinclude, fexclude, finclude, lbflag, nullflag, nulldataflag;
125extern unsigned char line_sep;
126extern unsigned long long Aflag, Bflag, mcount;
127extern char *label;
128extern const char *color;
129extern int binbehave, devbehave, dirbehave, filebehave, grepbehave, linkbehave;
130
131extern bool notfound;
132extern int tail;
133extern unsigned int dpatterns, fpatterns, patterns;
134extern char **pattern;
135extern struct epat *dpattern, *fpattern;
136extern regex_t *er_pattern, *r_pattern;
137extern fastgrep_t *fg_pattern;
138
139/* For regex errors */
140#define RE_ERROR_BUF 512
141extern char re_error[RE_ERROR_BUF + 1]; /* Seems big enough */
142
143/* util.c */
144bool file_matching(const char *fname);
145int procfile(const char *fn);
146int grep_tree(char **argv);
147void *grep_malloc(size_t size);
148void *grep_calloc(size_t nmemb, size_t size);
149void *grep_realloc(void *ptr, size_t size);
150char *grep_strdup(const char *str);
151void printline(struct str *line, int sep, regmatch_t *matches, int m);
152
153/* queue.c */
154void enqueue(struct str *x);
155void printqueue(void);
156void clearqueue(void);
157
158/* file.c */
159void grep_close(struct file *f);
160struct file *grep_open(const char *path);
161char *grep_fgetln(struct file *f, size_t *len);
162
163/* fastgrep.c */
164int fastcomp(fastgrep_t *, const char *);
165void fgrepcomp(fastgrep_t *, const char *);
166int grep_search(fastgrep_t *, const unsigned char *, size_t, regmatch_t *);