diff options
author | Jari Aalto <jari.aalto@cante.net> | 2001-11-13 17:56:06 +0000 |
---|---|---|
committer | Jari Aalto <jari.aalto@cante.net> | 2009-09-12 16:46:54 +0000 |
commit | f73dda092b33638d2d5e9c35375f687a607b5403 (patch) | |
tree | f21584e70a444d6a1ecba0fb5e2cf79e8cce91db /examples/functions | |
parent | 28ef6c316f1aff914bb95ac09787a3c83c1815fd (diff) | |
download | android_external_bash-f73dda092b33638d2d5e9c35375f687a607b5403.tar.gz android_external_bash-f73dda092b33638d2d5e9c35375f687a607b5403.tar.bz2 android_external_bash-f73dda092b33638d2d5e9c35375f687a607b5403.zip |
Imported from ../bash-2.05a.tar.gz.
Diffstat (limited to 'examples/functions')
-rw-r--r-- | examples/functions/autoload | 26 | ||||
-rw-r--r-- | examples/functions/autoload.v2 | 2 | ||||
-rw-r--r-- | examples/functions/coproc.bash | 109 | ||||
-rw-r--r-- | examples/functions/coshell.README | 53 | ||||
-rw-r--r-- | examples/functions/coshell.bash | 127 |
5 files changed, 307 insertions, 10 deletions
diff --git a/examples/functions/autoload b/examples/functions/autoload index 206b012..a563a77 100644 --- a/examples/functions/autoload +++ b/examples/functions/autoload @@ -51,7 +51,7 @@ autoload() # yet defined, but we don't have enough information to do that here. # if [ $# -eq 0 ] ; then - echo "usage: autoload function [function...]" + echo "usage: autoload function [function...]" >&2 return 1 fi @@ -60,7 +60,7 @@ autoload() # if [ -z "$FPATH" ] ; then - echo autoload: FPATH not set + echo autoload: FPATH not set or null >&2 return 1 fi @@ -71,17 +71,25 @@ autoload() # The path splitting command is taken from Kernighan and Pike # - fp=$(echo $FPATH | sed 's/^:/.:/ - s/::/:.:/g - s/:$/:./ - s/:/ /g') +# fp=$(echo $FPATH | sed 's/^:/.:/ +# s/::/:.:/g +# s/:$/:./ +# s/:/ /g') + + # replaced with builtin mechanisms 2001 Oct 10 + + fp=${FPATH/#:/.:} + fp=${fp//::/:.:} + fp=${fp/%:/:.} + fp=${fp//:/ } for FUNC in $args ; do # # We're blowing away the arguments to autoload here... - # We have to; there are no arrays. + # We have to; there are no arrays (well, there are, but + # this doesn't use them yet). # - set $fp + set -- $fp while [ $# -ne 0 ] ; do if [ -f $1/$FUNC ] ; then @@ -91,7 +99,7 @@ autoload() done if [ $# -eq 0 ] ; then - echo "$FUNC: autoload function not found" + echo "$FUNC: autoload function not found" >&2 continue fi diff --git a/examples/functions/autoload.v2 b/examples/functions/autoload.v2 index 8041b84..e8f3433 100644 --- a/examples/functions/autoload.v2 +++ b/examples/functions/autoload.v2 @@ -18,7 +18,7 @@ # -p print in a format that can be reused as input # -u unset each function and remove it from the autoload list # -# The first cut of this was by Bill Trost, trost@reed.bitnet +# The first cut of this was by Bill Trost, trost@reed.edu # # Chet Ramey # chet@ins.CWRU.Edu diff --git a/examples/functions/coproc.bash b/examples/functions/coproc.bash new file mode 100644 index 0000000..d9175a1 --- /dev/null +++ b/examples/functions/coproc.bash @@ -0,0 +1,109 @@ +# coprocess.bash +# +# vi:set sts=2 sw=2 ai: +# + +coprocess_pid= + +# +# coprocess - Start, control, and end coprocesses. +# +function coprocess () +{ + while (( $# > 0 )) ; do + case "$1" in + # + # coprocess close + # + c|cl|clo|clos|close) + shift + exec 61>&- 62<&- + coprocess_pid= + if [ "$1" = "-SIGPIPE" ] ; then + # Only print message in an interactive shell + case "$-" in + *i*) + echo 'SIGPIPE' >&2 + ;; + esac + return 1 + fi + return 0 + ;; + + # + # coprocess open + # + o|op|ope|open) + shift + local fifo="/var/tmp/coprocess.$$.$RANDOM" + + local cmd="/bin/bash" + if (( $# > 0 )) ; then + cmd="$@" + fi + + mkfifo "$fifo.in" || return $? + mkfifo "$fifo.out" || { + ret=$? + rm -f "$fifo.in" + return $? + } + + ( "$@" <$fifo.in >$fifo.out ; rm -f "$fifo.in" "$fifo.out" ) & + coprocess_pid=$! + exec 61>$fifo.in 62<$fifo.out + return 0 + ;; + + # + # coprocess print - write to the coprocess + # + p|pr|pri|prin|print) + shift + local old_trap=$(trap -p SIGPIPE) + trap 'coprocess close -SIGPIPE' SIGPIPE + if [ $# -eq 1 -a "$1" = "--stdin" ] ; then + cat >&61 + else + echo "$@" >&61 + fi + local ret=$? + eval "$old_trap" + return $ret + ;; + + # + # coprocess read - read from the coprocess + # + r|re|rea|read) + shift + local old_trap=$(trap -p SIGPIPE) + trap '_coprocess_close -SIGPIPE' SIGPIPE + builtin read "$@" <&62 + local ret=$? + eval "$old_trap" + return $ret + ;; + + s|st|sta|stat|statu|status) + if [ -z "$coprocess_pid" ] ; then + echo 'no active coprocess' + return 1 + else + echo " coprocess is active [$coprocess_pid]" + return 0 + fi + ;; + + *) + coprocess print "$@" + return $? + ;; + esac + shift + done + coprocess status + return $? +} + diff --git a/examples/functions/coshell.README b/examples/functions/coshell.README new file mode 100644 index 0000000..9675cda --- /dev/null +++ b/examples/functions/coshell.README @@ -0,0 +1,53 @@ +Date: Fri, 21 Sep 2001 14:50:29 -0400 +From: "Jason M. Felice" <jfelice@cronosys.com> +To: bash-maintainers@gnu.org, chet@po.cwru.edu +Subject: Bash co-processes functions +Message-ID: <20010921145029.A6093@argo.eraserhead.net> +Mime-Version: 1.0 + +Attached to this message you will find coprocess.bash and coshell.bash. +Here's a brief synopsis of use: + +coprocess open telnet localhost +while coprocess read il ; do + echo "$il" + case "$il" in + *ogin:*) + coprocess print 'user' + ;; + *ord:*) + echo 'pass' |coprocess print --stdin + ;; + *$ *) + coprocess print 'exit' + break + ;; + esac +done +coprocess close + +And here's an example of the coshell function: + +coshell open ssh -l root otherbox +coshell eval hostname +coshell ls -l +if coshell test -d /tmp ; then echo 'otherbox has a /tmp!' ; fi + +coshell sendfile /var/lib/upgrade.rpm /tmp/test.rpm || exit $? +coshell eval rpm -ivh /tmp/test.rpm || exit $? +coshell eval rm -f /tmp/test.rpm || exit $? +coshell close +exit 0 + +There are a few minor issues that I'd like to work out, but it works well +enough for me ;-) The issues are: + +- Shell quoting issue with 'coshell eval' commands - need to somehow + re-quote words. +- Interactive commands hang 'coshell eval', tried redirecting in </dev/null + to executed command, but it caused strange shell exit problems. +- Some way to copy stdin from local coshell eval to remote shell. Probably + logically impossible, but would be wonderfully useful. + +I'm using it for writing scripts to publish websites and other scripts to +co-located servers. 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 $? +} + |