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