5 BEGIN { unshift @INC, '.' }
7 require './regen/regen_lib.pl';
8 require './regen/charset_translations.pl';
10 # Generates the EBCDIC translation tables that were formerly hard-coded into
13 my $out_fh = open_new('ebcdic_tables.h', '>',
14 {style => '*', by => $0, });
16 sub output_table ($$;$) {
17 my $table_ref = shift;
20 # Tables in hex easier to debug, but don't fit into 80 columns
21 my $print_in_hex = shift // 1;
23 die "Requres 256 entries in table $name, got @$table_ref" if @$table_ref != 256;
25 print $out_fh "EXTCONST U8 $name\[\] = {\n";
27 my $column_numbers= "/*_0 _1 _2 _3 _4 _5 _6 _7 _8 _9 _A _B _C _D _E _F*/\n";
28 print $out_fh $column_numbers if $print_in_hex;
29 for my $i (0 .. 255) {
31 # No row headings, so will fit in 80 cols.
32 #printf $out_fh "/* %X_ */ ", $i / 16 if $i % 16 == 0;
33 printf $out_fh "0x%02X", $table_ref->[$i];
36 printf $out_fh "%4d", $table_ref->[$i];
38 print $out_fh ",", if $i < 255;
39 #print $out_fh ($i < 255) ? "," : " ";
40 #printf $out_fh " /* %X_ */", $i / 16 if $print_in_hex && $i % 16 == 15;
41 print $out_fh "\n" if $i % 16 == 15;
43 print $out_fh $column_numbers if $print_in_hex;
44 print $out_fh "};\n\n";
49 #ifndef PERL_EBCDIC_TABLES_H_ /* Guard against nested #includes */
50 #define PERL_EBCDIC_TABLES_H_ 1
52 /* This file contains definitions for various tables used in EBCDIC handling.
53 * More info is in utfebcdic.h */
56 my @charsets = get_supported_code_pages();
57 shift @charsets; # ASCII is the 0th, and we don't deal with that here.
58 foreach my $charset (@charsets) {
59 # we process the whole array several times, make a copy
60 my @a2e = @{get_a2n($charset)};
62 print $out_fh "\n" . get_conditional_compile_line_start($charset);
65 print $out_fh "/* Index is ASCII platform code point; value is $charset equivalent */\n";
66 output_table(\@a2e, "PL_a2e");
68 { # Construct the inverse
70 for my $i (0 .. 255) {
73 print $out_fh "/* Index is $charset code point; value is ASCII platform equivalent */\n";
74 output_table(\@e2a, "PL_e2a");
77 my @i82utf = @{get_I8_2_utf($charset)};
79 /* (Confusingly named) Index is $charset I8 byte; value is
80 * $charset UTF-EBCDIC equivalent */
82 output_table(\@i82utf, "PL_utf2e");
84 { #Construct the inverse
86 for my $i (0 .. 255) {
87 $utf2i8[$i82utf[$i]] = $i;
90 /* (Confusingly named) Index is $charset UTF-EBCDIC byte; value is
91 * $charset I8 equivalent */
93 output_table(\@utf2i8, "PL_e2utf");
99 # These are invariants or continuation bytes.
100 for my $i (0 .. 0xBF) {
101 $utf8skip[$i82utf[$i]] = 1;
104 # These are start bytes; The skip is the number of consecutive highest
105 # order 1-bits (up to 7)
106 for my $i (0xC0 .. 255) {
108 if ($i == 0b11111111) {
110 $count = $CHARSET_TRANSLATIONS::UTF_EBCDIC_MAXBYTES;
112 elsif (($i & 0b11111110) == 0b11111110) {
115 elsif (($i & 0b11111100) == 0b11111100) {
118 elsif (($i & 0b11111000) == 0b11111000) {
121 elsif (($i & 0b11110000) == 0b11110000) {
124 elsif (($i & 0b11100000) == 0b11100000) {
127 elsif (($i & 0b11000000) == 0b11000000) {
131 die "Something wrong for UTF8SKIP calculation for $i";
133 $utf8skip[$i82utf[$i]] = $count;
137 /* Index is $charset UTF-EBCDIC byte; value is UTF8SKIP for start bytes;
138 * 1 for continuation. Adapted from the shadow flags table in tr16. The
139 * entries marked 9 in tr16 are continuation bytes and are marked as length 1
140 * here so that we can recover. */
142 output_table(\@utf8skip, "PL_utf8skip", 0); # The 0 means don't print
146 use feature 'unicode_strings';
150 for my $i (0 .. 255) {
151 $lc[$a2e[$i]] = $a2e[ord lc chr $i];
153 print $out_fh "/* Index is $charset code point; value is its lowercase equivalent */\n";
154 output_table(\@lc, "PL_latin1_lc");
159 for my $i (0 .. 255) {
161 if (length $uc > 1 || ord $uc > 255) {
162 $uc = "\N{LATIN SMALL LETTER Y WITH DIAERESIS}";
164 $uc[$a2e[$i]] = $a2e[ord $uc];
167 /* Index is $charset code point; value is its uppercase equivalent.
168 * The 'mod' in the name means that codepoints whose uppercase is above 255 or
169 * longer than 1 character map to LATIN SMALL LETTER Y WITH DIARESIS */
171 output_table(\@uc, "PL_mod_latin1_uc");
176 for my $i (0 .. 255) { # Initialise to identity map
177 $ascii_fold[$i] = $i;
180 # Overwrite the entries that aren't identity
181 for my $chr ('A' .. 'Z') {
182 $ascii_fold[$a2e[ord $chr]] = $a2e[ord lc $chr];
184 for my $chr ('a' .. 'z') {
185 $ascii_fold[$a2e[ord $chr]] = $a2e[ord uc $chr];
188 /* Index is $charset code point; For A-Z, value is a-z; for a-z, value
189 * is A-Z; all other code points map to themselves */
191 output_table(\@ascii_fold, "PL_fold");
196 for my $i (0 .. 255) {
200 # lc and uc adequately proxy for fold-case pairs in this 0-255
203 $uc = $char if length $uc > 1 || ord $uc > 255;
205 $latin1_fold[$a2e[$i]] = $a2e[ord $lc];
207 elsif ($uc ne $char) {
208 $latin1_fold[$a2e[$i]] = $a2e[ord $uc];
211 $latin1_fold[$a2e[$i]] = $a2e[$i];
215 /* Index is $charset code point; value is its other fold-pair equivalent
216 * (A => a; a => A, etc) in the 0-255 range. If no such equivalent, value is
217 * the code point itself */
219 output_table(\@latin1_fold, "PL_fold_latin1");
222 print $out_fh get_conditional_compile_line_end();
225 print $out_fh "\n#endif /* PERL_EBCDIC_TABLES_H_ */\n";
227 read_only_bottom_close_and_rename($out_fh);