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