4 $ENV{PATH} .= ';../..' if $^O eq 'MSWin32';
13 # UTF-8 encode long hand - only covers part of perl's range
15 # chr() works in native space so convert value from table
16 # into that space before using chr().
17 my $ch = chr(utf8::unicode_to_native($uv));
18 # Now get core perl to encode that the way it likes.
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');
49 getopts('qo:f:n:',\%opt);
50 my $cname = (exists $opt{'o'}) ? $opt{'o'} : shift(@ARGV);
51 chmod(0666,$cname) if -f $cname && !-w $cname;
52 open(C,">$cname") || die "Cannot open $cname:$!";
56 $dname =~ s/(\.[^\.]*)?$/.def/;
58 my ($doC,$doEnc,$doUcm,$doPet);
60 if ($cname =~ /\.(c|xs)$/)
63 chmod(0666,$dname) if -f $cname && !-w $dname;
64 open(D,">$dname") || die "Cannot open $dname:$!";
66 $hname =~ s/(\.[^\.]*)?$/.h/;
67 chmod(0666,$hname) if -f $cname && !-w $hname;
68 open(H,">$hname") || die "Cannot open $hname:$!";
70 foreach my $fh (\*C,\*D,\*H)
72 print $fh <<"END" unless $opt{'q'};
74 !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
75 This file was autogenerated by:
76 $^X $0 $cname @orig_ARGV
77 (Repository $perforce)
82 if ($cname =~ /(\w+)\.xs$/)
84 print C "#include <EXTERN.h>\n";
85 print C "#include <perl.h>\n";
86 print C "#include <XSUB.h>\n";
87 print C "#define U8 U8\n";
89 print C "#include \"encode.h\"\n";
91 elsif ($cname =~ /\.enc$/)
95 elsif ($cname =~ /\.ucm$/)
99 elsif ($cname =~ /\.pet$/)
105 if (exists $opt{'f'})
107 # -F is followed by name of file containing list of filenames
108 my $flist = $opt{'f'};
109 open(FLIST,$flist) || die "Cannot open $flist:$!";
110 chomp(@encfiles = <FLIST>);
123 if ($a =~ /^.*-(\d+)/)
126 if ($b =~ /^.*-(\d+)/)
136 foreach my $enc (sort cmp_name @encfiles)
138 my ($name,$sfx) = $enc =~ /^.*?([\w-]+)\.(enc|ucm)$/;
139 $name = $opt{'n'} if exists $opt{'n'};
144 compile_enc(\*E,lc($name));
148 compile_ucm(\*E,lc($name));
153 warn "Cannot open $enc for $name:$!";
159 foreach my $name (sort cmp_name keys %encoding)
161 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
162 output(\*C,$name.'_utf8',$e2u);
163 output(\*C,'utf8_'.$name,$u2e);
164 push(@{$encoding{$name}},outstring(\*C,$e2u->{Cname}.'_def',$erep));
166 foreach my $enc (sort cmp_name keys %encoding)
168 my ($e2u,$u2e,$rep,$min_el,$max_el,$rsym) = @{$encoding{$enc}};
169 my @info = ($e2u->{Cname},$u2e->{Cname},$rsym,length($rep),$min_el,$max_el);
170 my $sym = "${enc}_encoding";
172 print C "encode_t $sym = \n";
173 print C " {",join(',',@info,"{\"$enc\",(const char *)0}"),"};\n\n";
176 foreach my $enc (sort cmp_name keys %encoding)
178 my $sym = "${enc}_encoding";
180 print H "extern encode_t $sym;\n";
181 print D " Encode_Define(aTHX_ &$sym);\n";
184 if ($cname =~ /(\w+)\.xs$/)
187 print C "\nMODULE = Encode::$mod\tPACKAGE = Encode::$mod\n\n";
188 print C "BOOT:\n{\n";
189 print C "#include \"$dname\"\n";
197 foreach my $name (sort cmp_name keys %encoding)
199 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
200 output_enc(\*C,$name,$e2u);
205 foreach my $name (sort cmp_name keys %encoding)
207 my ($e2u,$u2e,$erep,$min_el,$max_el) = @{$encoding{$name}};
208 output_ucm(\*C,$name,$u2e,$erep,$min_el,$max_el);
225 last if /^\s*CHARMAP\s*$/i;
226 if (/^\s*<(\w+)>\s+"?([^"]*)"?\s*$/i)
231 if (!defined($cs = $attr{'code_set_name'}))
233 warn "No <code_set_name> in $name\n";
237 $name = $cs unless exists $opt{'n'};
243 if (exists $attr{'subchar'})
246 $attr{'subchar'} =~ /^\s*/cg;
247 push(@byte,$1) while $attr{'subchar'} =~ /\G\\x([0-9a-f]+)/icg;
248 $erep = join('',map(chr(hex($_)),@byte));
250 print "Scanning $name ($cs)\n";
256 last if /^\s*END\s+CHARMAP\s*$/i;
260 $u = $1 if (/^<U([0-9a-f]+)>\s+/igc);
261 push(@byte,$1) while /\G\\x([0-9a-f]+)/igc;
262 $fb = $1 if /\G\s*(\|[0-3])/gc;
263 # warn "$_: $u @byte | $fb\n";
264 die "Bad line:$_" unless /\G\s*(#.*)?$/gc;
267 my $uch = encode_U(hex($u));
268 my $ech = join('',map(chr(hex($_)),@byte));
269 my $el = length($ech);
270 $max_el = $el if (!defined($max_el) || $el > $max_el);
271 $min_el = $el if (!defined($min_el) || $el < $min_el);
282 # $fb is fallback flag
283 # 0 - round trip safe
284 # 1 - fallback for unicode -> enc
285 # 2 - skip sub-char mapping
286 # 3 - fallback enc -> unicode
287 enter($u2e,$uch,$ech,$u2e,$fb+0) if ($fb =~ /[01]/);
288 enter($e2u,$ech,$uch,$e2u,$fb+0) if ($fb =~ /[03]/);
297 die "$nfb entries without fallback, $hfb entries with\n";
299 $encoding{$name} = [$e2u,$u2e,$erep,$min_el,$max_el];
309 while ($type = <$fh>)
311 last if $type !~ /^\s*#/;
314 return if $type eq 'E';
315 my ($def,$sym,$pages) = split(/\s+/,scalar(<$fh>));
316 warn "$type encoded $name\n";
323 $rep = &{"encode_$type"}($v & 0xFF, ($v >> 8) & 0xffe);
330 my $page = hex($line);
332 for (my $i = 0; $i < 16; $i++)
335 for (my $j = 0; $j < 16; $j++)
338 my $ech = &{"encode_$type"}($ch,$page);
339 my $val = hex(substr($line,0,4,''));
340 next if $val == 0xFFFD;
341 if ($val || (!$ch && !$page))
343 my $el = length($ech);
344 $max_el = $el if (!defined($max_el) || $el > $max_el);
345 $min_el = $el if (!defined($min_el) || $el < $min_el);
346 my $uch = encode_U($val);
347 if (exists $seen{$uch})
349 warn sprintf("U%04X is %02X%02X and %02X%02X\n",
350 $val,$page,$ch,@{$seen{$uch}});
354 $seen{$uch} = [$page,$ch];
356 enter($e2u,$ech,$uch,$e2u,0);
357 enter($u2e,$uch,$ech,$u2e,0);
361 # No character at this position
362 # enter($e2u,$ech,undef,$e2u);
368 $encoding{$name} = [$e2u,$u2e,$rep,$min_el,$max_el];
373 my ($a,$s,$d,$t,$fb) = @_;
375 my $b = substr($s,0,1);
379 $e = [$b,$b,'',{},length($s),0,$fb];
384 enter($e->[3],substr($s,1),$d,$t,$fb);
390 $e->[5] = length($d);
396 my ($fh,$name,$s) = @_;
397 my $sym = $strings{$s};
400 foreach my $o (keys %strings)
402 my $i = index($o,$s);
406 $sym .= sprintf("+0x%02x",$i) if ($i);
410 $strings{$s} = $sym = $name;
411 printf $fh "\nstatic const U8 %s[%d] =\n",$name,length($s);
412 # Do in chunks of 16 chars to constrain line length
413 # Assumes ANSI C adjacent string litteral concatenation
416 my $c = substr($s,0,16,'');
417 print $fh '"',join('',map(sprintf('\x%02x',ord($_)),split(//,$c))),'"';
418 print $fh "\n" if length($s);
430 my @keys = grep(ref($a->{$_}),sort keys %$a);
433 foreach my $b (@keys)
435 my ($s,$f,$out,$t,$end) = @{$a->{$b}};
437 ord($b) == ord($a->{$l}[1])+1 &&
438 $a->{$l}[3] == $a->{$b}[3] &&
439 $a->{$l}[4] == $a->{$b}[4] &&
440 $a->{$l}[5] == $a->{$b}[5] &&
441 $a->{$l}[6] == $a->{$b}[6]
442 # && length($a->{$l}[2]) < 16
445 my $i = ord($b)-ord($a->{$l}[0]);
447 $a->{$l}[2] .= $a->{$b}[2];
454 if (exists $t->{Cname})
456 $t->{'Forward'} = 1 if $t != $a;
460 process(sprintf("%s_%02x",$name,ord($s)),$t);
463 if (ord($keys[-1]) < 255)
465 my $t = chr(ord($keys[-1])+1);
466 $a->{$t} = [$t,chr(255),undef,$a,0,0];
469 $a->{'Entries'} = \@ent;
475 my $name = $a->{'Cname'};
477 foreach my $b (@{$a->{'Entries'}})
479 next unless $a->{$b}[5];
480 my $s = ord($a->{$b}[0]);
481 my $e = ord($a->{$b}[1]);
482 outstring($fh,sprintf("%s__%02x_%02x",$name,$s,$e),$a->{$b}[2]);
486 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"];\n";
489 foreach my $b (@{$a->{'Entries'}})
491 my ($s,$e,$out,$t,$end,$l) = @{$a->{$b}};
492 outtable($fh,$t) unless $t->{'Done'};
494 print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"] = {\n";
495 foreach my $b (@{$a->{'Entries'}})
497 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
504 printf $fh outstring($fh,'',$out);
510 print $fh ",",$t->{Cname};
511 printf $fh ",0x%02x,0x%02x,$l,$end},\n",$sc,$ec;
518 my ($fh,$name,$a) = @_;
526 my ($fh,$name,$a) = @_;
527 foreach my $b (sort keys %$a)
529 my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
541 my $s = do "unicore/Name.pl";
542 die "char_names: unicore/Name.pl: $!\n" unless defined $s;
544 while ($s =~ /\G([0-9a-f]+)\t([0-9a-f]*)\t(.*?)\s*\n/igc)
548 last if $s >= 0x10000;
549 my $e = length($2) ? hex($2) : $s;
550 for (my $i = $s; $i <= $e; $i++)
553 # print sprintf("U%04X $name\n",$i);
560 my ($cmap,$a,$t,$pre) = @_;
561 # warn sprintf("Page %x\n",$pre);
562 foreach my $b (sort keys %$t)
564 my ($s,$e,$out,$n,$end,$l,$fb) = @{$t->{$b}};
565 die "oops $s $e" unless $s eq $e;
567 if ($n != $a && $n != $t)
569 output_ucm_page($cmap,$a,$n,(($pre|($u &0x3F)) << 6)&0xFFFF);
575 $u = $pre|($u &0x3f);
577 my $s = sprintf "<U%04X> ",$u;
578 foreach my $c (split(//,$out))
580 $s .= sprintf "\\x%02X",ord($c);
582 $s .= sprintf " |%d # %s\n",($fb ? 1 : 0),$uname[$u];
587 warn join(',',@{$t->{$b}},$a,$t);
594 my ($fh,$name,$h,$rep,$min_el,$max_el) = @_;
595 print $fh "# Written $perforce\n# $0 @orig_ARGV\n" unless $opt{'q'};
596 print $fh "<code_set_name> \"$name\"\n";
600 print $fh "<mb_cur_min> $min_el\n";
604 print $fh "<mb_cur_max> $max_el\n";
608 print $fh "<subchar> ";
609 foreach my $c (split(//,$rep))
611 printf $fh "\\x%02X",ord($c);
616 output_ucm_page(\@cmap,$h,$h,0);
617 print $fh "#\nCHARMAP\n";
618 foreach my $line (sort { substr($a,8) cmp substr($b,8) } @cmap)
622 print $fh "END CHARMAP\n";