textviewer.pl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/perl
  2. # COPYRIGHT AND LICENSE
  3. # Copyright (C) 2005-2006 H.Merijn Brand
  4. #
  5. # This script is free software; you can redistribute it and/or modify it
  6. # under the same terms as Perl and/or Claws Mail itself. (GPL)
  7. use strict;
  8. use warnings;
  9. sub usage ($;$)
  10. {
  11. my ($err, $str) = (@_, "");
  12. $err and select STDERR;
  13. print
  14. "usage: $0 [--html] [--type=<type>] file\n",
  15. " --html Generate HTML (if supported)\n",
  16. " --type=X X as mimetype (msword => doc)\n";
  17. $str and print "$str\n";
  18. exit $err;
  19. } # usage
  20. @ARGV == 1 and $ARGV[0] eq "-?" || $ARGV[0] =~ m/^-+help$/ and usage (0);
  21. use Getopt::Long qw(:config bundling nopermute);
  22. my $opt_v = 0;
  23. my $opt_t;
  24. my $opt_h = "text";
  25. GetOptions (
  26. "v|verbose:1" => \$opt_v,
  27. "t|type|mimetype=s" => \$opt_t,
  28. "h|html" => sub { $opt_h = "html" },
  29. ) or usage (1);
  30. $opt_v and print "$0 @ARGV\n";
  31. my $file = shift or usage (1, "File argument is missing");
  32. -f $file or usage (1, "File argument is not a plain file");
  33. -r $file or usage (1, "File argument is not a readable file");
  34. -s $file or usage (1, "File argument is an empty file");
  35. # anon-list contains all possible commands to show content
  36. # plain text is a reference to same type (alias)
  37. # %f will be replaced with file. If no %f, file will be the last arg
  38. my %fh = (
  39. text => {
  40. bin => [ "strings" ], # fallback for binary files
  41. txt => [ "cat" ], # Plain text
  42. html => [ "txt2htm",
  43. "text2html" ], # HTML
  44. msword => "doc",
  45. doc => [ "antiword -w 72" ], # M$ Word
  46. "vnd.ms-excel" => "xls",
  47. "ms-excel" => "xls",
  48. xls => [ "xlscat -L" ], # M$ Excel
  49. # ppt => [ "ppthtml" ], # M$ PowerPoint
  50. # ppthtml "$1" | html2text
  51. rtf => [ "rtf2text",
  52. "unrtf -t text" ], # RTF
  53. pdf => [ "pdftotext %f -" ], # Adobe PDF
  54. sxc => "xls", # OpenOffice spreadsheet
  55. odt => [ "ooo2txt" ], # OpenOffice writer
  56. csv => "xls", # Comma Separated Values
  57. pl => [ "perltidy -st -se",
  58. "cat" ], # Perl
  59. pm => "pl",
  60. ( map { $_ => "txt" } qw(
  61. diff
  62. c h ic ec cc
  63. sh sed awk
  64. plain
  65. )),
  66. bz2 => [ "bzip2 -d < %f | strings" ],
  67. test => [ \&test ], # Internal
  68. },
  69. html => {
  70. rtf => [ "rtf2html" ],
  71. },
  72. );
  73. my $ext = $file =~ m/\.(\w+)$/ ? lc $1 : "";
  74. $opt_t && exists $fh{text}{lc $opt_t} and $ext = lc$opt_t;
  75. unless (exists $fh{text}{$ext}) {
  76. my $ftype = `file --brief $file`;
  77. $ext =
  78. $ftype =~ m/^pdf doc/i ? "pdf" :
  79. $ftype =~ m/^ascii( english)? text/i ? "txt" :
  80. $ftype =~ m/^(utf-8 unicode|iso-\d+)( english)? text/i ? "txt" :
  81. $ftype =~ m/^xml doc/i ? "xml" :
  82. $ftype =~ m/^\w+ compress/i ? "bin" :
  83. "bin" ;
  84. # \w+ archive
  85. # \w+ image
  86. # ...
  87. }
  88. $ext ||= "txt";
  89. exists $fh{$opt_h}{$ext} or $opt_h = "text";
  90. exists $fh{$opt_h}{$ext} or $ext = "txt";
  91. my $ref = $fh{$opt_h}{$ext};
  92. ref $ref or $ref = $fh{$opt_h}{$ref};
  93. $opt_v and print STDERR "[ @$ref ] $file\n";
  94. sub which ($)
  95. {
  96. (my $cmd = shift) =~ s/\s.*//; # Only the command. Discard arguments here
  97. foreach my $path (split m/:+/, $ENV{PATH}) {
  98. -x "$path/$cmd" and return "$path/$cmd";
  99. }
  100. return 0;
  101. } # which
  102. my $cmd = "cat -ve";
  103. foreach my $c (@$ref) {
  104. if (ref $c) {
  105. $c->($file);
  106. exit;
  107. }
  108. my $cp = which ($c) or next;
  109. $cmd = $c;
  110. last;
  111. }
  112. $cmd =~ s/%f\b/$file/g or $cmd .= " $file";
  113. $opt_v and print "$cmd\n";
  114. exec $cmd;