14887Schin######################################################################## 24887Schin# # 34887Schin# This software is part of the ast package # 4*10898Sroland.mainz@nrubsig.org# Copyright (c) 1982-2009 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 32*10898Sroland.mainz@nrubsig.orgtmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; } 33*10898Sroland.mainz@nrubsig.orgtrap "cd /; rm -rf $tmp" EXIT 34*10898Sroland.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 52*10898Sroland.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 56*10898Sroland.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 93*10898Sroland.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} |& 105*10898Sroland.mainz@nrubsig.orgcop=$! 106*10898Sroland.mainz@nrubsig.orgexp=Done 107*10898Sroland.mainz@nrubsig.orgprint -p $'print hello | cat\nprint '$exp 1084887Schinread -t 5 -p 1094887Schinread -t 5 -p 110*10898Sroland.mainz@nrubsig.orggot=$REPLY 111*10898Sroland.mainz@nrubsig.orgif [[ $got != $exp ]] 112*10898Sroland.mainz@nrubsig.orgthen err_exit "${SHELL-ksh} coprocess io failed -- got '$got', expected '$exp'" 1134887Schinfi 1144887Schinexec 5<&p 6>&p 1154887Schinexec 5<&- 6>&- 116*10898Sroland.mainz@nrubsig.org{ sleep 4; kill $cop; } 2>/dev/null & 117*10898Sroland.mainz@nrubsig.orgspy=$! 118*10898Sroland.mainz@nrubsig.orgif wait $cop 2>/dev/null 119*10898Sroland.mainz@nrubsig.orgthen kill $spy 2>/dev/null 120*10898Sroland.mainz@nrubsig.orgelse err_exit "coprocess hung after 'exec 5<&p 6>&p; exec 5<&- 6>&-'" 121*10898Sroland.mainz@nrubsig.orgfi 122*10898Sroland.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 137*10898Sroland.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 177*10898Sroland.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 192*10898Sroland.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=$! 220*10898Sroland.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=$! 232*10898Sroland.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 245*10898Sroland.mainz@nrubsig.orgexp=ksh 246*10898Sroland.mainz@nrubsig.orggot=$(print -r $'#00315 247*10898Sroland.mainz@nrubsig.orgCOATTRIBUTES=\'label=make \' 248*10898Sroland.mainz@nrubsig.org# @(#)$Id: libcoshell (AT&T Research) 2008-04-28 $ 249*10898Sroland.mainz@nrubsig.org_COSHELL_msgfd=5 250*10898Sroland.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) 251*10898Sroland.mainz@nrubsig.org[[ $got == $exp ]] || err_exit "coshell(3) identification sequence failed -- expected '$exp', got '$got'" 252*10898Sroland.mainz@nrubsig.org 253*10898Sroland.mainz@nrubsig.orgfunction cop 254*10898Sroland.mainz@nrubsig.org{ 255*10898Sroland.mainz@nrubsig.org read 256*10898Sroland.mainz@nrubsig.org print ok 257*10898Sroland.mainz@nrubsig.org} 258*10898Sroland.mainz@nrubsig.org 259*10898Sroland.mainz@nrubsig.orgexp=ok 260*10898Sroland.mainz@nrubsig.org 261*10898Sroland.mainz@nrubsig.orgcop |& 262*10898Sroland.mainz@nrubsig.orgpid=$! 263*10898Sroland.mainz@nrubsig.orgif print -p yo 2>/dev/null 264*10898Sroland.mainz@nrubsig.orgthen read -p got 265*10898Sroland.mainz@nrubsig.orgelse got='no coprocess' 266*10898Sroland.mainz@nrubsig.orgfi 267*10898Sroland.mainz@nrubsig.org[[ $got == $exp ]] || err_exit "main coprocess main query failed -- expected $exp, got '$got'" 268*10898Sroland.mainz@nrubsig.orgkill $pid 2>/dev/null 269*10898Sroland.mainz@nrubsig.orgwait 270*10898Sroland.mainz@nrubsig.org 271*10898Sroland.mainz@nrubsig.orgcop |& 272*10898Sroland.mainz@nrubsig.orgpid=$! 273*10898Sroland.mainz@nrubsig.org( 274*10898Sroland.mainz@nrubsig.orgif print -p yo 2>/dev/null 275*10898Sroland.mainz@nrubsig.orgthen read -p got 276*10898Sroland.mainz@nrubsig.orgelse got='no coprocess' 277*10898Sroland.mainz@nrubsig.orgfi 278*10898Sroland.mainz@nrubsig.org[[ $got == $exp ]] || err_exit "main coprocess subshell query failed -- expected $exp, got '$got'" 279*10898Sroland.mainz@nrubsig.org) 280*10898Sroland.mainz@nrubsig.orgkill $pid 2>/dev/null 281*10898Sroland.mainz@nrubsig.orgwait 282*10898Sroland.mainz@nrubsig.org 283*10898Sroland.mainz@nrubsig.orgexp='no coprocess' 284*10898Sroland.mainz@nrubsig.org 285*10898Sroland.mainz@nrubsig.org( 286*10898Sroland.mainz@nrubsig.orgcop |& 287*10898Sroland.mainz@nrubsig.orgprint $! > $tmp/pid 288*10898Sroland.mainz@nrubsig.org) 289*10898Sroland.mainz@nrubsig.orgpid=$(<$tmp/pid) 290*10898Sroland.mainz@nrubsig.orgif print -p yo 2>/dev/null 291*10898Sroland.mainz@nrubsig.orgthen read -p got 292*10898Sroland.mainz@nrubsig.orgelse got=$exp 293*10898Sroland.mainz@nrubsig.orgfi 294*10898Sroland.mainz@nrubsig.org[[ $got == $exp ]] || err_exit "subshell coprocess main query failed -- expected $exp, got '$got'" 295*10898Sroland.mainz@nrubsig.orgkill $pid 2>/dev/null 296*10898Sroland.mainz@nrubsig.orgwait 297*10898Sroland.mainz@nrubsig.org 298*10898Sroland.mainz@nrubsig.org( 299*10898Sroland.mainz@nrubsig.orgcop |& 300*10898Sroland.mainz@nrubsig.orgprint $! > $tmp/pid 301*10898Sroland.mainz@nrubsig.org) 302*10898Sroland.mainz@nrubsig.orgpid=$(<$tmp/pid) 303*10898Sroland.mainz@nrubsig.org( 304*10898Sroland.mainz@nrubsig.orgif print -p yo 2>/dev/null 305*10898Sroland.mainz@nrubsig.orgthen read -p got 306*10898Sroland.mainz@nrubsig.orgelse got=$exp 307*10898Sroland.mainz@nrubsig.orgfi 308*10898Sroland.mainz@nrubsig.org[[ $got == $exp ]] || err_exit "subshell coprocess subshell query failed -- expected $exp, got '$got'" 309*10898Sroland.mainz@nrubsig.orgkill $pid 2>/dev/null 310*10898Sroland.mainz@nrubsig.orgwait 311*10898Sroland.mainz@nrubsig.org) 312*10898Sroland.mainz@nrubsig.org 3134887Schinexit $((Errors)) 314