nspr: import 3.0 RC1 cutoff from CVS
[mozilla-nspr.git] / nsprpub / pr / src / misc / compile-et.pl
blob9f0d90bc108bd65293d0920bcfe7b357d3d062ec
1 #!/usr/bin/perl
3 # usage: compile-et input.et
5 #
6 # ***** BEGIN LICENSE BLOCK *****
7 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
9 # The contents of this file are subject to the Mozilla Public License Version
10 # 1.1 (the "License"); you may not use this file except in compliance with
11 # the License. You may obtain a copy of the License at
12 # http://www.mozilla.org/MPL/
14 # Software distributed under the License is distributed on an "AS IS" basis,
15 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16 # for the specific language governing rights and limitations under the
17 # License.
19 # The Original Code is the Netscape Portable Runtime (NSPR).
21 # The Initial Developer of the Original Code is
22 # Netscape Communications Corporation.
23 # Portions created by the Initial Developer are Copyright (C) 1998-2000
24 # the Initial Developer. All Rights Reserved.
26 # Contributor(s):
28 # Alternatively, the contents of this file may be used under the terms of
29 # either the GNU General Public License Version 2 or later (the "GPL"), or
30 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 # in which case the provisions of the GPL or the LGPL are applicable instead
32 # of those above. If you wish to allow use of your version of this file only
33 # under the terms of either the GPL or the LGPL, and not to allow others to
34 # use your version of this file under the terms of the MPL, indicate your
35 # decision by deleting the provisions above and replace them with the notice
36 # and other provisions required by the GPL or the LGPL. If you do not delete
37 # the provisions above, a recipient may use your version of this file under
38 # the terms of any one of the MPL, the GPL or the LGPL.
40 # ***** END LICENSE BLOCK *****
42 sub header
44 local($filename, $comment) = @_;
46 <<EOF
47 $comment
48 $comment $filename
49 $comment This file is automatically generated; please do not edit it.
50 EOF
53 sub table_base
55 local($name) = @_;
56 local($base) = 0;
58 for ($i = 0; $i < length($name); $i++) {
59 $base *= 64;
60 $base += index("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_", substr($name, $i, 1)) + 1;
62 $base -= 0x1000000 if ($base > 0x7fffff);
63 $base*256;
66 sub code {
67 local($macro, $text) = @_;
68 $code = $table_base + $table_item_count;
70 print H "\n";
71 print H "/* ", $text, " */\n";
72 printf H "#define %-40s (%dL)\n", $macro, $code;
74 print C "\t{\"", $macro, "\", \"", $text, "\"},\n";
76 print PROPERTIES $macro, "=", $text, "\n";
78 $table_item_count++;
82 $filename = $ARGV[0];
83 open(INPUT, "< $filename") || die "Can't read $filename: $!\n";
85 $base = "$filename";
86 $base =~ s/\.et$//;
87 $base =~ s#.*/##;
89 open(H, "> ${base}.h") || die "Can't write ${base}.h\n";
90 open(C, "> ${base}.c") || die "Can't write ${base}.c\n";
91 open(PROPERTIES, "> ${base}.properties") || die "Can't write ${base}.properties\n";
93 print H "/*\n", &header("${base}.h", " *"), " */\n";
94 print C "/*\n", &header("${base}.c", " *"), " */\n";
95 print PROPERTIES &header("${base}.properties", "#");
97 $skipone = 0;
99 while ($_ = <INPUT>) {
100 next if /^#/;
102 if (/^[ \t]*(error_table|et)[ \t]+([a-zA-Z][a-zA-Z0-9_]+) *(-?[0-9]*)/) {
103 $table_name = $2;
104 if ($3) {
105 $table_base = $3;
107 else {
108 $table_base = &table_base($table_name);
110 $table_item_count = 0;
112 print C "#include \"prerror.h\"\n";
113 print C "static const struct PRErrorMessage text[] = {\n";
115 elsif (/^[ \t]*(error_code|ec)[ \t]+([A-Z_0-9]+),[ \t]*$/) {
116 $skipone = 1;
117 $macro = $2;
119 elsif (/^[ \t]*(error_code|ec)[ \t]+([A-Z_0-9]+),[ \t]*"(.*)"[ \t]*$/) {
120 &code($2, $3);
122 elsif ($skipone && /^[ \t]*"(.*)"[ \t]*$/) {
123 &code($macro, $1);
127 print H "\n";
128 print H "extern void ", $table_name, "_InitializePRErrorTable","(void);\n";
129 printf H "#define ERROR_TABLE_BASE_%s (%dL)\n", $table_name, $table_base;
131 print C "\t{0, 0}\n";
132 print C "};\n\n";
133 printf C "static const struct PRErrorTable et = { text, \"%s\", %dL, %d };\n",
134 $base, $table_base, $table_item_count;
135 print C "\n";
136 print C "void ", $table_name, "_InitializePRErrorTable", "(void) {\n";
137 print C " PR_ErrorInstallTable(&et);\n";
138 print C "}\n";