xref: /f-stack/freebsd/contrib/zstd/programs/zstdgrep (revision 22ce4aff)
1*22ce4affSfengbojiang#!/bin/sh
2*22ce4affSfengbojiang#
3*22ce4affSfengbojiang# Copyright (c) 2003 Thomas Klausner.
4*22ce4affSfengbojiang#
5*22ce4affSfengbojiang# Redistribution and use in source and binary forms, with or without
6*22ce4affSfengbojiang# modification, are permitted provided that the following conditions
7*22ce4affSfengbojiang# are met:
8*22ce4affSfengbojiang# 1. Redistributions of source code must retain the above copyright
9*22ce4affSfengbojiang#    notice, this list of conditions and the following disclaimer.
10*22ce4affSfengbojiang# 2. Redistributions in binary form must reproduce the above copyright
11*22ce4affSfengbojiang#    notice, this list of conditions and the following disclaimer in the
12*22ce4affSfengbojiang#    documentation and/or other materials provided with the distribution.
13*22ce4affSfengbojiang#
14*22ce4affSfengbojiang# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15*22ce4affSfengbojiang# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16*22ce4affSfengbojiang# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17*22ce4affSfengbojiang# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18*22ce4affSfengbojiang# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19*22ce4affSfengbojiang# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*22ce4affSfengbojiang# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21*22ce4affSfengbojiang# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22*22ce4affSfengbojiang# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23*22ce4affSfengbojiang# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*22ce4affSfengbojiang
25*22ce4affSfengbojianggrep=${GREP:-grep}
26*22ce4affSfengbojiangzcat=${ZCAT:-zstdcat}
27*22ce4affSfengbojiang
28*22ce4affSfengbojiangendofopts=0
29*22ce4affSfengbojiangpattern_found=0
30*22ce4affSfengbojianggrep_args=""
31*22ce4affSfengbojianghyphen=0
32*22ce4affSfengbojiangsilent=0
33*22ce4affSfengbojiang
34*22ce4affSfengbojiangprog=${0##*/}
35*22ce4affSfengbojiang
36*22ce4affSfengbojiang# handle being called 'zegrep' or 'zfgrep'
37*22ce4affSfengbojiangcase $prog in
38*22ce4affSfengbojiang    *egrep*) prog=zegrep; grep_args='-E';;
39*22ce4affSfengbojiang    *fgrep*) prog=zfgrep; grep_args='-F';;
40*22ce4affSfengbojiang    *)       prog=zstdgrep;;
41*22ce4affSfengbojiangesac
42*22ce4affSfengbojiang
43*22ce4affSfengbojiang# skip all options and pass them on to grep taking care of options
44*22ce4affSfengbojiang# with arguments, and if -e was supplied
45*22ce4affSfengbojiang
46*22ce4affSfengbojiangwhile [ "$#" -gt 0 ] && [ "${endofopts}" -eq 0 ]; do
47*22ce4affSfengbojiang    case "$1" in
48*22ce4affSfengbojiang    # from GNU grep-2.5.1 -- keep in sync!
49*22ce4affSfengbojiang        -[ABCDXdefm])
50*22ce4affSfengbojiang            if [ "$#" -lt 2 ]; then
51*22ce4affSfengbojiang                printf '%s: missing argument for %s flag\n' "${prog}" "$1" >&2
52*22ce4affSfengbojiang                exit 1
53*22ce4affSfengbojiang            fi
54*22ce4affSfengbojiang            case "$1" in
55*22ce4affSfengbojiang                -e)
56*22ce4affSfengbojiang                    pattern="$2"
57*22ce4affSfengbojiang                    pattern_found=1
58*22ce4affSfengbojiang                    shift 2
59*22ce4affSfengbojiang                    break
60*22ce4affSfengbojiang                    ;;
61*22ce4affSfengbojiang                -f)
62*22ce4affSfengbojiang                    pattern_found=2
63*22ce4affSfengbojiang                    ;;
64*22ce4affSfengbojiang                *)
65*22ce4affSfengbojiang                    ;;
66*22ce4affSfengbojiang            esac
67*22ce4affSfengbojiang            grep_args="${grep_args} $1 $2"
68*22ce4affSfengbojiang            shift 2
69*22ce4affSfengbojiang            ;;
70*22ce4affSfengbojiang        --)
71*22ce4affSfengbojiang            shift
72*22ce4affSfengbojiang            endofopts=1
73*22ce4affSfengbojiang            ;;
74*22ce4affSfengbojiang        -)
75*22ce4affSfengbojiang            hyphen=1
76*22ce4affSfengbojiang            shift
77*22ce4affSfengbojiang            ;;
78*22ce4affSfengbojiang        -h)
79*22ce4affSfengbojiang            silent=1
80*22ce4affSfengbojiang            shift
81*22ce4affSfengbojiang            ;;
82*22ce4affSfengbojiang        -*)
83*22ce4affSfengbojiang            grep_args="${grep_args} $1"
84*22ce4affSfengbojiang            shift
85*22ce4affSfengbojiang            ;;
86*22ce4affSfengbojiang        *)
87*22ce4affSfengbojiang            # pattern to grep for
88*22ce4affSfengbojiang            endofopts=1
89*22ce4affSfengbojiang            ;;
90*22ce4affSfengbojiang    esac
91*22ce4affSfengbojiangdone
92*22ce4affSfengbojiang
93*22ce4affSfengbojiang# if no -e option was found, take next argument as grep-pattern
94*22ce4affSfengbojiangif [ "${pattern_found}" -lt 1 ]; then
95*22ce4affSfengbojiang    if [ "$#" -ge 1 ]; then
96*22ce4affSfengbojiang        pattern="$1"
97*22ce4affSfengbojiang        shift
98*22ce4affSfengbojiang    elif [ "${hyphen}" -gt 0 ]; then
99*22ce4affSfengbojiang        pattern="-"
100*22ce4affSfengbojiang    else
101*22ce4affSfengbojiang        printf '%s: missing pattern\n' "${prog}" >&2
102*22ce4affSfengbojiang        exit 1
103*22ce4affSfengbojiang    fi
104*22ce4affSfengbojiangfi
105*22ce4affSfengbojiang
106*22ce4affSfengbojiangEXIT_CODE=0
107*22ce4affSfengbojiang# call grep ...
108*22ce4affSfengbojiangif [ "$#" -lt 1 ]; then
109*22ce4affSfengbojiang    # ... on stdin
110*22ce4affSfengbojiang    set -f # Disable file name generation (globbing).
111*22ce4affSfengbojiang    # shellcheck disable=SC2086
112*22ce4affSfengbojiang    "${zcat}" - | "${grep}" ${grep_args} -- "${pattern}" -
113*22ce4affSfengbojiang    EXIT_CODE=$?
114*22ce4affSfengbojiang    set +f
115*22ce4affSfengbojiangelse
116*22ce4affSfengbojiang    # ... on all files given on the command line
117*22ce4affSfengbojiang    if [ "${silent}" -lt 1 ] && [ "$#" -gt 1 ]; then
118*22ce4affSfengbojiang        grep_args="-H ${grep_args}"
119*22ce4affSfengbojiang    fi
120*22ce4affSfengbojiang    set -f
121*22ce4affSfengbojiang    while [ "$#" -gt 0 ]; do
122*22ce4affSfengbojiang        # shellcheck disable=SC2086
123*22ce4affSfengbojiang        if [ $pattern_found -eq 2 ]; then
124*22ce4affSfengbojiang            "${zcat}" -- "$1" | "${grep}" --label="${1}" ${grep_args} -- -
125*22ce4affSfengbojiang        else
126*22ce4affSfengbojiang            "${zcat}" -- "$1" | "${grep}" --label="${1}" ${grep_args} -- "${pattern}" -
127*22ce4affSfengbojiang        fi
128*22ce4affSfengbojiang        [ "$?" -ne 0 ] && EXIT_CODE=1
129*22ce4affSfengbojiang        shift
130*22ce4affSfengbojiang    done
131*22ce4affSfengbojiang    set +f
132*22ce4affSfengbojiangfi
133*22ce4affSfengbojiang
134*22ce4affSfengbojiangexit "${EXIT_CODE}"
135