New options `-t': timeout
[nethome.git] / .bashrc
diff --git a/.bashrc b/.bashrc
index 6f7a9c9..928315f 100644 (file)
--- a/.bashrc
+++ b/.bashrc
@@ -1,16 +1,46 @@
-if [ -z "$_home_short_dotbashrc" ];then
-       _home_short_dotbashrc=true
+# $Id$
+
+
+if [ -n "$_home_short_dotbashrc_norc" -o "${TERM#norc-}" != "$TERM" ];then
+       # We could get executed twice:
+       export _home_short_dotbashrc_norc=true
+       # Do not use $TZ as Solaris sshd(8) does not propagate it.
+       # Be aware you must use 'ssh -t' to propagate $TERM otherwise '=dumb'.
+       export TERM="${TERM#norc-}"
+       export _home_short_dotbashrc=true
+fi
+
+# "$PS1" for interactive shells detection.
+if [ -n "$PS1" -a -z "$_home_short_dotbashrc" ];then
+       # We permit later reinitialization by unsetting it.
+       # During our execution we forbid spawning to prevent deadlock by:
+       #       source /etc/bashrc
+       #       . /etc/profile.d/lang.sh
+       #       unicode_start iso02.08
+       export _home_short_dotbashrc=true
+       # $TERM gets restored to its 'norc-' state after ~/.bashrc
+       # alias norc='TERM="norc-$TERM" ssh -t -o 'NoHostAuthenticationForLocalhost yes' $USER@localhost "cd $PWD; TERM=$TERM;"'
+       function norc { TERM="norc-$TERM" ssh -t -o 'NoHostAuthenticationForLocalhost yes' $USER@localhost "cd $PWD; TERM=$TERM;" "$@"; }; export -f norc
+
+# Fix $USER and $LOGNAME (not $MAIL) for /etc/passwd line:
+# root-lace:x:0:0:root for Jan Kratochvil:/home/root-lace:/bin/bash
+if [ "$UID" -eq 0 -a "$USER" = "root" -a "$HOME" != "${HOME#*/root-}" ];then
+       USER="${HOME##*/}"
+       LOGNAME="$USER"
+fi
 
 if [ -f /etc/bashrc ];then source /etc/bashrc;fi
 
+_bash_profile_addpath_fini=""
 function _bash_profile_addpath # <variablename>,<dirpathname>
 {
-       _OPTIND="$OPTIND"
+       local _varname _varval _varvalold _dirpathname _opt_f _opt_s
        _opt_f=0;_opt_s=0
-       OPTIND=1;while getopts fs got;do
+       unset -v OPTIND
+       while getopts fs got;do
                case "$got" in
                        [fs]) eval "_opt_$got=1" ;;
-                       *) exit 1 ;;
+                       *) return 1 ;;
                        esac
                done
        eval "_varname=\"\$$[$OPTIND+0]\""
@@ -19,87 +49,180 @@ function _bash_profile_addpath     # <variablename>,<dirpathname>
        if [ $_opt_s = 1 -o -z "`eval "echo \\${$_varname}"`" ];then
                eval "$_varname='$_dirpathname'"
        else
-               # we want to override any original directories, place dirpathname first!
+               # We want to prevent duplicates due to .bashrc reentrancy across fork(2)s. It is array!
+               _bash_profile_addpath_fini="$_bash_profile_addpath_fini $_varname"
+               # We want to override any original directories, place dirpathname first!
                eval "$_varname=\"$_dirpathname:\${$_varname}\""
        fi
        export "$_varname"
-       unset _varname _dirpathname _opt_f _opt_s
-       OPTIND="$_OPTIND";unset _OPTIND
+}
+function _bash_profile_addpath_fini
+{
+       local _varname
+       for _varname in `echo "$_bash_profile_addpath_fini"|tr ' ' '\n'|sort -u`;do
+               eval "$_varname=\"`eval $(echo "echo \"\\$$_varname\"")|awk -v RS=':' '/[^\n]/{if (!x[$0]++) e=e":"$0;}END{print substr(e,2)}'`\""
+               done
+       unset -v _bash_profile_addpath_fini
 }
 
-_bash_profile_addpath -f PATH            "$HOME/bin"
-_bash_profile_addpath -f PATH            "$HOME/sbin"
-_bash_profile_addpath -f LD_LIBRARY_PATH "$HOME/lib"
-_bash_profile_addpath -f MANPATH         "$HOME/man"
-
-_bash_profile_addpath -f -s CVSROOT "short@vellum.cz:/home/short/pserver/cvs"  # default
+# Use reverse order of preferred directories:
+# gid(1) dependency on: /opt/sfw/bin
+# EUID="`id -u 2>/dev/null || /usr/xpg4/bin/id -u 2>/dev/null || gid -u`"
+_bash_profile_addpath -f PATH            "/sbin"
+_bash_profile_addpath -f PATH            "/usr/sbin"
+[ $EUID -eq 0 ] && _bash_profile_addpath -f PATH "/usr/local/sbin"
+_bash_profile_addpath -f PATH            "/usr/local/bin"
+_bash_profile_addpath -f MANPATH         "/usr/X11R6/man"
+_bash_profile_addpath -f MANPATH         "/usr/share/man"
+_bash_profile_addpath -f MANPATH         "/usr/local/man"
+_bash_profile_addpath -s JAVA_HOME       "/usr/java"
+_bash_profile_addpath -s JAVA_HOME       "/usr/local/java"
+uname_p="`uname -p 2>/dev/null || uname -m`"
+for family in $HOME;do
+       for arch in $family $family/$uname_p;do
+               # Do not "-f": We use /net !
+               _bash_profile_addpath PATH            "$arch/bin"
+               _bash_profile_addpath -s JAVA_HOME    "$arch/java"
+               [ $EUID -eq 0 ] && _bash_profile_addpath PATH "$arch/sbin"
+               _bash_profile_addpath LD_LIBRARY_PATH "$arch/lib64"
+               _bash_profile_addpath LD_LIBRARY_PATH "$arch/lib"
+               _bash_profile_addpath MANPATH         "$arch/man"
+               done
+       done
+_bash_profile_addpath -f -s CVSROOT ":pserver:pserver:@cvs.jankratochvil.net/cvs"      # default
+[ -n "$JAVA_HOME" ] && _bash_profile_addpath PATH "$JAVA_HOME/bin"
 _bash_profile_addpath    -s CVSROOT "$HOME/pserver/cvs"
 _bash_profile_addpath    -s CVSROOT "$HOME/cvs"
+_bash_profile_addpath    -s ANT_HOME "/usr/local/ant"
 
-mkdir -p $HOME/lib/perl5       # otherwise CPAN will install to "$HOME/lib/site_perl" etc.
-for _perldir in $(eval "echo $HOME/lib/perl5{,/site_perl}$(
-       for perlvar in version archname;do
-               echo -n '{,/'
-               perl -V:"$perlvar"|cut -d "'" -f 2|tr -d '\n'
-               echo -n '}'
-               done
-       )");do
-       _bash_profile_addpath -f PERLLIB "$_perldir"
+# /share/perl for Debian
+# /lib/perl for Debian
+# /lib/perl5 for Fedora Core
+mkdir -p $HOME/lib{64,}/perl5  # otherwise CPAN will install to "$HOME/lib{64,}/site_perl" etc.
+_perlpatt="$(perl -MConfig -e 'print "{,/".$Config{$_}."}" for qw(version archname);')"
+for _perldir in $(eval "echo {/usr,$HOME}{/lib{64,}/perl{,5},/share/perl}{,/site_perl}$_perlpatt");do
+       # $PERL5LIB overrides $PERLLIB and it is being used by: /etc/profile.d/atrpms.*
+       _bash_profile_addpath -f PERL5LIB "$_perldir"
        done
 unset _perldir
 
 unset -f _bash_profile_addpath
+_bash_profile_addpath_fini
+unset -f _bash_profile_addpath_fini
+
+for i in tar ls find cp mv ln md5sum du grep ar chown chmod tail;do
+       # Prevent g$i from system directory overriding customized $i binary
+       which g$i 2>/dev/null | grep '^/' >/dev/null && alias $i=g$i
+       done
 
 alias ls='ls -bF'
 alias l='ls -lbF'
 alias maple="ssh -t vega.fjfi.cvut.cz maple"
 #alias cc='cc -I/home/short/include'
-alias vi='vim'
+if which vim &>/dev/null;then
+       alias vi='vim'
+else
+       echo >&2 'WARNING: No vim(1).'
+fi
 alias grepb='grep --binary-files=binary'
 alias L='lynx'
+alias chown='chown --no-dereference'
+alias chgrp='chgrp --no-dereference'
+alias hexedit='hexedit -s'
+alias cvs2cl='cvs2cl --window 3600 --separate-header --no-wrap'
 alias cvsfiles='cvsutil --files --print'
 alias cvsignores='cvsutil --ignores --print'
 alias cvsignoresall='cvsutil --ignores --workings --print'
 alias cvsignoresrm='cvsutil --ignores --rm'
 alias cvsignoresrmall='cvsutil --ignores --workings --rm'
 alias cvsignoresallrm='cvsutil --ignores --workings --rm'
+alias svnignoresall='svn status |sed -n "s/^[?]      //p"'
+alias svnignoresallrm='svn status |sed -n "s/^[?]      //p"|tr "\n" "\0"|xargs -0 rm -rf'
+alias disass='objdump -D --target=binary --architecture=i386'
+alias cal='cal -m'
+alias vncreadonly='vncviewer -FullColor -Shared -ViewOnly'
+alias diff='diff -u'
+alias rpmbuildlocal='nice rpmbuild --define "_topdir $PWD" --define "_builddir $PWD" --define "_rpmdir $PWD" --define "_sourcedir $PWD" --define "_specdir $PWD" --define "_srcrpmdir $PWD"'
+alias gdbn='gdb -nx --command=~/.gdbinit'
 
-weberr="/var/log/httpd/error/klokan.error_log"
-if [ -f "$weberr" ];then alias weberr='tail "$weberr"';fi
-weblog="/var/log/httpd/klokan.access_log"
-if [ -f "$weblog" ];then alias weblog='tail -n 20000 "$weblog"|grep "\\(~\\|%7[eE]\\)short"';fi
+# '2>/dev/null' to prevent: unalias: x: not found
+unalias 2>/dev/null cp # -i
+unalias 2>/dev/null mv # -i
+unalias 2>/dev/null rm # -i
+unalias 2>/dev/null df # -h
+unalias 2>/dev/null du # -h
 
 export IGNOREEOF=10
-export TMOUT=900
+# '2>/dev/null' to prevent: TMOUT: readonly variable
+[ -n "$SSH_TTY" ] && export TMOUT=900 2>/dev/null
 export EDITOR='vim'
-export LC_CTYPE=cs_CZ.ISO-8859-2
-export PERL_MM_OPT="PERL=$HOME/bin/perlilib PREFIX=$HOME"
-export WWW_HOME="file:///home/short/.bookmarks.html"
+[ -x /bin/less ] && export PAGER='/bin/less -MMh5cis'
+export PERL_MM_OPT="PERL=$HOME/bin/perlilib"
+[ "$UID" -ne 0 ] && export PERL_MM_OPT="$PERL_MM_OPT PREFIX=$HOME"
+for file in $HOME/.bookmarks.html;do
+       export WWW_HOME="file://$HOME/"
+       [ -r $file ] && export WWW_HOME="file://$file"
+       done
 export GREP_OPTIONS="--binary-files=without-match --directories=skip"
-[ -n "$PS1" ] && export PS1="`echo -en '\017'`$PS1"    # set only in interactive sessions
+if echo x|GREP_OPTIONS="--exclude=y" grep -q x 2>/dev/null;then
+       export GREP_OPTIONS="$GREP_OPTIONS --exclude=tags --exclude=TAGS --exclude=CVS --exclude=ChangeLog*"
+       # FIXME: grep(1) ignores subdirectories for --exclude.
+       #export GREP_OPTIONS="$GREP_OPTIONS --exclude=testsuite --exclude=.pc --exclude=.svn"
+fi
+if [ -n "$PS1" ];then  # set only in interactive sessions
+       export PS1='[bash]${LOGNAME}@${HOSTNAME}:${PWD}# '
+       export PS1='\[\017\]'"$PS1"     # \017=std charmap
+       export PS1='\[\033%G\]'"$PS1"   # \033%G=utf-8
+       # Do not: kbd_mode -u # utf-8
+       # as it breaks keyboard state in X and it is not permitted for non-root.
+       fi
 export MINICOM="-m -c on"      # metakeys+color
 export HISTSIZE=100000
 export HISTFILESIZE="$HISTSIZE"
+export GDBHISTFILE="$HOME/.gdb_history"
 export CVS_RSH="ssh"
+# Do not: grep -w "$TERM" /etc/termcap >/dev/null || export TERM=vt220
+# as Sun workaround as Debian misses: /etc/termcap
+# expects: $uname_p
+[ "$TERM" = vt220 -a "${uname_p%86}" != "$uname_p" ] && export TERM=linux
+if grep 2>/dev/null -q '^..............:0C38 00000000:0000 0A ' /proc/net/tcp;then
+       export  http_proxy=http://127.0.0.1:3128/
+       export https_proxy=http://127.0.0.1:3128/
+       export   ftp_proxy=http://127.0.0.1:3128/
+fi
+_selfabs=
+[ "$_selfabs" = "${_selfabs#/}" ] && _selfabs="$BASH_SOURCE"
+[ "$_selfabs" = "${_selfabs#/}" ] && _selfabs="$PWD/.bashrc"
+[ -r "$_selfabs" ] || unset _selfabs
+[ -n "$_selfabs" ] && export VIMINIT="source `echo "$_selfabs"|sed 's/[.]bashrc$/.vimrc/'`"
 
+# Force English.
+unset LANG LANGUAGE `set|sed -n 's/^\(LC_[^=]*\)=.*$/\1/p'`
+export LANG=en_US.UTF-8
 # Force strcoll() to sort case-sensitively! (empty/undef doesn't work)
 export LC_COLLATE=C
-
-function cvsdiff    { cvs diff   "$@" 2>&1|pipebuf -q|             less; }; export -f cvsdiff
-function cvsdiffi   { cvs diff   "$@" 2>&1|pipebuf -q|grep -v '^?'|less; }; export -f cvsdiffi
-function cvsupdate  { cvs update "$@" 2>&1|pipebuf -q|             less; }; export -f cvsupdate
-function cvsupdatei { cvs update "$@" 2>&1|pipebuf -q|grep -v '^?'|less; }; export -f cvsupdatei
+# Do not: # Prevent: perl: warning: Setting locale failed.
+#         # due to LC_COLLATE=C and LC_ALL=(unset)
+#         export LC_ALL=C
+# as it breaks Mutt "utf-8" and somehow it is no longer needed.
 
 function wclines { awk '{x[$0]++}END{for (i in x) print x[i]"\t"i}'; }; export -f wclines
-function ulimitme { ulimit -m 100000 -d 150000 -v 200000; }; export -f ulimitme
+# SunOS does not support "-m"
+export ULIMITME_OPTS=""
+for i in "-m 100000" "-d 150000" "-v 200000";do
+       (ulimit $i 2>/dev/null) && ULIMITME_OPTS="$ULIMITME_OPTS $i"
+       done
+# Prevent: ulimit:  -m 100000 -d 150000 -v 200000: invalid number
+# by forcing our known $IFS value; eval(1) is needed to take $IFS to effect:
+function ulimitme { IFS=" " eval "ulimit $ULIMITME_OPTS"; }; export -f ulimitme
 function grep {(unset grep; ( ulimitme; grep "$@"; ); );}; export -f grep
 function finame { find . -false `
        awk </dev/null 'BEGIN{ for (i=1;i<ARGC;i++) print " -o -iname *"ARGV[i]"*"; }' "$@";
        `; }; export -f finame
 function mkcd { mkdir -p "$1"; cd "$1"; }; export -f mkcd
 function pushdmk { mkdir -p "$1"; pushd "$1"; }; export -f pushdmk
-function rsync {(unset rsync; rsync -v -e ssh --progress --stats --partial --block-size=512 "$@"; );}; export -f rsync
-function tohex { od -An -txC /tmp/out|tr -d '\n '|tr 'a-f' 'A-F';echo; }; export -f tohex
+function rsync {(unset rsync; nice -n20 rsync -v -e ssh --progress --stats --partial --rsync-path='nice -n20 rsync' "$@"; );}; export -f rsync
+function tohex { od -An -txC|tr -d '\n '|tr 'a-f' 'A-F';echo; }; export -f tohex
 function edall {
        find -type f|while read -r i;do
                (awk </dev/null 'BEGIN{ for (i=1;i<ARGC;i++) print ARGV[i]; }' "$@";echo -e 'w\nq')|ed -s "$i"
@@ -109,15 +232,99 @@ function mi { (
        set -ex;cd "$1";while true;do test "$PWD" != "/";if [ -e configure.in -o -e Makefile.PL ];then break;fi;cd ..;done;make install;
        ); };export -f mi
 function ctags {(unset ctags;
-       if [ $# != 0 ];then ctags "$@";else
-               find -type f -a '(' -name "*.[chC]" -o -name "*.cc" -o -name "*.java" ')'|ctags --file-tags -L -
+       if [ $# -ne 0 -a '(' $# -ne 1 -o "$1" '!=' "--c-types=+px" ')' ];then ctags "$@";else
+               find -name testsuite -prune -o -name .pc -prune -o -name .svn -prune -o \
+                               -type f -a '!' -name tags -a '!' -name TAGS -a '(' -false \
+                                               -o -name "*.[chC]" \
+                                               -o -name "*.cc"    \
+                                               -o -name "*.cxx"   \
+                                               -o -name "*.java"  \
+                                               -o -name "*.p[lm]" \
+                                               -o -name "*.py" \
+                                               -o -name "*.exp" \
+                                               ')' \
+                               |ctags --file-tags -L - --langmap=Tcl:+.exp "$@"
        fi; );}; export -f ctags
+function ctagsh { ctags "--c-types=+px"; }; export -f ctagsh
+function cpan {(unset cpan; if [ $# = 0 ];then cpan;else perl -MCPAN -e "install qw($*);";fi; );}; export -f cpan
+function exx { cd "`unset exx;exx "$@"|tee /proc/self/fd/2|tail -n1|sed -n 's/^extdir=//p'`"; };export -f exx
+function revlines { perl -e 'print reverse <>;'; }; export -f revlines
+function debuginfo { if test -n "$*";then echo "$*";else cat;fi \
+       |xargs rpm -qf|xargs rpmquery --qf '%{SOURCERPM}\n'|sed 's/-[^-]*-[^-]*[.]src[.]rpm$/-debuginfo/'|sort -u; }
+function ipcsrmall { ipcs|awk '/- Shared Memory/{x="-m"}/- Semaphore/{x="-s"}/^0x/{print "ipcrm "x" "$2}'; }
+function eog {(unset eog; touch /tmp/eog.$$;eog "$@" {,,,,,,,,,}/tmp/eog.$$;rm -f /tmp/eog.$$; );}; export -f eog
+function diffc { diff -u "$@"|grep -v '^\(Only in \|Binary files\)'; }; export -f diffc
+function diffi { diffc -X ~/.diffi.list "$@"; }; export -f diffi
+function cvs {(unset cvs;
+       local _opts _opts_d _now _line _o
+       _opts_d="-d"
+       if grep -iw '\(sources.redhat.com\|sourceware.org\):/cvs/src' CVS/Root &>/dev/null;then _opts_d="";fi
+       if grep '^tests/kernel/syscalls/ptrace' CVS/Repository &>/dev/null;then _opts_d="";fi
+       _now=false
+       _line=""
+       _opts=""
+       while [ $# != 0 ];do
+               _o="$1"
+               shift
+               _line="$_line $_o"
+               if [ "$_o" = "--" ] && ! $_now;then _now=true;continue;fi
+               if [ "${_o#-}" != "$_o" ] && ! $_now;then continue;fi
+               if [ "$_o" = update -o "$_o" = upd -o "$_o" = up ];then _opts="$_opts_d";fi
+               break
+               done
+       cvs $_line $_opts "$@"
+       ); }; export -f cvs
+function perlrpm { repoquery -q --whatprovides $(for i in "$@";do echo "perl($i)";done|sed -e 's/\//::/g' -e 's/[.]pm//g'); }; export -f perlrpm
 
-ulimit -c 0
+ulimit -S -c 0
 set +H
 shopt -s mailwarn
+unset OPTIND
+umask 022
 
 if [ -f "$HOME/.bashrc.local" ];then source "$HOME/.bashrc.local";fi
 
+# Interactive part:
+for key in $HOME/.ssh/id_dsa;do
+       [ -r "$key" ] || continue
+       # Do not: [ -n "$SSH_TTY" ] && continue
+       # as we may want foreign SSH keys too.
+
+       agenteval=/tmp/.$USER.ssh-agent.eval
+       if lockfile -1 -r 3 -l 10 -s 1 $agenteval.lock;then
+               test -f $agenteval -a -O $agenteval -a -G $agenteval &&
+                               # ">/dev/null" to prevent: echo Agent pid 31074;
+                               source $agenteval >/dev/null
+               if [ "ssh-agent" != "`(xargs -0 </proc/"$SSH_AGENT_PID"/cmdline sh -c 'echo "$0"') 2>/dev/null `" ];then
+                       rm -f $agenteval
+                       (test ! -e $agenteval && ssh-agent -s >$agenteval) || echo >&2 'Failing ssh-agent!'
+                       # ">/dev/null" to prevent: echo Agent pid 31074;
+                       source $agenteval >/dev/null
+               fi
+               rm -f $agenteval.lock
+       fi
+
+       # Prevent gdm(8) lockups on non-interactive consoles.
+       tty -s || continue
+       (ssh-add -l|grep -qw "$key") && continue
+       if lockfile -! -1 -r 0 $agenteval.input.lock 2>/dev/null;then
+               echo >&2 -n "Locking: $agenteval.input.lock ... "
+               lockfile -1 -r -1 $agenteval.input.lock
+               echo >&2 "locked"
+       fi
+       # Recheck identity existence after the lock acquire
+       if ssh-add -l|grep -qw "$key";then
+               echo >&2 "Identity $key already appended in the meantime."
+       else
+               # "2>/dev/null" to prevent: Identity added: .ssh/id_dsa (.ssh/id_dsa)
+               ssh-add "$key" 2>/dev/null
+       fi
+       rm -f $agenteval.input.lock
+       echo >&2 "$agenteval.input.lock unlocked."
+done
+unset agenteval
+
        unset _home_short_dotbashrc     # permit later reinitializations
 fi     # _home_short_dotbashrc
+
+true   # Prevent crash in '-e' mode