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)
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
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
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";
41 print "#include \"tld.h\"\n";
46 foreach my $tldfile (@ARGV)
49 my $data = process_definition
($tldfile, \
$tld, \
$version);
55 print_tld_domain
($tld, \@
$data, $version);
58 print "/* Main array */\n";
59 print "const Tld_table * _tld_tables[] =\n";
63 print " &_tld_${_},\n";
68 # Print tld domain structure.
69 # void print_tld_domain($tldfile, \$tld, \@intervals, $version);
73 my $intervals = shift;
75 my $num_intervals = @
$intervals;
77 print "/* TLD $tld */\n";
78 print "static const Tld_table_element _tld_${tld}_valid[] =\n";
82 my $last = pop @
$intervals;
85 printf " { 0x%x, 0x%x },\n", $_->[0], $_->[1];
87 printf " { 0x%x, 0x%x }\n", $last->[0], $last->[1];
92 print "static const Tld_table _tld_${tld} =\n";
95 print " \"$version\",\n";
96 print " $num_intervals,\n";
97 print " &_tld_${tld}_valid[0]\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;
115 open(FILE
, "<$filename") or die "Cannot open $filename";
118 $path =~ s/\/[^\/]+$//;
120 my ($is_int,$have_num,$num,$cnum);
128 if (m/^include\s+(\S+)\s*$/i)
132 my $incdata = process_definition
("$path/$incfile", \
$junk, \
$ver);
133 $incversion = $incversion . " $incfile ($ver)";
134 push @data, @
$incdata;
138 if (m/^version\s+"(.*)"\s*$/i)
144 if (m/^tld\s+(\S+)\s*$/i)
153 if ( (s/^(0x)([a-f0-9]+)//i) ||
154 (s/^(U\+)([a-f0-9]+)//i) ||
159 if (((lc($1) eq "0x") || (lc($1) eq "u+")) && $2)
163 elsif (($1 eq "0") && $2)
172 push @data, [$num, $cnum];
178 push @data, [$num,$num];
202 die "Parser error in file $filename at line $line near $_, "
214 push @data, [$num, $cnum];
218 push @data, [$num, $num];
222 $$version = $$version . $incversion if $incversion;
226 # Build minimal set of intervals.
227 # void optimize(\@intervals)
230 my $intervals = shift;
232 return undef unless (@
$intervals);
234 my @intervals = sort {$a->[0] <=> $b->[0]} @
$intervals;
237 my $cur_int = shift @intervals;
241 if ($_->[0]>($cur_int->[1]+1))
243 push @
$intervals, $cur_int;
248 if ($_->[1] > $cur_int->[1])
250 $cur_int->[1] = $_->[1];
254 push @
$intervals, $cur_int;