diff --git a/programs/zstdgrep b/programs/zstdgrep index a10e0710a..cb804b8be 100755 --- a/programs/zstdgrep +++ b/programs/zstdgrep @@ -22,8 +22,8 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -grep=grep -zcat=zstdcat +grep=${GREP:-grep} +zcat=${ZCAT:-zstdcat} endofopts=0 pattern_found=0 @@ -31,12 +31,13 @@ grep_args="" hyphen=0 silent=0 -prg=$(basename "$0") +prog=${0##*/} # handle being called 'zegrep' or 'zfgrep' -case "${prg}" in - *zegrep) grep_args="-E";; - *zfgrep) grep_args="-F";; +case $prog in + *egrep*) prog=zegrep; grep_args='-E';; + *fgrep*) prog=zfgrep; grep_args='-F';; + *) prog=zstdgrep;; esac # skip all options and pass them on to grep taking care of options @@ -47,7 +48,7 @@ while [ "$#" -gt 0 ] && [ "${endofopts}" -eq 0 ]; do # from GNU grep-2.5.1 -- keep in sync! -[ABCDXdefm]) if [ "$#" -lt 2 ]; then - printf '%s: missing argument for %s flag\n' "${prg}" "$1" >&2 + printf '%s: missing argument for %s flag\n' "${prog}" "$1" >&2 exit 1 fi case "$1" in @@ -94,7 +95,7 @@ if [ "${pattern_found}" -lt 1 ]; then elif [ "${hyphen}" -gt 0 ]; then pattern="-" else - printf '%s: missing pattern\n' "${prg}" >&2 + printf '%s: missing pattern\n' "${prog}" >&2 exit 1 fi fi @@ -113,16 +114,11 @@ else if [ "${silent}" -lt 1 ] && [ "$#" -gt 1 ]; then grep_args="-H ${grep_args}" fi - CUR_EXIT_CODE=0 - EXIT_CODE=1 set -f while [ "$#" -gt 0 ]; do # shellcheck disable=SC2086 "${zcat}" -fq -- "$1" | "${grep}" --label="${1}" ${grep_args} -- "${pattern}" - - CUR_EXIT_CODE=$? - if [ "${CUR_EXIT_CODE}" -eq 0 ] && [ "${EXIT_CODE}" -ne 1 ]; then - EXIT_CODE=0 - fi + [ "$?" -ne 0 ] && EXIT_CODE=1 shift done set +f diff --git a/tests/Makefile b/tests/Makefile index 25bd5c84e..2daf0970f 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -353,8 +353,12 @@ test-gzstd: gzstd $(RM) *.gz *.zst README2.md gz_zstd zstd_gz hello.txt test-zstdgrep: gzstd - @echo a | $(PRGDIR)/zstd | $(PRGDIR)/zstdgrep a - @echo a | $(PRGDIR)/zstd | $(PRGDIR)/zstdgrep b && return 1 || return 0 + -[ -f /tmp/zstdcat ] || ln -s $(PWD)/$(PRGDIR)/zstd /tmp/zstdcat + echo a | $(PRGDIR)/zstd | env ZCAT=/tmp/zstdcat $(PRGDIR)/zstdgrep a + echo a | $(PRGDIR)/zstd | env ZCAT=/tmp/zstdcat $(PRGDIR)/zstdgrep b && return 1 || return 0 + -echo 'hello world' > test.txt && $(PRGDIR)/zstd test.txt + env ZCAT=/tmp/zstdcat $(PRGDIR)/zstdgrep hello test.txt.zst + env ZCAT=/tmp/zstdcat $(PRGDIR)/zstdgrep weird test.txt.zst && return 1 || return 0 test-fullbench: fullbench datagen $(QEMU_SYS) ./fullbench -i1