PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/usr/src/lib/libshell/common/data/bash_pre_rc.sh

https://github.com/richlowe/illumos-gate
Shell | 255 lines | 216 code | 8 blank | 31 comment | 8 complexity | 462abb30a950071fda6b22c956bbfe21 MD5 | raw file
  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. ########################################################################
  20. #
  21. # bash compatibility startup script
  22. #
  23. # Author:
  24. # Karsten Fleischer
  25. # Omnium Software Engineering
  26. # An der Luisenburg 7
  27. # D-51379 Leverkusen
  28. # Germany
  29. #
  30. # <K.Fleischer@omnium.de>
  31. #
  32. alias declare=typeset
  33. nameref FUNCNAME=.sh.fun
  34. integer SHLVL
  35. export SHLVL
  36. SHLVL+=1
  37. if [[ ! $EUID ]]
  38. then EUID=$(id -u)
  39. readonly EUID
  40. fi
  41. if [[ ! $UID ]]
  42. then UID=$(id -u)
  43. readonly UID
  44. fi
  45. readonly SHELLOPTS
  46. if ! shopt -qo restricted; then
  47. IFS=:
  48. for i in $SHELLOPTS
  49. do
  50. [[ -n "$i" ]] && set -o $i
  51. done
  52. unset IFS
  53. fi
  54. function SHELLOPTS.get
  55. {
  56. .sh.value=$(shopt -so)
  57. .sh.value=${.sh.value//+([[:space:]])on*([[:space:]])/:}
  58. .sh.value=${.sh.value%:}
  59. }
  60. set -A GROUPS $(id -G)
  61. function GROUPS.set
  62. {
  63. return 1
  64. }
  65. function GROUPS.unset
  66. {
  67. unset -f GROUPS.set
  68. unset -f GROUPS.unset
  69. }
  70. typeset -A DIRSTACK
  71. function DIRSTACK.get
  72. {
  73. set -A .sh.value $(dirs)
  74. }
  75. function DIRSTACK.set
  76. {
  77. integer index
  78. index=_push_max-.sh.subscript
  79. (( index == _push_max || index < _push_top )) && return
  80. _push_stack[index]=${.sh.value}
  81. }
  82. function DIRSTACK.unset
  83. {
  84. unset -f DIRSTACK.get
  85. unset -f DIRSTACK.set
  86. unset -f DIRSTACK.unset
  87. }
  88. function PS1.set
  89. {
  90. typeset prefix remaining=${.sh.value} var= n= k=
  91. while [[ $remaining ]]
  92. do prefix=${remaining%%'\'*}
  93. remaining=${remaining#$prefix}
  94. var+="$prefix"
  95. case ${remaining:1:1} in
  96. t) var+="\$(printf '%(%H:%M:%S)T')";;
  97. d) var+="\$(printf '%(%a %b:%e)T')";;
  98. n) var+=$'\n';;
  99. s) var+=ksh;;
  100. w) var+="\$(pwd)";;
  101. W) var+="\$(basename \"\$(pwd)\")";;
  102. u) var+=$USER;;
  103. h) var+=$(hostname -s);;
  104. '#') var+=!;;
  105. !) var+=!;;
  106. @) var+="\$(printf '%(%I:%M%p)T')";;
  107. '$') if (( $(id -u) == 0 ))
  108. then var+='#'
  109. else var+='$'
  110. fi;;
  111. '\') var+='\\';;
  112. '['|']') ;;
  113. [0-7]) case ${remaining:1:3} in
  114. [0-7][0-7][0-7])
  115. k=4;;
  116. [0-7][0-7])
  117. k=3;;
  118. *) k=2;;
  119. esac
  120. eval n="\$'"${remaining:0:k}"'"
  121. var+=$n
  122. remaining=${remaining:k}
  123. continue
  124. ;;
  125. "") ;;
  126. *) var+='\'${remaining:0:2};;
  127. esac
  128. remaining=${remaining:2}
  129. done
  130. .sh.value=$var
  131. }
  132. function logout
  133. {
  134. if shopt -q login_shell; then
  135. exit
  136. else
  137. print ${BASH##*/}: $0: not login shell: use 'exit' >&2
  138. return 1
  139. fi
  140. }
  141. PS1="bash$ "
  142. function source
  143. {
  144. if ! shopt -qpo posix; then
  145. unset OPATH
  146. typeset OPATH=$PATH
  147. typeset PATH=$PATH
  148. if shopt -q sourcepath; then
  149. PATH=$OPATH:.
  150. else
  151. PATH=.
  152. fi
  153. fi
  154. . "$@"
  155. }
  156. unalias .
  157. alias .=source
  158. alias enable=builtin
  159. function help
  160. {
  161. typeset b cmd usage try_cmd man
  162. function has_help_option
  163. {
  164. [[ $1 == @(''|/*|:|echo|false|true|login|test|'[') ]] && return 1
  165. return 0
  166. }
  167. typeset -A short_use=(
  168. [echo]='Usage: echo [ options ] [arg]...'
  169. [:]='Usage: : ...'
  170. [true]='Usage: true ...'
  171. [false]='Usage: false ...'
  172. [login]='Usage: login [-p] [name]'
  173. ['[']='Usage: [ EXPRESSION ] | [ OPTION'
  174. [test]='Usage: test EXPRESSION | test'
  175. )
  176. b=$(builtin)
  177. if (( $# == 0))
  178. then print 'The following is the current list of built-in commands:'
  179. print -r $'Type help *name* for more information about name\n'
  180. for cmd in $b
  181. do if has_help_option $cmd
  182. then usage=$($cmd --short 2>&1)
  183. print -r -- "${usage:7}"
  184. else print -r -- ${short_use[$cmd]:7}
  185. fi
  186. done
  187. return
  188. fi
  189. b=${b/'['/}
  190. man=--man
  191. [[ $1 == -s ]] && man=--short && shift
  192. for try_cmd
  193. do if has_help_option $try_cmd
  194. then if [[ $try_cmd == @(${b//$'\n'/'|'}) ]]
  195. then $try_cmd $man
  196. else man $try_cmd
  197. fi
  198. elif [[ $man == '--short' ]]
  199. then print -r -- ${short_use[$try_cmd]}
  200. else man $try_cmd
  201. fi
  202. done
  203. }
  204. function cd
  205. {
  206. local msg
  207. local args
  208. local i
  209. local a
  210. local ret
  211. if ! shopt -q cdable_vars; then
  212. command cd "$@"
  213. else
  214. msg=$(command cd "$@" 2>&1)
  215. ret=$?
  216. if [[ $ret != 0 ]]; then
  217. for i
  218. do
  219. case $i in
  220. -*) args="$args $i" ;;
  221. */*) args="$args $i" ;;
  222. *) eval a="$"$i
  223. if [[ -n $a ]]; then args="$args $a"
  224. else args="$args $i"
  225. fi
  226. ;;
  227. esac
  228. done
  229. command cd $args
  230. else
  231. print -- $msg
  232. return $ret
  233. fi
  234. fi
  235. }
  236. typeset BASH=$0
  237. ! shopt -qo posix && HISTFILE=~/.bash_history
  238. HOSTNAME=$(hostname)
  239. nameref BASH_SUBSHELL=.sh.subshell