1#!/usr/bin/perl
2
3# usage: compile-et input.et
4
5#
6# This Source Code Form is subject to the terms of the Mozilla Public
7# License, v. 2.0. If a copy of the MPL was not distributed with this
8# file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10sub header
11{
12    local($filename, $comment) = @_;
13
14<<EOF
15$comment
16$comment $filename
17$comment This file is automatically generated; please do not edit it.
18EOF
19}
20
21sub table_base
22{
23    local($name) = @_;
24    local($base) = 0;
25
26    for ($i = 0; $i < length($name); $i++) {
27	$base *= 64;
28	$base += index("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_", substr($name, $i, 1)) + 1;
29    }
30    $base -= 0x1000000 if ($base > 0x7fffff);
31    $base*256;
32}
33
34sub code {
35    local($macro, $text) = @_;
36    $code = $table_base + $table_item_count;
37
38    print H "\n";
39    print H "/* ", $text, " */\n";
40    printf H "#define %-40s (%dL)\n", $macro, $code;
41
42    print C "\t{\"", $macro, "\",    \"", $text, "\"},\n";
43
44    print PROPERTIES $macro, "=", $text, "\n";
45
46    $table_item_count++;
47}
48
49
50$filename = $ARGV[0];
51open(INPUT, "< $filename") || die "Can't read $filename: $!\n";
52
53$base = "$filename";
54$base =~ s/\.et$//;
55$base =~ s#.*/##;
56
57open(H, "> ${base}.h") || die "Can't write ${base}.h\n";
58open(C, "> ${base}.c") || die "Can't write ${base}.c\n";
59open(PROPERTIES, "> ${base}.properties") || die "Can't write ${base}.properties\n";
60
61print H "/*\n", &header("${base}.h", " *"), " */\n";
62print C "/*\n", &header("${base}.c", " *"), " */\n";
63print PROPERTIES &header("${base}.properties", "#");
64
65$skipone = 0;
66
67while ($_ = <INPUT>) {
68    next if /^#/;
69
70    if (/^[ \t]*(error_table|et)[ \t]+([a-zA-Z][a-zA-Z0-9_]+) *(-?[0-9]*)/) {
71	$table_name = $2;
72	if ($3) {
73	    $table_base = $3;
74	}
75	else {
76	    $table_base = &table_base($table_name);
77	}
78	$table_item_count = 0;
79
80	print C "#include \"prerror.h\"\n";
81	print C "static const struct PRErrorMessage text[] = {\n";
82    }
83    elsif (/^[ \t]*(error_code|ec)[ \t]+([A-Z_0-9]+),[ \t]*$/) {
84	$skipone = 1;
85	$macro = $2;
86    }
87    elsif (/^[ \t]*(error_code|ec)[ \t]+([A-Z_0-9]+),[ \t]*"(.*)"[ \t]*$/) {
88	&code($2, $3);
89    }
90    elsif ($skipone && /^[ \t]*"(.*)"[ \t]*$/) {
91	&code($macro, $1);
92    }
93}
94
95print H "\n";
96print H "extern void ", $table_name, "_InitializePRErrorTable","(void);\n";
97printf H "#define ERROR_TABLE_BASE_%s (%dL)\n", $table_name, $table_base;
98
99print C "\t{0, 0}\n";
100print C "};\n\n";
101printf C "static const struct PRErrorTable et = { text, \"%s\", %dL, %d };\n",
102    $base, $table_base, $table_item_count;
103print C "\n";
104print C "void ", $table_name, "_InitializePRErrorTable", "(void) {\n";
105print C "    PR_ErrorInstallTable(&et);\n";
106print C "}\n";
107
1080;
109