From 612c330d85a4e48869c4f0307efe3f133969aa6c Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Wed, 25 Apr 2018 13:23:58 +0000 Subject: [PATCH] Use a script wrapper for grep Import the wrapper script from zstdgrep (written by wiz@netbsd.org) Modify it to support more than just zstd (adding support for gzip, lzma, xz and bzip2) Write a simple manpage dedicated for it. Only use that new wrapper both for gnu grep and bsd grep Next step will be removing code related to compression format from bsdgrep Reviewed by: kevans Approved by: kevans MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D15193 --- gnu/usr.bin/grep/Makefile | 13 -- tools/build/mk/OptionalObsoleteFiles.inc | 15 --- usr.bin/grep/Makefile | 70 ++++++----- usr.bin/grep/zgrep.1 | 90 ++++++++++++++ usr.bin/grep/zgrep.sh | 151 +++++++++++++++++++++++ usr.bin/zstd/Makefile | 1 - 6 files changed, 277 insertions(+), 63 deletions(-) create mode 100644 usr.bin/grep/zgrep.1 create mode 100755 usr.bin/grep/zgrep.sh diff --git a/gnu/usr.bin/grep/Makefile b/gnu/usr.bin/grep/Makefile index e8d6dd8ba4b5..b24b1ea292e0 100644 --- a/gnu/usr.bin/grep/Makefile +++ b/gnu/usr.bin/grep/Makefile @@ -24,22 +24,9 @@ MLINKS= grep.1 egrep.1 grep.1 fgrep.1 LIBADD= gnuregex bz2 -.if ${MK_BSD_GREP} != "yes" -LINKS+= ${BINDIR}/grep ${BINDIR}/bzgrep \ - ${BINDIR}/grep ${BINDIR}/bzegrep \ - ${BINDIR}/grep ${BINDIR}/bzfgrep -MLINKS+=grep.1 bzgrep.1 grep.1 bzegrep.1 grep.1 bzfgrep.1 -.endif - .if defined(GREP_LIBZ) && !empty(GREP_LIBZ) LIBADD+= z CFLAGS+=-DHAVE_LIBZ=1 -.if ${MK_BSD_GREP} != "yes" -LINKS+= ${BINDIR}/grep ${BINDIR}/zgrep \ - ${BINDIR}/grep ${BINDIR}/zegrep \ - ${BINDIR}/grep ${BINDIR}/zfgrep -MLINKS+=grep.1 zgrep.1 grep.1 zegrep.1 grep.1 zfgrep.1 -.endif .endif gnugrep.1: grep.1 diff --git a/tools/build/mk/OptionalObsoleteFiles.inc b/tools/build/mk/OptionalObsoleteFiles.inc index e1ef3d30a0cb..ff7be4e94692 100644 --- a/tools/build/mk/OptionalObsoleteFiles.inc +++ b/tools/build/mk/OptionalObsoleteFiles.inc @@ -657,21 +657,6 @@ OLD_FILES+=usr/share/man/man1/bsdcpio.1.gz OLD_FILES+=usr/share/man/man1/cpio.1.gz .endif -.if ${MK_BSD_GREP} == no -OLD_FILES+=usr/bin/lzegrep -OLD_FILES+=usr/bin/lzfgrep -OLD_FILES+=usr/bin/lzgrep -OLD_FILES+=usr/bin/xzegrep -OLD_FILES+=usr/bin/xzfgrep -OLD_FILES+=usr/bin/xzgrep -OLD_FILES+=usr/share/man/man1/lzegrep.1.gz -OLD_FILES+=usr/share/man/man1/lzfgrep.1.gz -OLD_FILES+=usr/share/man/man1/lzgrep.1.gz -OLD_FILES+=usr/share/man/man1/xzegrep.1.gz -OLD_FILES+=usr/share/man/man1/xzfgrep.1.gz -OLD_FILES+=usr/share/man/man1/xzgrep.1.gz -.endif - .if ${MK_BSDINSTALL} == no OLD_FILES+=usr/libexec/bsdinstall/adduser OLD_FILES+=usr/libexec/bsdinstall/auto diff --git a/usr.bin/grep/Makefile b/usr.bin/grep/Makefile index 5166303c09eb..752d56decdf8 100644 --- a/usr.bin/grep/Makefile +++ b/usr.bin/grep/Makefile @@ -6,9 +6,11 @@ .if ${MK_BSD_GREP} == "yes" PROG= grep +MAN1= grep.1 zgrep.1 .else PROG= bsdgrep CLEANFILES+= bsdgrep.1 +MAN1= grep.1 zgrep.1 bsdgrep.1: grep.1 ${CP} ${.ALLSRC} ${.TARGET} @@ -24,59 +26,59 @@ CFLAGS+=-I${.CURDIR}/regex CFLAGS+= -DWITHOUT_FASTMATCH .endif +SCRIPTS= zgrep.sh +LINKS= ${BINDIR}/zgrep ${BINDIR}/zfgrep \ + ${BINDIR}/zgrep ${BINDIR}/zegrep \ + ${BINDIR}/zgrep ${BINDIR}/bzgrep \ + ${BINDIR}/zgrep ${BINDIR}/bzegrep \ + ${BINDIR}/zgrep ${BINDIR}/bzfgrep \ + ${BINDIR}/zgrep ${BINDIR}/lzgrep \ + ${BINDIR}/zgrep ${BINDIR}/lzegrep \ + ${BINDIR}/zgrep ${BINDIR}/lzfgrep \ + ${BINDIR}/zgrep ${BINDIR}/xzgrep \ + ${BINDIR}/zgrep ${BINDIR}/xzegrep \ + ${BINDIR}/zgrep ${BINDIR}/xzfgrep \ + ${BINDIR}/zgrep ${BINDIR}/zstdgrep \ + ${BINDIR}/zgrep ${BINDIR}/zstdegrep \ + ${BINDIR}/zgrep ${BINDIR}/zstdegrep + +MLINKS= zgrep.1 zfgrep.1 \ + zgrep.1 zegrep.1 \ + zgrep.1 bzgrep.1 \ + zgrep.1 bzegrep.1 \ + zgrep.1 bzfgrep.1 \ + zgrep.1 lzgrep.1 \ + zgrep.1 lzegrep.1 \ + zgrep.1 lzfgrep.1 \ + zgrep.1 xzgrep.1 \ + zgrep.1 xzegrep.1 \ + zgrep.1 xzfgrep.1 \ + zgrep.1 zstdgrep.1 \ + zgrep.1 zstdegrep.1 \ + zgrep.1 zstdfgrep.1 + CFLAGS.gcc+= --param max-inline-insns-single=500 .if ${MK_BSD_GREP} == "yes" -LINKS= ${BINDIR}/grep ${BINDIR}/egrep \ +LINKS+= ${BINDIR}/grep ${BINDIR}/egrep \ ${BINDIR}/grep ${BINDIR}/fgrep \ ${BINDIR}/grep ${BINDIR}/rgrep \ - ${BINDIR}/grep ${BINDIR}/zgrep \ - ${BINDIR}/grep ${BINDIR}/zegrep \ - ${BINDIR}/grep ${BINDIR}/zfgrep -MLINKS= grep.1 egrep.1 \ +MLINKS+= grep.1 egrep.1 \ grep.1 fgrep.1 \ - grep.1 rgrep.1 \ - grep.1 zgrep.1 \ - grep.1 zegrep.1 \ - grep.1 zfgrep.1 + grep.1 rgrep.1 .endif LIBADD= z .if ${MK_LZMA_SUPPORT} != "no" LIBADD+= lzma - -.if ${MK_BSD_GREP} == "yes" -LINKS+= ${BINDIR}/${PROG} ${BINDIR}/xzgrep \ - ${BINDIR}/${PROG} ${BINDIR}/xzegrep \ - ${BINDIR}/${PROG} ${BINDIR}/xzfgrep \ - ${BINDIR}/${PROG} ${BINDIR}/lzgrep \ - ${BINDIR}/${PROG} ${BINDIR}/lzegrep \ - ${BINDIR}/${PROG} ${BINDIR}/lzfgrep - -MLINKS+= 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 .else CFLAGS+= -DWITHOUT_LZMA .endif .if ${MK_BZIP2_SUPPORT} != "no" LIBADD+= bz2 - -.if ${MK_BSD_GREP} == "yes" -LINKS+= ${BINDIR}/grep ${BINDIR}/bzgrep \ - ${BINDIR}/grep ${BINDIR}/bzegrep \ - ${BINDIR}/grep ${BINDIR}/bzfgrep -MLINKS+= grep.1 bzgrep.1 \ - grep.1 bzegrep.1 \ - grep.1 bzfgrep.1 -.endif .else CFLAGS+= -DWITHOUT_BZIP2 .endif diff --git a/usr.bin/grep/zgrep.1 b/usr.bin/grep/zgrep.1 new file mode 100644 index 000000000000..05448093bf44 --- /dev/null +++ b/usr.bin/grep/zgrep.1 @@ -0,0 +1,90 @@ +.\" Copyright (c) 2018 Baptiste Daroussin +.\" 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. +.\" +.\" $FreeBSD$ +.\" +.Dd April 25, 2018 +.Dt ZGREP 1 +.Os +.Sh NAME +.Nm zgrep , +.Nm zegrep , +.Nm zfgrep , +.Nm bzgrep , +.Nm bzegrep , +.Nm bzfgrep , +.Nm lzgrep , +.Nm lzegrep , +.Nm lzfgrep , +.Nm xzgrep , +.Nm xzegrep , +.Nm xzfgrep , +.Nm zstdgrep , +.Nm zstdegrep , +.Nm zstdfgrep +.Nd +.Xr grep 1 +compressed files +.Sh SYNOPSIS +.Nm +.Oo Ar flags Oc Ar files +.Nm zegrep +.Oo Ar flags Oc Ar files +.Nm zfgrep +.Oo Ar flags Oc Ar files +.Nm bzgrep +.Oo Ar flags Oc Ar files +.Nm bzegrep +.Oo Ar flags Oc Ar files +.Nm bzfgrep +.Oo Ar flags Oc Ar files +.Nm lzgrep +.Oo Ar flags Oc Ar files +.Nm lzegrep +.Oo Ar flags Oc Ar files +.Nm lzfgrep +.Oo Ar flags Oc Ar files +.Nm xzgrep +.Oo Ar flags Oc Ar files +.Nm xzegrep +.Oo Ar flags Oc Ar files +.Nm xzfgrep +.Oo Ar flags Oc Ar files +.Nm zstdgrep +.Oo Ar flags Oc Ar files +.Nm zstdegrep +.Oo Ar flags Oc Ar files +.Nm zstdfgrep +.Oo Ar flags Oc Ar files +.Sh DESCRIPTION +Allow +.Xr grep 1 +to read compressed files +.Sh SEE ALSO +.Xr grep 1 +.Sh AUTHORS +This version of the +.Nm +utility was written by +.An Thomas Klausner Aq Mt wiz@NetBSD.org . diff --git a/usr.bin/grep/zgrep.sh b/usr.bin/grep/zgrep.sh new file mode 100755 index 000000000000..c8b91fc34a4f --- /dev/null +++ b/usr.bin/grep/zgrep.sh @@ -0,0 +1,151 @@ +#!/bin/sh +# +# Copyright (c) 2003 Thomas Klausner. +# +# 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 ``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 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. +# +# $FreeBSD$ + +set -eu +grep=grep +zcat=zstdcat + +endofopts=0 +pattern_found=0 +grep_args="" +hyphen=0 +silent=0 + +prg=$(basename $0) + +# handle being called 'zegrep' or 'zfgrep' +case ${prg} in +*egrep) + grep_args="-E";; +*fgrep) + grep_args="-F";; +esac + +catargs="-f" +case ${prg} in +zstd*) + cattool="/usr/bin/zstdcat" + catargs="-fq" + ;; +bz*) + cattool="/usr/bin/bzcat" + ;; +z*) + cattool="/usr/bin/zcat" + ;; +xz*) + cattool="/usr/bin/xzcat" + ;; +lz*) + cattool="/usr/bin/lzcat" + ;; +*) + echo "Invalid command: ${prg}" >&2 + exit 1 + ;; +esac + +# skip all options and pass them on to grep taking care of options +# with arguments, and if -e was supplied + +while [ $# -gt 0 -a ${endofopts} -eq 0 ] +do + case $1 in + # from GNU grep-2.5.1 -- keep in sync! + -[ABCDXdefm]) + if [ $# -lt 2 ] + then + echo "${prg}: missing argument for $1 flag" >&2 + exit 1 + fi + case $1 in + -e) + pattern="$2" + pattern_found=1 + shift 2 + break + ;; + *) + ;; + esac + grep_args="${grep_args} $1 $2" + shift 2 + ;; + --) + shift + endofopts=1 + ;; + -) + hyphen=1 + shift + ;; + -h) + silent=1 + shift + ;; + -*) + grep_args="${grep_args} $1" + shift + ;; + *) + # pattern to grep for + endofopts=1 + ;; + esac +done + +# if no -e option was found, take next argument as grep-pattern +if [ ${pattern_found} -lt 1 ] +then + if [ $# -ge 1 ]; then + pattern="$1" + shift + elif [ ${hyphen} -gt 0 ]; then + pattern="-" + else + echo "${prg}: missing pattern" >&2 + exit 1 + fi +fi + +# call grep ... +if [ $# -lt 1 ] +then + # ... on stdin + ${cattool} ${catargs} - | ${grep} ${grep_args} -- "${pattern}" - +else + # ... on all files given on the command line + if [ ${silent} -lt 1 -a $# -gt 1 ]; then + grep_args="-H ${grep_args}" + fi + while [ $# -gt 0 ] + do + ${cattool} ${catargs} -- "$1" | ${grep} --label="${1}" ${grep_args} -- "${pattern}" - + shift + done +fi + +exit 0 diff --git a/usr.bin/zstd/Makefile b/usr.bin/zstd/Makefile index 73bcbbcc371c..6be99ab1f0f7 100644 --- a/usr.bin/zstd/Makefile +++ b/usr.bin/zstd/Makefile @@ -15,7 +15,6 @@ CFLAGS+= -I${SRCTOP}/sys/contrib/zstd/programs \ -DXXH_NAMESPACE=ZSTD_ \ -DHAVE_THREAD=1 \ -DZSTD_MULTITHREAD=1 -SCRIPTS= zstdgrep LINKS= ${BINDIR}/zstd ${BINDIR}/unzstd \ ${BINDIR}/zstd ${BINDIR}/zstdcat \ ${BINDIR}/zstd ${BINDIR}/zstdmt