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$/)
95 # 2nd argument is file containing list of filenames
96 my $flist = shift(@ARGV);
97 open(FLIST,$flist) || die "Cannot open $flist:$!";
98 chomp(my @encfiles = <FLIST>);
106 if ($a =~ /^.*-(\d+)/)
109 if ($b =~ /^.*-(\d+)/)
118 foreach my $enc (sort cmp_name @encfiles)
120 my ($name,$sfx) = $enc =~ /^.*?([\w-]+)\.(enc|ucm)$/;
125 compile_enc(\*E,lc($name),\*C);
129 compile_ucm(\*E,lc($name),\*C);
134 warn "Cannot open $enc for $name:$!";
140 foreach my $enc (sort cmp_name keys %encoding)
142 my $sym = "${enc}_encoding";
144 print C "encode_t $sym = \n";
145 print C " {",join(',',"\"$enc\"",@{$encoding{$enc}}),"};\n\n";
148 foreach my $enc (sort cmp_name keys %encoding)
150 my $sym = "${enc}_encoding";
152 print H "extern encode_t $sym;\n";
153 print D " Encode_Define(aTHX_ &$sym);\n";
156 if ($cname =~ /(\w+)\.xs$/)
159 print C "\nMODULE = Encode::$mod\tPACKAGE = Encode::$mod\n\n";
160 print C "BOOT:\n{\n";
161 print C "#include \"$dname\"\n";
172 my ($fh,$name,$ch) = @_;
180 last if /^\s*CHARMAP\s*$/i;
181 if (/^\s*<(\w+)>\s+"?([^"]*)"?\s*$/i)
186 if (!defined($cs = $attr{'code_set_name'}))
188 warn "No <code_set_name> in $name\n";
196 if (exists $attr{'subchar'})
198 my @byte = $attr{'subchar'} =~ /^\s*(?:\\x([0-9a-f]+))+\s*$/;
199 $erep = join('',map(hex($_),@byte));
201 warn "Scanning $name ($cs)\n";
207 last if /^\s*END\s+CHARMAP\s*$/i;
209 my ($u,@byte) = /^<U([0-9a-f]+)>\s+(?:\\x([0-9a-f]+))+\s*(\|[0-3]|)\s*$/i;
213 my $uch = encode_U(hex($u));
214 my $ech = join('',map(chr(hex($_)),@byte));
225 # $fb is fallback flag
226 # 0 - round trip safe
227 # 1 - fallback for unicode -> enc
228 # 2 - skip sub-char mapping
229 # 3 - fallback enc -> unicode
230 enter($u2e,$uch,$ech,$u2e,$fb+0) if ($fb =~ /[01]/);
231 enter($e2u,$ech,$uch,$e2u,$fb+0) if ($fb =~ /[03]/);
241 die "$nfb entries without fallback, $hfb entries with\n";
245 output($ch,$name.'_utf8',$e2u);
246 output($ch,'utf8_'.$name,$u2e);
247 $encoding{$name} = [$e2u->{Cname},$u2e->{Cname},
248 outstring($ch,$e2u->{Cname}.'_def',$erep),length($erep)];
252 output_enc($ch,$name,$e2u);
256 output_ucm($ch,$name,$u2e);
262 my ($fh,$name,$ch) = @_;
267 while ($type = <$fh>)
269 last if $type !~ /^\s*#/;
272 return if $type eq 'E';
273 my ($def,$sym,$pages) = split(/\s+/,scalar(<$fh>));
274 warn "$type encoded $name\n";
279 $rep = &{"encode_$type"}($v & 0xFF, ($v >> 8) & 0xffe);
285 my $page = hex($line);
287 for (my $i = 0; $i < 16; $i++)
290 for (my $j = 0; $j < 16; $j++)
293 my $ech = &{"encode_$type"}($ch,$page);
294 my $val = hex(substr($line,0,4,''));
295 if ($val || (!$ch && !$page))
297 my $uch = encode_U($val);
298 enter($e2u,$ech,$uch,$e2u,0);
299 enter($u2e,$uch,$ech,$u2e,0);
303 # No character at this position
304 # enter($e2u,$ech,undef,$e2u);
312 output($ch,$name.'_utf8',$e2u);
313 output($ch,'utf8_'.$name,$u2e);
314 $encoding{$name} = [$e2u->{Cname},$u2e->{Cname},
315 outstring($ch,$e2u->{Cname}.'_def',$rep),length($rep)];
319 output_enc($ch,$name,$e2u);
323 output_ucm($ch,$name,$u2e);
329 my ($a,$s,$d,$t,$fb) = @_;
331 my $b = substr($s,0,1);
335 $e = [$b,$b,'',{},length($s),0,$fb];
340 enter($e->[3],substr($s,1),$d,$t,$fb);
346 $e->[5] = length($d);
352 my ($fh,$name,$s) = @_;
353 my $sym = $strings{$s};
356 foreach my $o (keys %strings)
358 my $i = index($o,$s);
362 $sym .= sprintf("+0x%02x",$i) if ($i);
366 $strings{$s} = $sym = $name;
367 printf $fh "\nstatic const U8 %s[%d] =\n",$name,length($s);
368 # Do in chunks of 16 chars to constrain line length
369 # Assumes ANSI C adjacent string litteral concatenation
372 my $c = substr($s,0,16,'');
373 print $fh '"',join('',map(sprintf('\x%02x',ord($_)),split(//,$c))),'"';
374 print $fh "\n" if length($s);
386 my @keys = grep(ref($a->{$_}),sort keys %$a);
389 foreach my $b (@keys)
391 my ($s,$f,$out,$t,$end) = @{$a->{$b}};
393 ord($b) == ord($a->{$l}[1])+1 &&
394 $a->{$l}[3] == $a->{$b}[3] &&
395 $a->{$l}[4] == $a->{$b}[4] &&
396 $a->{$l}[5] == $a->{$b}[5] &&
397 $a->{$l}[6] == $a->{$b}[6]
398 # && length($a->{$l}[2]) < 16
401 my $i = ord($b)-ord($a->{$l}[0]);
403 $a->{$l}[2] .= $a->{$b}[2];
410 if (exists $t->{Cname})
412 $t->{'Forward'} = 1 if $t != $a;
416 process(sprintf("%s_%02x",$name,ord($s)),$t);
419 if (ord($keys[-1]) < 255)
421 my $t = chr(ord($keys[-1])+1);
422 $a->{$t} = [$t,chr(255),undef,$a,0,0];
425 $a->{'Entries'} = \@ent;
431 my $name = $a->{'Cname'};
433 foreach my $b (@{$a->{'Entries'}})
435 next unless $a->{$b}[5];
436 my $s = ord($a->{$b}[0]);
437 my $e = ord($a->{$b}[1]);
438 outstring($fh,sprintf("%s__%02x_%02x",$name,$s,$e),$a->{$b}[2]);
442 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"];\n";
445 foreach my $b (@{$a->{'Entries'}})
447 my ($s,$e,$out,$t,$end,$l) = @{$a->{$b}};
448 outtable($fh,$t) unless $t->{'Done'};
450 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"] = {\n";
451 foreach my $b (@{$a->{'Entries'}})
453 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
460 printf $fh outstring($fh,'',$out);
466 print $fh ",",$t->{Cname};
467 printf $fh ",0x%02x,0x%02x,$l,$end},\n",$sc,$ec;
474 my ($fh,$name,$a) = @_;
482 my ($fh,$name,$a) = @_;
483 foreach my $b (sort keys %$a)
485 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
498 my ($fh,$a,$t,$pre) = @_;
499 # warn sprintf("Page %x\n",$pre);
500 foreach my $b (sort keys %$t)
502 my ($s,$e,$out,$n,$end,$l,$fb) = @{$t->{$b}};
503 die "oops $s $e" unless $s eq $e;
505 if ($n != $a && $n != $t)
507 output_ucm_page($fh,$a,$n,(($pre|($u &0x3F)) << 6)&0xFFFF);
513 $u = $pre|($u &0x3f);
515 printf $fh "<U%04X> ",$u;
516 foreach my $c (split(//,$out))
518 printf $fh "\\x%02X",ord($c);
520 printf $fh " |%d\n",($fb ? 1 : 0);
524 warn join(',',@{$t->{$b}},$a,$t);
531 my ($fh,$name,$a) = @_;
532 print $fh "CHARMAP\n";
533 output_ucm_page($fh,$a,$a,0);
534 print $fh "END CHARMAP\n";