8 my ($err, $str) = (@_, "");
9 $err and select STDERR;
11 "usage: $0 [--html] [--type=<type>] file\n",
12 " --html Generate HTML (if supported)\n",
13 " --type=X X as mimetype (msword => doc)\n";
14 $str and print "$str\n";
18 @ARGV == 1 and $ARGV[0] eq "-?" || $ARGV[0] =~ m/^-+help$/ and usage (0);
20 use Getopt::Long qw(:config bundling nopermute);
25 "v|verbose:1" => \$opt_v,
26 "t|type|mimetype=s" => \$opt_t,
27 "h|html" => sub { $opt_h = "html" },
30 $opt_v and print STDERR "$0 @ARGV\n";
32 my $file = shift or usage (1, "File argument is missing");
33 -f $file or usage (1, "File argument is not a plain file");
34 -r $file or usage (1, "File argument is not a readable file");
35 -s $file or usage (1, "File argument is an empty file");
37 # anon-list contains all possible commands to show content
38 # plain text is a reference to same type (alias)
39 # %f will be replaced with file. If no %f, file will be the last arg
42 bin => [ "strings" ], # fallback for binary files
44 txt => [ "cat" ], # Plain text
50 doc => [ "antiword -w 72" ], # M$ Word
51 "vnd.ms-excel" => "xls",
53 xls => [ "xlscat -L" ], # M$ Excel
54 # ppt => [ "ppthtml" ], # M$ PowerPoint
55 # ppthtml "$1" | html2text
58 "unrtf -t text" ], # RTF
59 pdf => [ "pdftotext %f -" ], # Adobe PDF
61 sxc => "xls", # OpenOffice spreadsheet
62 odt => [ "ooo2txt" ], # OpenOffice writer
64 pl => [ "perltidy -st -se",
68 ( map { $_ => "txt" } qw(
75 test => [ \&test ], # Internal
79 rtf => [ "rtf2html" ],
83 my $ext = $file =~ m/\.(\w+)$/ ? lc $1 : "";
84 $opt_t && exists $fh{text}{lc $opt_t} and $ext = lc$opt_t;
85 unless (exists $fh{text}{$ext}) {
86 my $ftype = `file --brief $file`;
88 $ftype =~ m/^pdf doc/i ? "pdf" :
89 $ftype =~ m/^ascii( english)? text/i ? "txt" :
90 $ftype =~ m/^(utf-8 unicode|iso-\d+)( english)? text/i ? "txt" :
91 $ftype =~ m/^xml doc/i ? "xml" :
92 $ftype =~ m/^\w+ compress/i ? "bin" :
99 exists $fh{$opt_h}{$ext} or $opt_h = "text";
100 exists $fh{$opt_h}{$ext} or $ext = "txt";
101 my $ref = $fh{$opt_h}{$ext};
102 ref $ref or $ref = $fh{$opt_h}{$ref};
104 $opt_v and print STDERR "[ @$ref ] $file\n";
108 (my $cmd = shift) =~ s/\s.*//; # Only the command. Discard arguments here
109 foreach my $path (split m/:+/, $ENV{PATH}) {
110 -x "$path/$cmd" and return "$path/$cmd";
116 foreach my $c (@$ref) {
122 my $cp = which ($c) or next;
127 $cmd =~ s/%f\b/$file/g or $cmd .= " $file";
128 $opt_v and print STDERR "$cmd\n";