3 unshift @INC, qw(../../lib ../../../lib);
4 $ENV{PATH} .= ';../..;../../..' if $^O eq 'MSWin32';
12 # UTF-8 encode long hand - only covers part of perl's range
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($_[0]));
17 # Now get core perl to encode that the way it likes.
25 ## my ($ch,$page) = @_; return chr($ch);
31 # encode double byte MS byte first
32 ## my ($ch,$page) = @_; return chr($page).chr($ch);
33 return chr ($_[1]) . chr $_[0];
38 # encode Multi-byte - single for 0..255 otherwise double
39 ## my ($ch,$page) = @_;
40 ## return &encode_D if $page;
42 return chr ($_[1]) . chr $_[0] if $_[1];
46 my %encode_types = (U => \&encode_U,
52 # Win32 does not expand globs on command line
53 eval "\@ARGV = map(glob(\$_),\@ARGV)" if ($^O eq 'MSWin32');
56 getopts('qOo:f:n:',\%opt);
57 my $cname = (exists $opt{'o'}) ? $opt{'o'} : shift(@ARGV);
58 chmod(0666,$cname) if -f $cname && !-w $cname;
59 open(C,">$cname") || die "Cannot open $cname:$!";
63 $dname =~ s/(\.[^\.]*)?$/_def.h/;
65 my ($doC,$doEnc,$doUcm,$doPet);
67 if ($cname =~ /\.(c|xs)$/)
70 chmod(0666,$dname) if -f $cname && !-w $dname;
71 open(D,">$dname") || die "Cannot open $dname:$!";
73 $hname =~ s/(\.[^\.]*)?$/.h/;
74 chmod(0666,$hname) if -f $cname && !-w $hname;
75 open(H,">$hname") || die "Cannot open $hname:$!";
77 foreach my $fh (\*C,\*D,\*H)
79 print $fh <<"END" unless $opt{'q'};
81 !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
82 This file was autogenerated by:
88 if ($cname =~ /(\w+)\.xs$/)
90 print C "#include <EXTERN.h>\n";
91 print C "#include <perl.h>\n";
92 print C "#include <XSUB.h>\n";
93 print C "#define U8 U8\n";
95 print C "#include \"encode.h\"\n";
98 elsif ($cname =~ /\.enc$/)
102 elsif ($cname =~ /\.ucm$/)
106 elsif ($cname =~ /\.pet$/)
112 if (exists $opt{'f'})
114 # -F is followed by name of file containing list of filenames
115 my $flist = $opt{'f'};
116 open(FLIST,$flist) || die "Cannot open $flist:$!";
117 chomp(@encfiles = <FLIST>);
133 if ($a =~ /^.*-(\d+)/)
136 if ($b =~ /^.*-(\d+)/)
146 foreach my $enc (sort cmp_name @encfiles)
148 my ($name,$sfx) = $enc =~ /^.*?([\w-]+)\.(enc|ucm)$/;
149 $name = $opt{'n'} if exists $opt{'n'};
154 compile_enc(\*E,lc($name));
158 compile_ucm(\*E,lc($name));
163 warn "Cannot open $enc for $name:$!";
169 print STDERR "Writing compiled form\n";
170 foreach my $name (sort cmp_name keys %encoding)
172 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
173 output(\*C,$name.'_utf8',$e2u);
174 output(\*C,'utf8_'.$name,$u2e);
175 push(@{$encoding{$name}},outstring(\*C,$e2u->{Cname}.'_def',$erep));
177 foreach my $enc (sort cmp_name keys %encoding)
179 my ($e2u,$u2e,$rep,$min_el,$max_el,$rsym) = @{$encoding{$enc}};
180 my @info = ($e2u->{Cname},$u2e->{Cname},$rsym,length($rep),$min_el,$max_el);
181 my $sym = "${enc}_encoding";
183 print C "encode_t $sym = \n";
184 print C " {",join(',',@info,"{\"$enc\",(const char *)0}"),"};\n\n";
187 foreach my $enc (sort cmp_name keys %encoding)
189 my $sym = "${enc}_encoding";
191 print H "extern encode_t $sym;\n";
192 print D " Encode_XSEncoding(aTHX_ &$sym);\n";
195 if ($cname =~ /(\w+)\.xs$/)
201 Encode_XSEncoding(pTHX_ encode_t *enc)
204 HV *stash = gv_stashpv("Encode::XS", TRUE);
205 SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(enc))),stash);
211 const char *name = enc->name[i++];
212 XPUSHs(sv_2mortal(newSVpvn(name,strlen(name))));
215 call_pv("Encode::define_encoding",G_DISCARD);
221 print C "\nMODULE = Encode::$mod\tPACKAGE = Encode::$mod\n\n";
222 print C "BOOT:\n{\n";
223 print C "#include \"$dname\"\n";
228 printf STDERR "%d bytes in string tables\n",$strings;
229 printf STDERR "%d bytes (%.3g%%) saved spotting duplicates\n",$saved,100*$saved/$strings if $saved;
230 printf STDERR "%d bytes (%.3g%%) saved using substrings\n",$subsave,100*$subsave/$strings if $subsave;
234 foreach my $name (sort cmp_name keys %encoding)
236 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
237 output_enc(\*C,$name,$e2u);
242 foreach my $name (sort cmp_name keys %encoding)
244 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
245 output_ucm(\*C,$name,$u2e,$erep,$min_el,$max_el);
263 last if /^\s*CHARMAP\s*$/i;
264 if (/^\s*<(\w+)>\s+"?([^"]*)"?\s*$/i) # " # Grrr
269 if (!defined($cs = $attr{'code_set_name'}))
271 warn "No <code_set_name> in $name\n";
275 $name = $cs unless exists $opt{'n'};
281 if (exists $attr{'subchar'})
284 $attr{'subchar'} =~ /^\s*/cg;
285 push(@byte,$1) while $attr{'subchar'} =~ /\G\\x([0-9a-f]+)/icg;
286 $erep = join('',map(chr(hex($_)),@byte));
288 print "Reading $name ($cs)\n";
294 last if /^\s*END\s+CHARMAP\s*$/i;
298 $u = $1 if (/^<U([0-9a-f]+)>\s+/igc);
299 push(@byte,$1) while /\G\\x([0-9a-f]+)/igc;
300 $fb = $1 if /\G\s*(\|[0-3])/gc;
301 # warn "$_: $u @byte | $fb\n";
302 die "Bad line:$_" unless /\G\s*(#.*)?$/gc;
305 my $uch = encode_U(hex($u));
306 my $ech = join('',map(chr(hex($_)),@byte));
307 my $el = length($ech);
308 $max_el = $el if (!defined($max_el) || $el > $max_el);
309 $min_el = $el if (!defined($min_el) || $el < $min_el);
320 # $fb is fallback flag
321 # 0 - round trip safe
322 # 1 - fallback for unicode -> enc
323 # 2 - skip sub-char mapping
324 # 3 - fallback enc -> unicode
325 enter($u2e,$uch,$ech,$u2e,$fb+0) if ($fb =~ /[01]/);
326 enter($e2u,$ech,$uch,$e2u,$fb+0) if ($fb =~ /[03]/);
335 die "$nfb entries without fallback, $hfb entries with\n";
337 $encoding{$name} = [$e2u,$u2e,$erep,$min_el,$max_el];
347 while ($type = <$fh>)
349 last if $type !~ /^\s*#/;
352 return if $type eq 'E';
353 my ($def,$sym,$pages) = split(/\s+/,scalar(<$fh>));
354 warn "$type encoded $name\n";
356 # Save a defined test by setting these to defined values.
357 my $min_el = ~0; # A very big integer
358 my $max_el = 0; # Anything must be longer than 0
361 $rep = &{$encode_types{$type}}($v & 0xFF, ($v >> 8) & 0xffe);
368 my $page = hex($line);
373 die "Line should be exactly 65 characters long including newline"
374 unless length ($line) == 65;
375 # Split line into groups of 4 hex digits, convert groups to ints
376 for my $val (map {hex $_} $line =~ /(....)/g)
378 next if $val == 0xFFFD;
379 my $ech = &{$encode_types{$type}}($ch,$page);
380 if ($val || (!$ch && !$page))
382 my $el = length($ech);
383 $max_el = $el if $el > $max_el;
384 $min_el = $el if $el < $min_el;
385 my $uch = encode_U($val);
386 if (exists $seen{$uch})
388 warn sprintf("U%04X is %02X%02X and %02X%02X\n",
389 $val,$page,$ch,@{$seen{$uch}});
393 $seen{$uch} = [$page,$ch];
395 enter($e2u,$ech,$uch,$e2u,0);
396 enter($u2e,$uch,$ech,$u2e,0);
400 # No character at this position
401 # enter($e2u,$ech,undef,$e2u);
407 die "\$min_el=$min_el, \$max_el=$max_el - seems we read no lines"
408 if $min_el > $max_el;
409 $encoding{$name} = [$e2u,$u2e,$rep,$min_el,$max_el];
414 my ($a,$s,$d,$t,$fb) = @_;
422 $a->{$b} = $e = [$b,$b,'',{},1+length($s),0,$fb] unless $e;
423 unless (length($s)) {
426 $e->[5] = length($d);
438 my ($fh,$name,$s) = @_;
439 my $sym = $strings{$s};
442 $saved += length($s);
447 foreach my $o (keys %strings)
449 my $i = index($o,$s);
453 $sym .= sprintf("+0x%02x",$i) if ($i);
454 $subsave += length($s);
460 $strings{$s} = $sym = $name;
461 $strings += length($s);
462 my $definition = sprintf "static const U8 %s[%d] = { ",$name,length($s);
463 # Maybe we should assert that these are all <256.
464 $definition .= join(',',unpack "C*",$s);
465 # We have a single long line. Split it at convenient commas.
466 $definition =~ s/(.{74,77},)/$1\n/g;
467 print $fh "$definition };\n\n";
477 my @keys = sort grep(ref($a->{$_}),keys %$a);
480 foreach my $b (@keys)
482 my ($s,undef,undef,$t,undef) = @{$a->{$b}};
484 ord($b) == ord($a->{$l}[1])+1 &&
485 $a->{$l}[3] == $a->{$b}[3] &&
486 $a->{$l}[4] == $a->{$b}[4] &&
487 $a->{$l}[5] == $a->{$b}[5] &&
488 $a->{$l}[6] == $a->{$b}[6]
489 # && length($a->{$l}[2]) < 16
492 my $i = ord($b)-ord($a->{$l}[0]);
494 $a->{$l}[2] .= $a->{$b}[2];
501 if (exists $t->{Cname})
503 $t->{'Forward'} = 1 if $t != $a;
507 process(sprintf("%s_%02x",$name,ord($s)),$t);
510 if (ord($keys[-1]) < 255)
512 my $t = chr(ord($keys[-1])+1);
513 $a->{$t} = [$t,chr(255),undef,$a,0,0];
516 $a->{'Entries'} = \@ent;
522 my $name = $a->{'Cname'};
524 foreach my $b (@{$a->{'Entries'}})
526 next unless $a->{$b}[5];
527 my $s = ord($a->{$b}[0]);
528 my $e = ord($a->{$b}[1]);
529 outstring($fh,sprintf("%s__%02x_%02x",$name,$s,$e),$a->{$b}[2]);
533 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"];\n";
536 foreach my $b (@{$a->{'Entries'}})
538 my ($s,$e,$out,$t,$end,$l) = @{$a->{$b}};
539 outtable($fh,$t) unless $t->{'Done'};
541 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"] = {\n";
542 foreach my $b (@{$a->{'Entries'}})
544 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
551 printf $fh outstring($fh,'',$out);
557 print $fh ",",$t->{Cname};
558 printf $fh ",0x%02x,0x%02x,$l,$end},\n",$sc,$ec;
565 my ($fh,$name,$a) = @_;
573 my ($fh,$name,$a) = @_;
574 foreach my $b (sort keys %$a)
576 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
588 my $s = do "unicore/Name.pl";
589 die "char_names: unicore/Name.pl: $!\n" unless defined $s;
591 while ($s =~ /\G([0-9a-f]+)\t([0-9a-f]*)\t(.*?)\s*\n/igc)
595 last if $s >= 0x10000;
596 my $e = length($2) ? hex($2) : $s;
597 for (my $i = $s; $i <= $e; $i++)
600 # print sprintf("U%04X $name\n",$i);
607 my ($cmap,$a,$t,$pre) = @_;
608 # warn sprintf("Page %x\n",$pre);
609 foreach my $b (sort keys %$t)
611 my ($s,$e,$out,$n,$end,$l,$fb) = @{$t->{$b}};
612 die "oops $s $e" unless $s eq $e;
614 if ($n != $a && $n != $t)
616 output_ucm_page($cmap,$a,$n,(($pre|($u &0x3F)) << 6)&0xFFFF);
622 $u = $pre|($u &0x3f);
624 my $s = sprintf "<U%04X> ",$u;
625 foreach my $c (split(//,$out))
627 $s .= sprintf "\\x%02X",ord($c);
629 $s .= sprintf " |%d # %s\n",($fb ? 1 : 0),$uname[$u];
634 warn join(',',@{$t->{$b}},$a,$t);
641 my ($fh,$name,$h,$rep,$min_el,$max_el) = @_;
642 print $fh "# $0 @orig_ARGV\n" unless $opt{'q'};
643 print $fh "<code_set_name> \"$name\"\n";
647 print $fh "<mb_cur_min> $min_el\n";
651 print $fh "<mb_cur_max> $max_el\n";
655 print $fh "<subchar> ";
656 foreach my $c (split(//,$rep))
658 printf $fh "\\x%02X",ord($c);
663 output_ucm_page(\@cmap,$h,$h,0);
664 print $fh "#\nCHARMAP\n";
665 foreach my $line (sort { substr($a,8) cmp substr($b,8) } @cmap)
669 print $fh "END CHARMAP\n";