636405e32dfdc3616d3a32e914bf60889889b887
[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 ; shift 1 ;;
43         -*) echo "whence: bad option: $1" ; return 1 ;;
44          *) ;;
45         esac
46
47         if [ "$#" = "0" ] ; then
48                 echo "whence: bad argument count"
49                 return 1
50         fi
51
52         for cmd
53         do
54                 if [ "$vflag" ]  ; then
55                         echo $(builtin type $cmd | sed 1q)
56                 else
57                         path=$(builtin type -path $cmd)
58                         if [ "$path" ] ; then
59                                 echo $path
60                         else
61                                 case "$cmd" in
62                                 /*) echo "" ;;
63                                  *) case "$(builtin type -type $cmd)" in
64                                     "") echo "" ;;
65                                     *) echo "$cmd" ;;
66                                     esac
67                                     ;;
68                                 esac
69                         fi
70                 fi
71         done
72         return 0
73 }
74
75 #
76 # For real ksh homeboy fanatics, redefine the `type' builtin with a ksh
77 # version.
78 #
79 #type()
80 #{
81 #       whence -v "$*"
82 #}
83
84 cd()
85 {
86         case $# in
87         0)      builtin cd "$HOME" ;;
88         1)      builtin cd "$@" ;;
89         2)      old="$1"
90                 new="$2"
91                 dir=$(echo "$PWD" | sed "s:$old:$new:g")
92                 case "$dir" in
93                 "$PWD") echo "bash: cd: bad substitution" >&2 ; return 1 ;;
94                 *)      echo "$dir"
95                         builtin cd "$dir"
96                         ;;
97                 esac
98                 ;;
99         *)      echo "cd: wrong arg count" >&2 ; return 1 ;;
100         esac
101 }
102
103 #
104 # ksh print emulation
105 #
106 #       print [-Rnprsu[n]] [arg ...]
107 #
108 #       -       end of options
109 #       -R      BSD-style -- only accept -n, no escapes
110 #       -n      do not add trailing newline
111 #       -p      no-op (no coprocesses)
112 #       -r      no escapes
113 #       -s      print to the history file
114 #       -u n    redirect output to fd n
115 #
116
117 print()
118 {
119         local eflag=-e
120         local nflag=
121         local fd=1
122
123         OPTIND=1
124         while getopts "Rnprsu:" c
125         do
126                 case $c in
127                 R)      eflag= ;;
128                 r)      eflag= ;;
129                 n)      nflag=-n ;;
130                 s)      sflag=y ;;
131                 u)      fd=$OPTARG ;;
132                 p)      ;;
133                 esac
134         done
135         shift $[ $OPTIND - 1 ]
136
137         case "$sflag" in
138         y)      builtin history -s "$*" ;;
139         *)      builtin echo $eflag $nflag "$@" >&$fd
140         esac
141 }
142
143 # substring function
144 # this function should be equivalent to the substring built-in which was
145 # eliminated after the 06/29/84 version
146 substring ()
147 {
148         local lpat flag str     #local variables
149         set -f
150         case $1 in
151         -l|-L)
152                 flag=$1
153                 lpat=$2
154                 shift 2
155                 ;;
156         esac
157         # test for too few or too many arguments
158         if [ x"$1" = x -o $# -gt 2 ]; then
159                 print -u2 'substring: bad argument count'
160                 return 1
161         fi
162         str=$1
163         if [ x"$flag" = x-l ]; then             #substring -l lpat
164                 str=${str#$lpat}
165         elif [ x"$flag" = x-L ]; then
166                 str=${str##$lpat}               #substring -L lpat
167         fi
168
169         if [ x"$2" != x ]; then
170                 echo ${str%$2}
171         else
172                 echo $str
173         fi
174
175         return 0
176 }