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