Accept NULL in *_free.
[openssl.git] / crypto / err / err_genc.pl
1 #!/usr/local/bin/perl
2
3 if ($ARGV[0] eq "-s") { $static=1; shift @ARGV; }
4
5 ($#ARGV == 1) || die "usage: $0 [-s] <header file> <output C file>\n";
6 open(IN,"<$ARGV[0]") || die "unable to open $ARGV[0]:$!\n";
7 open(STDOUT,">$ARGV[1]") || die "unable to open $ARGV[1]:$!\n";
8
9 $Func=0;
10 $Reas=0;
11 $fuction{'FOPEN'}='fopen';
12 while (<IN>)
13         {
14         if (/(\S+)\s*\(\);/)
15                 {
16                 $t=$1;
17                 $t =~ s/\*//;
18                 ($upper=$t) =~ tr/a-z/A-Z/;
19                 $fuction{$upper}=$t;
20                 }
21         next unless (/^#define\s+(\S+)\s/);
22
23         $o=$1;
24         if ($o =~ /^([^_]+)_F_(.*)/)
25                 {
26                 $type=$1;
27                 $Func++;
28                 $n=$2;
29                 $n=$fuction{$n} if (defined($fuction{$n}));
30                 $out{$1."_str_functs"}.=
31                         sprintf("{ERR_PACK(0,%s,0),\t\"$n\"},\n",$o);
32                 }
33         elsif ($o =~ /^([^_]+)_R_(.*)/)
34                 {
35                 $type=$1;
36                 $Reas++;
37                 $r=$2;
38                 $r =~ tr/A-Z_/a-z /;
39                 $pkg{$type."_str_reasons"}=$type;
40                 $out{$type."_str_reasons"}.=sprintf("{%-40s,\"$r\"},\n",$o);
41                 }
42         elsif ($ARGV[0] =~ /rsaref/ && $o =~ /^RE_(.*)/)
43                 {
44                 $type="RSAREF";
45                 $Reas++;
46                 $r=$1;
47                 $r =~ tr/A-Z_/a-z /;
48                 $pkg{$type."_str_reasons"}=$type;
49                 $out{$type."_str_reasons"}.=sprintf("{%-40s,\"$r\"},\n",$o);
50                 }
51         }
52 close(IN);
53
54 &header($type,$ARGV[0]);
55
56 foreach (sort keys %out)
57         {
58         print "static ERR_STRING_DATA ${_}[]=\n\t{\n";
59         print $out{$_};
60         print "{0,NULL},\n";
61         print "\t};\n\n";
62         }
63 print "#endif\n";
64
65 if ($static)
66         { $lib="ERR_LIB_$type"; }
67 else
68         { 
69         print <<"EOF";
70
71 #ifdef ${type}_LIB_NAME
72 static ERR_STRING_DATA ${type}_lib_name[]=
73         {
74 {0      ,${type}_LIB_NAME},
75 {0,NULL}
76         };
77 #endif
78
79 EOF
80         $lib="${type}_lib_error_code"; 
81
82
83
84         }
85
86 $str="";
87 $str.="#ifndef NO_ERR\n";
88 $str.="\t\tERR_load_strings($lib,${type}_str_functs);\n" if $Func;
89 $str.="\t\tERR_load_strings($lib,${type}_str_reasons);\n" if $Reas;
90 $str.="#endif\n";
91
92 if (!$static)
93         {
94 print <<"EOF";
95
96 int ${type}_lib_error_code=0;
97
98 void ERR_load_${type}_strings()
99         {
100         static int init=1;
101
102         if (${type}_lib_error_code == 0)
103                 ${type}_lib_error_code=ERR_get_next_error_library();
104
105         if (init)
106                 {
107                 init=0;
108 $str
109 #ifdef ${type}_LIB_NAME
110                 ${type}_lib_name->error = ERR_PACK(${type}_lib_error_code,0,0);
111                 ERR_load_strings(0,${type}_lib_name);
112 #endif;
113                 }
114         }
115
116 void ERR_${type}_error(function,reason,file,line)
117 int function;
118 int reason;
119 char *file;
120 int line;
121         {
122         if (${type}_lib_error_code == 0)
123                 ${type}_lib_error_code=ERR_get_next_error_library();
124         ERR_PUT_error(${type}_lib_error_code,function,reason,file,line);
125         }
126 EOF
127         }
128 else # $static
129         {
130         print <<"EOF";
131
132 void ERR_load_${type}_strings()
133         {
134         static int init=1;
135
136         if (init)
137                 {
138                 init=0;
139 $str
140                 }
141         }
142 EOF
143         }
144
145 sub header
146         {
147         ($type,$header)=@_;
148
149         ($lc=$type) =~ tr/A-Z/a-z/;
150         $header =~ s/^.*\///;
151
152         print "/* lib/$lc/${lc}\_err.c */\n";
153         print <<'EOF';
154 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
155  * All rights reserved.
156  *
157  * This package is an SSL implementation written
158  * by Eric Young (eay@cryptsoft.com).
159  * The implementation was written so as to conform with Netscapes SSL.
160  * 
161  * This library is free for commercial and non-commercial use as long as
162  * the following conditions are aheared to.  The following conditions
163  * apply to all code found in this distribution, be it the RC4, RSA,
164  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
165  * included with this distribution is covered by the same copyright terms
166  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
167  * 
168  * Copyright remains Eric Young's, and as such any Copyright notices in
169  * the code are not to be removed.
170  * If this package is used in a product, Eric Young should be given attribution
171  * as the author of the parts of the library used.
172  * This can be in the form of a textual message at program startup or
173  * in documentation (online or textual) provided with the package.
174  * 
175  * Redistribution and use in source and binary forms, with or without
176  * modification, are permitted provided that the following conditions
177  * are met:
178  * 1. Redistributions of source code must retain the copyright
179  *    notice, this list of conditions and the following disclaimer.
180  * 2. Redistributions in binary form must reproduce the above copyright
181  *    notice, this list of conditions and the following disclaimer in the
182  *    documentation and/or other materials provided with the distribution.
183  * 3. All advertising materials mentioning features or use of this software
184  *    must display the following acknowledgement:
185  *    "This product includes cryptographic software written by
186  *     Eric Young (eay@cryptsoft.com)"
187  *    The word 'cryptographic' can be left out if the rouines from the library
188  *    being used are not cryptographic related :-).
189  * 4. If you include any Windows specific code (or a derivative thereof) from 
190  *    the apps directory (application code) you must include an acknowledgement:
191  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
192  * 
193  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
194  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
195  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
196  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
197  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
198  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
200  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
201  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
202  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
203  * SUCH DAMAGE.
204  * 
205  * The licence and distribution terms for any publically available version or
206  * derivative of this code cannot be changed.  i.e. this code cannot simply be
207  * copied and put under another distribution licence
208  * [including the GNU Public Licence.]
209  */
210 EOF
211
212         print "#include <stdio.h>\n";
213         print "#include \"err.h\"\n";
214         print "#include \"$header\"\n";
215         print "\n/* BEGIN ERROR CODES */\n";
216         print "#ifndef NO_ERR\n";
217         }
218