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($uv));
17 # Now get core perl to encode that the way it likes.
31 # encode double byte MS byte first
33 return chr($page).chr($ch);
38 # encode Multi-byte - single for 0..255 otherwise double
40 return &encode_D if $page;
44 # Win32 does not expand globs on command line
45 eval "\@ARGV = map(glob(\$_),\@ARGV)" if ($^O eq 'MSWin32');
48 getopts('qOo:f:n:',\%opt);
49 my $cname = (exists $opt{'o'}) ? $opt{'o'} : shift(@ARGV);
50 chmod(0666,$cname) if -f $cname && !-w $cname;
51 open(C,">$cname") || die "Cannot open $cname:$!";
55 $dname =~ s/(\.[^\.]*)?$/_def.h/;
57 my ($doC,$doEnc,$doUcm,$doPet);
59 if ($cname =~ /\.(c|xs)$/)
62 chmod(0666,$dname) if -f $cname && !-w $dname;
63 open(D,">$dname") || die "Cannot open $dname:$!";
65 $hname =~ s/(\.[^\.]*)?$/.h/;
66 chmod(0666,$hname) if -f $cname && !-w $hname;
67 open(H,">$hname") || die "Cannot open $hname:$!";
69 foreach my $fh (\*C,\*D,\*H)
71 print $fh <<"END" unless $opt{'q'};
73 !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
74 This file was autogenerated by:
80 if ($cname =~ /(\w+)\.xs$/)
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";
87 print C "#include \"encode.h\"\n";
90 elsif ($cname =~ /\.enc$/)
94 elsif ($cname =~ /\.ucm$/)
98 elsif ($cname =~ /\.pet$/)
104 if (exists $opt{'f'})
106 # -F is followed by name of file containing list of filenames
107 my $flist = $opt{'f'};
108 open(FLIST,$flist) || die "Cannot open $flist:$!";
109 chomp(@encfiles = <FLIST>);
125 if ($a =~ /^.*-(\d+)/)
128 if ($b =~ /^.*-(\d+)/)
138 foreach my $enc (sort cmp_name @encfiles)
140 my ($name,$sfx) = $enc =~ /^.*?([\w-]+)\.(enc|ucm)$/;
141 $name = $opt{'n'} if exists $opt{'n'};
146 compile_enc(\*E,lc($name));
150 compile_ucm(\*E,lc($name));
155 warn "Cannot open $enc for $name:$!";
161 print STDERR "Writing compiled form\n";
162 foreach my $name (sort cmp_name keys %encoding)
164 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
165 output(\*C,$name.'_utf8',$e2u);
166 output(\*C,'utf8_'.$name,$u2e);
167 push(@{$encoding{$name}},outstring(\*C,$e2u->{Cname}.'_def',$erep));
169 foreach my $enc (sort cmp_name keys %encoding)
171 my ($e2u,$u2e,$rep,$min_el,$max_el,$rsym) = @{$encoding{$enc}};
172 my @info = ($e2u->{Cname},$u2e->{Cname},$rsym,length($rep),$min_el,$max_el);
173 my $sym = "${enc}_encoding";
175 print C "encode_t $sym = \n";
176 print C " {",join(',',@info,"{\"$enc\",(const char *)0}"),"};\n\n";
179 foreach my $enc (sort cmp_name keys %encoding)
181 my $sym = "${enc}_encoding";
183 print H "extern encode_t $sym;\n";
184 print D " Encode_XSEncoding(aTHX_ &$sym);\n";
187 if ($cname =~ /(\w+)\.xs$/)
193 Encode_XSEncoding(pTHX_ encode_t *enc)
196 HV *stash = gv_stashpv("Encode::XS", TRUE);
197 SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(enc))),stash);
203 const char *name = enc->name[i++];
204 XPUSHs(sv_2mortal(newSVpvn(name,strlen(name))));
207 call_pv("Encode::define_encoding",G_DISCARD);
213 print C "\nMODULE = Encode::$mod\tPACKAGE = Encode::$mod\n\n";
214 print C "BOOT:\n{\n";
215 print C "#include \"$dname\"\n";
220 printf STDERR "%d bytes in string tables\n",$strings;
221 printf STDERR "%d bytes (%.3g%%) saved spotting duplicates\n",$saved,100*$saved/$strings if $saved;
222 printf STDERR "%d bytes (%.3g%%) saved using substrings\n",$subsave,100*$subsave/$strings if $subsave;
226 foreach my $name (sort cmp_name keys %encoding)
228 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
229 output_enc(\*C,$name,$e2u);
234 foreach my $name (sort cmp_name keys %encoding)
236 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
237 output_ucm(\*C,$name,$u2e,$erep,$min_el,$max_el);
255 last if /^\s*CHARMAP\s*$/i;
256 if (/^\s*<(\w+)>\s+"?([^"]*)"?\s*$/i)
261 if (!defined($cs = $attr{'code_set_name'}))
263 warn "No <code_set_name> in $name\n";
267 $name = $cs unless exists $opt{'n'};
273 if (exists $attr{'subchar'})
276 $attr{'subchar'} =~ /^\s*/cg;
277 push(@byte,$1) while $attr{'subchar'} =~ /\G\\x([0-9a-f]+)/icg;
278 $erep = join('',map(chr(hex($_)),@byte));
280 print "Reading $name ($cs)\n";
286 last if /^\s*END\s+CHARMAP\s*$/i;
290 $u = $1 if (/^<U([0-9a-f]+)>\s+/igc);
291 push(@byte,$1) while /\G\\x([0-9a-f]+)/igc;
292 $fb = $1 if /\G\s*(\|[0-3])/gc;
293 # warn "$_: $u @byte | $fb\n";
294 die "Bad line:$_" unless /\G\s*(#.*)?$/gc;
297 my $uch = encode_U(hex($u));
298 my $ech = join('',map(chr(hex($_)),@byte));
299 my $el = length($ech);
300 $max_el = $el if (!defined($max_el) || $el > $max_el);
301 $min_el = $el if (!defined($min_el) || $el < $min_el);
312 # $fb is fallback flag
313 # 0 - round trip safe
314 # 1 - fallback for unicode -> enc
315 # 2 - skip sub-char mapping
316 # 3 - fallback enc -> unicode
317 enter($u2e,$uch,$ech,$u2e,$fb+0) if ($fb =~ /[01]/);
318 enter($e2u,$ech,$uch,$e2u,$fb+0) if ($fb =~ /[03]/);
327 die "$nfb entries without fallback, $hfb entries with\n";
329 $encoding{$name} = [$e2u,$u2e,$erep,$min_el,$max_el];
339 while ($type = <$fh>)
341 last if $type !~ /^\s*#/;
344 return if $type eq 'E';
345 my ($def,$sym,$pages) = split(/\s+/,scalar(<$fh>));
346 warn "$type encoded $name\n";
353 $rep = &{"encode_$type"}($v & 0xFF, ($v >> 8) & 0xffe);
360 my $page = hex($line);
362 for (my $i = 0; $i < 16; $i++)
365 for (my $j = 0; $j < 16; $j++)
368 my $ech = &{"encode_$type"}($ch,$page);
369 my $val = hex(substr($line,0,4,''));
370 next if $val == 0xFFFD;
371 if ($val || (!$ch && !$page))
373 my $el = length($ech);
374 $max_el = $el if (!defined($max_el) || $el > $max_el);
375 $min_el = $el if (!defined($min_el) || $el < $min_el);
376 my $uch = encode_U($val);
377 if (exists $seen{$uch})
379 warn sprintf("U%04X is %02X%02X and %02X%02X\n",
380 $val,$page,$ch,@{$seen{$uch}});
384 $seen{$uch} = [$page,$ch];
386 enter($e2u,$ech,$uch,$e2u,0);
387 enter($u2e,$uch,$ech,$u2e,0);
391 # No character at this position
392 # enter($e2u,$ech,undef,$e2u);
398 $encoding{$name} = [$e2u,$u2e,$rep,$min_el,$max_el];
403 my ($a,$s,$d,$t,$fb) = @_;
411 $a->{$b} = $e = [$b,$b,'',{},1+length($s),0,$fb] unless $e;
412 unless (length($s)) {
415 $e->[5] = length($d);
427 my ($fh,$name,$s) = @_;
428 my $sym = $strings{$s};
431 $saved += length($s);
436 foreach my $o (keys %strings)
438 my $i = index($o,$s);
442 $sym .= sprintf("+0x%02x",$i) if ($i);
443 $subsave += length($s);
449 $strings{$s} = $sym = $name;
450 $strings += length($s);
451 my $definition = sprintf "static const U8 %s[%d] = { ",$name,length($s);
452 # Maybe we should assert that these are all <256.
453 $definition .= join(',',unpack "C*",$s);
454 # We have a single long line. Split it at convenient commas.
455 $definition =~ s/(.{74,77},)/$1\n/g;
456 print $fh "$definition };\n\n";
466 my @keys = grep(ref($a->{$_}),sort keys %$a);
469 foreach my $b (@keys)
471 my ($s,$f,$out,$t,$end) = @{$a->{$b}};
473 ord($b) == ord($a->{$l}[1])+1 &&
474 $a->{$l}[3] == $a->{$b}[3] &&
475 $a->{$l}[4] == $a->{$b}[4] &&
476 $a->{$l}[5] == $a->{$b}[5] &&
477 $a->{$l}[6] == $a->{$b}[6]
478 # && length($a->{$l}[2]) < 16
481 my $i = ord($b)-ord($a->{$l}[0]);
483 $a->{$l}[2] .= $a->{$b}[2];
490 if (exists $t->{Cname})
492 $t->{'Forward'} = 1 if $t != $a;
496 process(sprintf("%s_%02x",$name,ord($s)),$t);
499 if (ord($keys[-1]) < 255)
501 my $t = chr(ord($keys[-1])+1);
502 $a->{$t} = [$t,chr(255),undef,$a,0,0];
505 $a->{'Entries'} = \@ent;
511 my $name = $a->{'Cname'};
513 foreach my $b (@{$a->{'Entries'}})
515 next unless $a->{$b}[5];
516 my $s = ord($a->{$b}[0]);
517 my $e = ord($a->{$b}[1]);
518 outstring($fh,sprintf("%s__%02x_%02x",$name,$s,$e),$a->{$b}[2]);
522 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"];\n";
525 foreach my $b (@{$a->{'Entries'}})
527 my ($s,$e,$out,$t,$end,$l) = @{$a->{$b}};
528 outtable($fh,$t) unless $t->{'Done'};
530 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"] = {\n";
531 foreach my $b (@{$a->{'Entries'}})
533 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
540 printf $fh outstring($fh,'',$out);
546 print $fh ",",$t->{Cname};
547 printf $fh ",0x%02x,0x%02x,$l,$end},\n",$sc,$ec;
554 my ($fh,$name,$a) = @_;
562 my ($fh,$name,$a) = @_;
563 foreach my $b (sort keys %$a)
565 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
577 my $s = do "unicore/Name.pl";
578 die "char_names: unicore/Name.pl: $!\n" unless defined $s;
580 while ($s =~ /\G([0-9a-f]+)\t([0-9a-f]*)\t(.*?)\s*\n/igc)
584 last if $s >= 0x10000;
585 my $e = length($2) ? hex($2) : $s;
586 for (my $i = $s; $i <= $e; $i++)
589 # print sprintf("U%04X $name\n",$i);
596 my ($cmap,$a,$t,$pre) = @_;
597 # warn sprintf("Page %x\n",$pre);
598 foreach my $b (sort keys %$t)
600 my ($s,$e,$out,$n,$end,$l,$fb) = @{$t->{$b}};
601 die "oops $s $e" unless $s eq $e;
603 if ($n != $a && $n != $t)
605 output_ucm_page($cmap,$a,$n,(($pre|($u &0x3F)) << 6)&0xFFFF);
611 $u = $pre|($u &0x3f);
613 my $s = sprintf "<U%04X> ",$u;
614 foreach my $c (split(//,$out))
616 $s .= sprintf "\\x%02X",ord($c);
618 $s .= sprintf " |%d # %s\n",($fb ? 1 : 0),$uname[$u];
623 warn join(',',@{$t->{$b}},$a,$t);
630 my ($fh,$name,$h,$rep,$min_el,$max_el) = @_;
631 print $fh "# $0 @orig_ARGV\n" unless $opt{'q'};
632 print $fh "<code_set_name> \"$name\"\n";
636 print $fh "<mb_cur_min> $min_el\n";
640 print $fh "<mb_cur_max> $max_el\n";
644 print $fh "<subchar> ";
645 foreach my $c (split(//,$rep))
647 printf $fh "\\x%02X",ord($c);
652 output_ucm_page(\@cmap,$h,$h,0);
653 print $fh "#\nCHARMAP\n";
654 foreach my $line (sort { substr($a,8) cmp substr($b,8) } @cmap)
658 print $fh "END CHARMAP\n";