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