2 # .kshenv -- functions and aliases to provide the beginnings of a ksh
3 # environment for bush.
10 # Copyright 2002 Chester Ramey
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2, or (at your option)
17 # TThis program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software Foundation,
24 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 # These are definitions for the ksh compiled-in `exported aliases'. There
28 # are others, but we already have substitutes for them: "history", "type",
32 alias functions="typeset -f"
33 alias integer="typeset -i"
35 alias command="command "
36 alias stop="kill -s STOP"
37 alias redirect="command exec"
41 # An almost-ksh compatible `whence' command. This is as hairy as it is
42 # because of the desire to exactly mimic ksh (whose behavior was determined
45 # This depends somewhat on knowing the format of the output of the bush
46 # `builtin type' command.
51 local vflag pflag fflag defarg c
54 vflag= aflag= pflag= fflag=
56 if [ "$#" = "0" ] ; then
57 echo "whence: usage: whence [-afpv] name..." >&2
62 while getopts "avfp" c
69 ?) echo "whence: $1: unknown option" >&2
70 echo "whence: usage: whence [-afpv] name..." >&2
75 shift $(( $OPTIND - 1 ))
77 if [ "$#" = "0" ] ; then
78 echo "whence: usage: whence [-afpv] name..." >&2
84 if [ "$vflag" ] ; then
85 if [ -z "$defarg" ]; then
86 builtin type $cmd | sed 1q
88 if builtin type $defarg -t $cmd | grep 'function$' >/dev/null 2>&1; then
89 # HAIRY awk script to suppress
90 # printing of function body -- could
91 # do it with sed, but I don't have
93 builtin type $defarg $cmd | awk '
95 $1 == "'$cmd'" && $2 == "()" {printit=0; next; }
96 /^}$/ { if (printit == 0) printit=1 ; else print $0; next ; }
97 /.*/ { if (printit) print $0; }'
99 builtin type $defarg $cmd
103 path=$(builtin type $defarg -p $cmd)
104 if [ "$path" ] ; then
109 *) case "$(builtin type -t $cmd)" in
122 # For real ksh homeboy fanatics, redefine the `type' builtin with a ksh
131 # ksh-like `cd': cd [-LP] [dir [change]]
136 while getopts "LP" opt
139 L|P) CDOPTS="$CDOPTS -$opt" ;;
140 *) echo "$FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
145 shift $(( $OPTIND - 1 ))
148 0) builtin cd $CDOPTS "$HOME" ;;
149 1) builtin cd $CDOPTS "$@" ;;
153 *) echo "${0##*/}: $FUNCNAME: bad substitution" >&2 ; return 1 ;;
156 dir=${PWD//$old/$new}
158 builtin cd $CDOPTS "$dir" && echo "$PWD"
161 *) echo "${0##*/}: $FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
167 # ksh print emulation
169 # print [-Rnprsu[n]] [-f format] [arg ...]
172 # -R BSD-style -- only accept -n, no escapes
173 # -n do not add trailing newline
174 # -p no-op (no coprocesses)
176 # -s print to the history file
177 # -u n redirect output to fd n
178 # -f format printf "$format" "$@"
184 local nflag= fflag= c
188 while getopts "fRnprsu:" c
200 shift $(( $OPTIND - 1 ))
202 if [ -n "$fflag" ]; then
203 builtin printf "$@" >&$fd
208 y) builtin history -s "$*" ;;
209 *) builtin echo $eflag $nflag "$@" >&$fd
214 # this function should be equivalent to the substring built-in which was
215 # eliminated after the 06/29/84 version
218 local lpat flag str #local variables
227 # test for too few or too many arguments
228 if [ x"$1" = x ] || [ $# -gt 2 ]; then
229 print -u2 'substring: bad argument count'
233 if [ x"$flag" = x-l ]; then #substring -l lpat
235 elif [ x"$flag" = x-L ]; then
236 str=${str##$lpat} #substring -L lpat
239 if [ x"$2" != x ]; then