diff options
author | Jari Aalto <jari.aalto@cante.net> | 1999-02-19 17:11:39 +0000 |
---|---|---|
committer | Jari Aalto <jari.aalto@cante.net> | 2009-09-12 16:46:52 +0000 |
commit | b72432fdcc59300c6fe7c9d6c8a31ad3447933f5 (patch) | |
tree | b9899162338c2ff3fd83a8aef8831cb119e85cd7 /examples/functions | |
parent | bc4cd23ce958feda898c618215f94d8a4e8f4ffa (diff) | |
download | bash-b72432fdcc59300c6fe7c9d6c8a31ad3447933f5.tar.gz |
Imported from ../bash-2.03.tar.gz.
Diffstat (limited to 'examples/functions')
-rw-r--r-- | examples/functions/autoload.v3 | 125 | ||||
-rw-r--r-- | examples/functions/inetaddr | 18 | ||||
-rw-r--r-- | examples/functions/lowercase | 30 | ||||
-rw-r--r-- | examples/functions/repeat3 | 12 | ||||
-rw-r--r-- | examples/functions/seq2 | 37 | ||||
-rw-r--r-- | examples/functions/which | 44 |
6 files changed, 250 insertions, 16 deletions
diff --git a/examples/functions/autoload.v3 b/examples/functions/autoload.v3 new file mode 100644 index 00000000..a82ffe9d --- /dev/null +++ b/examples/functions/autoload.v3 @@ -0,0 +1,125 @@ +#From: Mark Kennedy <mtk@ny.ubs.com> +#Message-ID: <35E2B899.63A02DF5@ny.ubs.com> +#Date: Tue, 25 Aug 1998 09:14:01 -0400 +#To: chet@nike.ins.cwru.edu +#Subject: a newer version of the ksh-style 'autoload' + +#enclosed you'll find 'autoload.v3', a version of the autoloader +#that emulates the ksh semantics of delaying the resolution (and loading) of the function +#until its first use. i took the liberty of simplifying the code a bit although it still uses the +#same functional breakdown. i recently went through the exercise of converting +#my ksh-based environment to bash (a very, very pleasant experience) +#and this popped out. + +# the psuedo-ksh autoloader. + +# The first cut of this was by Bill Trost, trost@reed.bitnet. +# The second cut came from Chet Ramey, chet@ins.CWRU.Edu +# The third cut came from Mark Kennedy, mtk@ny.ubs.com. 1998/08/25 + +unset _AUTOLOADS + +_aload() +{ + local func + for func; do + eval $func '() + { + local f=$(_autoload_resolve '$func') + if [[ $f ]]; then + . $f + '$func' "$@" + return $? + else + return 1 + fi + }' + _autoload_addlist $func + done +} + +_autoload_addlist() +{ + local func + + for func in ${_AUTOLOADS[@]}; do + [[ $func = "$1" ]] && return + done + + _AUTOLOADS[${#_AUTOLOADS[@]}]=$1 +} + +_autoload_dump() +{ + local func + + for func in ${_AUTOLOADS[@]}; do + [[ $1 ]] && echo -n "autoload " + echo $func + done +} + +_autoload_remove_one() +{ + local func + local -a NEW_AUTOLOADS + + for func in ${_AUTOLOADS[@]}; do + [[ $func != "$1" ]] && NEW_AUTOLOADS[${#NEW_AUTOLOADS[@]}]=$func + done + + _AUTOLOADS=( ${NEW_AUTOLOADS[@]} ) +} + +_autoload_remove() +{ + local victim func + + for victim; do + for func in ${_AUTOLOADS[@]}; do + [[ $victim = "$func" ]] && unset -f $func && continue 2 + done + echo "autoload: $func: not an autoloaded function" >&2 + done + + for func; do + _autoload_remove_one $func + done +} + +_autoload_resolve() +{ + if [[ ! "$FPATH" ]]; then + echo "autoload: FPATH not set or null" >&2 + return + fi + + local p + + for p in $( (IFS=':'; set -- ${FPATH}; echo "$@") ); do + p=${p:-.} + if [ -f $p/$1 ]; then echo $p/$1; return; fi + done + + echo "autoload: $1: function source file not found" >&2 +} + +autoload() +{ + if (( $# == 0 )) ; then _autoload_dump; return; fi + + local opt OPTIND + + while getopts pu opt + do + case $opt in + p) _autoload_dump printable; return;; + u) shift $((OPTIND-1)); _autoload_remove "$@"; return;; + *) echo "autoload: usage: autoload [-pu] [function ...]" >&2; return;; + esac + done + + shift $(($OPTIND-1)) + + _aload "$@" +} diff --git a/examples/functions/inetaddr b/examples/functions/inetaddr index 776b204a..08086aea 100644 --- a/examples/functions/inetaddr +++ b/examples/functions/inetaddr @@ -23,6 +23,17 @@ inet2hex () hex2inet () { local x1 x2 x3 x4 + local rev + + OPTIND=1 + while getopts "r" o + do + case "$o" in + r) rev=true;; + *) echo "hex2inet: usage: hex2inet [0x]XXXXXXXX" >&2 ; exit 2;; + esac + done + shift $(( $OPTIND - 1 )) case "$1" in 0x*) h=${1#??} ;; @@ -40,5 +51,10 @@ hex2inet () x3=$(( 0x${h:4:2} )) x4=$(( 0x${h:6:2} )) - printf "%d.%d.%d.%d\n" $x1 $x2 $x3 $x4 + if [ -z "$rev" ] ; then + printf "%d.%d.%d.%d\n" $x1 $x2 $x3 $x4 + else + printf "%d.%d.%d.%d\n" $x4 $x3 $x2 $x1 + fi + return 0 } diff --git a/examples/functions/lowercase b/examples/functions/lowercase index 5f4fc9d4..a0649f84 100644 --- a/examples/functions/lowercase +++ b/examples/functions/lowercase @@ -8,19 +8,19 @@ lowercase() { -for file; do - filename=${file##*/} - case "$filename" in - */*) dirname=${file%/*} ;; - *) dirname=.;; - esac - nf=$(echo $filename | tr A-Z a-z) - newname="${dirname}/${nf}" - if [ "$nf" != "$filename" ]; then - mv "$file" "$newname" - echo "$0: $file -> $newname" - else - echo "$0: $file not changed." - fi -done + for file; do + filename=${file##*/} + case "$filename" in + */*) dirname=${file%/*} ;; + *) dirname=.;; + esac + nf=$(echo $filename | tr A-Z a-z) + newname="${dirname}/${nf}" + if [ "$nf" != "$filename" ]; then + mv "$file" "$newname" + echo "lowercase: $file -> $newname" + else + echo "lowercase: $file not changed." + fi + done } diff --git a/examples/functions/repeat3 b/examples/functions/repeat3 new file mode 100644 index 00000000..65048bf9 --- /dev/null +++ b/examples/functions/repeat3 @@ -0,0 +1,12 @@ +# From psamuels@jake.niar.twsu.edu (Peter Samuelson) +# posted to usenet, Message-ID: <6rtp8j$2a0$1@jake.niar.twsu.edu> + +repeat () +{ + local i max; # note that you can use \$i in the command string + max=$1; shift; + + i=1; while ((i <= max)); do + eval "$@"; ((i = i + 1)); + done; +} diff --git a/examples/functions/seq2 b/examples/functions/seq2 new file mode 100644 index 00000000..c3ad95c4 --- /dev/null +++ b/examples/functions/seq2 @@ -0,0 +1,37 @@ +# Generate a sequence from m to n, m defaults to 1. + +seq () +{ + declare -i lo hi i # makes local + local _SEQ INIT COMPARE STEP + + case "$1" in + -r) INIT='i=$hi _SEQ=""' COMPARE='let "i >= $lo"' STEP='let i-=1' ; shift ;; + *) INIT='i=$lo _SEQ=""' COMPARE='let "i <= $hi"' STEP='let i+=1' ;; + esac + + case $# in + 1) lo=1 hi="$1" ;; + 2) lo=$1 hi=$2 ;; + *) echo seq: usage: seq [-r] [low] high 1>&2 ; return 2 ;; + esac + + # equivalent to the as-yet-unimplemented + # for (( "$INIT" ; "$COMPARE" ; "$STEP" )); do _SEQ="${_SEQ}$i "; done + eval "$INIT" + while eval "$COMPARE"; do + _SEQ="${_SEQ}$i " + eval "$STEP" + done + echo "${_SEQ# }" + return 0 +} + +# like the APL `iota' function (or at least how I remember it :-) +iota() +{ + case $# in + 1) seq 1 "$1"; return $?;; + *) echo "iota: usage: iota high" 1>&2; return 2;; + esac +} diff --git a/examples/functions/which b/examples/functions/which new file mode 100644 index 00000000..54ace771 --- /dev/null +++ b/examples/functions/which @@ -0,0 +1,44 @@ +# +# which - emulation of `which' as it appears in FreeBSD +# +# usage: which [-as] command [command...] +# + +which() +{ + local aflag sflag ES a + + OPTIND=1 + while builtin getopts as opt ; do + case "$opt" in + a) aflag=-a ;; + s) sflag=1 ;; + ?) echo "which: usage: which [-as] command [command ...]" >&2 + exit 2 ;; + esac + done + + (( $OPTIND > 1 )) && shift $(( $OPTIND - 1 )) + + # without command arguments, exit with status 1 + ES=1 + + # exit status is 0 if all commands are found, 1 if any are not found + for command; do + # if $command is a function, make sure we add -a so type + # will look in $PATH after finding the function + a=$aflag + case "$(builtin type -t $command)" in + "function") a=-a;; + esac + + if [ -n "$sflag" ]; then + builtin type -p $a $command >/dev/null 2>&1 + else + builtin type -p $a $command + fi + ES=$? + done + + return $ES +} |