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";
89 elsif ($cname =~ /\.enc$/)
93 elsif ($cname =~ /\.ucm$/)
97 elsif ($cname =~ /\.pet$/)
103 if (exists $opt{'f'})
105 # -F is followed by name of file containing list of filenames
106 my $flist = $opt{'f'};
107 open(FLIST,$flist) || die "Cannot open $flist:$!";
108 chomp(@encfiles = <FLIST>);
121 if ($a =~ /^.*-(\d+)/)
124 if ($b =~ /^.*-(\d+)/)
134 foreach my $enc (sort cmp_name @encfiles)
136 my ($name,$sfx) = $enc =~ /^.*?([\w-]+)\.(enc|ucm)$/;
137 $name = $opt{'n'} if exists $opt{'n'};
142 compile_enc(\*E,lc($name));
146 compile_ucm(\*E,lc($name));
151 warn "Cannot open $enc for $name:$!";
157 foreach my $name (sort cmp_name keys %encoding)
159 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
160 output(\*C,$name.'_utf8',$e2u);
161 output(\*C,'utf8_'.$name,$u2e);
162 push(@{$encoding{$name}},outstring(\*C,$e2u->{Cname}.'_def',$erep));
164 foreach my $enc (sort cmp_name keys %encoding)
166 my ($e2u,$u2e,$rep,$min_el,$max_el,$rsym) = @{$encoding{$enc}};
167 my @info = ($e2u->{Cname},$u2e->{Cname},$rsym,length($rep),$min_el,$max_el);
168 my $sym = "${enc}_encoding";
170 print C "encode_t $sym = \n";
171 print C " {",join(',',@info,"{\"$enc\",(const char *)0}"),"};\n\n";
174 foreach my $enc (sort cmp_name keys %encoding)
176 my $sym = "${enc}_encoding";
178 print H "extern encode_t $sym;\n";
179 print D " Encode_Define(aTHX_ &$sym);\n";
182 if ($cname =~ /(\w+)\.xs$/)
185 print C "\nMODULE = Encode::$mod\tPACKAGE = Encode::$mod\n\n";
186 print C "BOOT:\n{\n";
187 print C "#include \"$dname\"\n";
195 foreach my $name (sort cmp_name keys %encoding)
197 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
198 output_enc(\*C,$name,$e2u);
203 foreach my $name (sort cmp_name keys %encoding)
205 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
206 output_ucm(\*C,$name,$u2e,$erep,$min_el,$max_el);
223 last if /^\s*CHARMAP\s*$/i;
224 if (/^\s*<(\w+)>\s+"?([^"]*)"?\s*$/i)
229 if (!defined($cs = $attr{'code_set_name'}))
231 warn "No <code_set_name> in $name\n";
235 $name = $cs unless exists $opt{'n'};
241 if (exists $attr{'subchar'})
244 $attr{'subchar'} =~ /^\s*/cg;
245 push(@byte,$1) while $attr{'subchar'} =~ /\G\\x([0-9a-f]+)/icg;
246 $erep = join('',map(chr(hex($_)),@byte));
248 print "Scanning $name ($cs)\n";
254 last if /^\s*END\s+CHARMAP\s*$/i;
258 $u = $1 if (/^<U([0-9a-f]+)>\s+/igc);
259 push(@byte,$1) while /\G\\x([0-9a-f]+)/igc;
260 $fb = $1 if /\G\s*(\|[0-3])/gc;
261 # warn "$_: $u @byte | $fb\n";
262 die "Bad line:$_" unless /\G\s*(#.*)?$/gc;
265 my $uch = encode_U(hex($u));
266 my $ech = join('',map(chr(hex($_)),@byte));
267 my $el = length($ech);
268 $max_el = $el if (!defined($max_el) || $el > $max_el);
269 $min_el = $el if (!defined($min_el) || $el < $min_el);
280 # $fb is fallback flag
281 # 0 - round trip safe
282 # 1 - fallback for unicode -> enc
283 # 2 - skip sub-char mapping
284 # 3 - fallback enc -> unicode
285 enter($u2e,$uch,$ech,$u2e,$fb+0) if ($fb =~ /[01]/);
286 enter($e2u,$ech,$uch,$e2u,$fb+0) if ($fb =~ /[03]/);
295 die "$nfb entries without fallback, $hfb entries with\n";
297 $encoding{$name} = [$e2u,$u2e,$erep,$min_el,$max_el];
307 while ($type = <$fh>)
309 last if $type !~ /^\s*#/;
312 return if $type eq 'E';
313 my ($def,$sym,$pages) = split(/\s+/,scalar(<$fh>));
314 warn "$type encoded $name\n";
321 $rep = &{"encode_$type"}($v & 0xFF, ($v >> 8) & 0xffe);
328 my $page = hex($line);
330 for (my $i = 0; $i < 16; $i++)
333 for (my $j = 0; $j < 16; $j++)
336 my $ech = &{"encode_$type"}($ch,$page);
337 my $val = hex(substr($line,0,4,''));
338 next if $val == 0xFFFD;
339 if ($val || (!$ch && !$page))
341 my $el = length($ech);
342 $max_el = $el if (!defined($max_el) || $el > $max_el);
343 $min_el = $el if (!defined($min_el) || $el < $min_el);
344 my $uch = encode_U($val);
345 if (exists $seen{$uch})
347 warn sprintf("U%04X is %02X%02X and %02X%02X\n",
348 $val,$page,$ch,@{$seen{$uch}});
352 $seen{$uch} = [$page,$ch];
354 enter($e2u,$ech,$uch,$e2u,0);
355 enter($u2e,$uch,$ech,$u2e,0);
359 # No character at this position
360 # enter($e2u,$ech,undef,$e2u);
366 $encoding{$name} = [$e2u,$u2e,$rep,$min_el,$max_el];
371 my ($a,$s,$d,$t,$fb) = @_;
373 my $b = substr($s,0,1);
377 $e = [$b,$b,'',{},length($s),0,$fb];
382 enter($e->[3],substr($s,1),$d,$t,$fb);
388 $e->[5] = length($d);
394 my ($fh,$name,$s) = @_;
395 my $sym = $strings{$s};
398 foreach my $o (keys %strings)
400 my $i = index($o,$s);
404 $sym .= sprintf("+0x%02x",$i) if ($i);
408 $strings{$s} = $sym = $name;
409 printf $fh "\nstatic const U8 %s[%d] =\n",$name,length($s);
410 # Do in chunks of 16 chars to constrain line length
411 # Assumes ANSI C adjacent string litteral concatenation
414 my $c = substr($s,0,16,'');
415 print $fh '"',join('',map(sprintf('\x%02x',ord($_)),split(//,$c))),'"';
416 print $fh "\n" if length($s);
428 my @keys = grep(ref($a->{$_}),sort keys %$a);
431 foreach my $b (@keys)
433 my ($s,$f,$out,$t,$end) = @{$a->{$b}};
435 ord($b) == ord($a->{$l}[1])+1 &&
436 $a->{$l}[3] == $a->{$b}[3] &&
437 $a->{$l}[4] == $a->{$b}[4] &&
438 $a->{$l}[5] == $a->{$b}[5] &&
439 $a->{$l}[6] == $a->{$b}[6]
440 # && length($a->{$l}[2]) < 16
443 my $i = ord($b)-ord($a->{$l}[0]);
445 $a->{$l}[2] .= $a->{$b}[2];
452 if (exists $t->{Cname})
454 $t->{'Forward'} = 1 if $t != $a;
458 process(sprintf("%s_%02x",$name,ord($s)),$t);
461 if (ord($keys[-1]) < 255)
463 my $t = chr(ord($keys[-1])+1);
464 $a->{$t} = [$t,chr(255),undef,$a,0,0];
467 $a->{'Entries'} = \@ent;
473 my $name = $a->{'Cname'};
475 foreach my $b (@{$a->{'Entries'}})
477 next unless $a->{$b}[5];
478 my $s = ord($a->{$b}[0]);
479 my $e = ord($a->{$b}[1]);
480 outstring($fh,sprintf("%s__%02x_%02x",$name,$s,$e),$a->{$b}[2]);
484 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"];\n";
487 foreach my $b (@{$a->{'Entries'}})
489 my ($s,$e,$out,$t,$end,$l) = @{$a->{$b}};
490 outtable($fh,$t) unless $t->{'Done'};
492 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"] = {\n";
493 foreach my $b (@{$a->{'Entries'}})
495 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
502 printf $fh outstring($fh,'',$out);
508 print $fh ",",$t->{Cname};
509 printf $fh ",0x%02x,0x%02x,$l,$end},\n",$sc,$ec;
516 my ($fh,$name,$a) = @_;
524 my ($fh,$name,$a) = @_;
525 foreach my $b (sort keys %$a)
527 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
539 my $s = do "unicore/Name.pl";
540 die "char_names: unicore/Name.pl: $!\n" unless defined $s;
542 while ($s =~ /\G([0-9a-f]+)\t([0-9a-f]*)\t(.*?)\s*\n/igc)
546 last if $s >= 0x10000;
547 my $e = length($2) ? hex($2) : $s;
548 for (my $i = $s; $i <= $e; $i++)
551 # print sprintf("U%04X $name\n",$i);
558 my ($cmap,$a,$t,$pre) = @_;
559 # warn sprintf("Page %x\n",$pre);
560 foreach my $b (sort keys %$t)
562 my ($s,$e,$out,$n,$end,$l,$fb) = @{$t->{$b}};
563 die "oops $s $e" unless $s eq $e;
565 if ($n != $a && $n != $t)
567 output_ucm_page($cmap,$a,$n,(($pre|($u &0x3F)) << 6)&0xFFFF);
573 $u = $pre|($u &0x3f);
575 my $s = sprintf "<U%04X> ",$u;
576 foreach my $c (split(//,$out))
578 $s .= sprintf "\\x%02X",ord($c);
580 $s .= sprintf " |%d # %s\n",($fb ? 1 : 0),$uname[$u];
585 warn join(',',@{$t->{$b}},$a,$t);
592 my ($fh,$name,$h,$rep,$min_el,$max_el) = @_;
593 print $fh "# $0 @orig_ARGV\n" unless $opt{'q'};
594 print $fh "<code_set_name> \"$name\"\n";
598 print $fh "<mb_cur_min> $min_el\n";
602 print $fh "<mb_cur_max> $max_el\n";
606 print $fh "<subchar> ";
607 foreach my $c (split(//,$rep))
609 printf $fh "\\x%02X",ord($c);
614 output_ucm_page(\@cmap,$h,$h,0);
615 print $fh "#\nCHARMAP\n";
616 foreach my $line (sort { substr($a,8) cmp substr($b,8) } @cmap)
620 print $fh "END CHARMAP\n";