| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
zrun - automatically uncompress arguments to command |
| 6 |
|
| 7 |
=head1 SYNOPSIS |
| 8 |
|
| 9 |
zrun command file.gz [...] |
| 10 |
|
| 11 |
=head1 DESCRIPTION |
| 12 |
|
| 13 |
Prefixing a shell command with "zrun" causes any compressed files that are |
| 14 |
arguments of the command to be transparently uncompressed to temp files |
| 15 |
(not pipes) and the uncompressed files fed to the command. |
| 16 |
|
| 17 |
This is a quick way to run a command that does not itself support |
| 18 |
compressed files, without manually uncompressing the files. |
| 19 |
|
| 20 |
The following compression types are supported: gz bz2 Z xz lzma lzo zstd |
| 21 |
|
| 22 |
If zrun is linked to some name beginning with z, like zprog, and the link is |
| 23 |
executed, this is equivalent to executing "zrun prog". |
| 24 |
|
| 25 |
=head1 BUGS |
| 26 |
|
| 27 |
Modifications to the uncompressed temporary file are not fed back into the |
| 28 |
input file, so using this as a quick way to make an editor support |
| 29 |
compressed files won't work. |
| 30 |
|
| 31 |
=head1 AUTHOR |
| 32 |
|
| 33 |
Copyright 2006 by Chung-chieh Shan <[email protected]> |
| 34 |
|
| 35 |
=cut |
| 36 |
|
| 37 |
use warnings; |
| 38 |
use strict; |
| 39 |
use IO::Handle; |
| 40 |
use File::Spec; |
| 41 |
use File::Temp qw{tempfile}; |
| 42 |
|
| 43 |
my $program; |
| 44 |
|
| 45 |
if ($0 =~ m{(?:^|/)z([^/]+)$} && $1 ne 'run') { |
| 46 |
$program = $1; |
| 47 |
if (! @ARGV) { |
| 48 |
die "Usage: z$1 <args>\nEquivalent to: zrun $1 <args>\n"; |
| 49 |
} |
| 50 |
} |
| 51 |
else { |
| 52 |
$program = shift; |
| 53 |
if (! @ARGV) { |
| 54 |
die "Usage: zrun <command> <args>\n"; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
my @argument; |
| 59 |
my %child; |
| 60 |
foreach my $argument (@ARGV) { |
| 61 |
if ($argument =~ m{^(.*/)?([^/]*)\.(gz|Z|bz2|xz|lzo|lzma|zst)$}s) { |
| 62 |
my $suffix = "-$2"; |
| 63 |
my @preprocess = $3 eq "bz2" ? qw(bzip2 -d -c) : |
| 64 |
$3 eq "xz" ? qw(xz -d -c) : |
| 65 |
$3 eq "lzo" ? qw(lzop -d -c) : |
| 66 |
$3 eq "lzma" ? qw(lzma -d -c) : |
| 67 |
$3 eq "zst" ? qw(zstd -d -c) : qw(gzip -d -c); |
| 68 |
|
| 69 |
my ($fh, $tmpname) = tempfile(SUFFIX => $suffix, |
| 70 |
DIR => File::Spec->tmpdir, UNLINK => 1) |
| 71 |
or die "zrun: cannot create temporary file: $!\n"; |
| 72 |
|
| 73 |
if (my $child = fork) { |
| 74 |
$child{$child} = $argument; |
| 75 |
$argument = $tmpname; |
| 76 |
} |
| 77 |
elsif (defined $child) { |
| 78 |
STDOUT->fdopen($fh, "w"); |
| 79 |
exec @preprocess, $argument; |
| 80 |
} |
| 81 |
else { |
| 82 |
die "zrun: cannot fork to handle $argument: $!\n"; |
| 83 |
} |
| 84 |
} |
| 85 |
push @argument, $argument; |
| 86 |
} |
| 87 |
|
| 88 |
while (%child and (my $pid = wait) != -1) { |
| 89 |
if (defined(my $argument = delete $child{$pid})) { |
| 90 |
if ($? & 255) { |
| 91 |
die "zrun: preprocessing for $argument terminated abnormally: $?\n"; |
| 92 |
} |
| 93 |
elsif (my $code = $? >> 8) { |
| 94 |
die "zrun: preprocessing for $argument terminated with code $code\n"; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
my $status = system $program ($program, @argument); |
| 100 |
if ($status & 255) { |
| 101 |
die "zrun: $program terminated abnormally: $?\n"; |
| 102 |
} |
| 103 |
else { |
| 104 |
my $code = $? >> 8; |
| 105 |
exit $code; |
| 106 |
} |