chiark / gitweb /
dot/vm: Make `vm-reply-ignored-addresses' patterns match correctly.
[profile] / dot / shell-rc
1 ### -*-sh-*-
2 ###
3 ### Common per-shell configuration.
4
5 ###--------------------------------------------------------------------------
6 ### Utilities.
7
8 __mdw_programp () { type >/dev/null 2>&1 "$1"; }
9
10 __mdw_source_if_exists () {
11   local i
12   for i in "$@"; do
13     if [ -r "$i" ]; then . "$i"; fi
14   done
15 }
16
17 ###--------------------------------------------------------------------------
18 ### Hooks.
19
20 __mdw_addhook () {
21   local hk=$1 fn=$2 t
22
23   eval t=\${$hk+t}
24   case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac
25
26   eval t=\$$hk
27   case " $t " in
28     *" $fn "*) ;;
29     *) eval "$hk=\${$hk:+\$$hk }\$fn" ;;
30   esac
31 }
32
33 __mdw_delhook () {
34   local hk=$1 fn=$2 t l r
35
36   eval t=\${$hk+t}
37   case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac
38
39   eval t=\" \$$hk \"
40   case $t in
41     *" $fn "*)
42       l=${t%% $fn*} r=${t##*$fn }
43       l=${l# } r=${r% }
44       eval "$hk=\$l\${l:+ }\$r"
45       ;;
46   esac
47 }
48
49 __mdw_setrc () { return $1; }
50
51 __mdw_runhook () {
52   local hk=$1 saverc=$? t i; shift
53
54   eval t=\${$hk+t}
55   case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac
56
57   eval t=\$$hk
58   for i in $t; do __mdw_setrc $saverc; "$i" "$@"; done
59 }
60
61 ###--------------------------------------------------------------------------
62 ### Prompt machinery.
63
64 __mdw_host=$(hostname)
65 __mdw_hqual=
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=""; }
69
70 __mdw_system=$(uname -s)
71 : ${USER-${LOGNAME-$(id -un)}}
72 __mdw_user=$USER
73
74 case $(id -u) in
75   0)
76     __mdw_rootp=t
77     ;;
78   *)
79     case $__mdw_system in
80       CYGWIN_*)
81         case " $(id -G) " in
82           *" 544 "*) __mdw_rootp=t __mdw_user="$__mdw_user%admin" ;;
83           *) __mdw_rootp=nil ;;
84         esac
85         ;;
86       *)
87         __mdw_rootp=nil
88         ;;
89     esac
90 esac
91
92 __mdw_set_prompt_pieces () {
93
94   ## Fancy highlighting in some terminals.
95   local bold unbold nl gitcolour rccolour uncolour
96   local host dir more
97   bold="" unbold="" nl="" gitcolour="" rccolour="" uncolour="" more=""
98   __mdw_set_prompt_hacks
99
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
104   case $__mdw_rootp in
105     t)
106       left=$(echo « | iconv -f UTF-8 -t //translit)
107       right=$(echo » | iconv -f UTF-8 -t //translit)
108       ;;
109     nil)
110       case $USER in
111         mdw | mwooding | nemo) u="" left="[" right="]" ;;
112         *) u="$__mdw_user@" left="{" right="}" ;;
113       esac
114       tty=$(tty)
115       case "$__mdw_tty" in
116         "$tty") left="<" right=">" ;;
117         *) __mdw_tty=$tty; export __mdw_tty ;;
118       esac
119       ;;
120   esac
121
122   ## If this session is insecure then highlight that.
123   local sec_l sec_r h
124   h=$(hostname)
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=")" ;;
128     *) sec_l="" sec_r=""
129   esac
130
131   ## If this is an schroot environment or some other interesting augmented
132   ## environment then point this out.
133
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 "
142 }
143
144 __mdw_set_prompt () {
145   case "${TERM-dumb}:${INSIDE_EMACS+$INSIDE_EMACS}" in
146     dumb:)
147       case $(id -u) in 0) PS1='# ' ;; *) PS1='$ ' ;; esac
148       PS2='> '
149       ;;
150     *)
151       __mdw_last_rc=$?
152       local git rc
153       if type __git_ps1 >/dev/null 2>&1; then
154         git="$__mdw_prompt_git_left$(__git_ps1)$__mdw_prompt_git_right"
155       else
156         git=""
157       fi
158       case $__mdw_last_rc in
159         0) rc="" ;;
160         *) rc="$__mdw_prompt_rc_left rc=$__mdw_last_rc$__mdw_prompt_rc_right" ;;
161       esac
162       PS1="$__mdw_prompt_left$git$rc$__mdw_prompt_right"
163       PS2="$PS1$__mdw_prompt_more"
164       unset __mdw_last_rc
165       ;;
166   esac
167 }
168
169 __mdw_xterm_settitle () {
170   printf >/dev/tty \
171     "\e]2;%s@%s:%s – %s\e\\" \
172     "$__mdw_user" "$__mdw_host$__mdw_hqual" "$PWD" \
173     "$1"
174 }
175 __mdw_xterm_precmd () { __mdw_xterm_settitle "$__mdw_shell"; }
176 __mdw_xterm_preexec () { __mdw_xterm_settitle "$1"; }
177
178 __mdw_screen_settitle () {
179   printf >/dev/tty \
180     "\ek%s\e\\" \
181     "$1"
182 }
183 __mdw_screen_precmd () { __mdw_screen_settitle "$__mdw_shell"; }
184 __mdw_screen_preexec () { __mdw_screen_settitle "$1"; }
185
186 if [ -t 0 ]; then
187   case ${STY+t},${__mdw_precmd_hook+t},${__mdw_preexec_hook+t},${TERM} in
188     ,t,t,xterm*)
189       __mdw_addhook __mdw_precmd_hook __mdw_xterm_precmd
190       __mdw_addhook __mdw_preexec_hook __mdw_xterm_preexec
191       ;;
192     t,t,t,*)
193       __mdw_addhook __mdw_precmd_hook __mdw_screen_precmd
194       __mdw_addhook __mdw_preexec_hook __mdw_screen_preexec
195       ;;
196   esac
197   case ${__mdw_precmd_hook+t} in
198     t) __mdw_addhook __mdw_precmd_hook __mdw_set_prompt ;;
199   esac
200 fi
201
202 ###--------------------------------------------------------------------------
203 ### Some handy aliases.
204
205 alias cx='chmod +x'
206 alias which="command -v"
207 alias rc="rc -l"
208 rootly () {
209   case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac
210   $__MDW_ROOTLY "$@"
211 }
212 alias r=rootly
213 alias re="rootly $EDITOR"
214 alias pstree="pstree -hl"
215 alias cdtmp='cd ${TMPDIR-/tmp}'
216 alias pushtmp='pushd ${TMPDIR-/tmp}'
217 alias e="$EDITOR"
218 alias svn="svnwrap svn"
219 alias @="ssh"
220 alias make="nice make"
221 alias cross-run="nice cross-run"
222 alias gdb="gdb -q"
223
224 ###--------------------------------------------------------------------------
225 ### Colour output.
226
227 ## Arrange for `ls' output to be in colour.
228 if __mdw_programp dircolors; then eval $(dircolors -b "$HOME/.dircolors")
229 else unset LS_COLORS; fi
230
231 unalias ls 2>/dev/null || :
232 ls () {
233   if [ -t 1 ]; then command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
234   else command ls "$@"; fi
235 }
236
237 ## Arrange for `grep' output to be in colour.
238 export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
239
240 greplike () {
241   local grep=$1; shift
242   if [ -t 1 ]; then
243     command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
244   else
245     command $grep "$@"
246   fi
247 }
248 alias grep="greplike grep"
249 alias egrep="greplike egrep"
250 alias fgrep="greplike fgrep"
251 alias zgrep="greplike zgrep"
252
253 ## Arrange for `diff' output to be in colour.
254 export DIFF_COLORS="hd=1:ln=36:ad=32:de=31"
255 difflike () {
256   local diff=$1; shift
257   if [ -t 1 ]; then
258     command $diff \
259             ${DIFF_COLORS+--color=always} \
260             ${DIFF_COLORS+--palette="$DIFF_COLORS"} \
261             "$@" | mdw-pager
262   else
263     command $diff "$@" | cat
264   fi
265 }
266 alias diff="difflike diff"
267
268 ###--------------------------------------------------------------------------
269 ### Other hacks.
270
271 ## Turn off pagers inside Emacs shell buffers.
272 case "$INSIDE_EMACS" in
273   2[2-9].*,comint | [3-9][0-9].*,comint) export PAGER=cat ;;
274 esac
275
276 ###--------------------------------------------------------------------------
277 ### More complicated shell functions.
278
279 ## xt [@HOST] XTERM-ARGS
280 ##
281 ## Open a terminal, maybe on a remote host.
282 xt () {
283   case "$1" in
284     @*)
285       local remote=${1#@} title
286       shift
287       if [ $# -gt 0 ]; then
288         title="xterm [$remote] $1"
289       else
290         title="xterm [$remote]"
291       fi
292       (xterm -title "$title" -e ssh $remote "$@" &)
293       ;;
294     *)
295       (xterm "$@" &)
296       ;;
297   esac
298 }
299
300 ## core [y|n]
301 ##
302 ## Tweak core dumps on and off, or show the current status.
303 core () {
304   case "x$1" in
305     xon|xy|xyes) ulimit -Sc $(ulimit -Hc) ;;
306     xoff|xn|xno) ulimit -Sc 0 ;;
307     x)
308       local l=$(ulimit -Sc)
309       case $l in
310         0) echo "Core dumps disabled" ;;
311         unlimited) echo "Core dumps enabled" ;;
312         *) echo "Core dump limit is $l blocks" ;;
313       esac
314       ;;
315     *)
316       echo >&2 "usage: core [y|n]"
317       return 1
318       ;;
319   esac
320 }
321
322 ## world [NAME]
323 ##
324 ## Set current security world to NAME.  With no NAME, print the currently
325 ## selected world.
326 world () {
327   local nfast=${NFAST_HOME-/opt/nfast}
328   local kmdata
329   case "$#" in
330     0)
331       echo "${NFAST_KMDATA#$nfast/kmdata-}"
332       ;;
333     *)
334       if [ -d "$1" ]; then
335         kmdata=$1
336       elif [ -d "$nfast/kmdata-$1" ]; then
337         kmdata=$nfast/kmdata-$1
338       else
339         echo >&2 "world: can't find world $1"
340         return 1
341       fi
342       shift
343       case "$#" in
344         0) export NFAST_KMDATA=$kmdata ;;
345         *) "$@" ;;
346       esac
347       ;;
348   esac
349 }
350
351 ## path-add [VAR] DIR
352 ##
353 ## Add DIR to the beginning of PATH-like variable VAR (defaults to PATH) if
354 ## it's not there already.
355 path_add () {
356   local pathvar export dir val
357   case $# in
358     1) pathvar=PATH dir=$1 export="export PATH" ;;
359     2) pathvar=$1 dir=$2 export=: ;;
360     *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;;
361   esac
362   eval val=\$$pathvar
363   case ":$val:" in
364     *:"$dir":*) ;;
365     *) val=$dir:$val ;;
366   esac
367   eval $pathvar=\$val
368   eval $export
369 }
370
371 ## path-remove [VAR] DIR
372 ##
373 ## Remove DIR from PATH-like variable VAR (defaults to PATH); it's not an
374 ## error if DIR isn't in VAR.
375 path_remove () {
376   local pathvar export dir val
377   case $# in
378     1) pathvar=PATH dir=$1 export="export PATH" ;;
379     2) pathvar=$1 dir=$2 export=: ;;
380     *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;;
381   esac
382   eval val=\$$pathvar
383   case ":$val:" in
384     :"$dir":) val= ;;
385     :"$dir":*) val=${val#$dir:} ;;
386     *:"$dir":) val=${val%:$dir} ;;
387     *:"$dir":*) val=${val%%:$dir:*}:${val#*:$dir:} ;;
388   esac
389   eval $pathvar=\$val
390   eval $export
391 }
392
393 ## pathhack [-f] +HACK|-HACK...
394 ##
395 ## Each HACK refers to a subdirectory of `~/bin/hacks'.  A hack name preceded
396 ## by `+' adds the directory to the PATH; a `-' removes.  Adding a hack
397 ## that's already on the PATH doesn't do anything unless `-f' is set, in
398 ## which case it gets moved to the beginning.  With no arguments, print the
399 ## currently installed hacks.
400 pathhack () {
401   local p e force arg hack dir
402   p=$PATH
403   if [ $# -eq 0 ]; then
404     while :; do
405       e=${p%%:*}
406       case "$e" in "$HOME/bin/hacks/"*) echo ${e#$HOME/bin/hacks/} ;; esac
407       case "$p" in *:*) p=${p#*:} ;; *) break ;; esac
408     done
409     return
410   fi
411   force=nil
412   while [ $# -gt 0 ]; do
413     arg=$1
414     case "$arg" in
415       -f | --force) force=t; shift; continue ;;
416       --) shift; break ;;
417       [-+]*) ;;
418       *) break; ;;
419     esac
420     hack=${arg#[+-]}
421     dir=$HOME/bin/hacks/$hack
422     if ! [ -d "$dir" ]; then
423       echo "$0: path hack $hack not found"
424       return 1
425     fi
426     case "$arg,$force,:$PATH:" in
427       -*,*,*:"$dir":*) path_remove p "$dir" ;;
428       +*,t,*:"$dir":*) path_remove p "$dir"; path_add p "$dir" ;;
429       +*,nil,*:"$dir":*) ;;
430       +*,*) path_add p "$dir" ;;
431     esac
432     shift
433   done
434   if [ $# -eq 0 ]; then PATH=$p; export PATH
435   else PATH=$p "$@"; fi
436 }
437
438 ###--------------------------------------------------------------------------
439 ### Finishing touches.
440
441 ## Make sure `$HOME/bin' is on the path.
442 path_add "$HOME/bin"
443
444 ## Set the temporary directory again.  (A setuid or setgid program may have
445 ## unhelpfully forgotten this for us.)
446 case ${TMPDIR+t} in
447   t) ;;
448   *) if __mdw_programp tmpdir; then eval $(tmpdir -b); fi ;;
449 esac
450
451 ## For `root' use -- some simple molly-guards.
452 case $(id -u) in
453   0)
454     alias rm="rm -i" cp="cp -i" mv="mv -i"
455     set -o noclobber
456     ;;
457 esac
458
459 ## Run any local hooks.
460 __mdw_source_if_exists "$HOME/.shell-local"
461
462 ###----- That's all, folks --------------------------------------------------