Add example.
[libidn.git] / gen-stringprep-tables.pl
blobcf83659bb73e5ac18dd99791b8b35fa8d63b2149
1 #! /usr/bin/perl -w
3 # Copyright (C) 2002 Simon Josefsson
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2, or (at your option)
8 # any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18 # 02111-1307, USA.
20 # I consider the output of this program to be unrestricted. Use it as
21 # you will.
23 use strict;
25 my ($tab) = 59;
26 my ($intable) = 0;
27 my ($tablename);
28 my ($varname);
29 my ($starheader, $header);
30 my ($prefix) = "stringprep";
31 my ($profile) = "generic";
32 my ($filename) = "${prefix}_${profile}.c";
33 my ($line, $start, $end, @map);
35 open(FH, ">$filename") or die "cannot open $filename for writing";
37 print FH "#include <stringprep.h>\n";
39 while(<>) {
40 $line = $_;
42 die "already in table" if $intable && m,^----- Start Table (.*) -----,;
43 die "not in table" if !$intable && m,^----- End Table (.*) -----,;
45 if ($intable && m,^----- End Table (.*) -----,) {
46 die "table error" unless $1 eq $tablename ||
47 ($1 eq "C.1.2" && $tablename eq "C.1.1"); # Typo in draft
48 $intable = 0;
49 print FH " { 0 },\n";
50 print FH "};\n\n";
53 if (m,^[A-Z],) {
54 $header = $line;
55 } elsif (!m,^-,) {
56 $header .= $line;
59 next unless ($intable || m,^----- Start Table (.*) -----,);
61 if ($intable) {
62 $_ = $line;
63 chop $line;
65 die "regexp failed on line: $line" unless
66 m,^([0-9A-F]+)(-([0-9A-F]+))?(; ([0-9A-F]+)( ([0-9A-F]+))?( ([0-9A-F]+))?( ([0-9A-F]+))?;)?,;
68 die "too many mapping targets on line: $line" if $12;
70 $start = $1;
71 $end = $3;
72 $map[0] = $5;
73 $map[1] = $7;
74 $map[2] = $9;
75 $map[3] = $11;
77 die "tables tried to map a range" if $end && $map[0];
79 if ($map[3]) {
80 printf FH " { 0x%06s, 0, { 0x%06s,%*s/* %s */\n 0x%06s, 0x%06s, 0x%06s }},\n",
81 $start, $map[0], $tab-length($line)-13, " ", $line,
82 $map[1], $map[2], $map[3];
83 } elsif ($map[2]) {
84 printf FH " { 0x%06s, 0, { 0x%06s,%*s/* %s */\n 0x%06s, 0x%06s }},\n",
85 $start, $map[0], $tab-length($line)-14, " ", $line,
86 $map[1], $map[2];
87 } elsif ($map[1]) {
88 printf FH " { 0x%06s, 0, { 0x%06s,%*s/* %s */\n 0x%06s }},\n",
89 $start, $map[0], $tab-length($line)-14, " ", $line,
90 $map[1];
91 } elsif ($map[0]) {
92 printf FH " { 0x%06s, 0, { 0x%06s }},%*s/* %s */\n",
93 $start, $map[0], $tab-length($line)-17, " ", $line;
94 } elsif ($end) {
95 printf FH " { 0x%06s, 0x%06s },%*s/* %s */\n",
96 $start, $end, $tab-length($line)-11, " ", $line;
97 } else {
98 printf FH " { 0x%06s },%*s/* %s */\n",
99 $start, $tab-length($line)-11, " ", $line;
101 } else {
102 $intable = 1 if !$intable;
103 $tablename = $1;
105 ($varname = $tablename) =~ tr/./_/;
106 $header =~ s/\n/\n * /s;
108 print FH "\n/*\n * $header */\n\n";
109 print FH "struct ${prefix}_table_element ${prefix}_${profile}_${varname}\[\] = {\n";
113 close FH or die "cannot close $filename";