xref: /freebsd/usr.bin/grep/zgrep.sh (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
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_found=0
33grep_args=""
34hyphen=0
35silent=0
36
37prg=${0##*/}
38
39# handle being called 'zegrep' or 'zfgrep'
40case ${prg} in
41*egrep)
42	grep_args="-E";;
43*fgrep)
44	grep_args="-F";;
45esac
46
47catargs="-f"
48case ${prg} in
49zstd*)
50	cattool="/usr/bin/zstdcat"
51	catargs="-fq"
52	;;
53bz*)
54	cattool="/usr/bin/bzcat"
55	;;
56z*)
57	cattool="/usr/bin/zcat"
58	;;
59xz*)
60	cattool="/usr/bin/xzcat"
61	;;
62lz*)
63	cattool="/usr/bin/lzcat"
64	;;
65*)
66	echo "Invalid command: ${prg}" >&2
67	exit 1
68	;;
69esac
70
71# skip all options and pass them on to grep taking care of options
72# with arguments, and if -e was supplied
73
74while [ $# -gt 0 -a ${endofopts} -eq 0 ]
75do
76    case $1 in
77    # from GNU grep-2.5.1 -- keep in sync!
78	-[ABCDXdefm])
79	    if [ $# -lt 2 ]
80		then
81		echo "${prg}: missing argument for $1 flag" >&2
82		exit 1
83	    fi
84	    case $1 in
85		-e)
86		    pattern="$2"
87		    pattern_found=1
88		    shift 2
89		    break
90		    ;;
91		*)
92		    ;;
93	    esac
94	    grep_args="${grep_args} $1 $2"
95	    shift 2
96	    ;;
97	--)
98	    shift
99	    endofopts=1
100	    ;;
101	-)
102	    hyphen=1
103	    shift
104	    ;;
105	-h)
106	    silent=1
107	    shift
108	    ;;
109	-r|-R)
110	    echo "${prg}: the ${1} flag is not currently supported" >&2
111	    exit 1
112	    ;;
113	-V|--version)
114	    exec ${grep} -V
115	    ;;
116	-*)
117	    grep_args="${grep_args} $1"
118	    shift
119	    ;;
120	*)
121	    # pattern to grep for
122	    endofopts=1
123	    ;;
124    esac
125done
126
127# if no -e option was found, take next argument as grep-pattern
128if [ ${pattern_found} -lt 1 ]
129then
130    if [ $# -ge 1 ]; then
131	pattern="$1"
132	shift
133    elif [ ${hyphen} -gt 0 ]; then
134	pattern="-"
135    else
136	echo "${prg}: missing pattern" >&2
137	exit 1
138    fi
139fi
140
141ret=0
142# call grep ...
143if [ $# -lt 1 ]
144then
145    # ... on stdin
146    ${cattool} ${catargs} - | ${grep} ${grep_args} -- "${pattern}" - || ret=$?
147else
148    # ... on all files given on the command line
149    if [ ${silent} -lt 1 -a $# -gt 1 ]; then
150	grep_args="-H ${grep_args}"
151    fi
152    for file; do
153	${cattool} ${catargs} -- "${file}" |
154	    ${grep} --label="${file}" ${grep_args} -- "${pattern}" - || ret=$?
155    done
156fi
157
158exit ${ret}
159