Documente Academic
Documente Profesional
Documente Cultură
/usr/bin/perl -w
use strict;
use warnings;
use Data::Dumper;
my @regexes;
my $config_file = "$home_dir/.mysmartpager";
my $tee_file = "$home_dir/.mysqltee";
if ( -e "$config_file" ) {
$is_config_present = 1;
open( CONFIG, "<$config_file" );
while ( <CONFIG> ) {
chomp; # no newline
s/#.*//xm; # no comments
s/^\s+//xm; # no leading white
s/\s+$//xm; # no trailing white
next unless length; # anything left?
my ( $command, $regex )
= split( /\=/, $_, 2 ); # command = regex model
$command =~ s/\s+$//xm;
$regex =~ s/^\s+//xm;
if ( $regex ) {
push @regexes,
{ 'regex' => $regex, 'command' => $command };
}
}
}
my $input = '';
while ( <> ) {
$input .= $_;
}
# This is to fool mysql client that smartpager has finished the job and will
resume to
# tee-ing the data to the $tee_file.
exit if fork;
# Child process should sleep for a small time giving mysql client enough time to
write to
# tee_file
sleep(1);
print "\n";
# Pass and test the command against all the regular expressions one by one and
stop when it matches
if ( $is_config_present ) {
foreach my $regex ( @regexes ) {
if ( $mysql_command =~ /$regex->{'regex'}/xmi ) {
$pager_command = $regex->{'command'};
last;
}
}
}
eval {
local $SIG{'__WARN__'};
system "$pager_command /tmp/mysql_pager.log 2>/dev/null";
};