blib/script/afpdump.pl untested

filter: Statementstmt 0.0% 0 / 160 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 57 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 321 CC 22 SCAR 44.6
CC
22
Cov
0%
guess
210.0
dump_afp
72.0
dump_members
6.0
CRAP 86.9
1 #!/usr/local/bin/perl
2  
3 use FindBin;
4 use lib "$FindBin::Bin/../lib";
5 use lib "$FindBin::Bin/../../Parse-Binary/lib";
6  
7 # XXX - use encoding 'utf8' cannot be used as it interferes with code parsing
8 # resulting in inconsistent utf8 flag on all of the @ARGV!
9 #use encoding 'utf8';
10 #utf8::upgrade($_) for @ARGV; # XXX black, black voodoo magic
11  
12 use File::Basename;
13 use Parse::AFP;
14 use Encode::IBM;
15 use Encode::Guess;
16 use Getopt::Std;
17  
18 binmode(STDIN, ':utf8');
19 binmode(STDOUT, ':utf8');
20  
21 $Encode::Guess::NoUTFAutoGuess = 1;
22 use vars qw/$opt_e/; getopts('e:');
23 Encode::Guess->set_suspects(split(/\s*,\s*/, ($opt_e || 'cp500,ibm-835')));
24  
25 =head1 NAME
26  
27 adpdump.pl - Dump IBM AFP data to HTML
28  
29 =head1 SYNOPSIS
30  
31 # Defaults to "cp500, ibm-835" encoding
32 % afpdump.pl input.afp > output.html
33  
34 # For Big5-encoded AFPs
35 % afpdump -e cp437,ibm-947 big5.afp > output.html
36  
37 =cut
38  
39 my %desc;
40 foreach my $type qw( Record Triplet PTX/ControlSequence ) {
41 require "Parse/AFP/$type.pm";
42 open my $fh, $INC{"Parse/AFP/$type.pm"} or die $!;
43 while (<$fh>) {
44 /'([A-Z][:\w]+)',\s+#\s?(.+)/ or next;
45 $desc{$1} = $2;
46 }
47 }
48  
49 sub Header ();
50 sub Parse::AFP::PTX::TRN::ENCODING () { 'Guess' };
51  
52 die "Usage: $0 [ -e codepage1,codepage2... ] file.afp > file.html\n"
53 unless @ARGV;
54  
55 $SIG{__WARN__} = sub { use Carp; Carp::cluck(@_) };
56 $SIG{__DIE__} = sub { use Carp; Carp::confess(@_) };
57  
58 my $input = shift;
59 my $afp = Parse::AFP->new($input, { lazy => 1 });
60 $input = basename($input);
61  
62 print Header();
63 print "<h1>$input</h1><ol class='top'>\n";
64 dump_members($afp);
65 print "</ol></body></html>\n";
66  
67 sub dump_afp {
68 my $obj = shift;
69 my $struct = $obj->struct;
70 print "<table border=0 summary='$obj'>";
71  
72 my @keys = sort grep !/^_|^(?:Data|EscapeSequence|ControlCode|Length|CC|(?:Sub)?Type|FlagByte)$/, keys %$struct;
73 push @keys, 'Data' if exists $struct->{Data};
74 foreach my $key (@keys) {
75 next if ref $struct->{$key};
76 length($x = $struct->{$key}) or next;
77  
78 if ($obj->ENCODING and grep { $key eq $_ } $obj->ENCODED_FIELDS) {
79 $x = $obj->$key;
80 $x = qq("$x");
81 }
82 elsif ($x =~ /[^\w\s]/) {
83 $x = '<span class="hex">'.uc(join(' ',
84 (length($x) <= 80)
85 ? unpack('(H2)*', $x)
86 : (unpack('(H2)*', substr($x, 0, 80)), '...')
87 )).'</span>';
88 }
89 if ($key eq 'Data') {
90 print "<tr><td colspan='2' class='item'>$x</td></tr>\n";
91 }
92 else {
93 print "<tr><td class='label'>$key</td><td class='item'>$x</td></tr>\n";
94 }
95 }
96  
97 print "</table>";
98 if ($obj->has_members) {
99 print "<ol>";
100 dump_members($obj);
101 print "</ol>";
102 }
103 }
104  
105 sub dump_members {
106 my $obj = shift;
107 while (my $rec = $obj->next_member) {
108 my $type = substr(ref($rec), 12);
109 print "<li><div><strong>$type</strong>";
110 print " &ndash; $desc{$type}" if exists $desc{$type};
111 print "</div>";
112 dump_afp($rec);
113 print "</li>";
114 }
115 }
116  
117 use constant Header => << '.';
118 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
119 <html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'><style type='text/css'><!--
120 body { background: #e0e0e0; font-family: times new roman, times; margin-left: 20px }
121 h1 { font-family: times }
122 span.hex { font-family: andale mono, courier }
123 ol { border-left: 1px dotted black }
124 ol.top { border-left: none }
125 table { font-size: small; border-left: 1px dotted black; padding-left: 6pt; width: 100% }
126 td.label { background: #d0d0d0; font-family: arial unicode ms, helvetica }
127 td.item { background: white; width: 100%; font-family: arial unicode ms, helvetica }
128 div { text-decoration: underline; background: #e0e0ff; font-family: arial unicode ms, helvetica }
129 --></style><title>AFP Dump</title></head><body>
130 .
131  
132 1;
133  
134 no warnings 'redefine';
135 package Encode::Guess;
136 sub guess {
137 my $class = shift;
138 my $obj = ref($class) ? $class : $Encode::Encoding{$Canon};
139 my $octet = shift;
140  
141 # sanity check
142 return unless defined $octet and length $octet;
143  
144 # cheat 0: utf8 flag;
145 if ( Encode::is_utf8($octet) ) {
146 return find_encoding('utf8') unless $NoUTFAutoGuess;
147 Encode::_utf8_off($octet);
148 }
149 # cheat 1: BOM
150 use Encode::Unicode;
151 unless ($NoUTFAutoGuess) {
152 my $BOM = unpack('n', $octet);
153 return find_encoding('UTF-16')
154 if (defined $BOM and ($BOM == 0xFeFF or $BOM == 0xFFFe));
155 $BOM = unpack('N', $octet);
156 return find_encoding('UTF-32')
157 if (defined $BOM and ($BOM == 0xFeFF or $BOM == 0xFFFe0000));
158 if ($octet =~ /\x00/o){ # if \x00 found, we assume UTF-(16|32)(BE|LE)
159 my $utf;
160 my ($be, $le) = (0, 0);
161 if ($octet =~ /\x00\x00/o){ # UTF-32(BE|LE) assumed
162 $utf = "UTF-32";
163 for my $char (unpack('N*', $octet)){
164 $char & 0x0000ffff and $be++;
165 $char & 0xffff0000 and $le++;
166 }
167 }else{ # UTF-16(BE|LE) assumed
168 $utf = "UTF-16";
169 for my $char (unpack('n*', $octet)){
170 $char & 0x00ff and $be++;
171 $char & 0xff00 and $le++;
172 }
173 }
174 DEBUG and warn "$utf, be == $be, le == $le";
175 $be == $le
176 and return
177 "Encodings ambiguous between $utf BE and LE ($be, $le)";
178 $utf .= ($be > $le) ? 'BE' : 'LE';
179 return find_encoding($utf);
180 }
181 }
182 my %try = %{$obj->{Suspects}};
183 for my $c (@_){
184 my $e = find_encoding($c) or die "Unknown encoding: $c";
185 $try{$e->name} = $e;
186 DEBUG and warn "Added: ", $e->name;
187 }
188 my $nline = 1;
189 for my $line (split /\r\n?|\n/, $octet){
190 # cheat 2 -- \e in the string
191 if ($line =~ /\e/o){
192 my @keys = keys %try;
193 delete @try{qw/utf8 ascii/};
194 for my $k (@keys){
195 ref($try{$k}) eq 'Encode::XS' and delete $try{$k};
196 }
197 }
198 my %ok = %try;
199 # warn join(",", keys %try);
200 for my $k (keys %try){
201 my $scratch = $line;
202 $try{$k}->decode($scratch, FB_QUIET);
203 if ($scratch eq ''){
204 DEBUG and warn sprintf("%4d:%-24s ok\n", $nline, $k);
205 }else{
206 use bytes ();
207 DEBUG and
208 warn sprintf("%4d:%-24s not ok; %d bytes left\n",
209 $nline, $k, bytes::length($scratch));
210 delete $ok{$k};
211 }
212 }
213 %ok or return "No appropriate encodings found!";
214 if (scalar(keys(%ok)) >= 1){
215 my ($retval) = sort values(%ok);
216 return $retval;
217 }
218 %try = %ok; $nline++;
219 }
220 $try{ascii} or
221 return "Encodings too ambiguous: ", join(" or ", keys %try);
222 return $try{ascii};
223 }
224