3 # This script generates sbcsdat.c (the data for all the SBCSes) from its
4 # source form sbcs.dat.
7 $infile = shift @ARGV if defined $ARGV[0];
8 $outfile = "sbcsdat.c";
9 $outfile = shift @ARGV if defined $ARGV[0];
10 $outheader = "sbcsdat.h";
11 $outheader = shift @ARGV if defined $ARGV[0];
14 open BAR
, ">$outfile";
18 print " * sbcsdat.c - data definitions for single-byte character sets.\n";
20 print " * Generated by sbcsgen.pl from sbcs.dat.\n";
21 print " * You should edit those files rather than editing this one.\n";
24 print "#ifndef ENUM_CHARSETS\n";
26 print "#include \"charset.h\"\n";
27 print "#include \"internal.h\"\n";
30 my $charsetname = undef;
33 my @charsetnames = ();
34 my @sortpriority = ();
38 y/\r\n//; # robustness in the face of strange line endings
39 if (/^charset (.*)$/) {
42 @sortpriority = map { 0 } 0..255;
43 } elsif (/^sortpriority ([^-]*)-([^-]*) (.*)$/) {
44 for ($i = hex $1; $i <= hex $2; $i++) {
45 $sortpriority[$i] += $3;
47 } elsif (/^[0-9a-fA-FX]/) {
48 push @vals, map { $_ eq "XXXX" ?
-1 : hex $_ } split / +/, $_;
49 if (scalar @vals > 256) {
50 die "$infile:$.: charset $charsetname has more than 256 values\n";
51 } elsif (scalar @vals == 256) {
52 &outcharset
($charsetname, \
@vals, \
@sortpriority);
53 push @charsetnames, $charsetname;
56 @sortpriority = map { 0 } 0..255;
61 print "#else /* ENUM_CHARSETS */\n";
64 foreach $i (@charsetnames) {
65 print "ENUM_CHARSET($i)\n";
69 print "#endif /* ENUM_CHARSETS */\n";
73 open BAR
, ">$outheader";
77 print " * sbcsdat.h - header file for SBCS data structures.\n";
79 print " * Generated by sbcsgen.pl from sbcs.dat.\n";
80 print " * You should edit those files rather than editing this one.\n";
83 print "#ifndef charset_sbcsdat_h\n";
84 print "#define charset_sbcsdat_h\n";
86 print "#include \"charset.h\"\n";
87 print "#include \"internal.h\"\n";
89 foreach $i (@charsetnames) {
90 print "extern const sbcs_data sbcsdata_$i;\n";
93 print "#endif /* charset_sbcsdat_h */\n";
98 my ($name, $vals, $sortpriority) = @_;
99 my ($prefix, $i, @sorted);
101 print "const sbcs_data sbcsdata_$name = {\n";
105 for ($i = 0; $i < 256; $i++) {
106 if ($vals->[$i] < 0) {
107 printf "%sERROR ", $prefix;
109 printf "%s0x%04x", $prefix, $vals->[$i];
110 die "ooh? $i\n" unless defined $sortpriority->[$i];
111 push @sorted, [$i, $vals->[$i], 0+$sortpriority->[$i]];
120 @sorted = sort { ($a->[1] == $b->[1] ?
121 $b->[2] <=> $a->[2] :
122 $a->[1] <=> $b->[1]) ||
123 $a->[0] <=> $b->[0] } @sorted;
126 for ($i = $j = 0; $i < scalar @sorted; $i++) {
127 next if ($uval == $sorted[$i]->[1]); # low-priority alternative
128 $uval = $sorted[$i]->[1];
129 printf "%s0x%02x", $prefix, $sorted[$i]->[0];
137 printf "\n },\n %d\n", $j;
139 print "const charset_spec charset_$name = {\n" .
140 " $name, read_sbcs, write_sbcs, &sbcsdata_$name\n};\n\n";