OS/390 seems to do length 0 udp reads, Nicholas says
[p5sagit/p5-mst-13.2.git] / ext / Encode / compile
CommitLineData
017e2add 1#!../../perl -w
508a30f1 2BEGIN {
df1df145 3 unshift @INC, '../../lib';
508a30f1 4 $ENV{PATH} .= ';../..' if $^O eq 'MSWin32';
5}
017e2add 6use strict;
afdae191 7use Getopt::Std;
8my @orig_ARGV = @ARGV;
dcda1f94 9
017e2add 10sub encode_U
11{
9b37254d 12 # UTF-8 encode long hand - only covers part of perl's range
017e2add 13 my $uv = shift;
1b026014 14 # chr() works in native space so convert value from table
15 # into that space before using chr().
16 my $ch = chr(utf8::unicode_to_native($uv));
17 # Now get core perl to encode that the way it likes.
18 utf8::encode($ch);
19 return $ch;
017e2add 20}
21
22sub encode_S
23{
14a8264b 24 # encode single byte
017e2add 25 my ($ch,$page) = @_;
26 return chr($ch);
27}
28
29sub encode_D
30{
14a8264b 31 # encode double byte MS byte first
017e2add 32 my ($ch,$page) = @_;
33 return chr($page).chr($ch);
34}
35
36sub encode_M
37{
14a8264b 38 # encode Multi-byte - single for 0..255 otherwise double
017e2add 39 my ($ch,$page) = @_;
40 return &encode_D if $page;
41 return &encode_S;
42}
43
14a8264b 44# Win32 does not expand globs on command line
252a8565 45eval "\@ARGV = map(glob(\$_),\@ARGV)" if ($^O eq 'MSWin32');
18b7339f 46
afdae191 47my %opt;
3865e8e1 48getopts('qo:f:n:',\%opt);
afdae191 49my $cname = (exists $opt{'o'}) ? $opt{'o'} : shift(@ARGV);
2f2b4ff2 50chmod(0666,$cname) if -f $cname && !-w $cname;
017e2add 51open(C,">$cname") || die "Cannot open $cname:$!";
afdae191 52
53
2f2b4ff2 54my $dname = $cname;
55$dname =~ s/(\.[^\.]*)?$/.def/;
e0c49a6b 56
dcda1f94 57my ($doC,$doEnc,$doUcm,$doPet);
e0c49a6b 58
59if ($cname =~ /\.(c|xs)$/)
60 {
61 $doC = 1;
62 chmod(0666,$dname) if -f $cname && !-w $dname;
63 open(D,">$dname") || die "Cannot open $dname:$!";
64 my $hname = $cname;
65 $hname =~ s/(\.[^\.]*)?$/.h/;
66 chmod(0666,$hname) if -f $cname && !-w $hname;
67 open(H,">$hname") || die "Cannot open $hname:$!";
68
69 foreach my $fh (\*C,\*D,\*H)
70 {
afdae191 71 print $fh <<"END" unless $opt{'q'};
14a8264b 72/*
73 !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
74 This file was autogenerated by:
afdae191 75 $^X $0 $cname @orig_ARGV
14a8264b 76*/
77END
e0c49a6b 78 }
14a8264b 79
e0c49a6b 80 if ($cname =~ /(\w+)\.xs$/)
81 {
82 print C "#include <EXTERN.h>\n";
83 print C "#include <perl.h>\n";
84 print C "#include <XSUB.h>\n";
85 print C "#define U8 U8\n";
86 }
87 print C "#include \"encode.h\"\n";
88 }
89elsif ($cname =~ /\.enc$/)
90 {
91 $doEnc = 1;
92 }
93elsif ($cname =~ /\.ucm$/)
2f2b4ff2 94 {
e0c49a6b 95 $doUcm = 1;
2f2b4ff2 96 }
dcda1f94 97elsif ($cname =~ /\.pet$/)
98 {
99 $doPet = 1;
100 }
017e2add 101
afdae191 102my @encfiles;
3865e8e1 103if (exists $opt{'f'})
afdae191 104 {
105 # -F is followed by name of file containing list of filenames
3865e8e1 106 my $flist = $opt{'f'};
afdae191 107 open(FLIST,$flist) || die "Cannot open $flist:$!";
108 chomp(@encfiles = <FLIST>);
109 close(FLIST);
110 }
111else
112 {
113 @encfiles = @ARGV;
114 }
c6fdb90a 115
017e2add 116my %encoding;
117my %strings;
118
2f2b4ff2 119sub cmp_name
120{
121 if ($a =~ /^.*-(\d+)/)
122 {
123 my $an = $1;
124 if ($b =~ /^.*-(\d+)/)
125 {
126 my $r = $an <=> $1;
127 return $r if $r;
128 }
129 }
130 return $a cmp $b;
131}
132
dcda1f94 133
c6fdb90a 134foreach my $enc (sort cmp_name @encfiles)
017e2add 135 {
9b37254d 136 my ($name,$sfx) = $enc =~ /^.*?([\w-]+)\.(enc|ucm)$/;
51ef4e11 137 $name = $opt{'n'} if exists $opt{'n'};
017e2add 138 if (open(E,$enc))
139 {
9b37254d 140 if ($sfx eq 'enc')
141 {
dcda1f94 142 compile_enc(\*E,lc($name));
9b37254d 143 }
144 else
145 {
dcda1f94 146 compile_ucm(\*E,lc($name));
9b37254d 147 }
017e2add 148 }
149 else
150 {
151 warn "Cannot open $enc for $name:$!";
152 }
153 }
154
e0c49a6b 155if ($doC)
2f2b4ff2 156 {
dcda1f94 157 foreach my $name (sort cmp_name keys %encoding)
158 {
159 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
160 output(\*C,$name.'_utf8',$e2u);
161 output(\*C,'utf8_'.$name,$u2e);
162 push(@{$encoding{$name}},outstring(\*C,$e2u->{Cname}.'_def',$erep));
163 }
e0c49a6b 164 foreach my $enc (sort cmp_name keys %encoding)
165 {
dcda1f94 166 my ($e2u,$u2e,$rep,$min_el,$max_el,$rsym) = @{$encoding{$enc}};
167 my @info = ($e2u->{Cname},$u2e->{Cname},$rsym,length($rep),$min_el,$max_el);
e0c49a6b 168 my $sym = "${enc}_encoding";
169 $sym =~ s/\W+/_/g;
170 print C "encode_t $sym = \n";
dcda1f94 171 print C " {",join(',',@info,"{\"$enc\",(const char *)0}"),"};\n\n";
e0c49a6b 172 }
2f2b4ff2 173
e0c49a6b 174 foreach my $enc (sort cmp_name keys %encoding)
175 {
176 my $sym = "${enc}_encoding";
177 $sym =~ s/\W+/_/g;
178 print H "extern encode_t $sym;\n";
179 print D " Encode_Define(aTHX_ &$sym);\n";
180 }
017e2add 181
e0c49a6b 182 if ($cname =~ /(\w+)\.xs$/)
183 {
184 my $mod = $1;
185 print C "\nMODULE = Encode::$mod\tPACKAGE = Encode::$mod\n\n";
186 print C "BOOT:\n{\n";
187 print C "#include \"$dname\"\n";
188 print C "}\n";
189 }
190 close(D);
191 close(H);
2f2b4ff2 192 }
dcda1f94 193elsif ($doEnc)
194 {
195 foreach my $name (sort cmp_name keys %encoding)
196 {
197 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
198 output_enc(\*C,$name,$e2u);
199 }
200 }
201elsif ($doUcm)
202 {
203 foreach my $name (sort cmp_name keys %encoding)
204 {
205 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
206 output_ucm(\*C,$name,$u2e,$erep,$min_el,$max_el);
207 }
208 }
209
017e2add 210close(C);
211
9b37254d 212
213sub compile_ucm
214{
dcda1f94 215 my ($fh,$name) = @_;
9b37254d 216 my $e2u = {};
217 my $u2e = {};
218 my $cs;
219 my %attr;
220 while (<$fh>)
221 {
222 s/#.*$//;
223 last if /^\s*CHARMAP\s*$/i;
224 if (/^\s*<(\w+)>\s+"?([^"]*)"?\s*$/i)
225 {
226 $attr{$1} = $2;
227 }
228 }
229 if (!defined($cs = $attr{'code_set_name'}))
230 {
231 warn "No <code_set_name> in $name\n";
232 }
233 else
234 {
51ef4e11 235 $name = $cs unless exists $opt{'n'};
9b37254d 236 }
237 my $erep;
238 my $urep;
afdae191 239 my $max_el;
240 my $min_el;
9b37254d 241 if (exists $attr{'subchar'})
242 {
afdae191 243 my @byte;
244 $attr{'subchar'} =~ /^\s*/cg;
245 push(@byte,$1) while $attr{'subchar'} =~ /\G\\x([0-9a-f]+)/icg;
246 $erep = join('',map(chr(hex($_)),@byte));
9b37254d 247 }
d049a12c 248 print "Scanning $name ($cs)\n";
9b37254d 249 my $nfb = 0;
250 my $hfb = 0;
251 while (<$fh>)
252 {
253 s/#.*$//;
254 last if /^\s*END\s+CHARMAP\s*$/i;
255 next if /^\s*$/;
afdae191 256 my ($u,@byte);
257 my $fb = '';
258 $u = $1 if (/^<U([0-9a-f]+)>\s+/igc);
259 push(@byte,$1) while /\G\\x([0-9a-f]+)/igc;
260 $fb = $1 if /\G\s*(\|[0-3])/gc;
261 # warn "$_: $u @byte | $fb\n";
262 die "Bad line:$_" unless /\G\s*(#.*)?$/gc;
9b37254d 263 if (defined($u))
264 {
265 my $uch = encode_U(hex($u));
e0c49a6b 266 my $ech = join('',map(chr(hex($_)),@byte));
afdae191 267 my $el = length($ech);
268 $max_el = $el if (!defined($max_el) || $el > $max_el);
269 $min_el = $el if (!defined($min_el) || $el < $min_el);
9b37254d 270 if (length($fb))
271 {
272 $fb = substr($fb,1);
273 $hfb++;
274 }
275 else
276 {
277 $nfb++;
278 $fb = '0';
279 }
280 # $fb is fallback flag
281 # 0 - round trip safe
282 # 1 - fallback for unicode -> enc
283 # 2 - skip sub-char mapping
284 # 3 - fallback enc -> unicode
285 enter($u2e,$uch,$ech,$u2e,$fb+0) if ($fb =~ /[01]/);
286 enter($e2u,$ech,$uch,$e2u,$fb+0) if ($fb =~ /[03]/);
287 }
288 else
289 {
290 warn $_;
291 }
9b37254d 292 }
293 if ($nfb && $hfb)
294 {
295 die "$nfb entries without fallback, $hfb entries with\n";
296 }
dcda1f94 297 $encoding{$name} = [$e2u,$u2e,$erep,$min_el,$max_el];
9b37254d 298}
299
14a8264b 300sub compile_enc
017e2add 301{
dcda1f94 302 my ($fh,$name) = @_;
017e2add 303 my $e2u = {};
304 my $u2e = {};
305
306 my $type;
307 while ($type = <$fh>)
308 {
309 last if $type !~ /^\s*#/;
310 }
311 chomp($type);
312 return if $type eq 'E';
313 my ($def,$sym,$pages) = split(/\s+/,scalar(<$fh>));
14a8264b 314 warn "$type encoded $name\n";
017e2add 315 my $rep = '';
afdae191 316 my $min_el;
317 my $max_el;
017e2add 318 {
319 my $v = hex($def);
320 no strict 'refs';
321 $rep = &{"encode_$type"}($v & 0xFF, ($v >> 8) & 0xffe);
322 }
e03ac092 323 my %seen;
017e2add 324 while ($pages--)
325 {
326 my $line = <$fh>;
327 chomp($line);
328 my $page = hex($line);
329 my $ch = 0;
330 for (my $i = 0; $i < 16; $i++)
331 {
332 my $line = <$fh>;
333 for (my $j = 0; $j < 16; $j++)
334 {
335 no strict 'refs';
336 my $ech = &{"encode_$type"}($ch,$page);
337 my $val = hex(substr($line,0,4,''));
e03ac092 338 next if $val == 0xFFFD;
017e2add 339 if ($val || (!$ch && !$page))
340 {
afdae191 341 my $el = length($ech);
342 $max_el = $el if (!defined($max_el) || $el > $max_el);
343 $min_el = $el if (!defined($min_el) || $el < $min_el);
017e2add 344 my $uch = encode_U($val);
e03ac092 345 if (exists $seen{$uch})
346 {
347 warn sprintf("U%04X is %02X%02X and %02X%02X\n",
348 $val,$page,$ch,@{$seen{$uch}});
349 }
350 else
351 {
352 $seen{$uch} = [$page,$ch];
353 }
9b37254d 354 enter($e2u,$ech,$uch,$e2u,0);
355 enter($u2e,$uch,$ech,$u2e,0);
017e2add 356 }
357 else
358 {
359 # No character at this position
360 # enter($e2u,$ech,undef,$e2u);
361 }
362 $ch++;
363 }
364 }
365 }
dcda1f94 366 $encoding{$name} = [$e2u,$u2e,$rep,$min_el,$max_el];
017e2add 367}
368
369sub enter
370{
9b37254d 371 my ($a,$s,$d,$t,$fb) = @_;
017e2add 372 $t = $a if @_ < 4;
373 my $b = substr($s,0,1);
374 my $e = $a->{$b};
375 unless ($e)
376 { # 0 1 2 3 4 5
9b37254d 377 $e = [$b,$b,'',{},length($s),0,$fb];
017e2add 378 $a->{$b} = $e;
379 }
380 if (length($s) > 1)
381 {
9b37254d 382 enter($e->[3],substr($s,1),$d,$t,$fb);
017e2add 383 }
384 else
385 {
386 $e->[2] = $d;
387 $e->[3] = $t;
388 $e->[5] = length($d);
389 }
390}
391
017e2add 392sub outstring
393{
394 my ($fh,$name,$s) = @_;
395 my $sym = $strings{$s};
396 unless ($sym)
397 {
2f2b4ff2 398 foreach my $o (keys %strings)
017e2add 399 {
2f2b4ff2 400 my $i = index($o,$s);
401 if ($i >= 0)
017e2add 402 {
2f2b4ff2 403 $sym = $strings{$o};
404 $sym .= sprintf("+0x%02x",$i) if ($i);
405 return $sym;
017e2add 406 }
407 }
408 $strings{$s} = $sym = $name;
14a8264b 409 printf $fh "\nstatic const U8 %s[%d] =\n",$name,length($s);
2f2b4ff2 410 # Do in chunks of 16 chars to constrain line length
411 # Assumes ANSI C adjacent string litteral concatenation
017e2add 412 while (length($s))
413 {
414 my $c = substr($s,0,16,'');
415 print $fh '"',join('',map(sprintf('\x%02x',ord($_)),split(//,$c))),'"';
416 print $fh "\n" if length($s);
417 }
14a8264b 418 printf $fh ";\n";
017e2add 419 }
420 return $sym;
421}
422
14a8264b 423sub process
017e2add 424{
14a8264b 425 my ($name,$a) = @_;
017e2add 426 $name =~ s/\W+/_/g;
427 $a->{Cname} = $name;
428 my @keys = grep(ref($a->{$_}),sort keys %$a);
017e2add 429 my $l;
430 my @ent;
431 foreach my $b (@keys)
432 {
433 my ($s,$f,$out,$t,$end) = @{$a->{$b}};
434 if (defined($l) &&
435 ord($b) == ord($a->{$l}[1])+1 &&
436 $a->{$l}[3] == $a->{$b}[3] &&
437 $a->{$l}[4] == $a->{$b}[4] &&
9b37254d 438 $a->{$l}[5] == $a->{$b}[5] &&
439 $a->{$l}[6] == $a->{$b}[6]
2f2b4ff2 440 # && length($a->{$l}[2]) < 16
441 )
017e2add 442 {
443 my $i = ord($b)-ord($a->{$l}[0]);
444 $a->{$l}[1] = $b;
445 $a->{$l}[2] .= $a->{$b}[2];
446 }
447 else
448 {
449 $l = $b;
450 push(@ent,$b);
451 }
14a8264b 452 if (exists $t->{Cname})
017e2add 453 {
14a8264b 454 $t->{'Forward'} = 1 if $t != $a;
455 }
456 else
457 {
458 process(sprintf("%s_%02x",$name,ord($s)),$t);
017e2add 459 }
460 }
461 if (ord($keys[-1]) < 255)
462 {
463 my $t = chr(ord($keys[-1])+1);
464 $a->{$t} = [$t,chr(255),undef,$a,0,0];
465 push(@ent,$t);
466 }
14a8264b 467 $a->{'Entries'} = \@ent;
468}
469
470sub outtable
471{
472 my ($fh,$a) = @_;
473 my $name = $a->{'Cname'};
017e2add 474 # String tables
14a8264b 475 foreach my $b (@{$a->{'Entries'}})
017e2add 476 {
477 next unless $a->{$b}[5];
478 my $s = ord($a->{$b}[0]);
479 my $e = ord($a->{$b}[1]);
480 outstring($fh,sprintf("%s__%02x_%02x",$name,$s,$e),$a->{$b}[2]);
481 }
14a8264b 482 if ($a->{'Forward'})
483 {
484 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"];\n";
485 }
486 $a->{'Done'} = 1;
487 foreach my $b (@{$a->{'Entries'}})
488 {
489 my ($s,$e,$out,$t,$end,$l) = @{$a->{$b}};
490 outtable($fh,$t) unless $t->{'Done'};
491 }
492 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"] = {\n";
493 foreach my $b (@{$a->{'Entries'}})
017e2add 494 {
9b37254d 495 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
017e2add 496 my $sc = ord($s);
497 my $ec = ord($e);
e0c49a6b 498 $end |= 0x80 if $fb;
017e2add 499 print $fh "{";
500 if ($l)
501 {
502 printf $fh outstring($fh,'',$out);
503 }
504 else
505 {
506 print $fh "0";
507 }
508 print $fh ",",$t->{Cname};
2f2b4ff2 509 printf $fh ",0x%02x,0x%02x,$l,$end},\n",$sc,$ec;
017e2add 510 }
14a8264b 511 print $fh "};\n";
512}
513
514sub output
515{
516 my ($fh,$name,$a) = @_;
517 process($name,$a);
518 # Sub-tables
519 outtable($fh,$a);
017e2add 520}
521
e0c49a6b 522sub output_enc
523{
524 my ($fh,$name,$a) = @_;
525 foreach my $b (sort keys %$a)
526 {
527 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
528 }
529}
530
531sub decode_U
532{
533 my $s = shift;
e0c49a6b 534}
535
f39fb8cc 536my @uname;
537sub char_names
538{
9fb51cbc 539 my $s = do "unicore/Name.pl";
540 die "char_names: unicore/Name.pl: $!\n" unless defined $s;
f39fb8cc 541 pos($s) = 0;
542 while ($s =~ /\G([0-9a-f]+)\t([0-9a-f]*)\t(.*?)\s*\n/igc)
543 {
544 my $name = $3;
545 my $s = hex($1);
546 last if $s >= 0x10000;
547 my $e = length($2) ? hex($2) : $s;
548 for (my $i = $s; $i <= $e; $i++)
549 {
550 $uname[$i] = $name;
551# print sprintf("U%04X $name\n",$i);
552 }
553 }
554}
555
e0c49a6b 556sub output_ucm_page
557{
f39fb8cc 558 my ($cmap,$a,$t,$pre) = @_;
e0c49a6b 559 # warn sprintf("Page %x\n",$pre);
560 foreach my $b (sort keys %$t)
561 {
562 my ($s,$e,$out,$n,$end,$l,$fb) = @{$t->{$b}};
563 die "oops $s $e" unless $s eq $e;
564 my $u = ord($s);
565 if ($n != $a && $n != $t)
566 {
f39fb8cc 567 output_ucm_page($cmap,$a,$n,(($pre|($u &0x3F)) << 6)&0xFFFF);
e0c49a6b 568 }
569 elsif (length($out))
570 {
571 if ($pre)
572 {
573 $u = $pre|($u &0x3f);
574 }
f39fb8cc 575 my $s = sprintf "<U%04X> ",$u;
e0c49a6b 576 foreach my $c (split(//,$out))
577 {
f39fb8cc 578 $s .= sprintf "\\x%02X",ord($c);
e0c49a6b 579 }
f39fb8cc 580 $s .= sprintf " |%d # %s\n",($fb ? 1 : 0),$uname[$u];
581 push(@$cmap,$s);
e0c49a6b 582 }
583 else
584 {
585 warn join(',',@{$t->{$b}},$a,$t);
586 }
587 }
588}
589
590sub output_ucm
591{
f39fb8cc 592 my ($fh,$name,$h,$rep,$min_el,$max_el) = @_;
df1df145 593 print $fh "# $0 @orig_ARGV\n" unless $opt{'q'};
afdae191 594 print $fh "<code_set_name> \"$name\"\n";
f39fb8cc 595 char_names();
afdae191 596 if (defined $min_el)
597 {
598 print $fh "<mb_cur_min> $min_el\n";
599 }
600 if (defined $max_el)
601 {
602 print $fh "<mb_cur_max> $max_el\n";
603 }
604 if (defined $rep)
605 {
606 print $fh "<subchar> ";
607 foreach my $c (split(//,$rep))
608 {
609 printf $fh "\\x%02X",ord($c);
610 }
611 print $fh "\n";
612 }
f39fb8cc 613 my @cmap;
614 output_ucm_page(\@cmap,$h,$h,0);
afdae191 615 print $fh "#\nCHARMAP\n";
f39fb8cc 616 foreach my $line (sort { substr($a,8) cmp substr($b,8) } @cmap)
617 {
618 print $fh $line;
619 }
e0c49a6b 620 print $fh "END CHARMAP\n";
621}
017e2add 622