sbcsgen.pl 2.7 KB

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