lilalo

view l3bashrc @ 77:e56f21c44faf

Под root'ом приглашение красного цвета
author devi
date Mon Feb 13 19:28:51 2006 +0200 (2006-02-13)
parents 0da83215839a
children 147fb109c012
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 # L3_TTY - current tty
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 if _l3_is_not_running_here
22 then
23 _l3_start_session
24 _l3_run_script
25 else
26 _l3_env
27 _l3_init_prompt
28 l3_fix_prompt
29 _l3_tamper_editors
30 _l3_tamper_commands
31 _l3_unset_internal
32 fi
33 }
35 # ===================== STAGE 1 ============================
37 _l3_is_not_running_here()
38 {
39 export L3_TTY=`/usr/bin/tty`
40 proc_on_the_term=`w | grep "${L3_TTY##/dev/}" | awk '{print $8;}'`
41 # freeBSD:
42 [ -n "$bsd" ] && \
43 proc_on_the_term=`w | grep "${L3_TTY##/dev/tty}" | awk '{print $6;}'`
45 [ -n "$proc_on_the_term" ] && echo $proc_on_the_term | grep -qv script
46 }
48 _l3_start_session()
49 {
50 export L3_SESSION_ID=${L3_TTY##*/}-$$
51 export L3_HOME=~/.lilalo/
52 mkdir -p $L3_HOME
54 uname -a | grep -qi bsd && bsd=yes
55 parent=`cat /proc/$PPID/cmdline 2> /dev/null`
56 system=`uname -rs`
57 login_from=`who | grep "${L3_TTY##/dev/}" | awk '{print $6;}' | tr -d '()'`
58 #[ -n "$bsd" ] && login_from="" #FIXME!
59 start_time=`date +%s`
60 hostname=`hostname -f 2> /dev/null`
61 [ -n "$bsd" ] && hostname=`hostname`
63 cat <<INFO > $L3_HOME/$L3_SESSION_ID.info
64 <session>
65 <local_session_id>$L3_SESSION_ID</local_session_id>
66 <hostname>$hostname</hostname>
67 <user>$USER</user>
68 <uid>$UID</uid>
69 <login_from>$login_from</login_from>
70 <tty>$L3_TTY</tty>
71 <system>$system</system>
72 <parent>$parent</parent>
73 <ppid>$PPID</ppid>
74 <pid>$$</pid>
75 <start_time>$start_time</start_time>
76 <lang>$LANG</lang>
77 </session>
78 INFO
80 unset parent system login_from start_time hostname
81 }
83 _l3_run_script()
84 {
85 uname -a | grep -qi bsd && bsd=yes
86 flush="-f" #linux
87 [ -n "$bsd" ] && flush="-t 0" #freebsd
88 export L3_PARENT_TTY=`/usr/bin/tty`
89 exec script $flush -q $L3_HOME/${L3_SESSION_ID}.script
90 }
92 # ===================== STAGE 2 ============================
94 _l3_env()
95 {
96 trap l3_close_session 2
97 trap l3_close_session EXIT
98 true
99 }
101 l3_close_session()
102 {
103 (
104 echo '<history>'
105 history | sed 's/&/\&amp;/; s/</\&lt;/g; s/>/\&gt;/g'
106 echo '</history>'
107 ) >> $L3_HOME/$L3_SESSION_ID.info
108 }
110 _l3_init_prompt()
111 {
112 PS1='[\u@\h:\W]\$ '
113 [ $UID = 0 ] \
114 && PS1='\[\033[0;31m\]'$PS1'\[\033[0m\]' \
115 || PS1='\[\033[0;32m\]'$PS1'\[\033[0m\]' \
116 export PS1
117 }
119 l3_fix_prompt()
120 {
121 export PS1='\[v2#\!#$?#$UID#$$#$(/bin/date +%s)#$PWD#\033[1024D\033[K\]'$PS1
122 }
124 _l3_tamper_editors()
125 {
126 for editor_file in $_l3_editors_to_tamper
127 do
128 [ -x $editor_file ] || continue
129 editor_name=${editor_file##*/}
130 eval "
131 $editor_name() {
132 if [ -d \"\$1\" ]
133 then
134 $editor_file \"\$1\"
135 return \$?
136 else
137 TIME=\"\`date +%s\`\"
138 DIR=\"\"
139 [ \"\${1#/}\" = \"\$1\" ] && DIR=\"\$PWD/\"
140 DIFFNAME=\"\$PPID_\${TIME}_\`echo \$DIR\$1| /bin/sed s@_@__@ | /bin/sed 's@/@_@g'\`.diff\"
141 old_file=\"/tmp/l3-saved-\$\$.\$RANDOM.\$RANDOM\"
142 /bin/cp -- \"\$1\" \"\$old_file\" 2> /dev/null
143 $editor_file \"\$@\" || ERR=\$?
144 [ -e \"\$old_file\" ] && diff \"\$old_file\" \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null \
145 || diff /dev/null \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null
146 /bin/rm \"\$old_file\" 2> /dev/null
147 return \$ERR
148 fi
149 }
150 "
151 L3_TAMPERED_EDITORS="$L3_TAMPERED_EDITORS $editor_name"
152 done
153 [ -n "$L3_TAMPERED_EDITORS" ] && export L3_TAMPERED_EDITORS
154 }
156 _l3_tamper_commands()
157 {
158 tty()
159 {
160 [ -n "$L3_PARENT_TTY" ] && echo $L3_PARENT_TTY || /usr/bin/tty
161 }
162 }
164 _l3_unset_internal()
165 {
166 unset `set | grep '^_l3_.*()' | sed 's/()//'`
167 unset `set | grep '^_l3_.*=' | sed 's/=.*//'`
168 }