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