sbcsgen.pl was giving different results on different machines in the case
[sgt/charset] / sbcsgen.pl
CommitLineData
c6d25d8d 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$infile = shift @ARGV if defined $ARGV[0];
8$outfile = "sbcsdat.c";
9$outfile = shift @ARGV if defined $ARGV[0];
01081d4e 10$outheader = "sbcsdat.h";
11$outheader = shift @ARGV if defined $ARGV[0];
c6d25d8d 12
13open FOO, $infile;
14open BAR, ">$outfile";
15select BAR;
16
17print "/*\n";
18print " * sbcsdat.c - data definitions for single-byte character sets.\n";
19print " *\n";
20print " * Generated by sbcsgen.pl from sbcs.dat.\n";
21print " * You should edit those files rather than editing this one.\n";
22print " */\n";
23print "\n";
24print "#ifndef ENUM_CHARSETS\n";
25print "\n";
26print "#include \"charset.h\"\n";
27print "#include \"internal.h\"\n";
28print "\n";
29
30my $charsetname = undef;
31my @vals = ();
32
33my @charsetnames = ();
34my @sortpriority = ();
35
36while (<FOO>) {
37 chomp;
38 if (/^charset (.*)$/) {
39 $charsetname = $1;
40 @vals = ();
41 @sortpriority = map { 0 } 0..255;
42 } elsif (/^sortpriority ([^-]*)-([^-]*) (.*)$/) {
43 for ($i = hex $1; $i <= hex $2; $i++) {
44 $sortpriority[$i] += $3;
45 }
46 } elsif (/^[0-9a-fA-FX]/) {
47 push @vals, map { $_ eq "XXXX" ? -1 : hex $_ } split / +/, $_;
48 if (scalar @vals > 256) {
49 die "$infile:$.: charset $charsetname has more than 256 values\n";
50 } elsif (scalar @vals == 256) {
51 &outcharset($charsetname, \@vals, \@sortpriority);
52 push @charsetnames, $charsetname;
53 $charsetname = undef;
54 @vals = ();
55 @sortpriority = map { 0 } 0..255;
56 }
57 }
58}
59
60print "#else /* ENUM_CHARSETS */\n";
61print "\n";
62
63foreach $i (@charsetnames) {
64 print "ENUM_CHARSET($i)\n";
65}
66
67print "\n";
68print "#endif /* ENUM_CHARSETS */\n";
69
01081d4e 70close BAR;
71
72open BAR, ">$outheader";
73select BAR;
74
75print "/*\n";
76print " * sbcsdat.h - header file for SBCS data structures.\n";
77print " *\n";
78print " * Generated by sbcsgen.pl from sbcs.dat.\n";
79print " * You should edit those files rather than editing this one.\n";
80print " */\n";
81print "\n";
82print "#ifndef charset_sbcsdat_h\n";
83print "#define charset_sbcsdat_h\n";
84print "\n";
85print "#include \"charset.h\"\n";
86print "#include \"internal.h\"\n";
87print "\n";
88foreach $i (@charsetnames) {
89 print "extern const sbcs_data sbcsdata_$i;\n";
90}
91print "\n";
92print "#endif /* charset_sbcsdat_h */\n";
93
94close BAR;
95
c6d25d8d 96sub outcharset($$$) {
97 my ($name, $vals, $sortpriority) = @_;
98 my ($prefix, $i, @sorted);
99
01081d4e 100 print "const sbcs_data sbcsdata_$name = {\n";
c6d25d8d 101 print " {\n";
102 $prefix = " ";
103 @sorted = ();
104 for ($i = 0; $i < 256; $i++) {
105 if ($vals->[$i] < 0) {
106 printf "%sERROR ", $prefix;
107 } else {
108 printf "%s0x%04x", $prefix, $vals->[$i];
109 die "ooh? $i\n" unless defined $sortpriority->[$i];
110 push @sorted, [$i, $vals->[$i], 0+$sortpriority->[$i]];
111 }
112 if ($i % 8 == 7) {
113 $prefix = ",\n ";
114 } else {
115 $prefix = ", ";
116 }
117 }
118 print "\n },\n {\n";
4fd00a6f 119 @sorted = sort { ($a->[1] == $b->[1] ?
120 $b->[2] <=> $a->[2] :
121 $a->[1] <=> $b->[1]) ||
122 $a->[0] <=> $b->[0] } @sorted;
c6d25d8d 123 $prefix = " ";
124 $uval = -1;
125 for ($i = $j = 0; $i < scalar @sorted; $i++) {
126 next if ($uval == $sorted[$i]->[1]); # low-priority alternative
127 $uval = $sorted[$i]->[1];
128 printf "%s0x%02x", $prefix, $sorted[$i]->[0];
129 if ($j % 8 == 7) {
130 $prefix = ",\n ";
131 } else {
132 $prefix = ", ";
133 }
134 $j++;
135 }
136 printf "\n },\n %d\n", $j;
137 print "};\n";
138 print "const charset_spec charset_$name = {\n" .
01081d4e 139 " $name, read_sbcs, write_sbcs, &sbcsdata_$name\n};\n\n";
c6d25d8d 140}