2006-09-10 [wwp] 2.4.0cvs172
[claws.git] / tools / textviewer.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 sub usage ($;$)
7 {
8     my ($err, $str) = (@_, "");
9     $err and select STDERR;
10     print
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";
15     exit $err;
16     } # usage
17
18 @ARGV == 1 and $ARGV[0] eq "-?" || $ARGV[0] =~ m/^-+help$/ and usage (0);
19
20 use Getopt::Long qw(:config bundling nopermute);
21 my $opt_v = 0;
22 my $opt_t;
23 my $opt_h = "text";
24 GetOptions (
25     "v|verbose:1"       => \$opt_v,
26     "t|type|mimetype=s" => \$opt_t,
27     "h|html"            => sub { $opt_h = "html" },
28     ) or usage (1);
29
30 $opt_v and print STDERR "$0 @ARGV\n";
31
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");
36
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
40 my %fh = (
41     text => {
42         bin     => [ "strings"          ], # fallback for binary files
43
44         txt     => [ "cat"              ], # Plain text
45
46         html    => [ "txt2htm",
47                      "text2html"        ], # HTML
48
49         msword  => "doc",
50         doc     => [ "antiword -w 72"   ], # M$ Word
51         "vnd.ms-excel" => "xls",
52         "ms-excel"     => "xls",
53         xls     => [ "xlscat -L"        ], # M$ Excel
54 #       ppt     => [ "ppthtml"          ], # M$ PowerPoint
55 #                       ppthtml "$1" | html2text
56
57         rtf     => [ "rtf2text",
58                      "unrtf -t text"    ], # RTF
59         pdf     => [ "pdftotext %f -"   ], # Adobe PDF
60
61         sxc     => "xls",                  # OpenOffice spreadsheet
62         odt     => [ "ooo2txt"          ], # OpenOffice writer
63
64         pl      => [ "perltidy -st -se",
65                      "cat"              ], # Perl
66         pm      => "pl",
67
68         ( map { $_ => "txt" } qw(
69             diff
70             c h ic ec cc
71             sh sed awk
72             plain
73             )),
74
75         test    => [ \&test             ], # Internal
76         },
77
78     html => {
79         rtf     => [ "rtf2html"         ],
80         },
81     );
82
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`;
87     $ext =
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" :
93                                                                   "bin" ;
94     # \w+ archive
95     # \w+ image
96     # ...
97     }
98 $ext ||= "txt";
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};
103
104 $opt_v and print STDERR "[ @$ref ] $file\n";
105
106 sub which ($)
107 {
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";
111         }
112     return 0;
113     } # which
114
115 my $cmd = "cat -ve";
116 foreach my $c (@$ref) {
117     if (ref $c) {
118         $c->($file);
119         exit;
120         }
121
122     my $cp = which ($c) or next;
123     $cmd = $c;
124     last;
125     }
126
127 $cmd =~ s/%f\b/$file/g or $cmd .= " $file";
128 $opt_v and print STDERR "$cmd\n";
129 exec $cmd;