3 unshift @INC, '../../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/;
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:
76 $^X $0 $cname @orig_ARGV
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";
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_Define(aTHX_ &$sym);\n";
183 if ($cname =~ /(\w+)\.xs$/)
186 print C "\nMODULE = Encode::$mod\tPACKAGE = Encode::$mod\n\n";
187 print C "BOOT:\n{\n";
188 print C "#include \"$dname\"\n";
196 foreach my $name (sort cmp_name keys %encoding)
198 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
199 output_enc(\*C,$name,$e2u);
204 foreach my $name (sort cmp_name keys %encoding)
206 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
207 output_ucm(\*C,$name,$u2e,$erep,$min_el,$max_el);
224 last if /^\s*CHARMAP\s*$/i;
225 if (/^\s*<(\w+)>\s+"?([^"]*)"?\s*$/i)
230 if (!defined($cs = $attr{'code_set_name'}))
232 warn "No <code_set_name> in $name\n";
236 $name = $cs unless exists $opt{'n'};
242 if (exists $attr{'subchar'})
245 $attr{'subchar'} =~ /^\s*/cg;
246 push(@byte,$1) while $attr{'subchar'} =~ /\G\\x([0-9a-f]+)/icg;
247 $erep = join('',map(chr(hex($_)),@byte));
249 print "Scanning $name ($cs)\n";
255 last if /^\s*END\s+CHARMAP\s*$/i;
259 $u = $1 if (/^<U([0-9a-f]+)>\s+/igc);
260 push(@byte,$1) while /\G\\x([0-9a-f]+)/igc;
261 $fb = $1 if /\G\s*(\|[0-3])/gc;
262 # warn "$_: $u @byte | $fb\n";
263 die "Bad line:$_" unless /\G\s*(#.*)?$/gc;
266 my $uch = encode_U(hex($u));
267 my $ech = join('',map(chr(hex($_)),@byte));
268 my $el = length($ech);
269 $max_el = $el if (!defined($max_el) || $el > $max_el);
270 $min_el = $el if (!defined($min_el) || $el < $min_el);
281 # $fb is fallback flag
282 # 0 - round trip safe
283 # 1 - fallback for unicode -> enc
284 # 2 - skip sub-char mapping
285 # 3 - fallback enc -> unicode
286 enter($u2e,$uch,$ech,$u2e,$fb+0) if ($fb =~ /[01]/);
287 enter($e2u,$ech,$uch,$e2u,$fb+0) if ($fb =~ /[03]/);
296 die "$nfb entries without fallback, $hfb entries with\n";
298 $encoding{$name} = [$e2u,$u2e,$erep,$min_el,$max_el];
308 while ($type = <$fh>)
310 last if $type !~ /^\s*#/;
313 return if $type eq 'E';
314 my ($def,$sym,$pages) = split(/\s+/,scalar(<$fh>));
315 warn "$type encoded $name\n";
322 $rep = &{"encode_$type"}($v & 0xFF, ($v >> 8) & 0xffe);
329 my $page = hex($line);
331 for (my $i = 0; $i < 16; $i++)
334 for (my $j = 0; $j < 16; $j++)
337 my $ech = &{"encode_$type"}($ch,$page);
338 my $val = hex(substr($line,0,4,''));
339 next if $val == 0xFFFD;
340 if ($val || (!$ch && !$page))
342 my $el = length($ech);
343 $max_el = $el if (!defined($max_el) || $el > $max_el);
344 $min_el = $el if (!defined($min_el) || $el < $min_el);
345 my $uch = encode_U($val);
346 if (exists $seen{$uch})
348 warn sprintf("U%04X is %02X%02X and %02X%02X\n",
349 $val,$page,$ch,@{$seen{$uch}});
353 $seen{$uch} = [$page,$ch];
355 enter($e2u,$ech,$uch,$e2u,0);
356 enter($u2e,$uch,$ech,$u2e,0);
360 # No character at this position
361 # enter($e2u,$ech,undef,$e2u);
367 $encoding{$name} = [$e2u,$u2e,$rep,$min_el,$max_el];
372 my ($a,$s,$d,$t,$fb) = @_;
374 my $b = substr($s,0,1);
378 $e = [$b,$b,'',{},length($s),0,$fb];
383 enter($e->[3],substr($s,1),$d,$t,$fb);
389 $e->[5] = length($d);
395 my ($fh,$name,$s) = @_;
396 my $sym = $strings{$s};
399 foreach my $o (keys %strings)
401 my $i = index($o,$s);
405 $sym .= sprintf("+0x%02x",$i) if ($i);
409 $strings{$s} = $sym = $name;
410 printf $fh "\nstatic const U8 %s[%d] =\n",$name,length($s);
411 # Do in chunks of 16 chars to constrain line length
412 # Assumes ANSI C adjacent string litteral concatenation
415 my $c = substr($s,0,16,'');
416 print $fh '"',join('',map(sprintf('\x%02x',ord($_)),split(//,$c))),'"';
417 print $fh "\n" if length($s);
429 my @keys = grep(ref($a->{$_}),sort keys %$a);
432 foreach my $b (@keys)
434 my ($s,$f,$out,$t,$end) = @{$a->{$b}};
436 ord($b) == ord($a->{$l}[1])+1 &&
437 $a->{$l}[3] == $a->{$b}[3] &&
438 $a->{$l}[4] == $a->{$b}[4] &&
439 $a->{$l}[5] == $a->{$b}[5] &&
440 $a->{$l}[6] == $a->{$b}[6]
441 # && length($a->{$l}[2]) < 16
444 my $i = ord($b)-ord($a->{$l}[0]);
446 $a->{$l}[2] .= $a->{$b}[2];
453 if (exists $t->{Cname})
455 $t->{'Forward'} = 1 if $t != $a;
459 process(sprintf("%s_%02x",$name,ord($s)),$t);
462 if (ord($keys[-1]) < 255)
464 my $t = chr(ord($keys[-1])+1);
465 $a->{$t} = [$t,chr(255),undef,$a,0,0];
468 $a->{'Entries'} = \@ent;
474 my $name = $a->{'Cname'};
476 foreach my $b (@{$a->{'Entries'}})
478 next unless $a->{$b}[5];
479 my $s = ord($a->{$b}[0]);
480 my $e = ord($a->{$b}[1]);
481 outstring($fh,sprintf("%s__%02x_%02x",$name,$s,$e),$a->{$b}[2]);
485 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"];\n";
488 foreach my $b (@{$a->{'Entries'}})
490 my ($s,$e,$out,$t,$end,$l) = @{$a->{$b}};
491 outtable($fh,$t) unless $t->{'Done'};
493 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"] = {\n";
494 foreach my $b (@{$a->{'Entries'}})
496 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
503 printf $fh outstring($fh,'',$out);
509 print $fh ",",$t->{Cname};
510 printf $fh ",0x%02x,0x%02x,$l,$end},\n",$sc,$ec;
517 my ($fh,$name,$a) = @_;
525 my ($fh,$name,$a) = @_;
526 foreach my $b (sort keys %$a)
528 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
540 my $s = do "unicore/Name.pl";
541 die "char_names: unicore/Name.pl: $!\n" unless defined $s;
543 while ($s =~ /\G([0-9a-f]+)\t([0-9a-f]*)\t(.*?)\s*\n/igc)
547 last if $s >= 0x10000;
548 my $e = length($2) ? hex($2) : $s;
549 for (my $i = $s; $i <= $e; $i++)
552 # print sprintf("U%04X $name\n",$i);
559 my ($cmap,$a,$t,$pre) = @_;
560 # warn sprintf("Page %x\n",$pre);
561 foreach my $b (sort keys %$t)
563 my ($s,$e,$out,$n,$end,$l,$fb) = @{$t->{$b}};
564 die "oops $s $e" unless $s eq $e;
566 if ($n != $a && $n != $t)
568 output_ucm_page($cmap,$a,$n,(($pre|($u &0x3F)) << 6)&0xFFFF);
574 $u = $pre|($u &0x3f);
576 my $s = sprintf "<U%04X> ",$u;
577 foreach my $c (split(//,$out))
579 $s .= sprintf "\\x%02X",ord($c);
581 $s .= sprintf " |%d # %s\n",($fb ? 1 : 0),$uname[$u];
586 warn join(',',@{$t->{$b}},$a,$t);
593 my ($fh,$name,$h,$rep,$min_el,$max_el) = @_;
594 print $fh "# $0 @orig_ARGV\n" unless $opt{'q'};
595 print $fh "<code_set_name> \"$name\"\n";
599 print $fh "<mb_cur_min> $min_el\n";
603 print $fh "<mb_cur_max> $max_el\n";
607 print $fh "<subchar> ";
608 foreach my $c (split(//,$rep))
610 printf $fh "\\x%02X",ord($c);
615 output_ucm_page(\@cmap,$h,$h,0);
616 print $fh "#\nCHARMAP\n";
617 foreach my $line (sort { substr($a,8) cmp substr($b,8) } @cmap)
621 print $fh "END CHARMAP\n";