3 # Generate code page .c files from ftp.unicode.org descriptions
5 # Copyright 2000 Alexandre Julliard
8 # base directory for ftp.unicode.org files
9 $BASEDIR = "ftp.unicode.org/Public/";
10 $MAPPREFIX = $BASEDIR . "MAPPINGS/";
13 $UNICODEDATA = $BASEDIR . "UNIDATA/UnicodeData.txt";
16 $DEFAULTS = "./defaults";
18 # Default char for undefined mappings
23 [ 37, "VENDORS/MICSFT/EBCDIC/CP037.TXT", "IBM EBCDIC US Canada" ],
24 [ 42, "VENDORS/ADOBE/symbol.txt", "Symbol" ],
25 [ 424, "VENDORS/MISC/CP424.TXT", "IBM EBCDIC Hebrew" ],
26 [ 437, "VENDORS/MICSFT/PC/CP437.TXT", "OEM United States" ],
27 [ 500, "VENDORS/MICSFT/EBCDIC/CP500.TXT", "IBM EBCDIC International" ],
28 [ 737, "VENDORS/MICSFT/PC/CP737.TXT", "OEM Greek 437G" ],
29 [ 775, "VENDORS/MICSFT/PC/CP775.TXT", "OEM Baltic" ],
30 [ 850, "VENDORS/MICSFT/PC/CP850.TXT", "OEM Multilingual Latin 1" ],
31 [ 852, "VENDORS/MICSFT/PC/CP852.TXT", "OEM Slovak Latin 2" ],
32 [ 855, "VENDORS/MICSFT/PC/CP855.TXT", "OEM Cyrillic" ],
33 [ 856, "VENDORS/MISC/CP856.TXT", "Hebrew PC" ],
34 [ 857, "VENDORS/MICSFT/PC/CP857.TXT", "OEM Turkish" ],
35 [ 860, "VENDORS/MICSFT/PC/CP860.TXT", "OEM Portuguese" ],
36 [ 861, "VENDORS/MICSFT/PC/CP861.TXT", "OEM Icelandic" ],
37 [ 862, "VENDORS/MICSFT/PC/CP862.TXT", "OEM Hebrew" ],
38 [ 863, "VENDORS/MICSFT/PC/CP863.TXT", "OEM Canadian French" ],
39 [ 864, "VENDORS/MICSFT/PC/CP864.TXT", "OEM Arabic" ],
40 [ 865, "VENDORS/MICSFT/PC/CP865.TXT", "OEM Nordic" ],
41 [ 866, "VENDORS/MICSFT/PC/CP866.TXT", "OEM Russian" ],
42 [ 869, "VENDORS/MICSFT/PC/CP869.TXT", "OEM Greek" ],
43 [ 874, "VENDORS/MICSFT/PC/CP874.TXT", "ANSI/OEM Thai" ],
44 [ 875, "VENDORS/MICSFT/EBCDIC/CP875.TXT", "IBM EBCDIC Greek" ],
45 [ 878, "VENDORS/MISC/KOI8-R.TXT", "Russian KOI8" ],
46 [ 932, "VENDORS/MICSFT/WINDOWS/CP932.TXT", "ANSI/OEM Japanese Shift-JIS" ],
47 [ 936, "VENDORS/MICSFT/WINDOWS/CP936.TXT", "ANSI/OEM Simplified Chinese GBK" ],
48 [ 949, "VENDORS/MICSFT/WINDOWS/CP949.TXT", "ANSI/OEM Korean Unified Hangul" ],
49 [ 950, "VENDORS/MICSFT/WINDOWS/CP950.TXT", "ANSI/OEM Traditional Chinese Big5" ],
50 [ 1006, "VENDORS/MISC/CP1006.TXT", "IBM Arabic" ],
51 [ 1026, "VENDORS/MICSFT/EBCDIC/CP1026.TXT", "IBM EBCDIC Latin 5 Turkish" ],
52 [ 1250, "VENDORS/MICSFT/WINDOWS/CP1250.TXT", "ANSI Eastern Europe" ],
53 [ 1251, "VENDORS/MICSFT/WINDOWS/CP1251.TXT", "ANSI Cyrillic" ],
54 [ 1252, "VENDORS/MICSFT/WINDOWS/CP1252.TXT", "ANSI Latin 1" ],
55 [ 1253, "VENDORS/MICSFT/WINDOWS/CP1253.TXT", "ANSI Greek" ],
56 [ 1254, "VENDORS/MICSFT/WINDOWS/CP1254.TXT", "ANSI Turkish" ],
57 [ 1255, "VENDORS/MICSFT/WINDOWS/CP1255.TXT", "ANSI Hebrew" ],
58 [ 1256, "VENDORS/MICSFT/WINDOWS/CP1256.TXT", "ANSI Arabic" ],
59 [ 1257, "VENDORS/MICSFT/WINDOWS/CP1257.TXT", "ANSI Baltic" ],
60 [ 1258, "VENDORS/MICSFT/WINDOWS/CP1258.TXT", "ANSI/OEM Viet Nam" ],
61 [ 10000, "VENDORS/MICSFT/MAC/ROMAN.TXT", "Mac Roman" ],
62 [ 10006, "VENDORS/MICSFT/MAC/GREEK.TXT", "Mac Greek" ],
63 [ 10007, "VENDORS/MICSFT/MAC/CYRILLIC.TXT", "Mac Cyrillic" ],
64 [ 10029, "VENDORS/MICSFT/MAC/LATIN2.TXT", "Mac Latin 2" ],
65 [ 10079, "VENDORS/MICSFT/MAC/ICELAND.TXT", "Mac Icelandic" ],
66 [ 10081, "VENDORS/MICSFT/MAC/TURKISH.TXT", "Mac Turkish" ],
67 [ 20866, "VENDORS/MISC/KOI8-R.TXT", "Russian KOI8" ],
68 [ 28591, "ISO8859/8859-1.TXT", "ISO 8859-1 Latin 1" ],
69 [ 28592, "ISO8859/8859-2.TXT", "ISO 8859-2 Eastern Europe" ],
70 [ 28593, "ISO8859/8859-3.TXT", "ISO 8859-3 Turkish" ],
71 [ 28594, "ISO8859/8859-4.TXT", "ISO 8859-4 Baltic" ],
72 [ 28595, "ISO8859/8859-5.TXT", "ISO 8859-5 Cyrillic" ],
73 [ 28596, "ISO8859/8859-6.TXT", "ISO 8859-6 Arabic" ],
74 [ 28597, "ISO8859/8859-7.TXT", "ISO 8859-7 Greek" ],
75 [ 28598, "ISO8859/8859-8.TXT", "ISO 8859-8 Hebrew" ],
76 [ 28599, "ISO8859/8859-9.TXT", "ISO 8859-9 Latin 5" ]
79 ################################################################
85 foreach $file (@allfiles) { HANDLE_FILE
( @
$file ); }
92 ################################################################
93 # read in the defaults file
96 @unicode_defaults = ();
97 @unicode_aliases = ();
101 # first setup a few default mappings
103 open DEFAULTS
or die "Cannot open $DEFAULTS";
104 print "Loading $DEFAULTS\n";
107 next if /^\#/; # skip comments
108 next if /^$/; # skip empty lines
109 if (/^(([0-9a-fA-F]+)(,[0-9a-fA-F]+)*)\s+([0-9a-fA-F]+|'.'|none)\s+(\#.*)?/)
111 my @src = map hex, split /,/,$1;
114 if ($#src > 0) { push @unicode_aliases, \
@src; }
115 next if ($dst eq "none");
116 $dst = ($dst =~ /\'.\'/) ?
ord substr($dst,1,1) : hex $dst;
119 die "Duplicate value" if defined($unicode_defaults[$src]);
120 $unicode_defaults[$src] = $dst;
124 die "Unrecognized line $_\n";
127 # now build mappings from the decomposition field of the Unicode database
129 open UNICODEDATA
or die "Cannot open $UNICODEDATA";
130 print "Loading $UNICODEDATA\n";
131 while (<UNICODEDATA
>)
133 # Decode the fields ...
134 ($code, $name, $cat, $comb, $bidi,
135 $decomp, $dec, $dig, $num, $mirror,
136 $oldname, $comment, $upper, $lower, $title) = split /;/;
138 if ($lower ne "") { $tolower_table[hex $code] = hex $lower; }
139 if ($upper ne "") { $toupper_table[hex $code] = hex $upper; }
141 next if $decomp eq ""; # no decomposition, skip it
145 if ($decomp =~ /^<([a-zA-Z]+)>\s+([0-9a-fA-F]+)$/)
147 # decomposition of the form "<foo> 1234" -> use char if type is known
148 next unless ($1 eq "font" ||
159 elsif ($decomp =~ /^<compat>\s+0020\s+([0-9a-fA-F]+)/)
161 # decomposition "<compat> 0020 1234" -> combining accent
164 elsif ($decomp =~ /^([0-9a-fA-F]+)/)
166 # decomposition contains only char values without prefix -> use first char
174 next if defined($unicode_defaults[$src]); # may have been set in the defaults file
177 for ($i = $dst; ; $i = $unicode_defaults[$i])
179 die sprintf("loop detected for %04x -> %04x",$src,$dst) if $i == $src;
180 last unless defined($unicode_defaults[$i]);
182 $unicode_defaults[$src] = $dst;
187 ################################################################
188 # parse the input file
192 open INPUT
,$name or die "Cannot open $name";
199 next if /^\#/; # skip comments
200 next if /^$/; # skip empty lines
201 next if /\x1a/; # skip ^Z
202 next if (/^0x([0-9a-fA-F]+)\s+\#UNDEFINED/); # undefined char
204 if (/^0x([0-9a-fA-F]+)\s+\#DBCS LEAD BYTE/)
207 push @lead_bytes,$cp;
211 if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
215 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
216 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
219 die "$name: Unrecognized line $_\n";
224 ################################################################
225 # parse the symbol.txt file, since its syntax is different from the other ones
229 open INPUT
,$name or die "Cannot open $name";
236 next if /^\#/; # skip comments
237 next if /^$/; # skip empty lines
238 next if /\x1a/; # skip ^Z
239 if (/^([0-9a-fA-F]+)\s+([0-9a-fA-F]+)\s+(\#.*)?/)
243 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
244 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
247 die "$name: Unrecognized line $_\n";
252 ################################################################
253 # add default mappings once the file had been read
254 sub ADD_DEFAULT_MAPPINGS
258 foreach $alias (@unicode_aliases)
261 foreach $src (@
$alias)
263 if (defined($uni2cp[$src]))
265 $target = $uni2cp[$src];
269 next unless defined($target);
271 # At least one char of the alias set is defined, set the others to the same value
272 foreach $src (@
$alias)
274 $uni2cp[$src] = $target unless defined($uni2cp[$src]);
278 # For every src -> target mapping in the defaults table,
279 # make uni2cp[src] = uni2cp[target] if uni2cp[target] is defined
281 for ($src = 0; $src < 65536; $src++)
283 next if defined($uni2cp[$src]); # source has a definition already
284 next unless defined($unicode_defaults[$src]); # no default for this char
285 my $target = $unicode_defaults[$src];
287 # do a recursive mapping until we find a target char that is defined
288 while (!defined($uni2cp[$target]) &&
289 defined($unicode_defaults[$target])) { $target = $unicode_defaults[$target]; }
291 if (defined($uni2cp[$target])) { $uni2cp[$src] = $uni2cp[$target]; }
294 # Add an identity mapping for all undefined chars
296 for ($i = 0; $i < 256; $i++)
298 next if defined($cp2uni[$i]);
299 next if defined($uni2cp[$i]);
300 $cp2uni[$i] = $uni2cp[$i] = $i;
304 ################################################################
305 # dump an array of integers
308 my ($format,$default,@array) = @_;
310 for ($i = 0; $i < $#array; $i++)
312 $ret .= sprintf($format, defined $array[$i] ?
$array[$i] : $default);
313 $ret .= (($i % 8) != 7) ?
", " : ",\n ";
315 $ret .= sprintf($format, defined $array[$i] ?
$array[$i] : $default);
319 ################################################################
320 # dump an SBCS mapping table
323 my ($codepage, $name) = @_;
326 # output the ascii->unicode table
328 printf OUTPUT
"static const WCHAR cp2uni[256] =\n";
329 printf OUTPUT
"{\n%s\n};\n\n", DUMP_ARRAY
( "0x%04x", $DEF_CHAR, @cp2uni[0 .. 255] );
331 # count the number of unicode->ascii subtables that contain something
335 for ($i = 0; $i < 65536; $i++)
337 next unless defined $uni2cp[$i];
338 $filled[$i >> 8] = 1;
340 $i = ($i & ~255) + 256;
343 # output all the subtables into a single array
345 printf OUTPUT
"static const unsigned char uni2cp_low[%d] =\n{\n", $subtables*256;
346 for ($i = 0; $i < 256; $i++)
348 next unless $filled[$i];
349 printf OUTPUT
" /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
350 printf OUTPUT
"%s,\n", DUMP_ARRAY
( "0x%02x", $DEF_CHAR, @uni2cp[($i<<8) .. ($i<<8)+255] );
352 printf OUTPUT
" /* defaults */\n";
353 printf OUTPUT
"%s\n};\n\n", DUMP_ARRAY
( "0x%02x", 0, ($DEF_CHAR) x
256 );
355 # output a table of the offsets of the subtables in the previous array
359 for ($i = 0; $i < 256; $i++)
361 if ($filled[$i]) { push @offsets, $pos; $pos += 256; }
362 else { push @offsets, ($subtables-1) * 256; }
364 printf OUTPUT
"static const unsigned short uni2cp_high[256] =\n";
365 printf OUTPUT
"{\n%s\n};\n\n", DUMP_ARRAY
( "0x%04x", 0, @offsets );
367 # output the code page descriptor
369 printf OUTPUT
"const struct sbcs_table cptable_%03d =\n{\n", $codepage;
370 printf OUTPUT
" { %d, 1, 0x%04x, 0x%04x, \"%s\" },\n",
371 $codepage, $DEF_CHAR, $DEF_CHAR, $name;
372 printf OUTPUT
" cp2uni,\n";
373 printf OUTPUT
" uni2cp_low,\n";
374 printf OUTPUT
" uni2cp_high\n};\n";
378 ################################################################
379 # dump a DBCS mapping table
382 my ($codepage, $name) = @_;
385 # build a list of lead bytes that are actually used
388 LBLOOP
: for ($y = 0; $y <= $#lead_bytes; $y++)
390 my $base = $lead_bytes[$y] << 8;
391 for ($x = 0; $x < 256; $x++)
393 if (defined $cp2uni[$base+$x])
395 push @lblist,$lead_bytes[$y];
400 my $unused = ($#lead_bytes > $#lblist);
402 # output the ascii->unicode table for the single byte chars
404 printf OUTPUT
"static const WCHAR cp2uni[%d] =\n", 256 * ($#lblist + 2 + $unused);
405 printf OUTPUT
"{\n%s,\n", DUMP_ARRAY
( "0x%04x", $DEF_CHAR, @cp2uni[0 .. 255] );
407 # output the default table for unused lead bytes
411 printf OUTPUT
" /* unused lead bytes */\n";
412 printf OUTPUT
"%s,\n", DUMP_ARRAY
( "0x%04x", 0, ($DEF_CHAR) x
256 );
415 # output the ascii->unicode table for each DBCS lead byte
417 for ($y = 0; $y <= $#lblist; $y++)
419 my $base = $lblist[$y] << 8;
420 printf OUTPUT
" /* lead byte %02x */\n", $lblist[$y];
421 printf OUTPUT
"%s", DUMP_ARRAY
( "0x%04x", $DEF_CHAR, @cp2uni[$base .. $base+255] );
422 printf OUTPUT
($y < $#lblist) ?
",\n" : "\n};\n\n";
425 # output the lead byte subtables offsets
428 for ($x = 0; $x < 256; $x++) { $offsets[$x] = 0; }
429 for ($x = 0; $x <= $#lblist; $x++) { $offsets[$lblist[$x]] = $x + 1; }
432 # increment all lead bytes offset to take into account the unused table
433 for ($x = 0; $x <= $#lead_bytes; $x++) { $offsets[$lead_bytes[$x]]++; }
435 printf OUTPUT
"static const unsigned char cp2uni_leadbytes[256] =\n";
436 printf OUTPUT
"{\n%s\n};\n\n", DUMP_ARRAY
( "0x%02x", 0, @offsets );
438 # count the number of unicode->ascii subtables that contain something
442 for ($i = 0; $i < 65536; $i++)
444 next unless defined $uni2cp[$i];
445 $filled[$i >> 8] = 1;
447 $i = ($i & ~255) + 256;
450 # output all the subtables into a single array
452 printf OUTPUT
"static const unsigned short uni2cp_low[%d] =\n{\n", $subtables*256;
453 for ($y = 0; $y < 256; $y++)
455 next unless $filled[$y];
456 printf OUTPUT
" /* 0x%02x00 .. 0x%02xff */\n", $y, $y;
457 printf OUTPUT
"%s,\n", DUMP_ARRAY
( "0x%04x", $DEF_CHAR, @uni2cp[($y<<8) .. ($y<<8)+255] );
459 printf OUTPUT
" /* defaults */\n";
460 printf OUTPUT
"%s\n};\n\n", DUMP_ARRAY
( "0x%04x", 0, ($DEF_CHAR) x
256 );
462 # output a table of the offsets of the subtables in the previous array
466 for ($y = 0; $y < 256; $y++)
468 if ($filled[$y]) { push @offsets, $pos; $pos += 256; }
469 else { push @offsets, ($subtables-1) * 256; }
471 printf OUTPUT
"static const unsigned short uni2cp_high[256] =\n";
472 printf OUTPUT
"{\n%s\n};\n\n", DUMP_ARRAY
( "0x%04x", 0, @offsets );
474 # output the code page descriptor
476 printf OUTPUT
"const struct dbcs_table cptable_%03d =\n{\n", $codepage;
477 printf OUTPUT
" { %d, 2, 0x%04x, 0x%04x, \"%s\" },\n",
478 $codepage, $DEF_CHAR, $DEF_CHAR, $name;
479 printf OUTPUT
" cp2uni,\n";
480 printf OUTPUT
" cp2uni_leadbytes,\n";
481 printf OUTPUT
" uni2cp_low,\n";
482 printf OUTPUT
" uni2cp_high,\n";
484 printf OUTPUT
"};\n";
488 ################################################################
489 # dump the list of defined lead byte ranges
494 foreach $i (@lead_bytes) { $list[$i] = 1; }
497 for ($i = 0; $i < 256; $i++)
501 if (!defined $list[$i]) { printf OUTPUT
"0x%02x, ", $i-1; $on = 0; }
505 if ($list[$i]) { printf OUTPUT
"0x%02x, ", $i; $on = 1; }
508 if ($on) { printf OUTPUT
"0xff, "; }
509 printf OUTPUT
"0x00, 0x00 }\n";
513 ################################################################
514 # dump the case mapping tables
515 sub DUMP_CASE_MAPPINGS
517 open OUTPUT
,">casemap.c" or die "Cannot create casemap.c";
518 printf "Building casemap.c\n";
519 printf OUTPUT
"/* Unicode case mappings */\n";
520 printf OUTPUT
"/* Automatically generated; DO NOT EDIT!! */\n\n";
521 printf OUTPUT
"#include \"wine/unicode.h\"\n\n";
523 DUMP_CASE_TABLE
( "casemap_lower", @tolower_table );
524 DUMP_CASE_TABLE
( "casemap_upper", @toupper_table );
529 ################################################################
530 # dump a case mapping table
533 my ($name,@table) = @_;
535 # count the number of sub tables that contain something
539 for ($i = 0; $i < 65536; $i++)
541 next unless defined $table[$i];
542 $filled[$i >> 8] = $pos;
544 $i = ($i & ~255) + 256;
546 for ($i = 0; $i < 65536; $i++)
548 next unless defined $table[$i];
549 $table[$i] = ($table[$i] - $i) & 0xffff;
554 printf OUTPUT
"const WCHAR %s[%d] =\n", $name, $pos;
555 printf OUTPUT
"{\n /* index */\n";
556 printf OUTPUT
"%s,\n", DUMP_ARRAY
( "0x%04x", 256, @filled );
557 printf OUTPUT
" /* defaults */\n";
558 printf OUTPUT
"%s", DUMP_ARRAY
( "0x%04x", 0, (0) x
256 );
559 for ($i = 0; $i < 256; $i++)
561 next unless $filled[$i];
562 printf OUTPUT
",\n /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
563 printf OUTPUT
"%s", DUMP_ARRAY
( "0x%04x", 0, @table[($i<<8) .. ($i<<8)+255] );
565 printf OUTPUT
"\n};\n";
569 ################################################################
570 # read an input file and generate the corresponding .c file
573 my ($codepage,$filename,$comment) = @_;
575 # symbol codepage file is special
576 if ($codepage == 42) { READ_SYMBOL_FILE
($MAPPREFIX . $filename); }
577 else { READ_FILE
($MAPPREFIX . $filename); }
579 ADD_DEFAULT_MAPPINGS
();
581 my $output = sprintf "c_%03d.c", $codepage;
582 open OUTPUT
,">$output" or die "Cannot create $output";
584 printf "Building %s from %s (%s)\n", $output, $filename, $comment;
588 printf OUTPUT
"/* code page %03d (%s) */\n", $codepage, $comment;
589 printf OUTPUT
"/* generated from %s */\n", $MAPPREFIX . $filename;
590 printf OUTPUT
"/* DO NOT EDIT!! */\n\n";
591 printf OUTPUT
"#include \"wine/unicode.h\"\n\n";
593 if ($#lead_bytes == -1) { DUMP_SBCS_TABLE
( $codepage, $comment ); }
594 else { DUMP_DBCS_TABLE
( $codepage, $comment ); }
599 ################################################################
600 # output the list of codepage tables into the cptable.c file
605 foreach $file (@allfiles)
607 my ($codepage,$filename,$comment) = @
$file;
608 push @tables_decl, sprintf("extern union cptable cptable_%03d;\n",$codepage);
611 push @tables_decl, sprintf("\nstatic const union cptable * const cptables[%d] =\n{\n",$#allfiles+1);
612 foreach $file (@allfiles)
614 my ($codepage,$filename,$comment) = @
$file;
615 push @tables_decl, sprintf(" &cptable_%03d,\n", $codepage);
617 push @tables_decl, "};";
618 REPLACE_IN_FILE
( "cptable.c", @tables_decl );
621 ################################################################
622 # replace the contents of a file between ### cpmap ### marks
629 open(FILE
,$name) or die "Can't open $name";
633 last if /\#\#\# cpmap begin \#\#\#/;
638 if (/\#\#\# cpmap end \#\#\#/) { push @lines, "\n", $_; last; }
641 open(FILE
,">$name") or die "Can't modify $name";