lilalo
view l3bashrc @ 156:a0daf0c3fa52
bsd fix
author | Igor Chubin <igor@chub.in> |
---|---|
date | Mon Nov 28 13:15:16 2011 +0200 (2011-11-28) |
parents | 8ee5e59f1bd3 |
children |
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 L3_SQLITE=$L3_HOME/report.sqlite
61 mkdir -p $L3_HOME
63 tty=`/usr/bin/tty`
64 uname -a | grep -qi bsd && bsd=yes
65 parent=`cat /proc/$PPID/cmdline 2> /dev/null`
66 [ -z "$parent" ] && parent="`ps waux | awk '{if ($2 == '$PPID') print $11; }'`"
67 system=`uname -rs`
68 login_from=`who | grep "${tty##/dev/}" | awk '{print $6;}' | tr -d '()'`
69 #[ -n "$bsd" ] && login_from="" #FIXME!
70 start_time=`date +%s`
71 hostname=`hostname -f 2> /dev/null`
72 [ -n "$bsd" ] && hostname=`hostname`
74 cat <<INFO | perl
75 use DBI;
76 use strict;
77 my \$db = DBI->connect("dbi:SQLite:$L3_SQLITE", "", "",
78 {RaiseError => 1, AutoCommit => 1});
80 \$db->do("CREATE TABLE IF NOT EXISTS sessions (id INTEGER PRIMARY KEY,
81 session TEXT, hostname TEXT, user TEXT, uid TEXT, login_from TEXT,
82 tty TEXT, system TEXT, parent TEXT, ppid TEXT, pid TEXT, start_time TEXT, lang TEXT)");
83 \$db->do("INSERT INTO sessions VALUES (NULL, '$L3_SESSION_ID', '$hostname', '$USER', '$UID', '$login_from',
84 '$tty', '$system', '$parent', '$PPID', '$$',
85 '$start_time', '$LANG')");
86 INFO
87 #perl $temp_l3_name #; rm $temp_l3_name; unset temp_l3_name
89 cat <<INFO > $L3_HOME/$L3_SESSION_ID.info
90 <session>
91 <local_session_id>$L3_SESSION_ID</local_session_id>
92 <hostname>$hostname</hostname>
93 <user>$USER</user>
94 <uid>$UID</uid>
95 <login_from>$login_from</login_from>
96 <tty>$tty</tty>
97 <system>$system</system>
98 <parent>$parent</parent>
99 <ppid>$PPID</ppid>
100 <pid>$$</pid>
101 <start_time>$start_time</start_time>
102 <lang>$LANG</lang>
103 </session>
104 INFO
106 unset parent system login_from start_time hostname tty
107 }
109 _l3_run_script()
110 {
111 uname -a | grep -qi bsd && bsd=yes
112 flush="-f" #linux
113 [ -n "$bsd" ] && flush="-t 0" #freebsd
114 export L3_PARENT_TTY=`/usr/bin/tty`
115 if [ -n "$bsd" ]
116 then
117 exec script $flush -q $L3_HOME/${L3_SESSION_ID}.script ${0#-}
118 else
119 exec script $flush -c ${0#-} -q $L3_HOME/${L3_SESSION_ID}.script
120 fi
121 }
123 # ===================== STAGE 2 ============================
125 _l3_env()
126 {
127 trap l3_close_session 2
128 trap l3_close_session EXIT
129 true
130 }
132 l3_close_session()
133 {
134 (
135 echo '<history>'
136 history | sed 's/&/\&/; s/</\</g; s/>/\>/g'
137 echo '</history>'
138 ) >> $L3_HOME/$L3_SESSION_ID.info
139 }
141 _l3_init_prompt()
142 {
143 PS1='[\u@\h:\W]\$ '
144 [ $UID = 0 ] \
145 && PS1='\[\033[0;31m\]'$PS1'\[\033[0m\]' \
146 || PS1='\[\033[0;32m\]'$PS1'\[\033[0m\]' \
147 export PS1
148 }
150 l3_fix_prompt()
151 {
152 export PS1='\[$($L3_HOME/l3prompt "v3#\!#$?#$UID#$$#$(/bin/date +%s)#$PWD#$RANDOM#")$(l3_save_last_line >& /dev/null)\]'$PS1
153 }
155 _l3_tamper_editors()
156 {
157 for editor_file in $_l3_editors_to_tamper
158 do
159 [ -x $editor_file ] || continue
160 editor_name=${editor_file##*/}
161 eval "
162 $editor_name() {
163 if [ -d \"\$1\" ]
164 then
165 $editor_file \"\$1\"
166 return \$?
167 else
168 TIME=\"\`date +%s\`\"
169 DIR=\"\"
170 [ \"\${1#/}\" = \"\$1\" ] && DIR=\"\$PWD/\"
171 DIFFNAME=\"\${L3_SESSION_ID}_\${TIME}\`echo \$DIR\$1| sed s@_@__@ | sed 's@/@_@g'\`.diff\"
172 old_file=\"/tmp/l3-saved-\$\$.\$RANDOM.\$RANDOM\"
173 /bin/cp -- \"\$1\" \"\$old_file\" 2> /dev/null
174 $editor_file \"\$@\" || ERR=\$?
175 if [ -e \"\$old_file\" ]
176 then
177 diff -u \"\$old_file\" \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null
178 else
179 diff -u /dev/null \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null
180 fi
181 /bin/rm \"\$old_file\" 2> /dev/null
182 return \$ERR
183 fi
184 }
185 "
186 L3_TAMPERED_EDITORS="$L3_TAMPERED_EDITORS $editor_name"
187 done
188 [ -n "$L3_TAMPERED_EDITORS" ] && export L3_TAMPERED_EDITORS
189 }
191 _l3_tamper_commands()
192 {
193 tty()
194 {
195 [ -n "$L3_PARENT_TTY" ] && echo $L3_PARENT_TTY || /usr/bin/tty
196 }
197 }
199 _l3_unset_internal()
200 {
201 unset `set | grep '^_l3_.*()' | sed 's/()//'`
202 unset `set | grep '^_l3_.*=' | sed 's/=.*//'`
203 }
205 l3shot()
206 {
207 if [ -x "`which xwd`" ]
208 then
209 _l3_home=${L3_HOME:-~/.lilalo}
210 shot_name="${L3_SESSION_ID}_`date +%s`".xwd
211 echo -n Choose window to be shoot ... >&2
212 [ -d ${_l3_home} ] || mkdir -p ${_l3_home}
213 xwd -out "$_l3_home/$shot_name" \
214 && echo Screenshot is written to ${_l3_home}/${shot_name} \
215 && curl -s -F photo=@$_l3_home/$shot_name http://`l3-config backend_address`/l3-upload
216 else
217 {
218 echo
219 echo "Can't make screenshot :("
220 echo
221 echo I must use program xwd to make screenshot,
222 echo but it seems not to be installed
223 echo Try to find the program in the \"xbase-clients\" package
224 echo
225 } >&2
226 fi
227 }
229 l3upload()
230 {
231 if [ $# -lt 1 ]
232 then
233 echo Usage:
234 echo
235 echo l3upload "<filename> [<time>]"
236 echo
237 echo "<filename>" - name of the file to upload
238 return 1
239 else
240 source=$1
241 time=`date +%s`
242 [ -n "$2" ] && time="$2"
243 target="${L3_SESSION_ID}_$time"_"$(echo $source|sed s@.*/@@)"
244 if echo $source | grep -q http://
245 then
246 curl -s "$source" > /tmp/$target
247 else
248 [ -r "$source" ] || { echo "l3upload: Can't open $source for reading" > /dev/stderr; return 1; }
249 cp $source /tmp/$target
250 fi
251 echo Uploaded file name is ${target}
252 curl -s -F photo=@/tmp/$target http://`l3-config backend_address`/l3-upload && rm -f /tmp/$target
253 fi
254 }
256 l3mass_upload()
257 {
258 for i in "$@"
259 do
260 l3upload $i `perl -e 'print (((stat("$i"))[9])."\n")'`
261 done
262 }
264 # Append lines from "$@" files to the end of the shell history
265 hist_append ()
266 {
267 eval $(cat "$@" | sed 's/"/\\\\"/g' | while read line; do echo history -s \"$line\"\;; done);
268 }
270 l3_save_last_line ()
271 {
272 (
273 echo '<cline>'
274 history 1 | sed 's/&/&/; s/</\</g; s/>/\>/g'
275 echo '</cline>'
276 ) >> $L3_HOME/$L3_SESSION_ID.info
277 }
279 l3 ()
280 {
281 case "$1" in
282 context)
283 if [ -z "$2" ]
284 then
285 echo "$L3_CONTEXT"
286 else
287 echo $2 | grep -q ^/ && L3_CONTEXT="$2" || L3_CONTEXT="$L3_CONTEXT/$2"
288 export L3_CONTEXT="`echo $L3_CONTEXT | perl -e '$_=<>; 1 while s@/[^/]*/\.\.@@; print;'`"
289 fi
290 ;;
291 cd)
292 echo l3cd="$2" > ~/.l3rc
293 ;;
294 pwd)
295 grep ^l3cd= ~/.l3rc | sed s/[^=]*=//
296 ;;
297 *)
298 l3text "$@"
299 ;;
300 esac
301 }