lilalo
annotate l3bashrc @ 76:0da83215839a
Существенная переработка l3bashrc
* Код полностью структурирован
* Выполняется подмена редакторов с помощью функций
* Выполняется подмена команд. Сейчас только tty
* Устанавливаются переменные окружения L3_* с информацией о сессии
* Добавлена функция l3_fix_prompt восстанавливающая приглашение
LiLaLo со служебной информацией, в том случае, если
оно было модифицировано.
Приглашение остаётся внешне таким же как и до вызова функции,
но в нём теперь скрыта служебная информация LiLaLo
* Приглашение стало цветным. Теперь оно зелёного цвета
* Код полностью структурирован
* Выполняется подмена редакторов с помощью функций
* Выполняется подмена команд. Сейчас только tty
* Устанавливаются переменные окружения L3_* с информацией о сессии
* Добавлена функция l3_fix_prompt восстанавливающая приглашение
LiLaLo со служебной информацией, в том случае, если
оно было модифицировано.
Приглашение остаётся внешне таким же как и до вызова функции,
но в нём теперь скрыта служебная информация LiLaLo
* Приглашение стало цветным. Теперь оно зелёного цвета
author | devi |
---|---|
date | Mon Feb 13 19:01:19 2006 +0200 (2006-02-13) |
parents | a10db759e587 |
children | e56f21c44faf |
rev | line source |
---|---|
devi@70 | 1 #!/bin/sh |
devi@70 | 2 |
devi@70 | 3 |
devi@76 | 4 # Environment variables set by the script: |
devi@76 | 5 # |
devi@76 | 6 # L3_SESSION_ID - uniq id of the LiLaLo-session |
devi@76 | 7 # L3_PARENT_TTY - name of tty on which script is running |
devi@76 | 8 # L3_TTY - current tty |
devi@76 | 9 # PS1 - intercative shell prompt in which LiLaLo hides |
devi@76 | 10 # various information about the command |
devi@76 | 11 # L3_TAMPERED_EDITORS - list of editors which are tampered with functions |
devi@76 | 12 |
devi@76 | 13 |
devi@76 | 14 # Functions with the names starting _l3_ are internal. |
devi@76 | 15 # Such functions are unset before this rc script exits |
devi@76 | 16 |
devi@76 | 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' |
devi@76 | 18 |
devi@70 | 19 _l3_start() |
devi@70 | 20 { |
devi@76 | 21 if _l3_is_not_running_here |
devi@76 | 22 then |
devi@76 | 23 _l3_start_session |
devi@76 | 24 _l3_run_script |
devi@76 | 25 else |
devi@76 | 26 _l3_env |
devi@76 | 27 _l3_init_prompt |
devi@76 | 28 l3_fix_prompt |
devi@76 | 29 _l3_tamper_editors |
devi@76 | 30 _l3_tamper_commands |
devi@76 | 31 _l3_unset_internal |
devi@76 | 32 fi |
devi@76 | 33 } |
devi@76 | 34 |
devi@76 | 35 # ===================== STAGE 1 ============================ |
devi@76 | 36 |
devi@76 | 37 _l3_is_not_running_here() |
devi@76 | 38 { |
devi@76 | 39 export L3_TTY=`/usr/bin/tty` |
devi@70 | 40 proc_on_the_term=`w | grep "${L3_TTY##/dev/}" | awk '{print $8;}'` |
devi@70 | 41 # freeBSD: |
devi@70 | 42 [ -n "$bsd" ] && \ |
devi@70 | 43 proc_on_the_term=`w | grep "${L3_TTY##/dev/tty}" | awk '{print $6;}'` |
devi@70 | 44 |
devi@76 | 45 [ -n "$proc_on_the_term" ] && echo $proc_on_the_term | grep -qv script |
devi@70 | 46 } |
devi@70 | 47 |
devi@70 | 48 _l3_start_session() |
devi@70 | 49 { |
devi@71 | 50 export L3_SESSION_ID=${L3_TTY##*/}-$$ |
devi@71 | 51 export L3_HOME=~/.lilalo/ |
devi@70 | 52 mkdir -p $L3_HOME |
devi@70 | 53 |
devi@70 | 54 uname -a | grep -qi bsd && bsd=yes |
devi@70 | 55 parent=`cat /proc/$PPID/cmdline 2> /dev/null` |
devi@70 | 56 system=`uname -rs` |
devi@76 | 57 login_from=`who | grep "${L3_TTY##/dev/}" | awk '{print $6;}' | tr -d '()'` |
devi@70 | 58 #[ -n "$bsd" ] && login_from="" #FIXME! |
devi@70 | 59 start_time=`date +%s` |
devi@70 | 60 hostname=`hostname -f 2> /dev/null` |
devi@70 | 61 [ -n "$bsd" ] && hostname=`hostname` |
devi@70 | 62 |
devi@70 | 63 cat <<INFO > $L3_HOME/$L3_SESSION_ID.info |
devi@70 | 64 <session> |
devi@70 | 65 <local_session_id>$L3_SESSION_ID</local_session_id> |
devi@70 | 66 <hostname>$hostname</hostname> |
devi@70 | 67 <user>$USER</user> |
devi@70 | 68 <uid>$UID</uid> |
devi@70 | 69 <login_from>$login_from</login_from> |
devi@76 | 70 <tty>$L3_TTY</tty> |
devi@70 | 71 <system>$system</system> |
devi@70 | 72 <parent>$parent</parent> |
devi@70 | 73 <ppid>$PPID</ppid> |
devi@70 | 74 <pid>$$</pid> |
devi@70 | 75 <start_time>$start_time</start_time> |
devi@76 | 76 <lang>$LANG</lang> |
devi@70 | 77 </session> |
devi@70 | 78 INFO |
devi@70 | 79 |
devi@70 | 80 unset parent system login_from start_time hostname |
devi@70 | 81 } |
devi@70 | 82 |
devi@70 | 83 _l3_run_script() |
devi@70 | 84 { |
devi@70 | 85 uname -a | grep -qi bsd && bsd=yes |
devi@70 | 86 flush="-f" #linux |
devi@70 | 87 [ -n "$bsd" ] && flush="-t 0" #freebsd |
devi@76 | 88 export L3_PARENT_TTY=`/usr/bin/tty` |
devi@70 | 89 exec script $flush -q $L3_HOME/${L3_SESSION_ID}.script |
devi@70 | 90 } |
devi@70 | 91 |
devi@76 | 92 # ===================== STAGE 2 ============================ |
devi@76 | 93 |
devi@76 | 94 _l3_env() |
devi@76 | 95 { |
devi@76 | 96 trap l3_close_session 2 |
devi@76 | 97 trap l3_close_session EXIT |
devi@76 | 98 true |
devi@76 | 99 } |
devi@76 | 100 |
devi@76 | 101 l3_close_session() |
devi@70 | 102 { |
devi@71 | 103 ( |
devi@71 | 104 echo '<history>' |
devi@71 | 105 history | sed 's/&/\&/; s/</\</g; s/>/\>/g' |
devi@71 | 106 echo '</history>' |
devi@71 | 107 ) >> $L3_HOME/$L3_SESSION_ID.info |
devi@70 | 108 } |
devi@70 | 109 |
devi@76 | 110 _l3_init_prompt() |
devi@76 | 111 { |
devi@76 | 112 PS1='[\u@\h:\W]\$ ' |
devi@76 | 113 PS1='\[\033[0;32m\]'$PS1'\[\033[0m\]' |
devi@76 | 114 export PS1 |
devi@76 | 115 } |
devi@76 | 116 |
devi@76 | 117 l3_fix_prompt() |
devi@76 | 118 { |
devi@76 | 119 export PS1='\[v2#\!#$?#$UID#$$#$(/bin/date +%s)#$PWD#\033[1024D\033[K\]'$PS1 |
devi@76 | 120 } |
devi@76 | 121 |
devi@76 | 122 _l3_tamper_editors() |
devi@76 | 123 { |
devi@76 | 124 for editor_file in $_l3_editors_to_tamper |
devi@76 | 125 do |
devi@76 | 126 [ -x $editor_file ] || continue |
devi@76 | 127 editor_name=${editor_file##*/} |
devi@76 | 128 eval " |
devi@76 | 129 $editor_name() { |
devi@76 | 130 if [ -d \"\$1\" ] |
devi@76 | 131 then |
devi@76 | 132 $editor_file \"\$1\" |
devi@76 | 133 return \$? |
devi@76 | 134 else |
devi@76 | 135 TIME=\"\`date +%s\`\" |
devi@76 | 136 DIR=\"\" |
devi@76 | 137 [ \"\${1#/}\" = \"\$1\" ] && DIR=\"\$PWD/\" |
devi@76 | 138 DIFFNAME=\"\$PPID_\${TIME}_\`echo \$DIR\$1| /bin/sed s@_@__@ | /bin/sed 's@/@_@g'\`.diff\" |
devi@76 | 139 old_file=\"/tmp/l3-saved-\$\$.\$RANDOM.\$RANDOM\" |
devi@76 | 140 /bin/cp -- \"\$1\" \"\$old_file\" 2> /dev/null |
devi@76 | 141 $editor_file \"\$@\" || ERR=\$? |
devi@76 | 142 [ -e \"\$old_file\" ] && diff \"\$old_file\" \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null \ |
devi@76 | 143 || diff /dev/null \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null |
devi@76 | 144 /bin/rm \"\$old_file\" 2> /dev/null |
devi@76 | 145 return \$ERR |
devi@76 | 146 fi |
devi@76 | 147 } |
devi@76 | 148 " |
devi@76 | 149 L3_TAMPERED_EDITORS="$L3_TAMPERED_EDITORS $editor_name" |
devi@76 | 150 done |
devi@76 | 151 [ -n "$L3_TAMPERED_EDITORS" ] && export L3_TAMPERED_EDITORS |
devi@76 | 152 } |
devi@76 | 153 |
devi@76 | 154 _l3_tamper_commands() |
devi@76 | 155 { |
devi@76 | 156 tty() |
devi@76 | 157 { |
devi@76 | 158 [ -n "$L3_PARENT_TTY" ] && echo $L3_PARENT_TTY || /usr/bin/tty |
devi@76 | 159 } |
devi@76 | 160 } |
devi@76 | 161 |
devi@76 | 162 _l3_unset_internal() |
devi@76 | 163 { |
devi@76 | 164 unset `set | grep '^_l3_.*()' | sed 's/()//'` |
devi@76 | 165 unset `set | grep '^_l3_.*=' | sed 's/=.*//'` |
devi@76 | 166 } |