xref: /freebsd/usr.bin/grep/zgrep.sh (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1#!/bin/sh
2#
3# Copyright (c) 2003 Thomas Klausner.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24#
25# $FreeBSD$
26
27set -u
28grep=grep
29zcat=zstdcat
30
31endofopts=0
32pattern_file=0
33pattern_found=0
34grep_args=""
35hyphen=0
36silent=0
37
38prg=${0##*/}
39
40# handle being called 'zegrep' or 'zfgrep'
41case ${prg} in
42*egrep)
43	grep_args="-E";;
44*fgrep)
45	grep_args="-F";;
46esac
47
48catargs="-f"
49case ${prg} in
50zstd*)
51	cattool="/usr/bin/zstdcat"
52	catargs="-fq"
53	;;
54bz*)
55	cattool="/usr/bin/bzcat"
56	;;
57z*)
58	cattool="/usr/bin/zcat"
59	;;
60xz*)
61	cattool="/usr/bin/xzcat"
62	;;
63lz*)
64	cattool="/usr/bin/lzcat"
65	;;
66*)
67	echo "Invalid command: ${prg}" >&2
68	exit 1
69	;;
70esac
71
72# skip all options and pass them on to grep taking care of options
73# with arguments, and if -e was supplied
74
75while [ $# -gt 0 -a ${endofopts} -eq 0 ]
76do
77    case $1 in
78    # from GNU grep-2.5.1 -- keep in sync!
79	--)
80	    shift
81	    endofopts=1
82	    ;;
83	--file=*)
84	    pattern_file=1
85	    grep_args="${grep_args} ${1}"
86	    shift
87	    ;;
88	--regexp=*)
89	    pattern="${1#--regexp=}"
90	    pattern_found=1
91	    shift
92	    ;;
93	-h|--no-filename)
94	    silent=1
95	    shift
96	    ;;
97	-V|--version)
98	    exec ${grep} -V
99	    ;;
100	--*)
101	    grep_args="${grep_args} $1"
102	    shift
103	    ;;
104	-*[ABCDXdefm])
105	    if [ $# -lt 2 ]
106		then
107		echo "${prg}: missing argument for $1 flag" >&2
108		exit 1
109	    fi
110	    case $1 in
111		-*e)
112		    pattern="$2"
113		    pattern_found=1
114		    shift 2
115		    continue
116		    ;;
117		-*f)
118		    pattern_file=1
119		    ;;
120		*)
121		    ;;
122	    esac
123	    grep_args="${grep_args} $1 $2"
124	    shift 2
125	    ;;
126	-)
127	    hyphen=1
128	    shift
129	    ;;
130	-r|-R)
131	    echo "${prg}: the ${1} flag is not currently supported" >&2
132	    exit 1
133	    ;;
134	-*)
135	    grep_args="${grep_args} $1"
136	    shift
137	    ;;
138	*)
139	    # pattern to grep for
140	    endofopts=1
141	    ;;
142    esac
143done
144
145# if no -e option was found, take next argument as grep-pattern
146if [ ${pattern_file} -eq 0 -a ${pattern_found} -eq 0 ]
147then
148    if [ $# -ge 1 ]; then
149	pattern="$1"
150	shift
151    elif [ ${hyphen} -gt 0 ]; then
152	pattern="-"
153    else
154	echo "${prg}: missing pattern" >&2
155	exit 1
156    fi
157    pattern_found=1
158fi
159
160# call grep ...
161if [ $# -lt 1 ]
162then
163    # ... on stdin
164    if [ ${pattern_file} -eq 0 ]; then
165	${cattool} ${catargs} - | ${grep} ${grep_args} -- "${pattern}" -
166    else
167	${cattool} ${catargs} - | ${grep} ${grep_args} -- -
168    fi
169    ret=$?
170else
171    # ... on all files given on the command line
172    if [ ${silent} -lt 1 -a $# -gt 1 ]; then
173	grep_args="-H ${grep_args}"
174    fi
175    # Succeed if any file matches.  First assume no match.
176    ret=1
177    for file; do
178	if [ ${pattern_file} -eq 0 ]; then
179	    ${cattool} ${catargs} -- "${file}" |
180		${grep} --label="${file}" ${grep_args} -- "${pattern}" -
181	else
182	    ${cattool} ${catargs} -- "${file}" |
183		${grep} --label="${file}" ${grep_args} -- -
184	fi
185	this_ret=$?
186	# A match (0) overrides a no-match (1).  An error (>=2) overrides all.
187	if [ ${this_ret} -eq 0 -a ${ret} -eq 1 ] || [ ${this_ret} -ge 2 ]; then
188	    ret=${this_ret}
189	fi
190    done
191fi
192
193exit ${ret}
194