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