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