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 ###--------------------------------------------------------------------------
20 __mdw_set_prompt_hacks () { host=$(hostname); dir=""; }
22 __mdw_set_prompt_pieces () {
26 ## Fancy highlighting in some terminals.
27 local bold unbold nl gitcolour rccolour uncolour
29 bold="" unbold="" nl="" gitcolour="" rccolour="" uncolour="" more=""
30 __mdw_set_prompt_hacks
32 ## Choose the right delimiters. Highlight root prompts specially;
33 ## highlight when I'm running as some other user. Highlight when this
34 ## isn't the outermost shell on the terminal.
35 local left right user u tty
36 user=${USER-${LOGNAME-$(id -un)}}
39 left=$(echo « | iconv -f UTF-8 -t //translit)
40 right=$(echo » | iconv -f UTF-8 -t //translit)
44 mdw | mwooding | nemo) u="" left="[" right="]" ;;
45 *) u="$user@" left="{" right="}" ;;
49 "$tty") left="<" right=">" ;;
50 *) __mdw_tty=$tty; export __mdw_tty ;;
55 ## If this session is insecure then highlight that.
58 case ${SSH_CLIENT-nil},${SCHROOT_CHROOT_NAME-nil},$__mdw_sechost in
59 nil,nil,"$h") sec_l="" sec_r="" ;;
60 nil,nil,*) sec_l="(" sec_r=")" ;;
64 ## If this is an schroot environment then point this out.
65 hqual="$hqual${SCHROOT_CHROOT_NAME+/$SCHROOT_CHROOT_NAME}"
67 ## Put together the main pieces.
68 __mdw_prompt_left="$nl$bold$left$sec_l$u$host$hqual$sec_r$dir"
69 __mdw_prompt_git_left="$unbold$gitcolour"
70 __mdw_prompt_git_right="$uncolour$bold"
71 __mdw_prompt_rc_left="$unbold$rccolour"
72 __mdw_prompt_rc_right="$uncolour$bold"
73 __mdw_prompt_right="$right$unbold"
74 __mdw_prompt_more=" $more$bold>$unbold "
78 case "${TERM-dumb}:${INSIDE_EMACS+$INSIDE_EMACS}" in
80 case $(id -u) in 0) PS1='# ' ;; *) PS1='$ ' ;; esac
86 if type __git_ps1 >/dev/null 2>&1; then
87 git="$__mdw_prompt_git_left$(__git_ps1)$__mdw_prompt_git_right"
91 case $__mdw_last_rc in
93 *) rc="$__mdw_prompt_rc_left rc=$__mdw_last_rc$__mdw_prompt_rc_right" ;;
95 PS1="$__mdw_prompt_left$git$rc$__mdw_prompt_right"
96 PS2="$PS1$__mdw_prompt_more"
105 t) printf "
\ek%s
\e\\" "$__mdw_shell" ;;
111 t) printf "
\ek%s
\e\\" "$1" ;;
115 ###--------------------------------------------------------------------------
116 ### Some handy aliases.
119 alias which="command -v"
122 case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac
126 alias re="rootly $EDITOR"
127 alias pstree="pstree -hl"
128 alias cdtmp='cd ${TMPDIR-/tmp}'
129 alias pushtmp='pushd ${TMPDIR-/tmp}'
131 alias svn="svnwrap svn"
134 ###--------------------------------------------------------------------------
137 ## Arrange for `ls' output to be in colour.
138 if __mdw_programp dircolors; then eval $(dircolors -b "$HOME/.dircolors")
139 else unset LS_COLORS; fi
141 unalias ls 2>/dev/null || :
143 if [ -t 1 ]; then command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
144 else command ls "$@"; fi
147 ## Arrange for `grep' output to be in colour.
148 export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
153 command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
158 alias grep="greplike grep"
159 alias egrep="greplike egrep"
160 alias fgrep="greplike fgrep"
161 alias zgrep="greplike zgrep"
163 ###--------------------------------------------------------------------------
166 ## Turn off pagers inside Emacs shell buffers.
167 case "$INSIDE_EMACS" in
168 2[2-9].*,comint | [3-9][0-9].*,comint) export PAGER=cat ;;
171 ###--------------------------------------------------------------------------
172 ### More complicated shell functions.
174 ## xt [@HOST] XTERM-ARGS
176 ## Open a terminal, maybe on a remote host.
180 local remote=${1#@} title
182 if [ $# -gt 0 ]; then
183 title="xterm [$remote] $1"
185 title="xterm [$remote]"
187 (xterm -title "$title" -e ssh $remote "$@" &)
197 ## Tweak core dumps on and off, or show the current status.
200 xon|xy|xyes) ulimit -Sc $(ulimit -Hc) ;;
201 xoff|xn|xno) ulimit -Sc 0 ;;
203 local l=$(ulimit -Sc)
205 0) echo "Core dumps disabled" ;;
206 unlimited) echo "Core dumps enabled" ;;
207 *) echo "Core dump limit is $l blocks" ;;
211 echo >&2 "usage: core [y|n]"
219 ## Set current security world to NAME. With no NAME, print the currently
222 local nfast=${NFAST_HOME-/opt/nfast}
226 echo "${NFAST_KMDATA#$nfast/kmdata-}"
231 elif [ -d "$nfast/kmdata-$1" ]; then
232 kmdata=$nfast/kmdata-$1
234 echo >&2 "world: can't find world $1"
239 0) export NFAST_KMDATA=$kmdata ;;
246 ## path-add [VAR] DIR
248 ## Add DIR to the beginning of PATH-like variable VAR (defaults to PATH) if
249 ## it's not there already.
251 local pathvar export dir val
253 1) pathvar=PATH dir=$1 export="export PATH" ;;
254 2) pathvar=$1 dir=$2 export=: ;;
255 *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;;
266 ## path-remove [VAR] DIR
268 ## Remove DIR from PATH-like variable VAR (defaults to PATH); it's not an
269 ## error if DIR isn't in VAR.
271 local pathvar export dir val
273 1) pathvar=PATH dir=$1 export="export PATH" ;;
274 2) pathvar=$1 dir=$2 export=: ;;
275 *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;;
280 :"$dir":*) val=${val#$dir:} ;;
281 *:"$dir":) val=${val%:$dir} ;;
282 *:"$dir":*) val=${val%%:$dir:*}:${val#*:$dir:} ;;
288 ## pathhack [-f] +HACK|-HACK...
290 ## Each HACK refers to a subdirectory of `~/bin/hacks'. A hack name preceded
291 ## by `+' adds the directory to the PATH; a `-' removes. Adding a hack
292 ## that's already on the PATH doesn't do anything unless `-f' is set, in
293 ## which case it gets moved to the beginning. With no arguments, print the
294 ## currently installed hacks.
296 local p e force arg hack dir
298 if [ $# -eq 0 ]; then
301 case "$e" in "$HOME/bin/hacks/"*) echo ${e#$HOME/bin/hacks/} ;; esac
302 case "$p" in *:*) p=${p#*:} ;; *) break ;; esac
307 while [ $# -gt 0 ]; do
310 -f | --force) force=t; shift; continue ;;
316 dir=$HOME/bin/hacks/$hack
317 if ! [ -d "$dir" ]; then
318 echo "$0: path hack $hack not found"
321 case "$arg,$force,:$PATH:" in
322 -*,*,*:"$dir":*) path_remove p "$dir" ;;
323 +*,t,*:"$dir":*) path_remove p "$dir"; path_add p "$dir" ;;
324 +*,nil,*:"$dir":*) ;;
325 +*,*) path_add p "$dir" ;;
329 if [ $# -eq 0 ]; then PATH=$p; export PATH
330 else PATH=$p "$@"; fi
333 ###--------------------------------------------------------------------------
334 ### Finishing touches.
336 ## Make sure `$HOME/bin' is on the path.
339 ## Set the temporary directory again. (A setuid or setgid program may have
340 ## unhelpfully forgotten this for us.)
343 *) if __mdw_programp tmpdir; then eval $(tmpdir -b); fi ;;
346 ## For `root' use -- some simple molly-guards.
349 alias rm="rm -i" cp="cp -i" mv="mv -i"
354 ## Run any local hooks.
355 __mdw_source_if_exists "$HOME/.shell-local"
357 ###----- That's all, folks --------------------------------------------------