Merge remote-tracking branch 'crybaby'
[profile] / dot / bashrc
1 ### -*-sh-*-
2 ###
3 ### Bash session things
4
5 ## Only do this if we haven't done it before. (Note that this guard isn't
6 ## exported, so subshells will need to make their own arrangements.)
7 if [ -z "$__mdw_bashrc" ]; then
8 __mdw_bashrc=done
9
10 ## If we've not run the main profile yet, we should do that first. It sets
11 ## up things we rely on. Also, if there's a system script, we should run
12 ## that too.
13 [ -z "$__mdw_profile" -a -r $HOME/.bash_profile ] && . $HOME/.bash_profile
14 [ -r /etc/bashrc ] && . /etc/bashrc
15
16 ## Set the temporary directory again. (If we've switched users, we'll want a
17 ## different temporary directory.)
18 [ "${TMPDIR+yes}" ] || eval `tmpdir -b`
19
20 ###--------------------------------------------------------------------------
21 ### Prompt hacking.
22
23 ## Only bother if the shell is interactive.
24 if [ -t 0 ]; then
25
26 ## Fancy highlighting in some terminals.
27 case "$TERM" in
28 linux*|screen*|xterm*|vt100*|eterm*)
29 bold="\[$(tput bold)\]" unbold="\[$(tput sgr0)\]" nl="\[ \]" ;;
30 *)
31 bold='' unbold='' nl='' ;;
32 esac
33
34 ## Choose the right delimiters. Highlight root prompts specially;
35 ## highlight when I'm running as some other user. Highlight when this
36 ## isn't the outermost shell on the terminal.
37 if (( EUID == 0 )); then
38 left=`echo « | iconv -f utf8 -t //translit`
39 right=`echo » | iconv -f utf8 -t //translit`
40 else
41 case $USER in
42 mdw|mwooding) u="" left="[" right="]" ;;
43 *) u="\\u@" left="{" right="}" ;;
44 esac
45 if [ "$__mdw_tty" = "`tty`" ]; then
46 left="<" right=">"
47 else
48 export __mdw_tty="`tty`"
49 fi
50 fi
51
52 ## If this session is insecure then highlight that.
53 if [ -z "$SSH_CLIENT" ] &&
54 [ "$__mdw_sechost" != "`hostname`" ]
55 then
56 sec_l='(' sec_r=')'
57 fi
58
59 ## Build the prompt string.
60 PS1="$nl$bold$left$sec_l$u\\h$sec_r \\w$right$unbold"
61 PS2="$PS1 $bold>$unbold "
62
63 fi
64
65 ###--------------------------------------------------------------------------
66 ### Other shell tweaking.
67
68 ## Random shell tweaks.
69 notify=1
70 set -b
71 shopt -u cdable_vars
72 shopt -s cdspell
73 shopt -s checkhash
74 shopt -s checkwinsize
75 shopt -s cmdhist
76 shopt -u dotglob
77 shopt -s expand_aliases
78 shopt -s extglob
79 shopt -s globstar
80 shopt -s gnu_errfmt
81 shopt -s histappend
82 shopt -s histreedit
83 shopt -u histverify
84 shopt -s hostcomplete
85 shopt -s huponexit
86 shopt -s interactive_comments
87 shopt -s lithist
88 shopt -u mailwarn
89 shopt -u nocaseglob
90 shopt -u nullglob
91 shopt -s promptvars
92 shopt -u shift_verbose
93 shopt -s sourcepath
94 HISTCONTROL=ignorespace:erasedups
95
96 ## Some handy aliases.
97 alias cx='chmod a+x'
98 alias which="command -v"
99 alias rc="rc -l"
100 alias ssync="rsync -e ssh"
101 rootly () {
102 case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac
103 $__MDW_ROOTLY "$@"
104 }
105 alias r=rootly
106 alias re="rootly $EDITOR"
107 alias pstree="pstree -hl"
108 alias cdtmp='cd ${TMPDIR-/tmp}'
109 alias pushtmp='pushd ${TMPDIR-/tmp}'
110 alias e="$EDITOR"
111 alias svn="svnwrap svn"
112 alias @="ssh"
113
114 ## Completion.
115 [ -r /etc/bash_completion ] && . /etc/bash_completion
116 [ -r $HOME/.bash_completion ] && . $HOME/.bash_completion
117
118 ###--------------------------------------------------------------------------
119 ### Colour output.
120
121 ## Arrange for `ls' output to be in colour.
122 if [ -x /usr/bin/dircolors -o -x /usr/local/bin/dircolors ]; then
123 eval `dircolors -b ~/.dircolors`
124 else
125 unset LS_COLORS
126 fi
127
128 ls () {
129 if [ -t 1 ]; then
130 command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
131 else
132 command ls "$@"
133 fi
134 }
135
136 ## Arrange for `grep' output to be in colour.
137 export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
138
139 greplike () {
140 declare grep=$1; shift
141 if [ -t 1 ]; then
142 command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
143 else
144 command $grep "$@"
145 fi
146 }
147 alias grep="greplike grep"
148 alias egrep="greplike egrep"
149 alias fgrep="greplike fgrep"
150 alias zgrep="greplike zgrep"
151
152 ## Turn off pagers inside Emacs shell buffers.
153 case "$INSIDE_EMACS" in
154 22.*,comint) export PAGER=cat ;;
155 esac
156
157 ###--------------------------------------------------------------------------
158 ### More complicated shell functions.
159
160 ## xt [@HOST] XTERM-ARGS
161 ##
162 ## Open a terminal, maybe on a remote host.
163 xt () {
164 case "$1" in
165 @*)
166 local remote=${1#@} title
167 shift
168 if [ $# -gt 0 ]; then
169 title="xterm [$remote] $1"
170 else
171 title="xterm [$remote]"
172 fi
173 (xterm -title "$title" -e ssh $remote "$@" &)
174 ;;
175 *)
176 (xterm "$@" &)
177 ;;
178 esac
179 }
180
181 ## core [y|n]
182 ##
183 ## Tweak core dumps on and off, or show the current status.
184 core () {
185 case "x$1" in
186 xon|xy|xyes) ulimit -Sc `ulimit -Hc` ;;
187 xoff|xn|xno) ulimit -Sc 0 ;;
188 x)
189 local l=`ulimit -Sc`
190 case $l in
191 0) echo "Core dumps disabled" ;;
192 unlimited) echo "Core dumps enabled" ;;
193 *) echo "Core dump limit is $l blocks" ;;
194 esac
195 ;;
196 *)
197 echo >&2 "usage: core [y|n]"
198 return 1
199 ;;
200 esac
201 }
202
203 ## world [NAME]
204 ##
205 ## Set current security world to NAME. With no NAME, print the currently
206 ## selected world.
207 world () {
208 local nfast=${NFAST_HOME-/opt/nfast}
209 local kmdata
210 case "$#" in
211 0)
212 echo "${NFAST_KMDATA#$nfast/kmdata-}"
213 ;;
214 *)
215 if [ -d "$1" ]; then
216 kmdata=$1
217 elif [ -d "$nfast/kmdata-$1" ]; then
218 kmdata=$nfast/kmdata-$1
219 else
220 echo >&2 "world: can't find world $1"
221 return 1
222 fi
223 shift
224 case "$#" in
225 0) export NFAST_KMDATA=$kmdata ;;
226 *) "$@" ;;
227 esac
228 ;;
229 esac
230 }
231
232 ## Fix `man' under Slowaris.
233 case "$MACHTYPE" in
234 *solaris*)
235 man () {
236 declare -i i=0
237 declare arg
238 declare -a man
239 for arg; do
240 case "$arg" in [0-9]*) man[i+=1]="-s" ;; esac
241 man[i+=1]="$arg"
242 done
243 command man "${man[@]}"
244 }
245 ;;
246 esac
247
248 ###--------------------------------------------------------------------------
249 ### Path hacks.
250
251 ## path-add [VAR] DIR
252 ##
253 ## Add DIR to the beginning of PATH-like variable VAR (defaults to PATH) if
254 ## it's not there already.
255 path-add () {
256 local pathvar export dir val
257 case $# in
258 1) pathvar=PATH dir=$1 export="export PATH";;
259 2) pathvar=$1 dir=$2 export=:;;
260 *) echo >&2 "Usage: $0 [VAR] DIR";;
261 esac
262 eval "val=\$$pathvar"
263 case ":$val:" in
264 *:"$dir":*) ;;
265 *) val=$dir:$val ;;
266 esac
267 eval "$pathvar=\$val"
268 $export
269 }
270
271 ## path-remove [VAR] DIR
272 ##
273 ## Remove DIR from PATH-like variable VAR (defaults to PATH); it's not an
274 ## error if DIR isn't in VAR.
275 path-remove () {
276 local pathvar export dir val
277 case $# in
278 1) pathvar=PATH dir=$1 export="export PATH";;
279 2) pathvar=$1 dir=$2 export=:;;
280 *) echo >&2 "Usage: $0 [VAR] DIR";;
281 esac
282 eval "val=\$$pathvar"
283 case ":$val:" in
284 :"$dir":) val= ;;
285 :"$dir":*) val=${val#$dir:} ;;
286 *:"$dir":) val=${val%:$dir} ;;
287 *:"$dir":*) val=${val/:$dir:/:} ;;
288 esac
289 eval "$pathvar=\$val"
290 $export
291 }
292
293 ## pathhack [-f] +HACK|-HACK...
294 ##
295 ## Each HACK refers to a subdirectory of `~/bin/hacks'. A hack name preceded
296 ## by `+' adds the directory to the PATH; a `-' removes. Adding a hack
297 ## that's already on the PATH doesn't do anything unless `-f' is set, in
298 ## which case it gets moved to the beginning. With no arguments, print the
299 ## currently installed hacks.
300 pathhack () {
301 if [ $# -eq 0 ]; then
302 local IFS=:
303 for e in $PATH; do
304 case "$e" in
305 "$HOME/bin/hacks/"*)
306 echo ${e#$HOME/bin/hacks/}
307 ;;
308 esac
309 done
310 return
311 fi
312 local force=nil
313 local path=$PATH
314 while [ $# -gt 0 ]; do
315 arg=$1
316 case "$arg" in
317 -f | --force)
318 force=t
319 shift
320 continue
321 ;;
322 --)
323 shift
324 break
325 ;;
326 [-+]*)
327 ;;
328 *)
329 break
330 ;;
331 esac
332 hack=${arg#[+-]}
333 dir=$HOME/bin/hacks/$hack
334 [ -d "$dir" ] || {
335 echo "$0: path hack $hack not found"
336 return 1
337 }
338 case "$arg,$force,:$PATH:" in
339 -*,*,*:"$dir":*)
340 path-remove path "$dir"
341 ;;
342 +*,t,*:"$dir":*)
343 path-remove path "$dir"
344 path-add path "$dir"
345 ;;
346 +*,nil,*:"$dir":*)
347 ;;
348 +*,*)
349 path-add path "$dir"
350 ;;
351 esac
352 shift
353 done
354 if [ $# -eq 0 ]; then
355 PATH=$path
356 export PATH
357 else
358 PATH=$path "$@"
359 fi
360 }
361
362 ###--------------------------------------------------------------------------
363 ### Finishing touches.
364
365 ## For `root' use -- some simple molly-guards.
366 if (( UID == 0 )); then
367 alias rm='rm -i' cp='cp -i' mv='mv -i'
368 set -o noclobber
369 fi
370
371 ## Run any local hooks.
372 [ -f "$HOME/.bashrc-local" ] && . "$HOME/.bashrc-local"
373
374 ## Close the `__mdw_bashrc' guard.
375 fi
376
377 ###----- That's all, folks --------------------------------------------------