2 BEGIN { @INC = '../../lib' };
11 # UTF-8 encode long hand - only covers part of perl's range
19 return chr(($uv >> 6) | 0xC0).
20 chr(($uv & 0x3F) | 0x80);
22 return chr(($uv >> 12) | 0xE0).
23 chr((($uv >> 6) & 0x3F) | 0x80).
24 chr(($uv & 0x3F) | 0x80);
36 # encode double byte MS byte first
38 return chr($page).chr($ch);
43 # encode Multi-byte - single for 0..255 otherwise double
45 return &encode_D if $page;
49 # Win32 does not expand globs on command line
50 eval "\@ARGV = map(glob(\$_),\@ARGV)" if ($^O eq 'MSWin32');
53 getopts('qo:f:n:',\%opt);
54 my $cname = (exists $opt{'o'}) ? $opt{'o'} : shift(@ARGV);
55 chmod(0666,$cname) if -f $cname && !-w $cname;
56 open(C,">$cname") || die "Cannot open $cname:$!";
60 $dname =~ s/(\.[^\.]*)?$/.def/;
62 my ($doC,$doEnc,$doUcm,$doPet);
64 if ($cname =~ /\.(c|xs)$/)
67 chmod(0666,$dname) if -f $cname && !-w $dname;
68 open(D,">$dname") || die "Cannot open $dname:$!";
70 $hname =~ s/(\.[^\.]*)?$/.h/;
71 chmod(0666,$hname) if -f $cname && !-w $hname;
72 open(H,">$hname") || die "Cannot open $hname:$!";
74 foreach my $fh (\*C,\*D,\*H)
76 print $fh <<"END" unless $opt{'q'};
78 !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
79 This file was autogenerated by:
80 $^X $0 $cname @orig_ARGV
81 (Repository $perforce)
86 if ($cname =~ /(\w+)\.xs$/)
88 print C "#include <EXTERN.h>\n";
89 print C "#include <perl.h>\n";
90 print C "#include <XSUB.h>\n";
91 print C "#define U8 U8\n";
93 print C "#include \"encode.h\"\n";
95 elsif ($cname =~ /\.enc$/)
99 elsif ($cname =~ /\.ucm$/)
103 elsif ($cname =~ /\.pet$/)
109 if (exists $opt{'f'})
111 # -F is followed by name of file containing list of filenames
112 my $flist = $opt{'f'};
113 open(FLIST,$flist) || die "Cannot open $flist:$!";
114 chomp(@encfiles = <FLIST>);
127 if ($a =~ /^.*-(\d+)/)
130 if ($b =~ /^.*-(\d+)/)
140 foreach my $enc (sort cmp_name @encfiles)
142 my ($name,$sfx) = $enc =~ /^.*?([\w-]+)\.(enc|ucm)$/;
143 $name = delete $opt{'n'} if exists $opt{'n'};
148 compile_enc(\*E,lc($name));
152 compile_ucm(\*E,lc($name));
157 warn "Cannot open $enc for $name:$!";
163 foreach my $name (sort cmp_name keys %encoding)
165 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
166 output(\*C,$name.'_utf8',$e2u);
167 output(\*C,'utf8_'.$name,$u2e);
168 push(@{$encoding{$name}},outstring(\*C,$e2u->{Cname}.'_def',$erep));
170 foreach my $enc (sort cmp_name keys %encoding)
172 my ($e2u,$u2e,$rep,$min_el,$max_el,$rsym) = @{$encoding{$enc}};
173 my @info = ($e2u->{Cname},$u2e->{Cname},$rsym,length($rep),$min_el,$max_el);
174 my $sym = "${enc}_encoding";
176 print C "encode_t $sym = \n";
177 print C " {",join(',',@info,"{\"$enc\",(const char *)0}"),"};\n\n";
180 foreach my $enc (sort cmp_name keys %encoding)
182 my $sym = "${enc}_encoding";
184 print H "extern encode_t $sym;\n";
185 print D " Encode_Define(aTHX_ &$sym);\n";
188 if ($cname =~ /(\w+)\.xs$/)
191 print C "\nMODULE = Encode::$mod\tPACKAGE = Encode::$mod\n\n";
192 print C "BOOT:\n{\n";
193 print C "#include \"$dname\"\n";
201 foreach my $name (sort cmp_name keys %encoding)
203 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
204 output_enc(\*C,$name,$e2u);
209 foreach my $name (sort cmp_name keys %encoding)
211 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
212 output_ucm(\*C,$name,$u2e,$erep,$min_el,$max_el);
229 last if /^\s*CHARMAP\s*$/i;
230 if (/^\s*<(\w+)>\s+"?([^"]*)"?\s*$/i)
235 if (!defined($cs = $attr{'code_set_name'}))
237 warn "No <code_set_name> in $name\n";
247 if (exists $attr{'subchar'})
250 $attr{'subchar'} =~ /^\s*/cg;
251 push(@byte,$1) while $attr{'subchar'} =~ /\G\\x([0-9a-f]+)/icg;
252 $erep = join('',map(chr(hex($_)),@byte));
254 warn "Scanning $name ($cs)\n";
260 last if /^\s*END\s+CHARMAP\s*$/i;
264 $u = $1 if (/^<U([0-9a-f]+)>\s+/igc);
265 push(@byte,$1) while /\G\\x([0-9a-f]+)/igc;
266 $fb = $1 if /\G\s*(\|[0-3])/gc;
267 # warn "$_: $u @byte | $fb\n";
268 die "Bad line:$_" unless /\G\s*(#.*)?$/gc;
271 my $uch = encode_U(hex($u));
272 my $ech = join('',map(chr(hex($_)),@byte));
273 my $el = length($ech);
274 $max_el = $el if (!defined($max_el) || $el > $max_el);
275 $min_el = $el if (!defined($min_el) || $el < $min_el);
286 # $fb is fallback flag
287 # 0 - round trip safe
288 # 1 - fallback for unicode -> enc
289 # 2 - skip sub-char mapping
290 # 3 - fallback enc -> unicode
291 enter($u2e,$uch,$ech,$u2e,$fb+0) if ($fb =~ /[01]/);
292 enter($e2u,$ech,$uch,$e2u,$fb+0) if ($fb =~ /[03]/);
301 die "$nfb entries without fallback, $hfb entries with\n";
303 $encoding{$name} = [$e2u,$u2e,$erep,$min_el,$max_el];
313 while ($type = <$fh>)
315 last if $type !~ /^\s*#/;
318 return if $type eq 'E';
319 my ($def,$sym,$pages) = split(/\s+/,scalar(<$fh>));
320 warn "$type encoded $name\n";
327 $rep = &{"encode_$type"}($v & 0xFF, ($v >> 8) & 0xffe);
334 my $page = hex($line);
336 for (my $i = 0; $i < 16; $i++)
339 for (my $j = 0; $j < 16; $j++)
342 my $ech = &{"encode_$type"}($ch,$page);
343 my $val = hex(substr($line,0,4,''));
344 next if $val == 0xFFFD;
345 if ($val || (!$ch && !$page))
347 my $el = length($ech);
348 $max_el = $el if (!defined($max_el) || $el > $max_el);
349 $min_el = $el if (!defined($min_el) || $el < $min_el);
350 my $uch = encode_U($val);
351 if (exists $seen{$uch})
353 warn sprintf("U%04X is %02X%02X and %02X%02X\n",
354 $val,$page,$ch,@{$seen{$uch}});
358 $seen{$uch} = [$page,$ch];
360 enter($e2u,$ech,$uch,$e2u,0);
361 enter($u2e,$uch,$ech,$u2e,0);
365 # No character at this position
366 # enter($e2u,$ech,undef,$e2u);
372 $encoding{$name} = [$e2u,$u2e,$rep,$min_el,$max_el];
377 my ($a,$s,$d,$t,$fb) = @_;
379 my $b = substr($s,0,1);
383 $e = [$b,$b,'',{},length($s),0,$fb];
388 enter($e->[3],substr($s,1),$d,$t,$fb);
394 $e->[5] = length($d);
400 my ($fh,$name,$s) = @_;
401 my $sym = $strings{$s};
404 foreach my $o (keys %strings)
406 my $i = index($o,$s);
410 $sym .= sprintf("+0x%02x",$i) if ($i);
414 $strings{$s} = $sym = $name;
415 printf $fh "\nstatic const U8 %s[%d] =\n",$name,length($s);
416 # Do in chunks of 16 chars to constrain line length
417 # Assumes ANSI C adjacent string litteral concatenation
420 my $c = substr($s,0,16,'');
421 print $fh '"',join('',map(sprintf('\x%02x',ord($_)),split(//,$c))),'"';
422 print $fh "\n" if length($s);
434 my @keys = grep(ref($a->{$_}),sort keys %$a);
437 foreach my $b (@keys)
439 my ($s,$f,$out,$t,$end) = @{$a->{$b}};
441 ord($b) == ord($a->{$l}[1])+1 &&
442 $a->{$l}[3] == $a->{$b}[3] &&
443 $a->{$l}[4] == $a->{$b}[4] &&
444 $a->{$l}[5] == $a->{$b}[5] &&
445 $a->{$l}[6] == $a->{$b}[6]
446 # && length($a->{$l}[2]) < 16
449 my $i = ord($b)-ord($a->{$l}[0]);
451 $a->{$l}[2] .= $a->{$b}[2];
458 if (exists $t->{Cname})
460 $t->{'Forward'} = 1 if $t != $a;
464 process(sprintf("%s_%02x",$name,ord($s)),$t);
467 if (ord($keys[-1]) < 255)
469 my $t = chr(ord($keys[-1])+1);
470 $a->{$t} = [$t,chr(255),undef,$a,0,0];
473 $a->{'Entries'} = \@ent;
479 my $name = $a->{'Cname'};
481 foreach my $b (@{$a->{'Entries'}})
483 next unless $a->{$b}[5];
484 my $s = ord($a->{$b}[0]);
485 my $e = ord($a->{$b}[1]);
486 outstring($fh,sprintf("%s__%02x_%02x",$name,$s,$e),$a->{$b}[2]);
490 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"];\n";
493 foreach my $b (@{$a->{'Entries'}})
495 my ($s,$e,$out,$t,$end,$l) = @{$a->{$b}};
496 outtable($fh,$t) unless $t->{'Done'};
498 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"] = {\n";
499 foreach my $b (@{$a->{'Entries'}})
501 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
508 printf $fh outstring($fh,'',$out);
514 print $fh ",",$t->{Cname};
515 printf $fh ",0x%02x,0x%02x,$l,$end},\n",$sc,$ec;
522 my ($fh,$name,$a) = @_;
530 my ($fh,$name,$a) = @_;
531 foreach my $b (sort keys %$a)
533 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
544 my ($fh,$a,$t,$pre) = @_;
545 # warn sprintf("Page %x\n",$pre);
546 foreach my $b (sort keys %$t)
548 my ($s,$e,$out,$n,$end,$l,$fb) = @{$t->{$b}};
549 die "oops $s $e" unless $s eq $e;
551 if ($n != $a && $n != $t)
553 output_ucm_page($fh,$a,$n,(($pre|($u &0x3F)) << 6)&0xFFFF);
559 $u = $pre|($u &0x3f);
561 printf $fh "<U%04X> ",$u;
562 foreach my $c (split(//,$out))
564 printf $fh "\\x%02X",ord($c);
566 printf $fh " |%d\n",($fb ? 1 : 0);
570 warn join(',',@{$t->{$b}},$a,$t);
577 my ($fh,$name,$a,$rep,$min_el,$max_el) = @_;
578 print $fh "# Written $perforce\n# $0 @orig_ARGV\n" unless $opt{'q'};
579 print $fh "<code_set_name> \"$name\"\n";
582 print $fh "<mb_cur_min> $min_el\n";
586 print $fh "<mb_cur_max> $max_el\n";
590 print $fh "<subchar> ";
591 foreach my $c (split(//,$rep))
593 printf $fh "\\x%02X",ord($c);
597 print $fh "#\nCHARMAP\n";
598 output_ucm_page($fh,$a,$a,0);
599 print $fh "END CHARMAP\n";