5 zrun - automatically uncompress arguments to command
9 zrun command file.gz [...]
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.
17 This is a quick way to run a command that does not itself support
18 compressed files, without manually uncompressing the files.
20 The following compression types are supported: gz bz2 Z lzma lzo
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".
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.
33 Copyright 2006 by Chung-chieh Shan <ccshan@post.harvard.edu>
41 use File
::Temp
qw{tempfile
};
45 if ($0 =~ m{(?:^|/)z([^/]+)$} && $1 ne 'run') {
48 die "Usage: z$1 <args>\nEquivalent to: zrun $1 <args>\n";
54 die "Usage: zrun <command> <args>\n";
60 foreach my $argument (@ARGV) {
61 if ($argument =~ m{^(.*/)?([^/]*)\.(gz|Z|bz2|lzo|lzma)$}s) {
63 my @preprocess = $3 eq "bz2" ?
qw(bzip2 -d -c) :
64 $3 eq "lzo" ?
qw(lzop -d -c) :
65 $3 eq "lzma" ?
qw(lzma -d -c) : qw(gzip -d -c);
67 my ($fh, $tmpname) = tempfile
(SUFFIX
=> $suffix,
68 DIR
=> File
::Spec
->tmpdir, UNLINK
=> 1)
69 or die "zrun: cannot create temporary file: $!\n";
71 if (my $child = fork) {
72 $child{$child} = $argument;
75 elsif (defined $child) {
76 STDOUT
->fdopen($fh, "w");
77 exec @preprocess, $argument;
80 die "zrun: cannot fork to handle $argument: $!\n";
83 push @argument, $argument;
86 while (%child and (my $pid = wait) != -1) {
87 if (defined(my $argument = delete $child{$pid})) {
89 die "zrun: preprocessing for $argument terminated abnormally: $?\n";
91 elsif (my $code = $?
>> 8) {
92 die "zrun: preprocessing for $argument terminated with code $code\n";
97 my $status = system $program ($program, @argument);
99 die "zrun: $program terminated abnormally: $?\n";