lilalo

view l3bashrc @ 155:8ee5e59f1bd3

Локальное хранение и анализ данных с помощью SQlite

Очень много изменений, касающихся работы с sqlite
и локального использования результатов записи.
Подробнее:
README.l3text
author Igor Chubin <igor@chub.in>
date Tue Mar 16 20:05:30 2010 +0200 (2010-03-16)
parents 40d843395547
children a0daf0c3fa52
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 exec script $flush -c ${0#-} -q $L3_HOME/${L3_SESSION_ID}.script
116 }
118 # ===================== STAGE 2 ============================
120 _l3_env()
121 {
122 trap l3_close_session 2
123 trap l3_close_session EXIT
124 true
125 }
127 l3_close_session()
128 {
129 (
130 echo '<history>'
131 history | sed 's/&/\&amp;/; s/</\&lt;/g; s/>/\&gt;/g'
132 echo '</history>'
133 ) >> $L3_HOME/$L3_SESSION_ID.info
134 }
136 _l3_init_prompt()
137 {
138 PS1='[\u@\h:\W]\$ '
139 [ $UID = 0 ] \
140 && PS1='\[\033[0;31m\]'$PS1'\[\033[0m\]' \
141 || PS1='\[\033[0;32m\]'$PS1'\[\033[0m\]' \
142 export PS1
143 }
145 l3_fix_prompt()
146 {
147 export PS1='\[$($L3_HOME/l3prompt "v3#\!#$?#$UID#$$#$(/bin/date +%s)#$PWD#$RANDOM#")$(l3_save_last_line >& /dev/null)\]'$PS1
148 }
150 _l3_tamper_editors()
151 {
152 for editor_file in $_l3_editors_to_tamper
153 do
154 [ -x $editor_file ] || continue
155 editor_name=${editor_file##*/}
156 eval "
157 $editor_name() {
158 if [ -d \"\$1\" ]
159 then
160 $editor_file \"\$1\"
161 return \$?
162 else
163 TIME=\"\`date +%s\`\"
164 DIR=\"\"
165 [ \"\${1#/}\" = \"\$1\" ] && DIR=\"\$PWD/\"
166 DIFFNAME=\"\${L3_SESSION_ID}_\${TIME}\`echo \$DIR\$1| sed s@_@__@ | sed 's@/@_@g'\`.diff\"
167 old_file=\"/tmp/l3-saved-\$\$.\$RANDOM.\$RANDOM\"
168 /bin/cp -- \"\$1\" \"\$old_file\" 2> /dev/null
169 $editor_file \"\$@\" || ERR=\$?
170 if [ -e \"\$old_file\" ]
171 then
172 diff -u \"\$old_file\" \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null
173 else
174 diff -u /dev/null \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null
175 fi
176 /bin/rm \"\$old_file\" 2> /dev/null
177 return \$ERR
178 fi
179 }
180 "
181 L3_TAMPERED_EDITORS="$L3_TAMPERED_EDITORS $editor_name"
182 done
183 [ -n "$L3_TAMPERED_EDITORS" ] && export L3_TAMPERED_EDITORS
184 }
186 _l3_tamper_commands()
187 {
188 tty()
189 {
190 [ -n "$L3_PARENT_TTY" ] && echo $L3_PARENT_TTY || /usr/bin/tty
191 }
192 }
194 _l3_unset_internal()
195 {
196 unset `set | grep '^_l3_.*()' | sed 's/()//'`
197 unset `set | grep '^_l3_.*=' | sed 's/=.*//'`
198 }
200 l3shot()
201 {
202 if [ -x "`which xwd`" ]
203 then
204 _l3_home=${L3_HOME:-~/.lilalo}
205 shot_name="${L3_SESSION_ID}_`date +%s`".xwd
206 echo -n Choose window to be shoot ... >&2
207 [ -d ${_l3_home} ] || mkdir -p ${_l3_home}
208 xwd -out "$_l3_home/$shot_name" \
209 && echo Screenshot is written to ${_l3_home}/${shot_name} \
210 && curl -s -F photo=@$_l3_home/$shot_name http://`l3-config backend_address`/l3-upload
211 else
212 {
213 echo
214 echo "Can't make screenshot :("
215 echo
216 echo I must use program xwd to make screenshot,
217 echo but it seems not to be installed
218 echo Try to find the program in the \"xbase-clients\" package
219 echo
220 } >&2
221 fi
222 }
224 l3upload()
225 {
226 if [ $# -lt 1 ]
227 then
228 echo Usage:
229 echo
230 echo l3upload "<filename> [<time>]"
231 echo
232 echo "<filename>" - name of the file to upload
233 return 1
234 else
235 source=$1
236 time=`date +%s`
237 [ -n "$2" ] && time="$2"
238 target="${L3_SESSION_ID}_$time"_"$(echo $source|sed s@.*/@@)"
239 if echo $source | grep -q http://
240 then
241 curl -s "$source" > /tmp/$target
242 else
243 [ -r "$source" ] || { echo "l3upload: Can't open $source for reading" > /dev/stderr; return 1; }
244 cp $source /tmp/$target
245 fi
246 echo Uploaded file name is ${target}
247 curl -s -F photo=@/tmp/$target http://`l3-config backend_address`/l3-upload && rm -f /tmp/$target
248 fi
249 }
251 l3mass_upload()
252 {
253 for i in "$@"
254 do
255 l3upload $i `perl -e 'print (((stat("$i"))[9])."\n")'`
256 done
257 }
259 # Append lines from "$@" files to the end of the shell history
260 hist_append ()
261 {
262 eval $(cat "$@" | sed 's/"/\\\\"/g' | while read line; do echo history -s \"$line\"\;; done);
263 }
265 l3_save_last_line ()
266 {
267 (
268 echo '<cline>'
269 history 1 | sed 's/&/&amp;/; s/</\&lt;/g; s/>/\&gt;/g'
270 echo '</cline>'
271 ) >> $L3_HOME/$L3_SESSION_ID.info
272 }
274 l3 ()
275 {
276 case "$1" in
277 context)
278 if [ -z "$2" ]
279 then
280 echo "$L3_CONTEXT"
281 else
282 echo $2 | grep -q ^/ && L3_CONTEXT="$2" || L3_CONTEXT="$L3_CONTEXT/$2"
283 export L3_CONTEXT="`echo $L3_CONTEXT | perl -e '$_=<>; 1 while s@/[^/]*/\.\.@@; print;'`"
284 fi
285 ;;
286 cd)
287 echo l3cd="$2" > ~/.l3rc
288 ;;
289 pwd)
290 grep ^l3cd= ~/.l3rc | sed s/[^=]*=//
291 ;;
292 *)
293 l3text "$@"
294 ;;
295 esac
296 }