3 unshift @INC, '../../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('qo: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/;
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:
75 $^X $0 $cname @orig_ARGV
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>);
122 if ($a =~ /^.*-(\d+)/)
125 if ($b =~ /^.*-(\d+)/)
135 foreach my $enc (sort cmp_name @encfiles)
137 my ($name,$sfx) = $enc =~ /^.*?([\w-]+)\.(enc|ucm)$/;
138 $name = $opt{'n'} if exists $opt{'n'};
143 compile_enc(\*E,lc($name));
147 compile_ucm(\*E,lc($name));
152 warn "Cannot open $enc for $name:$!";
158 foreach my $name (sort cmp_name keys %encoding)
160 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
161 output(\*C,$name.'_utf8',$e2u);
162 output(\*C,'utf8_'.$name,$u2e);
163 push(@{$encoding{$name}},outstring(\*C,$e2u->{Cname}.'_def',$erep));
165 foreach my $enc (sort cmp_name keys %encoding)
167 my ($e2u,$u2e,$rep,$min_el,$max_el,$rsym) = @{$encoding{$enc}};
168 my @info = ($e2u->{Cname},$u2e->{Cname},$rsym,length($rep),$min_el,$max_el);
169 my $sym = "${enc}_encoding";
171 print C "encode_t $sym = \n";
172 print C " {",join(',',@info,"{\"$enc\",(const char *)0}"),"};\n\n";
175 foreach my $enc (sort cmp_name keys %encoding)
177 my $sym = "${enc}_encoding";
179 print H "extern encode_t $sym;\n";
180 print D " Encode_XSEncoding(aTHX_ &$sym);\n";
183 if ($cname =~ /(\w+)\.xs$/)
189 Encode_XSEncoding(pTHX_ encode_t *enc)
192 HV *stash = gv_stashpv("Encode::XS", TRUE);
193 SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(enc))),stash);
199 const char *name = enc->name[i++];
200 XPUSHs(sv_2mortal(newSVpvn(name,strlen(name))));
203 call_pv("Encode::define_encoding",G_DISCARD);
209 print C "\nMODULE = Encode::$mod\tPACKAGE = Encode::$mod\n\n";
210 print C "BOOT:\n{\n";
211 print C "#include \"$dname\"\n";
219 foreach my $name (sort cmp_name keys %encoding)
221 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
222 output_enc(\*C,$name,$e2u);
227 foreach my $name (sort cmp_name keys %encoding)
229 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
230 output_ucm(\*C,$name,$u2e,$erep,$min_el,$max_el);
247 last if /^\s*CHARMAP\s*$/i;
248 if (/^\s*<(\w+)>\s+"?([^"]*)"?\s*$/i)
253 if (!defined($cs = $attr{'code_set_name'}))
255 warn "No <code_set_name> in $name\n";
259 $name = $cs unless exists $opt{'n'};
265 if (exists $attr{'subchar'})
268 $attr{'subchar'} =~ /^\s*/cg;
269 push(@byte,$1) while $attr{'subchar'} =~ /\G\\x([0-9a-f]+)/icg;
270 $erep = join('',map(chr(hex($_)),@byte));
272 print "Scanning $name ($cs)\n";
278 last if /^\s*END\s+CHARMAP\s*$/i;
282 $u = $1 if (/^<U([0-9a-f]+)>\s+/igc);
283 push(@byte,$1) while /\G\\x([0-9a-f]+)/igc;
284 $fb = $1 if /\G\s*(\|[0-3])/gc;
285 # warn "$_: $u @byte | $fb\n";
286 die "Bad line:$_" unless /\G\s*(#.*)?$/gc;
289 my $uch = encode_U(hex($u));
290 my $ech = join('',map(chr(hex($_)),@byte));
291 my $el = length($ech);
292 $max_el = $el if (!defined($max_el) || $el > $max_el);
293 $min_el = $el if (!defined($min_el) || $el < $min_el);
304 # $fb is fallback flag
305 # 0 - round trip safe
306 # 1 - fallback for unicode -> enc
307 # 2 - skip sub-char mapping
308 # 3 - fallback enc -> unicode
309 enter($u2e,$uch,$ech,$u2e,$fb+0) if ($fb =~ /[01]/);
310 enter($e2u,$ech,$uch,$e2u,$fb+0) if ($fb =~ /[03]/);
319 die "$nfb entries without fallback, $hfb entries with\n";
321 $encoding{$name} = [$e2u,$u2e,$erep,$min_el,$max_el];
331 while ($type = <$fh>)
333 last if $type !~ /^\s*#/;
336 return if $type eq 'E';
337 my ($def,$sym,$pages) = split(/\s+/,scalar(<$fh>));
338 warn "$type encoded $name\n";
345 $rep = &{"encode_$type"}($v & 0xFF, ($v >> 8) & 0xffe);
352 my $page = hex($line);
354 for (my $i = 0; $i < 16; $i++)
357 for (my $j = 0; $j < 16; $j++)
360 my $ech = &{"encode_$type"}($ch,$page);
361 my $val = hex(substr($line,0,4,''));
362 next if $val == 0xFFFD;
363 if ($val || (!$ch && !$page))
365 my $el = length($ech);
366 $max_el = $el if (!defined($max_el) || $el > $max_el);
367 $min_el = $el if (!defined($min_el) || $el < $min_el);
368 my $uch = encode_U($val);
369 if (exists $seen{$uch})
371 warn sprintf("U%04X is %02X%02X and %02X%02X\n",
372 $val,$page,$ch,@{$seen{$uch}});
376 $seen{$uch} = [$page,$ch];
378 enter($e2u,$ech,$uch,$e2u,0);
379 enter($u2e,$uch,$ech,$u2e,0);
383 # No character at this position
384 # enter($e2u,$ech,undef,$e2u);
390 $encoding{$name} = [$e2u,$u2e,$rep,$min_el,$max_el];
395 my ($a,$s,$d,$t,$fb) = @_;
397 my $b = substr($s,0,1);
401 $e = [$b,$b,'',{},length($s),0,$fb];
406 enter($e->[3],substr($s,1),$d,$t,$fb);
412 $e->[5] = length($d);
418 my ($fh,$name,$s) = @_;
419 my $sym = $strings{$s};
422 foreach my $o (keys %strings)
424 my $i = index($o,$s);
428 $sym .= sprintf("+0x%02x",$i) if ($i);
432 $strings{$s} = $sym = $name;
433 printf $fh "\nstatic const U8 %s[%d] =\n",$name,length($s);
434 # Do in chunks of 16 chars to constrain line length
435 # Assumes ANSI C adjacent string litteral concatenation
438 my $c = substr($s,0,16,'');
439 print $fh '"',join('',map(sprintf('\x%02x',ord($_)),split(//,$c))),'"';
440 print $fh "\n" if length($s);
452 my @keys = grep(ref($a->{$_}),sort keys %$a);
455 foreach my $b (@keys)
457 my ($s,$f,$out,$t,$end) = @{$a->{$b}};
459 ord($b) == ord($a->{$l}[1])+1 &&
460 $a->{$l}[3] == $a->{$b}[3] &&
461 $a->{$l}[4] == $a->{$b}[4] &&
462 $a->{$l}[5] == $a->{$b}[5] &&
463 $a->{$l}[6] == $a->{$b}[6]
464 # && length($a->{$l}[2]) < 16
467 my $i = ord($b)-ord($a->{$l}[0]);
469 $a->{$l}[2] .= $a->{$b}[2];
476 if (exists $t->{Cname})
478 $t->{'Forward'} = 1 if $t != $a;
482 process(sprintf("%s_%02x",$name,ord($s)),$t);
485 if (ord($keys[-1]) < 255)
487 my $t = chr(ord($keys[-1])+1);
488 $a->{$t} = [$t,chr(255),undef,$a,0,0];
491 $a->{'Entries'} = \@ent;
497 my $name = $a->{'Cname'};
499 foreach my $b (@{$a->{'Entries'}})
501 next unless $a->{$b}[5];
502 my $s = ord($a->{$b}[0]);
503 my $e = ord($a->{$b}[1]);
504 outstring($fh,sprintf("%s__%02x_%02x",$name,$s,$e),$a->{$b}[2]);
508 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"];\n";
511 foreach my $b (@{$a->{'Entries'}})
513 my ($s,$e,$out,$t,$end,$l) = @{$a->{$b}};
514 outtable($fh,$t) unless $t->{'Done'};
516 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"] = {\n";
517 foreach my $b (@{$a->{'Entries'}})
519 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
526 printf $fh outstring($fh,'',$out);
532 print $fh ",",$t->{Cname};
533 printf $fh ",0x%02x,0x%02x,$l,$end},\n",$sc,$ec;
540 my ($fh,$name,$a) = @_;
548 my ($fh,$name,$a) = @_;
549 foreach my $b (sort keys %$a)
551 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
563 my $s = do "unicore/Name.pl";
564 die "char_names: unicore/Name.pl: $!\n" unless defined $s;
566 while ($s =~ /\G([0-9a-f]+)\t([0-9a-f]*)\t(.*?)\s*\n/igc)
570 last if $s >= 0x10000;
571 my $e = length($2) ? hex($2) : $s;
572 for (my $i = $s; $i <= $e; $i++)
575 # print sprintf("U%04X $name\n",$i);
582 my ($cmap,$a,$t,$pre) = @_;
583 # warn sprintf("Page %x\n",$pre);
584 foreach my $b (sort keys %$t)
586 my ($s,$e,$out,$n,$end,$l,$fb) = @{$t->{$b}};
587 die "oops $s $e" unless $s eq $e;
589 if ($n != $a && $n != $t)
591 output_ucm_page($cmap,$a,$n,(($pre|($u &0x3F)) << 6)&0xFFFF);
597 $u = $pre|($u &0x3f);
599 my $s = sprintf "<U%04X> ",$u;
600 foreach my $c (split(//,$out))
602 $s .= sprintf "\\x%02X",ord($c);
604 $s .= sprintf " |%d # %s\n",($fb ? 1 : 0),$uname[$u];
609 warn join(',',@{$t->{$b}},$a,$t);
616 my ($fh,$name,$h,$rep,$min_el,$max_el) = @_;
617 print $fh "# $0 @orig_ARGV\n" unless $opt{'q'};
618 print $fh "<code_set_name> \"$name\"\n";
622 print $fh "<mb_cur_min> $min_el\n";
626 print $fh "<mb_cur_max> $max_el\n";
630 print $fh "<subchar> ";
631 foreach my $c (split(//,$rep))
633 printf $fh "\\x%02X",ord($c);
638 output_ucm_page(\@cmap,$h,$h,0);
639 print $fh "#\nCHARMAP\n";
640 foreach my $line (sort { substr($a,8) cmp substr($b,8) } @cmap)
644 print $fh "END CHARMAP\n";