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