dot/bash*: Reformat.
[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 alias rootly=$__MDW_ROOTLY
102 alias r=rootly
103 alias re="rootly $EDITOR"
104 alias pstree="pstree -hl"
105 alias cdtmp='cd ${TMPDIR-/tmp}'
106 alias pushtmp='pushd ${TMPDIR-/tmp}'
107 alias e="$EDITOR"
108 alias svn="svnwrap svn"
109 alias @="ssh"
110
111 ## Completion.
112 [ -r /etc/bash_completion ] && . /etc/bash_completion
113 [ -r $HOME/.bash_completion ] && . $HOME/.bash_completion
114
115 ###--------------------------------------------------------------------------
116 ### Colour output.
117
118 ## Arrange for `ls' output to be in colour.
119 if [ -x /usr/bin/dircolors -o -x /usr/local/bin/dircolors ]; then
120 eval `dircolors -b ~/.dircolors`
121 else
122 unset LS_COLORS
123 fi
124
125 ls () {
126 if [ -t 1 ]; then
127 command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
128 else
129 command ls "$@"
130 fi
131 }
132
133 ## Arrange for `grep' output to be in colour.
134 export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
135
136 greplike () {
137 declare grep=$1; shift
138 if [ -t 1 ]; then
139 command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
140 else
141 command $grep "$@"
142 fi
143 }
144 alias grep="greplike grep"
145 alias egrep="greplike egrep"
146 alias fgrep="greplike fgrep"
147 alias zgrep="greplike zgrep"
148
149 ## Turn off pagers inside Emacs shell buffers.
150 case "$INSIDE_EMACS" in
151 22.*,comint) export PAGER=cat ;;
152 esac
153
154 ###--------------------------------------------------------------------------
155 ### More complicated shell functions.
156
157 ## xt [@HOST] XTERM-ARGS
158 ##
159 ## Open a terminal, maybe on a remote host.
160 xt () {
161 case "$1" in
162 @*)
163 local remote=${1#@} title
164 shift
165 if [ $# -gt 0 ]; then
166 title="xterm [$remote] $1"
167 else
168 title="xterm [$remote]"
169 fi
170 (xterm -title "$title" -e ssh $remote "$@" &)
171 ;;
172 *)
173 (xterm "$@" &)
174 ;;
175 esac
176 }
177
178 ## core [y|n]
179 ##
180 ## Tweak core dumps on and off, or show the current status.
181 core () {
182 case "x$1" in
183 xon|xy|xyes) ulimit -Sc `ulimit -Hc` ;;
184 xoff|xn|xno) ulimit -Sc 0 ;;
185 x)
186 local l=`ulimit -Sc`
187 case $l in
188 0) echo "Core dumps disabled" ;;
189 unlimited) echo "Core dumps enabled" ;;
190 *) echo "Core dump limit is $l blocks" ;;
191 esac
192 ;;
193 *)
194 echo >&2 "usage: core [y|n]"
195 return 1
196 ;;
197 esac
198 }
199
200 ## world [NAME]
201 ##
202 ## Set current security world to NAME. With no NAME, print the currently
203 ## selected world.
204 world () {
205 local nfast=${NFAST_HOME-/opt/nfast}
206 local kmdata
207 case "$#" in
208 0)
209 echo "${NFAST_KMDATA#$nfast/kmdata-}"
210 ;;
211 *)
212 if [ -d "$1" ]; then
213 kmdata=$1
214 elif [ -d "$nfast/kmdata-$1" ]; then
215 kmdata=$nfast/kmdata-$1
216 else
217 echo >&2 "world: can't find world $1"
218 return 1
219 fi
220 shift
221 case "$#" in
222 0) export NFAST_KMDATA=$kmdata ;;
223 *) "$@" ;;
224 esac
225 ;;
226 esac
227 }
228
229 ## Fix `man' under Slowaris.
230 case "$MACHTYPE" in
231 *solaris*)
232 man () {
233 declare -i i=0
234 declare arg
235 declare -a man
236 for arg; do
237 case "$arg" in [0-9]*) man[i+=1]="-s" ;; esac
238 man[i+=1]="$arg"
239 done
240 command man "${man[@]}"
241 }
242 ;;
243 esac
244
245 ###--------------------------------------------------------------------------
246 ### Path hacks.
247
248 ## path-add [VAR] DIR
249 ##
250 ## Add DIR to the beginning of PATH-like variable VAR (defaults to PATH) if
251 ## it's not there already.
252 path-add () {
253 local pathvar export dir val
254 case $# in
255 1) pathvar=PATH dir=$1 export="export PATH";;
256 2) pathvar=$1 dir=$2 export=:;;
257 *) echo >&2 "Usage: $0 [VAR] DIR";;
258 esac
259 eval "val=\$$pathvar"
260 case ":$val:" in
261 *:"$dir":*) ;;
262 *) val=$dir:$val ;;
263 esac
264 eval "$pathvar=\$val"
265 $export
266 }
267
268 ## path-remove [VAR] DIR
269 ##
270 ## Remove DIR from PATH-like variable VAR (defaults to PATH); it's not an
271 ## error if DIR isn't in VAR.
272 path-remove () {
273 local pathvar export dir val
274 case $# in
275 1) pathvar=PATH dir=$1 export="export PATH";;
276 2) pathvar=$1 dir=$2 export=:;;
277 *) echo >&2 "Usage: $0 [VAR] DIR";;
278 esac
279 eval "val=\$$pathvar"
280 case ":$val:" in
281 :"$dir":) val= ;;
282 :"$dir":*) val=${val#$dir:} ;;
283 *:"$dir":) val=${val%:$dir} ;;
284 *:"$dir":*) val=${val/:$dir:/:} ;;
285 esac
286 eval "$pathvar=\$val"
287 $export
288 }
289
290 ## pathhack [-f] +HACK|-HACK...
291 ##
292 ## Each HACK refers to a subdirectory of `~/bin/hacks'. A hack name preceded
293 ## by `+' adds the directory to the PATH; a `-' removes. Adding a hack
294 ## that's already on the PATH doesn't do anything unless `-f' is set, in
295 ## which case it gets moved to the beginning. With no arguments, print the
296 ## currently installed hacks.
297 pathhack () {
298 if [ $# -eq 0 ]; then
299 local IFS=:
300 for e in $PATH; do
301 case "$e" in
302 "$HOME/bin/hacks/"*)
303 echo ${e#$HOME/bin/hacks/}
304 ;;
305 esac
306 done
307 return
308 fi
309 local force=nil
310 local path=$PATH
311 while [ $# -gt 0 ]; do
312 arg=$1
313 case "$arg" in
314 -f | --force)
315 force=t
316 shift
317 continue
318 ;;
319 --)
320 shift
321 break
322 ;;
323 [-+]*)
324 ;;
325 *)
326 break
327 ;;
328 esac
329 hack=${arg#[+-]}
330 dir=$HOME/bin/hacks/$hack
331 [ -d "$dir" ] || {
332 echo "$0: path hack $hack not found"
333 return 1
334 }
335 case "$arg,$force,:$PATH:" in
336 -*,*,*:"$dir":*)
337 path-remove path "$dir"
338 ;;
339 +*,t,*:"$dir":*)
340 path-remove path "$dir"
341 path-add path "$dir"
342 ;;
343 +*,nil,*:"$dir":*)
344 ;;
345 +*,*)
346 path-add path "$dir"
347 ;;
348 esac
349 shift
350 done
351 if [ $# -eq 0 ]; then
352 PATH=$path
353 export PATH
354 else
355 PATH=$path "$@"
356 fi
357 }
358
359 ###--------------------------------------------------------------------------
360 ### Finishing touches.
361
362 ## For `root' use -- some simple molly-guards.
363 if (( UID == 0 )); then
364 alias rm='rm -i' cp='cp -i' mv='mv -i'
365 set -o noclobber
366 fi
367
368 ## Run any local hooks.
369 [ -f "$HOME/.bashrc-local" ] && . "$HOME/.bashrc-local"
370
371 ## Close the `__mdw_bashrc' guard.
372 fi
373
374 ###----- That's all, folks --------------------------------------------------