xref: /freebsd/sys/contrib/zstd/programs/zstdgrep (revision 37f1f2684f2670b204080ef2d6c303becd28545f)
10c16b537SWarner Losh#!/bin/sh
20c16b537SWarner Losh#
30c16b537SWarner Losh# Copyright (c) 2003 Thomas Klausner.
40c16b537SWarner Losh#
50c16b537SWarner Losh# Redistribution and use in source and binary forms, with or without
60c16b537SWarner Losh# modification, are permitted provided that the following conditions
70c16b537SWarner Losh# are met:
80c16b537SWarner Losh# 1. Redistributions of source code must retain the above copyright
90c16b537SWarner Losh#    notice, this list of conditions and the following disclaimer.
100c16b537SWarner Losh# 2. Redistributions in binary form must reproduce the above copyright
110c16b537SWarner Losh#    notice, this list of conditions and the following disclaimer in the
120c16b537SWarner Losh#    documentation and/or other materials provided with the distribution.
130c16b537SWarner Losh#
140c16b537SWarner Losh# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
150c16b537SWarner Losh# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
160c16b537SWarner Losh# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
170c16b537SWarner Losh# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
180c16b537SWarner Losh# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
190c16b537SWarner Losh# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
200c16b537SWarner Losh# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
210c16b537SWarner Losh# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
220c16b537SWarner Losh# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
230c16b537SWarner Losh# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
240c16b537SWarner Losh
252b9c00cbSConrad Meyergrep=${GREP:-grep}
262b9c00cbSConrad Meyerzcat=${ZCAT:-zstdcat}
270c16b537SWarner Losh
280c16b537SWarner Loshendofopts=0
290c16b537SWarner Loshpattern_found=0
300c16b537SWarner Loshgrep_args=""
310c16b537SWarner Loshhyphen=0
320c16b537SWarner Loshsilent=0
330c16b537SWarner Losh
342b9c00cbSConrad Meyerprog=${0##*/}
350c16b537SWarner Losh
360c16b537SWarner Losh# handle being called 'zegrep' or 'zfgrep'
372b9c00cbSConrad Meyercase $prog in
382b9c00cbSConrad Meyer    *egrep*) prog=zegrep; grep_args='-E';;
392b9c00cbSConrad Meyer    *fgrep*) prog=zfgrep; grep_args='-F';;
402b9c00cbSConrad Meyer    *)       prog=zstdgrep;;
410c16b537SWarner Loshesac
420c16b537SWarner Losh
430c16b537SWarner Losh# skip all options and pass them on to grep taking care of options
440c16b537SWarner Losh# with arguments, and if -e was supplied
450c16b537SWarner Losh
46a0483764SConrad Meyerwhile [ "$#" -gt 0 ] && [ "${endofopts}" -eq 0 ]; do
47a0483764SConrad Meyer    case "$1" in
480c16b537SWarner Losh    # from GNU grep-2.5.1 -- keep in sync!
490c16b537SWarner Losh        -[ABCDXdefm])
50a0483764SConrad Meyer            if [ "$#" -lt 2 ]; then
512b9c00cbSConrad Meyer                printf '%s: missing argument for %s flag\n' "${prog}" "$1" >&2
520c16b537SWarner Losh                exit 1
530c16b537SWarner Losh            fi
54a0483764SConrad Meyer            case "$1" in
550c16b537SWarner Losh                -e)
560c16b537SWarner Losh                    pattern="$2"
570c16b537SWarner Losh                    pattern_found=1
580c16b537SWarner Losh                    shift 2
590c16b537SWarner Losh                    break
600c16b537SWarner Losh                    ;;
614d3f1eafSConrad Meyer                -f)
624d3f1eafSConrad Meyer                    pattern_found=2
634d3f1eafSConrad Meyer                    ;;
640c16b537SWarner Losh                *)
650c16b537SWarner Losh                    ;;
660c16b537SWarner Losh            esac
670c16b537SWarner Losh            grep_args="${grep_args} $1 $2"
680c16b537SWarner Losh            shift 2
690c16b537SWarner Losh            ;;
700c16b537SWarner Losh        --)
710c16b537SWarner Losh            shift
720c16b537SWarner Losh            endofopts=1
730c16b537SWarner Losh            ;;
740c16b537SWarner Losh        -)
750c16b537SWarner Losh            hyphen=1
760c16b537SWarner Losh            shift
770c16b537SWarner Losh            ;;
780c16b537SWarner Losh        -h)
790c16b537SWarner Losh            silent=1
800c16b537SWarner Losh            shift
810c16b537SWarner Losh            ;;
820c16b537SWarner Losh        -*)
830c16b537SWarner Losh            grep_args="${grep_args} $1"
840c16b537SWarner Losh            shift
850c16b537SWarner Losh            ;;
860c16b537SWarner Losh        *)
870c16b537SWarner Losh            # pattern to grep for
880c16b537SWarner Losh            endofopts=1
890c16b537SWarner Losh            ;;
900c16b537SWarner Losh    esac
910c16b537SWarner Loshdone
920c16b537SWarner Losh
930c16b537SWarner Losh# if no -e option was found, take next argument as grep-pattern
94a0483764SConrad Meyerif [ "${pattern_found}" -lt 1 ]; then
95a0483764SConrad Meyer    if [ "$#" -ge 1 ]; then
960c16b537SWarner Losh        pattern="$1"
970c16b537SWarner Losh        shift
98a0483764SConrad Meyer    elif [ "${hyphen}" -gt 0 ]; then
990c16b537SWarner Losh        pattern="-"
1000c16b537SWarner Losh    else
1012b9c00cbSConrad Meyer        printf '%s: missing pattern\n' "${prog}" >&2
1020c16b537SWarner Losh        exit 1
1030c16b537SWarner Losh    fi
1040c16b537SWarner Loshfi
1050c16b537SWarner Losh
106a0483764SConrad MeyerEXIT_CODE=0
1070c16b537SWarner Losh# call grep ...
108a0483764SConrad Meyerif [ "$#" -lt 1 ]; then
1090c16b537SWarner Losh    # ... on stdin
110a0483764SConrad Meyer    set -f # Disable file name generation (globbing).
111a0483764SConrad Meyer    # shellcheck disable=SC2086
112*37f1f268SConrad Meyer    "${zcat}" - | "${grep}" ${grep_args} -- "${pattern}" -
113a0483764SConrad Meyer    EXIT_CODE=$?
114a0483764SConrad Meyer    set +f
1150c16b537SWarner Loshelse
1160c16b537SWarner Losh    # ... on all files given on the command line
117a0483764SConrad Meyer    if [ "${silent}" -lt 1 ] && [ "$#" -gt 1 ]; then
1180c16b537SWarner Losh        grep_args="-H ${grep_args}"
1190c16b537SWarner Losh    fi
120a0483764SConrad Meyer    set -f
121a0483764SConrad Meyer    while [ "$#" -gt 0 ]; do
122a0483764SConrad Meyer        # shellcheck disable=SC2086
1234d3f1eafSConrad Meyer        if [ $pattern_found -eq 2 ]; then
124*37f1f268SConrad Meyer            "${zcat}" -- "$1" | "${grep}" --label="${1}" ${grep_args} -- -
1254d3f1eafSConrad Meyer        else
126*37f1f268SConrad Meyer            "${zcat}" -- "$1" | "${grep}" --label="${1}" ${grep_args} -- "${pattern}" -
1274d3f1eafSConrad Meyer        fi
1282b9c00cbSConrad Meyer        [ "$?" -ne 0 ] && EXIT_CODE=1
1290c16b537SWarner Losh        shift
1300c16b537SWarner Losh    done
131a0483764SConrad Meyer    set +f
1320c16b537SWarner Loshfi
1330c16b537SWarner Losh
134a0483764SConrad Meyerexit "${EXIT_CODE}"
135