1b66a823bSGabor Kovesdan /* $NetBSD: grep.h,v 1.5 2011/02/27 17:33:37 joerg Exp $ */ 24dc88ebeSGabor Kovesdan /* $OpenBSD: grep.h,v 1.15 2010/04/05 03:03:55 tedu Exp $ */ 34dc88ebeSGabor Kovesdan 44dc88ebeSGabor Kovesdan /*- 54d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 61de7b4b8SPedro F. Giffuni * 7*e738085bSDag-Erling Smørgrav * Copyright (c) 1999 James Howard and Dag-Erling Smørgrav 84dc88ebeSGabor Kovesdan * Copyright (c) 2008-2009 Gabor Kovesdan <gabor@FreeBSD.org> 94dc88ebeSGabor Kovesdan * All rights reserved. 104dc88ebeSGabor Kovesdan * 114dc88ebeSGabor Kovesdan * Redistribution and use in source and binary forms, with or without 124dc88ebeSGabor Kovesdan * modification, are permitted provided that the following conditions 134dc88ebeSGabor Kovesdan * are met: 144dc88ebeSGabor Kovesdan * 1. Redistributions of source code must retain the above copyright 154dc88ebeSGabor Kovesdan * notice, this list of conditions and the following disclaimer. 164dc88ebeSGabor Kovesdan * 2. Redistributions in binary form must reproduce the above copyright 174dc88ebeSGabor Kovesdan * notice, this list of conditions and the following disclaimer in the 184dc88ebeSGabor Kovesdan * documentation and/or other materials provided with the distribution. 194dc88ebeSGabor Kovesdan * 204dc88ebeSGabor Kovesdan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 214dc88ebeSGabor Kovesdan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 224dc88ebeSGabor Kovesdan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 234dc88ebeSGabor Kovesdan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 244dc88ebeSGabor Kovesdan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 254dc88ebeSGabor Kovesdan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 264dc88ebeSGabor Kovesdan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 274dc88ebeSGabor Kovesdan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 284dc88ebeSGabor Kovesdan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 294dc88ebeSGabor Kovesdan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 304dc88ebeSGabor Kovesdan * SUCH DAMAGE. 314dc88ebeSGabor Kovesdan */ 324dc88ebeSGabor Kovesdan 334dc88ebeSGabor Kovesdan #include <bzlib.h> 344dc88ebeSGabor Kovesdan #include <limits.h> 354dc88ebeSGabor Kovesdan #include <regex.h> 364dc88ebeSGabor Kovesdan #include <stdbool.h> 374dc88ebeSGabor Kovesdan #include <stdio.h> 384dc88ebeSGabor Kovesdan #include <zlib.h> 394dc88ebeSGabor Kovesdan 404dc88ebeSGabor Kovesdan extern const char *errstr[]; 414dc88ebeSGabor Kovesdan 42b05c7cdeSEd Maste #define VERSION "2.6.0-FreeBSD" 434dc88ebeSGabor Kovesdan 444dc88ebeSGabor Kovesdan #define GREP_FIXED 0 454dc88ebeSGabor Kovesdan #define GREP_BASIC 1 464dc88ebeSGabor Kovesdan #define GREP_EXTENDED 2 474dc88ebeSGabor Kovesdan 4805ad8215SKyle Evans #if !defined(REG_NOSPEC) && !defined(REG_LITERAL) 4905ad8215SKyle Evans #define WITH_INTERNAL_NOSPEC 5005ad8215SKyle Evans #endif 5105ad8215SKyle Evans 524dc88ebeSGabor Kovesdan #define BINFILE_BIN 0 534dc88ebeSGabor Kovesdan #define BINFILE_SKIP 1 544dc88ebeSGabor Kovesdan #define BINFILE_TEXT 2 554dc88ebeSGabor Kovesdan 564dc88ebeSGabor Kovesdan #define FILE_STDIO 0 57f20f6f3fSGabor Kovesdan #define FILE_MMAP 1 584dc88ebeSGabor Kovesdan 5927116286SGabor Kovesdan #define DIR_READ 0 604dc88ebeSGabor Kovesdan #define DIR_SKIP 1 614dc88ebeSGabor Kovesdan #define DIR_RECURSE 2 624dc88ebeSGabor Kovesdan 6327116286SGabor Kovesdan #define DEV_READ 0 644dc88ebeSGabor Kovesdan #define DEV_SKIP 1 654dc88ebeSGabor Kovesdan 6627116286SGabor Kovesdan #define LINK_READ 0 674dc88ebeSGabor Kovesdan #define LINK_EXPLICIT 1 684dc88ebeSGabor Kovesdan #define LINK_SKIP 2 694dc88ebeSGabor Kovesdan 704dc88ebeSGabor Kovesdan #define EXCL_PAT 0 714dc88ebeSGabor Kovesdan #define INCL_PAT 1 724dc88ebeSGabor Kovesdan 738bf46064SEd Maste #define MAX_MATCHES 32 744dc88ebeSGabor Kovesdan 754dc88ebeSGabor Kovesdan struct file { 763ed1008bSGabor Kovesdan int fd; 774dc88ebeSGabor Kovesdan bool binary; 784dc88ebeSGabor Kovesdan }; 794dc88ebeSGabor Kovesdan 804dc88ebeSGabor Kovesdan struct str { 816d635d3bSEd Maste off_t boff; 824dc88ebeSGabor Kovesdan off_t off; 834dc88ebeSGabor Kovesdan size_t len; 844dc88ebeSGabor Kovesdan char *dat; 854dc88ebeSGabor Kovesdan char *file; 864dc88ebeSGabor Kovesdan int line_no; 874dc88ebeSGabor Kovesdan }; 884dc88ebeSGabor Kovesdan 89f20f6f3fSGabor Kovesdan struct pat { 90f20f6f3fSGabor Kovesdan char *pat; 91f20f6f3fSGabor Kovesdan int len; 92f20f6f3fSGabor Kovesdan }; 93f20f6f3fSGabor Kovesdan 944dc88ebeSGabor Kovesdan struct epat { 954dc88ebeSGabor Kovesdan char *pat; 964dc88ebeSGabor Kovesdan int mode; 974dc88ebeSGabor Kovesdan }; 984dc88ebeSGabor Kovesdan 99bd60b9b4SKyle Evans /* 100bd60b9b4SKyle Evans * Parsing context; used to hold things like matches made and 101bd60b9b4SKyle Evans * other useful bits 102bd60b9b4SKyle Evans */ 103bd60b9b4SKyle Evans struct parsec { 104bd60b9b4SKyle Evans regmatch_t matches[MAX_MATCHES]; /* Matches made */ 105bd60b9b4SKyle Evans /* XXX TODO: This should be a chunk, not a line */ 106bd60b9b4SKyle Evans struct str ln; /* Current line */ 107bd60b9b4SKyle Evans size_t lnstart; /* Position in line */ 108bd60b9b4SKyle Evans size_t matchidx; /* Latest match index */ 109bd60b9b4SKyle Evans int printed; /* Metadata printed? */ 110bd60b9b4SKyle Evans bool binary; /* Binary file? */ 111bd60b9b4SKyle Evans bool cntlines; /* Count lines? */ 112bd60b9b4SKyle Evans }; 113bd60b9b4SKyle Evans 1144dc88ebeSGabor Kovesdan /* Flags passed to regcomp() and regexec() */ 1154dc88ebeSGabor Kovesdan extern int cflags, eflags; 1164dc88ebeSGabor Kovesdan 1174dc88ebeSGabor Kovesdan /* Command line flags */ 1184dc88ebeSGabor Kovesdan extern bool Eflag, Fflag, Gflag, Hflag, Lflag, 1194dc88ebeSGabor Kovesdan bflag, cflag, hflag, iflag, lflag, mflag, nflag, oflag, 1204dc88ebeSGabor Kovesdan qflag, sflag, vflag, wflag, xflag; 12159218eb7SGabor Kovesdan extern bool dexclude, dinclude, fexclude, finclude, lbflag, nullflag; 122b5fc583cSEd Maste extern long long Aflag, Bflag; 123f20f6f3fSGabor Kovesdan extern long long mcount; 124924500b7SEitan Adler extern long long mlimit; 1255ee1ea02SEd Maste extern char fileeol; 12627116286SGabor Kovesdan extern char *label; 12727116286SGabor Kovesdan extern const char *color; 1284dc88ebeSGabor Kovesdan extern int binbehave, devbehave, dirbehave, filebehave, grepbehave, linkbehave; 1294dc88ebeSGabor Kovesdan 130a4f3f02bSEd Maste extern bool file_err, matchall; 13155e44f51SGabor Kovesdan extern unsigned int dpatterns, fpatterns, patterns; 132f20f6f3fSGabor Kovesdan extern struct pat *pattern; 13355e44f51SGabor Kovesdan extern struct epat *dpattern, *fpattern; 1344dc88ebeSGabor Kovesdan extern regex_t *er_pattern, *r_pattern; 1354dc88ebeSGabor Kovesdan 1364dc88ebeSGabor Kovesdan /* For regex errors */ 1374dc88ebeSGabor Kovesdan #define RE_ERROR_BUF 512 1384dc88ebeSGabor Kovesdan extern char re_error[RE_ERROR_BUF + 1]; /* Seems big enough */ 1394dc88ebeSGabor Kovesdan 1404dc88ebeSGabor Kovesdan /* util.c */ 14155e44f51SGabor Kovesdan bool file_matching(const char *fname); 142cbfff13fSKyle Evans bool procfile(const char *fn); 143cbfff13fSKyle Evans bool grep_tree(char **argv); 1444dc88ebeSGabor Kovesdan void *grep_malloc(size_t size); 1454dc88ebeSGabor Kovesdan void *grep_calloc(size_t nmemb, size_t size); 1464dc88ebeSGabor Kovesdan void *grep_realloc(void *ptr, size_t size); 14755e44f51SGabor Kovesdan char *grep_strdup(const char *str); 148a4f3f02bSEd Maste void grep_printline(struct str *line, int sep); 1494dc88ebeSGabor Kovesdan 1504dc88ebeSGabor Kovesdan /* queue.c */ 151df546c3bSKyle Evans void initqueue(void); 152a4f3f02bSEd Maste bool enqueue(struct str *x); 1534dc88ebeSGabor Kovesdan void printqueue(void); 1544dc88ebeSGabor Kovesdan void clearqueue(void); 1554dc88ebeSGabor Kovesdan 1564dc88ebeSGabor Kovesdan /* file.c */ 1574dc88ebeSGabor Kovesdan void grep_close(struct file *f); 1584dc88ebeSGabor Kovesdan struct file *grep_open(const char *path); 159bd60b9b4SKyle Evans char *grep_fgetln(struct file *f, struct parsec *pc); 160