]> git.ipfire.org Git - thirdparty/bash.git/blob - examples/functions/kshenv
Imported from ../bash-2.04.tar.gz.
[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 # These are definitions for the ksh compiled-in `exported aliases'. There
10 # are others, but we already have substitutes for them: "history", "type",
11 # and "hash".
12 #
13 alias r="fc -s"
14 alias functions="typeset -f"
15 alias integer="typeset -i"
16 alias nohup="nohup "
17 alias command="command "
18 alias stop="kill -s STOP"
19 alias redirect="command exec"
20 alias hist="fc"
21
22 #
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
25 # empirically).
26 #
27 # This depends somewhat on knowing the format of the output of the bash
28 # `builtin type' command.
29 #
30
31 whence()
32 {
33 local vflag pflag fflag defarg c
34 local path
35
36 vflag= aflag= pflag= fflag=
37 path=
38 if [ "$#" = "0" ] ; then
39 echo "whence: usage: whence [-afpv] name..." >&2
40 return 2
41 fi
42
43 OPTIND=1
44 while getopts "avfp" c
45 do
46 case "$c" in
47 a) defarg=-a ;;
48 f) fflag=1 ;; # no-op
49 p) pflag=1 ;;
50 v) vflag=1 ;;
51 ?) echo "whence: $1: unknown option" >&2
52 echo "whence: usage: whence [-afpv] name..." >&2
53 return 2 ;;
54 esac
55 done
56
57 shift $(( $OPTIND - 1 ))
58
59 if [ "$#" = "0" ] ; then
60 echo "whence: usage: whence [-afpv] name..." >&2
61 return 2
62 fi
63
64 for cmd
65 do
66 if [ "$vflag" ] ; then
67 if [ -z "$defarg" ]; then
68 builtin type $cmd | sed 1q
69 else
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
74 # that kind of time
75 builtin type $defarg $cmd | awk '
76 BEGIN {printit = 1;}
77 $1 == "'$cmd'" && $2 == "()" {printit=0; next; }
78 /^}$/ { if (printit == 0) printit=1 ; else print $0; next ; }
79 /.*/ { if (printit) print $0; }'
80 else
81 builtin type $defarg $cmd
82 fi
83 fi
84 else
85 path=$(builtin type $defarg -p $cmd)
86 if [ "$path" ] ; then
87 echo $path
88 else
89 case "$cmd" in
90 /*) echo "" ;;
91 *) case "$(builtin type -t $cmd)" in
92 "") echo "" ;;
93 *) echo "$cmd" ;;
94 esac
95 ;;
96 esac
97 fi
98 fi
99 done
100 return 0
101 }
102
103 #
104 # For real ksh homeboy fanatics, redefine the `type' builtin with a ksh
105 # version.
106 #
107 #type()
108 #{
109 # whence -v "$*"
110 #}
111
112 cd()
113 {
114 case $# in
115 0) builtin cd "$HOME" ;;
116 1) builtin cd "$@" ;;
117 2) old="$1"
118 new="$2"
119 # dir=$(echo "$PWD" | sed "s:$old:$new:g")
120 dir=${PWD//$old/$new}
121 case "$dir" in
122 "$PWD") case "$PWD" in
123 *$old*) ;;
124 *) echo "$FUNCNAME: bad substitution" >&2 ; return 1 ;;
125 esac ;;
126 *) echo "$dir"
127 builtin cd "$dir"
128 ;;
129 esac
130 ;;
131 *) echo "$FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
132 return 1 ;;
133 esac
134 }
135
136 #
137 # ksh print emulation
138 #
139 # print [-Rnprsu[n]] [-f format] [arg ...]
140 #
141 # - end of options
142 # -R BSD-style -- only accept -n, no escapes
143 # -n do not add trailing newline
144 # -p no-op (no coprocesses)
145 # -r no escapes
146 # -s print to the history file
147 # -u n redirect output to fd n
148 # -f format printf "$format" "$@"
149 #
150
151 print()
152 {
153 local eflag=-e
154 local nflag= fflag= c
155 local fd=1
156
157 OPTIND=1
158 while getopts "fRnprsu:" c
159 do
160 case $c in
161 R) eflag= ;;
162 r) eflag= ;;
163 n) nflag=-n ;;
164 s) sflag=y ;;
165 f) fflag=y ;;
166 u) fd=$OPTARG ;;
167 p) ;;
168 esac
169 done
170 shift $(( $OPTIND - 1 ))
171
172 if [ -n "$fflag" ]; then
173 builtin printf "$@" >&$fd
174 return
175 fi
176
177 case "$sflag" in
178 y) builtin history -s "$*" ;;
179 *) builtin echo $eflag $nflag "$@" >&$fd
180 esac
181 }
182
183 # substring function
184 # this function should be equivalent to the substring built-in which was
185 # eliminated after the 06/29/84 version
186 substring ()
187 {
188 local lpat flag str #local variables
189 set -f
190 case $1 in
191 -l|-L)
192 flag=$1
193 lpat=$2
194 shift 2
195 ;;
196 esac
197 # test for too few or too many arguments
198 if [ x"$1" = x -o $# -gt 2 ]; then
199 print -u2 'substring: bad argument count'
200 return 1
201 fi
202 str=$1
203 if [ x"$flag" = x-l ]; then #substring -l lpat
204 str=${str#$lpat}
205 elif [ x"$flag" = x-L ]; then
206 str=${str##$lpat} #substring -L lpat
207 fi
208
209 if [ x"$2" != x ]; then
210 echo ${str%$2}
211 else
212 echo $str
213 fi
214
215 return 0
216 }