]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - charset/sbcsgen.pl
Add all the Mac OS simple single-byte character sets from ftp.unicode.org.
[PuTTY.git] / charset / sbcsgen.pl
1 #!/usr/bin/env perl -w
2
3 # This script generates sbcsdat.c (the data for all the SBCSes) from its
4 # source form sbcs.dat.
5
6 $infile = "sbcs.dat";
7 $outfile = "sbcsdat.c";
8
9 open FOO, $infile;
10 open BAR, ">$outfile";
11 select BAR;
12
13 print "/*\n";
14 print " * sbcsdat.c - data definitions for single-byte character sets.\n";
15 print " *\n";
16 print " * Generated by sbcsgen.pl from sbcs.dat.\n";
17 print " * You should edit those files rather than editing this one.\n";
18 print " */\n";
19 print "\n";
20 print "#ifndef ENUM_CHARSETS\n";
21 print "\n";
22 print "#include \"charset.h\"\n";
23 print "#include \"internal.h\"\n";
24 print "\n";
25
26 my $charsetname = undef;
27 my @vals = ();
28
29 my @charsetnames = ();
30
31 while (<FOO>) {
32     chomp;
33     if (/^charset (.*)$/) {
34         $charsetname = $1;
35         @vals = ();
36     } elsif (/^[0-9a-fA-FX]/) {
37         push @vals, map { $_ eq "XXXX" ? -1 : hex $_ } split / +/, $_;
38         if (scalar @vals > 256) {
39             die "$infile:$.: charset $charsetname has more than 256 values\n";
40         } elsif (scalar @vals == 256) {
41             &outcharset($charsetname, @vals);
42             push @charsetnames, $charsetname;
43             $charsetname = undef;
44             @vals = ();
45         }
46     }
47 }
48
49 print "#else /* ENUM_CHARSETS */\n";
50 print "\n";
51
52 foreach $i (@charsetnames) {
53     print "ENUM_CHARSET($i)\n";
54 }
55
56 print "\n";
57 print "#endif /* ENUM_CHARSETS */\n";
58
59 sub outcharset($@) {
60     my ($name, @vals) = @_;
61     my ($prefix, $i, @sorted);
62
63     print "static const sbcs_data data_$name = {\n";
64     print "    {\n";
65     $prefix = "    ";
66     @sorted = ();
67     for ($i = 0; $i < 256; $i++) {
68         if ($vals[$i] < 0) {
69             printf "%sERROR ", $prefix;
70         } else {
71             printf "%s0x%04x", $prefix, $vals[$i];
72             push @sorted, [$i, $vals[$i]];
73         }
74         if ($i % 8 == 7) {
75             $prefix = ",\n    ";
76         } else {
77             $prefix = ", ";
78         }
79     }
80     print "\n    },\n    {\n";
81     @sorted = sort { $a->[1] <=> $b->[1] } @sorted;
82     $prefix = "    ";
83     for ($i = 0; $i < scalar @sorted; $i++) {
84         printf "%s0x%02x", $prefix, $sorted[$i]->[0];
85         if ($i % 8 == 7) {
86             $prefix = ",\n    ";
87         } else {
88             $prefix = ", ";
89         }
90     }
91     printf "\n    },\n    %d\n", scalar @sorted;
92     print "};\n";
93     print "const charset_spec charset_$name = {\n" .
94           "    $name, read_sbcs, write_sbcs, &data_$name\n};\n\n";
95 }