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