Fix.
[libidn.git] / lib / gen-tld-tables.pl
blob3f0245fc1ecfe10bdcbbf266f6b42e1412cce32e
1 #!/usr/bin/perl
3 # Author: Thomas Jacob, Internet24.de
5 # Copyright (C) 2004 Simon Josefsson.
6 # Copyright (C) 2004 Free Software Foundation, Inc.
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2, or (at your option)
11 # any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 # 02111-1307, USA.
23 # See doc/tld/README and draft-hoffman-idn-reg-*.txt inside Libidn for
24 # more information on the input file syntax.
26 # Use this to generate TLD tables:
27 # $ ./gen-tld-tables *.tld > tlds.c
29 # I consider the output of this program to be unrestricted. Use it as
30 # you will.
33 use strict;
34 use warnings;
36 die "Usage: $0 <TLD-FILE [TLD-FILE ...]>" if ($#ARGV == -1);
38 print "/* This file is automatically generated. DO NOT EDIT!\n";
39 print " Instead, edit gen-tld-tables.pl and re-run. */\n";
40 print "\n";
41 print "#include \"tld.h\"\n";
42 print "\n";
44 my @tlds;
46 foreach my $tldfile (@ARGV)
48 my ($tld, $version);
49 my $data = process_definition ($tldfile, \$tld, \$version);
51 push @tlds, $tld;
53 optimize($data);
55 print_tld_domain($tld, \@$data, $version);
58 print "/* Main array */\n";
59 print "const Tld_table * _tld_tables[] =\n";
60 print "{\n";
61 foreach (@tlds)
63 print " &_tld_${_},\n";
65 print " NULL\n";
66 print "};\n";
68 # Print tld domain structure.
69 # void print_tld_domain($tldfile, \$tld, \@intervals, $version);
70 sub print_tld_domain
72 my $tld = shift;
73 my $intervals = shift;
74 my $version = shift;
75 my $num_intervals = @$intervals;
77 print "/* TLD $tld */\n";
78 print "static const Tld_table_element _tld_${tld}_valid[] =\n";
79 print "{\n";
80 if (@$intervals)
82 my $last = pop @$intervals;
83 foreach (@$intervals)
85 printf " { 0x%x, 0x%x },\n", $_->[0], $_->[1];
87 printf " { 0x%x, 0x%x }\n", $last->[0], $last->[1];
89 print "};\n";
90 print "\n";
92 print "static const Tld_table _tld_${tld} =\n";
93 print "{\n";
94 print " \"$tld\",\n";
95 print " \"$version\",\n";
96 print " $num_intervals,\n";
97 print " &_tld_${tld}_valid[0]\n";
98 print "};\n\n";
101 # process a definition file
102 # $filename, \$tld, \$version
103 # return @data on success, die otherwise
104 sub process_definition
106 my $filename = shift;
107 my $tld = shift;
108 my $version = shift;
109 my $incversion;
110 my $path;
111 my @data;
113 local * FILE;
115 open(FILE, "<$filename") or die "Cannot open $filename";
117 $path = $filename;
118 $path =~ s/\/[^\/]+$//;
120 my ($is_int,$have_num,$num,$cnum);
121 my $line = 1;
122 while(<FILE>)
124 chomp;
126 s/#.*$//;
128 if (m/^include\s+(\S+)\s*$/i)
130 my $incfile = $1;
131 my ($junk, $ver);
132 my $incdata = process_definition("$path/$incfile", \$junk, \$ver);
133 $incversion = $incversion . " $incfile ($ver)";
134 push @data, @$incdata;
135 next;
138 if (m/^version\s+"(.*)"\s*$/i)
140 $$version = $1;
141 next;
144 if (m/^tld\s+(\S+)\s*$/i)
146 $$tld = $1;
147 next;
150 while ($_ ne "")
152 s/^\s*//;
153 if ( (s/^(0x)([a-f0-9]+)//i) ||
154 (s/^(U\+)([a-f0-9]+)//i) ||
155 (s/^(0)(\d+)//) ||
156 (s/^(\d+)//) )
158 $cnum = $1;
159 if (((lc($1) eq "0x") || (lc($1) eq "u+")) && $2)
161 $cnum = hex($2);
163 elsif (($1 eq "0") && $2)
165 $cnum = oct($2);
168 if ($have_num)
170 if ($is_int)
172 push @data, [$num, $cnum];
173 $have_num = 0;
174 $is_int = 0;
176 else
178 push @data, [$num,$num];
179 $num = $cnum;
182 else
184 $have_num = 1;
185 $num = $cnum;
188 elsif (s/^\-//)
190 $is_int = 1;
192 elsif (s/^\|//)
194 $is_int = 0;
196 elsif (s/^\://)
198 $is_int = 0;
200 else
202 die "Parser error in file $filename at line $line near $_, "
203 if $_ ne "";
206 $line++;
208 close(FILE);
210 if ($have_num)
212 if ($is_int)
214 push @data, [$num, $cnum];
216 else
218 push @data, [$num, $num];
222 $$version = $$version . $incversion if $incversion;
223 return \@data;
226 # Build minimal set of intervals.
227 # void optimize(\@intervals)
228 sub optimize
230 my $intervals = shift;
232 return undef unless (@$intervals);
234 my @intervals = sort {$a->[0] <=> $b->[0]} @$intervals;
235 @$intervals = ();
237 my $cur_int = shift @intervals;
239 foreach (@intervals)
241 if ($_->[0]>($cur_int->[1]+1))
243 push @$intervals, $cur_int;
244 $cur_int = $_;
246 else
248 if ($_->[1] > $cur_int->[1])
250 $cur_int->[1] = $_->[1];
254 push @$intervals, $cur_int;