14887Schin######################################################################## 24887Schin# # 34887Schin# This software is part of the ast package # 4*12068SRoger.Faulkner@Oracle.COM# Copyright (c) 1982-2010 AT&T Intellectual Property # 54887Schin# and is licensed under the # 64887Schin# Common Public License, Version 1.0 # 78462SApril.Chin@Sun.COM# by AT&T Intellectual Property # 84887Schin# # 94887Schin# A copy of the License is available at # 104887Schin# http://www.opensource.org/licenses/cpl1.0.txt # 114887Schin# (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) # 124887Schin# # 134887Schin# Information and Software Systems Research # 144887Schin# AT&T Research # 154887Schin# Florham Park NJ # 164887Schin# # 174887Schin# David Korn <dgk@research.att.com> # 184887Schin# # 194887Schin######################################################################## 204887Schin# test the behavior of co-processes 214887Schinfunction err_exit 224887Schin{ 234887Schin print -u2 -n "\t" 244887Schin print -u2 -r ${Command}[$1]: "${@:2}" 254887Schin let Errors+=1 264887Schin} 274887Schinalias err_exit='err_exit $LINENO' 284887Schin 294887SchinCommand=${0##*/} 304887Schininteger Errors=0 314887Schin 3210898Sroland.mainz@nrubsig.orgtmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; } 3310898Sroland.mainz@nrubsig.orgtrap "cd /; rm -rf $tmp" EXIT 3410898Sroland.mainz@nrubsig.org 354887Schinif [[ -d /cygdrive ]] 364887Schinthen err_exit cygwin detected - coprocess tests disabled - enable at the risk of wedging your system 374887Schin exit $((Errors)) 384887Schinfi 394887Schin 404887Schinfunction ping # id 414887Schin{ 424887Schin integer x=0 438462SApril.Chin@Sun.COM while ((x++ < 5)) 444887Schin do read -r 454887Schin print -r "$1 $REPLY" 464887Schin done 474887Schin} 484887Schin 494887Schincat |& 504887Schinprint -p "hello" 514887Schinread -p line 5210898Sroland.mainz@nrubsig.org[[ $line == hello ]] || err_exit 'coprocessing fails' 534887Schinexec 5>&p 6<&p 544887Schinprint -u5 'hello again' || err_exit 'write on u5 fails' 554887Schinread -u6 line 5610898Sroland.mainz@nrubsig.org[[ $line == 'hello again' ]] || err_exit 'coprocess after moving fds fails' 574887Schinexec 5<&- 6<&- 588462SApril.Chin@Sun.COMwait $! 594887Schin 604887Schinping three |& 614887Schinexec 3>&p 624887Schinping four |& 634887Schinexec 4>&p 644887Schinping pipe |& 654887Schin 664887Schininteger count 674887Schinfor i in three four pipe four pipe four three pipe pipe three pipe 684887Schindo case $i in 694887Schin three) to=-u3;; 704887Schin four) to=-u4;; 714887Schin pipe) to=-p;; 724887Schin esac 738462SApril.Chin@Sun.COM (( count++ )) 748462SApril.Chin@Sun.COM print $to $i $count 754887Schindone 764887Schin 774887Schinwhile ((count > 0)) 788462SApril.Chin@Sun.COMdo (( count-- )) 794887Schin read -p 804887Schin set -- $REPLY 814887Schin if [[ $1 != $2 ]] 828462SApril.Chin@Sun.COM then err_exit "$1 does not match $2" 834887Schin fi 844887Schin case $1 in 858462SApril.Chin@Sun.COM three) ;; 868462SApril.Chin@Sun.COM four) ;; 878462SApril.Chin@Sun.COM pipe) ;; 888462SApril.Chin@Sun.COM *) err_exit "unknown message +|$REPLY|+" ;; 894887Schin esac 904887Schindone 918462SApril.Chin@Sun.COMkill $(jobs -p) 2>/dev/null 924887Schin 9310898Sroland.mainz@nrubsig.orgfile=$tmp/regress 944887Schincat > $file <<\! 954887Schin/bin/cat |& 964887Schin! 974887Schinchmod +x $file 988462SApril.Chin@Sun.COMsleep 10 |& 998462SApril.Chin@Sun.COM$file 2> /dev/null || err_exit "parent coprocess prevents script coprocess" 1004887Schinexec 5<&p 6>&p 1014887Schinexec 5<&- 6>&- 1028462SApril.Chin@Sun.COMkill $(jobs -p) 2>/dev/null 1038462SApril.Chin@Sun.COM 1044887Schin${SHELL-ksh} |& 10510898Sroland.mainz@nrubsig.orgcop=$! 10610898Sroland.mainz@nrubsig.orgexp=Done 10710898Sroland.mainz@nrubsig.orgprint -p $'print hello | cat\nprint '$exp 1084887Schinread -t 5 -p 1094887Schinread -t 5 -p 11010898Sroland.mainz@nrubsig.orggot=$REPLY 11110898Sroland.mainz@nrubsig.orgif [[ $got != $exp ]] 11210898Sroland.mainz@nrubsig.orgthen err_exit "${SHELL-ksh} coprocess io failed -- got '$got', expected '$exp'" 1134887Schinfi 1144887Schinexec 5<&p 6>&p 1154887Schinexec 5<&- 6>&- 11610898Sroland.mainz@nrubsig.org{ sleep 4; kill $cop; } 2>/dev/null & 11710898Sroland.mainz@nrubsig.orgspy=$! 11810898Sroland.mainz@nrubsig.orgif wait $cop 2>/dev/null 11910898Sroland.mainz@nrubsig.orgthen kill $spy 2>/dev/null 12010898Sroland.mainz@nrubsig.orgelse err_exit "coprocess hung after 'exec 5<&p 6>&p; exec 5<&- 6>&-'" 12110898Sroland.mainz@nrubsig.orgfi 12210898Sroland.mainz@nrubsig.orgwait 1238462SApril.Chin@Sun.COM 1244887Schin{ 1254887Schinecho line1 | grep 'line2' 1264887Schinecho line2 | grep 'line1' 1274887Schin} |& 1288462SApril.Chin@Sun.COMSECONDS=0 count=0 1298462SApril.Chin@Sun.COMwhile read -p -t 10 line 1308462SApril.Chin@Sun.COMdo ((count++)) 1314887Schindone 1324887Schinif (( SECONDS > 8 )) 1338462SApril.Chin@Sun.COMthen err_exit "read -p hanging (SECONDS=$SECONDS count=$count)" 1344887Schinfi 1358462SApril.Chin@Sun.COMwait $! 1368462SApril.Chin@Sun.COM 13710898Sroland.mainz@nrubsig.org( sleep 3 |& sleep 1 && kill $!; sleep 1; sleep 3 |& sleep 1 && kill $! ) || 1384887Schin err_exit "coprocess cleanup not working correctly" 1398462SApril.Chin@Sun.COM{ : |& } 2>/dev/null || 1408462SApril.Chin@Sun.COM err_exit "subshell coprocess lingers in parent" 1418462SApril.Chin@Sun.COMwait $! 1428462SApril.Chin@Sun.COM 1438462SApril.Chin@Sun.COMunset N r e 1448462SApril.Chin@Sun.COMinteger N=5 1458462SApril.Chin@Sun.COMe=12345 1464887Schin( 1478462SApril.Chin@Sun.COM integer i 1488462SApril.Chin@Sun.COM for ((i = 1; i <= N; i++)) 1498462SApril.Chin@Sun.COM do print $i |& 1508462SApril.Chin@Sun.COM read -p r 1518462SApril.Chin@Sun.COM print -n $r 1528462SApril.Chin@Sun.COM wait $! 1538462SApril.Chin@Sun.COM done 1548462SApril.Chin@Sun.COM print 1558462SApril.Chin@Sun.COM) 2>/dev/null | read -t 10 r 1568462SApril.Chin@Sun.COM[[ $r == $e ]] || err_exit "coprocess timing bug -- expected $e, got '$r'" 1578462SApril.Chin@Sun.COMr= 1588462SApril.Chin@Sun.COM( 1598462SApril.Chin@Sun.COM integer i 1608462SApril.Chin@Sun.COM for ((i = 1; i <= N; i++)) 1618462SApril.Chin@Sun.COM do print $i |& 1628462SApril.Chin@Sun.COM sleep 0.01 1638462SApril.Chin@Sun.COM r=$r$(cat <&p) 1648462SApril.Chin@Sun.COM wait $! 1658462SApril.Chin@Sun.COM done 1668462SApril.Chin@Sun.COM print $r 1678462SApril.Chin@Sun.COM) 2>/dev/null | read -t 10 r 1688462SApril.Chin@Sun.COM[[ $r == $e ]] || err_exit "coprocess command substitution bug -- expected $e, got '$r'" 1698462SApril.Chin@Sun.COM 1704887Schin( 1714887Schin /bin/cat |& 1728462SApril.Chin@Sun.COM sleep 0.01 1734887Schin exec 6>&p 1744887Schin print -u6 ok 1754887Schin exec 6>&- 1764887Schin sleep 1 17710898Sroland.mainz@nrubsig.org kill $! 2> /dev/null 1784887Schin) && err_exit 'coprocess with subshell would hang' 1794887Schinfor sig in IOT ABRT 1804887Schindo if ( trap - $sig ) 2> /dev/null 1818462SApril.Chin@Sun.COM then if [[ $( { sig=$sig $SHELL 2> /dev/null <<- '++EOF++' 1824887Schin cat |& 1834887Schin pid=$! 1844887Schin trap "print TRAP" $sig 1854887Schin ( 1864887Schin sleep 2 1874887Schin kill -$sig $$ 1884887Schin sleep 2 1894887Schin kill -$sig $$ 1904887Schin kill $pid 1918462SApril.Chin@Sun.COM sleep 2 19210898Sroland.mainz@nrubsig.org kill $$ 1938462SApril.Chin@Sun.COM ) & 1944887Schin read -p 1958462SApril.Chin@Sun.COM ++EOF++ 1968462SApril.Chin@Sun.COM } ) != $'TRAP\nTRAP' ]] 2> /dev/null 1974887Schin then err_exit 'traps when reading from coprocess not working' 1984887Schin fi 1994887Schin break 2004887Schin fi 2014887Schindone 2024887Schin 2034887Schintrap 'sleep_pid=; kill $pid; err_exit "coprocess 1 hung"' TERM 2044887Schin{ sleep 5; kill $$; } & 2054887Schinsleep_pid=$! 2064887Schinbuiltin cat 2074887Schincat |& 2084887Schinpid=$! 2094887Schinexec 5<&p 6>&p 2104887Schinprint -u6 hi; read -u5 2114887Schin[[ $REPLY == hi ]] || err_exit 'REPLY is $REPLY not hi' 2124887Schinexec 6>&- 2134887Schinwait $pid 2144887Schintrap - TERM 2154887Schin[[ $sleep_pid ]] && kill $sleep_pid 2164887Schin 2174887Schintrap 'sleep_pid=; kill $pid; err_exit "coprocess 2 hung"' TERM 2184887Schin{ sleep 5; kill $$; } & 2194887Schinsleep_pid=$! 22010898Sroland.mainz@nrubsig.orgcat |& 2214887Schinpid=$! 2224887Schinprint foo >&p 2> /dev/null || err_exit 'first write of foo to coprocess failed' 2234887Schinprint foo >&p 2> /dev/null || err_exit 'second write of foo to coprocess failed' 2244887Schinkill $pid 2254887Schinwait $pid 2> /dev/null 2264887Schintrap - TERM 2274887Schin[[ $sleep_pid ]] && kill $sleep_pid 2284887Schin 2294887Schintrap 'sleep_pid=; kill $pid; err_exit "coprocess 3 hung"' TERM 2304887Schin{ sleep 5; kill $$; } & 2314887Schinsleep_pid=$! 23210898Sroland.mainz@nrubsig.orgcat |& 2334887Schinpid=$! 2344887Schinprint -p foo 2354887Schinprint -p bar 2364887Schinread <&p || err_exit 'first read from coprocess failed' 2374887Schin[[ $REPLY == foo ]] || err_exit "first REPLY is $REPLY not foo" 2384887Schinread <&p || err_exit 'second read from coprocess failed' 2394887Schin[[ $REPLY == bar ]] || err_exit "second REPLY is $REPLY not bar" 2404887Schinkill $pid 2414887Schinwait $pid 2> /dev/null 2424887Schintrap - TERM 2434887Schin[[ $sleep_pid ]] && kill $sleep_pid 2444887Schin 24510898Sroland.mainz@nrubsig.orgexp=ksh 24610898Sroland.mainz@nrubsig.orggot=$(print -r $'#00315 24710898Sroland.mainz@nrubsig.orgCOATTRIBUTES=\'label=make \' 24810898Sroland.mainz@nrubsig.org# @(#)$Id: libcoshell (AT&T Research) 2008-04-28 $ 24910898Sroland.mainz@nrubsig.org_COSHELL_msgfd=5 25010898Sroland.mainz@nrubsig.org{ { (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) 25110898Sroland.mainz@nrubsig.org[[ $got == $exp ]] || err_exit "coshell(3) identification sequence failed -- expected '$exp', got '$got'" 25210898Sroland.mainz@nrubsig.org 25310898Sroland.mainz@nrubsig.orgfunction cop 25410898Sroland.mainz@nrubsig.org{ 25510898Sroland.mainz@nrubsig.org read 25610898Sroland.mainz@nrubsig.org print ok 25710898Sroland.mainz@nrubsig.org} 25810898Sroland.mainz@nrubsig.org 25910898Sroland.mainz@nrubsig.orgexp=ok 26010898Sroland.mainz@nrubsig.org 26110898Sroland.mainz@nrubsig.orgcop |& 26210898Sroland.mainz@nrubsig.orgpid=$! 26310898Sroland.mainz@nrubsig.orgif print -p yo 2>/dev/null 26410898Sroland.mainz@nrubsig.orgthen read -p got 26510898Sroland.mainz@nrubsig.orgelse got='no coprocess' 26610898Sroland.mainz@nrubsig.orgfi 26710898Sroland.mainz@nrubsig.org[[ $got == $exp ]] || err_exit "main coprocess main query failed -- expected $exp, got '$got'" 26810898Sroland.mainz@nrubsig.orgkill $pid 2>/dev/null 26910898Sroland.mainz@nrubsig.orgwait 27010898Sroland.mainz@nrubsig.org 27110898Sroland.mainz@nrubsig.orgcop |& 27210898Sroland.mainz@nrubsig.orgpid=$! 27310898Sroland.mainz@nrubsig.org( 27410898Sroland.mainz@nrubsig.orgif print -p yo 2>/dev/null 27510898Sroland.mainz@nrubsig.orgthen read -p got 27610898Sroland.mainz@nrubsig.orgelse got='no coprocess' 27710898Sroland.mainz@nrubsig.orgfi 27810898Sroland.mainz@nrubsig.org[[ $got == $exp ]] || err_exit "main coprocess subshell query failed -- expected $exp, got '$got'" 27910898Sroland.mainz@nrubsig.org) 28010898Sroland.mainz@nrubsig.orgkill $pid 2>/dev/null 28110898Sroland.mainz@nrubsig.orgwait 28210898Sroland.mainz@nrubsig.org 28310898Sroland.mainz@nrubsig.orgexp='no coprocess' 28410898Sroland.mainz@nrubsig.org 28510898Sroland.mainz@nrubsig.org( 28610898Sroland.mainz@nrubsig.orgcop |& 28710898Sroland.mainz@nrubsig.orgprint $! > $tmp/pid 28810898Sroland.mainz@nrubsig.org) 28910898Sroland.mainz@nrubsig.orgpid=$(<$tmp/pid) 29010898Sroland.mainz@nrubsig.orgif print -p yo 2>/dev/null 29110898Sroland.mainz@nrubsig.orgthen read -p got 29210898Sroland.mainz@nrubsig.orgelse got=$exp 29310898Sroland.mainz@nrubsig.orgfi 29410898Sroland.mainz@nrubsig.org[[ $got == $exp ]] || err_exit "subshell coprocess main query failed -- expected $exp, got '$got'" 29510898Sroland.mainz@nrubsig.orgkill $pid 2>/dev/null 29610898Sroland.mainz@nrubsig.orgwait 29710898Sroland.mainz@nrubsig.org 29810898Sroland.mainz@nrubsig.org( 29910898Sroland.mainz@nrubsig.orgcop |& 30010898Sroland.mainz@nrubsig.orgprint $! > $tmp/pid 30110898Sroland.mainz@nrubsig.org) 30210898Sroland.mainz@nrubsig.orgpid=$(<$tmp/pid) 30310898Sroland.mainz@nrubsig.org( 30410898Sroland.mainz@nrubsig.orgif print -p yo 2>/dev/null 30510898Sroland.mainz@nrubsig.orgthen read -p got 30610898Sroland.mainz@nrubsig.orgelse got=$exp 30710898Sroland.mainz@nrubsig.orgfi 30810898Sroland.mainz@nrubsig.org[[ $got == $exp ]] || err_exit "subshell coprocess subshell query failed -- expected $exp, got '$got'" 30910898Sroland.mainz@nrubsig.orgkill $pid 2>/dev/null 31010898Sroland.mainz@nrubsig.orgwait 31110898Sroland.mainz@nrubsig.org) 31210898Sroland.mainz@nrubsig.org 3134887Schinexit $((Errors)) 314