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)
65 __mdw_set_prompt_hacks () { host=$__mdw_host; dir=""; }
67 __mdw_set_prompt_pieces () {
71 ## Fancy highlighting in some terminals.
72 local bold unbold nl gitcolour rccolour uncolour
74 bold="" unbold="" nl="" gitcolour="" rccolour="" uncolour="" more=""
75 __mdw_set_prompt_hacks
77 ## Choose the right delimiters. Highlight root prompts specially;
78 ## highlight when I'm running as some other user. Highlight when this
79 ## isn't the outermost shell on the terminal.
80 local left right user u tty
81 user=${USER-${LOGNAME-$(id -un)}}
84 left=$(echo « | iconv -f UTF-8 -t //translit)
85 right=$(echo » | iconv -f UTF-8 -t //translit)
89 mdw | mwooding | nemo) u="" left="[" right="]" ;;
90 *) u="$user@" left="{" right="}" ;;
94 "$tty") left="<" right=">" ;;
95 *) __mdw_tty=$tty; export __mdw_tty ;;
100 ## If this session is insecure then highlight that.
103 case ${SSH_CLIENT-nil},${SCHROOT_CHROOT_NAME-nil},$__mdw_sechost in
104 nil,nil,"$h") sec_l="" sec_r="" ;;
105 nil,nil,*) sec_l="(" sec_r=")" ;;
109 ## If this is an schroot environment or some other interesting augmented
110 ## environment then point this out.
111 hqual="$hqual${SCHROOT_CHROOT_NAME+/$SCHROOT_CHROOT_NAME}"
112 hqual="$hqual${CROSS_BUILDENV+/$CROSS_BUILDENV}"
114 ## Put together the main pieces.
115 __mdw_prompt_left="$nl$bold$left$sec_l$u$host$hqual$sec_r$dir"
116 __mdw_prompt_git_left="$unbold$gitcolour"
117 __mdw_prompt_git_right="$uncolour$bold"
118 __mdw_prompt_rc_left="$unbold$rccolour"
119 __mdw_prompt_rc_right="$uncolour$bold"
120 __mdw_prompt_right="$right$unbold"
121 __mdw_prompt_more=" $more$bold>$unbold "
124 __mdw_set_prompt () {
125 case "${TERM-dumb}:${INSIDE_EMACS+$INSIDE_EMACS}" in
127 case $(id -u) in 0) PS1='# ' ;; *) PS1='$ ' ;; esac
133 if type __git_ps1 >/dev/null 2>&1; then
134 git="$__mdw_prompt_git_left$(__git_ps1)$__mdw_prompt_git_right"
138 case $__mdw_last_rc in
140 *) rc="$__mdw_prompt_rc_left rc=$__mdw_last_rc$__mdw_prompt_rc_right" ;;
142 PS1="$__mdw_prompt_left$git$rc$__mdw_prompt_right"
143 PS2="$PS1$__mdw_prompt_more"
149 __mdw_screen_precmd () { printf "
\ek%s
\e\\" "$__mdw_shell"; }
150 __mdw_screen_preexec () { printf "
\ek%s
\e\\" "$1"; }
153 case ${STY+t},${__mdw_precmd_hook+t},${__mdw_preexec_hook+t} in
155 __mdw_addhook __mdw_precmd_hook __mdw_screen_precmd
156 __mdw_addhook __mdw_preexec_hook __mdw_screen_preexec
159 case ${__mdw_precmd_hook+t} in
160 t) __mdw_addhook __mdw_precmd_hook __mdw_set_prompt ;;
164 ###--------------------------------------------------------------------------
165 ### Some handy aliases.
168 alias which="command -v"
171 case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac
175 alias re="rootly $EDITOR"
176 alias pstree="pstree -hl"
177 alias cdtmp='cd ${TMPDIR-/tmp}'
178 alias pushtmp='pushd ${TMPDIR-/tmp}'
180 alias svn="svnwrap svn"
182 alias make="nice make"
185 ###--------------------------------------------------------------------------
188 ## Arrange for `ls' output to be in colour.
189 if __mdw_programp dircolors; then eval $(dircolors -b "$HOME/.dircolors")
190 else unset LS_COLORS; fi
192 unalias ls 2>/dev/null || :
194 if [ -t 1 ]; then command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
195 else command ls "$@"; fi
198 ## Arrange for `grep' output to be in colour.
199 export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
204 command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
209 alias grep="greplike grep"
210 alias egrep="greplike egrep"
211 alias fgrep="greplike fgrep"
212 alias zgrep="greplike zgrep"
214 ## Arrange for `diff' output to be in colour.
215 export DIFF_COLORS="hd=1:ln=36:ad=32:de=31"
220 ${DIFF_COLORS+--color=always} \
221 ${DIFF_COLORS+--palette="$DIFF_COLORS"} \
224 command $diff "$@" | cat
227 alias diff="difflike diff"
229 ###--------------------------------------------------------------------------
232 ## Turn off pagers inside Emacs shell buffers.
233 case "$INSIDE_EMACS" in
234 2[2-9].*,comint | [3-9][0-9].*,comint) export PAGER=cat ;;
237 ###--------------------------------------------------------------------------
238 ### More complicated shell functions.
240 ## xt [@HOST] XTERM-ARGS
242 ## Open a terminal, maybe on a remote host.
246 local remote=${1#@} title
248 if [ $# -gt 0 ]; then
249 title="xterm [$remote] $1"
251 title="xterm [$remote]"
253 (xterm -title "$title" -e ssh $remote "$@" &)
263 ## Tweak core dumps on and off, or show the current status.
266 xon|xy|xyes) ulimit -Sc $(ulimit -Hc) ;;
267 xoff|xn|xno) ulimit -Sc 0 ;;
269 local l=$(ulimit -Sc)
271 0) echo "Core dumps disabled" ;;
272 unlimited) echo "Core dumps enabled" ;;
273 *) echo "Core dump limit is $l blocks" ;;
277 echo >&2 "usage: core [y|n]"
285 ## Set current security world to NAME. With no NAME, print the currently
288 local nfast=${NFAST_HOME-/opt/nfast}
292 echo "${NFAST_KMDATA#$nfast/kmdata-}"
297 elif [ -d "$nfast/kmdata-$1" ]; then
298 kmdata=$nfast/kmdata-$1
300 echo >&2 "world: can't find world $1"
305 0) export NFAST_KMDATA=$kmdata ;;
312 ## path-add [VAR] DIR
314 ## Add DIR to the beginning of PATH-like variable VAR (defaults to PATH) if
315 ## it's not there already.
317 local pathvar export dir val
319 1) pathvar=PATH dir=$1 export="export PATH" ;;
320 2) pathvar=$1 dir=$2 export=: ;;
321 *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;;
332 ## path-remove [VAR] DIR
334 ## Remove DIR from PATH-like variable VAR (defaults to PATH); it's not an
335 ## error if DIR isn't in VAR.
337 local pathvar export dir val
339 1) pathvar=PATH dir=$1 export="export PATH" ;;
340 2) pathvar=$1 dir=$2 export=: ;;
341 *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;;
346 :"$dir":*) val=${val#$dir:} ;;
347 *:"$dir":) val=${val%:$dir} ;;
348 *:"$dir":*) val=${val%%:$dir:*}:${val#*:$dir:} ;;
354 ## pathhack [-f] +HACK|-HACK...
356 ## Each HACK refers to a subdirectory of `~/bin/hacks'. A hack name preceded
357 ## by `+' adds the directory to the PATH; a `-' removes. Adding a hack
358 ## that's already on the PATH doesn't do anything unless `-f' is set, in
359 ## which case it gets moved to the beginning. With no arguments, print the
360 ## currently installed hacks.
362 local p e force arg hack dir
364 if [ $# -eq 0 ]; then
367 case "$e" in "$HOME/bin/hacks/"*) echo ${e#$HOME/bin/hacks/} ;; esac
368 case "$p" in *:*) p=${p#*:} ;; *) break ;; esac
373 while [ $# -gt 0 ]; do
376 -f | --force) force=t; shift; continue ;;
382 dir=$HOME/bin/hacks/$hack
383 if ! [ -d "$dir" ]; then
384 echo "$0: path hack $hack not found"
387 case "$arg,$force,:$PATH:" in
388 -*,*,*:"$dir":*) path_remove p "$dir" ;;
389 +*,t,*:"$dir":*) path_remove p "$dir"; path_add p "$dir" ;;
390 +*,nil,*:"$dir":*) ;;
391 +*,*) path_add p "$dir" ;;
395 if [ $# -eq 0 ]; then PATH=$p; export PATH
396 else PATH=$p "$@"; fi
399 ###--------------------------------------------------------------------------
400 ### Finishing touches.
402 ## Make sure `$HOME/bin' is on the path.
405 ## Set the temporary directory again. (A setuid or setgid program may have
406 ## unhelpfully forgotten this for us.)
409 *) if __mdw_programp tmpdir; then eval $(tmpdir -b); fi ;;
412 ## For `root' use -- some simple molly-guards.
415 alias rm="rm -i" cp="cp -i" mv="mv -i"
420 ## Run any local hooks.
421 __mdw_source_if_exists "$HOME/.shell-local"
423 ###----- That's all, folks --------------------------------------------------