3 # This script generates sbcsdat.c (the data for all the SBCSes) from its
4 # source form sbcs.dat.
7 $outfile = "sbcsdat.c";
10 open BAR, ">$outfile";
14 print " * sbcsdat.c - data definitions for single-byte character sets.\n";
16 print " * Generated by sbcsgen.pl from sbcs.dat.\n";
17 print " * You should edit those files rather than editing this one.\n";
20 print "#ifndef ENUM_CHARSETS\n";
22 print "#include \"charset.h\"\n";
23 print "#include \"internal.h\"\n";
26 my $charsetname = undef;
29 my @charsetnames = ();
30 my @sortpriority = ();
34 if (/^charset (.*)$/) {
37 @sortpriority = map { 0 } 0..255;
38 } elsif (/^sortpriority ([^-]*)-([^-]*) (.*)$/) {
39 for ($i = hex $1; $i <= hex $2; $i++) {
40 $sortpriority[$i] += $3;
42 } elsif (/^[0-9a-fA-FX]/) {
43 push @vals, map { $_ eq "XXXX" ? -1 : hex $_ } split / +/, $_;
44 if (scalar @vals > 256) {
45 die "$infile:$.: charset $charsetname has more than 256 values\n";
46 } elsif (scalar @vals == 256) {
47 &outcharset($charsetname, \@vals, \@sortpriority);
48 push @charsetnames, $charsetname;
51 @sortpriority = map { 0 } 0..255;
56 print "#else /* ENUM_CHARSETS */\n";
59 foreach $i (@charsetnames) {
60 print "ENUM_CHARSET($i)\n";
64 print "#endif /* ENUM_CHARSETS */\n";
67 my ($name, $vals, $sortpriority) = @_;
68 my ($prefix, $i, @sorted);
70 print "static const sbcs_data data_$name = {\n";
74 for ($i = 0; $i < 256; $i++) {
75 if ($vals->[$i] < 0) {
76 printf "%sERROR ", $prefix;
78 printf "%s0x%04x", $prefix, $vals->[$i];
79 die "ooh? $i\n" unless defined $sortpriority->[$i];
80 push @sorted, [$i, $vals->[$i], 0+$sortpriority->[$i]];
89 @sorted = sort { ($a->[1] == $b->[1] ?
91 $a->[1] <=> $b->[1]) ||
92 $a->[0] <=> $b->[0] } @sorted;
95 for ($i = $j = 0; $i < scalar @sorted; $i++) {
96 next if ($uval == $sorted[$i]->[1]); # low-priority alternative
97 $uval = $sorted[$i]->[1];
98 printf "%s0x%02x", $prefix, $sorted[$i]->[0];
106 printf "\n },\n %d\n", $j;
108 print "const charset_spec charset_$name = {\n" .
109 " $name, read_sbcs, write_sbcs, &data_$name\n};\n\n";