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 : ${USER-${LOGNAME-$(id -un)}}
78 __mdw_set_prompt_pieces () {
80 ## Fancy highlighting in some terminals.
81 local bold unbold nl gitcolour rccolour uncolour
83 bold="" unbold="" nl="" gitcolour="" rccolour="" uncolour="" more=""
84 __mdw_set_prompt_hacks
86 ## Choose the right delimiters. Highlight root prompts specially;
87 ## highlight when I'm running as some other user. Highlight when this
88 ## isn't the outermost shell on the terminal.
89 local left right u tty
92 left=$(echo « | iconv -f UTF-8 -t //translit)
93 right=$(echo » | iconv -f UTF-8 -t //translit)
97 mdw | mwooding | nemo) u="" left="[" right="]" ;;
98 *) u="$__mdw_user@" left="{" right="}" ;;
102 "$tty") left="<" right=">" ;;
103 *) __mdw_tty=$tty; export __mdw_tty ;;
108 ## If this session is insecure then highlight that.
111 case ${SSH_CLIENT-nil},${SCHROOT_CHROOT_NAME-nil},$__mdw_sechost in
112 nil,nil,"$h") sec_l="" sec_r="" ;;
113 nil,nil,*) sec_l="(" sec_r=")" ;;
117 ## If this is an schroot environment or some other interesting augmented
118 ## environment then point this out.
120 ## Put together the main pieces.
121 __mdw_prompt_left="$nl$bold$left$sec_l$u$host$__mdw_hqual$sec_r$dir"
122 __mdw_prompt_git_left="$unbold$gitcolour"
123 __mdw_prompt_git_right="$uncolour$bold"
124 __mdw_prompt_rc_left="$unbold$rccolour"
125 __mdw_prompt_rc_right="$uncolour$bold"
126 __mdw_prompt_right="$right$unbold"
127 __mdw_prompt_more=" $more$bold>$unbold "
130 __mdw_set_prompt () {
131 case "${TERM-dumb}:${INSIDE_EMACS+$INSIDE_EMACS}" in
133 case $(id -u) in 0) PS1='# ' ;; *) PS1='$ ' ;; esac
139 if type __git_ps1 >/dev/null 2>&1; then
140 git="$__mdw_prompt_git_left$(__git_ps1)$__mdw_prompt_git_right"
144 case $__mdw_last_rc in
146 *) rc="$__mdw_prompt_rc_left rc=$__mdw_last_rc$__mdw_prompt_rc_right" ;;
148 PS1="$__mdw_prompt_left$git$rc$__mdw_prompt_right"
149 PS2="$PS1$__mdw_prompt_more"
155 __mdw_xterm_settitle () {
157 "
\e]2;%s@%s:%s – %s
\e\\" \
158 "$__mdw_user" "$__mdw_host$__mdw_hqual" "$PWD" \
161 __mdw_xterm_precmd () { __mdw_xterm_settitle "$__mdw_shell"; }
162 __mdw_xterm_preexec () { __mdw_xterm_settitle "$1"; }
164 __mdw_screen_settitle () {
169 __mdw_screen_precmd () { __mdw_screen_settitle "$__mdw_shell"; }
170 __mdw_screen_preexec () { __mdw_screen_settitle "$1"; }
173 case ${STY+t},${__mdw_precmd_hook+t},${__mdw_preexec_hook+t},${TERM} in
175 __mdw_addhook __mdw_precmd_hook __mdw_xterm_precmd
176 __mdw_addhook __mdw_preexec_hook __mdw_xterm_preexec
179 __mdw_addhook __mdw_precmd_hook __mdw_screen_precmd
180 __mdw_addhook __mdw_preexec_hook __mdw_screen_preexec
183 case ${__mdw_precmd_hook+t} in
184 t) __mdw_addhook __mdw_precmd_hook __mdw_set_prompt ;;
188 ###--------------------------------------------------------------------------
189 ### Some handy aliases.
192 alias which="command -v"
195 case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac
199 alias re="rootly $EDITOR"
200 alias pstree="pstree -hl"
201 alias cdtmp='cd ${TMPDIR-/tmp}'
202 alias pushtmp='pushd ${TMPDIR-/tmp}'
204 alias svn="svnwrap svn"
206 alias make="nice make"
207 alias cross-run="nice cross-run"
210 ###--------------------------------------------------------------------------
213 ## Arrange for `ls' output to be in colour.
214 if __mdw_programp dircolors; then eval $(dircolors -b "$HOME/.dircolors")
215 else unset LS_COLORS; fi
217 unalias ls 2>/dev/null || :
219 if [ -t 1 ]; then command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
220 else command ls "$@"; fi
223 ## Arrange for `grep' output to be in colour.
224 export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
229 command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
234 alias grep="greplike grep"
235 alias egrep="greplike egrep"
236 alias fgrep="greplike fgrep"
237 alias zgrep="greplike zgrep"
239 ## Arrange for `diff' output to be in colour.
240 export DIFF_COLORS="hd=1:ln=36:ad=32:de=31"
245 ${DIFF_COLORS+--color=always} \
246 ${DIFF_COLORS+--palette="$DIFF_COLORS"} \
249 command $diff "$@" | cat
252 alias diff="difflike diff"
254 ###--------------------------------------------------------------------------
257 ## Turn off pagers inside Emacs shell buffers.
258 case "$INSIDE_EMACS" in
259 2[2-9].*,comint | [3-9][0-9].*,comint) export PAGER=cat ;;
262 ###--------------------------------------------------------------------------
263 ### More complicated shell functions.
265 ## xt [@HOST] XTERM-ARGS
267 ## Open a terminal, maybe on a remote host.
271 local remote=${1#@} title
273 if [ $# -gt 0 ]; then
274 title="xterm [$remote] $1"
276 title="xterm [$remote]"
278 (xterm -title "$title" -e ssh $remote "$@" &)
288 ## Tweak core dumps on and off, or show the current status.
291 xon|xy|xyes) ulimit -Sc $(ulimit -Hc) ;;
292 xoff|xn|xno) ulimit -Sc 0 ;;
294 local l=$(ulimit -Sc)
296 0) echo "Core dumps disabled" ;;
297 unlimited) echo "Core dumps enabled" ;;
298 *) echo "Core dump limit is $l blocks" ;;
302 echo >&2 "usage: core [y|n]"
310 ## Set current security world to NAME. With no NAME, print the currently
313 local nfast=${NFAST_HOME-/opt/nfast}
317 echo "${NFAST_KMDATA#$nfast/kmdata-}"
322 elif [ -d "$nfast/kmdata-$1" ]; then
323 kmdata=$nfast/kmdata-$1
325 echo >&2 "world: can't find world $1"
330 0) export NFAST_KMDATA=$kmdata ;;
337 ## path-add [VAR] DIR
339 ## Add DIR to the beginning of PATH-like variable VAR (defaults to PATH) if
340 ## it's not there already.
342 local pathvar export dir val
344 1) pathvar=PATH dir=$1 export="export PATH" ;;
345 2) pathvar=$1 dir=$2 export=: ;;
346 *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;;
357 ## path-remove [VAR] DIR
359 ## Remove DIR from PATH-like variable VAR (defaults to PATH); it's not an
360 ## error if DIR isn't in VAR.
362 local pathvar export dir val
364 1) pathvar=PATH dir=$1 export="export PATH" ;;
365 2) pathvar=$1 dir=$2 export=: ;;
366 *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;;
371 :"$dir":*) val=${val#$dir:} ;;
372 *:"$dir":) val=${val%:$dir} ;;
373 *:"$dir":*) val=${val%%:$dir:*}:${val#*:$dir:} ;;
379 ## pathhack [-f] +HACK|-HACK...
381 ## Each HACK refers to a subdirectory of `~/bin/hacks'. A hack name preceded
382 ## by `+' adds the directory to the PATH; a `-' removes. Adding a hack
383 ## that's already on the PATH doesn't do anything unless `-f' is set, in
384 ## which case it gets moved to the beginning. With no arguments, print the
385 ## currently installed hacks.
387 local p e force arg hack dir
389 if [ $# -eq 0 ]; then
392 case "$e" in "$HOME/bin/hacks/"*) echo ${e#$HOME/bin/hacks/} ;; esac
393 case "$p" in *:*) p=${p#*:} ;; *) break ;; esac
398 while [ $# -gt 0 ]; do
401 -f | --force) force=t; shift; continue ;;
407 dir=$HOME/bin/hacks/$hack
408 if ! [ -d "$dir" ]; then
409 echo "$0: path hack $hack not found"
412 case "$arg,$force,:$PATH:" in
413 -*,*,*:"$dir":*) path_remove p "$dir" ;;
414 +*,t,*:"$dir":*) path_remove p "$dir"; path_add p "$dir" ;;
415 +*,nil,*:"$dir":*) ;;
416 +*,*) path_add p "$dir" ;;
420 if [ $# -eq 0 ]; then PATH=$p; export PATH
421 else PATH=$p "$@"; fi
424 ###--------------------------------------------------------------------------
425 ### Finishing touches.
427 ## Make sure `$HOME/bin' is on the path.
430 ## Set the temporary directory again. (A setuid or setgid program may have
431 ## unhelpfully forgotten this for us.)
434 *) if __mdw_programp tmpdir; then eval $(tmpdir -b); fi ;;
437 ## For `root' use -- some simple molly-guards.
440 alias rm="rm -i" cp="cp -i" mv="mv -i"
445 ## Run any local hooks.
446 __mdw_source_if_exists "$HOME/.shell-local"
448 ###----- That's all, folks --------------------------------------------------