| 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 | my @sortpriority = (); |
| 31 | |
| 32 | while (<FOO>) { |
| 33 | chomp; |
| 34 | if (/^charset (.*)$/) { |
| 35 | $charsetname = $1; |
| 36 | @vals = (); |
| 37 | @sortpriority = map { 0 } 0..255; |
| 38 | } elsif (/^sortpriority ([^-]*)-([^-]*) (.*)$/) { |
| 39 | for ($i = hex $1; $i <= hex $2; $i++) { |
| 40 | $sortpriority[$i] += $3; |
| 41 | } |
| 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; |
| 49 | $charsetname = undef; |
| 50 | @vals = (); |
| 51 | @sortpriority = map { 0 } 0..255; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | print "#else /* ENUM_CHARSETS */\n"; |
| 57 | print "\n"; |
| 58 | |
| 59 | foreach $i (@charsetnames) { |
| 60 | print "ENUM_CHARSET($i)\n"; |
| 61 | } |
| 62 | |
| 63 | print "\n"; |
| 64 | print "#endif /* ENUM_CHARSETS */\n"; |
| 65 | |
| 66 | sub outcharset($$$) { |
| 67 | my ($name, $vals, $sortpriority) = @_; |
| 68 | my ($prefix, $i, @sorted); |
| 69 | |
| 70 | print "static const sbcs_data data_$name = {\n"; |
| 71 | print " {\n"; |
| 72 | $prefix = " "; |
| 73 | @sorted = (); |
| 74 | for ($i = 0; $i < 256; $i++) { |
| 75 | if ($vals->[$i] < 0) { |
| 76 | printf "%sERROR ", $prefix; |
| 77 | } else { |
| 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]]; |
| 81 | } |
| 82 | if ($i % 8 == 7) { |
| 83 | $prefix = ",\n "; |
| 84 | } else { |
| 85 | $prefix = ", "; |
| 86 | } |
| 87 | } |
| 88 | print "\n },\n {\n"; |
| 89 | @sorted = sort { $a->[1] == $b->[1] ? |
| 90 | $b->[2] <=> $a->[2] : |
| 91 | $a->[1] <=> $b->[1] } @sorted; |
| 92 | $prefix = " "; |
| 93 | $uval = -1; |
| 94 | for ($i = $j = 0; $i < scalar @sorted; $i++) { |
| 95 | next if ($uval == $sorted[$i]->[1]); # low-priority alternative |
| 96 | $uval = $sorted[$i]->[1]; |
| 97 | printf "%s0x%02x", $prefix, $sorted[$i]->[0]; |
| 98 | if ($j % 8 == 7) { |
| 99 | $prefix = ",\n "; |
| 100 | } else { |
| 101 | $prefix = ", "; |
| 102 | } |
| 103 | $j++; |
| 104 | } |
| 105 | printf "\n },\n %d\n", $j; |
| 106 | print "};\n"; |
| 107 | print "const charset_spec charset_$name = {\n" . |
| 108 | " $name, read_sbcs, write_sbcs, &data_$name\n};\n\n"; |
| 109 | } |