i-bash/examples/functions/kshenv

229 lines
4.2 KiB
Text
Raw Normal View History

1996-08-26 18:22:31 +00:00
#
# .kshenv -- functions and aliases to provide the beginnings of a ksh
# environment for bash.
#
# Chet Ramey
# chet@ins.CWRU.Edu
#
#
# These are definitions for the ksh compiled-in `exported aliases'. There
# are others, but we already have substitutes for them: "history", "type",
# and "hash".
#
2000-03-17 21:46:59 +00:00
alias r="fc -s"
1996-08-26 18:22:31 +00:00
alias functions="typeset -f"
alias integer="typeset -i"
alias nohup="nohup "
2000-03-17 21:46:59 +00:00
alias command="command "
alias stop="kill -s STOP"
alias redirect="command exec"
1996-08-26 18:22:31 +00:00
alias hist="fc"
#
# An almost-ksh compatible `whence' command. This is as hairy as it is
# because of the desire to exactly mimic ksh (whose behavior was determined
# empirically).
#
# This depends somewhat on knowing the format of the output of the bash
# `builtin type' command.
#
whence()
{
2000-03-17 21:46:59 +00:00
local vflag pflag fflag defarg c
1996-08-26 18:22:31 +00:00
local path
2000-03-17 21:46:59 +00:00
vflag= aflag= pflag= fflag=
1996-08-26 18:22:31 +00:00
path=
if [ "$#" = "0" ] ; then
2000-03-17 21:46:59 +00:00
echo "whence: usage: whence [-afpv] name..." >&2
return 2
1996-08-26 18:22:31 +00:00
fi
2000-03-17 21:46:59 +00:00
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 ))
1996-08-26 18:22:31 +00:00
if [ "$#" = "0" ] ; then
2000-03-17 21:46:59 +00:00
echo "whence: usage: whence [-afpv] name..." >&2
return 2
1996-08-26 18:22:31 +00:00
fi
for cmd
do
if [ "$vflag" ] ; then
2000-03-17 21:46:59 +00:00
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
1996-08-26 18:22:31 +00:00
else
2000-03-17 21:46:59 +00:00
path=$(builtin type $defarg -p $cmd)
1996-08-26 18:22:31 +00:00
if [ "$path" ] ; then
echo $path
else
case "$cmd" in
1996-12-23 17:02:34 +00:00
/*) echo "" ;;
2000-03-17 21:46:59 +00:00
*) case "$(builtin type -t $cmd)" in
1996-12-23 17:02:34 +00:00
"") echo "" ;;
*) echo "$cmd" ;;
esac
;;
1996-08-26 18:22:31 +00:00
esac
fi
fi
done
return 0
}
#
# For real ksh homeboy fanatics, redefine the `type' builtin with a ksh
# version.
#
#type()
#{
# whence -v "$*"
#}
2001-04-06 19:14:31 +00:00
#
# ksh-like `cd': cd [-LP] [dir [change]]
#
1996-08-26 18:22:31 +00:00
cd()
{
2001-04-06 19:14:31 +00:00
OPTIND=1
while getopts "LP" opt
do
case $opt in
L|P) CDOPTS="$CDOPTS -$opt" ;;
*) echo "$FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
return 2;;
esac
done
shift $(( $OPTIND - 1 ))
1996-08-26 18:22:31 +00:00
case $# in
2001-04-06 19:14:31 +00:00
0) builtin cd $CDOPTS "$HOME" ;;
1) builtin cd $CDOPTS "$@" ;;
2) old="$1" new="$2"
case "$PWD" in
*$old*) ;;
*) echo "${0##*/}: $FUNCNAME: bad substitution" >&2 ; return 1 ;;
1996-08-26 18:22:31 +00:00
esac
2001-04-06 19:14:31 +00:00
dir=${PWD//$old/$new}
builtin cd $CDOPTS "$dir" && echo "$PWD"
1996-08-26 18:22:31 +00:00
;;
2001-04-06 19:14:31 +00:00
*) echo "${0##*/}: $FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
return 2 ;;
1996-08-26 18:22:31 +00:00
esac
}
#
# ksh print emulation
#
2000-03-17 21:46:59 +00:00
# print [-Rnprsu[n]] [-f format] [arg ...]
1996-08-26 18:22:31 +00:00
#
# - end of options
# -R BSD-style -- only accept -n, no escapes
# -n do not add trailing newline
# -p no-op (no coprocesses)
# -r no escapes
1996-12-23 17:02:34 +00:00
# -s print to the history file
1996-08-26 18:22:31 +00:00
# -u n redirect output to fd n
2000-03-17 21:46:59 +00:00
# -f format printf "$format" "$@"
1996-08-26 18:22:31 +00:00
#
print()
{
local eflag=-e
2000-03-17 21:46:59 +00:00
local nflag= fflag= c
1996-08-26 18:22:31 +00:00
local fd=1
OPTIND=1
2000-03-17 21:46:59 +00:00
while getopts "fRnprsu:" c
1996-08-26 18:22:31 +00:00
do
case $c in
1996-12-23 17:02:34 +00:00
R) eflag= ;;
r) eflag= ;;
n) nflag=-n ;;
s) sflag=y ;;
2000-03-17 21:46:59 +00:00
f) fflag=y ;;
1996-12-23 17:02:34 +00:00
u) fd=$OPTARG ;;
p) ;;
1996-08-26 18:22:31 +00:00
esac
done
2000-03-17 21:46:59 +00:00
shift $(( $OPTIND - 1 ))
if [ -n "$fflag" ]; then
builtin printf "$@" >&$fd
return
fi
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
case "$sflag" in
y) builtin history -s "$*" ;;
*) builtin echo $eflag $nflag "$@" >&$fd
esac
1996-08-26 18:22:31 +00:00
}
# substring function
# this function should be equivalent to the substring built-in which was
# eliminated after the 06/29/84 version
substring ()
{
local lpat flag str #local variables
set -f
case $1 in
-l|-L)
flag=$1
lpat=$2
shift 2
;;
esac
# test for too few or too many arguments
2004-07-27 13:29:18 +00:00
if [ x"$1" = x ] || [ $# -gt 2 ]; then
1996-08-26 18:22:31 +00:00
print -u2 'substring: bad argument count'
return 1
fi
str=$1
if [ x"$flag" = x-l ]; then #substring -l lpat
str=${str#$lpat}
elif [ x"$flag" = x-L ]; then
str=${str##$lpat} #substring -L lpat
fi
if [ x"$2" != x ]; then
echo ${str%$2}
else
echo $str
fi
return 0
}