lilalo
view lm-report @ 0:18b21f6918f0
Initial revision
| author | devi | 
|---|---|
| date | Sun May 22 16:29:55 2005 +0300 (2005-05-22) | 
| parents | |
| children | a0f30dd46d1a | 
 line source
     1 #!/usr/bin/perl -w
     3 #
     4 # (c) Igor Chubin, imchubin@mail.ru, 2004
     5 #
     7 use strict;
     8 use Getopt::Long;
     9 use Term::VT102;
    10 use Text::Iconv;
    11 use Data::Dumper;
    13 our $Config_File = "labmaker.conf";
    14 our %Config = (
    15 		"skip_empty" 			=> 	"yes",
    16 		"skip_interrupted" 		=>	"no",
    17 		"skip_wrong" 			=>	"no",
    18 		"editors"			=>	["vi", "pico", "ee", "vim"],
    19 		"pagers"			=>	["more", "less", "zmore", "zless", "info", 
    20 							"man", "mc", "trafshow", "screen", "cfdisk",
    21 							"trafshow-bsd", "yes", "lynx", "links", "centericq"
    22 							],
    23 		"terminal"			=>	["mc"],
    24 		"suppress_editors"		=>	"yes",
    25 		"suppress_pagers"		=>	"yes",
    26 		"suppress_terminal"		=>	"yes",
    28 		"terminal_width"		=> 	100,
    29 		"terminal_height"		=> 	100,
    30 		"verbose"			=>	"yes",
    32 		"head_lines"			=> 	5,
    33 		"tail_lines"			=>	5,
    34 		"skip_text"			=>	"...",
    35 		"show_time"			=>	"yes",
    36 		"show_diffs"			=>	"yes",
    37 		"show_comments"			=>	"yes",
    39 		"input"				=>	"/root/.labmaker",
    40 		"diffs"				=>	"",
    41 		"input_mask"			=>	"*.script",
    42 		"encoding"			=> 	"utf-8",
    44 		"output"			=>	"/var/www/lm/reportINDEX.html",
    45 		#"output"			=>	"report.xml",
    46 		"output_mask"			=>	"INDEX",
    47 		"output_format"			=>	"html",
    49 		"signature"			=>	"#lm:",
    50 		"from"				=>	"",
    51 		"to"				=>	"",
    52 		"lab"				=>	"",
    53 		"keywords"			=>	"linux command",
    54 		"files_keywords"		=>	"linux file",
    56 		comment_width			=>	"300",
    57 		time_width			=>	"60",
    59 		);
    61 our @Command_Lines;
    62 our @Command_Lines_Index;
    63 our @Diffs;
    65 our %Commands_Stat;		# Statistics about commands usage
    66 our %Files_Stat;		# Statistics about commands usage
    68 our %Search_Machines = (
    69 		"google" => 	{ 	"query" => 	"http://www.google.com/search?q=" ,
    70 					"icon" 	=> 	"google.ico" },
    71 		"freebsd" => 	{ 	"query" => 	"http://www.freebsd.org/cgi/man.cgi?query=",
    72 					"icon"	=>	"freebsd.ico" },
    73 		"linux"  => 	{ 	"query" => 	"http://man.he.net/?topic=",
    74 					"icon"	=>	"linux.ico"},
    75 		"opennet"  => 	{ 	"query" => 	"http://www.opennet.ru/search.shtml?words=",
    76 					"icon"	=>	"opennet.ico"},
    77 		"local" => 	{ 	"query" => 	"http://www.freebsd.org/cgi/man.cgi?query=",
    78 					"icon"	=>	"freebsd.ico" },
    80 	);
    82 our %Elements_Visibility = (
    83 		"note"		=>	"замечания",
    84 		"diff"		=>	"редактор",
    85 		"time"		=>	"время",
    86 		"ttychange" 	=>	"терминал",
    87 		"wrong_output wrong_cline wrong_root_output wrong_root_cline" 
    88 				=>	"команды с ошибками",
    89 		"interrupted_output interrupted_cline interrupted_root_output interrupted_root_cline" 
    90 				=>	"прерванные команды",
    91 		"tab_completion_output tab_completion_cline"	
    92 				=> 	"продолжение с помощью tab"
    93 );
    96 sub load_diff_files
    97 {
    98 	my @pathes = @_;
   100 	for my $path (@pathes) {
   101 		my $template = "*.diff";
   102 		my @files = <$path/$template>;
   103 		my $i=0;
   104 		for my $file (@files) {
   105 			my %diff;
   107 			$diff{"path"}=$path;
   108 			$diff{"uid"}="SET THIS";
   110 # Сейчас UID определяется из названия каталога
   111 # откуда берутся diff-файлы
   112 # Это неправильно
   113 #
   114 # ВАРИАНТ:
   115 # К файлам жураналам должны прилагаться ситемны файлы, 
   116 # мз которых и будет определяться соответствие 
   117 # имён пользователей их uid'ам
   118 #
   119 			$diff{"uid"} = 0 if $path =~ m@/root/@;	
   121 			$diff{"bind_to"}="";
   122 			$diff{"time_range"}=-1;
   124 			next if not $file=~m@/(D?[0-9][0-9]?[0-9]?)[^/]*?([0-9]*):([0-9]*):?([0-9]*)@;
   125 			$diff{"day"}=$1 || "";
   126 			$diff{"hour"}=$2;
   127 			$diff{"min"}=$3;
   128 			$diff{"sec"}=$4 || 0;
   130 			$diff{"index"}=$i;
   132 			print "diff loaded: $diff{day} $diff{hour}:$diff{min}:$diff{sec}\n";
   134 			local $/;
   135 			open (F, "$file")
   136 				or return "Can't open file $file ($_[0]) for reading";
   137 			my $text = <F>;
   138 			if ($Config{"encoding"} && $Config{"encoding"} !~ /^utf-8$/i) {
   139 				my $converter = Text::Iconv->new($Config{"encoding"}, "utf-8");
   140 				$text = $converter->convert($text);
   141 			}
   142 			close(F);	
   143 			$diff{"text"}=$text;
   144 			#print "$file loaded ($diff{day})\n";
   146 			push @Diffs, \%diff;
   147 			$i++;
   148 		}
   149 	}	
   150 }
   153 sub bind_diff
   154 {
   155 #	my $path = shift;
   156 #	my $pid = shift;
   157 #	my $day = shift;
   158 #	my $lab = shift;
   160 	print "Trying to bind diff...\n";
   162 	my $cl = shift;
   163 	my $hour = $cl->{"hour"};
   164 	my $min = $cl->{"min"};
   165 	my $sec = $cl->{"sec"};
   167 	my $min_dt = 10000;
   169 	for my $diff (@Diffs) {
   170 			# Check here date, time and user
   171 			next if ($diff->{"day"} && $cl->{"day"} && ($cl->{"day"} ne $diff->{"day"}));
   172 			#next if (!$diff->{"uid"} && $cl->{"euid"} != $diff->{"uid"});
   174 			my $dt=($diff->{"hour"}-$hour)*3600 +($diff->{"min"}-$min)*60 + ($diff->{"sec"}-$sec);
   175 			if ($dt >0  && $dt < $min_dt && ($diff->{"time_range"} <0 || $dt < $diff->{"time_range"})) {
   176 				print "Approppriate diff found: dt=$dt\n";
   177 				if ($diff->{"bind_to"}) {
   178 					undef $diff->{"bind_to"}->{"diff"};
   179 				};
   180 				$diff->{"time_range"}=$dt;
   181 				$diff->{"bind_to"}=$cl;
   183 				$cl->{"diff"} = $diff->{"index"};
   184 				$min_dt = $dt;	
   185 			}
   187 	}
   188 }
   191 sub extract_from_cline
   192 # Разобрать командную строку $_[1] и возвратить хэш, содержащий 
   193 # номер первого появление команды в строке:
   194 # 	команда => первая позиция
   195 {
   196 	my $what = $_[0];
   197 	my $cline = $_[1];
   198 	my @lists = split /\;/, $cline;
   201 	my @commands = ();
   202 	for my $list (@lists) {
   203 		push @commands, split /\|/, $list;
   204 	}
   206 	my %commands;
   207 	my %files;
   208 	my $i=0;
   209 	for my $command (@commands) {
   210 		$command =~ /\s*(\S+)\s*(.*)/;
   211 		if ($1 && $1 eq "sudo" ) {
   212 			$commands{"$1"}=$i++;
   213 			$command =~ s/\s*sudo\s+//;
   214 		}
   215 		$command =~ /\s*(\S+)\s*(.*)/;
   216 		if ($1 && !defined $commands{"$1"}) {
   217 				$commands{"$1"}=$i++;
   218 		};	
   219 		if ($2) {
   220 			my $args = $2;
   221 			my @args = split (/\s+/, $args);
   222 			for my $a (@args) {
   223 				$files{"$a"}=$i++
   224 					if !defined $files{"$a"};
   225 			};	
   228 		}
   229 	}
   231 	if ($what eq "commands") {
   232 		return %commands;
   233 	} else {
   234 		return %files;
   235 	}
   237 }
   239 sub load_command_lines
   240 {
   241 	my $lab_scripts_path = $_[0];
   242 	my $lab_scripts_mask = $_[1];
   244 	my $cline_re_base = qq'
   245 			(?:\\^?([0-9]*C?))					# exitcode
   246 			(?:_([0-9]+)_)?				# uid
   247 			(?:_([0-9]+)_)				# pid
   248 			(...?)					# day
   249 			(..)					# lab
   250 			\\s					# space separator
   251 			([0-9][0-9]):([0-9][0-9]):([0-9][0-9])	# time
   252 			.\\[50D.\\[K				# killing symbols
   253 			(.*?([\$\#]\\s?))			# prompt
   254 			(.*)					# command line
   255 			';
   256 	#my $cline_re = qr/$cline_re_base(?:$cline_re_base|$)/x;
   257 	#my $cline_re = qr/(?:$cline_re_base)*$cline_re_base$/x;
   258 	my $cline_re = qr/$cline_re_base/sx;
   259 	my $cline_re1 = qr/$cline_re_base\x0D/sx;
   260 	my $cline_re2 = qr/$cline_re_base$/sx;
   262 	my $vt = Term::VT102->new (	'cols' => $Config{"terminal_width"}, 
   263 					'rows' => $Config{"terminal_height"});
   264 	my $cline_vt = Term::VT102->new ('cols' => $Config{"terminal_width"}, 
   265 					'rows' => $Config{"terminal_height"});
   267 	my $converter = Text::Iconv->new($Config{"encoding"}, "utf-8")
   268 		if ($Config{"encoding"} && $Config{"encoding"} !~ /^utf-8$/i);
   270 	print "Loading lm-scripts...\n" if $Config{"verbose"} =~ /y/;
   272 	my @lab_scripts = <$lab_scripts_path/$lab_scripts_mask>;
   273 	my $file;
   274 	my $files_number = $#lab_scripts;
   275 	my $ii = 0;
   276 	my $skip_info;
   278 	my $commandlines_loaded =0;
   279 	my $commandlines_processed =0;
   281 	for $file (@lab_scripts){
   282 		#printf "\t%i %3.2f\n", $ii, (100*$ii++/$files_number) if $Config{"verbose"} =~ /y/;
   284 		open (FILE, "$file");
   285 		binmode FILE;
   286 		$file =~ m@.*/(.*?)-.*@;
   288 		my $tty = $1;
   289 		my $first_pass = 1;
   290 		my %cl;
   291 		my $last_output_length=0;
   292 		while (<FILE>) {
   293 			$commandlines_processed++;
   294 				# time
   296 			if (/[0-9][0-9]:[0-9][0-9]:[0-9][0-9].\[[0-9][0-9]D.\[K/ && m/$cline_re/) {
   297 				s/.*\x0d(?!\x0a)//;
   298 		#		print "!!!",$_,"!!!\n";
   299 			#	next;
   300 			#	while (m/$cline_re1/gs) {
   301 			#	}
   302 				m/$cline_re2/gs;
   304 				$commandlines_loaded++;
   305 				$last_output_length=0;
   307 				# Previous command
   308 				my %last_cl = %cl;
   309 				my $err = $1 || "";
   311 				# Parse new command 
   312 				$cl{"uid"} = $2;
   313 				$cl{"euid"} = $cl{"uid"};	# Если в команде обнаружится sudo, euid поменяем на 0
   314 				$cl{"pid"} = $3;
   315 				$cl{"day"} = $4;
   316 				$cl{"lab"} = $5;
   317 				$cl{"hour"} = $6;
   318 				$cl{"min"} = $7;
   319 				$cl{"sec"} = $8;
   320 				$cl{"fullprompt"} = $9;
   321 				$cl{"prompt"} = $10;
   322 				$cl{"raw_cline"} = $11;	
   324 				$cl{"err"} = 0;
   325 				$cl{"output"} = "";
   326 				$cl{"tty"} = $tty;
   328 				$cline_vt->process($cl{"raw_cline"}."\n");
   329 				$cl{"cline"} = $cline_vt->row_plaintext (1);
   330 				$cl{"cline"} =~ s/\s*$//;
   331 				$cline_vt->reset();
   333 				my %commands = extract_from_cline("commands", $cl{"cline"});
   334 				$cl{"euid"}=0 if defined $commands{"sudo"};
   335 				my @comms = sort { $commands{$a} cmp $commands{$b} } keys %commands; 
   336 				$cl{"last_command"} = $comms[$#comms] || ""; 
   338 				if (
   339 				$Config{"suppress_editors"} =~ /^y/i 
   340 					&& grep ($_ eq $cl{"last_command"}, @{$Config{"editors"}}) ||
   341 				$Config{"suppress_pagers"}  =~ /^y/i 
   342 					&& grep ($_ eq $cl{"last_command"}, @{$Config{"pagers"}}) ||
   343 				$Config{"suppress_terminal"}=~ /^y/i 
   344 					&& grep ($_ eq $cl{"last_command"}, @{$Config{"terminal"}})
   345 				) {
   346 					$cl{"suppress_output"} = "1";
   347 				}
   348 				else {
   349 					$cl{"suppress_output"} = "0";
   351 				}
   352 				$skip_info = 0;
   355 				print " ",$cl{"last_command"};
   357 				# Processing previous command line
   358 				if ($first_pass) {
   359 					$first_pass = 0;
   360 					next;
   361 				}
   363 				# Error code
   364 				$last_cl{"err"}=$err;
   365 				$last_cl{"err"}=130 if $err eq "^C";
   367 				if (grep ($_ eq $last_cl{"last_command"}, @{$Config{"editors"}})) {
   368 					bind_diff(\%last_cl);
   369 				}
   371 				# Output
   372 				if (!$last_cl{"suppress_output"} || $last_cl{"err"}) {
   373 					for (my $i=0; $i<$Config{"terminal_height"}; $i++) {
   374 						my $line= $vt->row_plaintext($i);
   375 						next if !defined ($line) || $line =~ /^\s*$/;
   376 						$line =~ s/\s*$//;
   377 						$last_cl{"output"} .= $line."\n";
   378 					}
   379 				}
   380 				else {
   381 					$last_cl{"output"}= "";
   382 				}
   384 				$vt->reset();
   387 				# Classifying the command line
   390 				# Save 
   391 				if (!$Config{"lab"} || $cl{"lab"} eq $Config{"lab"}) {
   392 					# Changing encoding 
   393 					for (keys %last_cl) {
   394 						$last_cl{$_} = $converter->convert($last_cl{$_})
   395 							if ($Config{"encoding"} && 
   396 							$Config{"encoding"} !~ /^utf-8$/i);
   397 					}
   398 					push @Command_Lines, \%last_cl;	
   399 				}	
   400 				next;
   401 			}
   402 			$last_output_length+=length($_);
   403 			#if (!$cl{"suppress_output"} || $last_output_length < 5000) {
   404 			if ($last_output_length < 5000) {
   405 				#print "(",length($_),")" if (length($_) > 2000) ;
   406 				$vt->process("$_"."\n") 
   407 			}
   408 			else
   409 			{
   410 				if (!$skip_info) {
   411 					print "($cl{last_command})";
   412 					$skip_info = 1;
   413 				}
   414 			}
   415 		}	
   416 		close(FILE);
   418 	}
   419 	if ($Config{"verbose"} =~ /y/) {
   420 		print "...finished." ;
   421 		print "Lines loaded: $commandlines_processed\n";
   422 		print "Command lines: $commandlines_loaded\n";
   423 	}
   424 }
   426 sub search_by
   427 {
   428 	my $sm = shift;
   429 	my $topic = shift;
   430 	$topic =~ s/ /+/;
   432 	return "<a href='".	$Search_Machines{$sm}->{"query"}."$topic'><img width='16' height='16' src='".
   433 				$Search_Machines{$sm}->{"icon"}."' border='0'/></a>";
   434 }
   436 sub make_comment
   437 {
   438 	my $commands = $_[0];
   439 	my $files = $_[1];
   440 	chomp $commands;
   441 	chomp $files;
   442 	return if (!$commands && !$files);
   444 	my $comment=""; 
   446 	# Commands
   447 	for my $command (split /\s+/,$commands) {
   448 		$command =~ s/'//g;
   449 		my $description="";
   450 		eval { $description=`mywi-client '$command'`; } ;
   451 		$description = join ("<br>\n", grep(/\([18]\)/, split(/\n/, $description)));
   452 		$description =~ s/.*?-//;
   453 		next if $description =~ /^\s*$/; 
   455 		my $query=$command." ".$Config{"keywords"};
   456 		$query =~ s/\ /+/g;
   457 		my $search= 	search_by("opennet",$query).
   458 				search_by("local",$command).
   459 				search_by("google",$query);
   461 		$comment .=     "<tr><td class='note_title'>$command</td>".
   462 				"<td class='note_search'>$search</td>".
   463 				"</tr><tr><td width='100%' colspan='2' class='note_text'>".
   464 				"$description</td></tr><tr/>";
   465 	}
   467 	# Files
   468 	for my $file (split /\s+/,$files) {
   469 		$file =~ s@.*/@@;
   470 		$file =~ s/'//g;
   471 		next if $file =~ /^\s*$/;
   472 		next if $file =~ /^-/;
   474 		my $description=`mywi '$file'`;
   475 		$description = join ("<br>\n", grep(/\(5\)/, split(/\n/, $description)));
   476 		next if $description =~ /^\s*$/; 
   478 		my $query=$file." ".$Config{"files_keywords"};
   479 		$query =~ s/\ /+/g;
   480 		my $search= 	search_by("opennet",$query).
   481 				search_by("local",$file).
   482 				search_by("google",$query);
   484 		$comment .=     "<tr><td class='note_title'>$file</td>".
   485 				"<td class='note_search'>$search</td>".
   486 				"</tr><tr><td width='100%' colspan='2' class='note_text'>".
   487 				"$description</td></tr><tr/>";
   488 	}
   491 	return $comment;
   492 }
   494 sub printq
   495 {
   496 	my $TO = shift;
   497 	my $text = join "", @_;
   498 	$text =~ s/&/&/g;
   499 	$text =~ s/</</g;
   500 	$text =~ s/>/>/g;
   501 	print $TO $text;
   502 }
   505 sub sort_command_lines
   506 {
   507 	print "Sorting command lines...\n" if $Config{"verbose"} =~ /y/;
   509 	# Sort Command_Lines
   510 	# Write Command_Lines to Command_Lines_Index
   512 	my @index;
   513 	for (my $i=0;$i<=$#Command_Lines;$i++) {
   514 		$index[$i]=$i;
   515 	}
   517 	@Command_Lines_Index = sort {
   518 		$Command_Lines[$index[$a]]->{"day"} cmp $Command_Lines[$index[$b]]->{"day"} ||
   519 		$Command_Lines[$index[$a]]->{"hour"} <=> $Command_Lines[$index[$b]]->{"hour"} ||
   520 		$Command_Lines[$index[$a]]->{"min"} <=> $Command_Lines[$index[$b]]->{"min"} ||
   521 		$Command_Lines[$index[$a]]->{"sec"} <=> $Command_Lines[$index[$b]]->{"sec"}
   522 	} @index;
   524 	print "...finished\n" if $Config{"verbose"} =~ /y/;
   526 }
   528 sub process_command_lines
   529 {
   530 	my $lab_scripts_path = $_[0];
   532 	for my $i (@Command_Lines_Index) {
   534 		my $cl = \$Command_Lines[$i];
   535 		@{${$cl}->{"new_commands"}} =();
   536 		@{${$cl}->{"new_files"}} =();
   537 		$$cl->{"class"} = ""; 
   539 		if ($$cl->{"err"}) {
   540 			$$cl->{"class"}="wrong";
   541 			$$cl->{"class"}="interrupted"
   542 				if ($$cl->{"err"} eq 130);
   543 		}	
   544 		if (!$$cl->{"euid"}) {
   545 			$$cl->{"class"}.="_root";
   546 		}
   548 #tab#		my @tab_words=split /\s+/, $$cl->{"output"};
   549 #tab#		my $last_word= $$cl->{"cline"} =~ /(\S*)$/;
   550 #tab#		$last_word =~ s@.*/@@;
   551 #tab#		my $this_is_tab=1;
   552 #tab#
   553 #tab#		if ($last_word && @tab_words >2) {
   554 #tab#			for my $tab_words (@tab_words) {
   555 #tab#				if ($tab_words !~ /^$last_word/) {
   556 #tab#					$this_is_tab=0;
   557 #tab#					last;
   558 #tab#				}
   559 #tab#			}
   560 #tab#		}	
   561 #tab#		$$cl->{"class"}="tab" if $this_is_tab;
   564 		if ( !$$cl->{"err"}) {
   565 			# Command does not contain mistakes
   567 			my %commands = extract_from_cline("commands", ${$cl}->{"cline"});
   568 			my %files = extract_from_cline("files", ${$cl}->{"cline"});
   570 			# Searching for new commands only
   571 			for my $command (keys  %commands) {
   572 				if (!defined $Commands_Stat{$command}) {
   573 					push @{$$cl->{new_commands}}, $command;
   574 				}	
   575 				$Commands_Stat{$command}++;
   576 			}
   578 			for my $file (keys  %files) {
   579 				if (!defined $Files_Stat{$file}) {
   580 					push @{$$cl->{new_files}}, $file;
   581 				}	
   582 				$Files_Stat{$file}++;
   583 			}
   584 		}	
   585 	}	
   587 }
   589 sub print_command_lines
   590 {
   591 	my $output_filename=$_[0];
   592 	my $format = $Config{"output_format"};
   594 	open(OUT, ">", $output_filename)
   595 		or die "Can't open $output_filename for writing\n";
   599 	if ($format eq "html") {
   600 	# vvvv HTML Header 
   601 		print OUT <<HEADER;
   602 		<html>
   603 		<head>
   604 		<meta content='text/html; charset=utf-8' http-equiv='Content-Type' />
   605 		<link rel='stylesheet' href='labmaker.css' type='text/css'/>
   606 		</head>
   607 		<body>
   608 		<script>
   609 		function getElementsByClassName(Class_Name)
   610 		{
   611 			var Result=new Array();
   612 			var All_Elements=document.all || document.getElementsByTagName('*');
   613 			for (i=0; i<All_Elements.length; i++)
   614 				if (All_Elements[i].className==Class_Name)
   615 			Result.push(All_Elements[i]);
   616 			return Result;
   617 		}
   618 		function ShowHide (name)
   619 		{
   620 			elements=getElementsByClassName(name);
   621 			for(i=0; i<elements.length; i++)
   622 				if (elements[i].style.display == "none")
   623 					elements[i].style.display = "";
   624 				else
   625 					elements[i].style.display = "none";
   626 				//if (elements[i].style.visibility == "hidden")
   627 				//	elements[i].style.visibility = "visible";
   628 				//else
   629 				//	elements[i].style.visibility = "hidden";
   630 		}
   631 		function filter_by_output(text)
   632 		{
   634 			var jjj=0;
   636 			elements=getElementsByClassName('command');
   637 			for(i=0; i<elements.length; i++) {
   638 				subelems = elements[i].getElementsByTagName('pre');
   639 				for(j=0; j<subelems.length; j++) {
   640 					if (subelems[j].className = 'output') {
   641 						var str = new String(subelems[j].nodeValue);
   642 						if (jjj != 1) { 
   643 							alert(str);
   644 							jjj=1;
   645 						}
   646 						if (str.indexOf(text) >0) 
   647 							subelems[j].style.display = "none";
   648 						else
   649 							subelems[j].style.display = "";
   651 					}
   653 				}
   654 			}		
   656 		}
   657 		</script>
   658 HEADER
   659 		print OUT "<table class='visibility_form'><tr><td><form>\n";
   660 		for my $element (keys %Elements_Visibility)
   661 		{
   662 			my @e = split /\s+/, $element;
   663 			my $showhide = join "", map { "ShowHide('$_');" } @e ;
   664 			print OUT "<input type='checkbox' name='$e[0]' onclick=\"$showhide\" checked>",
   665 					$Elements_Visibility{$element},
   666 					"</input><br>\n";
   667 		}
   668 		#print OUT "<input type='text' size='10' name=\"by_command\"/>".
   669 		#"<input type='button' value='фильтр по командам' onclick=\"filter_by_command()\"/> <br>\n";
   670 		#print OUT "<input type='text' size='10' name=\"by_output\"/>".
   671 		#"<input type='button' value='фильтр по результату' ".
   672 		#"onclick=\"filter_by_output(this.form.by_output.value)\"/> <br>\n";
   674 		print OUT "</form></td></tr></table>\n";
   675 		print OUT "<table width='100%'>\n";
   676 	# ^^^^ HTML Header 
   677 	}
   678 	else {
   679 		# XML Header
   680 		print OUT "<script>\n"
   681 	}
   683 	my $cl;
   684 	my $last_tty="";
   685 	my $in_range=0;
   686 	for my $i (@Command_Lines_Index) {
   688 		$cl = $Command_Lines[$i];
   690 		if ($Config{"from"} && $cl->{"cline"} =~ /$Config{"signature"}\s*$Config{"from"}/) {
   691 			$in_range=1;
   692 			next;
   693 		}
   694 		if ($Config{"to"} && $cl->{"cline"} =~ /$Config{"signature"}\s*$Config{"to"}/) {
   695 			$in_range=0;
   696 			next;
   697 		}
   698 		next if ($Config{"from"} && $Config{"to"} && !$in_range) 
   699 			||
   700 		    	($Config{"skip_empty"} =~ /^y/i && $cl->{"cline"} =~ /^\s*$/ )
   701 			||
   702 			($Config{"skip_wrong"} =~ /^y/i && $cl->{"err"} != 0)
   703 			||
   704 			($Config{"skip_interrupted"} =~ /^y/i && $cl->{"err"} == 130);
   706 		my @new_commands=@{$cl->{"new_commands"}};
   707 		my @new_files=@{$cl->{"new_files"}};
   709 		my $cl_class="cline";
   710 		my $out_class="output";
   711 		if ($cl->{"class"}) {
   712 			$cl_class = $cl->{"class"}."_".$cl_class;
   713 			$out_class = $cl->{"class"}."_".$out_class;
   714 		}
   716 		my $output="";
   717 		if ($Config{"head_lines"} || $Config{"tail_lines"}) {
   718 			# Partialy output
   719 			my @lines = split '\n', $cl->{"output"};
   720 			# head
   721 			my $mark=1;
   722 			for (my $i=0; $i<= $#lines && $i < $Config{"head_lines"}; $i++) {
   723 				$output .= $lines[$i]."\n";
   724 			}
   725 			# tail
   726 			my $start=$#lines-$Config{"tail_lines"}+1;
   727 			if ($start < 0) {
   728 				$start=0;
   729 				$mark=0;
   730 			}	
   731 			if ($start < $Config{"head_lines"}) {
   732 				$start=$Config{"head_lines"};
   733 				$mark=0;
   734 			}	
   735 			$output .= $Config{"skip_text"}."\n" if $mark;
   736 			for (my $i=$start; $i<= $#lines; $i++) {
   737 				$output .= $lines[$i]."\n";
   738 			}
   739 		} 
   740 		else {
   741 			# Full output
   742 			$output .= $cl->{"output"};
   743 		}	
   744 		$output .= "^C\n" if ($cl->{"err"} eq "130");
   747 # Printing out
   749 		# <command>
   750 		print OUT $format eq "html" 
   751 				? "<tr class='command'>\n"
   752 				: "\n<action time='$cl->{hour}:$cl->{min}:$cl->{sec}' tty='$cl->{tty}'>\n";
   755 		if ($format eq "html") {
   756 			# CONSOLE CHANGE
   757 			if ( $last_tty ne $cl->{"tty"}) {
   758 				print OUT "<td colspan='6'><table><tr><td class='ttychange' width='140' align='center'>",$cl->{"tty"},"</td><td/></tr></table></td></tr><tr>";
   759 				$last_tty=$cl->{"tty"};
   760 			}
   762 			# TIME
   763 			if ($Config{"show_time"} =~ /^y/i) {
   764 				print OUT "<td valign='top' class='time' width='$Config{time_width}'><pre>",
   765 					$cl->{"hour"}, ":", $cl->{"min"}, ":", $cl->{"sec"},
   766 					"</td>";
   767 			} else {
   768 				print OUT "<td width='0'/>"
   769 			}
   770 		}	
   772 		# COMMAND
   775 		if ($format eq "html") {
   776 			print OUT "<td class='script'>\n";
   777 			print OUT "<pre class='$cl_class'>\n";
   778 			my $cline = $cl->{"cline"};
   779 			$cline =~ s/\n//;
   780 			printq(\*OUT,$cl->{"prompt"},$cl->{"cline"});
   781 #			printq(\*OUT,"(sudo ".$cl->{"last_command"}.")\n") if !$cl->{"euid"};
   782 			print OUT "</pre>\n";
   783 		} 
   784 		else {
   785 			print OUT "<line class='$cl_class'>\n";
   786 			print OUT "<prompt>";
   787 			printq(\*OUT,$cl->{"prompt"});
   788 			print OUT "</prompt>";
   789 			print OUT "<command>";
   790 			printq(\*OUT,$cl->{"cline"});
   791 			print OUT "</command>";
   792 			print OUT "\n</line>\n";
   793 		}
   795 		my $last_command = $cl->{"last_command"};
   796 		if (!( 
   797 		$Config{"suppress_editors"} =~ /^y/i && grep ($_ eq $last_command, @{$Config{"editors"}}) ||
   798 		$Config{"suppress_pagers"}  =~ /^y/i && grep ($_ eq $last_command, @{$Config{"pagers"}}) ||
   799 		$Config{"suppress_terminal"}=~ /^y/i && grep ($_ eq $last_command, @{$Config{"terminal"}})
   800 			)) {
   802 			if ($format eq "html") {
   803 				print OUT "<pre class='$out_class'>";
   804 				printq(\*OUT,$output);
   805 				print OUT "</pre>\n";
   806 			} 
   807 			else {
   808 				print OUT "<output class='$out_class'>\n";
   809 				printq(\*OUT,$output);
   810 				print OUT "</output>\n";
   811 			}
   812 		}	
   814 		# DIFF
   815 		if ( $Config{"show_diffs"} =~ /^y/i && $cl->{"diff"}) {
   816 			if ($format eq "html") {
   817 				#print Dumper(%{$cl->{"diff"}});
   818 				print OUT "<table><tr><td width='5'/><td class='diff'><pre>";
   819 				printq(\*OUT,${$Diffs[$cl->{"diff"}]}{"text"});
   820 				print OUT "</pre></td></tr></table>";
   821 			}
   822 			else {
   823 				print OUT "<diff>\n";
   824 				printq(\*OUT,${$Diffs[$cl->{"diff"}]}{"text"});
   825 				print OUT "</diff>\n";
   826 			}
   827 		}
   829 		# COMMENT
   830 		if ( $Config{"show_comments"} =~ /^y/i) {
   831 			my $comment = make_comment(join(" ",@new_commands), join (" ",@new_files));
   832 			if ($comment) {
   833 				if ($format eq "html") {
   834 					print OUT "<table width='$Config{comment_width}'>".
   835 							"<tr><td width='5'/><td>";
   836 					print OUT "<table class='note' width='100%'>";
   837 					print OUT $comment;
   838 					print OUT "</table>\n";
   839 					print OUT "</td></tr></table>";
   840 				}
   841 			#	else {
   842 			#		print OUT "<comment>";
   843 			#		printq(\*OUT,$comment);
   844 			#		print OUT "</comment>";
   845 			#	}
   846 			}
   847 		}
   849 		if ($format eq "html") {
   850 			print OUT "</td>\n";
   851 			print OUT "</tr>\n";
   852 		}
   853 		else {
   854 			print OUT "</action>\n";
   855 		}
   857 	}
   858 	if ($format eq "html") {
   859 		print OUT "</table>\n";
   860 		print OUT "</body>\n";
   861 		print OUT "</html>\n";
   862 	} 
   863 	else {
   864 		print OUT "</script>\n";
   865 	}
   866 	close(OUT);
   867 }
   869 sub read_config_file
   870 {
   871 	my $config = $_[0];
   872 	my $filename = $_[1];
   873 	open(CONFIG, "$filename")
   874 		or return;
   875 	while (<CONFIG>) {
   876 		s/#.*//;
   877 		next if /^\s*$/;
   878 		my ($var, $val) =  split /\s*=\s*/, $_, 2;
   879 		$var =~ s/\s*//;
   880 		$config->{$var} = $val;
   881 	}
   882 	close(CONFIG);
   883 }
   885 $| = 1;
   887 my %file_config;
   888 my %argv_config;
   889 read_config_file(\%file_config, $Config_File);
   890 GetOptions(\%argv_config, map "$_=s", keys %Config);
   891 %Config = (%Config, %file_config, %argv_config);
   894 my $i=0;
   896 for my $lab_log (split (/\s+/, $Config{"diffs"} || $Config{"input"})) 
   897 {
   898 	load_diff_files($lab_log);
   899 }
   901 for my $lab_log (split /\s+/, $Config{"input"}) 
   902 {
   903 	my $tofile=$Config{"output"};
   904 	$tofile =~ s/$Config{"output_mask"}/$i/;
   905 	#load_diff_files($lab_log);
   906 	load_command_lines($lab_log, $Config{"input_mask"});
   907 	sort_command_lines;
   908  	process_command_lines($lab_log);
   909 	print_command_lines($tofile);
   910 	$i++;
   911 }
