lilalo

view l3bashrc @ 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 a10db759e587
children e56f21c44faf
line source
1 #!/bin/sh
4 # Environment variables set by the script:
5 #
6 # L3_SESSION_ID - uniq id of the LiLaLo-session
7 # L3_PARENT_TTY - name of tty on which script is running
8 # L3_TTY - current tty
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 if _l3_is_not_running_here
22 then
23 _l3_start_session
24 _l3_run_script
25 else
26 _l3_env
27 _l3_init_prompt
28 l3_fix_prompt
29 _l3_tamper_editors
30 _l3_tamper_commands
31 _l3_unset_internal
32 fi
33 }
35 # ===================== STAGE 1 ============================
37 _l3_is_not_running_here()
38 {
39 export L3_TTY=`/usr/bin/tty`
40 proc_on_the_term=`w | grep "${L3_TTY##/dev/}" | awk '{print $8;}'`
41 # freeBSD:
42 [ -n "$bsd" ] && \
43 proc_on_the_term=`w | grep "${L3_TTY##/dev/tty}" | awk '{print $6;}'`
45 [ -n "$proc_on_the_term" ] && echo $proc_on_the_term | grep -qv script
46 }
48 _l3_start_session()
49 {
50 export L3_SESSION_ID=${L3_TTY##*/}-$$
51 export L3_HOME=~/.lilalo/
52 mkdir -p $L3_HOME
54 uname -a | grep -qi bsd && bsd=yes
55 parent=`cat /proc/$PPID/cmdline 2> /dev/null`
56 system=`uname -rs`
57 login_from=`who | grep "${L3_TTY##/dev/}" | awk '{print $6;}' | tr -d '()'`
58 #[ -n "$bsd" ] && login_from="" #FIXME!
59 start_time=`date +%s`
60 hostname=`hostname -f 2> /dev/null`
61 [ -n "$bsd" ] && hostname=`hostname`
63 cat <<INFO > $L3_HOME/$L3_SESSION_ID.info
64 <session>
65 <local_session_id>$L3_SESSION_ID</local_session_id>
66 <hostname>$hostname</hostname>
67 <user>$USER</user>
68 <uid>$UID</uid>
69 <login_from>$login_from</login_from>
70 <tty>$L3_TTY</tty>
71 <system>$system</system>
72 <parent>$parent</parent>
73 <ppid>$PPID</ppid>
74 <pid>$$</pid>
75 <start_time>$start_time</start_time>
76 <lang>$LANG</lang>
77 </session>
78 INFO
80 unset parent system login_from start_time hostname
81 }
83 _l3_run_script()
84 {
85 uname -a | grep -qi bsd && bsd=yes
86 flush="-f" #linux
87 [ -n "$bsd" ] && flush="-t 0" #freebsd
88 export L3_PARENT_TTY=`/usr/bin/tty`
89 exec script $flush -q $L3_HOME/${L3_SESSION_ID}.script
90 }
92 # ===================== STAGE 2 ============================
94 _l3_env()
95 {
96 trap l3_close_session 2
97 trap l3_close_session EXIT
98 true
99 }
101 l3_close_session()
102 {
103 (
104 echo '<history>'
105 history | sed 's/&/\&amp;/; s/</\&lt;/g; s/>/\&gt;/g'
106 echo '</history>'
107 ) >> $L3_HOME/$L3_SESSION_ID.info
108 }
110 _l3_init_prompt()
111 {
112 PS1='[\u@\h:\W]\$ '
113 PS1='\[\033[0;32m\]'$PS1'\[\033[0m\]'
114 export PS1
115 }
117 l3_fix_prompt()
118 {
119 export PS1='\[v2#\!#$?#$UID#$$#$(/bin/date +%s)#$PWD#\033[1024D\033[K\]'$PS1
120 }
122 _l3_tamper_editors()
123 {
124 for editor_file in $_l3_editors_to_tamper
125 do
126 [ -x $editor_file ] || continue
127 editor_name=${editor_file##*/}
128 eval "
129 $editor_name() {
130 if [ -d \"\$1\" ]
131 then
132 $editor_file \"\$1\"
133 return \$?
134 else
135 TIME=\"\`date +%s\`\"
136 DIR=\"\"
137 [ \"\${1#/}\" = \"\$1\" ] && DIR=\"\$PWD/\"
138 DIFFNAME=\"\$PPID_\${TIME}_\`echo \$DIR\$1| /bin/sed s@_@__@ | /bin/sed 's@/@_@g'\`.diff\"
139 old_file=\"/tmp/l3-saved-\$\$.\$RANDOM.\$RANDOM\"
140 /bin/cp -- \"\$1\" \"\$old_file\" 2> /dev/null
141 $editor_file \"\$@\" || ERR=\$?
142 [ -e \"\$old_file\" ] && diff \"\$old_file\" \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null \
143 || diff /dev/null \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null
144 /bin/rm \"\$old_file\" 2> /dev/null
145 return \$ERR
146 fi
147 }
148 "
149 L3_TAMPERED_EDITORS="$L3_TAMPERED_EDITORS $editor_name"
150 done
151 [ -n "$L3_TAMPERED_EDITORS" ] && export L3_TAMPERED_EDITORS
152 }
154 _l3_tamper_commands()
155 {
156 tty()
157 {
158 [ -n "$L3_PARENT_TTY" ] && echo $L3_PARENT_TTY || /usr/bin/tty
159 }
160 }
162 _l3_unset_internal()
163 {
164 unset `set | grep '^_l3_.*()' | sed 's/()//'`
165 unset `set | grep '^_l3_.*=' | sed 's/=.*//'`
166 }