| 1 | # Demo for https://github.com/oilshell/oil/issues/986
 | 
| 2 | #
 | 
| 3 | # Usage:
 | 
| 4 | #   $SH demo/signal-during-read.sh <function name>
 | 
| 5 | #
 | 
| 6 | # Example:
 | 
| 7 | #   bin/osh demo/signal-during-read bash_read
 | 
| 8 | #
 | 
| 9 | # bash and zsh explicitly handle EINTR during the 'read' builtin, and run
 | 
| 10 | # pending traps!  OSH should do this too.
 | 
| 11 | 
 | 
| 12 | # Related: demo/sigwinch-bug.sh
 | 
| 13 | 
 | 
| 14 | handler() {
 | 
| 15 |   echo "SIGHUP received" >&2
 | 
| 16 | }
 | 
| 17 | 
 | 
| 18 | slow() {
 | 
| 19 |   for i in 1 2 3; do
 | 
| 20 |     echo $i
 | 
| 21 |     sleep $i
 | 
| 22 |   done
 | 
| 23 | }
 | 
| 24 | 
 | 
| 25 | trap handler HUP
 | 
| 26 | 
 | 
| 27 | echo "PID $$"
 | 
| 28 | #echo "SHELL $SHELL"
 | 
| 29 | 
 | 
| 30 | # Note: unlike the read builtin, none of bash/dash/zsh run signal handlers
 | 
| 31 | # during an interrupted read.
 | 
| 32 | command_sub() {
 | 
| 33 |   echo "command sub"
 | 
| 34 |   echo 'a=$(slow)'
 | 
| 35 |   a=$(slow)
 | 
| 36 |   echo "status=$? a=$a"
 | 
| 37 |   echo ---
 | 
| 38 | }
 | 
| 39 | 
 | 
| 40 | # There are several different kinds of 'read', test them all
 | 
| 41 | 
 | 
| 42 | bash_read() {
 | 
| 43 |   echo 'read x'
 | 
| 44 |   read x
 | 
| 45 |   echo
 | 
| 46 |   echo "status=$? x=$x"
 | 
| 47 | 
 | 
| 48 |   echo 'read -n 3 y'
 | 
| 49 |   read -n 3 y
 | 
| 50 |   echo "status=$? y=$y"
 | 
| 51 |   echo ---
 | 
| 52 | 
 | 
| 53 |   echo 'read -d , z'
 | 
| 54 |   read -d , z
 | 
| 55 |   echo "status=$? z=$z"
 | 
| 56 |   echo ---
 | 
| 57 | 
 | 
| 58 |   echo 'mapfile'
 | 
| 59 |   mapfile
 | 
| 60 |   echo "status=$? MAPFILE=${MAPFILE[@]}"
 | 
| 61 |   echo ---
 | 
| 62 | }
 | 
| 63 | 
 | 
| 64 | osh_read() {
 | 
| 65 |   echo 'read --line'
 | 
| 66 |   read --line
 | 
| 67 |   echo "status=$? _line=$_line"
 | 
| 68 |   echo ---
 | 
| 69 | 
 | 
| 70 |   echo 'read --all'
 | 
| 71 |   read --all
 | 
| 72 |   echo "status=$? _all=$_all"
 | 
| 73 |   echo ---
 | 
| 74 | }
 | 
| 75 | 
 | 
| 76 | "$@"
 |