lilalo
view l3bashrc @ 119:71bd999bcb04
Исправлено несколько багов:
* выполняется корректная привязка diff'ов
* правильно запоминается raw_start и проч raw_*
* временно отключен вывод признака нажатия ctrl-c (он ставился неверно)
* в приглашение добавлен случайный nonce (для правильной отработки tab)
* выполняется корректная привязка diff'ов
* правильно запоминается raw_start и проч raw_*
* временно отключен вывод признака нажатия ctrl-c (он ставился неверно)
* в приглашение добавлен случайный nonce (для правильной отработки tab)
| author | igor | 
|---|---|
| date | Thu Mar 13 12:19:42 2008 +0200 (2008-03-13) | 
| parents | 9e6359b7ad55 | 
| children | 42d9af3c851c | 
 line source
     1 #!/bin/sh
     3 # (c) Igor Chubin, igor@chub.in, 2004-2006
     5 # Environment variables set by the script:
     6 #
     7 #       L3_SESSION_ID   - uniq id of the LiLaLo-session
     8 #       L3_PARENT_TTY   - name of tty on which script is running
     9 #       PS1             - intercative shell prompt in which LiLaLo hides
    10 #                         various information about the command
    11 #       L3_TAMPERED_EDITORS - list of editors which are tampered with functions
    14 # Functions with the names starting _l3_ are internal.
    15 # Such functions are unset before this rc script exits
    17 _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'
    19 _l3_start()
    20 {
    21     echo $- | grep -q i || return 0
    22     if _l3_is_running_here
    23     then
    24         _l3_env
    25         _l3_init_prompt
    26          l3_fix_prompt
    27         _l3_tamper_editors
    28         _l3_tamper_commands
    29         _l3_unset_internal
    30     else
    31         _l3_start_session
    32         _l3_run_script
    33     fi
    34 }
    36 # ===================== STAGE 1 ============================
    38 _l3_is_running_here()
    39 {
    40 	ps waux | awk '{print $2" "$11 }' | grep -q ^$PPID" "script 
    41 	return $?
    43 # Check if ^^^^ run on Linux
    44 # and del  vvvv this if it does
    46     export L3_TTY=`/usr/bin/tty` 
    47     uname -a | grep -qi bsd && bsd=yes
    48     proc_on_the_term=`w | grep "${L3_TTY##/dev/}" | awk '{print $8;}'`
    49     # freeBSD: 
    50     [ -n "$bsd" ] && \
    51     proc_on_the_term=`w | grep "${L3_TTY##/dev/tty}" | awk '{print $6;}'`
    53     [ -n "$proc_on_the_term" ] && echo $proc_on_the_term | grep -q script
    54 }
    56 _l3_start_session()
    57 {
    58     export L3_SESSION_ID=${RANDOM}${RANDOM}${RANDOM}${RANDOM}-`date +%s`
    59     export L3_HOME=~/.lilalo/
    60     mkdir -p $L3_HOME
    62     tty=`/usr/bin/tty`
    63     uname -a | grep -qi bsd && bsd=yes
    64     parent=`cat /proc/$PPID/cmdline 2> /dev/null`
    65     [ -z "$parent" ] && parent="`ps waux | awk '{if ($2 == '$PPID') print $11; }'`"
    66     system=`uname -rs`
    67     login_from=`who | grep "${tty##/dev/}" | awk '{print $6;}' | tr -d '()'`
    68     #[ -n "$bsd" ] && login_from="" #FIXME!
    69     start_time=`date +%s`
    70     hostname=`hostname -f 2> /dev/null`
    71     [ -n "$bsd" ] && hostname=`hostname`
    73     cat <<INFO > $L3_HOME/$L3_SESSION_ID.info
    74 <session>
    75     <local_session_id>$L3_SESSION_ID</local_session_id>
    76     <hostname>$hostname</hostname>
    77     <user>$USER</user>
    78     <uid>$UID</uid>
    79     <login_from>$login_from</login_from>
    80     <tty>$tty</tty>
    81     <system>$system</system>
    82     <parent>$parent</parent>
    83     <ppid>$PPID</ppid>
    84     <pid>$$</pid>
    85     <start_time>$start_time</start_time>
    86     <lang>$LANG</lang>
    87 </session>
    88 INFO
    90     unset parent system login_from start_time hostname tty
    91 }
    93 _l3_run_script()
    94 {
    95     uname -a | grep -qi bsd && bsd=yes
    96     flush="-f"                          #linux
    97     [ -n "$bsd" ] && flush="-t 0"       #freebsd
    98     export L3_PARENT_TTY=`/usr/bin/tty`
    99     exec script $flush -q $L3_HOME/${L3_SESSION_ID}.script
   100 }
   102 # ===================== STAGE 2 ============================
   104 _l3_env()
   105 {
   106     trap l3_close_session 2
   107     trap l3_close_session EXIT
   108     true
   109 }
   111 l3_close_session()
   112 {
   113     ( 
   114         echo '<history>'
   115         history | sed 's/&/\&/; s/</\</g; s/>/\>/g'
   116         echo '</history>'
   117     ) >> $L3_HOME/$L3_SESSION_ID.info
   118 }
   120 _l3_init_prompt()
   121 {
   122     PS1='[\u@\h:\W]\$ '
   123     [ $UID = 0 ] \
   124         && PS1='\[\033[0;31m\]'$PS1'\[\033[0m\]' \
   125         || PS1='\[\033[0;32m\]'$PS1'\[\033[0m\]' \
   126     export PS1
   127 }
   129 l3_fix_prompt()
   130 {
   131 ##    export PS1='\[v2#\!#$?#$UID#$$#$(/bin/date +%s)$(l3_save_last_line)#$PWD#\033[1024D\033[K\]'$PS1
   132     export PS1='\[$($L3_HOME/l3prompt "v3#\!#$?#$UID#$$#$(/bin/date +%s)#$PWD#$RANDOM#")$(l3_save_last_line >& /dev/null)\]'$PS1
   133 }
   135 _l3_tamper_editors()
   136 {
   137     for editor_file in $_l3_editors_to_tamper
   138     do
   139         [ -x $editor_file ] || continue
   140         editor_name=${editor_file##*/}
   141         eval "
   142         $editor_name() { 
   143             if [ -d \"\$1\" ] 
   144             then
   145                 $editor_file \"\$1\"
   146                 return \$?
   147             else
   148                 TIME=\"\`date +%s\`\"
   149                 DIR=\"\"
   150                 [ \"\${1#/}\" = \"\$1\" ] && DIR=\"\$PWD/\"
   151                 DIFFNAME=\"\${L3_SESSION_ID}_\${TIME}\`echo \$DIR\$1| sed s@_@__@ | sed 's@/@_@g'\`.diff\"
   152                 old_file=\"/tmp/l3-saved-\$\$.\$RANDOM.\$RANDOM\"
   153                 /bin/cp -- \"\$1\" \"\$old_file\" 2> /dev/null
   154                 $editor_file \"\$@\" || ERR=\$?
   155                 if [ -e \"\$old_file\" ]
   156                 then
   157                 diff -u \"\$old_file\" \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null
   158                 else
   159                 diff -u /dev/null \"\$1\"  > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null
   160                 fi
   161                 /bin/rm \"\$old_file\" 2> /dev/null
   162                 return \$ERR
   163             fi
   164         }
   165         "
   166         L3_TAMPERED_EDITORS="$L3_TAMPERED_EDITORS $editor_name"
   167     done
   168     [ -n "$L3_TAMPERED_EDITORS" ] && export L3_TAMPERED_EDITORS
   169 }
   171 _l3_tamper_commands()
   172 {
   173     tty()
   174     {
   175         [ -n "$L3_PARENT_TTY" ] && echo $L3_PARENT_TTY || /usr/bin/tty
   176     }
   177 }
   179 _l3_unset_internal()
   180 {
   181     unset `set | grep '^_l3_.*()' | sed 's/()//'`
   182     unset `set | grep '^_l3_.*=' | sed 's/=.*//'`
   183 }
   185 l3shot()
   186 {
   187     if [ -x "`which xwd`" ]
   188     then
   189         _l3_home=${L3_HOME:-~/.lilalo}
   190         shot_name="${L3_SESSION_ID}_`date +%s`".xwd
   191         echo -n Choose window to be shoot ... >&2
   192         [ -d ${_l3_home} ] || mkdir -p ${_l3_home}
   193         xwd -out "$_l3_home/$shot_name" \
   194         && echo Screenshot is written to ${_l3_home}/${shot_name} \
   195         && curl -s -F photo=@$_l3_home/$shot_name http://`l3-config backend_address`/l3-upload
   196     else
   197         {
   198             echo
   199             echo "Can't make screenshot :("
   200             echo 
   201             echo I must use program xwd to make screenshot, 
   202             echo but it seems not to be installed
   203             echo Try to find the program in the \"xbase-clients\" package
   204             echo
   205         }   >&2 
   206     fi
   207 }
   209 l3upload()
   210 {
   211     if [ $# -lt 1 ]
   212     then
   213         echo Usage:
   214         echo 
   215         echo    l3upload "<filename>"
   216         echo
   217         echo "<filename>" - name of the file to upload
   218         return 1
   219     else
   220         source=$1 
   221         target="${L3_SESSION_ID}_`date +%s`"_"$(echo $source|sed s@.*/@@)"
   222         if echo $source | grep -q http://
   223         then
   224             curl -s "$source" > /tmp/$target 
   225         else 
   226             [ -r "$source" ] || { echo "l3upload: Can't open $source for reading" > /dev/stderr; return 1; }
   227             cp $source /tmp/$target
   228         fi
   229         echo Uploaded file name is ${target}
   230         curl -s -F photo=@/tmp/$target http://`l3-config backend_address`/l3-upload && rm -f /tmp/$target
   231     fi
   232 }
   234 # Append lines from "$@" files to the end of the shell history
   235 hist_append ()
   236 {
   237     eval $(cat "$@" | sed 's/"/\\\\"/g' | while read line; do echo history -s \"$line\"\;; done);
   238 }
   240 l3_save_last_line ()
   241 {
   242     (
   243     echo '<cline>'
   244     history 1 | sed 's/&/&/; s/</\</g; s/>/\>/g'
   245     echo '</cline>'
   246     ) >> $L3_HOME/$L3_SESSION_ID.info
   247 }
   249 l3 ()
   250 {
   251     case "$1" in
   252         on)
   253             echo switching on
   254         ;;
   255         off)
   256             echo switcing off
   257         ;;
   258         cd)
   259             echo l3cd="$2" > ~/.l3rc
   260         ;;
   261         pwd)
   262             grep ^l3cd= ~/.l3rc | sed s/[^=]*=//
   263         ;;
   264         *) 
   265             cat <<EOF
   266 l3 [command]:
   268     cd new_context   - change current lilalo context
   269     pwd              - show current lilalo context
   270     on               - switch writing on (NOT IMPLEMENTED YET)
   271     off              - switch writing off (NOT IMPLEMENTED YET)
   273 EOF
   274         ;;
   275     esac
   276 }
   278 l3cd()
   279 {
   280     l3 cd "$@"
   281 }
   283 l3pwd()
   284 {
   285     l3 pwd
   286 }
