#!/bin/sh # (c) Igor Chubin, igor@chub.in, 2004-2006 # Environment variables set by the script: # # L3_SESSION_ID - uniq id of the LiLaLo-session # L3_PARENT_TTY - name of tty on which script is running # PS1 - intercative shell prompt in which LiLaLo hides # various information about the command # L3_TAMPERED_EDITORS - list of editors which are tampered with functions # Functions with the names starting _l3_ are internal. # Such functions are unset before this rc script exits _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' _l3_start() { echo $- | grep -q i || return 0 if _l3_is_running_here then _l3_env _l3_init_prompt l3_fix_prompt _l3_tamper_editors _l3_tamper_commands _l3_unset_internal else _l3_start_session _l3_run_script fi } # ===================== STAGE 1 ============================ _l3_is_running_here() { ps waux | awk '{print $2" "$11 }' | grep -q ^$PPID" "script return $? # Check if ^^^^ run on Linux # and del vvvv this if it does export L3_TTY=`/usr/bin/tty` uname -a | grep -qi bsd && bsd=yes proc_on_the_term=`w | grep "${L3_TTY##/dev/}" | awk '{print $8;}'` # freeBSD: [ -n "$bsd" ] && \ proc_on_the_term=`w | grep "${L3_TTY##/dev/tty}" | awk '{print $6;}'` [ -n "$proc_on_the_term" ] && echo $proc_on_the_term | grep -q script } _l3_start_session() { export L3_SESSION_ID=${RANDOM}${RANDOM}${RANDOM}${RANDOM}-`date +%s` export L3_HOME=~/.lilalo/ mkdir -p $L3_HOME tty=`/usr/bin/tty` uname -a | grep -qi bsd && bsd=yes parent=`cat /proc/$PPID/cmdline 2> /dev/null` [ -z "$parent" ] && parent="`ps waux | awk '{if ($2 == '$PPID') print $11; }'`" system=`uname -rs` login_from=`who | grep "${tty##/dev/}" | awk '{print $6;}' | tr -d '()'` #[ -n "$bsd" ] && login_from="" #FIXME! start_time=`date +%s` hostname=`hostname -f 2> /dev/null` [ -n "$bsd" ] && hostname=`hostname` cat < $L3_HOME/$L3_SESSION_ID.info $L3_SESSION_ID $hostname $USER $UID $login_from $tty $system $parent $PPID $$ $start_time $LANG INFO unset parent system login_from start_time hostname tty } _l3_run_script() { uname -a | grep -qi bsd && bsd=yes flush="-f" #linux [ -n "$bsd" ] && flush="-t 0" #freebsd export L3_PARENT_TTY=`/usr/bin/tty` exec script $flush -q $L3_HOME/${L3_SESSION_ID}.script } # ===================== STAGE 2 ============================ _l3_env() { trap l3_close_session 2 trap l3_close_session EXIT true } l3_close_session() { ( echo '' history | sed 's/&/\&/; s//\>/g' echo '' ) >> $L3_HOME/$L3_SESSION_ID.info } _l3_init_prompt() { PS1='[\u@\h:\W]\$ ' [ $UID = 0 ] \ && PS1='\[\033[0;31m\]'$PS1'\[\033[0m\]' \ || PS1='\[\033[0;32m\]'$PS1'\[\033[0m\]' \ export PS1 } l3_fix_prompt() { export PS1='\[v2#\!#$?#$UID#$$#$(/bin/date +%s)$(l3_save_last_line)#$PWD#\033[1024D\033[K\]'$PS1 } _l3_tamper_editors() { for editor_file in $_l3_editors_to_tamper do [ -x $editor_file ] || continue editor_name=${editor_file##*/} eval " $editor_name() { if [ -d \"\$1\" ] then $editor_file \"\$1\" return \$? else TIME=\"\`date +%s\`\" DIR=\"\" [ \"\${1#/}\" = \"\$1\" ] && DIR=\"\$PWD/\" DIFFNAME=\"\${L3_SESSION_ID}_\${TIME}\`echo \$DIR\$1| sed s@_@__@ | sed 's@/@_@g'\`.diff\" old_file=\"/tmp/l3-saved-\$\$.\$RANDOM.\$RANDOM\" /bin/cp -- \"\$1\" \"\$old_file\" 2> /dev/null $editor_file \"\$@\" || ERR=\$? [ -e \"\$old_file\" ] \ && diff -u \"\$old_file\" \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null \ || diff -u /dev/null \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null /bin/rm \"\$old_file\" 2> /dev/null return \$ERR fi } " L3_TAMPERED_EDITORS="$L3_TAMPERED_EDITORS $editor_name" done [ -n "$L3_TAMPERED_EDITORS" ] && export L3_TAMPERED_EDITORS } _l3_tamper_commands() { tty() { [ -n "$L3_PARENT_TTY" ] && echo $L3_PARENT_TTY || /usr/bin/tty } } _l3_unset_internal() { unset `set | grep '^_l3_.*()' | sed 's/()//'` unset `set | grep '^_l3_.*=' | sed 's/=.*//'` } l3shot() { if [ -x "`which xwd`" ] then _l3_home=${L3_HOME:-~/.lilalo} shot_name="${L3_SESSION_ID}_`date +%s`".xwd echo -n Choose window to be shoot ... >&2 [ -d ${_l3_home} ] || mkdir -p ${_l3_home} xwd -out "$_l3_home/$shot_name" \ && echo Screenshot is written to ${_l3_home}/${shot_name} \ && curl -s -F photo=@$_l3_home/$shot_name http://xgu.ru/l3-upload else { echo echo "Can't make screenshot :(" echo echo I must use program xwd to make screenshot, echo but it seems not to be installed echo Try to find the program in the \"xbase-clients\" package echo } >&2 fi } l3upload() { if [ $# -lt 1 ] then echo Usage: echo echo l3upload "" echo echo "" - name of the file to upload return 1 else source=$1 target="${L3_SESSION_ID}_`date +%s`"_"$(echo $source|sed s@.*/@@)" if echo $source | grep -q http:// then curl -s "$source" > /tmp/$target else [ -r "$source" ] || { echo "l3upload: Can't open $source for reading" > /dev/stderr; return 1; } cp $source /tmp/$target fi echo Uploaded file name is ${target} curl -s -F photo=@/tmp/$target http://xgu.ru/l3-upload && rm -f /tmp/$target fi } # Append lines from "$@" files to the end of the shell history hist_append () { eval $(cat "$@" | sed 's/"/\\\\"/g' | while read line; do echo history -s \"$line\"\;; done); } l3_save_last_line () { ( echo '' history 1 | sed 's/&/&/; s//\>/g' echo '' ) >> $L3_HOME/$L3_SESSION_ID.info } l3 () { case "$1" in on) echo switching on ;; off) echo switcing off ;; cd) echo l3cd="$2" > ~/.l3rc ;; pwd) grep ^l3cd= ~/.l3rc | sed s/[^=]*=// ;; *) cat <