grep.h revision 211463
1210389Sgabor/* $OpenBSD: grep.h,v 1.15 2010/04/05 03:03:55 tedu Exp $ */ 2210389Sgabor/* $FreeBSD: head/usr.bin/grep/grep.h 211463 2010-08-18 17:40:10Z gabor $ */ 3210389Sgabor 4210389Sgabor/*- 5210389Sgabor * Copyright (c) 1999 James Howard and Dag-Erling Co�dan Sm�rgrav 6210389Sgabor * Copyright (c) 2008-2009 Gabor Kovesdan <gabor@FreeBSD.org> 7210389Sgabor * All rights reserved. 8210389Sgabor * 9210389Sgabor * Redistribution and use in source and binary forms, with or without 10210389Sgabor * modification, are permitted provided that the following conditions 11210389Sgabor * are met: 12210389Sgabor * 1. Redistributions of source code must retain the above copyright 13210389Sgabor * notice, this list of conditions and the following disclaimer. 14210389Sgabor * 2. Redistributions in binary form must reproduce the above copyright 15210389Sgabor * notice, this list of conditions and the following disclaimer in the 16210389Sgabor * documentation and/or other materials provided with the distribution. 17210389Sgabor * 18210389Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19210389Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20210389Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21210389Sgabor * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22210389Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23210389Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24210389Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25210389Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26210389Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27210389Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28210389Sgabor * SUCH DAMAGE. 29210389Sgabor */ 30210389Sgabor 31210389Sgabor#include <bzlib.h> 32210389Sgabor#include <limits.h> 33210389Sgabor#include <regex.h> 34210389Sgabor#include <stdbool.h> 35210389Sgabor#include <stdio.h> 36210389Sgabor#include <zlib.h> 37210389Sgabor 38210389Sgabor#ifdef WITHOUT_NLS 39210389Sgabor#define getstr(n) errstr[n] 40210389Sgabor#else 41210389Sgabor#include <nl_types.h> 42210389Sgabor 43210389Sgaborextern nl_catd catalog; 44210389Sgabor#define getstr(n) catgets(catalog, 1, n, errstr[n]) 45210389Sgabor#endif 46210389Sgabor 47210389Sgaborextern const char *errstr[]; 48210389Sgabor 49210389Sgabor#define VERSION "2.5.1-FreeBSD" 50210389Sgabor 51210389Sgabor#define GREP_FIXED 0 52210389Sgabor#define GREP_BASIC 1 53210389Sgabor#define GREP_EXTENDED 2 54210389Sgabor 55210389Sgabor#define BINFILE_BIN 0 56210389Sgabor#define BINFILE_SKIP 1 57210389Sgabor#define BINFILE_TEXT 2 58210389Sgabor 59210389Sgabor#define FILE_STDIO 0 60210389Sgabor#define FILE_GZIP 1 61210389Sgabor#define FILE_BZIP 2 62210389Sgabor 63210461Sgabor#define DIR_READ 0 64210389Sgabor#define DIR_SKIP 1 65210389Sgabor#define DIR_RECURSE 2 66210389Sgabor 67210461Sgabor#define DEV_READ 0 68210389Sgabor#define DEV_SKIP 1 69210389Sgabor 70210461Sgabor#define LINK_READ 0 71210389Sgabor#define LINK_EXPLICIT 1 72210389Sgabor#define LINK_SKIP 2 73210389Sgabor 74210389Sgabor#define EXCL_PAT 0 75210389Sgabor#define INCL_PAT 1 76210389Sgabor 77210389Sgabor#define MAX_LINE_MATCHES 32 78210389Sgabor 79210389Sgaborstruct file { 80211463Sgabor int fd; 81210389Sgabor bool binary; 82210389Sgabor}; 83210389Sgabor 84210389Sgaborstruct str { 85210389Sgabor off_t off; 86210389Sgabor size_t len; 87210389Sgabor char *dat; 88210389Sgabor char *file; 89210389Sgabor int line_no; 90210389Sgabor}; 91210389Sgabor 92210389Sgaborstruct epat { 93210389Sgabor char *pat; 94210389Sgabor int mode; 95210389Sgabor}; 96210389Sgabor 97210389Sgabortypedef struct { 98210389Sgabor size_t len; 99210389Sgabor unsigned char *pattern; 100210389Sgabor int qsBc[UCHAR_MAX + 1]; 101210389Sgabor /* flags */ 102210389Sgabor bool bol; 103210389Sgabor bool eol; 104210389Sgabor bool reversed; 105210389Sgabor} fastgrep_t; 106210389Sgabor 107210389Sgabor/* Flags passed to regcomp() and regexec() */ 108210389Sgaborextern int cflags, eflags; 109210389Sgabor 110210389Sgabor/* Command line flags */ 111210389Sgaborextern bool Eflag, Fflag, Gflag, Hflag, Lflag, 112210389Sgabor bflag, cflag, hflag, iflag, lflag, mflag, nflag, oflag, 113210389Sgabor qflag, sflag, vflag, wflag, xflag; 114211364Sgaborextern bool dexclude, dinclude, fexclude, finclude, lbflag, nullflag; 115210389Sgaborextern unsigned long long Aflag, Bflag, mcount; 116210461Sgaborextern char *label; 117210461Sgaborextern const char *color; 118210389Sgaborextern int binbehave, devbehave, dirbehave, filebehave, grepbehave, linkbehave; 119210389Sgabor 120210389Sgaborextern bool first, matchall, notfound, prev; 121210389Sgaborextern int tail; 122210578Sgaborextern unsigned int dpatterns, fpatterns, patterns; 123210389Sgaborextern char **pattern; 124210578Sgaborextern struct epat *dpattern, *fpattern; 125210389Sgaborextern regex_t *er_pattern, *r_pattern; 126210389Sgaborextern fastgrep_t *fg_pattern; 127210389Sgabor 128210389Sgabor/* For regex errors */ 129210389Sgabor#define RE_ERROR_BUF 512 130210389Sgaborextern char re_error[RE_ERROR_BUF + 1]; /* Seems big enough */ 131210389Sgabor 132210389Sgabor/* util.c */ 133210578Sgaborbool file_matching(const char *fname); 134210389Sgaborint procfile(const char *fn); 135210389Sgaborint grep_tree(char **argv); 136210389Sgaborvoid *grep_malloc(size_t size); 137210389Sgaborvoid *grep_calloc(size_t nmemb, size_t size); 138210389Sgaborvoid *grep_realloc(void *ptr, size_t size); 139210578Sgaborchar *grep_strdup(const char *str); 140210389Sgaborvoid printline(struct str *line, int sep, regmatch_t *matches, int m); 141210389Sgabor 142210389Sgabor/* queue.c */ 143210389Sgaborvoid enqueue(struct str *x); 144210389Sgaborvoid printqueue(void); 145210389Sgaborvoid clearqueue(void); 146210389Sgabor 147210389Sgabor/* file.c */ 148210389Sgaborvoid grep_close(struct file *f); 149210389Sgaborstruct file *grep_open(const char *path); 150210389Sgaborchar *grep_fgetln(struct file *f, size_t *len); 151210389Sgabor 152210389Sgabor/* fastgrep.c */ 153210389Sgaborint fastcomp(fastgrep_t *, const char *); 154210389Sgaborvoid fgrepcomp(fastgrep_t *, const char *); 155211463Sgaborint grep_search(fastgrep_t *, const unsigned char *, size_t, regmatch_t *); 156