dot/shell-rc: Use `$USER' directly now that I've set it properly.
[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 : ${USER-${LOGNAME-$(id -un)}}
71
72 __mdw_set_prompt_pieces () {
73
74 ## Fancy highlighting in some terminals.
75 local bold unbold nl gitcolour rccolour uncolour
76 local host dir more
77 bold="" unbold="" nl="" gitcolour="" rccolour="" uncolour="" more=""
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.
83 local left right u tty
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 *)
90 case $USER in
91 mdw | mwooding | nemo) u="" left="[" right="]" ;;
92 *) u="$USER@" left="{" right="}" ;;
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
111 ## If this is an schroot environment or some other interesting augmented
112 ## environment then point this out.
113
114 ## Put together the main pieces.
115 __mdw_prompt_left="$nl$bold$left$sec_l$u$host$__mdw_hqual$sec_r$dir"
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"
121 __mdw_prompt_more=" $more$bold>$unbold "
122 }
123
124 __mdw_set_prompt () {
125 case "${TERM-dumb}:${INSIDE_EMACS+$INSIDE_EMACS}" in
126 dumb:)
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"
143 PS2="$PS1$__mdw_prompt_more"
144 unset __mdw_last_rc
145 ;;
146 esac
147 }
148
149 __mdw_xterm_settitle () {
150 printf >/dev/tty \
151 "\e]2;%s@%s:%s – %s\e\\" \
152 "$USER" "$__mdw_host$__mdw_hqual" "$PWD" \
153 "$1"
154 }
155 __mdw_xterm_precmd () { __mdw_xterm_settitle "$__mdw_shell"; }
156 __mdw_xterm_preexec () { __mdw_xterm_settitle "$1"; }
157
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"; }
165
166 if [ -t 0 ]; then
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,*)
173 __mdw_addhook __mdw_precmd_hook __mdw_screen_precmd
174 __mdw_addhook __mdw_preexec_hook __mdw_screen_preexec
175 ;;
176 esac
177 case ${__mdw_precmd_hook+t} in
178 t) __mdw_addhook __mdw_precmd_hook __mdw_set_prompt ;;
179 esac
180 fi
181
182 ###--------------------------------------------------------------------------
183 ### Some handy aliases.
184
185 alias cx='chmod +x'
186 alias which="command -v"
187 alias rc="rc -l"
188 rootly () {
189 case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac
190 $__MDW_ROOTLY "$@"
191 }
192 alias r=rootly
193 alias re="rootly $EDITOR"
194 alias pstree="pstree -hl"
195 alias cdtmp='cd ${TMPDIR-/tmp}'
196 alias pushtmp='pushd ${TMPDIR-/tmp}'
197 alias e="$EDITOR"
198 alias svn="svnwrap svn"
199 alias @="ssh"
200 alias make="nice make"
201 alias cross-run="nice cross-run"
202 alias gdb="gdb -q"
203
204 ###--------------------------------------------------------------------------
205 ### Colour output.
206
207 ## Arrange for `ls' output to be in colour.
208 if __mdw_programp dircolors; then eval $(dircolors -b "$HOME/.dircolors")
209 else unset LS_COLORS; fi
210
211 unalias ls 2>/dev/null || :
212 ls () {
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.
218 export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
219
220 greplike () {
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 }
228 alias grep="greplike grep"
229 alias egrep="greplike egrep"
230 alias fgrep="greplike fgrep"
231 alias zgrep="greplike zgrep"
232
233 ## Arrange for `diff' output to be in colour.
234 export DIFF_COLORS="hd=1:ln=36:ad=32:de=31"
235 difflike () {
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 }
246 alias diff="difflike diff"
247
248 ###--------------------------------------------------------------------------
249 ### Other hacks.
250
251 ## Turn off pagers inside Emacs shell buffers.
252 case "$INSIDE_EMACS" in
253 2[2-9].*,comint | [3-9][0-9].*,comint) export PAGER=cat ;;
254 esac
255
256 ###--------------------------------------------------------------------------
257 ### More complicated shell functions.
258
259 ## xt [@HOST] XTERM-ARGS
260 ##
261 ## Open a terminal, maybe on a remote host.
262 xt () {
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.
283 core () {
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.
306 world () {
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.
335 path_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.
355 path_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.
380 pathhack () {
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
421 ## Make sure `$HOME/bin' is on the path.
422 path_add "$HOME/bin"
423
424 ## Set the temporary directory again. (A setuid or setgid program may have
425 ## unhelpfully forgotten this for us.)
426 case ${TMPDIR+t} in
427 t) ;;
428 *) if __mdw_programp tmpdir; then eval $(tmpdir -b); fi ;;
429 esac
430
431 ## For `root' use -- some simple molly-guards.
432 case $(id -u) in
433 0)
434 alias rm="rm -i" cp="cp -i" mv="mv -i"
435 set -o noclobber
436 ;;
437 esac
438
439 ## Run any local hooks.
440 __mdw_source_if_exists "$HOME/.shell-local"
441
442 ###----- That's all, folks --------------------------------------------------