lilalo
annotate l3bashrc @ 111:99ea38e538c9
Добавил:
* l3upload
Исправил:
* хинт теперь всплывает только при наведении непосредственно на команду
(а не на приглашение и не на символ кода завершения)
* подсветка неизвестных команд не такая сильная
* l3upload
Исправил:
* хинт теперь всплывает только при наведении непосредственно на команду
(а не на приглашение и не на символ кода завершения)
* подсветка неизвестных команд не такая сильная
author | igor |
---|---|
date | Sat Feb 16 13:41:48 2008 +0200 (2008-02-16) |
parents | 3cd466f35ad6 |
children | da65f1cbaf3a |
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/ |
devi@70 | 60 mkdir -p $L3_HOME |
devi@70 | 61 |
devi@79 | 62 tty=`/usr/bin/tty` |
devi@70 | 63 uname -a | grep -qi bsd && bsd=yes |
devi@70 | 64 parent=`cat /proc/$PPID/cmdline 2> /dev/null` |
devi@79 | 65 [ -z "$parent" ] && parent="`ps waux | awk '{if ($2 == '$PPID') print $11; }'`" |
devi@70 | 66 system=`uname -rs` |
devi@79 | 67 login_from=`who | grep "${tty##/dev/}" | awk '{print $6;}' | tr -d '()'` |
devi@70 | 68 #[ -n "$bsd" ] && login_from="" #FIXME! |
devi@70 | 69 start_time=`date +%s` |
devi@70 | 70 hostname=`hostname -f 2> /dev/null` |
devi@70 | 71 [ -n "$bsd" ] && hostname=`hostname` |
devi@70 | 72 |
devi@70 | 73 cat <<INFO > $L3_HOME/$L3_SESSION_ID.info |
devi@70 | 74 <session> |
devi@70 | 75 <local_session_id>$L3_SESSION_ID</local_session_id> |
devi@70 | 76 <hostname>$hostname</hostname> |
devi@70 | 77 <user>$USER</user> |
devi@70 | 78 <uid>$UID</uid> |
devi@70 | 79 <login_from>$login_from</login_from> |
devi@79 | 80 <tty>$tty</tty> |
devi@70 | 81 <system>$system</system> |
devi@70 | 82 <parent>$parent</parent> |
devi@70 | 83 <ppid>$PPID</ppid> |
devi@70 | 84 <pid>$$</pid> |
devi@70 | 85 <start_time>$start_time</start_time> |
devi@76 | 86 <lang>$LANG</lang> |
devi@70 | 87 </session> |
devi@70 | 88 INFO |
devi@70 | 89 |
devi@79 | 90 unset parent system login_from start_time hostname tty |
devi@70 | 91 } |
devi@70 | 92 |
devi@70 | 93 _l3_run_script() |
devi@70 | 94 { |
devi@70 | 95 uname -a | grep -qi bsd && bsd=yes |
devi@70 | 96 flush="-f" #linux |
devi@70 | 97 [ -n "$bsd" ] && flush="-t 0" #freebsd |
devi@76 | 98 export L3_PARENT_TTY=`/usr/bin/tty` |
devi@70 | 99 exec script $flush -q $L3_HOME/${L3_SESSION_ID}.script |
devi@70 | 100 } |
devi@70 | 101 |
devi@76 | 102 # ===================== STAGE 2 ============================ |
devi@76 | 103 |
devi@76 | 104 _l3_env() |
devi@76 | 105 { |
devi@76 | 106 trap l3_close_session 2 |
devi@76 | 107 trap l3_close_session EXIT |
devi@76 | 108 true |
devi@76 | 109 } |
devi@76 | 110 |
devi@76 | 111 l3_close_session() |
devi@70 | 112 { |
devi@71 | 113 ( |
devi@71 | 114 echo '<history>' |
devi@71 | 115 history | sed 's/&/\&/; s/</\</g; s/>/\>/g' |
devi@71 | 116 echo '</history>' |
devi@71 | 117 ) >> $L3_HOME/$L3_SESSION_ID.info |
devi@70 | 118 } |
devi@70 | 119 |
devi@76 | 120 _l3_init_prompt() |
devi@76 | 121 { |
devi@76 | 122 PS1='[\u@\h:\W]\$ ' |
devi@77 | 123 [ $UID = 0 ] \ |
devi@77 | 124 && PS1='\[\033[0;31m\]'$PS1'\[\033[0m\]' \ |
devi@77 | 125 || PS1='\[\033[0;32m\]'$PS1'\[\033[0m\]' \ |
devi@76 | 126 export PS1 |
devi@76 | 127 } |
devi@76 | 128 |
devi@76 | 129 l3_fix_prompt() |
devi@76 | 130 { |
devi@104 | 131 export PS1='\[v2#\!#$?#$UID#$$#$(/bin/date +%s)$(l3_save_last_line)#$PWD#\033[1024D\033[K\]'$PS1 |
devi@76 | 132 } |
devi@76 | 133 |
devi@76 | 134 _l3_tamper_editors() |
devi@76 | 135 { |
devi@76 | 136 for editor_file in $_l3_editors_to_tamper |
devi@76 | 137 do |
devi@76 | 138 [ -x $editor_file ] || continue |
devi@76 | 139 editor_name=${editor_file##*/} |
devi@76 | 140 eval " |
devi@76 | 141 $editor_name() { |
devi@76 | 142 if [ -d \"\$1\" ] |
devi@76 | 143 then |
devi@76 | 144 $editor_file \"\$1\" |
devi@76 | 145 return \$? |
devi@76 | 146 else |
devi@76 | 147 TIME=\"\`date +%s\`\" |
devi@76 | 148 DIR=\"\" |
devi@76 | 149 [ \"\${1#/}\" = \"\$1\" ] && DIR=\"\$PWD/\" |
devi@80 | 150 DIFFNAME=\"\${L3_SESSION_ID}_\${TIME}\`echo \$DIR\$1| sed s@_@__@ | sed 's@/@_@g'\`.diff\" |
devi@76 | 151 old_file=\"/tmp/l3-saved-\$\$.\$RANDOM.\$RANDOM\" |
devi@76 | 152 /bin/cp -- \"\$1\" \"\$old_file\" 2> /dev/null |
devi@76 | 153 $editor_file \"\$@\" || ERR=\$? |
igor@109 | 154 [ -e \"\$old_file\" ] \ |
igor@109 | 155 && diff -u \"\$old_file\" \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null \ |
igor@109 | 156 || diff -u /dev/null \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null |
devi@76 | 157 /bin/rm \"\$old_file\" 2> /dev/null |
devi@76 | 158 return \$ERR |
devi@76 | 159 fi |
devi@76 | 160 } |
devi@76 | 161 " |
devi@76 | 162 L3_TAMPERED_EDITORS="$L3_TAMPERED_EDITORS $editor_name" |
devi@76 | 163 done |
devi@76 | 164 [ -n "$L3_TAMPERED_EDITORS" ] && export L3_TAMPERED_EDITORS |
devi@76 | 165 } |
devi@76 | 166 |
devi@76 | 167 _l3_tamper_commands() |
devi@76 | 168 { |
devi@76 | 169 tty() |
devi@76 | 170 { |
devi@76 | 171 [ -n "$L3_PARENT_TTY" ] && echo $L3_PARENT_TTY || /usr/bin/tty |
devi@76 | 172 } |
devi@76 | 173 } |
devi@76 | 174 |
devi@76 | 175 _l3_unset_internal() |
devi@76 | 176 { |
devi@76 | 177 unset `set | grep '^_l3_.*()' | sed 's/()//'` |
devi@76 | 178 unset `set | grep '^_l3_.*=' | sed 's/=.*//'` |
devi@76 | 179 } |
devi@78 | 180 |
devi@78 | 181 |
devi@78 | 182 l3shot() |
devi@78 | 183 { |
devi@78 | 184 if [ -x "`which xwd`" ] |
devi@78 | 185 then |
devi@78 | 186 _l3_home=${L3_HOME:-~/.lilalo} |
devi@78 | 187 shot_name="${L3_SESSION_ID}_`date +%s`".xwd |
devi@78 | 188 echo -n Choose window to be shoot ... >&2 |
devi@78 | 189 [ -d ${_l3_home} ] || mkdir -p ${_l3_home} |
devi@78 | 190 xwd -out "$_l3_home/$shot_name" \ |
devi@78 | 191 && echo Screenshot is written to ${_l3_home}/${shot_name} \ |
igor@111 | 192 && curl -s -F photo=@$_l3_home/$shot_name http://xgu.ru/l3-upload |
devi@78 | 193 else |
devi@78 | 194 { |
devi@78 | 195 echo |
devi@78 | 196 echo "Can't make screenshot :(" |
devi@78 | 197 echo |
devi@78 | 198 echo I must use program xwd to make screenshot, |
devi@78 | 199 echo but it seems not to be installed |
devi@78 | 200 echo Try to find the program in the \"xbase-clients\" package |
devi@78 | 201 echo |
devi@78 | 202 } >&2 |
devi@78 | 203 fi |
devi@78 | 204 } |
devi@97 | 205 |
igor@111 | 206 l3upload() |
igor@111 | 207 { |
igor@111 | 208 if [ $# -lt 1 ] |
igor@111 | 209 then |
igor@111 | 210 echo Usage: |
igor@111 | 211 echo |
igor@111 | 212 echo l3upload "<filename>" |
igor@111 | 213 echo |
igor@111 | 214 echo "<filename>" - name of the file to upload |
igor@111 | 215 return 1 |
igor@111 | 216 else |
igor@111 | 217 source=$1 |
igor@111 | 218 target="${L3_SESSION_ID}_`date +%s`"_"$(echo $source|sed s@.*/@@)" |
igor@111 | 219 if echo $source | grep -q http:// |
igor@111 | 220 then |
igor@111 | 221 curl -s "$source" > /tmp/$target |
igor@111 | 222 else |
igor@111 | 223 [ -r "$source" ] || { echo "l3upload: Can't open $source for reading" > /dev/stderr; return 1; } |
igor@111 | 224 cp $source /tmp/$target |
igor@111 | 225 fi |
igor@111 | 226 echo Uploaded file name is ${target} |
igor@111 | 227 curl -s -F photo=@/tmp/$target http://xgu.ru/l3-upload && rm -f /tmp/$target |
igor@111 | 228 fi |
igor@111 | 229 } |
igor@111 | 230 |
devi@97 | 231 # Append lines from "$@" files to the end of the shell history |
igor@111 | 232 hist_append () |
igor@111 | 233 { |
igor@111 | 234 eval $(cat "$@" | sed 's/"/\\\\"/g' | while read line; do echo history -s \"$line\"\;; done); |
devi@97 | 235 } |
devi@97 | 236 |
devi@97 | 237 l3_save_last_line () |
devi@97 | 238 { |
devi@97 | 239 ( |
devi@97 | 240 echo '<cline>' |
devi@97 | 241 history 1 | sed 's/&/&/; s/</\</g; s/>/\>/g' |
devi@97 | 242 echo '</cline>' |
devi@97 | 243 ) >> $L3_HOME/$L3_SESSION_ID.info |
devi@97 | 244 } |
devi@97 | 245 |
devi@107 | 246 l3 () |
devi@107 | 247 { |
devi@107 | 248 case "$1" in |
devi@107 | 249 on) |
devi@107 | 250 echo switching on |
devi@107 | 251 ;; |
devi@107 | 252 off) |
devi@107 | 253 echo switcing off |
devi@107 | 254 ;; |
devi@107 | 255 cd) |
devi@107 | 256 echo l3cd="$2" > ~/.l3rc |
devi@107 | 257 ;; |
devi@107 | 258 pwd) |
devi@107 | 259 grep ^l3cd= ~/.l3rc | sed s/[^=]*=// |
devi@107 | 260 ;; |
devi@107 | 261 *) |
igor@109 | 262 cat <<EOF |
igor@109 | 263 l3 [command]: |
igor@109 | 264 |
igor@109 | 265 cd new_context - change current lilalo context |
igor@109 | 266 pwd - show current lilalo context |
igor@109 | 267 on - switch writing on (NOT IMPLEMENTED YET) |
igor@109 | 268 off - switch writing off (NOT IMPLEMENTED YET) |
igor@109 | 269 |
igor@109 | 270 EOF |
devi@107 | 271 ;; |
devi@107 | 272 esac |
devi@107 | 273 } |
devi@97 | 274 |
igor@109 | 275 l3cd() |
igor@109 | 276 { |
igor@109 | 277 l3 cd "$@" |
igor@109 | 278 } |
igor@109 | 279 |
igor@109 | 280 l3pwd() |
igor@109 | 281 { |
igor@109 | 282 l3 pwd |
igor@109 | 283 } |
igor@109 | 284 |