X-Git-Url: https://git.donarmstrong.com/?p=bin.git;a=blobdiff_plain;f=postfix_grep;h=b10e5be4675ac6844cd228920a4738a1dae7a225;hp=da659bcc21b3f6217eb631a4fe349fa2e7512391;hb=HEAD;hpb=76dc2e201f8a01f088a4476897fc0a9c3d7326a2 diff --git a/postfix_grep b/postfix_grep index da659bc..b10e5be 100755 --- a/postfix_grep +++ b/postfix_grep @@ -72,8 +72,6 @@ GetOptions(\%options, pod2usage() if $options{help}; pod2usage({verbose=>2}) if $options{man}; -use IO::Uncompress::Gunzip; - $DEBUG = $options{debug}; if (not exists $options{regex}) { @@ -102,12 +100,36 @@ if (not @ARGV) { push @ARGV,undef; } +sub open_compressed_file { + my ($file,$encoding) = @_; + $encoding //= ':encoding(UTF-8)'; + my $fh; + if (not defined $file) { + $fh = \*STDIN; + binmode($fh,':encoding(UTF-8)'); + return $fh; + } + my $mode = "<$encoding"; + my @opts; + if ($file =~ /\.gz$/) { + $mode = "-|$encoding"; + push @opts,'gzip','-dc'; + } + if ($file =~ /\.xz$/) { + $mode = "-|$encoding"; + push @opts,'xz','-dc'; + } + if ($file =~ /\.bz2$/) { + $mode = "-|$encoding"; + push @opts,'bzip2','-dc'; + } + open($fh,$mode,@opts,$file); + return $fh; +} + my %postfix_ids; for my $file (@ARGV) { - my $fh = IO::Uncompress::Gunzip->new(defined $file ? $file:\*STDIN, - MultiStream => 1, - Transparent => 1,) - or die "IO::Uncompress::Gunzip failed: $IO::Uncompress::Gunzip::GunzipError"; + my $fh = open_compressed_file($file); while (<$fh>) { chomp; my $line = $_;