devi@70: #!/bin/sh devi@70: devi@70: devi@76: # Environment variables set by the script: devi@76: # devi@76: # L3_SESSION_ID - uniq id of the LiLaLo-session devi@76: # L3_PARENT_TTY - name of tty on which script is running devi@76: # L3_TTY - current tty devi@76: # PS1 - intercative shell prompt in which LiLaLo hides devi@76: # various information about the command devi@76: # L3_TAMPERED_EDITORS - list of editors which are tampered with functions devi@76: devi@76: devi@76: # Functions with the names starting _l3_ are internal. devi@76: # Such functions are unset before this rc script exits devi@76: devi@76: _l3_editors_to_tamper='/bin/vi /usr/bin/vi /usr/bin/vim /bin/ee /usr/bin/ee /usr/bin/pico /usr/bin/nano /usr/local/bin/vim' devi@76: devi@70: _l3_start() devi@70: { devi@76: if _l3_is_not_running_here devi@76: then devi@76: _l3_start_session devi@76: _l3_run_script devi@76: else devi@76: _l3_env devi@76: _l3_init_prompt devi@76: l3_fix_prompt devi@76: _l3_tamper_editors devi@76: _l3_tamper_commands devi@76: _l3_unset_internal devi@76: fi devi@76: } devi@76: devi@76: # ===================== STAGE 1 ============================ devi@76: devi@76: _l3_is_not_running_here() devi@76: { devi@76: export L3_TTY=`/usr/bin/tty` devi@70: proc_on_the_term=`w | grep "${L3_TTY##/dev/}" | awk '{print $8;}'` devi@70: # freeBSD: devi@70: [ -n "$bsd" ] && \ devi@70: proc_on_the_term=`w | grep "${L3_TTY##/dev/tty}" | awk '{print $6;}'` devi@70: devi@76: [ -n "$proc_on_the_term" ] && echo $proc_on_the_term | grep -qv script devi@70: } devi@70: devi@70: _l3_start_session() devi@70: { devi@71: export L3_SESSION_ID=${L3_TTY##*/}-$$ devi@71: export L3_HOME=~/.lilalo/ devi@70: mkdir -p $L3_HOME devi@70: devi@70: uname -a | grep -qi bsd && bsd=yes devi@70: parent=`cat /proc/$PPID/cmdline 2> /dev/null` devi@70: system=`uname -rs` devi@76: login_from=`who | grep "${L3_TTY##/dev/}" | awk '{print $6;}' | tr -d '()'` devi@70: #[ -n "$bsd" ] && login_from="" #FIXME! devi@70: start_time=`date +%s` devi@70: hostname=`hostname -f 2> /dev/null` devi@70: [ -n "$bsd" ] && hostname=`hostname` devi@70: devi@70: cat < $L3_HOME/$L3_SESSION_ID.info devi@70: devi@70: $L3_SESSION_ID devi@70: $hostname devi@70: $USER devi@70: $UID devi@70: $login_from devi@76: $L3_TTY devi@70: $system devi@70: $parent devi@70: $PPID devi@70: $$ devi@70: $start_time devi@76: $LANG devi@70: devi@70: INFO devi@70: devi@70: unset parent system login_from start_time hostname devi@70: } devi@70: devi@70: _l3_run_script() devi@70: { devi@70: uname -a | grep -qi bsd && bsd=yes devi@70: flush="-f" #linux devi@70: [ -n "$bsd" ] && flush="-t 0" #freebsd devi@76: export L3_PARENT_TTY=`/usr/bin/tty` devi@70: exec script $flush -q $L3_HOME/${L3_SESSION_ID}.script devi@70: } devi@70: devi@76: # ===================== STAGE 2 ============================ devi@76: devi@76: _l3_env() devi@76: { devi@76: trap l3_close_session 2 devi@76: trap l3_close_session EXIT devi@76: true devi@76: } devi@76: devi@76: l3_close_session() devi@70: { devi@71: ( devi@71: echo '' devi@71: history | sed 's/&/\&/; s//\>/g' devi@71: echo '' devi@71: ) >> $L3_HOME/$L3_SESSION_ID.info devi@70: } devi@70: devi@76: _l3_init_prompt() devi@76: { devi@76: PS1='[\u@\h:\W]\$ ' devi@77: [ $UID = 0 ] \ devi@77: && PS1='\[\033[0;31m\]'$PS1'\[\033[0m\]' \ devi@77: || PS1='\[\033[0;32m\]'$PS1'\[\033[0m\]' \ devi@76: export PS1 devi@76: } devi@76: devi@76: l3_fix_prompt() devi@76: { devi@76: export PS1='\[v2#\!#$?#$UID#$$#$(/bin/date +%s)#$PWD#\033[1024D\033[K\]'$PS1 devi@76: } devi@76: devi@76: _l3_tamper_editors() devi@76: { devi@76: for editor_file in $_l3_editors_to_tamper devi@76: do devi@76: [ -x $editor_file ] || continue devi@76: editor_name=${editor_file##*/} devi@76: eval " devi@76: $editor_name() { devi@76: if [ -d \"\$1\" ] devi@76: then devi@76: $editor_file \"\$1\" devi@76: return \$? devi@76: else devi@76: TIME=\"\`date +%s\`\" devi@76: DIR=\"\" devi@76: [ \"\${1#/}\" = \"\$1\" ] && DIR=\"\$PWD/\" devi@76: DIFFNAME=\"\$PPID_\${TIME}_\`echo \$DIR\$1| /bin/sed s@_@__@ | /bin/sed 's@/@_@g'\`.diff\" devi@76: old_file=\"/tmp/l3-saved-\$\$.\$RANDOM.\$RANDOM\" devi@76: /bin/cp -- \"\$1\" \"\$old_file\" 2> /dev/null devi@76: $editor_file \"\$@\" || ERR=\$? devi@76: [ -e \"\$old_file\" ] && diff \"\$old_file\" \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null \ devi@76: || diff /dev/null \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null devi@76: /bin/rm \"\$old_file\" 2> /dev/null devi@76: return \$ERR devi@76: fi devi@76: } devi@76: " devi@76: L3_TAMPERED_EDITORS="$L3_TAMPERED_EDITORS $editor_name" devi@76: done devi@76: [ -n "$L3_TAMPERED_EDITORS" ] && export L3_TAMPERED_EDITORS devi@76: } devi@76: devi@76: _l3_tamper_commands() devi@76: { devi@76: tty() devi@76: { devi@76: [ -n "$L3_PARENT_TTY" ] && echo $L3_PARENT_TTY || /usr/bin/tty devi@76: } devi@76: } devi@76: devi@76: _l3_unset_internal() devi@76: { devi@76: unset `set | grep '^_l3_.*()' | sed 's/()//'` devi@76: unset `set | grep '^_l3_.*=' | sed 's/=.*//'` devi@76: }