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