chiark / gitweb /
setup: Modify URL for the FTP repository.
[profile] / dot / bashrc
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 globstar
73 shopt -s gnu_errfmt
74 shopt -s histappend
75 shopt -s histreedit
76 shopt -u histverify
77 shopt -s hostcomplete
78 shopt -s huponexit
79 shopt -s interactive_comments
80 shopt -s lithist
81 shopt -u mailwarn
82 shopt -u nocaseglob
83 shopt -u nullglob
84 shopt -s promptvars
85 shopt -u shift_verbose
86 shopt -s sourcepath
87
88 HISTCONTROL=ignorespace:erasedups
89
90 # --- Set the CDPATH ---
91 #
92 # CDPATH=~/src:/usr/src:/usr/lib:/usr/share
93 # dots=..
94 # i=6
95 # while (( i > 0 )); do
96 #   CDPATH=$CDPATH:$dots
97 #   dots=$dots/..
98 #   (( i -= 1 ))
99 # done
100 # CDPATH=$CDPATH:/
101
102 # --- Some colour `ls' support ---
103
104 [ "${TMPDIR+yes}" ] || eval `tmpdir -b`
105 if [ -x /usr/bin/dircolors -o -x /usr/local/bin/dircolors ]; then
106   eval `dircolors -b ~/.dircolors`
107 else
108   unset LS_COLORS
109 fi
110
111 ls () {
112   if [ -t 1 ]; then
113     command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
114   else
115     command ls "$@"
116   fi
117 }
118
119 # --- Some colour `grep' support ---
120
121 export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
122
123 greplike () {
124   declare grep=$1; shift
125   if [ -t 1 ]; then
126     command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
127   else
128     command $grep "$@"
129   fi
130 }
131 alias grep="greplike grep"
132 alias egrep="greplike egrep"
133 alias fgrep="greplike fgrep"
134 alias zgrep="greplike zgrep"
135
136 # --- Set up some simple aliases ---
137
138 alias cx='chmod a+x'
139 alias which="command -v"
140 alias rc="rc -l"
141 alias ssync="rsync -e ssh"
142 alias rootly=$__MDW_ROOTLY
143 alias r=rootly
144 alias re="rootly $EDITOR"
145 alias pstree="pstree -hl"
146 alias cdtmp='cd ${TMPDIR-/tmp}'
147 alias pushtmp='pushd ${TMPDIR-/tmp}'
148 alias e="$EDITOR"
149 alias svn="svnwrap svn"
150 alias @="ssh"
151
152 [ -r /etc/bash_completion ] && . /etc/bash_completion
153 [ -r $HOME/.bash_completion ] && . $HOME/.bash_completion
154
155 # --- Make `xt' start an xterm, maybe logging into a remote host ---
156
157 xt () {
158   case "$1" in
159     @*)
160       local remote=${1#@} title
161       shift
162       if [ $# -gt 0 ]; then
163         title="xterm [$remote] $1"
164       else
165         title="xterm [$remote]"
166       fi
167       (xterm -title "$title" -e ssh $remote "$@" &)
168       ;;
169     *)
170       (xterm "$@" &)
171       ;;
172   esac
173 }
174
175 # --- Turning on and off core dumps ---
176
177 core () {
178   case "x$1" in
179     xon|xy|xyes)
180       ulimit -Sc `ulimit -Hc`
181       ;;
182     xoff|xn|xno)
183       ulimit -Sc 0
184       ;;
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 [yn]"
195       return 1
196       ;;
197   esac
198 }
199
200 # --- Turning on and off path hacks ---
201
202 path-add () {
203   local pathvar export dir val
204   case $# in
205     1) pathvar=PATH dir=$1 export="export PATH";;
206     2) pathvar=$1 dir=$2 export=:;;
207     *) echo >&2 "Usage: $0 [VAR] DIR";;
208   esac
209   eval "val=\$$pathvar"
210   case ":$val:" in
211     *:"$dir":*) ;;
212     *) val=$dir:$val ;;
213   esac
214   eval "$pathvar=\$val"
215   $export
216 }
217
218 path-remove () {
219   local pathvar export dir val
220   case $# in
221     1) pathvar=PATH dir=$1 export="export PATH";;
222     2) pathvar=$1 dir=$2 export=:;;
223     *) echo >&2 "Usage: $0 [VAR] DIR";;
224   esac
225   eval "val=\$$pathvar"
226   case ":$val:" in
227     :"$dir":) val= ;;
228     :"$dir":*) val=${val#$dir:} ;;
229     *:"$dir":) val=${val%:$dir} ;;
230     *:"$dir":*) val=${val/:$dir:/:} ;;
231   esac
232   eval "$pathvar=\$val"
233   $export
234 }
235
236 pathhack () {
237   if [ $# -eq 0 ]; then
238     local IFS=:
239     for e in $PATH; do
240       case "$e" in
241         "$HOME/bin/hacks/"*)
242           echo ${e#$HOME/bin/hacks/}
243           ;;
244       esac
245     done
246     return
247   fi
248   local force=nil
249   local path=$PATH
250   while [ $# -gt 0 ]; do
251     arg=$1
252     case "$arg" in
253       -f | --force)
254         force=t
255         shift
256         continue
257         ;;
258       --)
259         shift
260         break
261         ;;
262       [-+]*)
263         ;;
264       *)
265         break
266         ;;
267     esac
268     hack=${arg#[+-]}
269     dir=$HOME/bin/hacks/$hack
270     [ -d "$dir" ] || {
271       echo "$0: path hack $hack not found"
272       return 1
273     }
274     case "$arg,$force,:$PATH:" in
275       -*,*,*:"$dir":*)
276         path-remove path "$dir"
277         ;;
278       +*,t,*:"$dir":*)
279         path-remove path "$dir"
280         path-add path "$dir"
281         ;;
282       +*,nil,*:"$dir":*)
283         ;;
284       +*,*)
285         path-add path "$dir"
286         ;;
287     esac
288     shift
289   done
290   if [ $# -eq 0 ]; then
291     PATH=$path
292     export PATH
293   else
294     PATH=$path "$@"
295   fi
296 }
297
298 # --- Switching security worlds ---
299
300 world () {
301   local nfast=${NFAST_HOME-/opt/nfast}
302   local kmdata
303   case "$#" in
304     0)
305       echo "${NFAST_KMDATA#$nfast/kmdata-}"
306       ;;
307     *)
308       if [ -d "$1" ]; then
309         kmdata=$1
310       elif [ -d "$nfast/kmdata-$1" ]; then
311         kmdata=$nfast/kmdata-$1
312       else
313         echo >&2 "world: can't find world $1"
314         return 1
315       fi
316       shift
317       case "$#" in
318         0) export NFAST_KMDATA=$kmdata ;;
319         *) "$@" ;;
320       esac
321       ;;
322   esac
323 }
324
325 # --- Fix `man' under Slowaris ---
326
327 case "$MACHTYPE" in
328   *solaris*)
329     man () {
330       declare -i i=0
331       declare arg
332       declare -a man
333
334       for arg; do
335         case "$arg" in [0-9]*) man[i+=1]="-s" ;; esac
336         man[i+=1]="$arg"
337       done
338       command man "${man[@]}"
339     }
340     ;;
341 esac
342
343 # --- For `root' use -- some simple molly-guards ---
344
345 if (( UID == 0 )); then
346   alias rm='rm -i' cp='cp -i' mv='mv -i'
347   set -o noclobber
348 fi
349
350 [ -f "$HOME/.bashrc-local" ] && . "$HOME/.bashrc-local"
351
352 fi