-
Notifications
You must be signed in to change notification settings - Fork 14
/
mimeopen
executable file
·376 lines (298 loc) · 9.41 KB
/
mimeopen
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/usr/bin/perl
use strict;
use warnings;
our $VERSION = '0.35';
$|++;
# ########## #
# Parse ARGV #
# ########## #
use Encode::Locale;
use Encode;
@ARGV = map { decode(locale => $_, 1) } @ARGV;
my %args = ();
my %opts = (
'help' => ['h'],
'usage' => ['u'],
'version' => ['v'],
'stdin' => [''],
'dereference' => ['L'],
'debug' => ['D'],
'database' => ['', 1],
'magic-only' => ['M'],
'ask' => ['a'],
'ask-default' => ['d'],
'no-ask' => ['n'],
);
while ((@ARGV) && ($ARGV[0] =~ /^-/)) {
my $opt = shift @ARGV;
if ($opt =~ /^--?$/) {
last;
}
elsif ($opt =~ s/^--([\w-]+)(?:=(.*))?/$1/) {
if (exists $opts{$opt}) {
if ($opts{$opt}[1]) {
my $arg = $2 || shift @ARGV;
complain('--'.$opt, 2) unless defined $arg;
$args{$opt} .= ( $args{$opt} ? ' ' : '' ) . $arg;
}
else { $args{$opt}++ }
}
else { complain('--'.$opt) }
}
elsif ($opt =~ s/^-(?!-)//) {
foreach my $o (split //, $opt) {
my ($key) = grep { $opts{$_}[0] eq $o } keys %opts;
complain($o) unless $key;
if ($opts{$key}[1]) {
my $arg = shift @ARGV;
complain('-'.$o, 2) unless defined $arg;
$args{$key} .= ( $args{$key} ? ' ' : '' ).$arg; # join with whitespace
}
else { $args{$key}++; }
}
}
else { complain($opt) }
}
if ($args{help} || $args{usage}) {
eval 'use Pod::Usage';
die "Could not find perl module Pod::Usage\n" if $@;
pod2usage( {
-verbose => 1,
-exitval => 0,
} );
}
if ($args{version}) {
print "mimeopen $VERSION\n\n", << 'EOV';
Copyright (c) 2005, 2012 Jaap G Karssenberg. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
EOV
exit 0;
}
complain(undef, 4) unless scalar(@ARGV);
# ############# #
# prepare stuff #
# ############# #
# --database
{
no warnings 'once';
@File::MimeInfo::DIRS = split /:/, $args{database} if $args{database};
}
## Actually use our modules ##
eval 'use File::MimeInfo::Magic qw/mimetype magic/;';
die $@ if $@;
eval 'use File::MimeInfo::Applications;';
die $@ if $@;
*default = \&File::MimeInfo::default;
# --debug
if ($args{debug}) {
{
no warnings 'once';
$File::MimeInfo::DEBUG++;
$File::MimeInfo::Magic::DEBUG++;
}
print '> Data dirs are: ', join( ', ',
$args{database}
? ( split /:/, $args{database} )
: (
File::BaseDir::xdg_data_home(),
File::BaseDir::xdg_data_dirs()
)
), "\n";
}
# --dereference ## deprecated - so always true
$args{dereference} = 1;
if ($args{dereference}) {
eval 'use File::Spec';
die "Could not find perl module File::Spec\n" if $@;
}
if (!File::MimeInfo::has_mimeinfo_database()) {
die "No mimeinfo database found\n";
}
# ######## #
# do stuff #
# ######## #
my $mimetype;
my $file = $ARGV[0];
# --dereference
my $f = ($args{dereference} && -l $file) ? resolvelink($file) : $file;
# --magic-only
$mimetype = $args{'magic-only'}
? (magic($f) || default($f))
: mimetype($f) ;
unless (length $mimetype) {
print STDERR "Could not determine mimetype for file: $file\n";
exit 5;
}
my ($default, @other) = mime_applications_all($mimetype);
if ($args{'no-ask'}) {
$default = defined($default) ? $default : $other[0];
}
elsif ($args{'ask'}) {
$default = choose($mimetype, 0, grep defined($_), $default, @other);
}
elsif ($args{'ask-default'}) {
$default = choose($mimetype, 1, grep defined($_), $default, @other);
}
elsif (! defined $default) {
($default) = (@other == 1) ? (@other) : choose($mimetype, 1, @other);
}
unless($default) {
# $default can still be undef, if $other[0] is undef and no-ask is set.
print STDERR "No applications found for mimetype: $mimetype\n.";
exit 6;
}
print 'Opening '.join(', ', map qq{"$_"}, @ARGV)
. ' with '.$default->get_value('Name')." ($mimetype)\n";
#print STDERR "exec string: ".$default->parse_Exec(@ARGV)."\n";
if (@ARGV == 1 or $default->wants_list) {
$default->run(@ARGV);
}
else {
my $last = pop @ARGV;
fork or $default->exec($_) for @ARGV;
$default->run($last);
}
# run() will fork and fall through to here, don't do anything after that except for exiting with a sucess code
exit 0;
# ########### #
# Subroutines #
# ########### #
sub choose {
my ($mime, $set_default, @app) = @_;
print $set_default ?
"Please choose a default application for files of type $mime\n\n" :
"Please choose an application\n\n" ;
my @done;
for my $i (0 .. $#app) {
my (undef, undef, $file) =
File::Spec->splitpath( $app[$i]->{file} );
$file =~ s/\.desktop$//;
if (grep {$_ eq $file} @done) {
$app[$i] = undef;
}
else {
push @done, $file;
print "\t", scalar(@done), ") ",
$app[$i]->get_value('Name'), " ($file)\n";
}
}
@app = grep defined($_), @app;
print "\t", scalar(@done)+1, ") Other...\n" if $set_default;
print "\nuse application #";
my $c = <STDIN>;
chomp $c;
unless ($c =~ /^\d+$/) {
print STDERR "Cancelled\n";
exit 8;
}
$c--; # base-1 => base-0
if ($set_default and $c == scalar(@done)) {
# ask for custom command
print "use command: ";
my $cmd = <STDIN>;
chomp $cmd;
push @app,
eval { mime_applications_set_custom($mime => $cmd) };
warn $@ if $@;
}
elsif ($c > scalar(@app)) {
print STDERR "Cancelled\n";
exit 8;
}
elsif ($set_default) {
eval { mime_applications_set_default($mime => $app[$c]) };
warn $@ if $@;
}
return $app[$c];
}
sub complain { # Error messages
my $opt = shift;
my $m = shift || 1;
my $bn = $0;
$bn =~ s|^(.*/)*||;
if ($m == 1) { print STDERR "$bn: unrecognized option '$opt'" }
elsif ($m == 2) { print STDERR "$bn: option '$opt' requires an argument" }
elsif ($m == 3) { print STDERR "$bn: $opt: No such file or directory\n" }
elsif ($m == 4) { print STDERR "usage: $bn [options] files" }
print "\nTry '$bn --help' for more information.\n" unless $m == 3;
exit $m;
}
sub resolvelink { # --dereference
my $file = shift;
my $link = readlink($file) || return $file;
my (undef, $dir, undef) = File::Spec->splitpath($file);
$link = File::Spec->rel2abs($link, $dir);
$link = resolvelink($link) if -l $link; # recurs
return $link;
}
__END__
=head1 NAME
mimeopen - Open files by mimetype
=head1 SYNOPSIS
mimeopen [options] [-] files
=head1 DESCRIPTION
This script tries to determine the mimetype of a file and open it with the
default desktop application. If no default application is configured the
user is prompted with an "open with" menu in the terminal.
To use this script you need the freedesktop mime-info database and the
freedesktop desktop-file-utils package. See L<File::MimeInfo::Applications(3)>
for more details.
=head1 OPTIONS
=over 4
=item B<-a>, B<--ask>
Do not execute the default application but ask which application to run.
This does not change the default application.
=item B<-d>, B<--ask-default>
Let the user choose a new default program for given files.
=item B<-n>, B<--no-ask>
Don't ask the user which program to use. Choose the default program or the
first program known to handle the file mimetype. This does not set the
default application.
=item B<-M>, B<--magic-only>
Do not check for extensions, globs or inode type, only look at the content
of the file. This is particularly useful if for some reason you don't trust
the name or the extension a file has.
=item B<--database>=I<mimedir>:I<mimedir>:...
Force the program to look in these directories for the shared mime-info
database. The directories specified by the basedir specification
are ignored.
=item B<-D>, B<--debug>
Print debug information about how the mimetype was determined.
=item B<-h>, B<--help>
=item B<-u>, B<--usage>
Print a help message and exits.
=item B<-v>, B<--version>
Print the version of the program and exit.
=back
=head1 DEPRECATED OPTIONS
=over 4
=item B<-L>, B<--dereference>
Follow symbolic links.
Deprecated because this is the logical default for this command.
Ignored silently.
=back
=head1 BUGS
If you find bugs, please file them in our Github
issue tracker at L<https://github.com/mbeijen/File-MimeInfo/issues>.
See File::MimeInfo::Applications(3) and File::DesktopEntry(3)
for some limitations.
=head1 AUTHOR
Jaap Karssenberg E<lt>pardus@cpan.orgE<gt>
Maintained by Michiel Beijen E<lt>mb@x14.nlE<gt>
=head1 COPYRIGHT
Copyright (c) 2002, 2012 Jaap G Karssenberg. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
=head1 SEE ALSO
L<mimetype(1)>,
L<update-mime-database(1)>,
L<update-desktop-database(1)>,
L<File::MimeInfo(3)>,
L<File::MimeInfo::Applications(3)>