chiark / gitweb /
dot/profile: Unbungle the setting of `$DEB_BUILD_OPTIONS'.
[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
4877b8d8
MW
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
74a53e28
MW
61###--------------------------------------------------------------------------
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
83aea396
MW
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"; }
05f8a57d 151
83aea396
MW
152__mdw_screen_precmd () { printf >/dev/tty "\ek%s\e\\" "$__mdw_shell"; }
153__mdw_screen_preexec () { printf >/dev/tty "\ek%s\e\\" "$1"; }
74a53e28 154
4877b8d8 155if [ -t 0 ]; then
05f8a57d
MW
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,*)
4877b8d8
MW
162 __mdw_addhook __mdw_precmd_hook __mdw_screen_precmd
163 __mdw_addhook __mdw_preexec_hook __mdw_screen_preexec
164 ;;
74a53e28 165 esac
4877b8d8
MW
166 case ${__mdw_precmd_hook+t} in
167 t) __mdw_addhook __mdw_precmd_hook __mdw_set_prompt ;;
168 esac
169fi
74a53e28
MW
170
171###--------------------------------------------------------------------------
172### Some handy aliases.
173
174alias cx='chmod +x'
175alias which="command -v"
176alias rc="rc -l"
177rootly () {
178 case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac
179 $__MDW_ROOTLY "$@"
180}
181alias r=rootly
182alias re="rootly $EDITOR"
183alias pstree="pstree -hl"
184alias cdtmp='cd ${TMPDIR-/tmp}'
185alias pushtmp='pushd ${TMPDIR-/tmp}'
186alias e="$EDITOR"
187alias svn="svnwrap svn"
188alias @="ssh"
daeece4b 189alias make="nice make"
ee747cc6 190alias cross-run="nice cross-run"
8712436f 191alias gdb="gdb -q"
74a53e28
MW
192
193###--------------------------------------------------------------------------
194### Colour output.
195
196## Arrange for `ls' output to be in colour.
197if __mdw_programp dircolors; then eval $(dircolors -b "$HOME/.dircolors")
198else unset LS_COLORS; fi
199
200unalias ls 2>/dev/null || :
201ls () {
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.
207export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
208
209greplike () {
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}
217alias grep="greplike grep"
218alias egrep="greplike egrep"
219alias fgrep="greplike fgrep"
220alias zgrep="greplike zgrep"
221
049cd015
MW
222## Arrange for `diff' output to be in colour.
223export DIFF_COLORS="hd=1:ln=36:ad=32:de=31"
224difflike () {
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}
235alias diff="difflike diff"
236
74a53e28
MW
237###--------------------------------------------------------------------------
238### Other hacks.
239
240## Turn off pagers inside Emacs shell buffers.
241case "$INSIDE_EMACS" in
242 2[2-9].*,comint | [3-9][0-9].*,comint) export PAGER=cat ;;
243esac
244
245###--------------------------------------------------------------------------
246### More complicated shell functions.
247
248## xt [@HOST] XTERM-ARGS
249##
250## Open a terminal, maybe on a remote host.
251xt () {
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.
272core () {
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.
295world () {
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.
324path_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.
344path_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.
369pathhack () {
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
4cb8b8da
MW
410## Make sure `$HOME/bin' is on the path.
411path_add "$HOME/bin"
412
6054bdb6
MW
413## Set the temporary directory again. (A setuid or setgid program may have
414## unhelpfully forgotten this for us.)
0a030a39
MW
415case ${TMPDIR+t} in
416 t) ;;
417 *) if __mdw_programp tmpdir; then eval $(tmpdir -b); fi ;;
418esac
6054bdb6 419
74a53e28
MW
420## For `root' use -- some simple molly-guards.
421case $(id -u) in
422 0)
423 alias rm="rm -i" cp="cp -i" mv="mv -i"
424 set -o noclobber
425 ;;
426esac
427
428## Run any local hooks.
429__mdw_source_if_exists "$HOME/.shell-local"
430
431###----- That's all, folks --------------------------------------------------