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