2011-04-07 13:03:35 +00:00
|
|
|
/* $NetBSD: file.c,v 1.5 2011/02/16 18:35:39 joerg Exp $ */
|
|
|
|
/* $FreeBSD$ */
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
/* $OpenBSD: file.c,v 1.11 2010/07/02 20:48:48 nicm Exp $ */
|
|
|
|
|
|
|
|
/*-
|
2010-08-19 09:28:59 +00:00
|
|
|
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
|
2010-08-18 17:40:10 +00:00
|
|
|
* Copyright (C) 2008-2010 Gabor Kovesdan <gabor@FreeBSD.org>
|
|
|
|
* Copyright (C) 2010 Dimitry Andric <dimitry@andric.com>
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2011-10-05 09:56:43 +00:00
|
|
|
#include <sys/mman.h>
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
#include <sys/stat.h>
|
2011-10-05 09:56:43 +00:00
|
|
|
#include <sys/types.h>
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2010-08-18 17:40:10 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stddef.h>
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
|
|
|
#include <zlib.h>
|
|
|
|
|
2013-01-08 18:37:12 +00:00
|
|
|
#ifndef WITHOUT_LZMA
|
|
|
|
#include <lzma.h>
|
|
|
|
#endif
|
|
|
|
|
2011-10-11 22:27:23 +00:00
|
|
|
#ifndef WITHOUT_BZIP2
|
|
|
|
#include <bzlib.h>
|
|
|
|
#endif
|
|
|
|
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
#include "grep.h"
|
|
|
|
|
2010-08-18 17:40:10 +00:00
|
|
|
#define MAXBUFSIZ (32 * 1024)
|
|
|
|
#define LNBUFBUMP 80
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
|
2010-08-18 17:40:10 +00:00
|
|
|
static gzFile gzbufdesc;
|
2013-01-08 18:37:12 +00:00
|
|
|
#ifndef WITHOUT_LZMA
|
2011-10-05 09:56:43 +00:00
|
|
|
static lzma_stream lstrm = LZMA_STREAM_INIT;
|
2015-01-21 01:11:37 +00:00
|
|
|
static lzma_action laction;
|
|
|
|
static uint8_t lin_buf[MAXBUFSIZ];
|
2013-01-08 18:37:12 +00:00
|
|
|
#endif
|
2011-10-11 22:27:23 +00:00
|
|
|
#ifndef WITHOUT_BZIP2
|
|
|
|
static BZFILE* bzbufdesc;
|
|
|
|
#endif
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
|
2011-10-05 09:56:43 +00:00
|
|
|
static unsigned char *buffer;
|
2010-08-18 17:40:10 +00:00
|
|
|
static unsigned char *bufpos;
|
|
|
|
static size_t bufrem;
|
2011-10-05 09:56:43 +00:00
|
|
|
static size_t fsiz;
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
|
2010-08-18 17:40:10 +00:00
|
|
|
static unsigned char *lnbuf;
|
|
|
|
static size_t lnbuflen;
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
|
2010-08-15 22:15:04 +00:00
|
|
|
static inline int
|
2010-08-18 17:40:10 +00:00
|
|
|
grep_refill(struct file *f)
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
{
|
2010-08-18 17:40:10 +00:00
|
|
|
ssize_t nr;
|
|
|
|
|
2011-10-05 09:56:43 +00:00
|
|
|
if (filebehave == FILE_MMAP)
|
|
|
|
return (0);
|
|
|
|
|
2010-08-18 17:40:10 +00:00
|
|
|
bufpos = buffer;
|
|
|
|
bufrem = 0;
|
|
|
|
|
2011-10-11 22:27:23 +00:00
|
|
|
if (filebehave == FILE_GZIP) {
|
2010-08-18 17:40:10 +00:00
|
|
|
nr = gzread(gzbufdesc, buffer, MAXBUFSIZ);
|
2011-10-11 22:27:23 +00:00
|
|
|
#ifndef WITHOUT_BZIP2
|
|
|
|
} else if (filebehave == FILE_BZIP && bzbufdesc != NULL) {
|
|
|
|
int bzerr;
|
|
|
|
|
2010-08-18 17:40:10 +00:00
|
|
|
nr = BZ2_bzRead(&bzerr, bzbufdesc, buffer, MAXBUFSIZ);
|
|
|
|
switch (bzerr) {
|
|
|
|
case BZ_OK:
|
|
|
|
case BZ_STREAM_END:
|
|
|
|
/* No problem, nr will be okay */
|
|
|
|
break;
|
|
|
|
case BZ_DATA_ERROR_MAGIC:
|
|
|
|
/*
|
|
|
|
* As opposed to gzread(), which simply returns the
|
|
|
|
* plain file data, if it is not in the correct
|
|
|
|
* compressed format, BZ2_bzRead() instead aborts.
|
|
|
|
*
|
|
|
|
* So, just restart at the beginning of the file again,
|
|
|
|
* and use plain reads from now on.
|
|
|
|
*/
|
|
|
|
BZ2_bzReadClose(&bzerr, bzbufdesc);
|
|
|
|
bzbufdesc = NULL;
|
|
|
|
if (lseek(f->fd, 0, SEEK_SET) == -1)
|
|
|
|
return (-1);
|
|
|
|
nr = read(f->fd, buffer, MAXBUFSIZ);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Make sure we exit with an error */
|
|
|
|
nr = -1;
|
|
|
|
}
|
2011-10-11 22:27:23 +00:00
|
|
|
#endif
|
2013-01-08 18:37:12 +00:00
|
|
|
#ifndef WITHOUT_LZMA
|
2011-10-05 09:56:43 +00:00
|
|
|
} else if ((filebehave == FILE_XZ) || (filebehave == FILE_LZMA)) {
|
|
|
|
lzma_ret ret;
|
2015-01-21 01:11:37 +00:00
|
|
|
lstrm.next_out = buffer;
|
2011-10-05 09:56:43 +00:00
|
|
|
|
2015-01-21 01:11:37 +00:00
|
|
|
do {
|
|
|
|
if (lstrm.avail_in == 0) {
|
|
|
|
lstrm.next_in = lin_buf;
|
|
|
|
nr = read(f->fd, lin_buf, MAXBUFSIZ);
|
2011-10-05 09:56:43 +00:00
|
|
|
|
2015-01-21 01:11:37 +00:00
|
|
|
if (nr < 0)
|
|
|
|
return (-1);
|
|
|
|
else if (nr == 0)
|
|
|
|
laction = LZMA_FINISH;
|
2011-10-05 09:56:43 +00:00
|
|
|
|
2015-01-21 01:11:37 +00:00
|
|
|
lstrm.avail_in = nr;
|
|
|
|
}
|
2011-10-05 09:56:43 +00:00
|
|
|
|
2015-01-21 01:11:37 +00:00
|
|
|
ret = lzma_code(&lstrm, laction);
|
2011-10-05 09:56:43 +00:00
|
|
|
|
2015-01-21 01:11:37 +00:00
|
|
|
if (ret != LZMA_OK && ret != LZMA_STREAM_END)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if (lstrm.avail_out == 0 || ret == LZMA_STREAM_END) {
|
|
|
|
bufrem = MAXBUFSIZ - lstrm.avail_out;
|
|
|
|
lstrm.next_out = buffer;
|
|
|
|
lstrm.avail_out = MAXBUFSIZ;
|
|
|
|
}
|
|
|
|
} while (bufrem == 0 && ret != LZMA_STREAM_END);
|
2011-10-05 09:56:43 +00:00
|
|
|
|
|
|
|
return (0);
|
2013-01-08 18:37:12 +00:00
|
|
|
#endif /* WIHTOUT_LZMA */
|
2010-08-18 17:40:10 +00:00
|
|
|
} else
|
|
|
|
nr = read(f->fd, buffer, MAXBUFSIZ);
|
|
|
|
|
|
|
|
if (nr < 0)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
bufrem = nr;
|
|
|
|
return (0);
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
}
|
|
|
|
|
2010-08-15 22:15:04 +00:00
|
|
|
static inline int
|
2010-08-18 17:40:10 +00:00
|
|
|
grep_lnbufgrow(size_t newlen)
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
{
|
|
|
|
|
2010-08-18 17:40:10 +00:00
|
|
|
if (lnbuflen < newlen) {
|
|
|
|
lnbuf = grep_realloc(lnbuf, newlen);
|
|
|
|
lnbuflen = newlen;
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
}
|
2010-08-18 17:40:10 +00:00
|
|
|
|
|
|
|
return (0);
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2010-08-18 17:40:10 +00:00
|
|
|
grep_fgetln(struct file *f, size_t *lenp)
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
{
|
2010-08-18 17:40:10 +00:00
|
|
|
unsigned char *p;
|
|
|
|
char *ret;
|
|
|
|
size_t len;
|
|
|
|
size_t off;
|
|
|
|
ptrdiff_t diff;
|
|
|
|
|
|
|
|
/* Fill the buffer, if necessary */
|
|
|
|
if (bufrem == 0 && grep_refill(f) != 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (bufrem == 0) {
|
|
|
|
/* Return zero length to indicate EOF */
|
|
|
|
*lenp = 0;
|
|
|
|
return (bufpos);
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
}
|
|
|
|
|
2010-08-18 17:40:10 +00:00
|
|
|
/* Look for a newline in the remaining part of the buffer */
|
2017-04-17 13:14:18 +00:00
|
|
|
if ((p = memchr(bufpos, fileeol, bufrem)) != NULL) {
|
2010-08-18 17:40:10 +00:00
|
|
|
++p; /* advance over newline */
|
|
|
|
ret = bufpos;
|
|
|
|
len = p - bufpos;
|
|
|
|
bufrem -= len;
|
|
|
|
bufpos = p;
|
|
|
|
*lenp = len;
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have to copy the current buffered data to the line buffer */
|
|
|
|
for (len = bufrem, off = 0; ; len += bufrem) {
|
|
|
|
/* Make sure there is room for more data */
|
|
|
|
if (grep_lnbufgrow(len + LNBUFBUMP))
|
|
|
|
goto error;
|
|
|
|
memcpy(lnbuf + off, bufpos, len - off);
|
2017-05-20 00:42:47 +00:00
|
|
|
/* With FILE_MMAP, this is EOF; there's no more to refill */
|
|
|
|
if (filebehave == FILE_MMAP) {
|
|
|
|
bufrem -= len;
|
|
|
|
break;
|
|
|
|
}
|
2010-08-18 17:40:10 +00:00
|
|
|
off = len;
|
2017-05-20 00:42:47 +00:00
|
|
|
/* Fetch more to try and find EOL/EOF */
|
2010-08-18 17:40:10 +00:00
|
|
|
if (grep_refill(f) != 0)
|
|
|
|
goto error;
|
|
|
|
if (bufrem == 0)
|
|
|
|
/* EOF: return partial line */
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
break;
|
2017-05-20 00:42:47 +00:00
|
|
|
if ((p = memchr(bufpos, fileeol, bufrem)) == NULL)
|
2010-08-18 17:40:10 +00:00
|
|
|
continue;
|
2017-05-20 00:42:47 +00:00
|
|
|
/* got it: finish up the line (like code above) */
|
|
|
|
++p;
|
|
|
|
diff = p - bufpos;
|
|
|
|
len += diff;
|
2010-08-18 17:40:10 +00:00
|
|
|
if (grep_lnbufgrow(len))
|
|
|
|
goto error;
|
|
|
|
memcpy(lnbuf + off, bufpos, diff);
|
|
|
|
bufrem -= diff;
|
|
|
|
bufpos = p;
|
|
|
|
break;
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
}
|
2010-08-18 17:40:10 +00:00
|
|
|
*lenp = len;
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
return (lnbuf);
|
2010-08-18 17:40:10 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
*lenp = 0;
|
|
|
|
return (NULL);
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
}
|
|
|
|
|
2011-10-05 09:56:43 +00:00
|
|
|
/*
|
|
|
|
* Opens a file for processing.
|
|
|
|
*/
|
|
|
|
struct file *
|
|
|
|
grep_open(const char *path)
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
{
|
2011-10-05 09:56:43 +00:00
|
|
|
struct file *f;
|
|
|
|
|
|
|
|
f = grep_malloc(sizeof *f);
|
|
|
|
memset(f, 0, sizeof *f);
|
|
|
|
if (path == NULL) {
|
|
|
|
/* Processing stdin implies --line-buffered. */
|
|
|
|
lbflag = true;
|
|
|
|
f->fd = STDIN_FILENO;
|
|
|
|
} else if ((f->fd = open(path, O_RDONLY)) == -1)
|
|
|
|
goto error1;
|
|
|
|
|
|
|
|
if (filebehave == FILE_MMAP) {
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if ((fstat(f->fd, &st) == -1) || (st.st_size > OFF_MAX) ||
|
|
|
|
(!S_ISREG(st.st_mode)))
|
|
|
|
filebehave = FILE_STDIO;
|
|
|
|
else {
|
|
|
|
int flags = MAP_PRIVATE | MAP_NOCORE | MAP_NOSYNC;
|
|
|
|
#ifdef MAP_PREFAULT_READ
|
|
|
|
flags |= MAP_PREFAULT_READ;
|
|
|
|
#endif
|
|
|
|
fsiz = st.st_size;
|
|
|
|
buffer = mmap(NULL, fsiz, PROT_READ, flags,
|
|
|
|
f->fd, (off_t)0);
|
|
|
|
if (buffer == MAP_FAILED)
|
|
|
|
filebehave = FILE_STDIO;
|
|
|
|
else {
|
|
|
|
bufrem = st.st_size;
|
|
|
|
bufpos = buffer;
|
|
|
|
madvise(buffer, st.st_size, MADV_SEQUENTIAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((buffer == NULL) || (buffer == MAP_FAILED))
|
|
|
|
buffer = grep_malloc(MAXBUFSIZ);
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
|
2010-08-18 17:40:10 +00:00
|
|
|
if (filebehave == FILE_GZIP &&
|
|
|
|
(gzbufdesc = gzdopen(f->fd, "r")) == NULL)
|
2011-10-05 09:56:43 +00:00
|
|
|
goto error2;
|
2010-08-15 22:15:04 +00:00
|
|
|
|
2011-10-11 22:27:23 +00:00
|
|
|
#ifndef WITHOUT_BZIP2
|
2010-08-18 17:40:10 +00:00
|
|
|
if (filebehave == FILE_BZIP &&
|
|
|
|
(bzbufdesc = BZ2_bzdopen(f->fd, "r")) == NULL)
|
2011-10-05 09:56:43 +00:00
|
|
|
goto error2;
|
2011-10-11 22:27:23 +00:00
|
|
|
#endif
|
2015-01-21 01:11:37 +00:00
|
|
|
#ifndef WITHOUT_LZMA
|
|
|
|
else if ((filebehave == FILE_XZ) || (filebehave == FILE_LZMA)) {
|
|
|
|
lzma_ret ret;
|
|
|
|
|
|
|
|
ret = (filebehave == FILE_XZ) ?
|
|
|
|
lzma_stream_decoder(&lstrm, UINT64_MAX,
|
|
|
|
LZMA_CONCATENATED) :
|
|
|
|
lzma_alone_decoder(&lstrm, UINT64_MAX);
|
|
|
|
|
|
|
|
if (ret != LZMA_OK)
|
|
|
|
goto error2;
|
|
|
|
|
|
|
|
lstrm.avail_in = 0;
|
|
|
|
lstrm.avail_out = MAXBUFSIZ;
|
|
|
|
laction = LZMA_RUN;
|
|
|
|
}
|
|
|
|
#endif
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
|
2010-08-18 17:40:10 +00:00
|
|
|
/* Fill read buffer, also catches errors early */
|
2011-10-05 09:56:43 +00:00
|
|
|
if (bufrem == 0 && grep_refill(f) != 0)
|
|
|
|
goto error2;
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
|
2010-08-18 17:40:10 +00:00
|
|
|
/* Check for binary stuff, if necessary */
|
2017-04-17 13:14:18 +00:00
|
|
|
if (binbehave != BINFILE_TEXT && fileeol != '\0' &&
|
|
|
|
memchr(bufpos, '\0', bufrem) != NULL)
|
2011-10-05 09:56:43 +00:00
|
|
|
f->binary = true;
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
|
2010-08-18 17:40:10 +00:00
|
|
|
return (f);
|
2011-10-05 09:56:43 +00:00
|
|
|
|
|
|
|
error2:
|
2010-08-18 17:40:10 +00:00
|
|
|
close(f->fd);
|
2011-10-05 09:56:43 +00:00
|
|
|
error1:
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
free(f);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-08-18 17:40:10 +00:00
|
|
|
* Closes a file.
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
grep_close(struct file *f)
|
|
|
|
{
|
|
|
|
|
2010-08-18 17:40:10 +00:00
|
|
|
close(f->fd);
|
|
|
|
|
|
|
|
/* Reset read buffer and line buffer */
|
2011-10-05 09:56:43 +00:00
|
|
|
if (filebehave == FILE_MMAP) {
|
|
|
|
munmap(buffer, fsiz);
|
|
|
|
buffer = NULL;
|
|
|
|
}
|
2010-08-18 17:40:10 +00:00
|
|
|
bufpos = buffer;
|
|
|
|
bufrem = 0;
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
|
2010-08-18 17:40:10 +00:00
|
|
|
free(lnbuf);
|
|
|
|
lnbuf = NULL;
|
|
|
|
lnbuflen = 0;
|
Add BSD grep to the base system and make it our default grep.
Deliverables: Small and clean code (1,4 KSLOC vs GNU's 8,5 KSLOC),
lower memory usage than GNU grep, GNU compatibility,
BSD license.
TODO: Performance is somewhat behind GNU grep but it is only
significant for bigger searches. The reason is complex, the
most important factor is that GNU grep uses lots of
optimizations to improve the speed of the regex library.
First, we need a modern regex library (practically by adopting
TRE), add support for GNU-style non-standard regexes and then
reevalute the performance issues and look for bottlenecks. In
the meantime, for those, who need better performance, it is
possible to build GNU grep by setting WITH_GNU_GREP.
Approved by: delphij (mentor)
Obtained from: OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/grep/),
freegrep (http://github.com/howardjp/freegrep)
Sponsored by: Google SoC 2008
Portbuild tests run by: kris, pav, erwin
Acknowledgements to: fjoe (as SoC 2008 mentor),
everyone who helped in reviewing and testing
2010-07-22 19:11:57 +00:00
|
|
|
}
|