1 1.10 christos /* $NetBSD: grep.h,v 1.10 2018/08/12 09:03:21 christos Exp $ */ 2 1.4 joerg /* $OpenBSD: grep.h,v 1.15 2010/04/05 03:03:55 tedu Exp $ */ 3 1.4 joerg /* $FreeBSD: head/usr.bin/grep/grep.h 211496 2010-08-19 09:28:59Z des $ */ 4 1.2 cjep 5 1.1 cjep /*- 6 1.4 joerg * Copyright (c) 1999 James Howard and Dag-Erling Codan Smrgrav 7 1.4 joerg * Copyright (c) 2008-2009 Gabor Kovesdan <gabor (at) FreeBSD.org> 8 1.1 cjep * All rights reserved. 9 1.1 cjep * 10 1.1 cjep * Redistribution and use in source and binary forms, with or without 11 1.1 cjep * modification, are permitted provided that the following conditions 12 1.1 cjep * are met: 13 1.1 cjep * 1. Redistributions of source code must retain the above copyright 14 1.1 cjep * notice, this list of conditions and the following disclaimer. 15 1.1 cjep * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 cjep * notice, this list of conditions and the following disclaimer in the 17 1.1 cjep * documentation and/or other materials provided with the distribution. 18 1.1 cjep * 19 1.1 cjep * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 1.1 cjep * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 1.1 cjep * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 1.1 cjep * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 1.1 cjep * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 1.1 cjep * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 1.1 cjep * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 1.1 cjep * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 1.1 cjep * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 1.1 cjep * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 1.1 cjep * SUCH DAMAGE. 30 1.1 cjep */ 31 1.1 cjep 32 1.9 christos #ifndef WITHOUT_BZ2 33 1.4 joerg #include <bzlib.h> 34 1.9 christos #endif 35 1.4 joerg #include <limits.h> 36 1.1 cjep #include <regex.h> 37 1.4 joerg #include <stdbool.h> 38 1.1 cjep #include <stdio.h> 39 1.10 christos #ifndef WITHOUT_GZIP 40 1.1 cjep #include <zlib.h> 41 1.10 christos #endif 42 1.1 cjep 43 1.4 joerg #ifdef WITHOUT_NLS 44 1.4 joerg #define getstr(n) errstr[n] 45 1.4 joerg #else 46 1.4 joerg #include <nl_types.h> 47 1.4 joerg 48 1.4 joerg extern nl_catd catalog; 49 1.4 joerg #define getstr(n) catgets(catalog, 1, n, errstr[n]) 50 1.4 joerg #endif 51 1.2 cjep 52 1.4 joerg extern const char *errstr[]; 53 1.2 cjep 54 1.4 joerg #define VERSION "2.5.1-FreeBSD" 55 1.2 cjep 56 1.4 joerg #define GREP_FIXED 0 57 1.4 joerg #define GREP_BASIC 1 58 1.4 joerg #define GREP_EXTENDED 2 59 1.1 cjep 60 1.4 joerg #define BINFILE_BIN 0 61 1.4 joerg #define BINFILE_SKIP 1 62 1.4 joerg #define BINFILE_TEXT 2 63 1.1 cjep 64 1.4 joerg #define FILE_STDIO 0 65 1.4 joerg #define FILE_GZIP 1 66 1.4 joerg #define FILE_BZIP 2 67 1.1 cjep 68 1.4 joerg #define DIR_READ 0 69 1.4 joerg #define DIR_SKIP 1 70 1.4 joerg #define DIR_RECURSE 2 71 1.2 cjep 72 1.4 joerg #define DEV_READ 0 73 1.4 joerg #define DEV_SKIP 1 74 1.2 cjep 75 1.4 joerg #define LINK_READ 0 76 1.4 joerg #define LINK_EXPLICIT 1 77 1.4 joerg #define LINK_SKIP 2 78 1.2 cjep 79 1.4 joerg #define EXCL_PAT 0 80 1.4 joerg #define INCL_PAT 1 81 1.2 cjep 82 1.4 joerg #define MAX_LINE_MATCHES 32 83 1.2 cjep 84 1.4 joerg struct file { 85 1.4 joerg int fd; 86 1.4 joerg bool binary; 87 1.4 joerg }; 88 1.4 joerg 89 1.4 joerg struct str { 90 1.4 joerg off_t off; 91 1.4 joerg size_t len; 92 1.4 joerg char *dat; 93 1.4 joerg char *file; 94 1.4 joerg int line_no; 95 1.4 joerg }; 96 1.4 joerg 97 1.4 joerg struct epat { 98 1.4 joerg char *pat; 99 1.4 joerg int mode; 100 1.4 joerg }; 101 1.2 cjep 102 1.4 joerg typedef struct { 103 1.4 joerg size_t len; 104 1.4 joerg unsigned char *pattern; 105 1.4 joerg int qsBc[UCHAR_MAX + 1]; 106 1.4 joerg /* flags */ 107 1.4 joerg bool bol; 108 1.4 joerg bool eol; 109 1.4 joerg bool reversed; 110 1.5 joerg bool word; 111 1.4 joerg } fastgrep_t; 112 1.2 cjep 113 1.4 joerg /* Flags passed to regcomp() and regexec() */ 114 1.4 joerg extern int cflags, eflags; 115 1.2 cjep 116 1.4 joerg /* Command line flags */ 117 1.4 joerg extern bool Eflag, Fflag, Gflag, Hflag, Lflag, 118 1.4 joerg bflag, cflag, hflag, iflag, lflag, mflag, nflag, oflag, 119 1.4 joerg qflag, sflag, vflag, wflag, xflag; 120 1.7 joerg extern bool dexclude, dinclude, fexclude, finclude, lbflag, nullflag, nulldataflag; 121 1.7 joerg extern unsigned char line_sep; 122 1.4 joerg extern unsigned long long Aflag, Bflag, mcount; 123 1.4 joerg extern char *label; 124 1.4 joerg extern const char *color; 125 1.4 joerg extern int binbehave, devbehave, dirbehave, filebehave, grepbehave, linkbehave; 126 1.4 joerg 127 1.8 joerg extern bool notfound; 128 1.4 joerg extern int tail; 129 1.4 joerg extern unsigned int dpatterns, fpatterns, patterns; 130 1.4 joerg extern char **pattern; 131 1.4 joerg extern struct epat *dpattern, *fpattern; 132 1.4 joerg extern regex_t *er_pattern, *r_pattern; 133 1.4 joerg extern fastgrep_t *fg_pattern; 134 1.4 joerg 135 1.4 joerg /* For regex errors */ 136 1.4 joerg #define RE_ERROR_BUF 512 137 1.4 joerg extern char re_error[RE_ERROR_BUF + 1]; /* Seems big enough */ 138 1.1 cjep 139 1.1 cjep /* util.c */ 140 1.4 joerg bool file_matching(const char *fname); 141 1.4 joerg int procfile(const char *fn); 142 1.4 joerg int grep_tree(char **argv); 143 1.4 joerg void *grep_malloc(size_t size); 144 1.4 joerg void *grep_calloc(size_t nmemb, size_t size); 145 1.4 joerg void *grep_realloc(void *ptr, size_t size); 146 1.4 joerg char *grep_strdup(const char *str); 147 1.4 joerg void printline(struct str *line, int sep, regmatch_t *matches, int m); 148 1.1 cjep 149 1.1 cjep /* queue.c */ 150 1.4 joerg void enqueue(struct str *x); 151 1.4 joerg void printqueue(void); 152 1.4 joerg void clearqueue(void); 153 1.1 cjep 154 1.1 cjep /* file.c */ 155 1.4 joerg void grep_close(struct file *f); 156 1.4 joerg struct file *grep_open(const char *path); 157 1.4 joerg char *grep_fgetln(struct file *f, size_t *len); 158 1.4 joerg 159 1.4 joerg /* fastgrep.c */ 160 1.4 joerg int fastcomp(fastgrep_t *, const char *); 161 1.4 joerg void fgrepcomp(fastgrep_t *, const char *); 162 1.4 joerg int grep_search(fastgrep_t *, const unsigned char *, size_t, regmatch_t *); 163