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";
91 elsif ($cname =~ /\.enc$/)
95 elsif ($cname =~ /\.ucm$/)
99 elsif ($cname =~ /\.pet$/)
105 if (exists $opt{'f'})
107 # -F is followed by name of file containing list of filenames
108 my $flist = $opt{'f'};
109 open(FLIST,$flist) || die "Cannot open $flist:$!";
110 chomp(@encfiles = <FLIST>);
123 if ($a =~ /^.*-(\d+)/)
126 if ($b =~ /^.*-(\d+)/)
136 foreach my $enc (sort cmp_name @encfiles)
138 my ($name,$sfx) = $enc =~ /^.*?([\w-]+)\.(enc|ucm)$/;
139 $name = $opt{'n'} if exists $opt{'n'};
144 compile_enc(\*E,lc($name));
148 compile_ucm(\*E,lc($name));
153 warn "Cannot open $enc for $name:$!";
159 foreach my $name (sort cmp_name keys %encoding)
161 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
162 output(\*C,$name.'_utf8',$e2u);
163 output(\*C,'utf8_'.$name,$u2e);
164 push(@{$encoding{$name}},outstring(\*C,$e2u->{Cname}.'_def',$erep));
166 foreach my $enc (sort cmp_name keys %encoding)
168 my ($e2u,$u2e,$rep,$min_el,$max_el,$rsym) = @{$encoding{$enc}};
169 my @info = ($e2u->{Cname},$u2e->{Cname},$rsym,length($rep),$min_el,$max_el);
170 my $sym = "${enc}_encoding";
172 print C "encode_t $sym = \n";
173 print C " {",join(',',@info,"{\"$enc\",(const char *)0}"),"};\n\n";
176 foreach my $enc (sort cmp_name keys %encoding)
178 my $sym = "${enc}_encoding";
180 print H "extern encode_t $sym;\n";
181 print D " Encode_XSEncoding(aTHX_ &$sym);\n";
184 if ($cname =~ /(\w+)\.xs$/)
190 Encode_XSEncoding(pTHX_ encode_t *enc)
193 HV *stash = gv_stashpv("Encode::XS", TRUE);
194 SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(enc))),stash);
200 const char *name = enc->name[i++];
201 XPUSHs(sv_2mortal(newSVpvn(name,strlen(name))));
204 call_pv("Encode::define_encoding",G_DISCARD);
210 print C "\nMODULE = Encode::$mod\tPACKAGE = Encode::$mod\n\n";
211 print C "BOOT:\n{\n";
212 print C "#include \"$dname\"\n";
220 foreach my $name (sort cmp_name keys %encoding)
222 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
223 output_enc(\*C,$name,$e2u);
228 foreach my $name (sort cmp_name keys %encoding)
230 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
231 output_ucm(\*C,$name,$u2e,$erep,$min_el,$max_el);
248 last if /^\s*CHARMAP\s*$/i;
249 if (/^\s*<(\w+)>\s+"?([^"]*)"?\s*$/i)
254 if (!defined($cs = $attr{'code_set_name'}))
256 warn "No <code_set_name> in $name\n";
260 $name = $cs unless exists $opt{'n'};
266 if (exists $attr{'subchar'})
269 $attr{'subchar'} =~ /^\s*/cg;
270 push(@byte,$1) while $attr{'subchar'} =~ /\G\\x([0-9a-f]+)/icg;
271 $erep = join('',map(chr(hex($_)),@byte));
273 print "Scanning $name ($cs)\n";
279 last if /^\s*END\s+CHARMAP\s*$/i;
283 $u = $1 if (/^<U([0-9a-f]+)>\s+/igc);
284 push(@byte,$1) while /\G\\x([0-9a-f]+)/igc;
285 $fb = $1 if /\G\s*(\|[0-3])/gc;
286 # warn "$_: $u @byte | $fb\n";
287 die "Bad line:$_" unless /\G\s*(#.*)?$/gc;
290 my $uch = encode_U(hex($u));
291 my $ech = join('',map(chr(hex($_)),@byte));
292 my $el = length($ech);
293 $max_el = $el if (!defined($max_el) || $el > $max_el);
294 $min_el = $el if (!defined($min_el) || $el < $min_el);
305 # $fb is fallback flag
306 # 0 - round trip safe
307 # 1 - fallback for unicode -> enc
308 # 2 - skip sub-char mapping
309 # 3 - fallback enc -> unicode
310 enter($u2e,$uch,$ech,$u2e,$fb+0) if ($fb =~ /[01]/);
311 enter($e2u,$ech,$uch,$e2u,$fb+0) if ($fb =~ /[03]/);
320 die "$nfb entries without fallback, $hfb entries with\n";
322 $encoding{$name} = [$e2u,$u2e,$erep,$min_el,$max_el];
332 while ($type = <$fh>)
334 last if $type !~ /^\s*#/;
337 return if $type eq 'E';
338 my ($def,$sym,$pages) = split(/\s+/,scalar(<$fh>));
339 warn "$type encoded $name\n";
346 $rep = &{"encode_$type"}($v & 0xFF, ($v >> 8) & 0xffe);
353 my $page = hex($line);
355 for (my $i = 0; $i < 16; $i++)
358 for (my $j = 0; $j < 16; $j++)
361 my $ech = &{"encode_$type"}($ch,$page);
362 my $val = hex(substr($line,0,4,''));
363 next if $val == 0xFFFD;
364 if ($val || (!$ch && !$page))
366 my $el = length($ech);
367 $max_el = $el if (!defined($max_el) || $el > $max_el);
368 $min_el = $el if (!defined($min_el) || $el < $min_el);
369 my $uch = encode_U($val);
370 if (exists $seen{$uch})
372 warn sprintf("U%04X is %02X%02X and %02X%02X\n",
373 $val,$page,$ch,@{$seen{$uch}});
377 $seen{$uch} = [$page,$ch];
379 enter($e2u,$ech,$uch,$e2u,0);
380 enter($u2e,$uch,$ech,$u2e,0);
384 # No character at this position
385 # enter($e2u,$ech,undef,$e2u);
391 $encoding{$name} = [$e2u,$u2e,$rep,$min_el,$max_el];
396 my ($a,$s,$d,$t,$fb) = @_;
398 my $b = substr($s,0,1);
402 $e = [$b,$b,'',{},length($s),0,$fb];
407 enter($e->[3],substr($s,1),$d,$t,$fb);
413 $e->[5] = length($d);
419 my ($fh,$name,$s) = @_;
420 my $sym = $strings{$s};
423 foreach my $o (keys %strings)
425 my $i = index($o,$s);
429 $sym .= sprintf("+0x%02x",$i) if ($i);
433 $strings{$s} = $sym = $name;
434 printf $fh "\nstatic const U8 %s[%d] =\n",$name,length($s);
435 # Do in chunks of 16 chars to constrain line length
436 # Assumes ANSI C adjacent string litteral concatenation
439 my $c = substr($s,0,16,'');
440 print $fh '"',join('',map(sprintf('\x%02x',ord($_)),split(//,$c))),'"';
441 print $fh "\n" if length($s);
453 my @keys = grep(ref($a->{$_}),sort keys %$a);
456 foreach my $b (@keys)
458 my ($s,$f,$out,$t,$end) = @{$a->{$b}};
460 ord($b) == ord($a->{$l}[1])+1 &&
461 $a->{$l}[3] == $a->{$b}[3] &&
462 $a->{$l}[4] == $a->{$b}[4] &&
463 $a->{$l}[5] == $a->{$b}[5] &&
464 $a->{$l}[6] == $a->{$b}[6]
465 # && length($a->{$l}[2]) < 16
468 my $i = ord($b)-ord($a->{$l}[0]);
470 $a->{$l}[2] .= $a->{$b}[2];
477 if (exists $t->{Cname})
479 $t->{'Forward'} = 1 if $t != $a;
483 process(sprintf("%s_%02x",$name,ord($s)),$t);
486 if (ord($keys[-1]) < 255)
488 my $t = chr(ord($keys[-1])+1);
489 $a->{$t} = [$t,chr(255),undef,$a,0,0];
492 $a->{'Entries'} = \@ent;
498 my $name = $a->{'Cname'};
500 foreach my $b (@{$a->{'Entries'}})
502 next unless $a->{$b}[5];
503 my $s = ord($a->{$b}[0]);
504 my $e = ord($a->{$b}[1]);
505 outstring($fh,sprintf("%s__%02x_%02x",$name,$s,$e),$a->{$b}[2]);
509 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"];\n";
512 foreach my $b (@{$a->{'Entries'}})
514 my ($s,$e,$out,$t,$end,$l) = @{$a->{$b}};
515 outtable($fh,$t) unless $t->{'Done'};
517 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"] = {\n";
518 foreach my $b (@{$a->{'Entries'}})
520 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
527 printf $fh outstring($fh,'',$out);
533 print $fh ",",$t->{Cname};
534 printf $fh ",0x%02x,0x%02x,$l,$end},\n",$sc,$ec;
541 my ($fh,$name,$a) = @_;
549 my ($fh,$name,$a) = @_;
550 foreach my $b (sort keys %$a)
552 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
564 my $s = do "unicore/Name.pl";
565 die "char_names: unicore/Name.pl: $!\n" unless defined $s;
567 while ($s =~ /\G([0-9a-f]+)\t([0-9a-f]*)\t(.*?)\s*\n/igc)
571 last if $s >= 0x10000;
572 my $e = length($2) ? hex($2) : $s;
573 for (my $i = $s; $i <= $e; $i++)
576 # print sprintf("U%04X $name\n",$i);
583 my ($cmap,$a,$t,$pre) = @_;
584 # warn sprintf("Page %x\n",$pre);
585 foreach my $b (sort keys %$t)
587 my ($s,$e,$out,$n,$end,$l,$fb) = @{$t->{$b}};
588 die "oops $s $e" unless $s eq $e;
590 if ($n != $a && $n != $t)
592 output_ucm_page($cmap,$a,$n,(($pre|($u &0x3F)) << 6)&0xFFFF);
598 $u = $pre|($u &0x3f);
600 my $s = sprintf "<U%04X> ",$u;
601 foreach my $c (split(//,$out))
603 $s .= sprintf "\\x%02X",ord($c);
605 $s .= sprintf " |%d # %s\n",($fb ? 1 : 0),$uname[$u];
610 warn join(',',@{$t->{$b}},$a,$t);
617 my ($fh,$name,$h,$rep,$min_el,$max_el) = @_;
618 print $fh "# $0 @orig_ARGV\n" unless $opt{'q'};
619 print $fh "<code_set_name> \"$name\"\n";
623 print $fh "<mb_cur_min> $min_el\n";
627 print $fh "<mb_cur_max> $max_el\n";
631 print $fh "<subchar> ";
632 foreach my $c (split(//,$rep))
634 printf $fh "\\x%02X",ord($c);
639 output_ucm_page(\@cmap,$h,$h,0);
640 print $fh "#\nCHARMAP\n";
641 foreach my $line (sort { substr($a,8) cmp substr($b,8) } @cmap)
645 print $fh "END CHARMAP\n";