xref: /freebsd/usr.bin/grep/zgrep.sh (revision 7cc42f6d)
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	--*)
94	    grep_args="${grep_args} $1"
95	    shift
96	    ;;
97	-*[ABCDXdefm])
98	    if [ $# -lt 2 ]
99		then
100		echo "${prg}: missing argument for $1 flag" >&2
101		exit 1
102	    fi
103	    case $1 in
104		-*e)
105		    pattern="$2"
106		    pattern_found=1
107		    shift 2
108		    continue
109		    ;;
110		-*f)
111		    pattern_file=1
112		    ;;
113		*)
114		    ;;
115	    esac
116	    grep_args="${grep_args} $1 $2"
117	    shift 2
118	    ;;
119	-)
120	    hyphen=1
121	    shift
122	    ;;
123	-h)
124	    silent=1
125	    shift
126	    ;;
127	-r|-R)
128	    echo "${prg}: the ${1} flag is not currently supported" >&2
129	    exit 1
130	    ;;
131	-V|--version)
132	    exec ${grep} -V
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
160ret=0
161# call grep ...
162if [ $# -lt 1 ]
163then
164    # ... on stdin
165    if [ ${pattern_file} -eq 0 ]; then
166	${cattool} ${catargs} - | ${grep} ${grep_args} -- "${pattern}" - || ret=$?
167    else
168	${cattool} ${catargs} - | ${grep} ${grep_args} -- - || ret=$?
169    fi
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    for file; do
176	if [ ${pattern_file} -eq 0 ]; then
177	    ${cattool} ${catargs} -- "${file}" |
178		${grep} --label="${file}" ${grep_args} -- "${pattern}" - || ret=$?
179	else
180	    ${cattool} ${catargs} -- "${file}" |
181		${grep} --label="${file}" ${grep_args} -- - || ret=$?
182	fi
183    done
184fi
185
186exit ${ret}
187