Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/ksh93/tests/coprocess.sh
1810 views
1
########################################################################
2
# #
3
# This software is part of the ast package #
4
# Copyright (c) 1982-2012 AT&T Intellectual Property #
5
# and is licensed under the #
6
# Eclipse Public License, Version 1.0 #
7
# by AT&T Intellectual Property #
8
# #
9
# A copy of the License is available at #
10
# http://www.eclipse.org/org/documents/epl-v10.html #
11
# (with md5 checksum b35adb5213ca9657e911e9befb180842) #
12
# #
13
# Information and Software Systems Research #
14
# AT&T Research #
15
# Florham Park NJ #
16
# #
17
# David Korn <[email protected]> #
18
# #
19
########################################################################
20
# test the behavior of co-processes
21
function err_exit
22
{
23
print -u2 -n "\t"
24
print -u2 -r ${Command}[$1]: "${@:2}"
25
let Errors+=1
26
}
27
alias err_exit='err_exit $LINENO'
28
29
Command=${0##*/}
30
integer Errors=0
31
32
tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
33
trap "cd /; rm -rf $tmp" EXIT
34
35
if [[ -d /cygdrive ]]
36
then err_exit cygwin detected - coprocess tests disabled - enable at the risk of wedging your system
37
exit $((Errors))
38
fi
39
40
bintrue=$(whence -p true)
41
42
function ping # id
43
{
44
integer x=0
45
while ((x++ < 5))
46
do read -r
47
print -r "$1 $REPLY"
48
done
49
}
50
51
bincat=$(whence -p cat)
52
builtin cat
53
54
for cat in cat $bincat
55
do
56
57
$cat |&
58
print -p "hello"
59
read -p line
60
[[ $line == hello ]] || err_exit "$cat coprocessing fails"
61
exec 5>&p 6<&p
62
print -u5 'hello again' || err_exit "write on $cat coprocess u5 fails"
63
read -u6 line
64
[[ $line == 'hello again' ]] || err_exit "$cat coprocess after moving fds fails"
65
exec 5<&- 6<&-
66
wait $!
67
68
ping three |&
69
exec 3>&p
70
ping four |&
71
exec 4>&p
72
ping pipe |&
73
74
integer count
75
for i in three four pipe four pipe four three pipe pipe three pipe
76
do case $i in
77
three) to=-u3;;
78
four) to=-u4;;
79
pipe) to=-p;;
80
esac
81
(( count++ ))
82
print $to $i $count
83
done
84
85
while ((count > 0))
86
do (( count-- ))
87
read -p
88
set -- $REPLY
89
if [[ $1 != $2 ]]
90
then err_exit "$cat coprocess $1 does not match $2"
91
fi
92
case $1 in
93
three) ;;
94
four) ;;
95
pipe) ;;
96
*) err_exit "$cat coprocess unknown message +|$REPLY|+" ;;
97
esac
98
done
99
kill $(jobs -p) 2>/dev/null
100
101
file=$tmp/regress
102
$cat > $file <<-!
103
$cat |&
104
!
105
chmod +x $file
106
sleep 10 |&
107
$file 2> /dev/null || err_exit "parent $cat coprocess prevents script coprocess"
108
exec 5<&p 6>&p
109
exec 5<&- 6>&-
110
kill $(jobs -p) 2>/dev/null
111
112
${SHELL-ksh} |&
113
cop=$!
114
exp=Done
115
print -p $'print hello | '$cat$'\nprint '$exp
116
read -t 5 -p
117
read -t 5 -p
118
got=$REPLY
119
if [[ $got != $exp ]]
120
then err_exit "${SHELL-ksh} $cat coprocess io failed -- got '$got', expected '$exp'"
121
fi
122
exec 5<&p 6>&p
123
exec 5<&- 6>&-
124
{ sleep 4; kill $cop; } 2>/dev/null &
125
spy=$!
126
if wait $cop 2>/dev/null
127
then kill $spy 2>/dev/null
128
else err_exit "$cat coprocess hung after 'exec 5<&p 6>&p; exec 5<&- 6>&-'"
129
fi
130
wait
131
132
{
133
echo line1 | grep 'line2'
134
echo line2 | grep 'line1'
135
} |&
136
SECONDS=0 count=0
137
while read -p -t 10 line
138
do ((count++))
139
done
140
if (( SECONDS > 8 ))
141
then err_exit "$cat coprocess read -p hanging (SECONDS=$SECONDS count=$count)"
142
fi
143
wait $!
144
145
( sleep 3 |& sleep 1 && kill $!; sleep 1; sleep 3 |& sleep 1 && kill $! ) ||
146
err_exit "$cat coprocess cleanup not working correctly"
147
{ : |& } 2>/dev/null ||
148
err_exit "subshell $cat coprocess lingers in parent"
149
wait $!
150
151
unset N r e
152
integer N=5
153
e=12345
154
(
155
integer i
156
for ((i = 1; i <= N; i++))
157
do print $i |&
158
read -p r
159
print -n $r
160
wait $!
161
done
162
print
163
) 2>/dev/null | read -t 10 r
164
[[ $r == $e ]] || err_exit "$cat coprocess timing bug -- expected $e, got '$r'"
165
166
r=
167
(
168
integer i
169
for ((i = 1; i <= N; i++))
170
do print $i |&
171
sleep 0.01
172
r=$r$($cat <&p)
173
wait $!
174
done
175
print $r
176
) 2>/dev/null | read -t 10 r
177
[[ $r == $e ]] || err_exit "$cat coprocess command substitution bug -- expected $e, got '$r'"
178
179
(
180
$cat |&
181
sleep 0.01
182
exec 6>&p
183
print -u6 ok
184
exec 6>&-
185
sleep 2
186
kill $! 2> /dev/null
187
) && err_exit "$cat coprocess with subshell would hang"
188
for sig in IOT ABRT
189
do if ( trap - $sig ) 2> /dev/null
190
then if [[ $( { sig=$sig $SHELL 2> /dev/null <<- ++EOF++
191
$cat |&
192
pid=\$!
193
trap "print TRAP" \$sig
194
(
195
sleep 2
196
kill -\$sig \$\$
197
sleep 2
198
kill -\$sig \$\$
199
kill \$pid
200
sleep 2
201
kill \$\$
202
) &
203
while read -p || ((\$? > 256))
204
do :
205
done
206
++EOF++
207
} ) != $'TRAP\nTRAP' ]] 2> /dev/null
208
then err_exit "traps when reading from $cat coprocess not working"
209
fi
210
break
211
fi
212
done
213
214
trap 'sleep_pid=; kill $pid; err_exit "$cat coprocess 1 hung"' TERM
215
{ sleep 5; kill $$; } &
216
sleep_pid=$!
217
$cat |&
218
pid=$!
219
exec 5<&p 6>&p
220
exp=hi
221
print -u6 $exp; read -u5
222
[[ $REPLY == "$exp" ]] || err_exit "REPLY from $cat coprocess failed -- expected '$exp', got '$REPLY'"
223
exec 6>&-
224
wait $pid
225
trap - TERM
226
[[ $sleep_pid ]] && kill $sleep_pid
227
228
trap 'sleep_pid=; kill $pid; err_exit "$cat coprocess 2 hung"' TERM
229
{ sleep 5; kill $$; } &
230
sleep_pid=$!
231
$cat |&
232
pid=$!
233
print foo >&p 2> /dev/null || err_exit "first write of foo to $cat coprocess failed"
234
print foo >&p 2> /dev/null || err_exit "second write of foo to coprocess failed"
235
kill $pid
236
wait $pid 2> /dev/null
237
trap - TERM
238
[[ $sleep_pid ]] && kill $sleep_pid
239
240
trap 'sleep_pid=; kill $pid; err_exit "$cat coprocess 3 hung"' TERM
241
{ sleep 5; kill $$; } &
242
sleep_pid=$!
243
$cat |&
244
pid=$!
245
print -p foo
246
print -p bar
247
read <&p || err_exit "first read from $cat coprocess failed"
248
[[ $REPLY == foo ]] || err_exit "first REPLY from $cat coprocess is $REPLY not foo"
249
read <&p || err_exit "second read from $cat coprocess failed"
250
[[ $REPLY == bar ]] || err_exit "second REPLY from $cat coprocess is $REPLY not bar"
251
kill $pid
252
wait $pid 2> /dev/null
253
trap - TERM
254
[[ $sleep_pid ]] && kill $sleep_pid
255
256
done
257
258
exp=ksh
259
got=$(print -r $'#00315
260
COATTRIBUTES=\'label=make \'
261
# @(#)$Id: libcoshell (AT&T Research) 2008-04-28 $
262
_COSHELL_msgfd=5
263
{ { (eval \'function fun { trap \":\" 0; return 1; }; trap \"exit 0\" 0; fun; exit 1\') && PATH= print -u$_COSHELL_msgfd ksh; } || { times && echo bsh >&$_COSHELL_msgfd; } || { echo osh >&$_COSHELL_msgfd; }; } >/dev/null 2>&1' | $SHELL 5>&1)
264
[[ $got == $exp ]] || err_exit "coshell(3) identification sequence failed -- expected '$exp', got '$got'"
265
266
function cop
267
{
268
read
269
print ok
270
}
271
272
exp=ok
273
274
cop |&
275
pid=$!
276
if print -p yo 2>/dev/null
277
then read -p got
278
else got='no coprocess'
279
fi
280
[[ $got == $exp ]] || err_exit "main coprocess main query failed -- expected $exp, got '$got'"
281
kill $pid 2>/dev/null
282
wait
283
284
cop |&
285
pid=$!
286
(
287
if print -p yo 2>/dev/null
288
then read -p got
289
else got='no coprocess'
290
fi
291
[[ $got == $exp ]] || err_exit "main coprocess subshell query failed -- expected $exp, got '$got'"
292
)
293
kill $pid 2>/dev/null
294
wait
295
296
exp='no coprocess'
297
298
(
299
cop |&
300
print $! > $tmp/pid
301
)
302
pid=$(<$tmp/pid)
303
if print -p yo 2>/dev/null
304
then read -p got
305
else got=$exp
306
fi
307
[[ $got == $exp ]] || err_exit "subshell coprocess main query failed -- expected $exp, got '$got'"
308
kill $pid 2>/dev/null
309
wait
310
311
(
312
cop |&
313
print $! > $tmp/pid
314
)
315
pid=$(<$tmp/pid)
316
(
317
if print -p yo 2>/dev/null
318
then read -p got
319
else got=$exp
320
fi
321
[[ $got == $exp ]] || err_exit "subshell coprocess subshell query failed -- expected $exp, got '$got'"
322
kill $pid 2>/dev/null
323
wait
324
)
325
326
function mypipe
327
{
328
read; read
329
print -r -- "$REPLY"
330
}
331
332
mypipe |&
333
print -p "hello"
334
z="$( $bintrue $($bintrue) )"
335
{ print -p "world";} 2> /dev/null
336
read -p
337
[[ $REPLY == world ]] || err_exit "expected 'world' got '$REPLY'"
338
kill $pid 2>/dev/null
339
wait
340
341
342
function cop
343
{
344
read
345
print ok
346
}
347
exp=ok
348
cop |&
349
pid=$!
350
(
351
if print -p yo 2>/dev/null
352
then read -p got
353
else got='no coprocess'
354
fi
355
[[ $got == $exp ]] || err_exit "main coprocess subshell query failed -- expected $exp, got '$got'"
356
)
357
kill $pid 2>/dev/null
358
wait
359
360
tee=$(whence -p tee)
361
ls -l |&
362
pid=$!
363
$tee -a /dev/null <&p > /dev/null
364
wait $pid
365
x=$?
366
[[ $x == 0 ]] || err_exit "coprocess exitval should be 0, not $x"
367
368
exit $((Errors<125?Errors:125))
369
370