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