Remove compression support from bsdgrep

Compression support is now handled by an external script, remove it from the
bsdgrep(1) utility.
This removes the support for -Z -J -X and -M

Note: that it matches the changes in newer GNU grep

Reviewed by:	kevans
Approved by:	kevans
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D15197
This commit is contained in:
Baptiste Daroussin 2018-04-25 14:40:15 +00:00
parent 19c5cea336
commit 4a5b420738
5 changed files with 15 additions and 225 deletions

View File

@ -69,20 +69,6 @@ MLINKS+= grep.1 egrep.1 \
grep.1 rgrep.1
.endif
LIBADD= z
.if ${MK_LZMA_SUPPORT} != "no"
LIBADD+= lzma
.else
CFLAGS+= -DWITHOUT_LZMA
.endif
.if ${MK_BZIP2_SUPPORT} != "no"
LIBADD+= bz2
.else
CFLAGS+= -DWITHOUT_BZIP2
.endif
.if ${MK_GNU_GREP_COMPAT} != "no"
CFLAGS+= -I${SYSROOT:U${DESTDIR}}/usr/include/gnu -DWITH_GNU
LIBADD+= gnuregex

View File

@ -49,31 +49,12 @@ __FBSDID("$FreeBSD$");
#include <unistd.h>
#include <wchar.h>
#include <wctype.h>
#include <zlib.h>
#ifndef WITHOUT_LZMA
#include <lzma.h>
#endif
#ifndef WITHOUT_BZIP2
#include <bzlib.h>
#endif
#include "grep.h"
#define MAXBUFSIZ (32 * 1024)
#define LNBUFBUMP 80
static gzFile gzbufdesc;
#ifndef WITHOUT_LZMA
static lzma_stream lstrm = LZMA_STREAM_INIT;
static lzma_action laction;
static uint8_t lin_buf[MAXBUFSIZ];
#endif
#ifndef WITHOUT_BZIP2
static BZFILE* bzbufdesc;
#endif
static unsigned char *buffer;
static unsigned char *bufpos;
static size_t bufrem;
@ -86,9 +67,6 @@ static inline int
grep_refill(struct file *f)
{
ssize_t nr;
#ifndef WITHOUT_LZMA
lzma_ret lzmaret;
#endif
if (filebehave == FILE_MMAP)
return (0);
@ -96,84 +74,7 @@ grep_refill(struct file *f)
bufpos = buffer;
bufrem = 0;
switch (filebehave) {
case FILE_GZIP:
nr = gzread(gzbufdesc, buffer, MAXBUFSIZ);
break;
#ifndef WITHOUT_BZIP2
case FILE_BZIP:
if (bzbufdesc != NULL) {
int bzerr;
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;
}
} else
/*
* Also an error case; we should never have a scenario
* where we have an open file but no bzip descriptor
* at this point. See: grep_open
*/
nr = -1;
break;
#endif
#ifndef WITHOUT_LZMA
case FILE_XZ:
case FILE_LZMA:
lstrm.next_out = buffer;
do {
if (lstrm.avail_in == 0) {
lstrm.next_in = lin_buf;
nr = read(f->fd, lin_buf, MAXBUFSIZ);
if (nr < 0)
return (-1);
else if (nr == 0)
laction = LZMA_FINISH;
lstrm.avail_in = nr;
}
lzmaret = lzma_code(&lstrm, laction);
if (lzmaret != LZMA_OK && lzmaret != LZMA_STREAM_END)
return (-1);
if (lstrm.avail_out == 0 || lzmaret == LZMA_STREAM_END) {
bufrem = MAXBUFSIZ - lstrm.avail_out;
lstrm.next_out = buffer;
lstrm.avail_out = MAXBUFSIZ;
}
} while (bufrem == 0 && lzmaret != LZMA_STREAM_END);
return (0);
#endif /* WITHOUT_LZMA */
default:
nr = read(f->fd, buffer, MAXBUFSIZ);
}
nr = read(f->fd, buffer, MAXBUFSIZ);
if (nr < 0)
return (-1);
@ -269,9 +170,6 @@ struct file *
grep_open(const char *path)
{
struct file *f;
#ifndef WITHOUT_LZMA
lzma_ret lzmaret;
#endif
f = grep_malloc(sizeof *f);
memset(f, 0, sizeof *f);
@ -309,37 +207,6 @@ grep_open(const char *path)
if ((buffer == NULL) || (buffer == MAP_FAILED))
buffer = grep_malloc(MAXBUFSIZ);
switch (filebehave) {
case FILE_GZIP:
if ((gzbufdesc = gzdopen(f->fd, "r")) == NULL)
goto error2;
break;
#ifndef WITHOUT_BZIP2
case FILE_BZIP:
if ((bzbufdesc = BZ2_bzdopen(f->fd, "r")) == NULL)
goto error2;
break;
#endif
#ifndef WITHOUT_LZMA
case FILE_XZ:
case FILE_LZMA:
if (filebehave == FILE_XZ)
lzmaret = lzma_stream_decoder(&lstrm, UINT64_MAX,
LZMA_CONCATENATED);
else
lzmaret = lzma_alone_decoder(&lstrm, UINT64_MAX);
if (lzmaret != LZMA_OK)
goto error2;
lstrm.avail_in = 0;
lstrm.avail_out = MAXBUFSIZ;
laction = LZMA_RUN;
break;
#endif
}
/* Fill read buffer, also catches errors early */
if (bufrem == 0 && grep_refill(f) != 0)
goto error2;

View File

@ -30,17 +30,16 @@
.\"
.\" @(#)grep.1 8.3 (Berkeley) 4/18/94
.\"
.Dd April 17, 2017
.Dd April 25, 2018
.Dt GREP 1
.Os
.Sh NAME
.Nm grep , egrep , fgrep , rgrep ,
.Nm zgrep , zegrep , zfgrep
.Nd file pattern searcher
.Sh SYNOPSIS
.Nm grep
.Bk -words
.Op Fl abcdDEFGHhIiJLlmnOopqRSsUVvwxZz
.Op Fl abcdDEFGHhIiLlmnOopqRSsUVvwxz
.Op Fl A Ar num
.Op Fl B Ar num
.Op Fl C Ns Op Ar num
@ -88,21 +87,6 @@ but can only handle fixed patterns
Patterns may consist of one or more lines,
allowing any of the pattern lines to match a portion of the input.
.Pp
.Nm zgrep ,
.Nm zegrep ,
and
.Nm zfgrep
act like
.Nm grep ,
.Nm egrep ,
and
.Nm fgrep ,
respectively, but accept input files compressed with the
.Xr compress 1
or
.Xr gzip 1
compression utilities.
.Pp
The following options are available:
.Bl -tag -width indent
.It Fl A Ar num , Fl Fl after-context Ns = Ns Ar num
@ -274,10 +258,6 @@ Note that
patterns take priority over
.Fl Fl include-dir
patterns.
.It Fl J, Fl Fl bz2decompress
Decompress the
.Xr bzip2 1
compressed file before looking for the text.
.It Fl L , Fl Fl files-without-match
Only the names of files not containing selected lines are written to
standard output.
@ -381,11 +361,6 @@ Obsoleted.
.It Fl z , Fl Fl null-data
Treat input and output data as sequences of lines terminated by a
zero-byte instead of a newline.
.It Fl Z , Fl Fl decompress
Force
.Nm grep
to behave as
.Nm zgrep .
.It Fl Fl binary-files Ns = Ns Ar value
Controls searching and printing of binary files.
Options are
@ -465,7 +440,6 @@ looking for either 19, 20, or 25.
.Sh SEE ALSO
.Xr ed 1 ,
.Xr ex 1 ,
.Xr gzip 1 ,
.Xr sed 1 ,
.Xr re_format 7
.Sh STANDARDS

View File

@ -68,9 +68,8 @@ nl_catd catalog;
const char *errstr[] = {
"",
/* 1*/ "(standard input)",
/* 2*/ "cannot read bzip2 compressed file",
/* 3*/ "unknown %s option",
/* 4*/ "usage: %s [-abcDEFGHhIiJLlmnOoPqRSsUVvwxZz] [-A num] [-B num] [-C[num]]\n",
/* 4*/ "usage: %s [-abcDEFGHhIiLlmnOoPqRSsUVvwxz] [-A num] [-B num] [-C[num]]\n",
/* 5*/ "\t[-e pattern] [-f file] [--binary-files=value] [--color=when]\n",
/* 6*/ "\t[--context[=num]] [--directories=action] [--label] [--line-buffered]\n",
/* 7*/ "\t[--null] [pattern] [file ...]\n",
@ -136,7 +135,7 @@ char *label; /* --label */
const char *color; /* --color */
int grepbehave = GREP_BASIC; /* -EFGP: type of the regex */
int binbehave = BINFILE_BIN; /* -aIU: handling of binary files */
int filebehave = FILE_STDIO; /* -JZ: normal, gzip or bzip2 file */
int filebehave = FILE_STDIO;
int devbehave = DEV_READ; /* -D: handling of devices */
int dirbehave = DIR_READ; /* -dRr: handling of directories */
int linkbehave = LINK_READ; /* -OpS: handling of symlinks */
@ -169,14 +168,14 @@ bool file_err; /* file reading error */
static void
usage(void)
{
fprintf(stderr, getstr(4), getprogname());
fprintf(stderr, getstr(3), getprogname());
fprintf(stderr, "%s", getstr(4));
fprintf(stderr, "%s", getstr(5));
fprintf(stderr, "%s", getstr(6));
fprintf(stderr, "%s", getstr(7));
exit(2);
}
static const char *optstr = "0123456789A:B:C:D:EFGHIJMLOPSRUVZabcd:e:f:hilm:nopqrsuvwxXyz";
static const char *optstr = "0123456789A:B:C:D:EFGHILOPSRUVabcd:e:f:hilm:nopqrsuvwxyz";
static const struct option long_options[] =
{
@ -208,11 +207,9 @@ static const struct option long_options[] =
{"no-filename", no_argument, NULL, 'h'},
{"with-filename", no_argument, NULL, 'H'},
{"ignore-case", no_argument, NULL, 'i'},
{"bz2decompress", no_argument, NULL, 'J'},
{"files-with-matches", no_argument, NULL, 'l'},
{"files-without-match", no_argument, NULL, 'L'},
{"max-count", required_argument, NULL, 'm'},
{"lzma", no_argument, NULL, 'M'},
{"line-number", no_argument, NULL, 'n'},
{"only-matching", no_argument, NULL, 'o'},
{"quiet", no_argument, NULL, 'q'},
@ -225,9 +222,7 @@ static const struct option long_options[] =
{"version", no_argument, NULL, 'V'},
{"word-regexp", no_argument, NULL, 'w'},
{"line-regexp", no_argument, NULL, 'x'},
{"xz", no_argument, NULL, 'X'},
{"null-data", no_argument, NULL, 'z'},
{"decompress", no_argument, NULL, 'Z'},
{NULL, no_argument, NULL, 0}
};
@ -367,21 +362,9 @@ main(int argc, char *argv[])
way we can have all the funcionalities in one binary
without the need of scripting and using ugly hacks. */
pn = getprogname();
if (pn[0] == 'b' && pn[1] == 'z') {
filebehave = FILE_BZIP;
pn += 2;
} else if (pn[0] == 'x' && pn[1] == 'z') {
filebehave = FILE_XZ;
pn += 2;
} else if (pn[0] == 'l' && pn[1] == 'z') {
filebehave = FILE_LZMA;
pn += 2;
} else if (pn[0] == 'r') {
if (pn[0] == 'r') {
dirbehave = DIR_RECURSE;
Hflag = true;
} else if (pn[0] == 'z') {
filebehave = FILE_GZIP;
pn += 1;
}
switch (pn[0]) {
case 'e':
@ -490,7 +473,7 @@ main(int argc, char *argv[])
else if (strcasecmp(optarg, "read") == 0)
devbehave = DEV_READ;
else
errx(2, getstr(3), "--devices");
errx(2, getstr(2), "--devices");
break;
case 'd':
if (strcasecmp("recurse", optarg) == 0) {
@ -501,7 +484,7 @@ main(int argc, char *argv[])
else if (strcasecmp("read", optarg) == 0)
dirbehave = DIR_READ;
else
errx(2, getstr(3), "--directories");
errx(2, getstr(2), "--directories");
break;
case 'E':
grepbehave = GREP_EXTENDED;
@ -541,13 +524,6 @@ main(int argc, char *argv[])
iflag = true;
cflags |= REG_ICASE;
break;
case 'J':
#ifdef WITHOUT_BZIP2
errno = EOPNOTSUPP;
err(2, "bzip2 support was disabled at compile-time");
#endif
filebehave = FILE_BZIP;
break;
case 'L':
lflag = false;
Lflag = true;
@ -568,9 +544,6 @@ main(int argc, char *argv[])
err(2, NULL);
}
break;
case 'M':
filebehave = FILE_LZMA;
break;
case 'n':
nflag = true;
break;
@ -607,9 +580,9 @@ main(int argc, char *argv[])
break;
case 'V':
#ifdef WITH_GNU
printf(getstr(10), getprogname(), VERSION);
#else
printf(getstr(9), getprogname(), VERSION);
#else
printf(getstr(8), getprogname(), VERSION);
#endif
exit(0);
case 'v':
@ -623,15 +596,9 @@ main(int argc, char *argv[])
xflag = true;
cflags &= ~REG_NOSUB;
break;
case 'X':
filebehave = FILE_XZ;
break;
case 'z':
fileeol = '\0';
break;
case 'Z':
filebehave = FILE_GZIP;
break;
case BIN_OPT:
if (strcasecmp("binary", optarg) == 0)
binbehave = BINFILE_BIN;
@ -640,7 +607,7 @@ main(int argc, char *argv[])
else if (strcasecmp("text", optarg) == 0)
binbehave = BINFILE_TEXT;
else
errx(2, getstr(3), "--binary-files");
errx(2, getstr(2), "--binary-files");
break;
case COLOR_OPT:
color = NULL;
@ -660,7 +627,7 @@ main(int argc, char *argv[])
} else if (strcasecmp("never", optarg) != 0 &&
strcasecmp("none", optarg) != 0 &&
strcasecmp("no", optarg) != 0)
errx(2, getstr(3), "--color");
errx(2, getstr(2), "--color");
cflags &= ~REG_NOSUB;
break;
case LABEL_OPT:

View File

@ -69,10 +69,6 @@ extern const char *errstr[];
#define FILE_STDIO 0
#define FILE_MMAP 1
#define FILE_GZIP 2
#define FILE_BZIP 3
#define FILE_XZ 4
#define FILE_LZMA 5
#define DIR_READ 0
#define DIR_SKIP 1