lilalo

view l3bashrc @ 79:44973d76ba4d

Подправлен скрипт l3bashrc.
Теперь определение запущен ли скрипт на текущем терминале или нет,
выполняется через PPID
Идентификатор сессии изменён.
Теперь это просто 8байтоное случайное число + время начала сессии.

l3scripts:
Вместо процедуры инсталляции lm-install
мы теперь просто копируем l3bashrc в lilalo и всё
author devi
date Sun Feb 19 16:02:42 2006 +0200 (2006-02-19)
parents 147fb109c012
children d28dda8ea18f
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 # PS1 - intercative shell prompt in which LiLaLo hides
9 # various information about the command
10 # L3_TAMPERED_EDITORS - list of editors which are tampered with functions
13 # Functions with the names starting _l3_ are internal.
14 # Such functions are unset before this rc script exits
16 _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'
18 _l3_start()
19 {
20 if _l3_is_running_here
21 then
22 _l3_env
23 _l3_init_prompt
24 l3_fix_prompt
25 _l3_tamper_editors
26 _l3_tamper_commands
27 _l3_unset_internal
28 else
29 _l3_start_session
30 _l3_run_script
31 fi
32 }
34 # ===================== STAGE 1 ============================
36 _l3_is_running_here()
37 {
38 ps waux | awk '{print $2" "$11 }' | grep -q ^$PPID" "script
39 return $?
41 # Check if ^^^^ run on Linux
42 # and del vvvv this if it does
44 export L3_TTY=`/usr/bin/tty`
45 uname -a | grep -qi bsd && bsd=yes
46 proc_on_the_term=`w | grep "${L3_TTY##/dev/}" | awk '{print $8;}'`
47 # freeBSD:
48 [ -n "$bsd" ] && \
49 proc_on_the_term=`w | grep "${L3_TTY##/dev/tty}" | awk '{print $6;}'`
51 [ -n "$proc_on_the_term" ] && echo $proc_on_the_term | grep -q script
52 }
54 _l3_start_session()
55 {
56 export L3_SESSION_ID=${RANDOM}${RANDOM}${RANDOM}${RANDOM}-`date +%s`
57 export L3_HOME=~/.lilalo/
58 mkdir -p $L3_HOME
60 tty=`/usr/bin/tty`
61 uname -a | grep -qi bsd && bsd=yes
62 parent=`cat /proc/$PPID/cmdline 2> /dev/null`
63 [ -z "$parent" ] && parent="`ps waux | awk '{if ($2 == '$PPID') print $11; }'`"
64 system=`uname -rs`
65 login_from=`who | grep "${tty##/dev/}" | awk '{print $6;}' | tr -d '()'`
66 #[ -n "$bsd" ] && login_from="" #FIXME!
67 start_time=`date +%s`
68 hostname=`hostname -f 2> /dev/null`
69 [ -n "$bsd" ] && hostname=`hostname`
71 cat <<INFO > $L3_HOME/$L3_SESSION_ID.info
72 <session>
73 <local_session_id>$L3_SESSION_ID</local_session_id>
74 <hostname>$hostname</hostname>
75 <user>$USER</user>
76 <uid>$UID</uid>
77 <login_from>$login_from</login_from>
78 <tty>$tty</tty>
79 <system>$system</system>
80 <parent>$parent</parent>
81 <ppid>$PPID</ppid>
82 <pid>$$</pid>
83 <start_time>$start_time</start_time>
84 <lang>$LANG</lang>
85 </session>
86 INFO
88 unset parent system login_from start_time hostname tty
89 }
91 _l3_run_script()
92 {
93 uname -a | grep -qi bsd && bsd=yes
94 flush="-f" #linux
95 [ -n "$bsd" ] && flush="-t 0" #freebsd
96 export L3_PARENT_TTY=`/usr/bin/tty`
97 exec script $flush -q $L3_HOME/${L3_SESSION_ID}.script
98 }
100 # ===================== STAGE 2 ============================
102 _l3_env()
103 {
104 trap l3_close_session 2
105 trap l3_close_session EXIT
106 true
107 }
109 l3_close_session()
110 {
111 (
112 echo '<history>'
113 history | sed 's/&/\&amp;/; s/</\&lt;/g; s/>/\&gt;/g'
114 echo '</history>'
115 ) >> $L3_HOME/$L3_SESSION_ID.info
116 }
118 _l3_init_prompt()
119 {
120 PS1='[\u@\h:\W]\$ '
121 [ $UID = 0 ] \
122 && PS1='\[\033[0;31m\]'$PS1'\[\033[0m\]' \
123 || PS1='\[\033[0;32m\]'$PS1'\[\033[0m\]' \
124 export PS1
125 }
127 l3_fix_prompt()
128 {
129 export PS1='\[v2#\!#$?#$UID#$$#$(/bin/date +%s)#$PWD#\033[1024D\033[K\]'$PS1
130 }
132 _l3_tamper_editors()
133 {
134 for editor_file in $_l3_editors_to_tamper
135 do
136 [ -x $editor_file ] || continue
137 editor_name=${editor_file##*/}
138 eval "
139 $editor_name() {
140 if [ -d \"\$1\" ]
141 then
142 $editor_file \"\$1\"
143 return \$?
144 else
145 TIME=\"\`date +%s\`\"
146 DIR=\"\"
147 [ \"\${1#/}\" = \"\$1\" ] && DIR=\"\$PWD/\"
148 DIFFNAME=\"\$PPID_\${TIME}_\`echo \$DIR\$1| /bin/sed s@_@__@ | /bin/sed 's@/@_@g'\`.diff\"
149 old_file=\"/tmp/l3-saved-\$\$.\$RANDOM.\$RANDOM\"
150 /bin/cp -- \"\$1\" \"\$old_file\" 2> /dev/null
151 $editor_file \"\$@\" || ERR=\$?
152 [ -e \"\$old_file\" ] && diff \"\$old_file\" \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null \
153 || diff /dev/null \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null
154 /bin/rm \"\$old_file\" 2> /dev/null
155 return \$ERR
156 fi
157 }
158 "
159 L3_TAMPERED_EDITORS="$L3_TAMPERED_EDITORS $editor_name"
160 done
161 [ -n "$L3_TAMPERED_EDITORS" ] && export L3_TAMPERED_EDITORS
162 }
164 _l3_tamper_commands()
165 {
166 tty()
167 {
168 [ -n "$L3_PARENT_TTY" ] && echo $L3_PARENT_TTY || /usr/bin/tty
169 }
170 }
172 _l3_unset_internal()
173 {
174 unset `set | grep '^_l3_.*()' | sed 's/()//'`
175 unset `set | grep '^_l3_.*=' | sed 's/=.*//'`
176 }
179 l3shot()
180 {
181 if [ -x "`which xwd`" ]
182 then
183 _l3_home=${L3_HOME:-~/.lilalo}
184 shot_name="${L3_SESSION_ID}_`date +%s`".xwd
185 echo -n Choose window to be shoot ... >&2
186 [ -d ${_l3_home} ] || mkdir -p ${_l3_home}
187 xwd -out "$_l3_home/$shot_name" \
188 && echo Ok\
189 && echo Shot was successful. \
190 && echo Screenshot is written to ${_l3_home}/${shot_name} \
191 && echo Screenshot will appears in the lablog.
192 else
193 {
194 echo
195 echo "Can't make screenshot :("
196 echo
197 echo I must use program xwd to make screenshot,
198 echo but it seems not to be installed
199 echo Try to find the program in the \"xbase-clients\" package
200 echo
201 } >&2
202 fi
203 }