-# -*- mode: sh; coding: utf-8 -*-
-#
-# Bash session things
-#
+### -*-bash-*-
+###
+### Bash session things
+## Only do this if we haven't done it before. (Note that this guard isn't
+## exported, so subshells will need to make their own arrangements.)
if [ -z "$__mdw_bashrc" ]; then
-
__mdw_bashrc=done
+## If we've not run the main profile yet, we should do that first. It sets
+## up things we rely on. Also, if there's a system script, we should run
+## that too.
[ -z "$__mdw_profile" -a -r $HOME/.bash_profile ] && . $HOME/.bash_profile
[ -r /etc/bashrc ] && . /etc/bashrc
-# --- First of all, set up the prompt string ---
+## Completion.
+[ -r /etc/bash_completion ] && . /etc/bash_completion
+[ -r $HOME/.bash_completion ] && . $HOME/.bash_completion
+
+## Set the temporary directory again. (If we've switched users, we'll want a
+## different temporary directory.)
+[ "${TMPDIR+yes}" ] || eval `tmpdir -b`
+###--------------------------------------------------------------------------
+### Prompt hacking.
+
+## Only bother if the shell is interactive.
if [ -t 0 ]; then
+ ## Fancy highlighting in some terminals.
+ marker=${STY+'\[\ek\e\\\]'}
case "$TERM" in
linux*|screen*|xterm*|vt100*|eterm*)
- bold="\[$(tput bold)\]" unbold="\[$(tput sgr0)\]" nl="\[\r\]" ;;
+ case "$(tput bold)" in
+ "") bold="\[$(tput md)\]" unbold="\[$(tput me)\]" ;;
+ *) bold="\[$(tput bold)\]" unbold="\[$(tput sgr0)\]" ;;
+ esac
+ gitcolour="\[$(tput setaf 6)\]"
+ rccolour="\[$(tput setaf 1)\]"
+ uncolour="\[$(tput op)\]"
+ nl="\[\r\]"
+ ;;
*)
- bold='' unbold='' nl='' ;;
+ bold='' unbold='' nl='' gitcolour='' rccolour='' uncolour='';;
esac
+ ## Choose the right delimiters. Highlight root prompts specially;
+ ## highlight when I'm running as some other user. Highlight when this
+ ## isn't the outermost shell on the terminal.
if (( EUID == 0 )); then
- left=`echo « | iconv -f utf8 -t //translit`
- right=`echo » | iconv -f utf8 -t //translit`
+ left=`echo « | iconv -f UTF-8 -t //translit`
+ right=`echo » | iconv -f UTF-8 -t //translit`
else
case $USER in
- mdw|mwooding)
- u="" left="[" right="]"
- ;;
- *)
- u="\\u@" left="{" right="}"
- ;;
+ mdw|mwooding|nemo) u="" left="[" right="]" ;;
+ *) u="\\u@" left="{" right="}" ;;
esac
if [ "$__mdw_tty" = "`tty`" ]; then
left="<" right=">"
fi
fi
- if [ -z "$SSH_CLIENT" ] &&
- [ "$__mdw_sechost" != "`hostname`" ]
- then
+ ## If this session is insecure then highlight that.
+ if [ -z "$SSH_CLIENT" ] && [ -z "$SCHROOT_CHROOT_NAME" ] &&
+ [ "$__mdw_sechost" != "`hostname`" ]
+ then
sec_l='(' sec_r=')'
fi
- PS1="$nl$bold$left$sec_l$u\\h$sec_r \\w$right$unbold"
- PS2="$PS1 $bold>$unbold "
-
-fi # is stdin a tty?
-
-# --- Pagers are less useful within Emacs ---
+ ## If this is an schroot environment then point this out.
+ hqual=""
+ hqual="$hqual${SCHROOT_CHROOT_NAME+/$SCHROOT_CHROOT_NAME}"
-case "$INSIDE_EMACS" in
- 22.*,comint) export PAGER=cat ;;
-esac
+ ## Build the prompt string.
+ git="" rc=""
+ if [[ ${BASH_VERSINFO[0]} -ge 4 ]]; then
+ if type __git_ps1 >/dev/null 2>&1; then
+ git="$unbold$gitcolour\$(rc=\$?;__git_ps1;exit \$rc)$uncolour$bold"
+ fi
+ rc="$unbold$rccolour\$(rc=\$?;case \$rc in 0);;"
+ rc="$rc*)echo -n \" rc=\$rc\";;esac;exit \$rc)$uncolour$bold"
+ fi
+ PS1="$nl$bold$left$sec_l$u\\h$hqual$sec_r \\w$git$rc$marker$right$unbold"
+ PS2="$PS1 $bold>$unbold "
+ unset nl bold unbold left right sec_l sec_r marker
+ unset gitcolour rccolour uncolour git rc hqual
+fi
-# --- Little preferences ---
+###--------------------------------------------------------------------------
+### Other shell tweaking.
+## Random shell tweaks.
notify=1
set -b
shopt -u cdable_vars
shopt -u dotglob
shopt -s expand_aliases
shopt -s extglob
+if (( ${BASH_VERSINFO[0]} >= 4 )); then shopt -s globstar; fi
+shopt -s gnu_errfmt
shopt -s histappend
shopt -s histreedit
shopt -u histverify
shopt -s promptvars
shopt -u shift_verbose
shopt -s sourcepath
+HISTCONTROL=ignorespace:erasedups
-# --- Set the CDPATH ---
-#
-# CDPATH=~/src:/usr/src:/usr/lib:/usr/share
-# dots=..
-# i=6
-# while (( i > 0 )); do
-# CDPATH=$CDPATH:$dots
-# dots=$dots/..
-# (( i -= 1 ))
-# done
-# CDPATH=$CDPATH:/
+## Some handy aliases.
+alias cx='chmod a+x'
+alias which="command -v"
+alias rc="rc -l"
+alias ssync="rsync -e ssh"
+rootly () {
+ case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac
+ $__MDW_ROOTLY "$@"
+}
+alias r=rootly
+alias re="rootly $EDITOR"
+alias pstree="pstree -hl"
+alias cdtmp='cd ${TMPDIR-/tmp}'
+alias pushtmp='pushd ${TMPDIR-/tmp}'
+alias e="$EDITOR"
+alias svn="svnwrap svn"
+alias @="ssh"
-# --- Some colour `ls' support ---
+###--------------------------------------------------------------------------
+### Colour output.
-[ "${TMPDIR+yes}" ] || eval `tmpdir -b`
+## Arrange for `ls' output to be in colour.
if [ -x /usr/bin/dircolors -o -x /usr/local/bin/dircolors ]; then
eval `dircolors -b ~/.dircolors`
else
unset LS_COLORS
fi
-ls () {
+unalias ls 2>/dev/null || :; function ls () {
if [ -t 1 ]; then
command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
else
fi
}
-# --- Some colour `grep' support ---
-
+## Arrange for `grep' output to be in colour.
export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
greplike () {
alias grep="greplike grep"
alias egrep="greplike egrep"
alias fgrep="greplike fgrep"
-alias zgrep="greplike fgrep"
-
-# --- Set up some simple aliases ---
+alias zgrep="greplike zgrep"
-alias cx='chmod a+x'
-alias which="command -v"
-alias ssync="rsync -e ssh"
-alias rootly=$__MDW_ROOTLY
-alias r=rootly
-alias re="rootly $EDITOR"
-alias pstree="pstree -hl"
-alias cdtmp='cd ${TMPDIR-/tmp}'
-alias pushtmp='pushd ${TMPDIR-/tmp}'
-alias e="$EDITOR"
-alias svn="svnwrap svn"
-alias @="ssh"
-
-[ -r /etc/bash_completion ] && . /etc/bash_completion
-[ -r $HOME/.bash_completion ] && . $HOME/.bash_completion
+## Turn off pagers inside Emacs shell buffers.
+case "$INSIDE_EMACS" in
+ 2[2-9].*,comint | [3-9][0-9].*,comint) export PAGER=cat ;;
+esac
-# --- Make `xt' start an xterm, maybe logging into a remote host ---
+###--------------------------------------------------------------------------
+### More complicated shell functions.
+## xt [@HOST] XTERM-ARGS
+##
+## Open a terminal, maybe on a remote host.
xt () {
case "$1" in
@*)
esac
}
-# --- Turning on and off core dumps ---
-
+## core [y|n]
+##
+## Tweak core dumps on and off, or show the current status.
core () {
case "x$1" in
- xon|xy|xyes)
- ulimit -Sc `ulimit -Hc`
- ;;
- xoff|xn|xno)
- ulimit -Sc 0
- ;;
+ xon|xy|xyes) ulimit -Sc `ulimit -Hc` ;;
+ xoff|xn|xno) ulimit -Sc 0 ;;
x)
local l=`ulimit -Sc`
case $l in
esac
;;
*)
- echo >&2 "usage: core [yn]"
+ echo >&2 "usage: core [y|n]"
return 1
;;
esac
}
-# --- Turning on and off path hacks ---
+## world [NAME]
+##
+## Set current security world to NAME. With no NAME, print the currently
+## selected world.
+world () {
+ local nfast=${NFAST_HOME-/opt/nfast}
+ local kmdata
+ case "$#" in
+ 0)
+ echo "${NFAST_KMDATA#$nfast/kmdata-}"
+ ;;
+ *)
+ if [ -d "$1" ]; then
+ kmdata=$1
+ elif [ -d "$nfast/kmdata-$1" ]; then
+ kmdata=$nfast/kmdata-$1
+ else
+ echo >&2 "world: can't find world $1"
+ return 1
+ fi
+ shift
+ case "$#" in
+ 0) export NFAST_KMDATA=$kmdata ;;
+ *) "$@" ;;
+ esac
+ ;;
+ esac
+}
+## Fix `man' under Slowaris.
+case "$MACHTYPE" in
+ *solaris*)
+ man () {
+ declare -i i=0
+ declare arg
+ declare -a man
+ for arg; do
+ case "$arg" in [0-9]*) man[i+=1]="-s" ;; esac
+ man[i+=1]="$arg"
+ done
+ command man "${man[@]}"
+ }
+ ;;
+esac
+
+###--------------------------------------------------------------------------
+### Path hacks.
+
+## path-add [VAR] DIR
+##
+## Add DIR to the beginning of PATH-like variable VAR (defaults to PATH) if
+## it's not there already.
path-add () {
local pathvar export dir val
case $# in
$export
}
+## path-remove [VAR] DIR
+##
+## Remove DIR from PATH-like variable VAR (defaults to PATH); it's not an
+## error if DIR isn't in VAR.
path-remove () {
local pathvar export dir val
case $# in
$export
}
+## pathhack [-f] +HACK|-HACK...
+##
+## Each HACK refers to a subdirectory of `~/bin/hacks'. A hack name preceded
+## by `+' adds the directory to the PATH; a `-' removes. Adding a hack
+## that's already on the PATH doesn't do anything unless `-f' is set, in
+## which case it gets moved to the beginning. With no arguments, print the
+## currently installed hacks.
pathhack () {
if [ $# -eq 0 ]; then
local IFS=:
fi
}
-# --- Switching security worlds ---
-
-world () {
- local nfast=${NFAST_HOME-/opt/nfast}
- local kmdata
- case "$#" in
- 0)
- echo "${NFAST_KMDATA#$nfast/kmdata-}"
- ;;
- *)
- if [ -d "$1" ]; then
- kmdata=$1
- elif [ -d "$nfast/kmdata-$1" ]; then
- kmdata=$nfast/kmdata-$1
- else
- echo >&2 "world: can't find world $1"
- return 1
- fi
- shift
- case "$#" in
- 0) export NFAST_KMDATA=$kmdata ;;
- *) "$@" ;;
- esac
- ;;
- esac
-}
-
-# --- Fix `man' under Slowaris ---
-
-case "$MACHTYPE" in
- *solaris*)
- man () {
- declare -i i=0
- declare arg
- declare -a man
-
- for arg; do
- case "$arg" in [0-9]*) man[i+=1]="-s" ;; esac
- man[i+=1]="$arg"
- done
- command man "${man[@]}"
- }
- ;;
-esac
-
-# --- For `root' use -- some simple molly-guards ---
+###--------------------------------------------------------------------------
+### Finishing touches.
+## For `root' use -- some simple molly-guards.
if (( UID == 0 )); then
alias rm='rm -i' cp='cp -i' mv='mv -i'
set -o noclobber
fi
+## Run any local hooks.
[ -f "$HOME/.bashrc-local" ] && . "$HOME/.bashrc-local"
+## Close the `__mdw_bashrc' guard.
fi
+
+###----- That's all, folks --------------------------------------------------
+
+: