3 ### Bash session things
5 ## Only do this if we haven't done it before. (Note that this guard isn't
6 ## exported, so subshells will need to make their own arrangements.)
7 if [ -z "$__mdw_bashrc" ]; then
10 ## If we've not run the main profile yet, we should do that first. It sets
11 ## up things we rely on. Also, if there's a system script, we should run
13 [ -z "$__mdw_profile" -a -r $HOME/.bash_profile ] && . $HOME/.bash_profile
14 [ -r /etc/bashrc ] && . /etc/bashrc
16 ## Set the temporary directory again. (If we've switched users, we'll want a
17 ## different temporary directory.)
18 [ "${TMPDIR+yes}" ] || eval `tmpdir -b`
20 ###--------------------------------------------------------------------------
23 ## Only bother if the shell is interactive.
26 ## Fancy highlighting in some terminals.
27 marker=${STY+'\[
\ek
\e\\\]'}
29 linux*|screen*|xterm*|vt100*|eterm*)
30 case "$(tput bold)" in
31 "") bold="\[$(tput md)\]" unbold="\[$(tput me)\]" ;;
32 *) bold="\[$(tput bold)\]" unbold="\[$(tput sgr0)\]" ;;
37 bold='' unbold='' nl='' ;;
40 ## Choose the right delimiters. Highlight root prompts specially;
41 ## highlight when I'm running as some other user. Highlight when this
42 ## isn't the outermost shell on the terminal.
43 if (( EUID == 0 )); then
44 left=`echo « | iconv -f UTF-8 -t //translit`
45 right=`echo » | iconv -f UTF-8 -t //translit`
48 mdw|mwooding) u="" left="[" right="]" ;;
49 *) u="\\u@" left="{" right="}" ;;
51 if [ "$__mdw_tty" = "`tty`" ]; then
54 export __mdw_tty="`tty`"
58 ## If this session is insecure then highlight that.
59 if [ -z "$SSH_CLIENT" ] &&
60 [ "$__mdw_sechost" != "`hostname`" ]
65 ## Build the prompt string.
66 PS1="$nl$bold$left$sec_l$u\\h$sec_r \\w$marker$right$unbold"
67 PS2="$PS1 $bold>$unbold "
71 ###--------------------------------------------------------------------------
72 ### Other shell tweaking.
74 ## Random shell tweaks.
83 shopt -s expand_aliases
92 shopt -s interactive_comments
98 shopt -u shift_verbose
100 HISTCONTROL=ignorespace:erasedups
102 ## Some handy aliases.
104 alias which="command -v"
106 alias ssync="rsync -e ssh"
108 case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac
112 alias re="rootly $EDITOR"
113 alias pstree="pstree -hl"
114 alias cdtmp='cd ${TMPDIR-/tmp}'
115 alias pushtmp='pushd ${TMPDIR-/tmp}'
117 alias svn="svnwrap svn"
121 [ -r /etc/bash_completion ] && . /etc/bash_completion
122 [ -r $HOME/.bash_completion ] && . $HOME/.bash_completion
124 ###--------------------------------------------------------------------------
127 ## Arrange for `ls' output to be in colour.
128 if [ -x /usr/bin/dircolors -o -x /usr/local/bin/dircolors ]; then
129 eval `dircolors -b ~/.dircolors`
136 command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
142 ## Arrange for `grep' output to be in colour.
143 export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
146 declare grep=$1; shift
148 command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
153 alias grep="greplike grep"
154 alias egrep="greplike egrep"
155 alias fgrep="greplike fgrep"
156 alias zgrep="greplike zgrep"
158 ## Turn off pagers inside Emacs shell buffers.
159 case "$INSIDE_EMACS" in
160 22.*,comint) export PAGER=cat ;;
163 ###--------------------------------------------------------------------------
164 ### More complicated shell functions.
166 ## xt [@HOST] XTERM-ARGS
168 ## Open a terminal, maybe on a remote host.
172 local remote=${1#@} title
174 if [ $# -gt 0 ]; then
175 title="xterm [$remote] $1"
177 title="xterm [$remote]"
179 (xterm -title "$title" -e ssh $remote "$@" &)
189 ## Tweak core dumps on and off, or show the current status.
192 xon|xy|xyes) ulimit -Sc `ulimit -Hc` ;;
193 xoff|xn|xno) ulimit -Sc 0 ;;
197 0) echo "Core dumps disabled" ;;
198 unlimited) echo "Core dumps enabled" ;;
199 *) echo "Core dump limit is $l blocks" ;;
203 echo >&2 "usage: core [y|n]"
211 ## Set current security world to NAME. With no NAME, print the currently
214 local nfast=${NFAST_HOME-/opt/nfast}
218 echo "${NFAST_KMDATA#$nfast/kmdata-}"
223 elif [ -d "$nfast/kmdata-$1" ]; then
224 kmdata=$nfast/kmdata-$1
226 echo >&2 "world: can't find world $1"
231 0) export NFAST_KMDATA=$kmdata ;;
238 ## Fix `man' under Slowaris.
246 case "$arg" in [0-9]*) man[i+=1]="-s" ;; esac
249 command man "${man[@]}"
254 ###--------------------------------------------------------------------------
257 ## path-add [VAR] DIR
259 ## Add DIR to the beginning of PATH-like variable VAR (defaults to PATH) if
260 ## it's not there already.
262 local pathvar export dir val
264 1) pathvar=PATH dir=$1 export="export PATH";;
265 2) pathvar=$1 dir=$2 export=:;;
266 *) echo >&2 "Usage: $0 [VAR] DIR";;
268 eval "val=\$$pathvar"
273 eval "$pathvar=\$val"
277 ## path-remove [VAR] DIR
279 ## Remove DIR from PATH-like variable VAR (defaults to PATH); it's not an
280 ## error if DIR isn't in VAR.
282 local pathvar export dir val
284 1) pathvar=PATH dir=$1 export="export PATH";;
285 2) pathvar=$1 dir=$2 export=:;;
286 *) echo >&2 "Usage: $0 [VAR] DIR";;
288 eval "val=\$$pathvar"
291 :"$dir":*) val=${val#$dir:} ;;
292 *:"$dir":) val=${val%:$dir} ;;
293 *:"$dir":*) val=${val/:$dir:/:} ;;
295 eval "$pathvar=\$val"
299 ## pathhack [-f] +HACK|-HACK...
301 ## Each HACK refers to a subdirectory of `~/bin/hacks'. A hack name preceded
302 ## by `+' adds the directory to the PATH; a `-' removes. Adding a hack
303 ## that's already on the PATH doesn't do anything unless `-f' is set, in
304 ## which case it gets moved to the beginning. With no arguments, print the
305 ## currently installed hacks.
307 if [ $# -eq 0 ]; then
312 echo ${e#$HOME/bin/hacks/}
320 while [ $# -gt 0 ]; do
339 dir=$HOME/bin/hacks/$hack
341 echo "$0: path hack $hack not found"
344 case "$arg,$force,:$PATH:" in
346 path-remove path "$dir"
349 path-remove path "$dir"
360 if [ $# -eq 0 ]; then
368 ###--------------------------------------------------------------------------
369 ### Finishing touches.
371 ## For `root' use -- some simple molly-guards.
372 if (( UID == 0 )); then
373 alias rm='rm -i' cp='cp -i' mv='mv -i'
377 ## Run any local hooks.
378 [ -f "$HOME/.bashrc-local" ] && . "$HOME/.bashrc-local"
380 ## Close the `__mdw_bashrc' guard.
383 ###----- That's all, folks --------------------------------------------------