1/* $NetBSD: grep.h,v 1.5 2011/02/27 17:33:37 joerg Exp $ */ 2/* $OpenBSD: grep.h,v 1.15 2010/04/05 03:03:55 tedu Exp $ */ 3 4/*- 5 * SPDX-License-Identifier: BSD-2-Clause 6 * 7 * Copyright (c) 1999 James Howard and Dag-Erling Sm��rgrav 8 * Copyright (c) 2008-2009 Gabor Kovesdan <gabor@FreeBSD.org> 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33#include <bzlib.h> 34#include <limits.h> 35#include <regex.h> 36#include <stdbool.h> 37#include <stdio.h> 38#include <zlib.h> 39 40extern const char *errstr[]; 41 42#define VERSION "2.6.0-FreeBSD" 43 44#define GREP_FIXED 0 45#define GREP_BASIC 1 46#define GREP_EXTENDED 2 47 48#if !defined(REG_NOSPEC) && !defined(REG_LITERAL) 49#define WITH_INTERNAL_NOSPEC 50#endif 51 52#define BINFILE_BIN 0 53#define BINFILE_SKIP 1 54#define BINFILE_TEXT 2 55 56#define FILE_STDIO 0 57#define FILE_MMAP 1 58 59#define DIR_READ 0 60#define DIR_SKIP 1 61#define DIR_RECURSE 2 62 63#define DEV_READ 0 64#define DEV_SKIP 1 65 66#define LINK_READ 0 67#define LINK_EXPLICIT 1 68#define LINK_SKIP 2 69 70#define EXCL_PAT 0 71#define INCL_PAT 1 72 73#define MAX_MATCHES 32 74 75struct file { 76 int fd; 77 bool binary; 78}; 79 80struct str { 81 off_t boff; 82 off_t off; 83 size_t len; 84 char *dat; 85 char *file; 86 int line_no; 87}; 88 89struct pat { 90 char *pat; 91 int len; 92}; 93 94struct epat { 95 char *pat; 96 int mode; 97}; 98 99/* 100 * Parsing context; used to hold things like matches made and 101 * other useful bits 102 */ 103struct parsec { 104 regmatch_t matches[MAX_MATCHES]; /* Matches made */ 105 /* XXX TODO: This should be a chunk, not a line */ 106 struct str ln; /* Current line */ 107 size_t lnstart; /* Position in line */ 108 size_t matchidx; /* Latest match index */ 109 int printed; /* Metadata printed? */ 110 bool binary; /* Binary file? */ 111 bool cntlines; /* Count lines? */ 112}; 113 114/* Flags passed to regcomp() and regexec() */ 115extern int cflags, eflags; 116 117/* Command line flags */ 118extern bool Eflag, Fflag, Gflag, Hflag, Lflag, 119 bflag, cflag, hflag, iflag, lflag, mflag, nflag, oflag, 120 qflag, sflag, vflag, wflag, xflag; 121extern bool dexclude, dinclude, fexclude, finclude, lbflag, nullflag; 122extern long long Aflag, Bflag; 123extern long long mcount; 124extern long long mlimit; 125extern char fileeol; 126extern char *label; 127extern const char *color; 128extern int binbehave, devbehave, dirbehave, filebehave, grepbehave, linkbehave; 129 130extern bool file_err, matchall; 131extern unsigned int dpatterns, fpatterns, patterns; 132extern struct pat *pattern; 133extern struct epat *dpattern, *fpattern; 134extern regex_t *er_pattern, *r_pattern; 135 136/* For regex errors */ 137#define RE_ERROR_BUF 512 138extern char re_error[RE_ERROR_BUF + 1]; /* Seems big enough */ 139 140/* util.c */ 141bool file_matching(const char *fname); 142bool procfile(const char *fn); 143bool grep_tree(char **argv); 144void *grep_malloc(size_t size); 145void *grep_calloc(size_t nmemb, size_t size); 146void *grep_realloc(void *ptr, size_t size); 147char *grep_strdup(const char *str); 148void grep_printline(struct str *line, int sep); 149 150/* queue.c */ 151void initqueue(void); 152bool enqueue(struct str *x); 153void printqueue(void); 154void clearqueue(void); 155 156/* file.c */ 157void grep_close(struct file *f); 158struct file *grep_open(const char *path); 159char *grep_fgetln(struct file *f, struct parsec *pc); 160