diff options
Diffstat (limited to 'examples/functions/kshenv')
-rw-r--r-- | examples/functions/kshenv | 88 |
1 files changed, 64 insertions, 24 deletions
diff --git a/examples/functions/kshenv b/examples/functions/kshenv index 636405e..35cc212 100644 --- a/examples/functions/kshenv +++ b/examples/functions/kshenv @@ -10,12 +10,13 @@ # are others, but we already have substitutes for them: "history", "type", # and "hash". # -alias r="fc -e -" +alias r="fc -s" alias functions="typeset -f" alias integer="typeset -i" alias nohup="nohup " -alias true=":" -alias false="let 0" +alias command="command " +alias stop="kill -s STOP" +alias redirect="command exec" alias hist="fc" # @@ -29,38 +30,65 @@ alias hist="fc" whence() { - local vflag + local vflag pflag fflag defarg c local path - vflag= + vflag= aflag= pflag= fflag= path= if [ "$#" = "0" ] ; then - echo "whence: argument expected" - return 1 + echo "whence: usage: whence [-afpv] name..." >&2 + return 2 fi - case "$1" in - -v) vflag=1 ; shift 1 ;; - -*) echo "whence: bad option: $1" ; return 1 ;; - *) ;; - esac + + OPTIND=1 + while getopts "avfp" c + do + case "$c" in + a) defarg=-a ;; + f) fflag=1 ;; # no-op + p) pflag=1 ;; + v) vflag=1 ;; + ?) echo "whence: $1: unknown option" >&2 + echo "whence: usage: whence [-afpv] name..." >&2 + return 2 ;; + esac + done + + shift $(( $OPTIND - 1 )) if [ "$#" = "0" ] ; then - echo "whence: bad argument count" - return 1 + echo "whence: usage: whence [-afpv] name..." >&2 + return 2 fi for cmd do if [ "$vflag" ] ; then - echo $(builtin type $cmd | sed 1q) + if [ -z "$defarg" ]; then + builtin type $cmd | sed 1q + else + if builtin type $defarg -t $cmd | grep 'function$' >/dev/null 2>&1; then + # HAIRY awk script to suppress + # printing of function body -- could + # do it with sed, but I don't have + # that kind of time + builtin type $defarg $cmd | awk ' +BEGIN {printit = 1;} +$1 == "'$cmd'" && $2 == "()" {printit=0; next; } +/^}$/ { if (printit == 0) printit=1 ; else print $0; next ; } +/.*/ { if (printit) print $0; }' + else + builtin type $defarg $cmd + fi + fi else - path=$(builtin type -path $cmd) + path=$(builtin type $defarg -p $cmd) if [ "$path" ] ; then echo $path else case "$cmd" in /*) echo "" ;; - *) case "$(builtin type -type $cmd)" in + *) case "$(builtin type -t $cmd)" in "") echo "" ;; *) echo "$cmd" ;; esac @@ -88,22 +116,27 @@ cd() 1) builtin cd "$@" ;; 2) old="$1" new="$2" - dir=$(echo "$PWD" | sed "s:$old:$new:g") + # dir=$(echo "$PWD" | sed "s:$old:$new:g") + dir=${PWD//$old/$new} case "$dir" in - "$PWD") echo "bash: cd: bad substitution" >&2 ; return 1 ;; + "$PWD") case "$PWD" in + *$old*) ;; + *) echo "$FUNCNAME: bad substitution" >&2 ; return 1 ;; + esac ;; *) echo "$dir" builtin cd "$dir" ;; esac ;; - *) echo "cd: wrong arg count" >&2 ; return 1 ;; + *) echo "$FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2 + return 1 ;; esac } # # ksh print emulation # -# print [-Rnprsu[n]] [arg ...] +# print [-Rnprsu[n]] [-f format] [arg ...] # # - end of options # -R BSD-style -- only accept -n, no escapes @@ -112,27 +145,34 @@ cd() # -r no escapes # -s print to the history file # -u n redirect output to fd n +# -f format printf "$format" "$@" # print() { local eflag=-e - local nflag= + local nflag= fflag= c local fd=1 OPTIND=1 - while getopts "Rnprsu:" c + while getopts "fRnprsu:" c do case $c in R) eflag= ;; r) eflag= ;; n) nflag=-n ;; s) sflag=y ;; + f) fflag=y ;; u) fd=$OPTARG ;; p) ;; esac done - shift $[ $OPTIND - 1 ] + shift $(( $OPTIND - 1 )) + + if [ -n "$fflag" ]; then + builtin printf "$@" >&$fd + return + fi case "$sflag" in y) builtin history -s "$*" ;; |