| 1 | # -*- mode: sh; coding: utf-8 -*- |
| 2 | # |
| 3 | # Bash session things |
| 4 | # |
| 5 | |
| 6 | if [ -z "$__mdw_bashrc" ]; then |
| 7 | |
| 8 | __mdw_bashrc=done |
| 9 | |
| 10 | [ -z "$__mdw_profile" -a -r $HOME/.bash_profile ] && . $HOME/.bash_profile |
| 11 | [ -r /etc/bashrc ] && . /etc/bashrc |
| 12 | |
| 13 | # --- First of all, set up the prompt string --- |
| 14 | |
| 15 | if [ -t 0 ]; then |
| 16 | |
| 17 | case "$TERM" in |
| 18 | linux*|screen*|xterm*|vt100*|eterm*) |
| 19 | bold="\[$(tput bold)\]" unbold="\[$(tput sgr0)\]" nl="\[\r\]" ;; |
| 20 | *) |
| 21 | bold='' unbold='' nl='' ;; |
| 22 | esac |
| 23 | |
| 24 | if (( EUID == 0 )); then |
| 25 | left=`echo « | iconv -f utf8 -t //translit` |
| 26 | right=`echo » | iconv -f utf8 -t //translit` |
| 27 | else |
| 28 | case $USER in |
| 29 | mdw|mwooding) |
| 30 | u="" left="[" right="]" |
| 31 | ;; |
| 32 | *) |
| 33 | u="\\u@" left="{" right="}" |
| 34 | ;; |
| 35 | esac |
| 36 | if [ "$__mdw_tty" = "`tty`" ]; then |
| 37 | left="<" right=">" |
| 38 | else |
| 39 | export __mdw_tty="`tty`" |
| 40 | fi |
| 41 | fi |
| 42 | |
| 43 | if [ -z "$SSH_CLIENT" ] && |
| 44 | [ "$__mdw_sechost" != "`hostname`" ] |
| 45 | then |
| 46 | sec_l='(' sec_r=')' |
| 47 | fi |
| 48 | |
| 49 | PS1="$nl$bold$left$sec_l$u\\h$sec_r \\w$right$unbold" |
| 50 | PS2="$PS1 $bold>$unbold " |
| 51 | |
| 52 | fi # is stdin a tty? |
| 53 | |
| 54 | # --- Pagers are less useful within Emacs --- |
| 55 | |
| 56 | case "$INSIDE_EMACS" in |
| 57 | 22.*,comint) export PAGER=cat ;; |
| 58 | esac |
| 59 | |
| 60 | # --- Little preferences --- |
| 61 | |
| 62 | notify=1 |
| 63 | set -b |
| 64 | shopt -u cdable_vars |
| 65 | shopt -s cdspell |
| 66 | shopt -s checkhash |
| 67 | shopt -s checkwinsize |
| 68 | shopt -s cmdhist |
| 69 | shopt -u dotglob |
| 70 | shopt -s expand_aliases |
| 71 | shopt -s extglob |
| 72 | shopt -s histappend |
| 73 | shopt -s histreedit |
| 74 | shopt -u histverify |
| 75 | shopt -s hostcomplete |
| 76 | shopt -s huponexit |
| 77 | shopt -s interactive_comments |
| 78 | shopt -s lithist |
| 79 | shopt -u mailwarn |
| 80 | shopt -u nocaseglob |
| 81 | shopt -u nullglob |
| 82 | shopt -s promptvars |
| 83 | shopt -u shift_verbose |
| 84 | shopt -s sourcepath |
| 85 | |
| 86 | # --- Set the CDPATH --- |
| 87 | # |
| 88 | # CDPATH=~/src:/usr/src:/usr/lib:/usr/share |
| 89 | # dots=.. |
| 90 | # i=6 |
| 91 | # while (( i > 0 )); do |
| 92 | # CDPATH=$CDPATH:$dots |
| 93 | # dots=$dots/.. |
| 94 | # (( i -= 1 )) |
| 95 | # done |
| 96 | # CDPATH=$CDPATH:/ |
| 97 | |
| 98 | # --- Some colour `ls' support --- |
| 99 | |
| 100 | [ "${TMPDIR+yes}" ] || eval `tmpdir -b` |
| 101 | if [ -x /usr/bin/dircolors -o -x /usr/local/bin/dircolors ]; then |
| 102 | eval `dircolors -b ~/.dircolors` |
| 103 | else |
| 104 | unset LS_COLORS |
| 105 | fi |
| 106 | |
| 107 | ls () { |
| 108 | if [ -t 1 ]; then |
| 109 | command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@" |
| 110 | else |
| 111 | command ls "$@" |
| 112 | fi |
| 113 | } |
| 114 | |
| 115 | # --- Some colour `grep' support --- |
| 116 | |
| 117 | export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34" |
| 118 | |
| 119 | greplike () { |
| 120 | declare grep=$1; shift |
| 121 | if [ -t 1 ]; then |
| 122 | command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager |
| 123 | else |
| 124 | command $grep "$@" |
| 125 | fi |
| 126 | } |
| 127 | alias grep="greplike grep" |
| 128 | alias egrep="greplike egrep" |
| 129 | alias fgrep="greplike fgrep" |
| 130 | alias zgrep="greplike zgrep" |
| 131 | |
| 132 | # --- Set up some simple aliases --- |
| 133 | |
| 134 | alias cx='chmod a+x' |
| 135 | alias which="command -v" |
| 136 | alias ssync="rsync -e ssh" |
| 137 | alias rootly=$__MDW_ROOTLY |
| 138 | alias r=rootly |
| 139 | alias re="rootly $EDITOR" |
| 140 | alias pstree="pstree -hl" |
| 141 | alias cdtmp='cd ${TMPDIR-/tmp}' |
| 142 | alias pushtmp='pushd ${TMPDIR-/tmp}' |
| 143 | alias e="$EDITOR" |
| 144 | alias svn="svnwrap svn" |
| 145 | alias @="ssh" |
| 146 | |
| 147 | [ -r /etc/bash_completion ] && . /etc/bash_completion |
| 148 | [ -r $HOME/.bash_completion ] && . $HOME/.bash_completion |
| 149 | |
| 150 | # --- Make `xt' start an xterm, maybe logging into a remote host --- |
| 151 | |
| 152 | xt () { |
| 153 | case "$1" in |
| 154 | @*) |
| 155 | local remote=${1#@} title |
| 156 | shift |
| 157 | if [ $# -gt 0 ]; then |
| 158 | title="xterm [$remote] $1" |
| 159 | else |
| 160 | title="xterm [$remote]" |
| 161 | fi |
| 162 | (xterm -title "$title" -e ssh $remote "$@" &) |
| 163 | ;; |
| 164 | *) |
| 165 | (xterm "$@" &) |
| 166 | ;; |
| 167 | esac |
| 168 | } |
| 169 | |
| 170 | # --- Turning on and off core dumps --- |
| 171 | |
| 172 | core () { |
| 173 | case "x$1" in |
| 174 | xon|xy|xyes) |
| 175 | ulimit -Sc `ulimit -Hc` |
| 176 | ;; |
| 177 | xoff|xn|xno) |
| 178 | ulimit -Sc 0 |
| 179 | ;; |
| 180 | x) |
| 181 | local l=`ulimit -Sc` |
| 182 | case $l in |
| 183 | 0) echo "Core dumps disabled" ;; |
| 184 | unlimited) echo "Core dumps enabled" ;; |
| 185 | *) echo "Core dump limit is $l blocks" ;; |
| 186 | esac |
| 187 | ;; |
| 188 | *) |
| 189 | echo >&2 "usage: core [yn]" |
| 190 | return 1 |
| 191 | ;; |
| 192 | esac |
| 193 | } |
| 194 | |
| 195 | # --- Turning on and off path hacks --- |
| 196 | |
| 197 | path-add () { |
| 198 | local pathvar export dir val |
| 199 | case $# in |
| 200 | 1) pathvar=PATH dir=$1 export="export PATH";; |
| 201 | 2) pathvar=$1 dir=$2 export=:;; |
| 202 | *) echo >&2 "Usage: $0 [VAR] DIR";; |
| 203 | esac |
| 204 | eval "val=\$$pathvar" |
| 205 | case ":$val:" in |
| 206 | *:"$dir":*) ;; |
| 207 | *) val=$dir:$val ;; |
| 208 | esac |
| 209 | eval "$pathvar=\$val" |
| 210 | $export |
| 211 | } |
| 212 | |
| 213 | path-remove () { |
| 214 | local pathvar export dir val |
| 215 | case $# in |
| 216 | 1) pathvar=PATH dir=$1 export="export PATH";; |
| 217 | 2) pathvar=$1 dir=$2 export=:;; |
| 218 | *) echo >&2 "Usage: $0 [VAR] DIR";; |
| 219 | esac |
| 220 | eval "val=\$$pathvar" |
| 221 | case ":$val:" in |
| 222 | :"$dir":) val= ;; |
| 223 | :"$dir":*) val=${val#$dir:} ;; |
| 224 | *:"$dir":) val=${val%:$dir} ;; |
| 225 | *:"$dir":*) val=${val/:$dir:/:} ;; |
| 226 | esac |
| 227 | eval "$pathvar=\$val" |
| 228 | $export |
| 229 | } |
| 230 | |
| 231 | pathhack () { |
| 232 | if [ $# -eq 0 ]; then |
| 233 | local IFS=: |
| 234 | for e in $PATH; do |
| 235 | case "$e" in |
| 236 | "$HOME/bin/hacks/"*) |
| 237 | echo ${e#$HOME/bin/hacks/} |
| 238 | ;; |
| 239 | esac |
| 240 | done |
| 241 | return |
| 242 | fi |
| 243 | local force=nil |
| 244 | local path=$PATH |
| 245 | while [ $# -gt 0 ]; do |
| 246 | arg=$1 |
| 247 | case "$arg" in |
| 248 | -f | --force) |
| 249 | force=t |
| 250 | shift |
| 251 | continue |
| 252 | ;; |
| 253 | --) |
| 254 | shift |
| 255 | break |
| 256 | ;; |
| 257 | [-+]*) |
| 258 | ;; |
| 259 | *) |
| 260 | break |
| 261 | ;; |
| 262 | esac |
| 263 | hack=${arg#[+-]} |
| 264 | dir=$HOME/bin/hacks/$hack |
| 265 | [ -d "$dir" ] || { |
| 266 | echo "$0: path hack $hack not found" |
| 267 | return 1 |
| 268 | } |
| 269 | case "$arg,$force,:$PATH:" in |
| 270 | -*,*,*:"$dir":*) |
| 271 | path-remove path "$dir" |
| 272 | ;; |
| 273 | +*,t,*:"$dir":*) |
| 274 | path-remove path "$dir" |
| 275 | path-add path "$dir" |
| 276 | ;; |
| 277 | +*,nil,*:"$dir":*) |
| 278 | ;; |
| 279 | +*,*) |
| 280 | path-add path "$dir" |
| 281 | ;; |
| 282 | esac |
| 283 | shift |
| 284 | done |
| 285 | if [ $# -eq 0 ]; then |
| 286 | PATH=$path |
| 287 | export PATH |
| 288 | else |
| 289 | PATH=$path "$@" |
| 290 | fi |
| 291 | } |
| 292 | |
| 293 | # --- Switching security worlds --- |
| 294 | |
| 295 | world () { |
| 296 | local nfast=${NFAST_HOME-/opt/nfast} |
| 297 | local kmdata |
| 298 | case "$#" in |
| 299 | 0) |
| 300 | echo "${NFAST_KMDATA#$nfast/kmdata-}" |
| 301 | ;; |
| 302 | *) |
| 303 | if [ -d "$1" ]; then |
| 304 | kmdata=$1 |
| 305 | elif [ -d "$nfast/kmdata-$1" ]; then |
| 306 | kmdata=$nfast/kmdata-$1 |
| 307 | else |
| 308 | echo >&2 "world: can't find world $1" |
| 309 | return 1 |
| 310 | fi |
| 311 | shift |
| 312 | case "$#" in |
| 313 | 0) export NFAST_KMDATA=$kmdata ;; |
| 314 | *) "$@" ;; |
| 315 | esac |
| 316 | ;; |
| 317 | esac |
| 318 | } |
| 319 | |
| 320 | # --- Fix `man' under Slowaris --- |
| 321 | |
| 322 | case "$MACHTYPE" in |
| 323 | *solaris*) |
| 324 | man () { |
| 325 | declare -i i=0 |
| 326 | declare arg |
| 327 | declare -a man |
| 328 | |
| 329 | for arg; do |
| 330 | case "$arg" in [0-9]*) man[i+=1]="-s" ;; esac |
| 331 | man[i+=1]="$arg" |
| 332 | done |
| 333 | command man "${man[@]}" |
| 334 | } |
| 335 | ;; |
| 336 | esac |
| 337 | |
| 338 | # --- For `root' use -- some simple molly-guards --- |
| 339 | |
| 340 | if (( UID == 0 )); then |
| 341 | alias rm='rm -i' cp='cp -i' mv='mv -i' |
| 342 | set -o noclobber |
| 343 | fi |
| 344 | |
| 345 | [ -f "$HOME/.bashrc-local" ] && . "$HOME/.bashrc-local" |
| 346 | |
| 347 | fi |