svn commit: r225251 - user/gabor/grep/trunk

Gabor Kovesdan gabor at FreeBSD.org
Mon Aug 29 20:36:22 UTC 2011


Author: gabor
Date: Mon Aug 29 20:36:21 2011
New Revision: 225251
URL: http://svn.freebsd.org/changeset/base/225251

Log:
  - Add support for xz[ef]?grep (1) and lz[ef]?grep.  The former handles xz(1)
    compressed files and the latter lzma(1) compressed files.  The -X and -M
    command-line options can also be used respectively.
  
  Suggested by:	naddy (1)

Modified:
  user/gabor/grep/trunk/Makefile
  user/gabor/grep/trunk/file.c
  user/gabor/grep/trunk/grep.1
  user/gabor/grep/trunk/grep.c
  user/gabor/grep/trunk/grep.h

Modified: user/gabor/grep/trunk/Makefile
==============================================================================
--- user/gabor/grep/trunk/Makefile	Mon Aug 29 20:00:57 2011	(r225250)
+++ user/gabor/grep/trunk/Makefile	Mon Aug 29 20:36:21 2011	(r225251)
@@ -28,7 +28,13 @@ LINKS=	${BINDIR}/grep ${BINDIR}/egrep \
 	${BINDIR}/grep ${BINDIR}/zfgrep \
 	${BINDIR}/grep ${BINDIR}/bzgrep \
 	${BINDIR}/grep ${BINDIR}/bzegrep \
-	${BINDIR}/grep ${BINDIR}/bzfgrep
+	${BINDIR}/grep ${BINDIR}/bzfgrep \
+	${BINDIR}/grep ${BINDIR}/xzgrep \
+	${BINDIR}/grep ${BINDIR}/xzefgrep \
+	${BINDIR}/grep ${BINDIR}/xzfgrep \
+	${BINDIR}/grep ${BINDIR}/lzgrep \
+	${BINDIR}/grep ${BINDIR}/lzegrep \
+	${BINDIR}/grep ${BINDIR}/lzfgrep
 
 MLINKS= grep.1 egrep.1 \
 	grep.1 fgrep.1 \
@@ -37,11 +43,17 @@ MLINKS= grep.1 egrep.1 \
 	grep.1 zfgrep.1 \
 	grep.1 bzgrep.1 \
 	grep.1 bzegrep.1 \
-	grep.1 bzfgrep.1
+	grep.1 bzfgrep.1 \
+	grep.1 xzgrep.1 \
+	grep.1 xzegrep.1 \
+	grep.1 xzfgrep.1 \
+	grep.1 lzgrep.1 \
+	grep.1 lzegrep.1 \
+	grep.1 lzfgrep.1
 .endif
 
-LDADD=	-lz -lbz2
-DPADD=	${LIBZ} ${LIBBZ2}
+LDADD=	-lz -lbz2 -llzma
+DPADD=	${LIBZ} ${LIBBZ2} ${LIBLZMA}
 
 .if !defined(WITHOUT_GNU_COMPAT)
 CFLAGS+= -I/usr/include/gnu

Modified: user/gabor/grep/trunk/file.c
==============================================================================
--- user/gabor/grep/trunk/file.c	Mon Aug 29 20:00:57 2011	(r225250)
+++ user/gabor/grep/trunk/file.c	Mon Aug 29 20:36:21 2011	(r225251)
@@ -41,6 +41,7 @@ __FBSDID("$FreeBSD$");
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <lzma.h>
 #include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
@@ -56,6 +57,7 @@ __FBSDID("$FreeBSD$");
 
 static gzFile gzbufdesc;
 static BZFILE* bzbufdesc;
+static lzma_stream lstrm = LZMA_STREAM_INIT;
 
 static unsigned char buffer[MAXBUFSIZ];
 static unsigned char *bufpos;
@@ -101,6 +103,35 @@ grep_refill(struct file *f)
 			/* Make sure we exit with an error */
 			nr = -1;
 		}
+	} else if ((filebehave == FILE_XZ) || (filebehave == FILE_LZMA)) {
+		lzma_action action = LZMA_RUN;
+		uint8_t in_buf[MAXBUFSIZ];
+		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)
+			return (-1);
+
+		lstrm.next_out = buffer;
+		lstrm.avail_out = MAXBUFSIZ;
+		lstrm.next_in = in_buf;
+		lstrm.avail_in = read(f->fd, in_buf, MAXBUFSIZ);
+
+		if (lstrm.avail_in < 0)
+			return (-1);
+		else if (lstrm.avail_in == 0)
+			action = LZMA_FINISH;
+
+		ret = lzma_code(&lstrm, action);
+
+		if (ret != LZMA_OK && ret != LZMA_STREAM_END)
+			return (-1);
+		bufrem = MAXBUFSIZ - lstrm.avail_out;
+		return (0);
 	} else
 		nr = read(f->fd, buffer, MAXBUFSIZ);
 

Modified: user/gabor/grep/trunk/grep.1
==============================================================================
--- user/gabor/grep/trunk/grep.1	Mon Aug 29 20:00:57 2011	(r225250)
+++ user/gabor/grep/trunk/grep.1	Mon Aug 29 20:36:21 2011	(r225251)
@@ -30,13 +30,15 @@
 .\"
 .\"	@(#)grep.1	8.3 (Berkeley) 4/18/94
 .\"
-.Dd July 28, 2010
+.Dd August 29, 2011
 .Dt GREP 1
 .Os
 .Sh NAME
 .Nm grep , egrep , fgrep ,
 .Nm zgrep , zegrep , zfgrep ,
 .Nm bzgrep , bzegrep , bzfgrep
+.Nm xzgrep , xzegrep , xzfgrep
+.Nm lzgrep , lzegrep , lzfgrep
 .Nd file pattern searcher
 .Sh SYNOPSIS
 .Nm grep
@@ -117,6 +119,32 @@ respectively, but accept input files com
 .Xr bzip2 1
 compression utility.
 .Pp
+.Nm xzgrep ,
+.Nm xzegrep ,
+and
+.Nm xzfgrep
+act like
+.Nm grep ,
+.Nm egrep ,
+and
+.Nm fgrep ,
+respectively, but accept input files compressed with the
+.Xr xz 1
+compression utility.
+.Pp
+.Nm lzgrep ,
+.Nm lzegrep ,
+and
+.Nm lzfgrep
+act like
+.Nm grep ,
+.Nm egrep ,
+and
+.Nm fgrep ,
+respectively, but accept input files compressed with the
+.Xr lzma 1
+compression utility.
+.Pp
 The following options are available:
 .Bl -tag -width indent
 .It Fl A Ar num , Fl Fl after-context Ns = Ns Ar num

Modified: user/gabor/grep/trunk/grep.c
==============================================================================
--- user/gabor/grep/trunk/grep.c	Mon Aug 29 20:00:57 2011	(r225250)
+++ user/gabor/grep/trunk/grep.c	Mon Aug 29 20:36:21 2011	(r225251)
@@ -165,7 +165,7 @@ usage(void)
 	exit(2);
 }
 
-static const char	*optstr = "0123456789A:B:C:D:EFGHIJLOPSRUVZabcd:e:f:hilm:nopqrsuvwxy";
+static const char	*optstr = "0123456789A:B:C:D:EFGHIJMLOPSRUVZabcd:e:f:hilm:nopqrsuvwxXy";
 
 struct option long_options[] =
 {
@@ -201,6 +201,7 @@ struct option long_options[] =
 	{"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'},
@@ -213,6 +214,7 @@ 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'},
 	{"decompress",          no_argument,            NULL, 'Z'},
 	{NULL,			no_argument,		NULL, 0}
 };
@@ -331,6 +333,12 @@ main(int argc, char *argv[])
 	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] == 'z') {
 		filebehave = FILE_GZIP;
 		pn += 1;
@@ -505,6 +513,9 @@ main(int argc, char *argv[])
 				err(2, NULL);
 			}
 			break;
+		case 'M':
+			filebehave = FILE_LZMA;
+			break;
 		case 'n':
 			nflag = true;
 			break;
@@ -553,6 +564,9 @@ main(int argc, char *argv[])
 			xflag = true;
 			cflags &= ~REG_NOSUB;
 			break;
+		case 'X':
+			filebehave = FILE_XZ;
+			break;
 		case 'Z':
 			filebehave = FILE_GZIP;
 			break;

Modified: user/gabor/grep/trunk/grep.h
==============================================================================
--- user/gabor/grep/trunk/grep.h	Mon Aug 29 20:00:57 2011	(r225250)
+++ user/gabor/grep/trunk/grep.h	Mon Aug 29 20:36:21 2011	(r225251)
@@ -62,6 +62,8 @@ extern const char		*errstr[];
 #define FILE_STDIO	0
 #define FILE_GZIP	1
 #define FILE_BZIP	2
+#define FILE_XZ		3
+#define FILE_LZMA	4
 
 #define DIR_READ	0
 #define DIR_SKIP	1


More information about the svn-src-user mailing list