xref: /onnv-gate/usr/src/lib/libshell/common/tests/coprocess.sh (revision 4887:feebf9260c2e)
1*4887Schin########################################################################
2*4887Schin#                                                                      #
3*4887Schin#               This software is part of the ast package               #
4*4887Schin#           Copyright (c) 1982-2007 AT&T Knowledge Ventures            #
5*4887Schin#                      and is licensed under the                       #
6*4887Schin#                  Common Public License, Version 1.0                  #
7*4887Schin#                      by AT&T Knowledge Ventures                      #
8*4887Schin#                                                                      #
9*4887Schin#                A copy of the License is available at                 #
10*4887Schin#            http://www.opensource.org/licenses/cpl1.0.txt             #
11*4887Schin#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12*4887Schin#                                                                      #
13*4887Schin#              Information and Software Systems Research               #
14*4887Schin#                            AT&T Research                             #
15*4887Schin#                           Florham Park NJ                            #
16*4887Schin#                                                                      #
17*4887Schin#                  David Korn <dgk@research.att.com>                   #
18*4887Schin#                                                                      #
19*4887Schin########################################################################
20*4887Schin# test the behavior of co-processes
21*4887Schinfunction err_exit
22*4887Schin{
23*4887Schin	print -u2 -n "\t"
24*4887Schin	print -u2 -r ${Command}[$1]: "${@:2}"
25*4887Schin	let Errors+=1
26*4887Schin}
27*4887Schinalias err_exit='err_exit $LINENO'
28*4887Schin
29*4887SchinCommand=${0##*/}
30*4887Schininteger Errors=0
31*4887Schin
32*4887Schinif	[[ -d /cygdrive ]]
33*4887Schinthen	err_exit cygwin detected - coprocess tests disabled - enable at the risk of wedging your system
34*4887Schin	exit $((Errors))
35*4887Schinfi
36*4887Schin
37*4887Schinfunction ping # id
38*4887Schin{
39*4887Schin	integer x=0
40*4887Schin	while ((x < 5))
41*4887Schin	do	read -r
42*4887Schin		print -r "$1 $REPLY"
43*4887Schin	done
44*4887Schin}
45*4887Schin
46*4887Schincat |&
47*4887Schinprint -p "hello"
48*4887Schinread -p line
49*4887Schin[[ $line == hello ]] || err_exit 'coprocessing fails'
50*4887Schinexec 5>&p 6<&p
51*4887Schinprint -u5 'hello again' || err_exit 'write on u5 fails'
52*4887Schinread -u6 line
53*4887Schin[[ $line == 'hello again' ]] || err_exit 'coprocess after moving fds fails'
54*4887Schinexec 5<&- 6<&-
55*4887Schin
56*4887Schinping three |&
57*4887Schinexec 3>&p
58*4887Schinping four |&
59*4887Schinexec 4>&p
60*4887Schinping pipe |&
61*4887Schin
62*4887Schininteger count
63*4887Schinfor i in three four pipe four pipe four three pipe pipe three pipe
64*4887Schindo	case $i in
65*4887Schin	three)	to=-u3;;
66*4887Schin	four)	to=-u4;;
67*4887Schin	pipe)	to=-p;;
68*4887Schin	esac
69*4887Schin	count=count+1
70*4887Schin	print  $to $i $count
71*4887Schindone
72*4887Schin
73*4887Schinwhile	((count > 0))
74*4887Schindo	count=count-1
75*4887Schin	read -p
76*4887Schin#	print -r - "$REPLY"
77*4887Schin	set -- $REPLY
78*4887Schin	if	[[ $1 != $2 ]]
79*4887Schin	then	err_exit "$1 does not match 2"
80*4887Schin	fi
81*4887Schin	case $1 in
82*4887Schin	three);;
83*4887Schin	four) ;;
84*4887Schin	pipe) ;;
85*4887Schin	*)	err_exit "unknown message +|$REPLY|+"
86*4887Schin	esac
87*4887Schindone
88*4887Schin
89*4887Schinfile=/tmp/regress$$
90*4887Schintrap "rm -f $file" EXIT
91*4887Schincat > $file  <<\!
92*4887Schin/bin/cat |&
93*4887Schin!
94*4887Schinchmod +x $file
95*4887Schin$file 2> /dev/null  || err_exit "parent coprocess prevents script coprocess"
96*4887Schinexec 5<&p 6>&p
97*4887Schinexec 5<&- 6>&-
98*4887Schin${SHELL-ksh} |&
99*4887Schinprint -p  $'print hello | cat\nprint Done'
100*4887Schinread -t 5 -p
101*4887Schinread -t 5 -p
102*4887Schinif	[[ $REPLY != Done ]]
103*4887Schinthen	err_exit	"${SHELL-ksh} coprocess not working"
104*4887Schinfi
105*4887Schinexec 5<&p 6>&p
106*4887Schinexec 5<&- 6>&-
107*4887Schincount=0
108*4887Schin{
109*4887Schinecho line1 | grep 'line2'
110*4887Schinecho line2 | grep 'line1'
111*4887Schin} |&
112*4887SchinSECONDS=0
113*4887Schinwhile
114*4887Schin   read -p -t 10 line
115*4887Schindo
116*4887Schin   ((count = count + 1))
117*4887Schin   echo "Line $count: $line"
118*4887Schindone
119*4887Schinif	(( SECONDS > 8 ))
120*4887Schinthen	err_exit 'read -p hanging'
121*4887Schinfi
122*4887Schin( sleep 3 |& sleep 1 && kill $!; sleep 1; sleep 3 |& sleep 1 && kill $! ) ||
123*4887Schin	err_exit "coprocess cleanup not working correctly"
124*4887Schinunset line
125*4887Schin(
126*4887Schin	integer n=0
127*4887Schin	while read  line
128*4887Schin	do	echo $line  |&
129*4887Schin		if	cat  <&p
130*4887Schin		then	((n++))
131*4887Schin			wait $!
132*4887Schin		fi
133*4887Schin	done > /dev/null 2>&1 <<-  !
134*4887Schin		line1
135*4887Schin		line2
136*4887Schin		line3
137*4887Schin		line4
138*4887Schin		line5
139*4887Schin		line6
140*4887Schin		line7
141*4887Schin	!
142*4887Schin	(( n==7 ))  && print ok
143*4887Schin)  | read -t 10 line
144*4887Schinif	[[ $line != ok ]]
145*4887Schinthen	err_exit 'coprocess timing bug'
146*4887Schinfi
147*4887Schin(
148*4887Schin	/bin/cat |&
149*4887Schin	exec 6>&p
150*4887Schin	print -u6 ok
151*4887Schin	exec 6>&-
152*4887Schin	sleep 1
153*4887Schin	kill $! 2> /dev/null
154*4887Schin) && err_exit 'coprocess with subshell would hang'
155*4887Schinfor sig in IOT ABRT
156*4887Schindo	if	( trap - $sig ) 2> /dev/null
157*4887Schin	then	if	[[ $(
158*4887Schin				cat |&
159*4887Schin				pid=$!
160*4887Schin				trap "print TRAP" $sig
161*4887Schin				(
162*4887Schin					sleep 2
163*4887Schin					kill -$sig $$
164*4887Schin					sleep 2
165*4887Schin					kill -$sig $$
166*4887Schin					kill $pid
167*4887Schin				) 2> /dev/null &
168*4887Schin				read -p
169*4887Schin			) != $'TRAP\nTRAP' ]]
170*4887Schin		then	err_exit 'traps when reading from coprocess not working'
171*4887Schin		fi
172*4887Schin		break
173*4887Schin	fi
174*4887Schindone
175*4887Schin
176*4887Schintrap 'sleep_pid=; kill $pid; err_exit "coprocess 1 hung"' TERM
177*4887Schin{ sleep 5; kill $$; } &
178*4887Schinsleep_pid=$!
179*4887Schinbuiltin cat
180*4887Schincat |&
181*4887Schinpid=$!
182*4887Schinexec 5<&p 6>&p
183*4887Schinprint -u6 hi; read -u5
184*4887Schin[[ $REPLY == hi ]] || err_exit 'REPLY is $REPLY not hi'
185*4887Schinexec 6>&-
186*4887Schinwait $pid
187*4887Schintrap - TERM
188*4887Schin[[ $sleep_pid ]] && kill $sleep_pid
189*4887Schin
190*4887Schintrap 'sleep_pid=; kill $pid; err_exit "coprocess 2 hung"' TERM
191*4887Schin{ sleep 5; kill $$; } &
192*4887Schinsleep_pid=$!
193*4887Schincat |&
194*4887Schinpid=$!
195*4887Schinprint foo >&p 2> /dev/null || err_exit 'first write of foo to coprocess failed'
196*4887Schinprint foo >&p 2> /dev/null || err_exit 'second write of foo to coprocess failed'
197*4887Schinkill $pid
198*4887Schinwait $pid 2> /dev/null
199*4887Schintrap - TERM
200*4887Schin[[ $sleep_pid ]] && kill $sleep_pid
201*4887Schin
202*4887Schintrap 'sleep_pid=; kill $pid; err_exit "coprocess 3 hung"' TERM
203*4887Schin{ sleep 5; kill $$; } &
204*4887Schinsleep_pid=$!
205*4887Schincat |&
206*4887Schinpid=$!
207*4887Schinprint -p foo
208*4887Schinprint -p bar
209*4887Schinread <&p || err_exit 'first read from coprocess failed'
210*4887Schin[[ $REPLY == foo ]] || err_exit "first REPLY is $REPLY not foo"
211*4887Schinread <&p || err_exit 'second read from coprocess failed'
212*4887Schin[[ $REPLY == bar ]] || err_exit "second REPLY is $REPLY not bar"
213*4887Schinkill $pid
214*4887Schinwait $pid 2> /dev/null
215*4887Schintrap - TERM
216*4887Schin[[ $sleep_pid ]] && kill $sleep_pid
217*4887Schin
218*4887Schinexit $((Errors))
219