2 BEGIN { @INC = '../../lib' };
9 # UTF-8 encode long hand - only covers part of perl's range
17 return chr(($uv >> 6) | 0xC0).
18 chr(($uv & 0x3F) | 0x80);
20 return chr(($uv >> 12) | 0xE0).
21 chr((($uv >> 6) & 0x3F) | 0x80).
22 chr(($uv & 0x3F) | 0x80);
34 # encode double byte MS byte first
36 return chr($page).chr($ch);
41 # encode Multi-byte - single for 0..255 otherwise double
43 return &encode_D if $page;
47 # Win32 does not expand globs on command line
48 eval "\@ARGV = map(glob(\$_),\@ARGV)" if ($^O eq 'MSWin32');
51 getopts('qo:F:',\%opt);
52 my $cname = (exists $opt{'o'}) ? $opt{'o'} : shift(@ARGV);
53 chmod(0666,$cname) if -f $cname && !-w $cname;
54 open(C,">$cname") || die "Cannot open $cname:$!";
58 $dname =~ s/(\.[^\.]*)?$/.def/;
60 my ($doC,$doEnc,$doUcm);
62 if ($cname =~ /\.(c|xs)$/)
65 chmod(0666,$dname) if -f $cname && !-w $dname;
66 open(D,">$dname") || die "Cannot open $dname:$!";
68 $hname =~ s/(\.[^\.]*)?$/.h/;
69 chmod(0666,$hname) if -f $cname && !-w $hname;
70 open(H,">$hname") || die "Cannot open $hname:$!";
72 foreach my $fh (\*C,\*D,\*H)
74 print $fh <<"END" unless $opt{'q'};
76 !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
77 This file was autogenerated by:
78 $^X $0 $cname @orig_ARGV
83 if ($cname =~ /(\w+)\.xs$/)
85 print C "#include <EXTERN.h>\n";
86 print C "#include <perl.h>\n";
87 print C "#include <XSUB.h>\n";
88 print C "#define U8 U8\n";
90 print C "#include \"encode.h\"\n";
92 elsif ($cname =~ /\.enc$/)
96 elsif ($cname =~ /\.ucm$/)
102 if (exists $opt{'F'})
104 # -F is followed by name of file containing list of filenames
105 my $flist = $opt{'F'};
106 open(FLIST,$flist) || die "Cannot open $flist:$!";
107 chomp(@encfiles = <FLIST>);
120 if ($a =~ /^.*-(\d+)/)
123 if ($b =~ /^.*-(\d+)/)
132 foreach my $enc (sort cmp_name @encfiles)
134 my ($name,$sfx) = $enc =~ /^.*?([\w-]+)\.(enc|ucm)$/;
139 compile_enc(\*E,lc($name),\*C);
143 compile_ucm(\*E,lc($name),\*C);
148 warn "Cannot open $enc for $name:$!";
154 foreach my $enc (sort cmp_name keys %encoding)
156 my $sym = "${enc}_encoding";
158 print C "encode_t $sym = \n";
159 print C " {",join(',',"\"$enc\"",@{$encoding{$enc}}),"};\n\n";
162 foreach my $enc (sort cmp_name keys %encoding)
164 my $sym = "${enc}_encoding";
166 print H "extern encode_t $sym;\n";
167 print D " Encode_Define(aTHX_ &$sym);\n";
170 if ($cname =~ /(\w+)\.xs$/)
173 print C "\nMODULE = Encode::$mod\tPACKAGE = Encode::$mod\n\n";
174 print C "BOOT:\n{\n";
175 print C "#include \"$dname\"\n";
186 my ($fh,$name,$ch) = @_;
194 last if /^\s*CHARMAP\s*$/i;
195 if (/^\s*<(\w+)>\s+"?([^"]*)"?\s*$/i)
200 if (!defined($cs = $attr{'code_set_name'}))
202 warn "No <code_set_name> in $name\n";
212 if (exists $attr{'subchar'})
215 $attr{'subchar'} =~ /^\s*/cg;
216 push(@byte,$1) while $attr{'subchar'} =~ /\G\\x([0-9a-f]+)/icg;
217 $erep = join('',map(chr(hex($_)),@byte));
219 warn "Scanning $name ($cs)\n";
225 last if /^\s*END\s+CHARMAP\s*$/i;
229 $u = $1 if (/^<U([0-9a-f]+)>\s+/igc);
230 push(@byte,$1) while /\G\\x([0-9a-f]+)/igc;
231 $fb = $1 if /\G\s*(\|[0-3])/gc;
232 # warn "$_: $u @byte | $fb\n";
233 die "Bad line:$_" unless /\G\s*(#.*)?$/gc;
236 my $uch = encode_U(hex($u));
237 my $ech = join('',map(chr(hex($_)),@byte));
238 my $el = length($ech);
239 $max_el = $el if (!defined($max_el) || $el > $max_el);
240 $min_el = $el if (!defined($min_el) || $el < $min_el);
251 # $fb is fallback flag
252 # 0 - round trip safe
253 # 1 - fallback for unicode -> enc
254 # 2 - skip sub-char mapping
255 # 3 - fallback enc -> unicode
256 enter($u2e,$uch,$ech,$u2e,$fb+0) if ($fb =~ /[01]/);
257 enter($e2u,$ech,$uch,$e2u,$fb+0) if ($fb =~ /[03]/);
266 die "$nfb entries without fallback, $hfb entries with\n";
270 output($ch,$name.'_utf8',$e2u);
271 output($ch,'utf8_'.$name,$u2e);
272 $encoding{$name} = [$e2u->{Cname},$u2e->{Cname},
273 outstring($ch,$e2u->{Cname}.'_def',$erep),length($erep)];
277 output_enc($ch,$name,$e2u);
281 output_ucm($ch,$name,$u2e,$erep,$min_el,$max_el);
287 my ($fh,$name,$ch) = @_;
292 while ($type = <$fh>)
294 last if $type !~ /^\s*#/;
297 return if $type eq 'E';
298 my ($def,$sym,$pages) = split(/\s+/,scalar(<$fh>));
299 warn "$type encoded $name\n";
306 $rep = &{"encode_$type"}($v & 0xFF, ($v >> 8) & 0xffe);
312 my $page = hex($line);
314 for (my $i = 0; $i < 16; $i++)
317 for (my $j = 0; $j < 16; $j++)
320 my $ech = &{"encode_$type"}($ch,$page);
321 my $val = hex(substr($line,0,4,''));
322 if ($val || (!$ch && !$page))
324 my $el = length($ech);
325 $max_el = $el if (!defined($max_el) || $el > $max_el);
326 $min_el = $el if (!defined($min_el) || $el < $min_el);
327 my $uch = encode_U($val);
328 enter($e2u,$ech,$uch,$e2u,0);
329 enter($u2e,$uch,$ech,$u2e,0);
333 # No character at this position
334 # enter($e2u,$ech,undef,$e2u);
342 output($ch,$name.'_utf8',$e2u);
343 output($ch,'utf8_'.$name,$u2e);
344 $encoding{$name} = [$e2u->{Cname},$u2e->{Cname},
345 outstring($ch,$e2u->{Cname}.'_def',$rep),length($rep)];
349 output_enc($ch,$name,$e2u);
353 output_ucm($ch,$name,$u2e,$rep,$min_el,$max_el);
359 my ($a,$s,$d,$t,$fb) = @_;
361 my $b = substr($s,0,1);
365 $e = [$b,$b,'',{},length($s),0,$fb];
370 enter($e->[3],substr($s,1),$d,$t,$fb);
376 $e->[5] = length($d);
382 my ($fh,$name,$s) = @_;
383 my $sym = $strings{$s};
386 foreach my $o (keys %strings)
388 my $i = index($o,$s);
392 $sym .= sprintf("+0x%02x",$i) if ($i);
396 $strings{$s} = $sym = $name;
397 printf $fh "\nstatic const U8 %s[%d] =\n",$name,length($s);
398 # Do in chunks of 16 chars to constrain line length
399 # Assumes ANSI C adjacent string litteral concatenation
402 my $c = substr($s,0,16,'');
403 print $fh '"',join('',map(sprintf('\x%02x',ord($_)),split(//,$c))),'"';
404 print $fh "\n" if length($s);
416 my @keys = grep(ref($a->{$_}),sort keys %$a);
419 foreach my $b (@keys)
421 my ($s,$f,$out,$t,$end) = @{$a->{$b}};
423 ord($b) == ord($a->{$l}[1])+1 &&
424 $a->{$l}[3] == $a->{$b}[3] &&
425 $a->{$l}[4] == $a->{$b}[4] &&
426 $a->{$l}[5] == $a->{$b}[5] &&
427 $a->{$l}[6] == $a->{$b}[6]
428 # && length($a->{$l}[2]) < 16
431 my $i = ord($b)-ord($a->{$l}[0]);
433 $a->{$l}[2] .= $a->{$b}[2];
440 if (exists $t->{Cname})
442 $t->{'Forward'} = 1 if $t != $a;
446 process(sprintf("%s_%02x",$name,ord($s)),$t);
449 if (ord($keys[-1]) < 255)
451 my $t = chr(ord($keys[-1])+1);
452 $a->{$t} = [$t,chr(255),undef,$a,0,0];
455 $a->{'Entries'} = \@ent;
461 my $name = $a->{'Cname'};
463 foreach my $b (@{$a->{'Entries'}})
465 next unless $a->{$b}[5];
466 my $s = ord($a->{$b}[0]);
467 my $e = ord($a->{$b}[1]);
468 outstring($fh,sprintf("%s__%02x_%02x",$name,$s,$e),$a->{$b}[2]);
472 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"];\n";
475 foreach my $b (@{$a->{'Entries'}})
477 my ($s,$e,$out,$t,$end,$l) = @{$a->{$b}};
478 outtable($fh,$t) unless $t->{'Done'};
480 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"] = {\n";
481 foreach my $b (@{$a->{'Entries'}})
483 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
490 printf $fh outstring($fh,'',$out);
496 print $fh ",",$t->{Cname};
497 printf $fh ",0x%02x,0x%02x,$l,$end},\n",$sc,$ec;
504 my ($fh,$name,$a) = @_;
512 my ($fh,$name,$a) = @_;
513 foreach my $b (sort keys %$a)
515 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
526 my ($fh,$a,$t,$pre) = @_;
527 # warn sprintf("Page %x\n",$pre);
528 foreach my $b (sort keys %$t)
530 my ($s,$e,$out,$n,$end,$l,$fb) = @{$t->{$b}};
531 die "oops $s $e" unless $s eq $e;
533 if ($n != $a && $n != $t)
535 output_ucm_page($fh,$a,$n,(($pre|($u &0x3F)) << 6)&0xFFFF);
541 $u = $pre|($u &0x3f);
543 printf $fh "<U%04X> ",$u;
544 foreach my $c (split(//,$out))
546 printf $fh "\\x%02X",ord($c);
548 printf $fh " |%d\n",($fb ? 1 : 0);
552 warn join(',',@{$t->{$b}},$a,$t);
559 my ($fh,$name,$a,$rep,$min_el,$max_el) = @_;
560 print $fh "# Written by $0 @orig_ARGV\n" unless $opt{'q'};
561 print $fh "<code_set_name> \"$name\"\n";
564 print $fh "<mb_cur_min> $min_el\n";
568 print $fh "<mb_cur_max> $max_el\n";
572 print $fh "<subchar> ";
573 foreach my $c (split(//,$rep))
575 printf $fh "\\x%02X",ord($c);
579 print $fh "#\nCHARMAP\n";
580 output_ucm_page($fh,$a,$a,0);
581 print $fh "END CHARMAP\n";