summaryrefslogtreecommitdiff
path: root/examples/functions/coshell.bash
diff options
context:
space:
mode:
authorAn-Cheng Huang <ancheng@vyatta.com>2007-11-12 13:06:02 -0800
committerAn-Cheng Huang <ancheng@vyatta.com>2007-11-12 13:06:02 -0800
commitb7fc9e0f6d6105ba2203f219743d4b269415e84b (patch)
treeef6586dfc62798c2b17487b443864699aca55f31 /examples/functions/coshell.bash
downloadvyatta-bash-b7fc9e0f6d6105ba2203f219743d4b269415e84b.tar.gz
vyatta-bash-b7fc9e0f6d6105ba2203f219743d4b269415e84b.zip
initial import from bash_3.1dfsg.orig.tar.gz
Diffstat (limited to 'examples/functions/coshell.bash')
-rw-r--r--examples/functions/coshell.bash127
1 files changed, 127 insertions, 0 deletions
diff --git a/examples/functions/coshell.bash b/examples/functions/coshell.bash
new file mode 100644
index 0000000..dc177b3
--- /dev/null
+++ b/examples/functions/coshell.bash
@@ -0,0 +1,127 @@
+# vi:set sts=2 sw=2 ai:
+#
+# coshell.bash - Control shell coprocesses (see coprocess.bash).
+#
+
+function coshell ()
+{
+ while (( $# > 0 )) ; do
+ case "$1" in
+ #
+ # coshell open
+ #
+ o|op|ope|open)
+ shift
+ coprocess open "$@"
+ local ret=$?
+
+ # This should eat any ssh error messages or what not.
+ coshell eval : >/dev/null 2>&1
+ return $ret
+ ;;
+
+ #
+ # coshell close
+ #
+ c|cl|clo|close)
+ shift
+ coprocess close "$@"
+ return $?
+ ;;
+
+ #
+ # coshell eval
+ #
+ e|ev|eva|eval)
+ shift
+ local cookie=$RANDOM
+ if (( $# == 0 )) ; then
+ echo "coshell eval: no argumentsl" >&2
+ return 1
+ fi
+ if [ x$coprocess_pid = x ] ; then
+ echo "coshell eval: no active coshell" >&2
+ return 1
+ fi
+
+ coprocess print "$@"
+ coprocess print "coprocess_rc=\$?"
+ coprocess print "printf 'coprocess-$cookie----\n%d\n' \$coprocess_rc"
+ if [ x$coprocess_pid = x ] ; then
+ return 0
+ fi
+
+ local ol
+ while coprocess read ol ; do
+ case "$ol" in
+ *coprocess-$cookie----*)
+ ol="${ol%coprocess-$cookie----}"
+ echo -n "$ol"
+ break
+ ;;
+ esac
+ echo "$ol"
+ done
+ coprocess read ol
+ return $ol
+ ;;
+
+ #
+ # coshell sendfile
+ #
+ s|se|sen|send|sendf|sendfi|sendfil|sendfile)
+ shift
+ if (( $# != 2 )) ; then
+ echo "coshell sendfile: syntax is 'coshell sendfile SRC TARGET'" >&2
+ return 1
+ fi
+ if [ x$coprocess_pid = x ] ; then
+ echo "coshell sendfile: no active coshell" >&2
+ return 1
+ fi
+
+ local target=$2
+ if coshell test -d "$target" ; then
+ target="$target/${1##*/}"
+ fi
+
+ coprocess print "uudecode <<END_OF_FILE"
+ uuencode -m "$target" <$1 |coprocess print --stdin
+ coshell eval "END_OF_FILE"
+ return $?
+ ;;
+
+ #
+ # coshell getfile
+ #
+ g|ge|get|getf|getfi|getfil|getfile)
+ shift
+ if (( $# != 2 )) ; then
+ echo "coshell getfile: syntax is 'coshell getfile SRC TARGET'" >&2
+ return 1
+ fi
+ if [ x$coprocess_pid = x ] ; then
+ echo "coshell getfile: no active coshell" >&2
+ return 1
+ fi
+
+ local target=$2
+ if test -d "$target" ; then
+ target="$target/${1##*/}"
+ fi
+
+ coshell eval uuencode -m "$target" "<" "$1" |uudecode
+ return $?
+ ;;
+
+ *)
+ coshell eval "$@"
+ return $?
+ ;;
+ esac
+ shift
+ done
+ coprocess status
+ return $?
+}
+