xref: /titanic_44/usr/src/lib/libshell/common/data/bash_pre_rc.sh (revision 3e14f97f673e8a630f076077de35afdd43dc1587)
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
33alias declare=typeset
34
35nameref FUNCNAME=.sh.fun
36integer SHLVL
37export SHLVL
38SHLVL+=1
39
40if	[[ ! $EUID ]]
41then	EUID=$(id -u)
42	readonly EUID
43fi
44
45if	[[ ! $UID ]]
46then	UID=$(id -u)
47	readonly UID
48fi
49
50readonly SHELLOPTS
51if ! shopt -qo restricted; then
52	IFS=:
53	for i in $SHELLOPTS
54	do
55		[[ -n "$i" ]] && set -o $i
56	done
57	unset IFS
58fi
59function SHELLOPTS.get
60{
61	.sh.value=$(shopt -so)
62	.sh.value=${.sh.value//+([[:space:]])on*([[:space:]])/:}
63	.sh.value=${.sh.value%:}
64}
65
66set -A GROUPS $(id -G)
67function GROUPS.set
68{
69	return 1
70}
71function GROUPS.unset
72{
73	unset -f GROUPS.set
74	unset -f GROUPS.unset
75}
76
77typeset -A DIRSTACK
78function DIRSTACK.get
79{
80	set -A .sh.value $(dirs)
81}
82function DIRSTACK.set
83{
84	integer index
85	index=_push_max-.sh.subscript
86	(( index == _push_max || index < _push_top )) && return
87	_push_stack[index]=${.sh.value}
88}
89function DIRSTACK.unset
90{
91	unset -f DIRSTACK.get
92	unset -f DIRSTACK.set
93	unset -f DIRSTACK.unset
94}
95
96function PS1.set
97{
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
132			;;
133		"")	;;
134		*)	var+='\'${remaining:0:2};;
135		esac
136		remaining=${remaining:2}
137	done
138	.sh.value=$var
139}
140function logout
141{
142	if shopt -q login_shell; then
143		exit
144	else
145		print ${BASH##*/}: $0: not login shell: use 'exit' >&2
146		return 1
147	fi
148}
149PS1="bash$ "
150
151function source
152{
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=.
161		fi
162	fi
163	. "$@"
164}
165unalias .
166alias .=source
167
168alias enable=builtin
169
170function help
171{
172	typeset b cmd usage try_cmd man
173	function has_help_option
174	{
175		[[ $1 == @(''|/*|:|echo|false|true|login|test|'[') ]] && return 1
176		return 0
177	}
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'
186	)
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}
196			fi
197		done
198		return
199	fi
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
208			fi
209		elif	[[ $man == '--short' ]]
210		then	print -r -- ${short_use[$try_cmd]}
211		else	man $try_cmd
212		fi
213	done
214}
215
216function cd
217{
218
219	local msg
220	local args
221	local i
222	local a
223	local ret
224
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
232			do
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"
239					fi
240					;;
241				esac
242			done
243
244			command cd $args
245		else
246			print -- $msg
247			return $ret
248		fi
249	fi
250}
251
252typeset BASH=$0
253! shopt -qo posix && HISTFILE=~/.bash_history
254HOSTNAME=$(hostname)
255nameref BASH_SUBSHELL=.sh.subshell
256