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