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.
28 linux*|screen*|xterm*|vt100*|eterm*)
29 case "$(tput bold)" in
30 "") bold="\[$(tput md)\]" unbold="\[$(tput me)\]" ;;
31 *) bold="\[$(tput bold)\]" unbold="\[$(tput sgr0)\]" ;;
36 bold='' unbold='' nl='' ;;
39 ## Choose the right delimiters. Highlight root prompts specially;
40 ## highlight when I'm running as some other user. Highlight when this
41 ## isn't the outermost shell on the terminal.
42 if (( EUID == 0 )); then
43 left=`echo « | iconv -f UTF-8 -t //translit`
44 right=`echo » | iconv -f UTF-8 -t //translit`
47 mdw|mwooding) u="" left="[" right="]" ;;
48 *) u="\\u@" left="{" right="}" ;;
50 if [ "$__mdw_tty" = "`tty`" ]; then
53 export __mdw_tty="`tty`"
57 ## If this session is insecure then highlight that.
58 if [ -z "$SSH_CLIENT" ] &&
59 [ "$__mdw_sechost" != "`hostname`" ]
64 ## Build the prompt string.
65 PS1="$nl$bold$left$sec_l$u\\h$sec_r \\w$right$unbold"
66 PS2="$PS1 $bold>$unbold "
70 ###--------------------------------------------------------------------------
71 ### Other shell tweaking.
73 ## Random shell tweaks.
82 shopt -s expand_aliases
91 shopt -s interactive_comments
97 shopt -u shift_verbose
99 HISTCONTROL=ignorespace:erasedups
101 ## Some handy aliases.
103 alias which="command -v"
105 alias ssync="rsync -e ssh"
107 case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac
111 alias re="rootly $EDITOR"
112 alias pstree="pstree -hl"
113 alias cdtmp='cd ${TMPDIR-/tmp}'
114 alias pushtmp='pushd ${TMPDIR-/tmp}'
116 alias svn="svnwrap svn"
120 [ -r /etc/bash_completion ] && . /etc/bash_completion
121 [ -r $HOME/.bash_completion ] && . $HOME/.bash_completion
123 ###--------------------------------------------------------------------------
126 ## Arrange for `ls' output to be in colour.
127 if [ -x /usr/bin/dircolors -o -x /usr/local/bin/dircolors ]; then
128 eval `dircolors -b ~/.dircolors`
135 command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
141 ## Arrange for `grep' output to be in colour.
142 export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
145 declare grep=$1; shift
147 command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
152 alias grep="greplike grep"
153 alias egrep="greplike egrep"
154 alias fgrep="greplike fgrep"
155 alias zgrep="greplike zgrep"
157 ## Turn off pagers inside Emacs shell buffers.
158 case "$INSIDE_EMACS" in
159 22.*,comint) export PAGER=cat ;;
162 ###--------------------------------------------------------------------------
163 ### More complicated shell functions.
165 ## xt [@HOST] XTERM-ARGS
167 ## Open a terminal, maybe on a remote host.
171 local remote=${1#@} title
173 if [ $# -gt 0 ]; then
174 title="xterm [$remote] $1"
176 title="xterm [$remote]"
178 (xterm -title "$title" -e ssh $remote "$@" &)
188 ## Tweak core dumps on and off, or show the current status.
191 xon|xy|xyes) ulimit -Sc `ulimit -Hc` ;;
192 xoff|xn|xno) ulimit -Sc 0 ;;
196 0) echo "Core dumps disabled" ;;
197 unlimited) echo "Core dumps enabled" ;;
198 *) echo "Core dump limit is $l blocks" ;;
202 echo >&2 "usage: core [y|n]"
210 ## Set current security world to NAME. With no NAME, print the currently
213 local nfast=${NFAST_HOME-/opt/nfast}
217 echo "${NFAST_KMDATA#$nfast/kmdata-}"
222 elif [ -d "$nfast/kmdata-$1" ]; then
223 kmdata=$nfast/kmdata-$1
225 echo >&2 "world: can't find world $1"
230 0) export NFAST_KMDATA=$kmdata ;;
237 ## Fix `man' under Slowaris.
245 case "$arg" in [0-9]*) man[i+=1]="-s" ;; esac
248 command man "${man[@]}"
253 ###--------------------------------------------------------------------------
256 ## path-add [VAR] DIR
258 ## Add DIR to the beginning of PATH-like variable VAR (defaults to PATH) if
259 ## it's not there already.
261 local pathvar export dir val
263 1) pathvar=PATH dir=$1 export="export PATH";;
264 2) pathvar=$1 dir=$2 export=:;;
265 *) echo >&2 "Usage: $0 [VAR] DIR";;
267 eval "val=\$$pathvar"
272 eval "$pathvar=\$val"
276 ## path-remove [VAR] DIR
278 ## Remove DIR from PATH-like variable VAR (defaults to PATH); it's not an
279 ## error if DIR isn't in VAR.
281 local pathvar export dir val
283 1) pathvar=PATH dir=$1 export="export PATH";;
284 2) pathvar=$1 dir=$2 export=:;;
285 *) echo >&2 "Usage: $0 [VAR] DIR";;
287 eval "val=\$$pathvar"
290 :"$dir":*) val=${val#$dir:} ;;
291 *:"$dir":) val=${val%:$dir} ;;
292 *:"$dir":*) val=${val/:$dir:/:} ;;
294 eval "$pathvar=\$val"
298 ## pathhack [-f] +HACK|-HACK...
300 ## Each HACK refers to a subdirectory of `~/bin/hacks'. A hack name preceded
301 ## by `+' adds the directory to the PATH; a `-' removes. Adding a hack
302 ## that's already on the PATH doesn't do anything unless `-f' is set, in
303 ## which case it gets moved to the beginning. With no arguments, print the
304 ## currently installed hacks.
306 if [ $# -eq 0 ]; then
311 echo ${e#$HOME/bin/hacks/}
319 while [ $# -gt 0 ]; do
338 dir=$HOME/bin/hacks/$hack
340 echo "$0: path hack $hack not found"
343 case "$arg,$force,:$PATH:" in
345 path-remove path "$dir"
348 path-remove path "$dir"
359 if [ $# -eq 0 ]; then
367 ###--------------------------------------------------------------------------
368 ### Finishing touches.
370 ## For `root' use -- some simple molly-guards.
371 if (( UID == 0 )); then
372 alias rm='rm -i' cp='cp -i' mv='mv -i'
376 ## Run any local hooks.
377 [ -f "$HOME/.bashrc-local" ] && . "$HOME/.bashrc-local"
379 ## Close the `__mdw_bashrc' guard.
382 ###----- That's all, folks --------------------------------------------------