grep.h revision 265161
1219019Sgabor/* $NetBSD: grep.h,v 1.5 2011/02/27 17:33:37 joerg Exp $ */ 2219019Sgabor/* $OpenBSD: grep.h,v 1.15 2010/04/05 03:03:55 tedu Exp $ */ 3219019Sgabor/* $FreeBSD: stable/9/usr.bin/grep/grep.h 265161 2014-04-30 20:46:52Z pfg $ */ 4219019Sgabor 5219019Sgabor/*- 6219019Sgabor * Copyright (c) 1999 James Howard and Dag-Erling Co��dan Sm��rgrav 7219019Sgabor * Copyright (c) 2008-2009 Gabor Kovesdan <gabor@FreeBSD.org> 8219019Sgabor * All rights reserved. 9219019Sgabor * 10219019Sgabor * Redistribution and use in source and binary forms, with or without 11219019Sgabor * modification, are permitted provided that the following conditions 12219019Sgabor * are met: 13219019Sgabor * 1. Redistributions of source code must retain the above copyright 14219019Sgabor * notice, this list of conditions and the following disclaimer. 15219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright 16219019Sgabor * notice, this list of conditions and the following disclaimer in the 17219019Sgabor * documentation and/or other materials provided with the distribution. 18219019Sgabor * 19219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20219019Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22219019Sgabor * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23219019Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24219019Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25219019Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26219019Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27219019Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28219019Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29219019Sgabor * SUCH DAMAGE. 30219019Sgabor */ 31219019Sgabor 32219019Sgabor#include <bzlib.h> 33219019Sgabor#include <limits.h> 34219019Sgabor#include <regex.h> 35219019Sgabor#include <stdbool.h> 36219019Sgabor#include <stdio.h> 37219019Sgabor#include <zlib.h> 38219019Sgabor 39219019Sgabor#include "fastmatch.h" 40219019Sgabor 41219019Sgabor#ifdef WITHOUT_NLS 42219019Sgabor#define getstr(n) errstr[n] 43219019Sgabor#else 44219019Sgabor#include <nl_types.h> 45219019Sgabor 46219019Sgaborextern nl_catd catalog; 47219019Sgabor#define getstr(n) catgets(catalog, 1, n, errstr[n]) 48219019Sgabor#endif 49219019Sgabor 50219019Sgaborextern const char *errstr[]; 51219019Sgabor 52219019Sgabor#define VERSION "2.5.1-FreeBSD" 53219019Sgabor 54219019Sgabor#define GREP_FIXED 0 55219019Sgabor#define GREP_BASIC 1 56219019Sgabor#define GREP_EXTENDED 2 57219019Sgabor 58219019Sgabor#define BINFILE_BIN 0 59219019Sgabor#define BINFILE_SKIP 1 60219019Sgabor#define BINFILE_TEXT 2 61219019Sgabor 62219019Sgabor#define FILE_STDIO 0 63219019Sgabor#define FILE_MMAP 1 64219019Sgabor#define FILE_GZIP 2 65219019Sgabor#define FILE_BZIP 3 66219019Sgabor#define FILE_XZ 4 67219019Sgabor#define FILE_LZMA 5 68219019Sgabor 69219019Sgabor#define DIR_READ 0 70219019Sgabor#define DIR_SKIP 1 71219019Sgabor#define DIR_RECURSE 2 72219019Sgabor 73219019Sgabor#define DEV_READ 0 74219019Sgabor#define DEV_SKIP 1 75219019Sgabor 76219019Sgabor#define LINK_READ 0 77219019Sgabor#define LINK_EXPLICIT 1 78219019Sgabor#define LINK_SKIP 2 79219019Sgabor 80219019Sgabor#define EXCL_PAT 0 81219019Sgabor#define INCL_PAT 1 82219019Sgabor 83219019Sgabor#define MAX_LINE_MATCHES 32 84219019Sgabor 85219019Sgaborstruct file { 86219019Sgabor int fd; 87219019Sgabor bool binary; 88219019Sgabor}; 89219019Sgabor 90219019Sgaborstruct str { 91219019Sgabor off_t off; 92219019Sgabor size_t len; 93219019Sgabor char *dat; 94219019Sgabor char *file; 95219019Sgabor int line_no; 96219019Sgabor}; 97219019Sgabor 98219019Sgaborstruct pat { 99219019Sgabor char *pat; 100219019Sgabor int len; 101219019Sgabor}; 102219019Sgabor 103219019Sgaborstruct epat { 104219019Sgabor char *pat; 105219019Sgabor int mode; 106219019Sgabor}; 107219019Sgabor 108219019Sgabor/* Flags passed to regcomp() and regexec() */ 109219019Sgaborextern int cflags, eflags; 110219019Sgabor 111219019Sgabor/* Command line flags */ 112219019Sgaborextern bool Eflag, Fflag, Gflag, Hflag, Lflag, 113219019Sgabor bflag, cflag, hflag, iflag, lflag, mflag, nflag, oflag, 114219019Sgabor qflag, sflag, vflag, wflag, xflag; 115219019Sgaborextern bool dexclude, dinclude, fexclude, finclude, lbflag, nullflag; 116219019Sgaborextern unsigned long long Aflag, Bflag; 117219019Sgaborextern long long mcount; 118219019Sgaborextern long long mlimit; 119219019Sgaborextern char *label; 120219019Sgaborextern const char *color; 121219019Sgaborextern int binbehave, devbehave, dirbehave, filebehave, grepbehave, linkbehave; 122219019Sgabor 123219019Sgaborextern bool file_err, first, matchall, prev; 124219019Sgaborextern int tail; 125219019Sgaborextern unsigned int dpatterns, fpatterns, patterns; 126219019Sgaborextern struct pat *pattern; 127219019Sgaborextern struct epat *dpattern, *fpattern; 128219019Sgaborextern regex_t *er_pattern, *r_pattern; 129219019Sgaborextern fastmatch_t *fg_pattern; 130219019Sgabor 131219019Sgabor/* For regex errors */ 132219019Sgabor#define RE_ERROR_BUF 512 133219019Sgaborextern char re_error[RE_ERROR_BUF + 1]; /* Seems big enough */ 134219019Sgabor 135219019Sgabor/* util.c */ 136219019Sgaborbool file_matching(const char *fname); 137219019Sgaborint procfile(const char *fn); 138219019Sgaborint grep_tree(char **argv); 139219019Sgaborvoid *grep_malloc(size_t size); 140219019Sgaborvoid *grep_calloc(size_t nmemb, size_t size); 141219019Sgaborvoid *grep_realloc(void *ptr, size_t size); 142219019Sgaborchar *grep_strdup(const char *str); 143219019Sgaborvoid printline(struct str *line, int sep, regmatch_t *matches, int m); 144219019Sgabor 145219019Sgabor/* queue.c */ 146219019Sgaborvoid enqueue(struct str *x); 147219019Sgaborvoid printqueue(void); 148219019Sgaborvoid clearqueue(void); 149219019Sgabor 150219019Sgabor/* file.c */ 151219019Sgaborvoid grep_close(struct file *f); 152219019Sgaborstruct file *grep_open(const char *path); 153219019Sgaborchar *grep_fgetln(struct file *f, size_t *len); 154219019Sgabor