Merge branch 'master' of metalzone:etc/profile
[profile] / dot / bashrc
CommitLineData
bc30f5c4 1# -*- mode: sh; coding: utf-8 -*-
f617db13 2#
f617db13
MW
3# Bash session things
4#
5
6if [ -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
15if [ -t 0 ]; then
16
15b95c26
MW
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`
f617db13 27 else
15b95c26
MW
28 case $USER in
29 mdw|mwooding)
30 u="" left="[" right="]"
31 ;;
f617db13 32 *)
15b95c26
MW
33 u="\\u@" left="{" right="}"
34 ;;
f617db13 35 esac
15b95c26
MW
36 if [ "$__mdw_tty" = "`tty`" ]; then
37 left="<" right=">"
f617db13 38 else
15b95c26 39 export __mdw_tty="`tty`"
f617db13 40 fi
15b95c26 41 fi
f617db13 42
15b95c26
MW
43 if [ -z "$SSH_CLIENT" ] &&
44 [ "$__mdw_sechost" != "`hostname`" ]
45 then
46 sec_l='(' sec_r=')'
f617db13
MW
47 fi
48
15b95c26
MW
49 PS1="$nl$bold$left$sec_l$u\\h$sec_r \\w$right$unbold"
50 PS2="$PS1 $bold>$unbold "
51
f617db13
MW
52fi # is stdin a tty?
53
15b95c26
MW
54# --- Pagers are less useful within Emacs ---
55
56case "$INSIDE_EMACS" in
57 22.*,comint) export PAGER=cat ;;
58esac
59
f617db13
MW
60# --- Little preferences ---
61
62notify=1
63set -b
64shopt -u cdable_vars
65shopt -s cdspell
66shopt -s checkhash
67shopt -s checkwinsize
68shopt -s cmdhist
69shopt -u dotglob
70shopt -s expand_aliases
71shopt -s extglob
72shopt -s histappend
73shopt -s histreedit
74shopt -u histverify
75shopt -s hostcomplete
76shopt -s huponexit
77shopt -s interactive_comments
78shopt -s lithist
79shopt -u mailwarn
80shopt -u nocaseglob
81shopt -u nullglob
82shopt -s promptvars
83shopt -u shift_verbose
84shopt -s sourcepath
85
86# --- Set the CDPATH ---
852cd5fb 87#
f617db13
MW
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`
15b95c26 101if [ -x /usr/bin/dircolors -o -x /usr/local/bin/dircolors ]; then
f617db13
MW
102 eval `dircolors -b ~/.dircolors`
103else
104 unset LS_COLORS
105fi
106
107ls () {
108 if [ -t 1 ]; then
109 command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
110 else
111 command ls "$@"
112 fi
113}
114
bf3f5761
MW
115# --- Some colour `grep' support ---
116
117export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
118
004beb57
MW
119greplike () {
120 declare grep=$1; shift
bf3f5761 121 if [ -t 1 ]; then
a085e9a8 122 command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
bf3f5761 123 else
004beb57 124 command $grep "$@"
bf3f5761
MW
125 fi
126}
004beb57
MW
127alias grep="greplike grep"
128alias egrep="greplike egrep"
129alias fgrep="greplike fgrep"
4da2e817 130alias zgrep="greplike zgrep"
bf3f5761 131
f617db13
MW
132# --- Set up some simple aliases ---
133
134alias cx='chmod a+x'
135alias which="command -v"
136alias ssync="rsync -e ssh"
f141fe0f 137alias rootly=$__MDW_ROOTLY
f617db13 138alias r=rootly
4832db48 139alias re="rootly $EDITOR"
a1545658 140alias pstree="pstree -hl"
f617db13
MW
141alias cdtmp='cd ${TMPDIR-/tmp}'
142alias pushtmp='pushd ${TMPDIR-/tmp}'
4832db48 143alias e="$EDITOR"
ed2a32e1 144alias svn="svnwrap svn"
4543696d 145alias @="ssh"
f617db13 146
75ff64d8
MW
147[ -r /etc/bash_completion ] && . /etc/bash_completion
148[ -r $HOME/.bash_completion ] && . $HOME/.bash_completion
149
f617db13
MW
150# --- Make `xt' start an xterm, maybe logging into a remote host ---
151
152xt () {
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
172core () {
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
32f790cc
MW
195# --- Turning on and off path hacks ---
196
197path-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
213path-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
231pathhack () {
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 --)
852cd5fb 254 shift
32f790cc
MW
255 break
256 ;;
257 [-+]*)
852cd5fb 258 ;;
32f790cc 259 *)
852cd5fb 260 break
32f790cc
MW
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":*)
852cd5fb 271 path-remove path "$dir"
32f790cc
MW
272 ;;
273 +*,t,*:"$dir":*)
852cd5fb 274 path-remove path "$dir"
32f790cc
MW
275 path-add path "$dir"
276 ;;
277 +*,nil,*:"$dir":*)
852cd5fb 278 ;;
32f790cc 279 +*,*)
852cd5fb 280 path-add path "$dir"
32f790cc
MW
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
c08211aa
MW
293# --- Switching security worlds ---
294
295world () {
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
f617db13
MW
320# --- Fix `man' under Slowaris ---
321
322case "$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 ;;
336esac
337
338# --- For `root' use -- some simple molly-guards ---
339
340if (( UID == 0 )); then
341 alias rm='rm -i' cp='cp -i' mv='mv -i'
342 set -o noclobber
343fi
344
68aea1d9
MW
345[ -f "$HOME/.bashrc-local" ] && . "$HOME/.bashrc-local"
346
f617db13 347fi