lilalo

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