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/;
53 chmod(0666,$dname) if -f $cname && !-w $dname;
54 open(D,">$dname") || die "Cannot open $dname:$!";
56 $hname =~ s/(\.[^\.]*)?$/.h/;
57 chmod(0666,$hname) if -f $cname && !-w $hname;
58 open(H,">$hname") || die "Cannot open $hname:$!";
60 foreach my $fh (\*C,\*D,\*H)
64 !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
65 This file was autogenerated by:
71 if ($cname =~ /(\w+)\.xs$/)
73 print C "#include <EXTERN.h>\n";
74 print C "#include <perl.h>\n";
75 print C "#include <XSUB.h>\n";
76 print C "#define U8 U8\n";
78 print C "#include \"encode.h\"\n";
85 if ($a =~ /^.*-(\d+)/)
88 if ($b =~ /^.*-(\d+)/)
97 foreach my $enc (sort cmp_name @ARGV)
99 my ($name,$sfx) = $enc =~ /^.*?([\w-]+)\.(enc|ucm)$/;
104 compile_enc(\*E,lc($name),\*C);
108 compile_ucm(\*E,lc($name),\*C);
113 warn "Cannot open $enc for $name:$!";
117 foreach my $enc (sort cmp_name keys %encoding)
119 my $sym = "${enc}_encoding";
121 print C "encode_t $sym = \n";
122 print C " {",join(',',"\"$enc\"",@{$encoding{$enc}}),"};\n\n";
125 foreach my $enc (sort cmp_name keys %encoding)
127 my $sym = "${enc}_encoding";
129 print H "extern encode_t $sym;\n";
130 print D " Encode_Define(aTHX_ &$sym);\n";
133 if ($cname =~ /(\w+)\.xs$/)
136 print C "\nMODULE = Encode::$mod\tPACKAGE = Encode::$mod\n\n";
137 print C "BOOT:\n{\n";
138 print C "#include \"$dname\"\n";
148 my ($fh,$name,$ch) = @_;
156 last if /^\s*CHARMAP\s*$/i;
157 if (/^\s*<(\w+)>\s+"?([^"]*)"?\s*$/i)
162 if (!defined($cs = $attr{'code_set_name'}))
164 warn "No <code_set_name> in $name\n";
172 if (exists $attr{'subchar'})
174 my @byte = $attr{'subchar'} =~ /^\s*(?:\\x([0-9a-f]+))+\s*$/;
175 $erep = join('',map(hex($_),@byte));
177 warn "Scanning $cs\n";
183 last if /^\s*END\s+CHARMAP\s*$/i;
185 my ($u,@byte) = /^<U([0-9a-f]+)>\s+(?:\\x([0-9a-f]+))+\s*(\|[0-3]|)\s*$/i;
189 my $uch = encode_U(hex($u));
190 my $ech = join('',map(hex($_),@byte));
201 # $fb is fallback flag
202 # 0 - round trip safe
203 # 1 - fallback for unicode -> enc
204 # 2 - skip sub-char mapping
205 # 3 - fallback enc -> unicode
206 enter($u2e,$uch,$ech,$u2e,$fb+0) if ($fb =~ /[01]/);
207 enter($e2u,$ech,$uch,$e2u,$fb+0) if ($fb =~ /[03]/);
217 die "$nfb entries without fallback, $hfb entries with\n";
219 output($ch,$name.'_utf8',$e2u);
220 output($ch,'utf8_'.$name,$u2e);
221 $encoding{$name} = [$e2u->{Cname},$u2e->{Cname},
222 outstring($ch,$e2u->{Cname}.'_def',$erep),length($erep)];
227 my ($fh,$name,$ch) = @_;
232 while ($type = <$fh>)
234 last if $type !~ /^\s*#/;
237 return if $type eq 'E';
238 my ($def,$sym,$pages) = split(/\s+/,scalar(<$fh>));
239 warn "$type encoded $name\n";
244 $rep = &{"encode_$type"}($v & 0xFF, ($v >> 8) & 0xffe);
250 my $page = hex($line);
252 for (my $i = 0; $i < 16; $i++)
255 for (my $j = 0; $j < 16; $j++)
258 my $ech = &{"encode_$type"}($ch,$page);
259 my $val = hex(substr($line,0,4,''));
260 if ($val || (!$ch && !$page))
262 my $uch = encode_U($val);
263 enter($e2u,$ech,$uch,$e2u,0);
264 enter($u2e,$uch,$ech,$u2e,0);
268 # No character at this position
269 # enter($e2u,$ech,undef,$e2u);
275 output($ch,$name.'_utf8',$e2u);
276 output($ch,'utf8_'.$name,$u2e);
277 $encoding{$name} = [$e2u->{Cname},$u2e->{Cname},
278 outstring($ch,$e2u->{Cname}.'_def',$rep),length($rep)];
283 my ($a,$s,$d,$t,$fb) = @_;
285 my $b = substr($s,0,1);
289 $e = [$b,$b,'',{},length($s),0,$fb];
294 enter($e->[3],substr($s,1),$d,$t,$fb);
300 $e->[5] = length($d);
306 my ($fh,$name,$s) = @_;
307 my $sym = $strings{$s};
310 foreach my $o (keys %strings)
312 my $i = index($o,$s);
316 $sym .= sprintf("+0x%02x",$i) if ($i);
320 $strings{$s} = $sym = $name;
321 printf $fh "\nstatic const U8 %s[%d] =\n",$name,length($s);
322 # Do in chunks of 16 chars to constrain line length
323 # Assumes ANSI C adjacent string litteral concatenation
326 my $c = substr($s,0,16,'');
327 print $fh '"',join('',map(sprintf('\x%02x',ord($_)),split(//,$c))),'"';
328 print $fh "\n" if length($s);
340 my @keys = grep(ref($a->{$_}),sort keys %$a);
343 foreach my $b (@keys)
345 my ($s,$f,$out,$t,$end) = @{$a->{$b}};
347 ord($b) == ord($a->{$l}[1])+1 &&
348 $a->{$l}[3] == $a->{$b}[3] &&
349 $a->{$l}[4] == $a->{$b}[4] &&
350 $a->{$l}[5] == $a->{$b}[5] &&
351 $a->{$l}[6] == $a->{$b}[6]
352 # && length($a->{$l}[2]) < 16
355 my $i = ord($b)-ord($a->{$l}[0]);
357 $a->{$l}[2] .= $a->{$b}[2];
364 if (exists $t->{Cname})
366 $t->{'Forward'} = 1 if $t != $a;
370 process(sprintf("%s_%02x",$name,ord($s)),$t);
373 if (ord($keys[-1]) < 255)
375 my $t = chr(ord($keys[-1])+1);
376 $a->{$t} = [$t,chr(255),undef,$a,0,0];
379 $a->{'Entries'} = \@ent;
385 my $name = $a->{'Cname'};
387 foreach my $b (@{$a->{'Entries'}})
389 next unless $a->{$b}[5];
390 my $s = ord($a->{$b}[0]);
391 my $e = ord($a->{$b}[1]);
392 outstring($fh,sprintf("%s__%02x_%02x",$name,$s,$e),$a->{$b}[2]);
396 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"];\n";
399 foreach my $b (@{$a->{'Entries'}})
401 my ($s,$e,$out,$t,$end,$l) = @{$a->{$b}};
402 outtable($fh,$t) unless $t->{'Done'};
404 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"] = {\n";
405 foreach my $b (@{$a->{'Entries'}})
407 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
413 printf $fh outstring($fh,'',$out);
419 print $fh ",",$t->{Cname};
420 printf $fh ",0x%02x,0x%02x,$l,$end},\n",$sc,$ec;
427 my ($fh,$name,$a) = @_;