Imported from ../bash-1.14.7.tar.gz.
[platform/upstream/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 -e -"
14 alias functions="typeset -f"
15 alias integer="typeset -i"
16 alias nohup="nohup "
17 alias true=":"
18 alias false="let 0"
19 alias hist="fc"
20
21 #
22 # An almost-ksh compatible `whence' command.  This is as hairy as it is 
23 # because of the desire to exactly mimic ksh (whose behavior was determined
24 # empirically).
25
26 # This depends somewhat on knowing the format of the output of the bash
27 # `builtin type' command.
28 #
29
30 whence()
31 {
32         local vflag
33         local path
34
35         vflag=
36         path=
37         if [ "$#" = "0" ] ; then
38                 echo "whence: argument expected"
39                 return 1
40         fi
41         case "$1" in
42                 -v) vflag=1
43                     shift 1
44                     ;;
45                 -*) echo "whence: bad option: $1"
46                     return 1
47                     ;;
48                  *) ;;
49         esac
50
51         if [ "$#" = "0" ] ; then
52                 echo "whence: bad argument count"
53                 return 1
54         fi
55
56         for cmd
57         do
58                 if [ "$vflag" ]  ; then
59                         echo $(builtin type $cmd | sed 1q)
60                 else
61                         path=$(builtin type -path $cmd)
62                         if [ "$path" ] ; then
63                                 echo $path
64                         else
65                                 case "$cmd" in
66                                         /*) echo ""
67                                             ;;
68                                          *) case "$(builtin type -type $cmd)" in
69                                                 "") echo ""
70                                                     ;;
71                                                  *) echo "$cmd"
72                                                     ;;
73                                             esac
74                                             ;;
75                                 esac
76                         fi
77                 fi
78         done
79         return 0
80 }
81
82 #
83 # For real ksh homeboy fanatics, redefine the `type' builtin with a ksh
84 # version.
85 #
86 #type()
87 #{
88 #       whence -v "$*"
89 #}
90
91 cd()
92 {
93         case $# in
94         0)      builtin cd "$HOME" ;;
95         1)      builtin cd "$@" ;;
96         2)      old="$1"
97                 new="$2"
98                 dir=$(echo "$PWD" | sed "s:$old:$new:g")
99                 case "$dir" in
100                 "$PWD") echo "bash: cd: bad substitution" >&2 ; return 1 ;;
101                 *)      echo "$dir"
102                         builtin cd "$dir"
103                         ;;
104                 esac
105                 ;;
106         *)      echo "cd: wrong arg count" >&2 ; return 1 ;;
107         esac
108 }
109
110 #
111 # ksh print emulation
112 #
113 #       print [-Rnprsu[n]] [arg ...]
114 #
115 #       -       end of options
116 #       -R      BSD-style -- only accept -n, no escapes
117 #       -n      do not add trailing newline
118 #       -p      no-op (no coprocesses)
119 #       -r      no escapes
120 #       -s      no-op (print to the history file)
121 #       -u n    redirect output to fd n
122 #
123
124 print()
125 {
126         local eflag=-e
127         local nflag=
128         local fd=1
129
130         OPTIND=1
131         while getopts "Rnprsu:" c
132         do
133                 case $c in
134                 R)      eflag=
135                         ;;
136                 r)      eflag=
137                         ;;
138                 n)      nflag=-n
139                         ;;
140                 u)      fd=$OPTARG
141                         ;;
142                 p|s)    ;;
143                 esac
144         done
145         shift $[ $OPTIND - 1 ]
146
147         builtin echo $eflag $nflag "$@" >&$fd
148 }
149
150 # substring function
151 # this function should be equivalent to the substring built-in which was
152 # eliminated after the 06/29/84 version
153 substring ()
154 {
155         local lpat flag str     #local variables
156         set -f
157         case $1 in
158         -l|-L)
159                 flag=$1
160                 lpat=$2
161                 shift 2
162                 ;;
163         esac
164         # test for too few or too many arguments
165         if [ x"$1" = x -o $# -gt 2 ]; then
166                 print -u2 'substring: bad argument count'
167                 return 1
168         fi
169         str=$1
170         if [ x"$flag" = x-l ]; then             #substring -l lpat
171                 str=${str#$lpat}
172         elif [ x"$flag" = x-L ]; then
173                 str=${str##$lpat}               #substring -L lpat
174         fi
175
176         if [ x"$2" != x ]; then
177                 echo ${str%$2}
178         else
179                 echo $str
180         fi
181
182         return 0
183 }