1#! /usr/bin/ksh
2#
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# http://www.illumos.org/license/CDDL.
12#
13
14#
15# Copyright 2017 Nexenta Systems, Inc. All rights reserved.
16# Copyright 2020 Peter Tribble.
17#
18
19XGREP=${XGREP:=/usr/bin/grep}
20FILEDIR=$MY_TESTS/tests/files
21
22fail() {
23	echo $1
24	exit -1
25}
26
27FLAGLIST="
28-n
29-c
30-q
31-v
32-nv
33-vc
34-A 5
35-nA 5
36-cA 5
37-qA 5
38-vA 5
39-nvA 5
40-vcA 5
41-B 5
42-nB 5
43-cB 5
44-qB 5
45-vB 5
46-nvB 5
47-vcB 5
48-C 5
49-nC 5
50-cC 5
51-qC 5
52-vC 5
53-nvC 5
54-vcC 5
55-B 5 -A 2
56-nB 5 -A 2
57-cB 5 -A 2
58-qB 5 -A 2
59-vB 5 -A 2
60-nvB 5 -A 2
61-vcB 5 -A 2
62-B 5 -A 2 -C 5
63-nB 5 -A 2 -C 5
64-cB 5 -A 2 -C 5
65-qB 5 -A 2 -C 5
66-vB 5 -A 2 -C 5
67-nvB 5 -A 2 -C 5
68-vcB 5 -A 2 -C 5
69-5
70-n -5
71-c -5
72-q -5
73-v -5
74-nv -5
75-vc -5
76-50000
77-n -50000
78-c -50000
79-q -50000
80-v -50000
81-nv -50000
82-vc -50000
83-C 5 -B 4 -A 2
84-nC 5 -B 4 -A 2
85-cC 5 -B 4 -A 2
86-qC 5 -B 4 -A 2
87-vC 5 -B 4 -A 2
88-nvC 5 -B 4 -A 2
89-vcC 5 -B 4 -A 2"
90
91echo "$FLAGLIST" > /tmp/flags
92
93cd $FILEDIR
94
95i=0
96while read flags; do
97	print -n "test $i: grep $flags: "
98	$XGREP $flags a test0 test1 test2 \
99	    test3 test4 test5 test6 \
100	    test7 > out
101	err="$?"
102	if [[ $err -ne 0 ]]; then
103		fail "failed on exit: $err"
104	elif [ -n "$(diff out gout$i)" ]; then
105		print "$(diff out gout$i)"
106		fail "output is different"
107	fi
108	echo "passed"
109	((i++))
110done < /tmp/flags
111
112FLAGS2="-nE"
113
114echo "$FLAGS2" > /tmp/flags
115
116while read flags; do
117	print -n "test $i: grep $flags: "
118	$XGREP $flags ".*" testnl > out
119	err="$?"
120	if [[ $err -ne 0 ]]; then
121		fail "failed on exit: $err"
122	elif [ -n "$(diff out gout$i)" ]; then
123		print "$(diff out gout$i)"
124		fail "output is different"
125	fi
126	echo "passed"
127	((i++))
128done < /tmp/flags
129
130FLAGS3="-B 1
131-vA 1
132-vB 1"
133
134echo "$FLAGS3" > /tmp/flags
135
136while read flags; do
137	print -n "test $i: grep $flags: "
138	$XGREP $flags a testnl > out
139	err="$?"
140	if [[ $err -ne 0 ]]; then
141		fail "failed on exit: $err"
142	elif [ -n "$(diff out gout$i)" ]; then
143		print "$(diff out gout$i)"
144		fail "output is different"
145	fi
146	echo "passed"
147	((i++))
148done < /tmp/flags
149
150FLAGS4="-h
151-H"
152
153echo "$FLAGS4" > /tmp/flags
154
155while read flags; do
156	print -n "test $i: grep $flags: "
157	$XGREP $flags a test0 > out
158	err="$?"
159	if [[ $err -ne 0 ]]; then
160		fail "failed on exit: $err"
161	elif [ -n "$(diff out gout$i)" ]; then
162		print "$(diff out gout$i)"
163		fail "output is different"
164	fi
165	echo "passed"
166	((i++))
167done < /tmp/flags
168
169FLAGS5="-r
170-hr"
171
172# need a directory with predictable contents
173rm -fr /tmp/test0
174mkdir /tmp/test0
175cp test0 /tmp/test0
176
177echo "$FLAGS5" > /tmp/flags
178
179while read flags; do
180	print -n "test $i: grep $flags: "
181	$XGREP $flags a /tmp/test0 > out
182	err="$?"
183	if [[ $err -ne 0 ]]; then
184		fail "failed on exit: $err"
185	elif [ -n "$(diff out gout$i)" ]; then
186		print "$(diff out gout$i)"
187		fail "output is different"
188	fi
189	echo "passed"
190	((i++))
191done < /tmp/flags
192