1220422Sgabor/* $NetBSD: grep.h,v 1.5 2011/02/27 17:33:37 joerg Exp $ */ 2210389Sgabor/* $OpenBSD: grep.h,v 1.15 2010/04/05 03:03:55 tedu Exp $ */ 3210389Sgabor/* $FreeBSD$ */ 4210389Sgabor 5210389Sgabor/*- 6211496Sdes * Copyright (c) 1999 James Howard and Dag-Erling Co��dan Sm��rgrav 7210389Sgabor * Copyright (c) 2008-2009 Gabor Kovesdan <gabor@FreeBSD.org> 8210389Sgabor * All rights reserved. 9210389Sgabor * 10210389Sgabor * Redistribution and use in source and binary forms, with or without 11210389Sgabor * modification, are permitted provided that the following conditions 12210389Sgabor * are met: 13210389Sgabor * 1. Redistributions of source code must retain the above copyright 14210389Sgabor * notice, this list of conditions and the following disclaimer. 15210389Sgabor * 2. Redistributions in binary form must reproduce the above copyright 16210389Sgabor * notice, this list of conditions and the following disclaimer in the 17210389Sgabor * documentation and/or other materials provided with the distribution. 18210389Sgabor * 19210389Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20210389Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21210389Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22210389Sgabor * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23210389Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24210389Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25210389Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26210389Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27210389Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28210389Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29210389Sgabor * SUCH DAMAGE. 30210389Sgabor */ 31210389Sgabor 32210389Sgabor#include <bzlib.h> 33210389Sgabor#include <limits.h> 34210389Sgabor#include <regex.h> 35210389Sgabor#include <stdbool.h> 36210389Sgabor#include <stdio.h> 37210389Sgabor#include <zlib.h> 38210389Sgabor 39226261Sgabor#include "fastmatch.h" 40226261Sgabor 41210389Sgabor#ifdef WITHOUT_NLS 42265161Spfg#define getstr(n) errstr[n] 43210389Sgabor#else 44210389Sgabor#include <nl_types.h> 45210389Sgabor 46210389Sgaborextern nl_catd catalog; 47265161Spfg#define getstr(n) catgets(catalog, 1, n, errstr[n]) 48210389Sgabor#endif 49210389Sgabor 50210389Sgaborextern const char *errstr[]; 51210389Sgabor 52265161Spfg#define VERSION "2.5.1-FreeBSD" 53210389Sgabor 54265161Spfg#define GREP_FIXED 0 55265161Spfg#define GREP_BASIC 1 56265161Spfg#define GREP_EXTENDED 2 57210389Sgabor 58265161Spfg#define BINFILE_BIN 0 59265161Spfg#define BINFILE_SKIP 1 60265161Spfg#define BINFILE_TEXT 2 61210389Sgabor 62265161Spfg#define FILE_STDIO 0 63265161Spfg#define FILE_MMAP 1 64265161Spfg#define FILE_GZIP 2 65265161Spfg#define FILE_BZIP 3 66265161Spfg#define FILE_XZ 4 67265161Spfg#define FILE_LZMA 5 68210389Sgabor 69265161Spfg#define DIR_READ 0 70265161Spfg#define DIR_SKIP 1 71265161Spfg#define DIR_RECURSE 2 72210389Sgabor 73265161Spfg#define DEV_READ 0 74265161Spfg#define DEV_SKIP 1 75210389Sgabor 76265161Spfg#define LINK_READ 0 77265161Spfg#define LINK_EXPLICIT 1 78265161Spfg#define LINK_SKIP 2 79210389Sgabor 80265161Spfg#define EXCL_PAT 0 81265161Spfg#define INCL_PAT 1 82210389Sgabor 83265161Spfg#define MAX_LINE_MATCHES 32 84210389Sgabor 85210389Sgaborstruct file { 86211463Sgabor int fd; 87210389Sgabor bool binary; 88210389Sgabor}; 89210389Sgabor 90210389Sgaborstruct str { 91210389Sgabor off_t off; 92210389Sgabor size_t len; 93210389Sgabor char *dat; 94210389Sgabor char *file; 95210389Sgabor int line_no; 96210389Sgabor}; 97210389Sgabor 98226261Sgaborstruct pat { 99226261Sgabor char *pat; 100226261Sgabor int len; 101226261Sgabor}; 102226261Sgabor 103210389Sgaborstruct epat { 104210389Sgabor char *pat; 105210389Sgabor int mode; 106210389Sgabor}; 107210389Sgabor 108210389Sgabor/* Flags passed to regcomp() and regexec() */ 109210389Sgaborextern int cflags, eflags; 110210389Sgabor 111210389Sgabor/* Command line flags */ 112210389Sgaborextern bool Eflag, Fflag, Gflag, Hflag, Lflag, 113210389Sgabor bflag, cflag, hflag, iflag, lflag, mflag, nflag, oflag, 114210389Sgabor qflag, sflag, vflag, wflag, xflag; 115211364Sgaborextern bool dexclude, dinclude, fexclude, finclude, lbflag, nullflag; 116226261Sgaborextern unsigned long long Aflag, Bflag; 117226261Sgaborextern long long mcount; 118246279Seadlerextern long long mlimit; 119210461Sgaborextern char *label; 120210461Sgaborextern const char *color; 121210389Sgaborextern int binbehave, devbehave, dirbehave, filebehave, grepbehave, linkbehave; 122210389Sgabor 123229081Sgaborextern bool file_err, first, matchall, prev; 124210389Sgaborextern int tail; 125210578Sgaborextern unsigned int dpatterns, fpatterns, patterns; 126226261Sgaborextern struct pat *pattern; 127210578Sgaborextern struct epat *dpattern, *fpattern; 128210389Sgaborextern regex_t *er_pattern, *r_pattern; 129226261Sgaborextern fastmatch_t *fg_pattern; 130210389Sgabor 131210389Sgabor/* For regex errors */ 132265161Spfg#define RE_ERROR_BUF 512 133210389Sgaborextern char re_error[RE_ERROR_BUF + 1]; /* Seems big enough */ 134210389Sgabor 135210389Sgabor/* util.c */ 136210578Sgaborbool file_matching(const char *fname); 137210389Sgaborint procfile(const char *fn); 138210389Sgaborint grep_tree(char **argv); 139210389Sgaborvoid *grep_malloc(size_t size); 140210389Sgaborvoid *grep_calloc(size_t nmemb, size_t size); 141210389Sgaborvoid *grep_realloc(void *ptr, size_t size); 142210578Sgaborchar *grep_strdup(const char *str); 143210389Sgaborvoid printline(struct str *line, int sep, regmatch_t *matches, int m); 144210389Sgabor 145210389Sgabor/* queue.c */ 146210389Sgaborvoid enqueue(struct str *x); 147210389Sgaborvoid printqueue(void); 148210389Sgaborvoid clearqueue(void); 149210389Sgabor 150210389Sgabor/* file.c */ 151210389Sgaborvoid grep_close(struct file *f); 152210389Sgaborstruct file *grep_open(const char *path); 153210389Sgaborchar *grep_fgetln(struct file *f, size_t *len); 154