2 # .kshenv -- functions and aliases to provide the beginnings of a ksh
3 # environment for bash.
9 # These are definitions for the ksh compiled-in `exported aliases'. There
10 # are others, but we already have substitutes for them: "history", "type",
14 alias functions="typeset -f"
15 alias integer="typeset -i"
17 alias command="command "
18 alias stop="kill -s STOP"
19 alias redirect="command exec"
23 # An almost-ksh compatible `whence' command. This is as hairy as it is
24 # because of the desire to exactly mimic ksh (whose behavior was determined
27 # This depends somewhat on knowing the format of the output of the bash
28 # `builtin type' command.
33 local vflag pflag fflag defarg c
36 vflag= aflag= pflag= fflag=
38 if [ "$#" = "0" ] ; then
39 echo "whence: usage: whence [-afpv] name..." >&2
44 while getopts "avfp" c
51 ?) echo "whence: $1: unknown option" >&2
52 echo "whence: usage: whence [-afpv] name..." >&2
57 shift $(( $OPTIND - 1 ))
59 if [ "$#" = "0" ] ; then
60 echo "whence: usage: whence [-afpv] name..." >&2
66 if [ "$vflag" ] ; then
67 if [ -z "$defarg" ]; then
68 builtin type $cmd | sed 1q
70 if builtin type $defarg -t $cmd | grep 'function$' >/dev/null 2>&1; then
71 # HAIRY awk script to suppress
72 # printing of function body -- could
73 # do it with sed, but I don't have
75 builtin type $defarg $cmd | awk '
77 $1 == "'$cmd'" && $2 == "()" {printit=0; next; }
78 /^}$/ { if (printit == 0) printit=1 ; else print $0; next ; }
79 /.*/ { if (printit) print $0; }'
81 builtin type $defarg $cmd
85 path=$(builtin type $defarg -p $cmd)
91 *) case "$(builtin type -t $cmd)" in
104 # For real ksh homeboy fanatics, redefine the `type' builtin with a ksh
113 # ksh-like `cd': cd [-LP] [dir [change]]
118 while getopts "LP" opt
121 L|P) CDOPTS="$CDOPTS -$opt" ;;
122 *) echo "$FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
127 shift $(( $OPTIND - 1 ))
130 0) builtin cd $CDOPTS "$HOME" ;;
131 1) builtin cd $CDOPTS "$@" ;;
135 *) echo "${0##*/}: $FUNCNAME: bad substitution" >&2 ; return 1 ;;
138 dir=${PWD//$old/$new}
140 builtin cd $CDOPTS "$dir" && echo "$PWD"
143 *) echo "${0##*/}: $FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
149 # ksh print emulation
151 # print [-Rnprsu[n]] [-f format] [arg ...]
154 # -R BSD-style -- only accept -n, no escapes
155 # -n do not add trailing newline
156 # -p no-op (no coprocesses)
158 # -s print to the history file
159 # -u n redirect output to fd n
160 # -f format printf "$format" "$@"
166 local nflag= fflag= c
170 while getopts "fRnprsu:" c
182 shift $(( $OPTIND - 1 ))
184 if [ -n "$fflag" ]; then
185 builtin printf "$@" >&$fd
190 y) builtin history -s "$*" ;;
191 *) builtin echo $eflag $nflag "$@" >&$fd
196 # this function should be equivalent to the substring built-in which was
197 # eliminated after the 06/29/84 version
200 local lpat flag str #local variables
209 # test for too few or too many arguments
210 if [ x"$1" = x ] || [ $# -gt 2 ]; then
211 print -u2 'substring: bad argument count'
215 if [ x"$flag" = x-l ]; then #substring -l lpat
217 elif [ x"$flag" = x-L ]; then
218 str=${str##$lpat} #substring -L lpat
221 if [ x"$2" != x ]; then