8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libshell / common / data / bash_pre_rc.sh
blobe5a7bd3736afa48ad9aa9878ea25a7031e74d48c
1 ########################################################################
2 # #
3 # This software is part of the ast package #
4 # Copyright (c) 1982-2010 AT&T Intellectual Property #
5 # and is licensed under the #
6 # Common Public License, Version 1.0 #
7 # by AT&T Intellectual Property #
8 # #
9 # A copy of the License is available at #
10 # http://www.opensource.org/licenses/cpl1.0.txt #
11 # (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) #
12 # #
13 # Information and Software Systems Research #
14 # AT&T Research #
15 # Florham Park NJ #
16 # #
17 # David Korn <dgk@research.att.com> #
18 # #
19 ########################################################################
21 # bash compatibility startup script
23 # Author:
24 # Karsten Fleischer
25 # Omnium Software Engineering
26 # An der Luisenburg 7
27 # D-51379 Leverkusen
28 # Germany
30 # <K.Fleischer@omnium.de>
33 alias declare=typeset
35 nameref FUNCNAME=.sh.fun
36 integer SHLVL
37 export SHLVL
38 SHLVL+=1
40 if [[ ! $EUID ]]
41 then EUID=$(id -u)
42 readonly EUID
45 if [[ ! $UID ]]
46 then UID=$(id -u)
47 readonly UID
50 readonly SHELLOPTS
51 if ! shopt -qo restricted; then
52 IFS=:
53 for i in $SHELLOPTS
55 [[ -n "$i" ]] && set -o $i
56 done
57 unset IFS
59 function SHELLOPTS.get
61 .sh.value=$(shopt -so)
62 .sh.value=${.sh.value//+([[:space:]])on*([[:space:]])/:}
63 .sh.value=${.sh.value%:}
66 set -A GROUPS $(id -G)
67 function GROUPS.set
69 return 1
71 function GROUPS.unset
73 unset -f GROUPS.set
74 unset -f GROUPS.unset
77 typeset -A DIRSTACK
78 function DIRSTACK.get
80 set -A .sh.value $(dirs)
82 function DIRSTACK.set
84 integer index
85 index=_push_max-.sh.subscript
86 (( index == _push_max || index < _push_top )) && return
87 _push_stack[index]=${.sh.value}
89 function DIRSTACK.unset
91 unset -f DIRSTACK.get
92 unset -f DIRSTACK.set
93 unset -f DIRSTACK.unset
96 function PS1.set
98 typeset prefix remaining=${.sh.value} var= n= k=
99 while [[ $remaining ]]
100 do prefix=${remaining%%'\'*}
101 remaining=${remaining#$prefix}
102 var+="$prefix"
103 case ${remaining:1:1} in
104 t) var+="\$(printf '%(%H:%M:%S)T')";;
105 d) var+="\$(printf '%(%a %b:%e)T')";;
106 n) var+=$'\n';;
107 s) var+=ksh;;
108 w) var+="\$(pwd)";;
109 W) var+="\$(basename \"\$(pwd)\")";;
110 u) var+=$USER;;
111 h) var+=$(hostname -s);;
112 '#') var+=!;;
113 !) var+=!;;
114 @) var+="\$(printf '%(%I:%M%p)T')";;
115 '$') if (( $(id -u) == 0 ))
116 then var+='#'
117 else var+='$'
118 fi;;
119 '\') var+='\\';;
120 '['|']') ;;
121 [0-7]) case ${remaining:1:3} in
122 [0-7][0-7][0-7])
123 k=4;;
124 [0-7][0-7])
125 k=3;;
126 *) k=2;;
127 esac
128 eval n="\$'"${remaining:0:k}"'"
129 var+=$n
130 remaining=${remaining:k}
131 continue
133 "") ;;
134 *) var+='\'${remaining:0:2};;
135 esac
136 remaining=${remaining:2}
137 done
138 .sh.value=$var
140 function logout
142 if shopt -q login_shell; then
143 exit
144 else
145 print ${BASH##*/}: $0: not login shell: use 'exit' >&2
146 return 1
149 PS1="bash$ "
151 function source
153 if ! shopt -qpo posix; then
154 unset OPATH
155 typeset OPATH=$PATH
156 typeset PATH=$PATH
157 if shopt -q sourcepath; then
158 PATH=$OPATH:.
159 else
160 PATH=.
163 . "$@"
165 unalias .
166 alias .=source
168 alias enable=builtin
170 function help
172 typeset b cmd usage try_cmd man
173 function has_help_option
175 [[ $1 == @(''|/*|:|echo|false|true|login|test|'[') ]] && return 1
176 return 0
178 typeset -A short_use=(
179 [echo]='Usage: echo [ options ] [arg]...'
180 [:]='Usage: : ...'
181 [true]='Usage: true ...'
182 [false]='Usage: false ...'
183 [login]='Usage: login [-p] [name]'
184 ['[']='Usage: [ EXPRESSION ] | [ OPTION'
185 [test]='Usage: test EXPRESSION | test'
187 b=$(builtin)
188 if (( $# == 0))
189 then print 'The following is the current list of built-in commands:'
190 print -r $'Type help *name* for more information about name\n'
191 for cmd in $b
192 do if has_help_option $cmd
193 then usage=$($cmd --short 2>&1)
194 print -r -- "${usage:7}"
195 else print -r -- ${short_use[$cmd]:7}
197 done
198 return
200 b=${b/'['/}
201 man=--man
202 [[ $1 == -s ]] && man=--short && shift
203 for try_cmd
204 do if has_help_option $try_cmd
205 then if [[ $try_cmd == @(${b//$'\n'/'|'}) ]]
206 then $try_cmd $man
207 else man $try_cmd
209 elif [[ $man == '--short' ]]
210 then print -r -- ${short_use[$try_cmd]}
211 else man $try_cmd
213 done
216 function cd
219 local msg
220 local args
221 local i
222 local a
223 local ret
225 if ! shopt -q cdable_vars; then
226 command cd "$@"
227 else
228 msg=$(command cd "$@" 2>&1)
229 ret=$?
230 if [[ $ret != 0 ]]; then
231 for i
233 case $i in
234 -*) args="$args $i" ;;
235 */*) args="$args $i" ;;
236 *) eval a="$"$i
237 if [[ -n $a ]]; then args="$args $a"
238 else args="$args $i"
241 esac
242 done
244 command cd $args
245 else
246 print -- $msg
247 return $ret
252 typeset BASH=$0
253 ! shopt -qo posix && HISTFILE=~/.bash_history
254 HOSTNAME=$(hostname)
255 nameref BASH_SUBSHELL=.sh.subshell