lilalo

view l3bashrc @ 104:77f033a04361

Пофиксил приглашение
author devi
date Tue Jun 27 10:58:57 2006 +0300 (2006-06-27)
parents 9f64b4cfa4f6
children 09ed722f2759
line source
1 #!/bin/sh
4 # Environment variables set by the script:
5 #
6 # L3_SESSION_ID - uniq id of the LiLaLo-session
7 # L3_PARENT_TTY - name of tty on which script is running
8 # PS1 - intercative shell prompt in which LiLaLo hides
9 # various information about the command
10 # L3_TAMPERED_EDITORS - list of editors which are tampered with functions
13 # Functions with the names starting _l3_ are internal.
14 # Such functions are unset before this rc script exits
16 _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'
18 _l3_start()
19 {
20 echo $- | grep -q i || return 0
21 if _l3_is_running_here
22 then
23 _l3_env
24 _l3_init_prompt
25 l3_fix_prompt
26 _l3_tamper_editors
27 _l3_tamper_commands
28 _l3_unset_internal
29 else
30 _l3_start_session
31 _l3_run_script
32 fi
33 }
35 # ===================== STAGE 1 ============================
37 _l3_is_running_here()
38 {
39 ps waux | awk '{print $2" "$11 }' | grep -q ^$PPID" "script
40 return $?
42 # Check if ^^^^ run on Linux
43 # and del vvvv this if it does
45 export L3_TTY=`/usr/bin/tty`
46 uname -a | grep -qi bsd && bsd=yes
47 proc_on_the_term=`w | grep "${L3_TTY##/dev/}" | awk '{print $8;}'`
48 # freeBSD:
49 [ -n "$bsd" ] && \
50 proc_on_the_term=`w | grep "${L3_TTY##/dev/tty}" | awk '{print $6;}'`
52 [ -n "$proc_on_the_term" ] && echo $proc_on_the_term | grep -q script
53 }
55 _l3_start_session()
56 {
57 export L3_SESSION_ID=${RANDOM}${RANDOM}${RANDOM}${RANDOM}-`date +%s`
58 export L3_HOME=~/.lilalo/
59 mkdir -p $L3_HOME
61 tty=`/usr/bin/tty`
62 uname -a | grep -qi bsd && bsd=yes
63 parent=`cat /proc/$PPID/cmdline 2> /dev/null`
64 [ -z "$parent" ] && parent="`ps waux | awk '{if ($2 == '$PPID') print $11; }'`"
65 system=`uname -rs`
66 login_from=`who | grep "${tty##/dev/}" | awk '{print $6;}' | tr -d '()'`
67 #[ -n "$bsd" ] && login_from="" #FIXME!
68 start_time=`date +%s`
69 hostname=`hostname -f 2> /dev/null`
70 [ -n "$bsd" ] && hostname=`hostname`
72 cat <<INFO > $L3_HOME/$L3_SESSION_ID.info
73 <session>
74 <local_session_id>$L3_SESSION_ID</local_session_id>
75 <hostname>$hostname</hostname>
76 <user>$USER</user>
77 <uid>$UID</uid>
78 <login_from>$login_from</login_from>
79 <tty>$tty</tty>
80 <system>$system</system>
81 <parent>$parent</parent>
82 <ppid>$PPID</ppid>
83 <pid>$$</pid>
84 <start_time>$start_time</start_time>
85 <lang>$LANG</lang>
86 </session>
87 INFO
89 unset parent system login_from start_time hostname tty
90 }
92 _l3_run_script()
93 {
94 uname -a | grep -qi bsd && bsd=yes
95 flush="-f" #linux
96 [ -n "$bsd" ] && flush="-t 0" #freebsd
97 export L3_PARENT_TTY=`/usr/bin/tty`
98 exec script $flush -q $L3_HOME/${L3_SESSION_ID}.script
99 }
101 # ===================== STAGE 2 ============================
103 _l3_env()
104 {
105 trap l3_close_session 2
106 trap l3_close_session EXIT
107 true
108 }
110 l3_close_session()
111 {
112 (
113 echo '<history>'
114 history | sed 's/&/\&amp;/; s/</\&lt;/g; s/>/\&gt;/g'
115 echo '</history>'
116 ) >> $L3_HOME/$L3_SESSION_ID.info
117 }
119 _l3_init_prompt()
120 {
121 PS1='[\u@\h:\W]\$ '
122 [ $UID = 0 ] \
123 && PS1='\[\033[0;31m\]'$PS1'\[\033[0m\]' \
124 || PS1='\[\033[0;32m\]'$PS1'\[\033[0m\]' \
125 export PS1
126 }
128 l3_fix_prompt()
129 {
130 export PS1='\[v2#\!#$?#$UID#$$#$(/bin/date +%s)$(l3_save_last_line)#$PWD#\033[1024D\033[K\]'$PS1
131 }
133 _l3_tamper_editors()
134 {
135 for editor_file in $_l3_editors_to_tamper
136 do
137 [ -x $editor_file ] || continue
138 editor_name=${editor_file##*/}
139 eval "
140 $editor_name() {
141 if [ -d \"\$1\" ]
142 then
143 $editor_file \"\$1\"
144 return \$?
145 else
146 TIME=\"\`date +%s\`\"
147 DIR=\"\"
148 [ \"\${1#/}\" = \"\$1\" ] && DIR=\"\$PWD/\"
149 DIFFNAME=\"\${L3_SESSION_ID}_\${TIME}\`echo \$DIR\$1| sed s@_@__@ | sed 's@/@_@g'\`.diff\"
150 old_file=\"/tmp/l3-saved-\$\$.\$RANDOM.\$RANDOM\"
151 /bin/cp -- \"\$1\" \"\$old_file\" 2> /dev/null
152 $editor_file \"\$@\" || ERR=\$?
153 [ -e \"\$old_file\" ] && diff \"\$old_file\" \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null
154 if [ "$?" == 2 ]
155 then
156 diff /dev/null \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null
157 fi
158 /bin/rm \"\$old_file\" 2> /dev/null
159 return \$ERR
160 fi
161 }
162 "
163 L3_TAMPERED_EDITORS="$L3_TAMPERED_EDITORS $editor_name"
164 done
165 [ -n "$L3_TAMPERED_EDITORS" ] && export L3_TAMPERED_EDITORS
166 }
168 _l3_tamper_commands()
169 {
170 tty()
171 {
172 [ -n "$L3_PARENT_TTY" ] && echo $L3_PARENT_TTY || /usr/bin/tty
173 }
174 }
176 _l3_unset_internal()
177 {
178 unset `set | grep '^_l3_.*()' | sed 's/()//'`
179 unset `set | grep '^_l3_.*=' | sed 's/=.*//'`
180 }
183 l3shot()
184 {
185 if [ -x "`which xwd`" ]
186 then
187 _l3_home=${L3_HOME:-~/.lilalo}
188 shot_name="${L3_SESSION_ID}_`date +%s`".xwd
189 echo -n Choose window to be shoot ... >&2
190 [ -d ${_l3_home} ] || mkdir -p ${_l3_home}
191 xwd -out "$_l3_home/$shot_name" \
192 && echo Ok\
193 && echo Shot was successful. \
194 && echo Screenshot is written to ${_l3_home}/${shot_name} \
195 && echo Screenshot will appears in the lablog.
196 else
197 {
198 echo
199 echo "Can't make screenshot :("
200 echo
201 echo I must use program xwd to make screenshot,
202 echo but it seems not to be installed
203 echo Try to find the program in the \"xbase-clients\" package
204 echo
205 } >&2
206 fi
207 }
209 # Append lines from "$@" files to the end of the shell history
210 hist_append ()
211 {
212 eval $(cat "$@" | sed 's/"/\\\\"/g' | while read line; do echo history -s \"$line\"\;; done);
213 }
215 l3_save_last_line ()
216 {
217 (
218 echo '<cline>'
219 history 1 | sed 's/&/&amp;/; s/</\&lt;/g; s/>/\&gt;/g'
220 echo '</cline>'
221 ) >> $L3_HOME/$L3_SESSION_ID.info
222 }