### -*-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 } ###-------------------------------------------------------------------------- ### Prompt machinery. __mdw_set_prompt_hacks () { host=$(hostname); dir=""; } __mdw_set_prompt_pieces () { local hqual hqual="" ## Fancy highlighting in some terminals. local bold unbold nl gitcolour rccolour uncolour local host dir more bold="" unbold="" nl="" gitcolour="" rccolour="" uncolour="" more="" __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 user u tty user=${USER-${LOGNAME-$(id -un)}} case $(id -u) in 0) left=$(echo « | iconv -f UTF-8 -t //translit) right=$(echo » | iconv -f UTF-8 -t //translit) ;; *) case $user in mdw | mwooding | nemo) u="" left="[" right="]" ;; *) u="$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. hqual="$hqual${SCHROOT_CHROOT_NAME+/$SCHROOT_CHROOT_NAME}" hqual="$hqual${MDW_BUILDENV+/$MDW_BUILDENV}" ## Put together the main pieces. __mdw_prompt_left="$nl$bold$left$sec_l$u$host$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_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 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 PS1="$__mdw_prompt_left$git$rc$__mdw_prompt_right" PS2="$PS1$__mdw_prompt_more" unset __mdw_last_rc ;; esac } __mdw_precmd () { __mdw_set_prompt case ${STY+t} in t) printf "k%s\\" "$__mdw_shell" ;; esac } __mdw_preexec () { case ${STY+t} in t) printf "k%s\\" "$1" ;; esac } ###-------------------------------------------------------------------------- ### 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" ###-------------------------------------------------------------------------- ### 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" ###-------------------------------------------------------------------------- ### 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 --------------------------------------------------