Upload Tizen:Base source
[external/bash.git] / examples / functions / coshell.bash
1 # vi:set sts=2 sw=2 ai:
2 #
3 # coshell.bash - Control shell coprocesses (see coprocess.bash).
4 #
5
6 function coshell ()
7 {
8   while (( $# > 0 )) ; do
9     case "$1" in
10       #
11       # coshell open
12       #
13       o|op|ope|open)
14         shift
15         coprocess open "$@"
16         local ret=$?
17
18         # This should eat any ssh error messages or what not.
19         coshell eval : >/dev/null 2>&1
20         return $ret
21         ;;
22
23       #
24       # coshell close
25       #
26       c|cl|clo|close)
27         shift
28         coprocess close "$@"
29         return $?
30         ;;
31
32       #
33       # coshell eval
34       #
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
41         fi
42         if [ x$coprocess_pid = x ] ; then
43           echo "coshell eval: no active coshell" >&2
44           return 1
45         fi
46
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
52         fi
53
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
61               ;;
62           esac
63           echo "$ol"
64         done
65         coprocess read ol
66         return $ol
67         ;;
68
69       #
70       # coshell sendfile
71       #
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
77         fi
78         if [ x$coprocess_pid = x ] ; then
79           echo "coshell sendfile: no active coshell" >&2
80           return 1
81         fi
82
83         local target=$2
84         if coshell test -d "$target" ; then
85           target="$target/${1##*/}" 
86         fi
87
88         coprocess print "uudecode <<END_OF_FILE"
89         uuencode -m "$target" <$1 |coprocess print --stdin
90         coshell eval "END_OF_FILE"
91         return $?
92         ;;
93
94       #
95       # coshell getfile
96       #
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
102         fi
103         if [ x$coprocess_pid = x ] ; then
104           echo "coshell getfile: no active coshell" >&2
105           return 1
106         fi
107
108         local target=$2
109         if test -d "$target" ; then
110           target="$target/${1##*/}" 
111         fi
112
113         coshell eval uuencode -m "$target" "<" "$1" |uudecode
114         return $?
115         ;;
116
117       *)
118         coshell eval "$@"
119         return $?
120         ;;
121     esac
122     shift
123   done
124   coprocess status
125   return $?
126 }
127