summaryrefslogtreecommitdiff
path: root/lib/gen-stringprep-tables.pl
blob: b1670ce01d15d71148196974fffed69340123d4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#! /usr/bin/perl -w

# Copyright (C) 2002, 2003, 2005, 2006, 2007, 2008, 2009 Simon Josefsson

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# I consider the output of this program to be unrestricted.  Use it as
# you will.

use strict;

my ($tab) = 59;
my ($intable) = 0;
my ($tablename);
my ($varname);
my ($starheader, $header);
my ($profile) = "rfc3454";
my ($filename) = "$profile.c";
my ($line, $start, $end, @map);

open(FH, ">$filename") or die "cannot open $filename for writing";

print FH "/* This file is automatically generated.  DO NOT EDIT!\n";
print FH "   Instead, edit gen-stringprep-tables.pl and re-run.  */\n\n";

print FH "#include \"stringprep.h\"\n";

while(<>) {
    s/^   (.*)/$1/g; # for rfc
    $line = $_;

    die "already in table" if $intable && m,^----- Start Table (.*) -----,;
    die "not in table" if !$intable && m,^----- End Table (.*) -----,;

    if ($intable && m,^----- End Table (.*) -----,) {
	die "table error" unless $1 eq $tablename ||
	    ($1 eq "C.1.2" && $tablename eq "C.1.1"); # Typo in draft
	$intable = 0;
	print FH "  { 0 },\n";
	print FH "};\n\n";
    }

    if (m,^[A-Z],) {
	$header = $line;
    } elsif (!m,^[ -],) {
	$header .= $line;
    }

    next unless ($intable || m,^----- Start Table (.*) -----,);

    if ($intable) {
	$_ = $line;
	chop $line;

	next if m,^$,;
	next if m,^Hoffman & Blanchet          Standards Track                    \[Page [0-9]+\]$,;
	next if m,^$,;
	next if m,RFC 3454        Preparation of Internationalized Strings   December 2002,;

	die "regexp failed on line: $line" unless
	    m,^([0-9A-F]+)(-([0-9A-F]+))?(; ([0-9A-F]+)( ([0-9A-F]+))?( ([0-9A-F]+))?( ([0-9A-F]+))?;)?,;

	die "too many mapping targets on line: $line" if $12;

	$start = $1;
	$end = $3;
	$map[0] = $5;
	$map[1] = $7;
	$map[2] = $9;
	$map[3] = $11;

	die "tables tried to map a range" if $end && $map[0];

	if ($map[3]) {
	    printf FH "  { 0x%06s, 0, { 0x%06s,%*s/* %s */\n                   0x%06s, 0x%06s, 0x%06s }},\n",
	    $start, $map[0], $tab-length($line)-13, " ", $line,
	    $map[1], $map[2], $map[3];
	} elsif ($map[2]) {
	    printf FH "  { 0x%06s, 0, { 0x%06s,%*s/* %s */\n                   0x%06s, 0x%06s }},\n",
	    $start, $map[0], $tab-length($line)-14, " ", $line,
	    $map[1], $map[2];
	} elsif ($map[1]) {
	    printf FH "  { 0x%06s, 0, { 0x%06s,%*s/* %s */\n                   0x%06s }},\n",
	    $start, $map[0], $tab-length($line)-14, " ", $line,
	    $map[1];
	} elsif ($map[0]) {
	    printf FH "  { 0x%06s, 0, { 0x%06s }},%*s/* %s */\n",
	    $start, $map[0], $tab-length($line)-17, " ",  $line;
	} elsif ($end) {
	    printf FH "  { 0x%06s, 0x%06s },%*s/* %s */\n",
	    $start, $end, $tab-length($line)-11, " ",  $line;
	} else {
	    printf FH "  { 0x%06s           },%*s/* %s */\n",
	    $start, $tab-length($line)-11, " ",  $line;
	}
    } else {
	$intable = 1 if !$intable;
	$tablename = $1;

	($varname = $tablename) =~ tr/./_/;
	$header =~ s/\n/\n * /s;

	print FH "\n/*\n * $header */\n\n";
	print FH "const Stringprep_table_element stringprep_${profile}_${varname}\[\] = {\n";
    }
}

close FH or die "cannot close $filename";