### -*-sh-*- ### ### Common per-shell configuration. ###-------------------------------------------------------------------------- ### Utilities. __mdw_programp () { type >/dev/null 2>&1 "$1"; } __mdw_source_if_exists () { local i for i in "$@"; do if [ -r "$i" ]; then . "$i"; fi done } ###-------------------------------------------------------------------------- ### Hooks. __mdw_addhook () { local hk=$1 fn=$2 t eval t=\${$hk+t} case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac eval t=\$$hk case " $t " in *" $fn "*) ;; *) eval "$hk=\${$hk:+\$$hk }\$fn" ;; esac } __mdw_delhook () { local hk=$1 fn=$2 t l r eval t=\${$hk+t} case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac eval t=\" \$$hk \" case $t in *" $fn "*) l=${t%% $fn*} r=${t##*$fn } l=${l# } r=${r% } eval "$hk=\$l\${l:+ }\$r" ;; esac } __mdw_setrc () { return $1; } __mdw_runhook () { local hk=$1 saverc=$? t i; shift eval t=\${$hk+t} case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac eval t=\$$hk for i in $t; do __mdw_setrc $saverc; "$i" "$@"; done } ###-------------------------------------------------------------------------- ### Prompt machinery. __mdw_host=$(hostname) __mdw_hqual= __mdw_hqual=$__mdw_hqual${SCHROOT_CHROOT_NAME+/$SCHROOT_CHROOT_NAME} __mdw_hqual=$__mdw_hqual${CROSS_BUILDENV+/$CROSS_BUILDENV} __mdw_set_prompt_hacks () { host=$__mdw_host; dir=""; } __mdw_system=$(uname -s) : ${USER-${LOGNAME-$(id -un)}} __mdw_user=$USER case $(id -u) in 0) __mdw_rootp=t ;; *) case $__mdw_system in CYGWIN_*) case " $(id -G) " in *" 544 "*) __mdw_rootp=t __mdw_user="$__mdw_user%admin" ;; *) __mdw_rootp=nil ;; esac ;; *) __mdw_rootp=nil ;; esac esac __mdw_set_prompt_pieces () { ## Fancy highlighting in some terminals. local bold unbold nl more host dir local gitcolour extracolour rccolour uncolour bold="" unbold="" nl="" more="" gitcolour="" extracolour="" rccolour="" uncolour="" __mdw_set_prompt_hacks ## 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. local left right u tty case $__mdw_rootp in t) left=$(echo « | iconv -f UTF-8 -t //translit) right=$(echo » | iconv -f UTF-8 -t //translit) ;; nil) case $USER in mdw | mwooding | nemo) u="" left="[" right="]" ;; *) u="$__mdw_user@" left="{" right="}" ;; esac tty=$(tty) case "$__mdw_tty" in "$tty") left="<" right=">" ;; *) __mdw_tty=$tty; export __mdw_tty ;; esac ;; esac ## If this session is insecure then highlight that. local sec_l sec_r h h=$(hostname) case ${SSH_CLIENT-nil},${SCHROOT_CHROOT_NAME-nil},$__mdw_sechost in nil,nil,"$h") sec_l="" sec_r="" ;; nil,nil,*) sec_l="(" sec_r=")" ;; *) sec_l="" sec_r="" esac ## If this is an schroot environment or some other interesting augmented ## environment then point this out. ## Put together the main pieces. __mdw_prompt_left="$nl$bold$left$sec_l$u$host$__mdw_hqual$sec_r$dir" __mdw_prompt_git_left="$unbold$gitcolour" __mdw_prompt_git_right="$uncolour$bold" __mdw_prompt_rc_left="$unbold$rccolour" __mdw_prompt_rc_right="$uncolour$bold" __mdw_prompt_extra_left="$unbold$extracolour" __mdw_prompt_extra_right="$uncolour$bold" __mdw_prompt_right="$right$unbold" __mdw_prompt_more=" $more$bold>$unbold " } __mdw_set_prompt () { case "${TERM-dumb}:${INSIDE_EMACS+$INSIDE_EMACS}" in dumb:) case $(id -u) in 0) PS1='# ' ;; *) PS1='$ ' ;; esac PS2='> ' ;; *) __mdw_last_rc=$? local git extra rc if type __git_ps1 >/dev/null 2>&1; then git="$__mdw_prompt_git_left$(__git_ps1)$__mdw_prompt_git_right" else git="" fi case $__mdw_last_rc in 0) rc="" ;; *) rc="$__mdw_prompt_rc_left rc=$__mdw_last_rc$__mdw_prompt_rc_right" ;; esac case $__mdw_prompt_extra in ?*) extra="$__mdw_prompt_extra_left [$__mdw_prompt_extra]$__mdw_prompt_extra_right" ;; *) extra="" ;; esac PS1="$__mdw_prompt_left$git$extra$rc$__mdw_prompt_right" PS2="$PS1$__mdw_prompt_more" unset __mdw_last_rc ;; esac } __mdw_xterm_settitle () { printf >/dev/tty \ "]2;%s@%s:%s – %s\\" \ "$__mdw_user" "$__mdw_host$__mdw_hqual" "$PWD" \ "$1" } __mdw_xterm_precmd () { __mdw_xterm_settitle "$__mdw_shell"; } __mdw_xterm_preexec () { __mdw_xterm_settitle "$1"; } __mdw_screen_settitle () { printf >/dev/tty \ "k%s\\" \ "$1" } __mdw_screen_precmd () { __mdw_screen_settitle "$__mdw_shell"; } __mdw_screen_preexec () { __mdw_screen_settitle "$1"; } if [ -t 0 ]; then case ${STY+t},${__mdw_precmd_hook+t},${__mdw_preexec_hook+t},${TERM} in ,t,t,xterm*) __mdw_addhook __mdw_precmd_hook __mdw_xterm_precmd __mdw_addhook __mdw_preexec_hook __mdw_xterm_preexec ;; t,t,t,*) __mdw_addhook __mdw_precmd_hook __mdw_screen_precmd __mdw_addhook __mdw_preexec_hook __mdw_screen_preexec ;; esac case ${__mdw_precmd_hook+t} in t) __mdw_addhook __mdw_precmd_hook __mdw_set_prompt ;; esac fi ###-------------------------------------------------------------------------- ### Some handy aliases. alias cx='chmod +x' alias which="command -v" alias rc="rc -l" 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" alias make="nice make" alias cross-run="nice cross-run" alias gdb="gdb -q" ## Shut up Lisp interpreters. alias clisp="clisp -q -q" alias cmucl="rlwrap cmucl -quiet" alias ecl="rlwrap ecl" alias sbcl="rlwrap sbcl --noinform" alias ccl="rlwrap ccl" alias ccl32="rlwrap ccl32" alias ccl64="rlwrap ccl64" alias abcl="rlwrap abcl --noinform" ###-------------------------------------------------------------------------- ### Colour output. ## Arrange for `ls' output to be in colour. if __mdw_programp dircolors; then eval $(dircolors -b "$HOME/.dircolors") else unset LS_COLORS; fi unalias ls 2>/dev/null || : ls () { if [ -t 1 ]; then command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@" else command ls "$@"; fi } ## 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 () { local grep=$1; shift if [ -t 1 ]; then command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager else command $grep "$@" fi } alias grep="greplike grep" alias egrep="greplike egrep" alias fgrep="greplike fgrep" alias zgrep="greplike zgrep" ## Arrange for `diff' output to be in colour. export DIFF_COLORS="hd=1:ln=36:ad=32:de=31" difflike () { local diff=$1; shift if [ -t 1 ]; then command $diff \ ${DIFF_COLORS+--color=always} \ ${DIFF_COLORS+--palette="$DIFF_COLORS"} \ "$@" | mdw-pager else command $diff "$@" | cat fi } alias diff="difflike diff" ###-------------------------------------------------------------------------- ### Other hacks. ## Turn off pagers inside Emacs shell buffers. case "$INSIDE_EMACS" in 2[2-9].*,comint | [3-9][0-9].*,comint) export PAGER=cat ;; esac ###-------------------------------------------------------------------------- ### More complicated shell functions. ## xt [@HOST] XTERM-ARGS ## ## Open a terminal, maybe on a remote host. xt () { case "$1" in @*) local remote=${1#@} title shift if [ $# -gt 0 ]; then title="xterm [$remote] $1" else title="xterm [$remote]" fi (xterm -title "$title" -e ssh $remote "$@" &) ;; *) (xterm "$@" &) ;; esac } ## 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 ;; x) local l=$(ulimit -Sc) case $l in 0) echo "Core dumps disabled" ;; unlimited) echo "Core dumps enabled" ;; *) echo "Core dump limit is $l blocks" ;; esac ;; *) echo >&2 "usage: core [y|n]" return 1 ;; esac } ## 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 } ## 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 1) pathvar=PATH dir=$1 export="export PATH" ;; 2) pathvar=$1 dir=$2 export=: ;; *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;; esac eval val=\$$pathvar case ":$val:" in *:"$dir":*) ;; *) val=$dir:$val ;; esac eval $pathvar=\$val eval $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 1) pathvar=PATH dir=$1 export="export PATH" ;; 2) pathvar=$1 dir=$2 export=: ;; *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;; esac eval val=\$$pathvar case ":$val:" in :"$dir":) val= ;; :"$dir":*) val=${val#$dir:} ;; *:"$dir":) val=${val%:$dir} ;; *:"$dir":*) val=${val%%:$dir:*}:${val#*:$dir:} ;; esac eval $pathvar=\$val eval $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 () { local p e force arg hack dir p=$PATH if [ $# -eq 0 ]; then while :; do e=${p%%:*} case "$e" in "$HOME/bin/hacks/"*) echo ${e#$HOME/bin/hacks/} ;; esac case "$p" in *:*) p=${p#*:} ;; *) break ;; esac done return fi force=nil while [ $# -gt 0 ]; do arg=$1 case "$arg" in -f | --force) force=t; shift; continue ;; --) shift; break ;; [-+]*) ;; *) break; ;; esac hack=${arg#[+-]} dir=$HOME/bin/hacks/$hack if ! [ -d "$dir" ]; then echo "$0: path hack $hack not found" return 1 fi case "$arg,$force,:$PATH:" in -*,*,*:"$dir":*) path_remove p "$dir" ;; +*,t,*:"$dir":*) path_remove p "$dir"; path_add p "$dir" ;; +*,nil,*:"$dir":*) ;; +*,*) path_add p "$dir" ;; esac shift done if [ $# -eq 0 ]; then PATH=$p; export PATH else PATH=$p "$@"; fi } ###-------------------------------------------------------------------------- ### Finishing touches. ## Make sure `$HOME/bin' is on the path. path_add "$HOME/bin" ## Set the temporary directory again. (A setuid or setgid program may have ## unhelpfully forgotten this for us.) case ${TMPDIR+t} in t) ;; *) if __mdw_programp tmpdir; then eval $(tmpdir -b); fi ;; esac ## For `root' use -- some simple molly-guards. case $(id -u) in 0) alias rm="rm -i" cp="cp -i" mv="mv -i" set -o noclobber ;; esac ## Run any local hooks. __mdw_source_if_exists "$HOME/.shell-local" ###----- That's all, folks --------------------------------------------------