c1b3861aa65dc77137bba89a191723dd28a15279
[sheet.git] / rfc1345convert
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5
6 use Data::Dumper;
7
8 our $VERSION = '1.00';
9
10 if (0) {
11         #TODO: automatic download if not specified on stdin
12         require LWP::Simple;
13         LWP::Simple::get('http://www.ietf.org/rfc/rfc1345.txt');
14 }
15
16 # skip everything until a character indented by 1 space (table start)
17 do {$_ = <>} until /^\s\S/;
18
19 my @t = $_;  # add first line (already read, assume it's ok)
20
21 # read the rest of the character table
22 while ($_ = <>) {
23         # check for table end (chapter 4)
24         last if /^4/;
25
26         # parse table lines (ignore (unindented) page break)
27         next unless s/^ //;
28         chomp;
29
30         # add the line to @t
31         if (s/^ {15}/ /) {
32                 # continuation line (add to last entry)
33                 $t[-1] .= $_;
34         }
35         else {
36                 # add a new entry
37                 push @t, $_;
38         }
39 }
40
41 # create a hash of desired input
42 my %di;
43 for (@t) {
44         my ($mnem, $char, $name) = split / +/, $_, 3;
45         next if length $mnem != 2;
46         $di{$mnem} = hex $char;
47 }
48
49 # output perl code of hash
50 # (assume no backslashes or curlies, so we can just q{} w/o escaping)
51 print "{\n";
52 print "q{$_}=>$di{$_},\n" for sort keys %di;
53 print "}\n";
54