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