lilalo

changeset 76:0da83215839a

Существенная переработка l3bashrc

* Код полностью структурирован
* Выполняется подмена редакторов с помощью функций
* Выполняется подмена команд. Сейчас только tty
* Устанавливаются переменные окружения L3_* с информацией о сессии
* Добавлена функция l3_fix_prompt восстанавливающая приглашение
LiLaLo со служебной информацией, в том случае, если
оно было модифицировано.
Приглашение остаётся внешне таким же как и до вызова функции,
но в нём теперь скрыта служебная информация LiLaLo
* Приглашение стало цветным. Теперь оно зелёного цвета
author devi
date Mon Feb 13 19:01:19 2006 +0200 (2006-02-13)
parents 58ea78973bbb
children e56f21c44faf
files l3bashrc
line diff
     1.1 --- a/l3bashrc	Fri Feb 10 23:35:24 2006 +0200
     1.2 +++ b/l3bashrc	Mon Feb 13 19:01:19 2006 +0200
     1.3 @@ -1,39 +1,48 @@
     1.4  #!/bin/sh
     1.5  
     1.6  
     1.7 +# Environment variables set by the script:
     1.8 +#
     1.9 +#       L3_SESSION_ID   - uniq id of the LiLaLo-session
    1.10 +#       L3_PARENT_TTY   - name of tty on which script is running
    1.11 +#       L3_TTY          - current tty
    1.12 +#       PS1             - intercative shell prompt in which LiLaLo hides
    1.13 +#                         various information about the command
    1.14 +#       L3_TAMPERED_EDITORS - list of editors which are tampered with functions
    1.15 +
    1.16 +
    1.17 +# Functions with the names starting _l3_ are internal.
    1.18 +# Such functions are unset before this rc script exits
    1.19 +
    1.20 +_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'
    1.21 +
    1.22  _l3_start()
    1.23  {
    1.24 -    L3_TTY=`tty` 
    1.25 +    if _l3_is_not_running_here
    1.26 +    then
    1.27 +        _l3_start_session
    1.28 +        _l3_run_script
    1.29 +    else
    1.30 +        _l3_env
    1.31 +        _l3_init_prompt
    1.32 +         l3_fix_prompt
    1.33 +        _l3_tamper_editors
    1.34 +        _l3_tamper_commands
    1.35 +        _l3_unset_internal
    1.36 +    fi
    1.37 +}
    1.38 +
    1.39 +# ===================== STAGE 1 ============================
    1.40 +
    1.41 +_l3_is_not_running_here()
    1.42 +{
    1.43 +    export L3_TTY=`/usr/bin/tty` 
    1.44      proc_on_the_term=`w | grep "${L3_TTY##/dev/}" | awk '{print $8;}'`
    1.45      # freeBSD: 
    1.46      [ -n "$bsd" ] && \
    1.47      proc_on_the_term=`w | grep "${L3_TTY##/dev/tty}" | awk '{print $6;}'`
    1.48  
    1.49 -    if [ -n "$proc_on_the_term" ] && echo $proc_on_the_term | grep -qv script
    1.50 -    then
    1.51 -        _l3_start_session
    1.52 -        _l3_run_script
    1.53 -    else
    1.54 -        _l3_env
    1.55 -        _l3_prompt
    1.56 -    fi
    1.57 -}
    1.58 -
    1.59 -_l3_env()
    1.60 -{
    1.61 -    trap _l3_close_session 2
    1.62 -    trap _l3_close_session EXIT
    1.63 -    true
    1.64 -}
    1.65 -
    1.66 -_l3_prompt()
    1.67 -{
    1.68 -    export PS1='\[v2#\!#$?#$UID#$$#$(date +%s)#$PWD#\033[1024D\033[K\][\u@\h:\W]\$ '
    1.69 -#    export PS1='\[`
    1.70 -#    a="$?";
    1.71 -#    HIDDEN=$([ "$a" = 0 ] || echo -n ^"$a")$(echo -n _${UID}_)$(echo -n _$$_)$(date\
    1.72 -#        +"%j$(cat ${LMHOME}/lab 2>/dev/null) %H:%M:%S");
    1.73 -#    echo $HIDDEN`\033[50D\033[K\][\u@\h:\W]\$ '
    1.74 +    [ -n "$proc_on_the_term" ] && echo $proc_on_the_term | grep -qv script
    1.75  }
    1.76  
    1.77  _l3_start_session()
    1.78 @@ -45,7 +54,7 @@
    1.79      uname -a | grep -qi bsd && bsd=yes
    1.80      parent=`cat /proc/$PPID/cmdline 2> /dev/null`
    1.81      system=`uname -rs`
    1.82 -    login_from=`who | grep "${tty##/dev/}" | awk '{print $6;}' | tr -d '()'`
    1.83 +    login_from=`who | grep "${L3_TTY##/dev/}" | awk '{print $6;}' | tr -d '()'`
    1.84      #[ -n "$bsd" ] && login_from="" #FIXME!
    1.85      start_time=`date +%s`
    1.86      hostname=`hostname -f 2> /dev/null`
    1.87 @@ -58,12 +67,13 @@
    1.88      <user>$USER</user>
    1.89      <uid>$UID</uid>
    1.90      <login_from>$login_from</login_from>
    1.91 -    <tty>$tty</tty>
    1.92 +    <tty>$L3_TTY</tty>
    1.93      <system>$system</system>
    1.94      <parent>$parent</parent>
    1.95      <ppid>$PPID</ppid>
    1.96      <pid>$$</pid>
    1.97      <start_time>$start_time</start_time>
    1.98 +    <lang>$LANG</lang>
    1.99  </session>
   1.100  INFO
   1.101  
   1.102 @@ -75,10 +85,20 @@
   1.103      uname -a | grep -qi bsd && bsd=yes
   1.104      flush="-f"                          #linux
   1.105      [ -n "$bsd" ] && flush="-t 0"       #freebsd
   1.106 +    export L3_PARENT_TTY=`/usr/bin/tty`
   1.107      exec script $flush -q $L3_HOME/${L3_SESSION_ID}.script
   1.108  }
   1.109  
   1.110 -_l3_close_session()
   1.111 +# ===================== STAGE 2 ============================
   1.112 +
   1.113 +_l3_env()
   1.114 +{
   1.115 +    trap l3_close_session 2
   1.116 +    trap l3_close_session EXIT
   1.117 +    true
   1.118 +}
   1.119 +
   1.120 +l3_close_session()
   1.121  {
   1.122      ( 
   1.123          echo '<history>'
   1.124 @@ -87,3 +107,60 @@
   1.125      ) >> $L3_HOME/$L3_SESSION_ID.info
   1.126  }
   1.127  
   1.128 +_l3_init_prompt()
   1.129 +{
   1.130 +    PS1='[\u@\h:\W]\$ '
   1.131 +    PS1='\[\033[0;32m\]'$PS1'\[\033[0m\]'
   1.132 +    export PS1
   1.133 +}
   1.134 +
   1.135 +l3_fix_prompt()
   1.136 +{
   1.137 +    export PS1='\[v2#\!#$?#$UID#$$#$(/bin/date +%s)#$PWD#\033[1024D\033[K\]'$PS1
   1.138 +}
   1.139 +
   1.140 +_l3_tamper_editors()
   1.141 +{
   1.142 +    for editor_file in $_l3_editors_to_tamper
   1.143 +    do
   1.144 +        [ -x $editor_file ] || continue
   1.145 +        editor_name=${editor_file##*/}
   1.146 +        eval "
   1.147 +        $editor_name() { 
   1.148 +            if [ -d \"\$1\" ] 
   1.149 +            then
   1.150 +                $editor_file \"\$1\"
   1.151 +                return \$?
   1.152 +            else
   1.153 +                TIME=\"\`date +%s\`\"
   1.154 +                DIR=\"\"
   1.155 +                [ \"\${1#/}\" = \"\$1\" ] && DIR=\"\$PWD/\"
   1.156 +                DIFFNAME=\"\$PPID_\${TIME}_\`echo \$DIR\$1| /bin/sed s@_@__@ | /bin/sed 's@/@_@g'\`.diff\"
   1.157 +                old_file=\"/tmp/l3-saved-\$\$.\$RANDOM.\$RANDOM\"
   1.158 +                /bin/cp -- \"\$1\" \"\$old_file\" 2> /dev/null
   1.159 +                $editor_file \"\$@\" || ERR=\$?
   1.160 +                [ -e \"\$old_file\" ] && diff \"\$old_file\" \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null \
   1.161 +                                  || diff /dev/null \"\$1\"  > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null
   1.162 +                /bin/rm \"\$old_file\" 2> /dev/null
   1.163 +                return \$ERR
   1.164 +            fi
   1.165 +        }
   1.166 +        "
   1.167 +        L3_TAMPERED_EDITORS="$L3_TAMPERED_EDITORS $editor_name"
   1.168 +    done
   1.169 +    [ -n "$L3_TAMPERED_EDITORS" ] && export L3_TAMPERED_EDITORS
   1.170 +}
   1.171 +
   1.172 +_l3_tamper_commands()
   1.173 +{
   1.174 +    tty()
   1.175 +    {
   1.176 +        [ -n "$L3_PARENT_TTY" ] && echo $L3_PARENT_TTY || /usr/bin/tty
   1.177 +    }
   1.178 +}
   1.179 +
   1.180 +_l3_unset_internal()
   1.181 +{
   1.182 +    unset `set | grep '^_l3_.*()' | sed 's/()//'`
   1.183 +    unset `set | grep '^_l3_.*=' | sed 's/=.*//'`
   1.184 +}