blob: 9faba08cd71f01ba54aa30675f556339428000ad [file] [log] [blame]
Jari Aalto726f6381996-08-26 18:22:31 +00001#
2# .kshenv -- functions and aliases to provide the beginnings of a ksh
3# environment for bash.
4#
5# Chet Ramey
6# chet@ins.CWRU.Edu
7#
Chet Rameyac50fba2014-02-26 09:36:43 -05008
9#
10# Copyright 2002 Chester Ramey
11#
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)
15# any later version.
16#
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.
21#
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.
25
Jari Aalto726f6381996-08-26 18:22:31 +000026#
27# These are definitions for the ksh compiled-in `exported aliases'. There
28# are others, but we already have substitutes for them: "history", "type",
29# and "hash".
30#
Jari Aaltobb706242000-03-17 21:46:59 +000031alias r="fc -s"
Jari Aalto726f6381996-08-26 18:22:31 +000032alias functions="typeset -f"
33alias integer="typeset -i"
34alias nohup="nohup "
Jari Aaltobb706242000-03-17 21:46:59 +000035alias command="command "
36alias stop="kill -s STOP"
37alias redirect="command exec"
Jari Aalto726f6381996-08-26 18:22:31 +000038alias hist="fc"
39
40#
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
43# empirically).
44#
45# This depends somewhat on knowing the format of the output of the bash
46# `builtin type' command.
47#
48
49whence()
50{
Jari Aaltobb706242000-03-17 21:46:59 +000051 local vflag pflag fflag defarg c
Jari Aalto726f6381996-08-26 18:22:31 +000052 local path
53
Jari Aaltobb706242000-03-17 21:46:59 +000054 vflag= aflag= pflag= fflag=
Jari Aalto726f6381996-08-26 18:22:31 +000055 path=
56 if [ "$#" = "0" ] ; then
Jari Aaltobb706242000-03-17 21:46:59 +000057 echo "whence: usage: whence [-afpv] name..." >&2
58 return 2
Jari Aalto726f6381996-08-26 18:22:31 +000059 fi
Jari Aaltobb706242000-03-17 21:46:59 +000060
61 OPTIND=1
62 while getopts "avfp" c
63 do
64 case "$c" in
65 a) defarg=-a ;;
66 f) fflag=1 ;; # no-op
67 p) pflag=1 ;;
68 v) vflag=1 ;;
69 ?) echo "whence: $1: unknown option" >&2
70 echo "whence: usage: whence [-afpv] name..." >&2
71 return 2 ;;
72 esac
73 done
74
75 shift $(( $OPTIND - 1 ))
Jari Aalto726f6381996-08-26 18:22:31 +000076
77 if [ "$#" = "0" ] ; then
Jari Aaltobb706242000-03-17 21:46:59 +000078 echo "whence: usage: whence [-afpv] name..." >&2
79 return 2
Jari Aalto726f6381996-08-26 18:22:31 +000080 fi
81
82 for cmd
83 do
84 if [ "$vflag" ] ; then
Jari Aaltobb706242000-03-17 21:46:59 +000085 if [ -z "$defarg" ]; then
86 builtin type $cmd | sed 1q
87 else
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
92 # that kind of time
93 builtin type $defarg $cmd | awk '
94BEGIN {printit = 1;}
95$1 == "'$cmd'" && $2 == "()" {printit=0; next; }
96/^}$/ { if (printit == 0) printit=1 ; else print $0; next ; }
97/.*/ { if (printit) print $0; }'
98 else
99 builtin type $defarg $cmd
100 fi
101 fi
Jari Aalto726f6381996-08-26 18:22:31 +0000102 else
Jari Aaltobb706242000-03-17 21:46:59 +0000103 path=$(builtin type $defarg -p $cmd)
Jari Aalto726f6381996-08-26 18:22:31 +0000104 if [ "$path" ] ; then
105 echo $path
106 else
107 case "$cmd" in
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000108 /*) echo "" ;;
Jari Aaltobb706242000-03-17 21:46:59 +0000109 *) case "$(builtin type -t $cmd)" in
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000110 "") echo "" ;;
111 *) echo "$cmd" ;;
112 esac
113 ;;
Jari Aalto726f6381996-08-26 18:22:31 +0000114 esac
115 fi
116 fi
117 done
118 return 0
119}
120
121#
122# For real ksh homeboy fanatics, redefine the `type' builtin with a ksh
123# version.
124#
125#type()
126#{
127# whence -v "$*"
128#}
129
Jari Aalto28ef6c32001-04-06 19:14:31 +0000130#
131# ksh-like `cd': cd [-LP] [dir [change]]
132#
Jari Aalto726f6381996-08-26 18:22:31 +0000133cd()
134{
Jari Aalto28ef6c32001-04-06 19:14:31 +0000135 OPTIND=1
136 while getopts "LP" opt
137 do
138 case $opt in
139 L|P) CDOPTS="$CDOPTS -$opt" ;;
140 *) echo "$FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
141 return 2;;
Jari Aalto726f6381996-08-26 18:22:31 +0000142 esac
Jari Aalto28ef6c32001-04-06 19:14:31 +0000143 done
144
145 shift $(( $OPTIND - 1 ))
146
147 case $# in
148 0) builtin cd $CDOPTS "$HOME" ;;
149 1) builtin cd $CDOPTS "$@" ;;
150 2) old="$1" new="$2"
151 case "$PWD" in
152 *$old*) ;;
153 *) echo "${0##*/}: $FUNCNAME: bad substitution" >&2 ; return 1 ;;
154 esac
155
156 dir=${PWD//$old/$new}
157
158 builtin cd $CDOPTS "$dir" && echo "$PWD"
159
Jari Aalto726f6381996-08-26 18:22:31 +0000160 ;;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000161 *) echo "${0##*/}: $FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
162 return 2 ;;
Jari Aalto726f6381996-08-26 18:22:31 +0000163 esac
164}
165
166#
167# ksh print emulation
168#
Jari Aaltobb706242000-03-17 21:46:59 +0000169# print [-Rnprsu[n]] [-f format] [arg ...]
Jari Aalto726f6381996-08-26 18:22:31 +0000170#
171# - end of options
172# -R BSD-style -- only accept -n, no escapes
173# -n do not add trailing newline
174# -p no-op (no coprocesses)
175# -r no escapes
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000176# -s print to the history file
Jari Aalto726f6381996-08-26 18:22:31 +0000177# -u n redirect output to fd n
Jari Aaltobb706242000-03-17 21:46:59 +0000178# -f format printf "$format" "$@"
Jari Aalto726f6381996-08-26 18:22:31 +0000179#
180
181print()
182{
183 local eflag=-e
Jari Aaltobb706242000-03-17 21:46:59 +0000184 local nflag= fflag= c
Jari Aalto726f6381996-08-26 18:22:31 +0000185 local fd=1
186
187 OPTIND=1
Jari Aaltobb706242000-03-17 21:46:59 +0000188 while getopts "fRnprsu:" c
Jari Aalto726f6381996-08-26 18:22:31 +0000189 do
190 case $c in
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000191 R) eflag= ;;
192 r) eflag= ;;
193 n) nflag=-n ;;
194 s) sflag=y ;;
Jari Aaltobb706242000-03-17 21:46:59 +0000195 f) fflag=y ;;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000196 u) fd=$OPTARG ;;
197 p) ;;
Jari Aalto726f6381996-08-26 18:22:31 +0000198 esac
199 done
Jari Aaltobb706242000-03-17 21:46:59 +0000200 shift $(( $OPTIND - 1 ))
201
202 if [ -n "$fflag" ]; then
203 builtin printf "$@" >&$fd
204 return
205 fi
Jari Aalto726f6381996-08-26 18:22:31 +0000206
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000207 case "$sflag" in
208 y) builtin history -s "$*" ;;
209 *) builtin echo $eflag $nflag "$@" >&$fd
210 esac
Jari Aalto726f6381996-08-26 18:22:31 +0000211}
212
213# substring function
214# this function should be equivalent to the substring built-in which was
215# eliminated after the 06/29/84 version
216substring ()
217{
218 local lpat flag str #local variables
219 set -f
220 case $1 in
221 -l|-L)
222 flag=$1
223 lpat=$2
224 shift 2
225 ;;
226 esac
227 # test for too few or too many arguments
Jari Aaltob80f6442004-07-27 13:29:18 +0000228 if [ x"$1" = x ] || [ $# -gt 2 ]; then
Jari Aalto726f6381996-08-26 18:22:31 +0000229 print -u2 'substring: bad argument count'
230 return 1
231 fi
232 str=$1
233 if [ x"$flag" = x-l ]; then #substring -l lpat
234 str=${str#$lpat}
235 elif [ x"$flag" = x-L ]; then
236 str=${str##$lpat} #substring -L lpat
237 fi
238
239 if [ x"$2" != x ]; then
240 echo ${str%$2}
241 else
242 echo $str
243 fi
244
245 return 0
246}