# HG changeset patch
# User devi
# Date 1139850079 -7200
# Node ID 0da83215839aa2af522045fef0b993c76f32b10e
# Parent  58ea78973bbb19acec563ad85d02f27c8f9b62e2
Существенная переработка l3bashrc

 * Код полностью структурирован
 * Выполняется подмена редакторов с помощью функций
 * Выполняется подмена команд. Сейчас только tty
 * Устанавливаются переменные окружения L3_* с информацией о сессии
 * Добавлена функция l3_fix_prompt восстанавливающая приглашение
   LiLaLo со служебной информацией, в том случае, если
   оно было модифицировано.
   Приглашение остаётся внешне таким же как и до вызова функции,
   но в нём теперь скрыта служебная информация LiLaLo
 * Приглашение стало цветным. Теперь оно зелёного цвета

diff -r 58ea78973bbb -r 0da83215839a l3bashrc
--- a/l3bashrc	Fri Feb 10 23:35:24 2006 +0200
+++ b/l3bashrc	Mon Feb 13 19:01:19 2006 +0200
@@ -1,39 +1,48 @@
 #!/bin/sh
 
 
+# Environment variables set by the script:
+#
+#       L3_SESSION_ID   - uniq id of the LiLaLo-session
+#       L3_PARENT_TTY   - name of tty on which script is running
+#       L3_TTY          - current tty
+#       PS1             - intercative shell prompt in which LiLaLo hides
+#                         various information about the command
+#       L3_TAMPERED_EDITORS - list of editors which are tampered with functions
+
+
+# Functions with the names starting _l3_ are internal.
+# Such functions are unset before this rc script exits
+
+_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'
+
 _l3_start()
 {
-    L3_TTY=`tty` 
+    if _l3_is_not_running_here
+    then
+        _l3_start_session
+        _l3_run_script
+    else
+        _l3_env
+        _l3_init_prompt
+         l3_fix_prompt
+        _l3_tamper_editors
+        _l3_tamper_commands
+        _l3_unset_internal
+    fi
+}
+
+# ===================== STAGE 1 ============================
+
+_l3_is_not_running_here()
+{
+    export L3_TTY=`/usr/bin/tty` 
     proc_on_the_term=`w | grep "${L3_TTY##/dev/}" | awk '{print $8;}'`
     # freeBSD: 
     [ -n "$bsd" ] && \
     proc_on_the_term=`w | grep "${L3_TTY##/dev/tty}" | awk '{print $6;}'`
 
-    if [ -n "$proc_on_the_term" ] && echo $proc_on_the_term | grep -qv script
-    then
-        _l3_start_session
-        _l3_run_script
-    else
-        _l3_env
-        _l3_prompt
-    fi
-}
-
-_l3_env()
-{
-    trap _l3_close_session 2
-    trap _l3_close_session EXIT
-    true
-}
-
-_l3_prompt()
-{
-    export PS1='\[v2#\!#$?#$UID#$$#$(date +%s)#$PWD#\033[1024D\033[K\][\u@\h:\W]\$ '
-#    export PS1='\[`
-#    a="$?";
-#    HIDDEN=$([ "$a" = 0 ] || echo -n ^"$a")$(echo -n _${UID}_)$(echo -n _$$_)$(date\
-#        +"%j$(cat ${LMHOME}/lab 2>/dev/null) %H:%M:%S");
-#    echo $HIDDEN`\033[50D\033[K\][\u@\h:\W]\$ '
+    [ -n "$proc_on_the_term" ] && echo $proc_on_the_term | grep -qv script
 }
 
 _l3_start_session()
@@ -45,7 +54,7 @@
     uname -a | grep -qi bsd && bsd=yes
     parent=`cat /proc/$PPID/cmdline 2> /dev/null`
     system=`uname -rs`
-    login_from=`who | grep "${tty##/dev/}" | awk '{print $6;}' | tr -d '()'`
+    login_from=`who | grep "${L3_TTY##/dev/}" | awk '{print $6;}' | tr -d '()'`
     #[ -n "$bsd" ] && login_from="" #FIXME!
     start_time=`date +%s`
     hostname=`hostname -f 2> /dev/null`
@@ -58,12 +67,13 @@
     <user>$USER</user>
     <uid>$UID</uid>
     <login_from>$login_from</login_from>
-    <tty>$tty</tty>
+    <tty>$L3_TTY</tty>
     <system>$system</system>
     <parent>$parent</parent>
     <ppid>$PPID</ppid>
     <pid>$$</pid>
     <start_time>$start_time</start_time>
+    <lang>$LANG</lang>
 </session>
 INFO
 
@@ -75,10 +85,20 @@
     uname -a | grep -qi bsd && bsd=yes
     flush="-f"                          #linux
     [ -n "$bsd" ] && flush="-t 0"       #freebsd
+    export L3_PARENT_TTY=`/usr/bin/tty`
     exec script $flush -q $L3_HOME/${L3_SESSION_ID}.script
 }
 
-_l3_close_session()
+# ===================== STAGE 2 ============================
+
+_l3_env()
+{
+    trap l3_close_session 2
+    trap l3_close_session EXIT
+    true
+}
+
+l3_close_session()
 {
     ( 
         echo '<history>'
@@ -87,3 +107,60 @@
     ) >> $L3_HOME/$L3_SESSION_ID.info
 }
 
+_l3_init_prompt()
+{
+    PS1='[\u@\h:\W]\$ '
+    PS1='\[\033[0;32m\]'$PS1'\[\033[0m\]'
+    export PS1
+}
+
+l3_fix_prompt()
+{
+    export PS1='\[v2#\!#$?#$UID#$$#$(/bin/date +%s)#$PWD#\033[1024D\033[K\]'$PS1
+}
+
+_l3_tamper_editors()
+{
+    for editor_file in $_l3_editors_to_tamper
+    do
+        [ -x $editor_file ] || continue
+        editor_name=${editor_file##*/}
+        eval "
+        $editor_name() { 
+            if [ -d \"\$1\" ] 
+            then
+                $editor_file \"\$1\"
+                return \$?
+            else
+                TIME=\"\`date +%s\`\"
+                DIR=\"\"
+                [ \"\${1#/}\" = \"\$1\" ] && DIR=\"\$PWD/\"
+                DIFFNAME=\"\$PPID_\${TIME}_\`echo \$DIR\$1| /bin/sed s@_@__@ | /bin/sed 's@/@_@g'\`.diff\"
+                old_file=\"/tmp/l3-saved-\$\$.\$RANDOM.\$RANDOM\"
+                /bin/cp -- \"\$1\" \"\$old_file\" 2> /dev/null
+                $editor_file \"\$@\" || ERR=\$?
+                [ -e \"\$old_file\" ] && diff \"\$old_file\" \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null \
+                                  || diff /dev/null \"\$1\"  > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null
+                /bin/rm \"\$old_file\" 2> /dev/null
+                return \$ERR
+            fi
+        }
+        "
+        L3_TAMPERED_EDITORS="$L3_TAMPERED_EDITORS $editor_name"
+    done
+    [ -n "$L3_TAMPERED_EDITORS" ] && export L3_TAMPERED_EDITORS
+}
+
+_l3_tamper_commands()
+{
+    tty()
+    {
+        [ -n "$L3_PARENT_TTY" ] && echo $L3_PARENT_TTY || /usr/bin/tty
+    }
+}
+
+_l3_unset_internal()
+{
+    unset `set | grep '^_l3_.*()' | sed 's/()//'`
+    unset `set | grep '^_l3_.*=' | sed 's/=.*//'`
+}