3 unshift @INC, qw(../../lib ../../../lib);
4 $ENV{PATH} .= ';../..;../../..' if $^O eq 'MSWin32';
13 # UTF-8 encode long hand - only covers part of perl's range
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.
32 # encode double byte MS byte first
34 return chr($page).chr($ch);
39 # encode Multi-byte - single for 0..255 otherwise double
41 return &encode_D if $page;
45 # Win32 does not expand globs on command line
46 eval "\@ARGV = map(glob(\$_),\@ARGV)" if ($^O eq 'MSWin32');
49 getopts('qo:f:n:',\%opt);
50 my $cname = (exists $opt{'o'}) ? $opt{'o'} : shift(@ARGV);
51 chmod(0666,$cname) if -f $cname && !-w $cname;
52 open(C,">$cname") || die "Cannot open $cname:$!";
56 $dname =~ s/(\.[^\.]*)?$/_def.h/;
58 my ($doC,$doEnc,$doUcm,$doPet);
60 if ($cname =~ /\.(c|xs)$/)
63 chmod(0666,$dname) if -f $cname && !-w $dname;
64 open(D,">$dname") || die "Cannot open $dname:$!";
66 $hname =~ s/(\.[^\.]*)?$/.h/;
67 chmod(0666,$hname) if -f $cname && !-w $hname;
68 open(H,">$hname") || die "Cannot open $hname:$!";
70 foreach my $fh (\*C,\*D,\*H)
72 print $fh <<"END" unless $opt{'q'};
74 !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
75 This file was autogenerated by:
81 if ($cname =~ /(\w+)\.xs$/)
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";
88 print C "#include \"encode.h\"\n";
91 elsif ($cname =~ /\.enc$/)
95 elsif ($cname =~ /\.ucm$/)
99 elsif ($cname =~ /\.pet$/)
105 if (exists $opt{'f'})
107 # -F is followed by name of file containing list of filenames
108 my $flist = $opt{'f'};
109 open(FLIST,$flist) || die "Cannot open $flist:$!";
110 chomp(@encfiles = <FLIST>);
126 if ($a =~ /^.*-(\d+)/)
129 if ($b =~ /^.*-(\d+)/)
139 foreach my $enc (sort cmp_name @encfiles)
141 my ($name,$sfx) = $enc =~ /^.*?([\w-]+)\.(enc|ucm)$/;
142 $name = $opt{'n'} if exists $opt{'n'};
147 compile_enc(\*E,lc($name));
151 compile_ucm(\*E,lc($name));
156 warn "Cannot open $enc for $name:$!";
162 print STDERR "Writing compiled form\n";
163 foreach my $name (sort cmp_name keys %encoding)
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));
170 foreach my $enc (sort cmp_name keys %encoding)
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);
174 my $sym = "${enc}_encoding";
176 print C "encode_t $sym = \n";
177 print C " {",join(',',@info,"{\"$enc\",(const char *)0}"),"};\n\n";
180 foreach my $enc (sort cmp_name keys %encoding)
182 my $sym = "${enc}_encoding";
184 print H "extern encode_t $sym;\n";
185 print D " Encode_XSEncoding(aTHX_ &$sym);\n";
188 if ($cname =~ /(\w+)\.xs$/)
194 Encode_XSEncoding(pTHX_ encode_t *enc)
197 HV *stash = gv_stashpv("Encode::XS", TRUE);
198 SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(enc))),stash);
204 const char *name = enc->name[i++];
205 XPUSHs(sv_2mortal(newSVpvn(name,strlen(name))));
208 call_pv("Encode::define_encoding",G_DISCARD);
214 print C "\nMODULE = Encode::$mod\tPACKAGE = Encode::$mod\n\n";
215 print C "BOOT:\n{\n";
216 print C "#include \"$dname\"\n";
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;
227 foreach my $name (sort cmp_name keys %encoding)
229 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
230 output_enc(\*C,$name,$e2u);
235 foreach my $name (sort cmp_name keys %encoding)
237 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
238 output_ucm(\*C,$name,$u2e,$erep,$min_el,$max_el);
256 last if /^\s*CHARMAP\s*$/i;
257 if (/^\s*<(\w+)>\s+"?([^"]*)"?\s*$/i)
262 if (!defined($cs = $attr{'code_set_name'}))
264 warn "No <code_set_name> in $name\n";
268 $name = $cs unless exists $opt{'n'};
274 if (exists $attr{'subchar'})
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));
281 print "Reading $name ($cs)\n";
287 last if /^\s*END\s+CHARMAP\s*$/i;
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;
298 my $uch = encode_U(hex($u));
299 my $ech = join('',map(chr(hex($_)),@byte));
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);
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]/);
328 die "$nfb entries without fallback, $hfb entries with\n";
330 $encoding{$name} = [$e2u,$u2e,$erep,$min_el,$max_el];
340 while ($type = <$fh>)
342 last if $type !~ /^\s*#/;
345 return if $type eq 'E';
346 my ($def,$sym,$pages) = split(/\s+/,scalar(<$fh>));
347 warn "$type encoded $name\n";
354 $rep = &{"encode_$type"}($v & 0xFF, ($v >> 8) & 0xffe);
361 my $page = hex($line);
363 for (my $i = 0; $i < 16; $i++)
366 for (my $j = 0; $j < 16; $j++)
369 my $ech = &{"encode_$type"}($ch,$page);
370 my $val = hex(substr($line,0,4,''));
371 next if $val == 0xFFFD;
372 if ($val || (!$ch && !$page))
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);
377 my $uch = encode_U($val);
378 if (exists $seen{$uch})
380 warn sprintf("U%04X is %02X%02X and %02X%02X\n",
381 $val,$page,$ch,@{$seen{$uch}});
385 $seen{$uch} = [$page,$ch];
387 enter($e2u,$ech,$uch,$e2u,0);
388 enter($u2e,$uch,$ech,$u2e,0);
392 # No character at this position
393 # enter($e2u,$ech,undef,$e2u);
399 $encoding{$name} = [$e2u,$u2e,$rep,$min_el,$max_el];
404 my ($a,$s,$d,$t,$fb) = @_;
406 my $b = substr($s,0,1);
410 $e = [$b,$b,'',{},length($s),0,$fb];
415 enter($e->[3],substr($s,1),$d,$t,$fb);
421 $e->[5] = length($d);
429 my ($fh,$name,$s) = @_;
430 my $sym = $strings{$s};
433 $saved += length($s);
437 foreach my $o (keys %strings)
439 my $i = index($o,$s);
443 $sym .= sprintf("+0x%02x",$i) if ($i);
444 $subsave += length($s);
448 $strings{$s} = $sym = $name;
449 $strings += length($s);
450 printf $fh "\nstatic const U8 %s[%d] =\n",$name,length($s);
451 # Do in chunks of 16 chars to constrain line length
452 # Assumes ANSI C adjacent string litteral concatenation
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);
469 my @keys = grep(ref($a->{$_}),sort keys %$a);
472 foreach my $b (@keys)
474 my ($s,$f,$out,$t,$end) = @{$a->{$b}};
476 ord($b) == ord($a->{$l}[1])+1 &&
477 $a->{$l}[3] == $a->{$b}[3] &&
478 $a->{$l}[4] == $a->{$b}[4] &&
479 $a->{$l}[5] == $a->{$b}[5] &&
480 $a->{$l}[6] == $a->{$b}[6]
481 # && length($a->{$l}[2]) < 16
484 my $i = ord($b)-ord($a->{$l}[0]);
486 $a->{$l}[2] .= $a->{$b}[2];
493 if (exists $t->{Cname})
495 $t->{'Forward'} = 1 if $t != $a;
499 process(sprintf("%s_%02x",$name,ord($s)),$t);
502 if (ord($keys[-1]) < 255)
504 my $t = chr(ord($keys[-1])+1);
505 $a->{$t} = [$t,chr(255),undef,$a,0,0];
508 $a->{'Entries'} = \@ent;
514 my $name = $a->{'Cname'};
516 foreach my $b (@{$a->{'Entries'}})
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]);
525 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"];\n";
528 foreach my $b (@{$a->{'Entries'}})
530 my ($s,$e,$out,$t,$end,$l) = @{$a->{$b}};
531 outtable($fh,$t) unless $t->{'Done'};
533 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"] = {\n";
534 foreach my $b (@{$a->{'Entries'}})
536 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
543 printf $fh outstring($fh,'',$out);
549 print $fh ",",$t->{Cname};
550 printf $fh ",0x%02x,0x%02x,$l,$end},\n",$sc,$ec;
557 my ($fh,$name,$a) = @_;
565 my ($fh,$name,$a) = @_;
566 foreach my $b (sort keys %$a)
568 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
580 my $s = do "unicore/Name.pl";
581 die "char_names: unicore/Name.pl: $!\n" unless defined $s;
583 while ($s =~ /\G([0-9a-f]+)\t([0-9a-f]*)\t(.*?)\s*\n/igc)
587 last if $s >= 0x10000;
588 my $e = length($2) ? hex($2) : $s;
589 for (my $i = $s; $i <= $e; $i++)
592 # print sprintf("U%04X $name\n",$i);
599 my ($cmap,$a,$t,$pre) = @_;
600 # warn sprintf("Page %x\n",$pre);
601 foreach my $b (sort keys %$t)
603 my ($s,$e,$out,$n,$end,$l,$fb) = @{$t->{$b}};
604 die "oops $s $e" unless $s eq $e;
606 if ($n != $a && $n != $t)
608 output_ucm_page($cmap,$a,$n,(($pre|($u &0x3F)) << 6)&0xFFFF);
614 $u = $pre|($u &0x3f);
616 my $s = sprintf "<U%04X> ",$u;
617 foreach my $c (split(//,$out))
619 $s .= sprintf "\\x%02X",ord($c);
621 $s .= sprintf " |%d # %s\n",($fb ? 1 : 0),$uname[$u];
626 warn join(',',@{$t->{$b}},$a,$t);
633 my ($fh,$name,$h,$rep,$min_el,$max_el) = @_;
634 print $fh "# $0 @orig_ARGV\n" unless $opt{'q'};
635 print $fh "<code_set_name> \"$name\"\n";
639 print $fh "<mb_cur_min> $min_el\n";
643 print $fh "<mb_cur_max> $max_el\n";
647 print $fh "<subchar> ";
648 foreach my $c (split(//,$rep))
650 printf $fh "\\x%02X",ord($c);
655 output_ucm_page(\@cmap,$h,$h,0);
656 print $fh "#\nCHARMAP\n";
657 foreach my $line (sort { substr($a,8) cmp substr($b,8) } @cmap)
661 print $fh "END CHARMAP\n";