4 $ENV{PATH} .= ';../..' if $^O eq 'MSWin32';
14 # UTF-8 encode long hand - only covers part of perl's range
22 return chr(($uv >> 6) | 0xC0).
23 chr(($uv & 0x3F) | 0x80);
25 return chr(($uv >> 12) | 0xE0).
26 chr((($uv >> 6) & 0x3F) | 0x80).
27 chr(($uv & 0x3F) | 0x80);
39 # encode double byte MS byte first
41 return chr($page).chr($ch);
46 # encode Multi-byte - single for 0..255 otherwise double
48 return &encode_D if $page;
52 # Win32 does not expand globs on command line
53 eval "\@ARGV = map(glob(\$_),\@ARGV)" if ($^O eq 'MSWin32');
56 getopts('qo:f:n:',\%opt);
57 my $cname = (exists $opt{'o'}) ? $opt{'o'} : shift(@ARGV);
58 chmod(0666,$cname) if -f $cname && !-w $cname;
59 open(C,">$cname") || die "Cannot open $cname:$!";
63 $dname =~ s/(\.[^\.]*)?$/.def/;
65 my ($doC,$doEnc,$doUcm,$doPet);
67 if ($cname =~ /\.(c|xs)$/)
70 chmod(0666,$dname) if -f $cname && !-w $dname;
71 open(D,">$dname") || die "Cannot open $dname:$!";
73 $hname =~ s/(\.[^\.]*)?$/.h/;
74 chmod(0666,$hname) if -f $cname && !-w $hname;
75 open(H,">$hname") || die "Cannot open $hname:$!";
77 foreach my $fh (\*C,\*D,\*H)
79 print $fh <<"END" unless $opt{'q'};
81 !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
82 This file was autogenerated by:
83 $^X $0 $cname @orig_ARGV
84 (Repository $perforce)
89 if ($cname =~ /(\w+)\.xs$/)
91 print C "#include <EXTERN.h>\n";
92 print C "#include <perl.h>\n";
93 print C "#include <XSUB.h>\n";
94 print C "#define U8 U8\n";
96 print C "#include \"encode.h\"\n";
98 elsif ($cname =~ /\.enc$/)
102 elsif ($cname =~ /\.ucm$/)
106 elsif ($cname =~ /\.pet$/)
112 if (exists $opt{'f'})
114 # -F is followed by name of file containing list of filenames
115 my $flist = $opt{'f'};
116 open(FLIST,$flist) || die "Cannot open $flist:$!";
117 chomp(@encfiles = <FLIST>);
130 if ($a =~ /^.*-(\d+)/)
133 if ($b =~ /^.*-(\d+)/)
143 foreach my $enc (sort cmp_name @encfiles)
145 my ($name,$sfx) = $enc =~ /^.*?([\w-]+)\.(enc|ucm)$/;
146 $name = delete $opt{'n'} if exists $opt{'n'};
151 compile_enc(\*E,lc($name));
155 compile_ucm(\*E,lc($name));
160 warn "Cannot open $enc for $name:$!";
166 foreach my $name (sort cmp_name keys %encoding)
168 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
169 output(\*C,$name.'_utf8',$e2u);
170 output(\*C,'utf8_'.$name,$u2e);
171 push(@{$encoding{$name}},outstring(\*C,$e2u->{Cname}.'_def',$erep));
173 foreach my $enc (sort cmp_name keys %encoding)
175 my ($e2u,$u2e,$rep,$min_el,$max_el,$rsym) = @{$encoding{$enc}};
176 my @info = ($e2u->{Cname},$u2e->{Cname},$rsym,length($rep),$min_el,$max_el);
177 my $sym = "${enc}_encoding";
179 print C "encode_t $sym = \n";
180 print C " {",join(',',@info,"{\"$enc\",(const char *)0}"),"};\n\n";
183 foreach my $enc (sort cmp_name keys %encoding)
185 my $sym = "${enc}_encoding";
187 print H "extern encode_t $sym;\n";
188 print D " Encode_Define(aTHX_ &$sym);\n";
191 if ($cname =~ /(\w+)\.xs$/)
194 print C "\nMODULE = Encode::$mod\tPACKAGE = Encode::$mod\n\n";
195 print C "BOOT:\n{\n";
196 print C "#include \"$dname\"\n";
204 foreach my $name (sort cmp_name keys %encoding)
206 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
207 output_enc(\*C,$name,$e2u);
212 foreach my $name (sort cmp_name keys %encoding)
214 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
215 output_ucm(\*C,$name,$u2e,$erep,$min_el,$max_el);
232 last if /^\s*CHARMAP\s*$/i;
233 if (/^\s*<(\w+)>\s+"?([^"]*)"?\s*$/i)
238 if (!defined($cs = $attr{'code_set_name'}))
240 warn "No <code_set_name> in $name\n";
250 if (exists $attr{'subchar'})
253 $attr{'subchar'} =~ /^\s*/cg;
254 push(@byte,$1) while $attr{'subchar'} =~ /\G\\x([0-9a-f]+)/icg;
255 $erep = join('',map(chr(hex($_)),@byte));
257 print "Scanning $name ($cs)\n";
263 last if /^\s*END\s+CHARMAP\s*$/i;
267 $u = $1 if (/^<U([0-9a-f]+)>\s+/igc);
268 push(@byte,$1) while /\G\\x([0-9a-f]+)/igc;
269 $fb = $1 if /\G\s*(\|[0-3])/gc;
270 # warn "$_: $u @byte | $fb\n";
271 die "Bad line:$_" unless /\G\s*(#.*)?$/gc;
274 my $uch = encode_U(hex($u));
275 my $ech = join('',map(chr(hex($_)),@byte));
276 my $el = length($ech);
277 $max_el = $el if (!defined($max_el) || $el > $max_el);
278 $min_el = $el if (!defined($min_el) || $el < $min_el);
289 # $fb is fallback flag
290 # 0 - round trip safe
291 # 1 - fallback for unicode -> enc
292 # 2 - skip sub-char mapping
293 # 3 - fallback enc -> unicode
294 enter($u2e,$uch,$ech,$u2e,$fb+0) if ($fb =~ /[01]/);
295 enter($e2u,$ech,$uch,$e2u,$fb+0) if ($fb =~ /[03]/);
304 die "$nfb entries without fallback, $hfb entries with\n";
306 $encoding{$name} = [$e2u,$u2e,$erep,$min_el,$max_el];
316 while ($type = <$fh>)
318 last if $type !~ /^\s*#/;
321 return if $type eq 'E';
322 my ($def,$sym,$pages) = split(/\s+/,scalar(<$fh>));
323 warn "$type encoded $name\n";
330 $rep = &{"encode_$type"}($v & 0xFF, ($v >> 8) & 0xffe);
337 my $page = hex($line);
339 for (my $i = 0; $i < 16; $i++)
342 for (my $j = 0; $j < 16; $j++)
345 my $ech = &{"encode_$type"}($ch,$page);
346 my $val = hex(substr($line,0,4,''));
347 next if $val == 0xFFFD;
348 if ($val || (!$ch && !$page))
350 my $el = length($ech);
351 $max_el = $el if (!defined($max_el) || $el > $max_el);
352 $min_el = $el if (!defined($min_el) || $el < $min_el);
353 my $uch = encode_U($val);
354 if (exists $seen{$uch})
356 warn sprintf("U%04X is %02X%02X and %02X%02X\n",
357 $val,$page,$ch,@{$seen{$uch}});
361 $seen{$uch} = [$page,$ch];
363 enter($e2u,$ech,$uch,$e2u,0);
364 enter($u2e,$uch,$ech,$u2e,0);
368 # No character at this position
369 # enter($e2u,$ech,undef,$e2u);
375 $encoding{$name} = [$e2u,$u2e,$rep,$min_el,$max_el];
380 my ($a,$s,$d,$t,$fb) = @_;
382 my $b = substr($s,0,1);
386 $e = [$b,$b,'',{},length($s),0,$fb];
391 enter($e->[3],substr($s,1),$d,$t,$fb);
397 $e->[5] = length($d);
403 my ($fh,$name,$s) = @_;
404 my $sym = $strings{$s};
407 foreach my $o (keys %strings)
409 my $i = index($o,$s);
413 $sym .= sprintf("+0x%02x",$i) if ($i);
417 $strings{$s} = $sym = $name;
418 printf $fh "\nstatic const U8 %s[%d] =\n",$name,length($s);
419 # Do in chunks of 16 chars to constrain line length
420 # Assumes ANSI C adjacent string litteral concatenation
423 my $c = substr($s,0,16,'');
424 print $fh '"',join('',map(sprintf('\x%02x',ord($_)),split(//,$c))),'"';
425 print $fh "\n" if length($s);
437 my @keys = grep(ref($a->{$_}),sort keys %$a);
440 foreach my $b (@keys)
442 my ($s,$f,$out,$t,$end) = @{$a->{$b}};
444 ord($b) == ord($a->{$l}[1])+1 &&
445 $a->{$l}[3] == $a->{$b}[3] &&
446 $a->{$l}[4] == $a->{$b}[4] &&
447 $a->{$l}[5] == $a->{$b}[5] &&
448 $a->{$l}[6] == $a->{$b}[6]
449 # && length($a->{$l}[2]) < 16
452 my $i = ord($b)-ord($a->{$l}[0]);
454 $a->{$l}[2] .= $a->{$b}[2];
461 if (exists $t->{Cname})
463 $t->{'Forward'} = 1 if $t != $a;
467 process(sprintf("%s_%02x",$name,ord($s)),$t);
470 if (ord($keys[-1]) < 255)
472 my $t = chr(ord($keys[-1])+1);
473 $a->{$t} = [$t,chr(255),undef,$a,0,0];
476 $a->{'Entries'} = \@ent;
482 my $name = $a->{'Cname'};
484 foreach my $b (@{$a->{'Entries'}})
486 next unless $a->{$b}[5];
487 my $s = ord($a->{$b}[0]);
488 my $e = ord($a->{$b}[1]);
489 outstring($fh,sprintf("%s__%02x_%02x",$name,$s,$e),$a->{$b}[2]);
493 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"];\n";
496 foreach my $b (@{$a->{'Entries'}})
498 my ($s,$e,$out,$t,$end,$l) = @{$a->{$b}};
499 outtable($fh,$t) unless $t->{'Done'};
501 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"] = {\n";
502 foreach my $b (@{$a->{'Entries'}})
504 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
511 printf $fh outstring($fh,'',$out);
517 print $fh ",",$t->{Cname};
518 printf $fh ",0x%02x,0x%02x,$l,$end},\n",$sc,$ec;
525 my ($fh,$name,$a) = @_;
533 my ($fh,$name,$a) = @_;
534 foreach my $b (sort keys %$a)
536 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
548 my $s = do "unicode/Name.pl";
550 while ($s =~ /\G([0-9a-f]+)\t([0-9a-f]*)\t(.*?)\s*\n/igc)
554 last if $s >= 0x10000;
555 my $e = length($2) ? hex($2) : $s;
556 for (my $i = $s; $i <= $e; $i++)
559 # print sprintf("U%04X $name\n",$i);
566 my ($cmap,$a,$t,$pre) = @_;
567 # warn sprintf("Page %x\n",$pre);
568 foreach my $b (sort keys %$t)
570 my ($s,$e,$out,$n,$end,$l,$fb) = @{$t->{$b}};
571 die "oops $s $e" unless $s eq $e;
573 if ($n != $a && $n != $t)
575 output_ucm_page($cmap,$a,$n,(($pre|($u &0x3F)) << 6)&0xFFFF);
581 $u = $pre|($u &0x3f);
583 my $s = sprintf "<U%04X> ",$u;
584 foreach my $c (split(//,$out))
586 $s .= sprintf "\\x%02X",ord($c);
588 $s .= sprintf " |%d # %s\n",($fb ? 1 : 0),$uname[$u];
593 warn join(',',@{$t->{$b}},$a,$t);
600 my ($fh,$name,$h,$rep,$min_el,$max_el) = @_;
601 print $fh "# Written $perforce\n# $0 @orig_ARGV\n" unless $opt{'q'};
602 print $fh "<code_set_name> \"$name\"\n";
606 print $fh "<mb_cur_min> $min_el\n";
610 print $fh "<mb_cur_max> $max_el\n";
614 print $fh "<subchar> ";
615 foreach my $c (split(//,$rep))
617 printf $fh "\\x%02X",ord($c);
622 output_ucm_page(\@cmap,$h,$h,0);
623 print $fh "#\nCHARMAP\n";
624 foreach my $line (sort { substr($a,8) cmp substr($b,8) } @cmap)
628 print $fh "END CHARMAP\n";