3 ### Common per-shell configuration.
5 ###--------------------------------------------------------------------------
8 __mdw_programp () { type >/dev/null 2>&1 "$1"; }
10 __mdw_source_if_exists () {
13 if [ -r "$i" ]; then . "$i"; fi
17 ###--------------------------------------------------------------------------
24 case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac
29 *) eval "$hk=\${$hk:+\$$hk }\$fn" ;;
34 local hk=$1 fn=$2 t l r
37 case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac
42 l=${t%% $fn*} r=${t##*$fn }
44 eval "$hk=\$l\${l:+ }\$r"
49 __mdw_setrc () { return $1; }
52 local hk=$1 saverc=$? t i; shift
55 case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac
58 for i in $t; do __mdw_setrc $saverc; "$i" "$@"; done
61 ###--------------------------------------------------------------------------
64 __mdw_host=$(hostname)
66 __mdw_hqual=$__mdw_hqual${SCHROOT_CHROOT_NAME+/$SCHROOT_CHROOT_NAME}
67 __mdw_hqual=$__mdw_hqual${CROSS_BUILDENV+/$CROSS_BUILDENV}
68 __mdw_set_prompt_hacks () { host=$__mdw_host; dir=""; }
70 __mdw_system=$(uname -s)
71 : ${USER-${LOGNAME-$(id -un)}}
82 *" 544 "*) __mdw_rootp=t __mdw_user="$__mdw_user%admin" ;;
92 __mdw_set_prompt_pieces () {
94 ## Fancy highlighting in some terminals.
95 local bold unbold nl gitcolour rccolour uncolour
97 bold="" unbold="" nl="" gitcolour="" rccolour="" uncolour="" more=""
98 __mdw_set_prompt_hacks
100 ## Choose the right delimiters. Highlight root prompts specially;
101 ## highlight when I'm running as some other user. Highlight when this
102 ## isn't the outermost shell on the terminal.
103 local left right u tty
106 left=$(echo « | iconv -f UTF-8 -t //translit)
107 right=$(echo » | iconv -f UTF-8 -t //translit)
111 mdw | mwooding | nemo) u="" left="[" right="]" ;;
112 *) u="$__mdw_user@" left="{" right="}" ;;
116 "$tty") left="<" right=">" ;;
117 *) __mdw_tty=$tty; export __mdw_tty ;;
122 ## If this session is insecure then highlight that.
125 case ${SSH_CLIENT-nil},${SCHROOT_CHROOT_NAME-nil},$__mdw_sechost in
126 nil,nil,"$h") sec_l="" sec_r="" ;;
127 nil,nil,*) sec_l="(" sec_r=")" ;;
131 ## If this is an schroot environment or some other interesting augmented
132 ## environment then point this out.
134 ## Put together the main pieces.
135 __mdw_prompt_left="$nl$bold$left$sec_l$u$host$__mdw_hqual$sec_r$dir"
136 __mdw_prompt_git_left="$unbold$gitcolour"
137 __mdw_prompt_git_right="$uncolour$bold"
138 __mdw_prompt_rc_left="$unbold$rccolour"
139 __mdw_prompt_rc_right="$uncolour$bold"
140 __mdw_prompt_right="$right$unbold"
141 __mdw_prompt_more=" $more$bold>$unbold "
144 __mdw_set_prompt () {
145 case "${TERM-dumb}:${INSIDE_EMACS+$INSIDE_EMACS}" in
147 case $(id -u) in 0) PS1='# ' ;; *) PS1='$ ' ;; esac
153 if type __git_ps1 >/dev/null 2>&1; then
154 git="$__mdw_prompt_git_left$(__git_ps1)$__mdw_prompt_git_right"
158 case $__mdw_last_rc in
160 *) rc="$__mdw_prompt_rc_left rc=$__mdw_last_rc$__mdw_prompt_rc_right" ;;
162 PS1="$__mdw_prompt_left$git$rc$__mdw_prompt_right"
163 PS2="$PS1$__mdw_prompt_more"
169 __mdw_xterm_settitle () {
171 "
\e]2;%s@%s:%s – %s
\e\\" \
172 "$__mdw_user" "$__mdw_host$__mdw_hqual" "$PWD" \
175 __mdw_xterm_precmd () { __mdw_xterm_settitle "$__mdw_shell"; }
176 __mdw_xterm_preexec () { __mdw_xterm_settitle "$1"; }
178 __mdw_screen_settitle () {
183 __mdw_screen_precmd () { __mdw_screen_settitle "$__mdw_shell"; }
184 __mdw_screen_preexec () { __mdw_screen_settitle "$1"; }
187 case ${STY+t},${__mdw_precmd_hook+t},${__mdw_preexec_hook+t},${TERM} in
189 __mdw_addhook __mdw_precmd_hook __mdw_xterm_precmd
190 __mdw_addhook __mdw_preexec_hook __mdw_xterm_preexec
193 __mdw_addhook __mdw_precmd_hook __mdw_screen_precmd
194 __mdw_addhook __mdw_preexec_hook __mdw_screen_preexec
197 case ${__mdw_precmd_hook+t} in
198 t) __mdw_addhook __mdw_precmd_hook __mdw_set_prompt ;;
202 ###--------------------------------------------------------------------------
203 ### Some handy aliases.
206 alias which="command -v"
209 case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac
213 alias re="rootly $EDITOR"
214 alias pstree="pstree -hl"
215 alias cdtmp='cd ${TMPDIR-/tmp}'
216 alias pushtmp='pushd ${TMPDIR-/tmp}'
218 alias svn="svnwrap svn"
220 alias make="nice make"
221 alias cross-run="nice cross-run"
224 ## Shut up Lisp interpreters.
225 alias clisp="clisp -q -q"
226 alias cmucl="rlwrap cmucl -quiet"
227 alias ecl="rlwrap ecl"
228 alias sbcl="rlwrap sbcl --noinform"
229 alias ccl="rlwrap ccl"
230 alias ccl32="rlwrap ccl32"
231 alias ccl64="rlwrap ccl64"
232 alias abcl="rlwrap abcl --noinform"
234 ###--------------------------------------------------------------------------
237 ## Arrange for `ls' output to be in colour.
238 if __mdw_programp dircolors; then eval $(dircolors -b "$HOME/.dircolors")
239 else unset LS_COLORS; fi
241 unalias ls 2>/dev/null || :
243 if [ -t 1 ]; then command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
244 else command ls "$@"; fi
247 ## Arrange for `grep' output to be in colour.
248 export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
253 command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
258 alias grep="greplike grep"
259 alias egrep="greplike egrep"
260 alias fgrep="greplike fgrep"
261 alias zgrep="greplike zgrep"
263 ## Arrange for `diff' output to be in colour.
264 export DIFF_COLORS="hd=1:ln=36:ad=32:de=31"
269 ${DIFF_COLORS+--color=always} \
270 ${DIFF_COLORS+--palette="$DIFF_COLORS"} \
273 command $diff "$@" | cat
276 alias diff="difflike diff"
278 ###--------------------------------------------------------------------------
281 ## Turn off pagers inside Emacs shell buffers.
282 case "$INSIDE_EMACS" in
283 2[2-9].*,comint | [3-9][0-9].*,comint) export PAGER=cat ;;
286 ###--------------------------------------------------------------------------
287 ### More complicated shell functions.
289 ## xt [@HOST] XTERM-ARGS
291 ## Open a terminal, maybe on a remote host.
295 local remote=${1#@} title
297 if [ $# -gt 0 ]; then
298 title="xterm [$remote] $1"
300 title="xterm [$remote]"
302 (xterm -title "$title" -e ssh $remote "$@" &)
312 ## Tweak core dumps on and off, or show the current status.
315 xon|xy|xyes) ulimit -Sc $(ulimit -Hc) ;;
316 xoff|xn|xno) ulimit -Sc 0 ;;
318 local l=$(ulimit -Sc)
320 0) echo "Core dumps disabled" ;;
321 unlimited) echo "Core dumps enabled" ;;
322 *) echo "Core dump limit is $l blocks" ;;
326 echo >&2 "usage: core [y|n]"
334 ## Set current security world to NAME. With no NAME, print the currently
337 local nfast=${NFAST_HOME-/opt/nfast}
341 echo "${NFAST_KMDATA#$nfast/kmdata-}"
346 elif [ -d "$nfast/kmdata-$1" ]; then
347 kmdata=$nfast/kmdata-$1
349 echo >&2 "world: can't find world $1"
354 0) export NFAST_KMDATA=$kmdata ;;
361 ## path-add [VAR] DIR
363 ## Add DIR to the beginning of PATH-like variable VAR (defaults to PATH) if
364 ## it's not there already.
366 local pathvar export dir val
368 1) pathvar=PATH dir=$1 export="export PATH" ;;
369 2) pathvar=$1 dir=$2 export=: ;;
370 *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;;
381 ## path-remove [VAR] DIR
383 ## Remove DIR from PATH-like variable VAR (defaults to PATH); it's not an
384 ## error if DIR isn't in VAR.
386 local pathvar export dir val
388 1) pathvar=PATH dir=$1 export="export PATH" ;;
389 2) pathvar=$1 dir=$2 export=: ;;
390 *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;;
395 :"$dir":*) val=${val#$dir:} ;;
396 *:"$dir":) val=${val%:$dir} ;;
397 *:"$dir":*) val=${val%%:$dir:*}:${val#*:$dir:} ;;
403 ## pathhack [-f] +HACK|-HACK...
405 ## Each HACK refers to a subdirectory of `~/bin/hacks'. A hack name preceded
406 ## by `+' adds the directory to the PATH; a `-' removes. Adding a hack
407 ## that's already on the PATH doesn't do anything unless `-f' is set, in
408 ## which case it gets moved to the beginning. With no arguments, print the
409 ## currently installed hacks.
411 local p e force arg hack dir
413 if [ $# -eq 0 ]; then
416 case "$e" in "$HOME/bin/hacks/"*) echo ${e#$HOME/bin/hacks/} ;; esac
417 case "$p" in *:*) p=${p#*:} ;; *) break ;; esac
422 while [ $# -gt 0 ]; do
425 -f | --force) force=t; shift; continue ;;
431 dir=$HOME/bin/hacks/$hack
432 if ! [ -d "$dir" ]; then
433 echo "$0: path hack $hack not found"
436 case "$arg,$force,:$PATH:" in
437 -*,*,*:"$dir":*) path_remove p "$dir" ;;
438 +*,t,*:"$dir":*) path_remove p "$dir"; path_add p "$dir" ;;
439 +*,nil,*:"$dir":*) ;;
440 +*,*) path_add p "$dir" ;;
444 if [ $# -eq 0 ]; then PATH=$p; export PATH
445 else PATH=$p "$@"; fi
448 ###--------------------------------------------------------------------------
449 ### Finishing touches.
451 ## Make sure `$HOME/bin' is on the path.
454 ## Set the temporary directory again. (A setuid or setgid program may have
455 ## unhelpfully forgotten this for us.)
458 *) if __mdw_programp tmpdir; then eval $(tmpdir -b); fi ;;
461 ## For `root' use -- some simple molly-guards.
464 alias rm="rm -i" cp="cp -i" mv="mv -i"
469 ## Run any local hooks.
470 __mdw_source_if_exists "$HOME/.shell-local"
472 ###----- That's all, folks --------------------------------------------------