Delete the unnecessary ERR and ERRC lines in makefiles, add some functionality
[openssl.git] / util / mkerr.pl
1 #!/usr/local/bin/perl -w
2
3 my $config = "crypto/err/openssl.ec";
4 my $debug = 0;
5 my $rebuild = 0;
6 my $static = 0;
7 my $recurse = 0;
8 my $reindex = 0;
9
10
11 while (@ARGV) {
12         my $arg = $ARGV[0];
13         if($arg eq "-conf") {
14                 shift @ARGV;
15                 $config = shift @ARGV;
16         } elsif($arg eq "-debug") {
17                 $debug = 1;
18                 shift @ARGV;
19         } elsif($arg eq "-rebuild") {
20                 $rebuild = 1;
21                 shift @ARGV;
22         } elsif($arg eq "-recurse") {
23                 $recurse = 1;
24                 shift @ARGV;
25         } elsif($arg eq "-reindex") {
26                 $reindex = 1;
27                 shift @ARGV;
28         } elsif($arg eq "-static") {
29                 $static = 1;
30                 shift @ARGV;
31         } else {
32                 last;
33         }
34 }
35
36 if($recurse) {
37         @source = (<crypto/*.c>, <crypto/*/*.c>, ,<rsaref/*.c>, <ssl/*.c>);
38 } else {
39         @source = @ARGV;
40 }
41
42 # Read in the config file
43
44 open(IN, "<$config") || die "Can't open config file $config";
45
46 # Parse config file
47
48 while(<IN>)
49 {
50         if(/^L\s+(\S+)\s+(\S+)\s+(\S+)/) {
51                 $hinc{$1} = $2;
52                 $cskip{$3} = $1;
53                 if($3 ne "NONE") {
54                         $csrc{$1} = $3;
55                         $fmax{$1} = 99;
56                         $rmax{$1} = 99;
57                         $fnew{$1} = 0;
58                         $rnew{$1} = 0;
59                 }
60         } elsif (/^F\s+(\S+)/) {
61         # Add extra function with $1
62         } elsif (/^R\s+(\S+)\s+(\S+)/) {
63                 $rextra{$1} = $2;
64                 $rcodes{$1} = $2;
65         # Add extra reason with $1, value $2
66         }
67 }
68
69 close IN;
70
71 # Scan each header file in turn and make a list of error codes
72 # and function names
73
74 while (($lib, $hdr) = each %hinc)
75 {
76         next if($hdr eq "NONE");
77         print STDERR "Scanning header file $hdr\n" if $debug; 
78         open(IN, "<$hdr") || die "Can't open Header file $hdr\n";
79         my $line = "", $def= "";
80         while(<IN>) {
81             last if(/BEGIN\s+ERROR\s+CODES/);
82             if ($line ne '') {
83                 $_ = $line . $_;
84                 $line = '';
85             }
86
87             if (/\\$/) {
88                 $line = $_;
89                 next;
90             }
91
92             $cpp = 1 if /^#.*ifdef.*cplusplus/;  # skip "C" declaration
93             if ($cpp) {
94                 $cpp = 0 if /^#.*endif/;
95                 next;
96             }
97
98             next if (/^#/);                      # skip preprocessor directives
99
100             s/\/\*.*?\*\///gs;                   # ignore comments
101             s/{[^{}]*}//gs;                      # ignore {} blocks
102
103             if (/{|\/\*/) { # Add a } so editor works...
104                 $line = $_;
105             } else {
106                 $def .= $_;
107             }
108         }
109
110         foreach (split /;/, $def) {
111             s/^[\n\s]*//g;
112             s/[\n\s]*$//g;
113             next if (/\w+\W+(\w+)\W*\(\s*\)$/s); # K&R C
114             next if (/\(\*(\w*)\([^\)]+/);
115             if (/\w+\W+\w+\W*\(.*\)$/s) {
116                 while (not /\(\)$/s) {
117                     s/[^\(\)]*\)$/\)/s;
118                     s/\([^\(\)]*\)\)$/\)/s;
119                 }
120                 s/\(void\)//;
121                 /(\w+)\W*\(\)/s;
122                 next if(/typedef\W/);
123                 my $name = $1;
124                 $name =~ tr/[a-z]/[A-Z]/;
125                 $ftrans{$name} = $1;
126                 
127             } elsif (/\(/ and not (/=/ or /DECLARE_STACK/)) {
128                 print STDERR "Header $hdr: cannot parse: $_;\n";
129             }
130         }
131
132         next if $reindex;
133
134         # Scan function and reason codes and store them: keep a note of the
135         # maximum code used.
136
137         while(<IN>) {
138                 if(/^#define\s+(\S+)\s+(\S+)/) {
139                         $name = $1;
140                         $code = $2;
141                         unless($name =~ /^${lib}_([RF])_(\w+)$/) {
142                                 print STDERR "Invalid error code $name\n";
143                                 next;
144                         }
145                         if($1 eq "R") {
146                                 $rcodes{$name} = $code;
147                                 if(!(exists $rextra{$name}) &&
148                                          ($code > $rmax{$lib}) ) {
149                                         $rmax{$lib} = $code;
150                                 }
151                         } else {
152                                 if($code > $fmax{$lib}) {
153                                         $fmax{$lib} = $code;
154                                 }
155                                 $fcodes{$name} = $code;
156                         }
157                 }
158         }
159         close IN;
160 }
161
162 # Scan each C source file and look for function and reason codes
163 # This is done by looking for strings that "look like" function or
164 # reason codes: basically anything consisting of all upper case and
165 # numerics which has _F_ or _R_ in it and which has the name of an
166 # error library at the start. This seems to work fine except for the
167 # oddly named structure BIO_F_CTX which needs to be ignored.
168 # If a code doesn't exist in list compiled from headers then mark it
169 # with the value "X" as a place holder to give it a value later.
170 # Store all function and reason codes found in %ufcodes and %urcodes
171 # so all those unreferenced can be printed out.
172
173
174 foreach $file (@source) {
175         # Don't parse the error source file.
176         next if exists $cskip{$file};
177         open(IN, "<$file") || die "Can't open source file $file\n";
178         while(<IN>) {
179                 if(/(([A-Z0-9]+)_F_([A-Z0-9_]+))/) {
180                         next unless exists $csrc{$2};
181                         next if($1 eq "BIO_F_BUFFER_CTX");
182                         $ufcodes{$1} = 1;
183                         if(!exists $fcodes{$1}) {
184                                 $fcodes{$1} = "X";
185                                 $fnew{$2}++;
186                         }
187                         $notrans{$1} = 1 unless exists $ftrans{$3};
188                 }
189                 if(/(([A-Z0-9]+)_R_[A-Z0-9_]+)/) {
190                         next unless exists $csrc{$2};
191                         $urcodes{$1} = 1;
192                         if(!exists $rcodes{$1}) {
193                                 $rcodes{$1} = "X";
194                                 $rnew{$2}++;
195                         }
196                 } 
197         }
198         close IN;
199 }
200
201 # Now process each library in turn.
202
203 foreach $lib (keys %csrc)
204 {
205         my $hfile = $hinc{$lib};
206         my $cfile = $csrc{$lib};
207         if(!$fnew{$lib} && !$rnew{$lib}) {
208                 print STDERR "$lib:\t\tNo new error codes\n";
209                 next unless $rebuild;
210         } else {
211                 print STDERR "$lib:\t\t$fnew{$lib} New Functions,";
212                 print STDERR " $rnew{$lib} New Reasons.\n";
213
214         }
215
216         # If we get here then we have some new error codes so we
217         # need to rebuild the header file and C file.
218
219         # Make a sorted list of error and reason codes for later use.
220
221         my @function = sort grep(/^${lib}_/,keys %fcodes);
222         my @reasons = sort grep(/^${lib}_/,keys %rcodes);
223
224         # Rewrite the header file
225
226         open(IN, "<$hfile") || die "Can't Open Header File $hfile\n";
227
228         # Copy across the old file
229         while(<IN>) {
230                 push @out, $_;
231                 last if (/BEGIN ERROR CODES/);
232         }
233         close IN;
234
235         open (OUT, ">$hfile") || die "Can't Open File $hfile for writing\n";
236
237         print OUT @out;
238         undef @out;
239         print OUT <<"EOF";
240 /* The following lines are auto generated by the script mkerr.pl. Any changes
241  * made after this point may be overwritten when the script is next run.
242  */
243
244 /* Error codes for the $lib functions. */
245
246 /* Function codes. */
247 EOF
248
249         foreach $i (@function) {
250                 $z=6-int(length($i)/8);
251                 if($fcodes{$i} eq "X") {
252                         $fcodes{$i} = ++$fmax{$lib};
253                         print STDERR "New Function code $i\n" if $debug;
254                 }
255                 printf OUT "#define $i%s $fcodes{$i}\n","\t" x $z;
256         }
257
258         print OUT "\n/* Reason codes. */\n";
259
260         foreach $i (@reasons) {
261                 $z=6-int(length($i)/8);
262                 if($rcodes{$i} eq "X") {
263                         $rcodes{$i} = ++$rmax{$lib};
264                         print STDERR "New Reason code   $i\n" if $debug;
265                 }
266                 printf OUT "#define $i%s $rcodes{$i}\n","\t" x $z;
267         }
268         print OUT <<"EOF";
269
270 #ifdef  __cplusplus
271 }
272 #endif
273 #endif
274
275 EOF
276         close OUT;
277
278         # Rewrite the C source file containing the error details.
279
280         $hfile =~ /([^\/]+)$/;
281         my $hincf = $1;
282
283         open (OUT,">$cfile") || die "Can't open $cfile for writing";
284
285         print OUT <<"EOF";
286 /* $cfile */
287 /* ====================================================================
288  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
289  *
290  * Redistribution and use in source and binary forms, with or without
291  * modification, are permitted provided that the following conditions
292  * are met:
293  *
294  * 1. Redistributions of source code must retain the above copyright
295  *    notice, this list of conditions and the following disclaimer. 
296  *
297  * 2. Redistributions in binary form must reproduce the above copyright
298  *    notice, this list of conditions and the following disclaimer in
299  *    the documentation and/or other materials provided with the
300  *    distribution.
301  *
302  * 3. All advertising materials mentioning features or use of this
303  *    software must display the following acknowledgment:
304  *    "This product includes software developed by the OpenSSL Project
305  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
306  *
307  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
308  *    endorse or promote products derived from this software without
309  *    prior written permission. For written permission, please contact
310  *    openssl-core\@OpenSSL.org.
311  *
312  * 5. Products derived from this software may not be called "OpenSSL"
313  *    nor may "OpenSSL" appear in their names without prior written
314  *    permission of the OpenSSL Project.
315  *
316  * 6. Redistributions of any form whatsoever must retain the following
317  *    acknowledgment:
318  *    "This product includes software developed by the OpenSSL Project
319  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
320  *
321  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
322  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
323  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
324  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
325  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
326  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
327  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
328  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
329  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
330  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
331  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
332  * OF THE POSSIBILITY OF SUCH DAMAGE.
333  * ====================================================================
334  *
335  * This product includes cryptographic software written by Eric Young
336  * (eay\@cryptsoft.com).  This product includes software written by Tim
337  * Hudson (tjh\@cryptsoft.com).
338  *
339  */
340
341 /* NOTE: this file was auto generated by the mkerr.pl script: any changes
342  * made to it will be overwritten when the script next updates this file.
343  */
344
345 #include <stdio.h>
346 #include <openssl/err.h>
347 #include <openssl/$hincf>
348
349 /* BEGIN ERROR CODES */
350 #ifndef NO_ERR
351 static ERR_STRING_DATA ${lib}_str_functs[]=
352         {
353 EOF
354         # Add each function code: if a function name is found then use it.
355         foreach $i (@function) {
356                 my $fn;
357                 $i =~ /^${lib}_F_(\S+)$/;
358                 $fn = $1;
359                 if(exists $ftrans{$fn}) {
360                         $fn = $ftrans{$fn};
361                 }
362                 print OUT "{ERR_PACK(0,$i,0),\t\"$fn\"},\n";
363         }
364         print OUT <<"EOF";
365 {0,NULL}
366         };
367
368 static ERR_STRING_DATA ${lib}_str_reasons[]=
369         {
370 EOF
371         # Add each reason code.
372         foreach $i (@reasons) {
373                 my $rn;
374                 my $nspc = 0;
375                 $i =~ /^${lib}_R_(\S+)$/;
376                 $rn = $1;
377                 $rn =~ tr/_[A-Z]/ [a-z]/;
378                 $nspc = 40 - length($i) unless length($i) > 40;
379                 $nspc = " " x $nspc;
380                 print OUT "{${i}${nspc},\"$rn\"},\n";
381         }
382 if($static) {
383         print OUT <<"EOF";
384 {0,NULL}
385         };
386
387 #endif
388
389 void ERR_load_${lib}_strings(void)
390         {
391         static int init=1;
392
393         if (init)
394                 {
395                 init=0;
396 #ifndef NO_ERR
397                 ERR_load_strings(ERR_LIB_${lib},${lib}_str_functs);
398                 ERR_load_strings(ERR_LIB_${lib},${lib}_str_reasons);
399 #endif
400
401                 }
402         }
403 EOF
404 } else {
405         print OUT <<"EOF";
406 {0,NULL}
407         };
408
409 #endif
410
411 #ifdef ${lib}_LIB_NAME
412 static ERR_STRING_DATA ${lib}_lib_name[]=
413         {
414 {0      ,${lib}_LIB_NAME},
415 {0,NULL}
416         };
417 #endif
418
419
420 int ${lib}_lib_error_code=0;
421
422 void ERR_load_${lib}_strings(void)
423         {
424         static int init=1;
425
426         if (${lib}_lib_error_code == 0)
427                 ${lib}_lib_error_code=ERR_get_next_error_library();
428
429         if (init)
430                 {
431                 init=0;
432 #ifndef NO_ERR
433                 ERR_load_strings(${lib}_lib_error_code,${lib}_str_functs);
434                 ERR_load_strings(${lib}_lib_error_code,${lib}_str_reasons);
435 #endif
436
437 #ifdef ${lib}_LIB_NAME
438                 ${lib}_lib_name->error = ERR_PACK(${lib}_lib_error_code,0,0);
439                 ERR_load_strings(0,${lib}_lib_name);
440 #endif;
441                 }
442         }
443
444 void ERR_${lib}_error(int function, int reason, char *file, int line)
445         {
446         if (${lib}_lib_error_code == 0)
447                 ${lib}_lib_error_code=ERR_get_next_error_library();
448         ERR_PUT_error(${lib}_lib_error_code,function,reason,file,line);
449         }
450 EOF
451
452 }
453
454         close OUT;
455
456 }
457
458 if($debug && defined(%notrans)) {
459         print STDERR "The following function codes were not translated:\n";
460         foreach(sort keys %notrans)
461         {
462                 print STDERR "$_\n";
463         }
464 }
465
466 # Make a list of unreferenced function and reason codes
467
468 foreach (keys %fcodes) {
469         push (@funref, $_) unless exists $ufcodes{$_};
470 }
471
472 foreach (keys %rcodes) {
473         push (@runref, $_) unless exists $urcodes{$_};
474 }
475
476 if($debug && defined(@funref) ) {
477         print STDERR "The following function codes were not referenced:\n";
478         foreach(sort @funref)
479         {
480                 print STDERR "$_\n";
481         }
482 }
483
484 if($debug && defined(@runref) ) {
485         print STDERR "The following reason codes were not referenced:\n";
486         foreach(sort @runref)
487         {
488                 print STDERR "$_\n";
489         }
490 }