2 BEGIN { @INC = '../../lib' };
7 # UTF-8 encode long hand - only covers part of perl's range
15 return chr(($uv >> 6) | 0xC0).
16 chr(($uv & 0x3F) | 0x80);
18 return chr(($uv >> 12) | 0xE0).
19 chr((($uv >> 6) & 0x3F) | 0x80).
20 chr(($uv & 0x3F) | 0x80);
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');
48 my $cname = shift(@ARGV);
49 chmod(0666,$cname) if -f $cname && !-w $cname;
50 open(C,">$cname") || die "Cannot open $cname:$!";
52 $dname =~ s/(\.[^\.]*)?$/.def/;
54 my ($doC,$doEnc,$doUcm);
56 if ($cname =~ /\.(c|xs)$/)
59 chmod(0666,$dname) if -f $cname && !-w $dname;
60 open(D,">$dname") || die "Cannot open $dname:$!";
62 $hname =~ s/(\.[^\.]*)?$/.h/;
63 chmod(0666,$hname) if -f $cname && !-w $hname;
64 open(H,">$hname") || die "Cannot open $hname:$!";
66 foreach my $fh (\*C,\*D,\*H)
70 !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
71 This file was autogenerated by:
77 if ($cname =~ /(\w+)\.xs$/)
79 print C "#include <EXTERN.h>\n";
80 print C "#include <perl.h>\n";
81 print C "#include <XSUB.h>\n";
82 print C "#define U8 U8\n";
84 print C "#include \"encode.h\"\n";
86 elsif ($cname =~ /\.enc$/)
90 elsif ($cname =~ /\.ucm$/)
100 if ($a =~ /^.*-(\d+)/)
103 if ($b =~ /^.*-(\d+)/)
112 foreach my $enc (sort cmp_name @ARGV)
114 my ($name,$sfx) = $enc =~ /^.*?([\w-]+)\.(enc|ucm)$/;
119 compile_enc(\*E,lc($name),\*C);
123 compile_ucm(\*E,lc($name),\*C);
128 warn "Cannot open $enc for $name:$!";
134 foreach my $enc (sort cmp_name keys %encoding)
136 my $sym = "${enc}_encoding";
138 print C "encode_t $sym = \n";
139 print C " {",join(',',"\"$enc\"",@{$encoding{$enc}}),"};\n\n";
142 foreach my $enc (sort cmp_name keys %encoding)
144 my $sym = "${enc}_encoding";
146 print H "extern encode_t $sym;\n";
147 print D " Encode_Define(aTHX_ &$sym);\n";
150 if ($cname =~ /(\w+)\.xs$/)
153 print C "\nMODULE = Encode::$mod\tPACKAGE = Encode::$mod\n\n";
154 print C "BOOT:\n{\n";
155 print C "#include \"$dname\"\n";
166 my ($fh,$name,$ch) = @_;
174 last if /^\s*CHARMAP\s*$/i;
175 if (/^\s*<(\w+)>\s+"?([^"]*)"?\s*$/i)
180 if (!defined($cs = $attr{'code_set_name'}))
182 warn "No <code_set_name> in $name\n";
190 if (exists $attr{'subchar'})
192 my @byte = $attr{'subchar'} =~ /^\s*(?:\\x([0-9a-f]+))+\s*$/;
193 $erep = join('',map(hex($_),@byte));
195 warn "Scanning $name ($cs)\n";
201 last if /^\s*END\s+CHARMAP\s*$/i;
203 my ($u,@byte) = /^<U([0-9a-f]+)>\s+(?:\\x([0-9a-f]+))+\s*(\|[0-3]|)\s*$/i;
207 my $uch = encode_U(hex($u));
208 my $ech = join('',map(chr(hex($_)),@byte));
219 # $fb is fallback flag
220 # 0 - round trip safe
221 # 1 - fallback for unicode -> enc
222 # 2 - skip sub-char mapping
223 # 3 - fallback enc -> unicode
224 enter($u2e,$uch,$ech,$u2e,$fb+0) if ($fb =~ /[01]/);
225 enter($e2u,$ech,$uch,$e2u,$fb+0) if ($fb =~ /[03]/);
235 die "$nfb entries without fallback, $hfb entries with\n";
239 output($ch,$name.'_utf8',$e2u);
240 output($ch,'utf8_'.$name,$u2e);
241 $encoding{$name} = [$e2u->{Cname},$u2e->{Cname},
242 outstring($ch,$e2u->{Cname}.'_def',$erep),length($erep)];
246 output_enc($ch,$name,$e2u);
250 output_ucm($ch,$name,$u2e);
256 my ($fh,$name,$ch) = @_;
261 while ($type = <$fh>)
263 last if $type !~ /^\s*#/;
266 return if $type eq 'E';
267 my ($def,$sym,$pages) = split(/\s+/,scalar(<$fh>));
268 warn "$type encoded $name\n";
273 $rep = &{"encode_$type"}($v & 0xFF, ($v >> 8) & 0xffe);
279 my $page = hex($line);
281 for (my $i = 0; $i < 16; $i++)
284 for (my $j = 0; $j < 16; $j++)
287 my $ech = &{"encode_$type"}($ch,$page);
288 my $val = hex(substr($line,0,4,''));
289 if ($val || (!$ch && !$page))
291 my $uch = encode_U($val);
292 enter($e2u,$ech,$uch,$e2u,0);
293 enter($u2e,$uch,$ech,$u2e,0);
297 # No character at this position
298 # enter($e2u,$ech,undef,$e2u);
306 output($ch,$name.'_utf8',$e2u);
307 output($ch,'utf8_'.$name,$u2e);
308 $encoding{$name} = [$e2u->{Cname},$u2e->{Cname},
309 outstring($ch,$e2u->{Cname}.'_def',$rep),length($rep)];
313 output_enc($ch,$name,$e2u);
317 output_ucm($ch,$name,$u2e);
323 my ($a,$s,$d,$t,$fb) = @_;
325 my $b = substr($s,0,1);
329 $e = [$b,$b,'',{},length($s),0,$fb];
334 enter($e->[3],substr($s,1),$d,$t,$fb);
340 $e->[5] = length($d);
346 my ($fh,$name,$s) = @_;
347 my $sym = $strings{$s};
350 foreach my $o (keys %strings)
352 my $i = index($o,$s);
356 $sym .= sprintf("+0x%02x",$i) if ($i);
360 $strings{$s} = $sym = $name;
361 printf $fh "\nstatic const U8 %s[%d] =\n",$name,length($s);
362 # Do in chunks of 16 chars to constrain line length
363 # Assumes ANSI C adjacent string litteral concatenation
366 my $c = substr($s,0,16,'');
367 print $fh '"',join('',map(sprintf('\x%02x',ord($_)),split(//,$c))),'"';
368 print $fh "\n" if length($s);
380 my @keys = grep(ref($a->{$_}),sort keys %$a);
383 foreach my $b (@keys)
385 my ($s,$f,$out,$t,$end) = @{$a->{$b}};
387 ord($b) == ord($a->{$l}[1])+1 &&
388 $a->{$l}[3] == $a->{$b}[3] &&
389 $a->{$l}[4] == $a->{$b}[4] &&
390 $a->{$l}[5] == $a->{$b}[5] &&
391 $a->{$l}[6] == $a->{$b}[6]
392 # && length($a->{$l}[2]) < 16
395 my $i = ord($b)-ord($a->{$l}[0]);
397 $a->{$l}[2] .= $a->{$b}[2];
404 if (exists $t->{Cname})
406 $t->{'Forward'} = 1 if $t != $a;
410 process(sprintf("%s_%02x",$name,ord($s)),$t);
413 if (ord($keys[-1]) < 255)
415 my $t = chr(ord($keys[-1])+1);
416 $a->{$t} = [$t,chr(255),undef,$a,0,0];
419 $a->{'Entries'} = \@ent;
425 my $name = $a->{'Cname'};
427 foreach my $b (@{$a->{'Entries'}})
429 next unless $a->{$b}[5];
430 my $s = ord($a->{$b}[0]);
431 my $e = ord($a->{$b}[1]);
432 outstring($fh,sprintf("%s__%02x_%02x",$name,$s,$e),$a->{$b}[2]);
436 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"];\n";
439 foreach my $b (@{$a->{'Entries'}})
441 my ($s,$e,$out,$t,$end,$l) = @{$a->{$b}};
442 outtable($fh,$t) unless $t->{'Done'};
444 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"] = {\n";
445 foreach my $b (@{$a->{'Entries'}})
447 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
454 printf $fh outstring($fh,'',$out);
460 print $fh ",",$t->{Cname};
461 printf $fh ",0x%02x,0x%02x,$l,$end},\n",$sc,$ec;
468 my ($fh,$name,$a) = @_;
476 my ($fh,$name,$a) = @_;
477 foreach my $b (sort keys %$a)
479 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
492 my ($fh,$a,$t,$pre) = @_;
493 # warn sprintf("Page %x\n",$pre);
494 foreach my $b (sort keys %$t)
496 my ($s,$e,$out,$n,$end,$l,$fb) = @{$t->{$b}};
497 die "oops $s $e" unless $s eq $e;
499 if ($n != $a && $n != $t)
501 output_ucm_page($fh,$a,$n,(($pre|($u &0x3F)) << 6)&0xFFFF);
507 $u = $pre|($u &0x3f);
509 printf $fh "<U%04X> ",$u;
510 foreach my $c (split(//,$out))
512 printf $fh "\\x%02X",ord($c);
514 printf $fh " |%d\n",($fb ? 1 : 0);
518 warn join(',',@{$t->{$b}},$a,$t);
525 my ($fh,$name,$a) = @_;
526 print $fh "CHARMAP\n";
527 output_ucm_page($fh,$a,$a,0);
528 print $fh "END CHARMAP\n";