1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
#!/usr/bin/env perl
use v5.34;
use warnings;
use feature 'signatures';
no warnings qw(experimental::signatures);
use Getopt::Std ();
use File::Temp ();
use File::Basename ();
use List::Util qw(any);
sub usage($fh) {
print $fh <<~'EOF';
Usage:
z COMMAND...
z -h
EOF
}
sub help($fh) {
print $fh <<~'EOF';
Options:
-h, --help show this message
COMMAND shell command to be wrapped
Wrapper that uncompresses file arguments to commands.
This enables having commands that operate on plain files to not
need to know if they're compressed or not.
It doesn't depend on the file extension, but on what file(1) says
of it.
Examples:
Replacement for zcat(1p):
$ z cat a-file.gz
Transparent grep (where my-file.dat is xz compressed):
$ z grep -E '[a-z]+' my-file.dat
EOF
}
for (@ARGV) {
last if $_ eq '--';
if ($_ eq '--help') {
usage *STDOUT;
help *STDOUT;
exit;
}
}
my %opts;
if (!Getopt::Std::getopts('h', \%opts)) {
usage *STDERR;
exit 2;
}
if ($opts{h}) {
usage *STDOUT;
help *STDOUT;
exit;
}
# Transform
# FILENAME: content/type; charset=some\n
# into
# content/type
sub trim($x) {
chomp $x;
$x =~ s/^.*?: //;
$x =~ s/;.*$//;
return $x;
}
my %TYPES = (
'application/gzip' => [qw(gzip -dc)],
'application/x-bzip2' => [qw(bzip2 -dc)],
'application/x-xz' => [qw(xz -dc)],
'application/x-lzma' => [qw(lzma -dc)],
);
my @tmpfiles;
sub arg_for($arg) {
if (! -e $arg) {
return $arg;
}
my $type = trim `file -i $_`;
if (any { $type eq $_ } keys %TYPES) {
my ($fh, $tmpname) = File::Temp::tempfile();
push @tmpfiles, $tmpname;
my @command = @{$TYPES{$type}};
print $fh `@command $arg`;
die $! if $?;
close $fh;
return $tmpname;
}
return $arg;
}
sub status_for($n) {
if ($n == -1) {
return 127;
} elsif ($n & 127) {
return $n & 127;
} else {
return $n >> 8;
}
}
my @CMD = map { arg_for $_ } @ARGV;
exit status_for(system @CMD);
END {
unlink @tmpfiles;
}
__END__
=head1 NAME
z - Wrapper that uncompresses file arguments to commands.
=head1 SYNOPSIS
z COMMAND FILE...
=head1 DESCRIPTION
Prefixing a shell command with "zrun" causes any compressed files that are
arguments of the command to be transparently uncompressed to temp files
(not pipes) and the uncompressed files fed to the command.
This is a quick way to run a command that does not itself support
compressed files, without manually uncompressing the files.
The following compression types are supported: gz bz2 Z xz lzma lzo
If zrun is linked to some name beginning with z, like zprog, and the link is
executed, this is equivalent to executing "zrun prog".
=cut
|