3 # COPYRIGHT AND LICENSE
4 # Copyright (C) 2005-2006 H.Merijn Brand
6 # This script is free software; you can redistribute it and/or modify it
7 # under the same terms as Perl and/or Claws Mail itself. (GPL)
14 my ($err, $str) = (@_, "");
15 $err and select STDERR;
17 "usage: $0 [--html] [--type=<type>] file\n",
18 " --html Generate HTML (if supported)\n",
19 " --type=X X as mimetype (msword => doc)\n";
20 $str and print "$str\n";
24 @ARGV == 1 and $ARGV[0] eq "-?" || $ARGV[0] =~ m/^-+help$/ and usage (0);
26 use Getopt::Long qw(:config bundling nopermute);
31 "v|verbose:1" => \$opt_v,
32 "t|type|mimetype=s" => \$opt_t,
33 "h|html" => sub { $opt_h = "html" },
36 $opt_v and print "$0 @ARGV\n";
38 my $file = shift or usage (1, "File argument is missing");
39 -f $file or usage (1, "File argument is not a plain file");
40 -r $file or usage (1, "File argument is not a readable file");
41 -s $file or usage (1, "File argument is an empty file");
43 # anon-list contains all possible commands to show content
44 # plain text is a reference to same type (alias)
45 # %f will be replaced with file. If no %f, file will be the last arg
48 bin => [ "strings" ], # fallback for binary files
50 txt => [ "cat" ], # Plain text
56 doc => [ "antiword -w 72" ], # M$ Word
57 "vnd.ms-excel" => "xls",
59 xls => [ "xlscat -L" ], # M$ Excel
60 # ppt => [ "ppthtml" ], # M$ PowerPoint
61 # ppthtml "$1" | html2text
64 "unrtf -t text" ], # RTF
65 pdf => [ "pdftotext %f -" ], # Adobe PDF
67 sxc => "xls", # OpenOffice spreadsheet
68 odt => [ "ooo2txt" ], # OpenOffice writer
70 csv => "xls", # Comma Separated Values
72 pl => [ "perltidy -st -se",
76 ( map { $_ => "txt" } qw(
83 bz2 => [ "bzip2 -d < %f | strings" ],
85 test => [ \&test ], # Internal
89 rtf => [ "rtf2html" ],
93 my $ext = $file =~ m/\.(\w+)$/ ? lc $1 : "";
94 $opt_t && exists $fh{text}{lc $opt_t} and $ext = lc$opt_t;
95 unless (exists $fh{text}{$ext}) {
96 my $ftype = `file --brief $file`;
98 $ftype =~ m/^pdf doc/i ? "pdf" :
99 $ftype =~ m/^ascii( english)? text/i ? "txt" :
100 $ftype =~ m/^(utf-8 unicode|iso-\d+)( english)? text/i ? "txt" :
101 $ftype =~ m/^xml doc/i ? "xml" :
102 $ftype =~ m/^\w+ compress/i ? "bin" :
109 exists $fh{$opt_h}{$ext} or $opt_h = "text";
110 exists $fh{$opt_h}{$ext} or $ext = "txt";
111 my $ref = $fh{$opt_h}{$ext};
112 ref $ref or $ref = $fh{$opt_h}{$ref};
114 $opt_v and print STDERR "[ @$ref ] $file\n";
118 (my $cmd = shift) =~ s/\s.*//; # Only the command. Discard arguments here
119 foreach my $path (split m/:+/, $ENV{PATH}) {
120 -x "$path/$cmd" and return "$path/$cmd";
126 foreach my $c (@$ref) {
132 my $cp = which ($c) or next;
137 $cmd =~ s/%f\b/$file/g or $cmd .= " $file";
138 $opt_v and print "$cmd\n";