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