coprocess.sh revision 3e14f97f673e8a630f076077de35afdd43dc1587
########################################################################
# #
# This software is part of the ast package #
# Copyright (c) 1982-2010 AT&T Intellectual Property #
# and is licensed under the #
# Common Public License, Version 1.0 #
# by AT&T Intellectual Property #
# #
# A copy of the License is available at #
# (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) #
# #
# Information and Software Systems Research #
# AT&T Research #
# Florham Park NJ #
# #
# David Korn <dgk@research.att.com> #
# #
########################################################################
# test the behavior of co-processes
function err_exit
{
print -u2 -n "\t"
let Errors+=1
}
alias err_exit='err_exit $LINENO'
integer Errors=0
if [[ -d /cygdrive ]]
then err_exit cygwin detected - coprocess tests disabled - enable at the risk of wedging your system
fi
function ping # id
{
integer x=0
while ((x++ < 5))
do read -r
print -r "$1 $REPLY"
done
}
cat |&
print -p "hello"
read -p line
exec 5>&p 6<&p
exec 5<&- 6<&-
wait $!
exec 3>&p
exec 4>&p
integer count
do case $i in
esac
done
read -p
set -- $REPLY
if [[ $1 != $2 ]]
then err_exit "$1 does not match $2"
fi
case $1 in
three) ;;
four) ;;
pipe) ;;
esac
done
!
sleep 10 |&
exec 5<&p 6>&p
exec 5<&- 6>&-
cop=$!
print -p $'print hello | cat\nprint '$exp
read -t 5 -p
read -t 5 -p
fi
exec 5<&p 6>&p
exec 5<&- 6>&-
spy=$!
else err_exit "coprocess hung after 'exec 5<&p 6>&p; exec 5<&- 6>&-'"
fi
wait
{
} |&
while read -p -t 10 line
done
fi
wait $!
err_exit "coprocess cleanup not working correctly"
err_exit "subshell coprocess lingers in parent"
wait $!
unset N r e
integer N=5
e=12345
(
integer i
for ((i = 1; i <= N; i++))
do print $i |&
read -p r
print -n $r
wait $!
done
print
[[ $r == $e ]] || err_exit "coprocess timing bug -- expected $e, got '$r'"
r=
(
integer i
for ((i = 1; i <= N; i++))
do print $i |&
sleep 0.01
r=$r$(cat <&p)
wait $!
done
print $r
[[ $r == $e ]] || err_exit "coprocess command substitution bug -- expected $e, got '$r'"
(
sleep 0.01
exec 6>&p
exec 6>&-
sleep 1
cat |&
pid=$!
(
sleep 2
kill -$sig $$
sleep 2
kill -$sig $$
kill $pid
sleep 2
kill $$
) &
read -p
++EOF++
then err_exit 'traps when reading from coprocess not working'
fi
break
fi
done
{ sleep 5; kill $$; } &
sleep_pid=$!
builtin cat
cat |&
pid=$!
exec 5<&p 6>&p
exec 6>&-
wait $pid
trap - TERM
[[ $sleep_pid ]] && kill $sleep_pid
{ sleep 5; kill $$; } &
sleep_pid=$!
cat |&
pid=$!
kill $pid
trap - TERM
[[ $sleep_pid ]] && kill $sleep_pid
{ sleep 5; kill $$; } &
sleep_pid=$!
cat |&
pid=$!
print -p foo
print -p bar
read <&p || err_exit 'first read from coprocess failed'
read <&p || err_exit 'second read from coprocess failed'
kill $pid
trap - TERM
[[ $sleep_pid ]] && kill $sleep_pid
COATTRIBUTES=\'label=make \'
# @(#)$Id: libcoshell (AT&T Research) 2008-04-28 $
_COSHELL_msgfd=5
{ { (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)
[[ $got == $exp ]] || err_exit "coshell(3) identification sequence failed -- expected '$exp', got '$got'"
function cop
{
read
print ok
}
cop |&
pid=$!
then read -p got
else got='no coprocess'
fi
wait
cop |&
pid=$!
(
then read -p got
else got='no coprocess'
fi
)
wait
exp='no coprocess'
(
cop |&
)
then read -p got
fi
wait
(
cop |&
)
(
then read -p got
fi
[[ $got == $exp ]] || err_exit "subshell coprocess subshell query failed -- expected $exp, got '$got'"
wait
)