]> git.ipfire.org Git - thirdparty/bash.git/blob - examples/functions/kshenv
Bash-5.2 patch 26: fix typo when specifying readline's custom color prefix
[thirdparty/bash.git] / examples / functions / kshenv
1 #
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 #
8
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
26 #
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 #
31 alias r="fc -s"
32 alias functions="typeset -f"
33 alias integer="typeset -i"
34 alias nohup="nohup "
35 alias command="command "
36 alias stop="kill -s STOP"
37 alias redirect="command exec"
38 alias 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
49 whence()
50 {
51 local vflag pflag fflag defarg c
52 local path
53
54 vflag= aflag= pflag= fflag=
55 path=
56 if [ "$#" = "0" ] ; then
57 echo "whence: usage: whence [-afpv] name..." >&2
58 return 2
59 fi
60
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 ))
76
77 if [ "$#" = "0" ] ; then
78 echo "whence: usage: whence [-afpv] name..." >&2
79 return 2
80 fi
81
82 for cmd
83 do
84 if [ "$vflag" ] ; then
85 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 '
94 BEGIN {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
102 else
103 path=$(builtin type $defarg -p $cmd)
104 if [ "$path" ] ; then
105 echo $path
106 else
107 case "$cmd" in
108 /*) echo "" ;;
109 *) case "$(builtin type -t $cmd)" in
110 "") echo "" ;;
111 *) echo "$cmd" ;;
112 esac
113 ;;
114 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
130 #
131 # ksh-like `cd': cd [-LP] [dir [change]]
132 #
133 cd()
134 {
135 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;;
142 esac
143 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
160 ;;
161 *) echo "${0##*/}: $FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
162 return 2 ;;
163 esac
164 }
165
166 #
167 # ksh print emulation
168 #
169 # print [-Rnprsu[n]] [-f format] [arg ...]
170 #
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
176 # -s print to the history file
177 # -u n redirect output to fd n
178 # -f format printf "$format" "$@"
179 #
180
181 print()
182 {
183 local eflag=-e
184 local nflag= fflag= c
185 local fd=1
186
187 OPTIND=1
188 while getopts "fRnprsu:" c
189 do
190 case $c in
191 R) eflag= ;;
192 r) eflag= ;;
193 n) nflag=-n ;;
194 s) sflag=y ;;
195 f) fflag=y ;;
196 u) fd=$OPTARG ;;
197 p) ;;
198 esac
199 done
200 shift $(( $OPTIND - 1 ))
201
202 if [ -n "$fflag" ]; then
203 builtin printf "$@" >&$fd
204 return
205 fi
206
207 case "$sflag" in
208 y) builtin history -s "$*" ;;
209 *) builtin echo $eflag $nflag "$@" >&$fd
210 esac
211 }
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
216 substring ()
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
228 if [ x"$1" = x ] || [ $# -gt 2 ]; then
229 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 }