lilalo
diff l3-agent @ 119:71bd999bcb04
Исправлено несколько багов:
* выполняется корректная привязка diff'ов
* правильно запоминается raw_start и проч raw_*
* временно отключен вывод признака нажатия ctrl-c (он ставился неверно)
* в приглашение добавлен случайный nonce (для правильной отработки tab)
* выполняется корректная привязка diff'ов
* правильно запоминается raw_start и проч raw_*
* временно отключен вывод признака нажатия ctrl-c (он ставился неверно)
* в приглашение добавлен случайный nonce (для правильной отработки tab)
author | igor |
---|---|
date | Thu Mar 13 12:19:42 2008 +0200 (2008-03-13) |
parents | 9e6359b7ad55 |
children | 42d9af3c851c |
line diff
1.1 --- a/l3-agent Sun Mar 09 22:54:22 2008 +0200 1.2 +++ b/l3-agent Thu Mar 13 12:19:42 2008 +0200 1.3 @@ -1,13 +1,9 @@ 1.4 #!/usr/bin/perl -w 1.5 1.6 # 1.7 -# (c) Igor Chubin, igor@chub.in, 2004-2006 1.8 +# (c) Igor Chubin, igor@chub.in, 2004-2008 1.9 # 1.10 1.11 - 1.12 -## Эта строчка добавлена из блокнота Windows 1.13 -## Надо отдать должное, он каким-то образом научился понимать кодировку 1.14 - 1.15 use strict; 1.16 use POSIX; 1.17 use Term::VT102; 1.18 @@ -18,7 +14,6 @@ 1.19 use lib "/etc/lilalo"; 1.20 use l3config; 1.21 1.22 - 1.23 our @Command_Lines; 1.24 our @Command_Lines_Index; 1.25 our %Diffs; 1.26 @@ -122,35 +117,54 @@ 1.27 1.28 my $min_dt = 10000; 1.29 1.30 + if (defined($cl->{"diff"})) { 1.31 + print STDERR "Command ".$cl->{time}." is already bound"; 1.32 + return; 1.33 + } 1.34 + 1.35 +# Загружаем новые diff-файлы 1.36 +# Это нужно делать непосредственно перед привязкой, поскольку diff'ы могли образоваться только что 1.37 + for my $lab_log (split (/\s+/, $Config{"diffs"} || $Config{"input"})) { 1.38 + load_diff_files($lab_log); 1.39 + } 1.40 + 1.41 + my $diff_to_bind; 1.42 for my $diff_key (keys %Diffs) { 1.43 - my $diff = $Diffs{$diff_key}; 1.44 - next if ($diff->{"local_session_id"} 1.45 - && $cl->{"local_session_id"} 1.46 - && ($cl->{"local_session_id"} ne $diff->{"local_session_id"})); 1.47 + my $diff = $Diffs{$diff_key}; 1.48 + next if ($diff->{"local_session_id"} 1.49 + && $cl->{"local_session_id"} 1.50 + && ($cl->{"local_session_id"} ne $diff->{"local_session_id"})); 1.51 1.52 - next if ($diff->{"day"} && $cl->{"day"} && ($cl->{"day"} ne $diff->{"day"})); 1.53 + next if ($diff->{"day"} && $cl->{"day"} && ($cl->{"day"} ne $diff->{"day"})); 1.54 1.55 - my $dt; 1.56 - if ($diff->{"time"} && $cl->{"time"}) { 1.57 - $dt = $diff->{"time"} - $cl->{"time"} 1.58 - } 1.59 - else { 1.60 - $dt=($diff->{"hour"}-$hour)*3600 +($diff->{"min"}-$min)*60 + ($diff->{"sec"}-$sec); 1.61 - } 1.62 - if ($dt >0 1.63 - && $dt < $min_dt 1.64 - && ($diff->{"time_range"} <0 1.65 - || $dt < $diff->{"time_range"})) { 1.66 - print "Approppriate diff found: dt=$dt\n"; 1.67 - if ($diff->{"bind_to"}) { 1.68 - undef $diff->{"bind_to"}->{"diff"}; 1.69 - }; 1.70 - $diff->{"time_range"}=$dt; 1.71 - $diff->{"bind_to"}=$cl; 1.72 - 1.73 - $cl->{"diff"} = $diff_key; 1.74 - $min_dt = $dt; 1.75 - } 1.76 + my $dt; 1.77 + if (not $diff->{"time"}) { 1.78 + print STDERR "diff time is 0"; 1.79 + print STDERR join(" ", keys(%$diff)); 1.80 + print STDERR $diff->{text}; 1.81 + } 1.82 + if (not $cl->{"time"}) { 1.83 + print STDERR "cl time is 0"; 1.84 + } 1.85 + if ($diff->{"time"} && $cl->{"time"}) { 1.86 + $dt = $diff->{"time"} - $cl->{"time"} 1.87 + } 1.88 + else { 1.89 + $dt=($diff->{"hour"}-$hour)*3600 +($diff->{"min"}-$min)*60 + ($diff->{"sec"}-$sec); 1.90 + } 1.91 + if ($dt >=0 && $dt < $min_dt && !$diff->{"bind_to"}) { 1.92 + $min_dt = $dt; 1.93 + $diff_to_bind = $diff_key; 1.94 + } 1.95 + } 1.96 + if ($diff_to_bind) { 1.97 + print "Approppriate diff found: dt=$min_dt\n"; 1.98 + $Diffs{$diff_to_bind}->{"bind_to"}=$cl; 1.99 + $cl->{"diff"} = $diff_to_bind; 1.100 + } 1.101 + else { 1.102 + print STDERR "Diff not found\n"; 1.103 + print STDERR "cl{time}",$cl->{time},"\n"; 1.104 } 1.105 } 1.106 1.107 @@ -243,6 +257,7 @@ 1.108 ([0-9]+)[\#] # pid 1.109 ([0-9]+)[\#] # time 1.110 (.*?)[\#] # pwd 1.111 + (.*?)[\#] # nonce 1.112 (.*?([\$\#]\\s?)) # prompt 1.113 (.*) # command line 1.114 ) 1.115 @@ -344,6 +359,7 @@ 1.116 1.117 # Previous command 1.118 my %last_cl = %cl; 1.119 + my $this_line = $1; 1.120 my $err = $2 || ""; 1.121 1.122 $cl{"local_session_id"} = $local_session_id; 1.123 @@ -362,7 +378,7 @@ 1.124 1.125 { 1.126 use bytes; 1.127 - $cl{"raw_start"} = tell (FILE) - length($1); 1.128 + $cl{"raw_start"} = tell (FILE) - length($this_line); 1.129 $cl{"raw_output_start"} = tell FILE; 1.130 } 1.131 $cl{"raw_file"} = $file; 1.132 @@ -374,6 +390,7 @@ 1.133 $cline_vt->process($cl{"raw_cline"}."\n"); 1.134 $cl{"cline"} = $cline_vt->row_plaintext (1); 1.135 $cl{"cline"} =~ s/\s*$//; 1.136 + $cl{"cline"} =~ s/.*?[\#\$]\s*//; 1.137 $cline_vt->reset(); 1.138 1.139 my %commands = extract_commands_from_cline($cl{"cline"}); 1.140 @@ -399,20 +416,21 @@ 1.141 1.142 print " ",$cl{"last_command"}; 1.143 1.144 + if (grep ($_ eq $last_cl{"last_command"}, @{$Config{"editors"}})) { 1.145 + bind_diff(\%last_cl); 1.146 + } 1.147 + 1.148 # Processing previous command line 1.149 - if ($first_pass) { 1.150 - $first_pass = 0; 1.151 - next; 1.152 - } 1.153 + #if ($first_pass) { 1.154 + # $first_pass = 0; 1.155 + # next; 1.156 + #} 1.157 1.158 # Error code 1.159 $last_cl{"raw_end"} = $cl{"raw_start"}; 1.160 $last_cl{"err"}=$err; 1.161 $last_cl{"err"}=130 if $err eq "^C"; 1.162 1.163 - if (grep ($_ eq $last_cl{"last_command"}, @{$Config{"editors"}})) { 1.164 - bind_diff(\%last_cl); 1.165 - } 1.166 1.167 # Output 1.168 if (!$last_cl{"suppress_output"} || $last_cl{"err"}) { 1.169 @@ -451,8 +469,9 @@ 1.170 1.171 elsif (m/$cline_re_v2/ || m/$cline_re_v3/) { 1.172 # Разбираем командную строку версии 2 1.173 + my $before=$_; 1.174 + s/.*\x0d(?!\x0a)//; 1.175 1.176 - s/.*\x0d(?!\x0a)//; 1.177 my $re; 1.178 if (m/$cline_re_v2/) { 1.179 $re=$cline_re2_v2; 1.180 @@ -460,8 +479,8 @@ 1.181 else { 1.182 s/.\[1K.\[10D//gs; 1.183 $re=$cline_re2_v3; 1.184 + print STDERR "... $_ ...\n"; 1.185 } 1.186 - m/$re/gs; 1.187 1.188 $commandlines_loaded++; 1.189 $last_output_length=0; 1.190 @@ -471,6 +490,7 @@ 1.191 1.192 $cl{"local_session_id"} = $local_session_id; 1.193 # Parse new command 1.194 + my $this_line = $1; 1.195 $cl{"history"} = $2; 1.196 my $err = $3; 1.197 $cl{"uid"} = $4; 1.198 @@ -478,13 +498,15 @@ 1.199 $cl{"pid"} = $5; 1.200 $cl{"time"} = $6; 1.201 $cl{"pwd"} = $7; 1.202 + $cl{"nonce"} = $8; 1.203 #$cl{"fullprompt"} = $8; 1.204 - $cl{"prompt"} = $9; 1.205 - $cl{"raw_cline"}= $10; 1.206 + $cl{"prompt"} = $10; 1.207 + #$cl{"raw_cline"}= $10; 1.208 + $cl{"raw_cline"}= $before; 1.209 1.210 { 1.211 use bytes; 1.212 - $cl{"raw_start"} = tell (FILE) - length($1); 1.213 + $cl{"raw_start"} = tell (FILE) - length($before); 1.214 $cl{"raw_output_start"} = tell FILE; 1.215 } 1.216 $cl{"raw_file"} = $file; 1.217 @@ -496,7 +518,9 @@ 1.218 $cline_vt->process($cl{"raw_cline"}."\n"); 1.219 $cl{"cline"} = $cline_vt->row_plaintext (1); 1.220 $cl{"cline"} =~ s/\s*$//; 1.221 + $cl{"cline"} =~ s/.*?[\#\$]\s*//; 1.222 $cline_vt->reset(); 1.223 + print STDERR "cline=".$cl{"cline"}."<<\n"; 1.224 1.225 my %commands = extract_commands_from_cline($cl{"cline"}); 1.226 #$cl{"euid"} = 0 if defined $commands{"sudo"}; 1.227 @@ -504,6 +528,8 @@ 1.228 $cl{"last_command"} 1.229 = $comms[$#comms] || ""; 1.230 1.231 + print STDERR "last_command=".$cl{"last_command"}."<<\n"; 1.232 + 1.233 if ( 1.234 $Config{"suppress_editors"} =~ /^y/i 1.235 && grep ($_ eq $cl{"last_command"}, @{$Config{"editors"}}) 1.236 @@ -519,26 +545,20 @@ 1.237 } 1.238 $skip_info = 0; 1.239 1.240 - 1.241 if ($Config{verbose} =~ /y/i) { 1.242 print "\n| " if $commandlines_loaded % 5 == 1; 1.243 print " ",$cl{"last_command"}; 1.244 } 1.245 1.246 - # Processing previous command line 1.247 - if ($first_pass) { 1.248 - $first_pass = 0; 1.249 - next; 1.250 + if (defined($last_cl{time}) 1.251 + && grep ($_ eq $last_cl{"last_command"}, @{$Config{"editors"}})) { 1.252 + bind_diff(\%last_cl); 1.253 } 1.254 1.255 # Error code 1.256 $last_cl{"err"}=$err; 1.257 $last_cl{"raw_end"} = $cl{"raw_start"}; 1.258 1.259 - if (grep ($_ eq $last_cl{"last_command"}, @{$Config{"editors"}})) { 1.260 - bind_diff(\%last_cl); 1.261 - } 1.262 - 1.263 # Output 1.264 if (!$last_cl{"suppress_output"} || $last_cl{"err"}) { 1.265 for (my $i=0; $i<$Config{"terminal_height"}; $i++) { 1.266 @@ -564,14 +584,14 @@ 1.267 $last_cl{$_} = $converter->convert($last_cl{$_}) 1.268 } 1.269 } 1.270 - push @Command_Lines, \%last_cl; 1.271 - 1.272 - # Сохранение позиции в файле, до которой выполнен 1.273 - # успешный разбор 1.274 - $Script_Files{$file}->{tell} = $last_cl{raw_end}; 1.275 - 1.276 + if (defined($last_cl{time})) { 1.277 + print STDERR "push id=".$last_cl{time}."\n"; 1.278 + push @Command_Lines, \%last_cl; 1.279 + # Сохранение позиции в файле, до которой выполнен 1.280 + # успешный разбор 1.281 + $Script_Files{$file}->{tell} = $last_cl{raw_end}; 1.282 + } 1.283 next; 1.284 - 1.285 } 1.286 1.287 # Иначе, это строка вывода 1.288 @@ -583,7 +603,7 @@ 1.289 } 1.290 else 1.291 { 1.292 - if (!$skip_info) { 1.293 + if (!$skip_info && defined($cl{last_command})) { 1.294 print "($cl{last_command})"; 1.295 $skip_info = 1; 1.296 } 1.297 @@ -747,6 +767,7 @@ 1.298 err 1.299 last_command 1.300 history 1.301 + nonce 1.302 )) { 1.303 next unless defined($cl->{"$element"}); 1.304 print OUT "<$element>".$cl->{$element}."</$element>\n"; 1.305 @@ -882,9 +903,6 @@ 1.306 if (open (CACHE, ">", $Config{"cache"})) { 1.307 close(CACHE); 1.308 }; 1.309 - for my $lab_log (split (/\s+/, $Config{"diffs"} || $Config{"input"})) { 1.310 - load_diff_files($lab_log); 1.311 - } 1.312 load_command_lines($Config{"input"}, $Config{"input_mask"}); 1.313 sort_command_lines; 1.314 #process_command_lines; 1.315 @@ -941,9 +959,6 @@ 1.316 while (not $Killed) { 1.317 @Command_Lines = (); 1.318 @Command_Lines_Index = (); 1.319 - for my $lab_log (split (/\s+/, $Config{"diffs"} || $Config{"input"})) { 1.320 - load_diff_files($lab_log); 1.321 - } 1.322 load_cache_stat(); 1.323 load_command_lines($Config{"input"}, $Config{"input_mask"}); 1.324 if (@Command_Lines) {