Start of support of ICU-style .ucm files:
[p5sagit/p5-mst-13.2.git] / ext / Encode / compile
1 #!../../perl -w
2 BEGIN { @INC = '../../lib' };
3 use strict;
4
5 sub encode_U
6 {
7  # UTF-8 encode long hand - only covers part of perl's range
8  my $uv = shift;
9  if ($uv < 0x80)
10   {
11    return chr($uv)
12   }
13  if ($uv < 0x800)
14   {
15    return chr(($uv >> 6)        | 0xC0).
16           chr(($uv & 0x3F)      | 0x80);
17   }
18  return chr(($uv >> 12)         | 0xE0).
19         chr((($uv >> 6) & 0x3F) | 0x80).
20         chr(($uv & 0x3F)        | 0x80);
21 }
22
23 sub encode_S
24 {
25  # encode single byte
26  my ($ch,$page) = @_;
27  return chr($ch);
28 }
29
30 sub encode_D
31 {
32  # encode double byte MS byte first
33  my ($ch,$page) = @_;
34  return chr($page).chr($ch);
35 }
36
37 sub encode_M
38 {
39  # encode Multi-byte - single for 0..255 otherwise double
40  my ($ch,$page) = @_;
41  return &encode_D if $page;
42  return &encode_S;
43 }
44
45 # Win32 does not expand globs on command line
46 eval "\@ARGV = map(glob(\$_),\@ARGV)" if ($^O eq 'MSWin32');
47
48 my $cname = shift(@ARGV);
49 chmod(0666,$cname) if -f $cname && !-w $cname;
50 open(C,">$cname") || die "Cannot open $cname:$!";
51 my $dname = $cname;
52 $dname =~ s/(\.[^\.]*)?$/.def/;
53 chmod(0666,$dname) if -f $cname && !-w $dname;
54 open(D,">$dname") || die "Cannot open $dname:$!";
55 my $hname = $cname;
56 $hname =~ s/(\.[^\.]*)?$/.h/;
57 chmod(0666,$hname) if -f $cname && !-w $hname;
58 open(H,">$hname") || die "Cannot open $hname:$!";
59
60 foreach my $fh (\*C,\*D,\*H)
61 {
62  print $fh <<"END";
63 /*
64  !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
65  This file was autogenerated by:
66  $^X $0 $cname @ARGV
67 */
68 END
69 }
70
71 if ($cname =~ /(\w+)\.xs$/)
72  {
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";
77  }
78 print C "#include \"encode.h\"\n";
79
80 my %encoding;
81 my %strings;
82
83 sub cmp_name
84 {
85  if ($a =~ /^.*-(\d+)/)
86   {
87    my $an = $1;
88    if ($b =~ /^.*-(\d+)/)
89     {
90      my $r = $an <=> $1;
91      return $r if $r;
92     }
93   }
94  return $a cmp $b;
95 }
96
97 foreach my $enc (sort cmp_name @ARGV)
98  {
99   my ($name,$sfx) = $enc =~ /^.*?([\w-]+)\.(enc|ucm)$/;
100   if (open(E,$enc))
101    {
102     if ($sfx eq 'enc')
103      {
104       compile_enc(\*E,lc($name),\*C);
105      }
106     else
107      {
108       compile_ucm(\*E,lc($name),\*C);
109      }
110    }
111   else
112    {
113     warn "Cannot open $enc for $name:$!";
114    }
115  }
116
117 foreach my $enc (sort cmp_name keys %encoding)
118  {
119   my $sym = "${enc}_encoding";
120   $sym =~ s/\W+/_/g;
121   print C "encode_t $sym = \n";
122   print C " {",join(',',"\"$enc\"",@{$encoding{$enc}}),"};\n\n";
123  }
124
125 foreach my $enc (sort cmp_name keys %encoding)
126  {
127   my $sym = "${enc}_encoding";
128   $sym =~ s/\W+/_/g;
129   print H "extern encode_t $sym;\n";
130   print D " Encode_Define(aTHX_ &$sym);\n";
131  }
132
133 if ($cname =~ /(\w+)\.xs$/)
134  {
135   my $mod = $1;
136   print C "\nMODULE = Encode::$mod\tPACKAGE = Encode::$mod\n\n";
137   print C "BOOT:\n{\n";
138   print C "#include \"$dname\"\n";
139   print C "}\n";
140  }
141 close(C);
142 close(D);
143 close(H);
144
145
146 sub compile_ucm
147 {
148  my ($fh,$name,$ch) = @_;
149  my $e2u = {};
150  my $u2e = {};
151  my $cs;
152  my %attr;
153  while (<$fh>)
154   {
155    s/#.*$//;
156    last if /^\s*CHARMAP\s*$/i;
157    if (/^\s*<(\w+)>\s+"?([^"]*)"?\s*$/i)
158     {
159      $attr{$1} = $2;
160     }
161   }
162  if (!defined($cs =  $attr{'code_set_name'}))
163   {
164    warn "No <code_set_name> in $name\n";
165   }
166  else
167   {
168    $name = lc($cs);
169   }
170  my $erep;
171  my $urep;
172  if (exists $attr{'subchar'})
173   {
174    my @byte = $attr{'subchar'} =~ /^\s*(?:\\x([0-9a-f]+))+\s*$/;
175    $erep = join('',map(hex($_),@byte));
176   }
177  warn "Scanning $cs\n";
178  my $nfb = 0;
179  my $hfb = 0;
180  while (<$fh>)
181   {
182    s/#.*$//;
183    last if /^\s*END\s+CHARMAP\s*$/i;
184    next if /^\s*$/;
185    my ($u,@byte) = /^<U([0-9a-f]+)>\s+(?:\\x([0-9a-f]+))+\s*(\|[0-3]|)\s*$/i;
186    my $fb = pop(@byte);
187    if (defined($u))
188     {
189      my $uch = encode_U(hex($u));
190      my $ech = join('',map(hex($_),@byte));
191      if (length($fb))
192       {
193        $fb = substr($fb,1);
194        $hfb++;
195       }
196      else
197       {
198        $nfb++;
199        $fb = '0';
200       }
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]/);
208     }
209    else
210     {
211      warn $_;
212     }
213
214   }
215  if ($nfb && $hfb)
216   {
217    die "$nfb entries without fallback, $hfb entries with\n";
218   }
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)];
223 }
224
225 sub compile_enc
226 {
227  my ($fh,$name,$ch) = @_;
228  my $e2u = {};
229  my $u2e = {};
230
231  my $type;
232  while ($type = <$fh>)
233   {
234    last if $type !~ /^\s*#/;
235   }
236  chomp($type);
237  return if $type eq 'E';
238  my ($def,$sym,$pages) = split(/\s+/,scalar(<$fh>));
239  warn "$type encoded $name\n";
240  my $rep = '';
241  {
242   my $v = hex($def);
243   no strict 'refs';
244   $rep = &{"encode_$type"}($v & 0xFF, ($v >> 8) & 0xffe);
245  }
246  while ($pages--)
247   {
248    my $line = <$fh>;
249    chomp($line);
250    my $page = hex($line);
251    my $ch = 0;
252    for (my $i = 0; $i < 16; $i++)
253     {
254      my $line = <$fh>;
255      for (my $j = 0; $j < 16; $j++)
256       {
257        no strict 'refs';
258        my $ech = &{"encode_$type"}($ch,$page);
259        my $val = hex(substr($line,0,4,''));
260        if ($val || (!$ch && !$page))
261         {
262          my $uch = encode_U($val);
263          enter($e2u,$ech,$uch,$e2u,0);
264          enter($u2e,$uch,$ech,$u2e,0);
265         }
266        else
267         {
268          # No character at this position
269          # enter($e2u,$ech,undef,$e2u);
270         }
271        $ch++;
272       }
273     }
274   }
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)];
279 }
280
281 sub enter
282 {
283  my ($a,$s,$d,$t,$fb) = @_;
284  $t = $a if @_ < 4;
285  my $b = substr($s,0,1);
286  my $e = $a->{$b};
287  unless ($e)
288   {     # 0  1  2  3         4  5
289    $e = [$b,$b,'',{},length($s),0,$fb];
290    $a->{$b} = $e;
291   }
292  if (length($s) > 1)
293   {
294    enter($e->[3],substr($s,1),$d,$t,$fb);
295   }
296  else
297   {
298    $e->[2] = $d;
299    $e->[3] = $t;
300    $e->[5] = length($d);
301   }
302 }
303
304 sub outstring
305 {
306  my ($fh,$name,$s) = @_;
307  my $sym = $strings{$s};
308  unless ($sym)
309   {
310    foreach my $o (keys %strings)
311     {
312      my $i = index($o,$s);
313      if ($i >= 0)
314       {
315        $sym = $strings{$o};
316        $sym .= sprintf("+0x%02x",$i) if ($i);
317        return $sym;
318       }
319     }
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
324    while (length($s))
325     {
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);
329     }
330    printf $fh ";\n";
331   }
332  return $sym;
333 }
334
335 sub process
336 {
337  my ($name,$a) = @_;
338  $name =~ s/\W+/_/g;
339  $a->{Cname} = $name;
340  my @keys = grep(ref($a->{$_}),sort keys %$a);
341  my $l;
342  my @ent;
343  foreach my $b (@keys)
344   {
345    my ($s,$f,$out,$t,$end) = @{$a->{$b}};
346    if (defined($l) &&
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
353       )
354     {
355      my $i = ord($b)-ord($a->{$l}[0]);
356      $a->{$l}[1]  = $b;
357      $a->{$l}[2] .= $a->{$b}[2];
358     }
359    else
360     {
361      $l = $b;
362      push(@ent,$b);
363     }
364    if (exists $t->{Cname})
365     {
366      $t->{'Forward'} = 1 if $t != $a;
367     }
368    else
369     {
370      process(sprintf("%s_%02x",$name,ord($s)),$t);
371     }
372   }
373  if (ord($keys[-1]) < 255)
374   {
375    my $t = chr(ord($keys[-1])+1);
376    $a->{$t} = [$t,chr(255),undef,$a,0,0];
377    push(@ent,$t);
378   }
379  $a->{'Entries'} = \@ent;
380 }
381
382 sub outtable
383 {
384  my ($fh,$a) = @_;
385  my $name = $a->{'Cname'};
386  # String tables
387  foreach my $b (@{$a->{'Entries'}})
388   {
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]);
393   }
394  if ($a->{'Forward'})
395   {
396    print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"];\n";
397   }
398  $a->{'Done'} = 1;
399  foreach my $b (@{$a->{'Entries'}})
400   {
401    my ($s,$e,$out,$t,$end,$l) = @{$a->{$b}};
402    outtable($fh,$t) unless $t->{'Done'};
403   }
404  print $fh "\nstatic encpage_t $name\[",scalar(@{$a->{'Entries'}}),"] = {\n";
405  foreach my $b (@{$a->{'Entries'}})
406   {
407    my ($s,$e,$out,$t,$end,$l,$fb) = @{$a->{$b}};
408    my $sc = ord($s);
409    my $ec = ord($e);
410    print  $fh "{";
411    if ($l)
412     {
413      printf $fh outstring($fh,'',$out);
414     }
415    else
416     {
417      print  $fh "0";
418     }
419    print  $fh ",",$t->{Cname};
420    printf $fh ",0x%02x,0x%02x,$l,$end},\n",$sc,$ec;
421   }
422  print $fh "};\n";
423 }
424
425 sub output
426 {
427  my ($fh,$name,$a) = @_;
428  process($name,$a);
429  # Sub-tables
430  outtable($fh,$a);
431 }
432
433