Do not run in non-interactive shells.
[nethome.git] / .bashrc
1 # $Id$
2
3
4 if [ -n "$_home_short_dotbashrc_norc" -o "${TERM#norc-}" != "$TERM" ];then
5         # We could get executed twice:
6         export _home_short_dotbashrc_norc=true
7         # Do not use $TZ as Solaris sshd(8) does not propagate it.
8         # Be aware you must use 'ssh -t' to propagate $TERM otherwise '=dumb'.
9         export TERM="${TERM#norc-}"
10         export _home_short_dotbashrc=true
11 fi
12
13 # "$PS1" for interactive shells detection.
14 if [ -n "$PS1" -a -z "$_home_short_dotbashrc" ];then
15         # We permit later reinitialization by unsetting it.
16         # During our execution we forbid spawning to prevent deadlock by:
17         #       source /etc/bashrc
18         #       . /etc/profile.d/lang.sh
19         #       unicode_start iso02.08
20         export _home_short_dotbashrc=true
21         # $TERM gets restored to its 'norc-' state after ~/.bashrc
22         # alias norc='TERM="norc-$TERM" ssh -t -o 'NoHostAuthenticationForLocalhost yes' $USER@localhost "cd $PWD; TERM=$TERM;"'
23         function norc { TERM="norc-$TERM" ssh -t -o 'NoHostAuthenticationForLocalhost yes' $USER@localhost "cd $PWD; TERM=$TERM;" "$@"; }; export -f norc
24
25 # Fix $USER and $LOGNAME (not $MAIL) for /etc/passwd line:
26 # root-lace:x:0:0:root for Jan Kratochvil:/home/root-lace:/bin/bash
27 if [ "$UID" -eq 0 -a "$USER" = "root" -a "$HOME" != "${HOME#*/root-}" ];then
28         USER="${HOME##*/}"
29         LOGNAME="$USER"
30 fi
31
32 if [ -f /etc/bashrc ];then source /etc/bashrc;fi
33
34 _bash_profile_addpath_fini=""
35 function _bash_profile_addpath  # <variablename>,<dirpathname>
36 {
37         local _varname _varval _varvalold _dirpathname _opt_f _opt_s
38         _opt_f=0;_opt_s=0
39         unset -v OPTIND
40         while getopts fs got;do
41                 case "$got" in
42                         [fs]) eval "_opt_$got=1" ;;
43                         *) return 1 ;;
44                         esac
45                 done
46         eval "_varname=\"\$$[$OPTIND+0]\""
47         eval "_dirpathname=\"\$$[$OPTIND+1]\""
48         if [ $_opt_f = 0 -a '!' -d "$_dirpathname" ];then return;fi
49         if [ $_opt_s = 1 -o -z "`eval "echo \\${$_varname}"`" ];then
50                 eval "$_varname='$_dirpathname'"
51         else
52                 # We want to prevent duplicates due to .bashrc reentrancy across fork(2)s. It is array!
53                 _bash_profile_addpath_fini="$_bash_profile_addpath_fini $_varname"
54                 # We want to override any original directories, place dirpathname first!
55                 eval "$_varname=\"$_dirpathname:\${$_varname}\""
56         fi
57         export "$_varname"
58 }
59 function _bash_profile_addpath_fini
60 {
61         local _varname
62         for _varname in `echo "$_bash_profile_addpath_fini"|tr ' ' '\n'|sort -u`;do
63                 eval "$_varname=\"`eval $(echo "echo \"\\$$_varname\"")|awk -v RS=':' '/[^\n]/{if (!x[$0]++) e=e":"$0;}END{print substr(e,2)}'`\""
64                 done
65         unset -v _bash_profile_addpath_fini
66 }
67
68 # Use reverse order of preferred directories:
69 # gid(1) dependency on: /opt/sfw/bin
70 # EUID="`id -u 2>/dev/null || /usr/xpg4/bin/id -u 2>/dev/null || gid -u`"
71 _bash_profile_addpath -f PATH            "/opt/SUNWspro/bin"
72 _bash_profile_addpath -f PATH            "/usr/dist/exe"
73 _bash_profile_addpath -f PATH            "/sbin"
74 _bash_profile_addpath -f PATH            "/usr/sbin"
75 _bash_profile_addpath -f PATH            "/opt/sfw/bin"
76 [ -x $HOME/bin/ProductivityTools-check ] && eval `$HOME/bin/ProductivityTools-check`
77 [ -n "$PATH_PRODUCTIVITY_TOOLS" ] && _bash_profile_addpath -f PATH "$PATH_PRODUCTIVITY_TOOLS"
78 [ $EUID -eq 0 ] && _bash_profile_addpath -f PATH "/usr/local/sbin"
79 _bash_profile_addpath -f PATH            "/usr/local/bin"
80 _bash_profile_addpath -f LD_LIBRARY_PATH "/opt/sfw/lib"
81 _bash_profile_addpath -f MANPATH         "/usr/X11R6/man"
82 _bash_profile_addpath -f MANPATH         "/usr/share/man"
83 _bash_profile_addpath -f MANPATH         "/usr/local/man"
84 _bash_profile_addpath -s JAVA_HOME       "/usr/java"
85 _bash_profile_addpath -s JAVA_HOME       "/usr/local/java"
86 uname_p="`uname -p 2>/dev/null || uname -m`"
87 [ "`uname`" = "SunOS" ] && _bash_profile_addpath -f LD_LIBRARY_PATH "$HOME/Netscape/dist/lib"
88 for family in $HOME;do
89         for arch in $family $family/$uname_p;do
90                 # Do not "-f": We use /net !
91                 _bash_profile_addpath PATH            "$arch/bin"
92                 _bash_profile_addpath PATH            "$arch/OpenOffice.org"
93                 _bash_profile_addpath PATH            "$arch/forte4j/extbin"
94                 _bash_profile_addpath -s JAVA_HOME    "$arch/java"
95                 [ $EUID -eq 0 ] && _bash_profile_addpath PATH "$arch/sbin"
96                 _bash_profile_addpath LD_LIBRARY_PATH "$arch/lib"
97                 _bash_profile_addpath MANPATH         "$arch/man"
98                 done
99         done
100 _bash_profile_addpath -f -s CVSROOT "short@vellum.cz:/home/short/pserver/cvs"   # default
101 [ -n "$JAVA_HOME" ] && _bash_profile_addpath PATH "$JAVA_HOME/bin"
102 _bash_profile_addpath    -s CVSROOT "$HOME/pserver/cvs"
103 _bash_profile_addpath    -s CVSROOT "$HOME/cvs"
104 _bash_profile_addpath    -s ANT_HOME "/usr/local/ant"
105
106 # /share/perl for Debian
107 # /lib/perl for Debian
108 # /lib/perl5 for Fedora Core
109 mkdir -p $HOME/lib/perl5        # otherwise CPAN will install to "$HOME/lib/site_perl" etc.
110 _perlpatt="$(perl -MConfig -e 'print "{,/".$Config{$_}."}" for qw(version archname);')"
111 for _perldir in $(eval "echo {/usr,$HOME}{/lib{,64}/perl{,5},/share/perl}{,/site_perl}$_perlpatt");do
112         _bash_profile_addpath -f PERLLIB "$_perldir"
113         done
114 unset _perldir
115
116 unset -f _bash_profile_addpath
117 _bash_profile_addpath_fini
118 unset -f _bash_profile_addpath_fini
119
120 for i in tar ls find cp mv ln md5sum du grep ar chown chmod tail;do
121         # Prevent g$i from system directory overriding customized $i binary
122         ###which $i 2>/dev/null | grep /net/jsc-nfs.czech.sun.com/export/jscqa-local/ >/dev/null && continue
123         which g$i 2>/dev/null | grep '^/' >/dev/null && alias $i=g$i
124         done
125
126 alias ls='ls -bF'
127 alias l='ls -lbF'
128 alias maple="ssh -t vega.fjfi.cvut.cz maple"
129 #alias cc='cc -I/home/short/include'
130 if which vim &>/dev/null;then
131         alias vi='vim'
132 else
133         echo >&2 'WARNING: No vim(1).'
134 fi
135 alias grepb='grep --binary-files=binary'
136 alias L='lynx'
137 alias chown='chown --no-dereference'
138 alias chgrp='chgrp --no-dereference'
139 alias hexedit='hexedit -s'
140 alias cvs2cl='cvs2cl --window 3600 --separate-header --no-wrap'
141 alias cvsfiles='cvsutil --files --print'
142 alias cvsignores='cvsutil --ignores --print'
143 alias cvsignoresall='cvsutil --ignores --workings --print'
144 alias cvsignoresrm='cvsutil --ignores --rm'
145 alias cvsignoresrmall='cvsutil --ignores --workings --rm'
146 alias cvsignoresallrm='cvsutil --ignores --workings --rm'
147 alias svnignoresall='svn status |sed -n "s/^[?]      //p"'
148 alias svnignoresallrm='svn status |sed -n "s/^[?]      //p"|tr "\n" "\0"|xargs -0 rm -rf'
149 [ "`uname`" = "SunOS" -a -x $HOME/bin/rpm-by-pkg ] && alias rpm='rpm-by-pkg'
150 alias disass='objdump -D --target=binary --architecture=i386'
151 alias cal='cal -m'
152 alias vncreadonly='vncviewer -FullColor -Shared -ViewOnly'
153 alias diff='diff -u'
154 alias rpmqf='rpm --qf "%{name}-%{version}-%{release}.%{arch}\n"'
155 alias rpmbuildlocal='rpmbuild --define "_topdir $PWD" --define "_builddir $PWD" --define "_rpmdir $PWD" --define "_sourcedir $PWD" --define "_specdir $PWD" --define "_srcrpmdir $PWD"'
156 alias gdbn='gdb -nx --command=~/.gdbinit'
157
158 # '2>/dev/null' to prevent: unalias: x: not found
159 unalias 2>/dev/null cp  # -i
160 unalias 2>/dev/null mv  # -i
161 unalias 2>/dev/null rm  # -i
162 unalias 2>/dev/null df  # -h
163 unalias 2>/dev/null du  # -h
164
165 export IGNOREEOF=10
166 # '2>/dev/null' to prevent: TMOUT: readonly variable
167 [ -n "$SSH_TTY" ] && export TMOUT=900 2>/dev/null
168 export EDITOR='vim'
169 [ -x /bin/less ] && export PAGER='/bin/less -MMh5cis'
170 export PERL_MM_OPT="PERL=$HOME/bin/perlilib"
171 [ "$UID" -ne 0 ] && export PERL_MM_OPT="$PERL_MM_OPT PREFIX=$HOME"
172 for file in $HOME/.bookmarks.html;do
173         export WWW_HOME="file://$HOME/"
174         [ -r $file ] && export WWW_HOME="file://$file"
175         done
176 export GREP_OPTIONS="--binary-files=without-match --directories=skip --exclude=tags --exclude=TAGS --exclude=CVS"
177 if [ -n "$PS1" ];then   # set only in interactive sessions
178         export PS1='[bash]${LOGNAME}@${HOSTNAME}:${PWD}# '
179         export PS1='\[\017\]'"$PS1"     # \017=std charmap
180         export PS1='\[\033%G\]'"$PS1"   # \033%G=utf-8
181         # Do not: kbd_mode -u # utf-8
182         # as it breaks keyboard state in X and it is not permitted for non-root.
183         fi
184 export MINICOM="-m -c on"       # metakeys+color
185 export HISTSIZE=100000
186 export HISTFILESIZE="$HISTSIZE"
187 export GDBHISTFILE="$HOME/.gdb_history"
188 export CVS_RSH="ssh"
189 # Do not: grep -w "$TERM" /etc/termcap >/dev/null || export TERM=vt220
190 # as Sun workaround as Debian misses: /etc/termcap
191 # expects: $uname_p
192 [ "$TERM" = vt220 -a "${uname_p%86}" != "$uname_p" ] && export TERM=linux
193 if grep 2>/dev/null -q '^..............:0C38 00000000:0000 0A ' /proc/net/tcp;then
194         export  http_proxy=http://127.0.0.1:3128/
195         export https_proxy=http://127.0.0.1:3128/
196         export   ftp_proxy=http://127.0.0.1:3128/
197 fi
198 _selfabs=
199 [ "$_selfabs" = "${_selfabs#/}" ] && _selfabs="$BASH_SOURCE"
200 [ "$_selfabs" = "${_selfabs#/}" ] && _selfabs="$PWD/.bashrc"
201 [ -r "$_selfabs" ] || unset _selfabs
202 [ -n "$_selfabs" ] && export VIMINIT="source `echo "$_selfabs"|sed 's/[.]bashrc$/.vimrc/'`"
203
204 ###export SOCKS5_SERVER=icsocks.holland.sun.com
205
206 # Force English.
207 unset LANG LANGUAGE `set|sed -n 's/^\(LC_[^=]*\)=.*$/\1/p'`
208 export LANG=en_US.UTF-8
209 # Force strcoll() to sort case-sensitively! (empty/undef doesn't work)
210 export LC_COLLATE=C
211 # Do not: # Prevent: perl: warning: Setting locale failed.
212 #         # due to LC_COLLATE=C and LC_ALL=(unset)
213 #         export LC_ALL=C
214 # as it breaks Mutt "utf-8" and somehow it is no longer needed.
215
216 function wclines { awk '{x[$0]++}END{for (i in x) print x[i]"\t"i}'; }; export -f wclines
217 # SunOS does not support "-m"
218 export ULIMITME_OPTS=""
219 for i in "-m 100000" "-d 150000" "-v 200000";do
220         (ulimit $i 2>/dev/null) && ULIMITME_OPTS="$ULIMITME_OPTS $i"
221         done
222 # Prevent: ulimit:  -m 100000 -d 150000 -v 200000: invalid number
223 # by forcing our known $IFS value; eval(1) is needed to take $IFS to effect:
224 function ulimitme { IFS=" " eval "ulimit $ULIMITME_OPTS"; }; export -f ulimitme
225 function grep {(unset grep; ( ulimitme; grep "$@"; ); );}; export -f grep
226 function finame { find . -false `
227         awk </dev/null 'BEGIN{ for (i=1;i<ARGC;i++) print " -o -iname *"ARGV[i]"*"; }' "$@";
228         `; }; export -f finame
229 function mkcd { mkdir -p "$1"; cd "$1"; }; export -f mkcd
230 function pushdmk { mkdir -p "$1"; pushd "$1"; }; export -f pushdmk
231 function rsync {(unset rsync; rsync -v -e ssh --progress --stats --partial --block-size=512 "$@"; );}; export -f rsync
232 function tohex { od -An -txC|tr -d '\n '|tr 'a-f' 'A-F';echo; }; export -f tohex
233 function edall {
234         find -type f|while read -r i;do
235                 (awk </dev/null 'BEGIN{ for (i=1;i<ARGC;i++) print ARGV[i]; }' "$@";echo -e 'w\nq')|ed -s "$i"
236                 done
237         }; export -f edall
238 function mi { (
239         set -ex;cd "$1";while true;do test "$PWD" != "/";if [ -e configure.in -o -e Makefile.PL ];then break;fi;cd ..;done;make install;
240         ); };export -f mi
241 function ctags {(unset ctags;
242         if [ $# -ne 0 -a '(' $# -ne 1 -o "$1" '!=' "--c-types=+px" ')' ];then ctags "$@";else
243                 find -name testsuite -prune -o -name .pc -prune -o -name .svn -o \
244                                 -type f -a '!' -name tags -a '!' -name TAGS -a '(' -false \
245                                                 -o -name "*.[chC]" \
246                                                 -o -name "*.cc"    \
247                                                 -o -name "*.cxx"   \
248                                                 -o -name "*.java"  \
249                                                 -o -name "*.p[lm]" \
250                                                 -o -name "*.py" \
251                                                 -o -name "*.exp" \
252                                                 ')' \
253                                 |ctags --file-tags -L - --langmap=Tcl:+.exp "$@"
254         fi; );}; export -f ctags
255 function ctagsh { ctags "--c-types=+px"; }; export -f ctagsh
256 function cpan {(unset cpan; if [ $# = 0 ];then cpan;else perl -MCPAN -e "install qw($*);";fi; );}; export -f cpan
257 function exx { cd "`unset exx;exx "$@"|tee /proc/self/fd/2|tail -n1|sed -n 's/^extdir=//p'`"; };export -f exx
258 function revlines { perl -e 'print reverse <>;'; }; export -f revlines
259 function debuginfo { if test -n "$*";then echo "$*";else cat;fi \
260         |xargs rpm -qf|xargs rpmquery --qf '%{SOURCERPM}\n'|sed 's/-[^-]*-[^-]*[.]src[.]rpm$/-debuginfo/'|sort -u; }
261 function ipcsrmall { ipcs|awk '/- Shared Memory/{x="-m"}/- Semaphore/{x="-s"}/^0x/{print "ipcrm "x" "$2}'; }
262 function eog {(unset eog; touch /tmp/eog.$$;eog "$@" {,,,,,,,,,}/tmp/eog.$$;rm -f /tmp/eog.$$; );}; export -f eog
263 function diffc { diff -u "$@"|grep -v '^\(Only in \|Binary files\)'; }; export -f diffc
264 function diffi { diffc -X ~/.diffi.list "$@"; }; export -f diffi
265 function cvs {(unset cvs;
266         local _opts _opts_d _now _line _o
267         if grep -iw '\(sources.redhat.com\|sourceware.org\):/cvs/src' CVS/Root &>/dev/null;then _opts_d="";else _opts_d="-d";fi
268         _now=false
269         _line=""
270         _opts=""
271         while [ $# != 0 ];do
272                 _o="$1"
273                 shift
274                 _line="$_line $_o"
275                 if [ "$_o" = "--" ] && ! $_now;then _now=true;continue;fi
276                 if [ "${_o#-}" != "$_o" ] && ! $_now;then continue;fi
277                 if [ "$_o" = update -o "$_o" = upd -o "$_o" = up ];then _opts="$_opts_d";fi
278                 break
279                 done
280         cvs $_line $_opts "$@"
281         ); }; export -f cvs
282
283 ulimit -S -c 0
284 set +H
285 shopt -s mailwarn
286 unset OPTIND
287
288 if [ -f "$HOME/.bashrc.local" ];then source "$HOME/.bashrc.local";fi
289
290 # Interactive part:
291 for key in $HOME/.ssh/id_dsa;do
292         [ -r "$key" ] || continue
293         # Do not: [ -n "$SSH_TTY" ] && continue
294         # as we may want foreign SSH keys too.
295
296         agenteval=/tmp/.$USER.ssh-agent.eval
297         if lockfile -1 -r 3 -l 10 -s 1 $agenteval.lock;then
298                 test -f $agenteval -a -O $agenteval -a -G $agenteval &&
299                                 # ">/dev/null" to prevent: echo Agent pid 31074;
300                                 source $agenteval >/dev/null
301                 if [ "ssh-agent" != "`(xargs -0 </proc/"$SSH_AGENT_PID"/cmdline sh -c 'echo "$0"') 2>/dev/null `" ];then
302                         rm -f $agenteval
303                         (test ! -e $agenteval && ssh-agent -s >$agenteval) || echo >&2 'Failing ssh-agent!'
304                         # ">/dev/null" to prevent: echo Agent pid 31074;
305                         source $agenteval >/dev/null
306                 fi
307                 rm -f $agenteval.lock
308         fi
309
310         # Prevent gdm(8) lockups on non-interactive consoles.
311         tty -s || continue
312         (ssh-add -l|grep -qw "$key") && continue
313         if lockfile -! -1 -r 0 $agenteval.input.lock 2>/dev/null;then
314                 echo >&2 -n "Locking: $agenteval.input.lock ... "
315                 lockfile -1 -r -1 $agenteval.input.lock
316                 echo >&2 "locked"
317         fi
318         # Recheck identity existence after the lock acquire
319         if ssh-add -l|grep -qw "$key";then
320                 echo >&2 "Identity $key already appended in the meantime."
321         else
322                 # "2>/dev/null" to prevent: Identity added: .ssh/id_dsa (.ssh/id_dsa)
323                 ssh-add "$key" 2>/dev/null
324         fi
325         rm -f $agenteval.input.lock
326         echo >&2 "$agenteval.input.lock unlocked."
327 done
328 unset agenteval
329
330         unset _home_short_dotbashrc     # permit later reinitializations
331 fi      # _home_short_dotbashrc
332
333 true    # Prevent crash in '-e' mode