Patch-ID: bash41-003
[bash.git] / examples / functions / coshell.bash
blobdc177b30329144a9c8a7aa78e5ad441cc9cd1afa
1 # vi:set sts=2 sw=2 ai:
3 # coshell.bash - Control shell coprocesses (see coprocess.bash).
6 function coshell ()
8 while (( $# > 0 )) ; do
9 case "$1" in
11 # coshell open
13 o|op|ope|open)
14 shift
15 coprocess open "$@"
16 local ret=$?
18 # This should eat any ssh error messages or what not.
19 coshell eval : >/dev/null 2>&1
20 return $ret
24 # coshell close
26 c|cl|clo|close)
27 shift
28 coprocess close "$@"
29 return $?
33 # coshell eval
35 e|ev|eva|eval)
36 shift
37 local cookie=$RANDOM
38 if (( $# == 0 )) ; then
39 echo "coshell eval: no argumentsl" >&2
40 return 1
42 if [ x$coprocess_pid = x ] ; then
43 echo "coshell eval: no active coshell" >&2
44 return 1
47 coprocess print "$@"
48 coprocess print "coprocess_rc=\$?"
49 coprocess print "printf 'coprocess-$cookie----\n%d\n' \$coprocess_rc"
50 if [ x$coprocess_pid = x ] ; then
51 return 0
54 local ol
55 while coprocess read ol ; do
56 case "$ol" in
57 *coprocess-$cookie----*)
58 ol="${ol%coprocess-$cookie----}"
59 echo -n "$ol"
60 break
62 esac
63 echo "$ol"
64 done
65 coprocess read ol
66 return $ol
70 # coshell sendfile
72 s|se|sen|send|sendf|sendfi|sendfil|sendfile)
73 shift
74 if (( $# != 2 )) ; then
75 echo "coshell sendfile: syntax is 'coshell sendfile SRC TARGET'" >&2
76 return 1
78 if [ x$coprocess_pid = x ] ; then
79 echo "coshell sendfile: no active coshell" >&2
80 return 1
83 local target=$2
84 if coshell test -d "$target" ; then
85 target="$target/${1##*/}"
88 coprocess print "uudecode <<END_OF_FILE"
89 uuencode -m "$target" <$1 |coprocess print --stdin
90 coshell eval "END_OF_FILE"
91 return $?
95 # coshell getfile
97 g|ge|get|getf|getfi|getfil|getfile)
98 shift
99 if (( $# != 2 )) ; then
100 echo "coshell getfile: syntax is 'coshell getfile SRC TARGET'" >&2
101 return 1
103 if [ x$coprocess_pid = x ] ; then
104 echo "coshell getfile: no active coshell" >&2
105 return 1
108 local target=$2
109 if test -d "$target" ; then
110 target="$target/${1##*/}"
113 coshell eval uuencode -m "$target" "<" "$1" |uudecode
114 return $?
118 coshell eval "$@"
119 return $?
121 esac
122 shift
123 done
124 coprocess status
125 return $?