util/mkerr.pl: allow module names prefixed with OSSL_ or OPENSSL_
[openssl.git] / util / mkerr.pl
index 23e4a2279764da58b16f73ac83cceeeb39cdf80a..6bc27c498b32b0fa2ec48172e797b4a598faa57a 100755 (executable)
@@ -17,6 +17,7 @@ my $rebuild      = 0;
 my $reindex      = 0;
 my $static       = 0;
 my $unref        = 0;
+my %modules         = ();
 
 my $errors       = 0;
 my @t            = localtime();
@@ -42,6 +43,13 @@ Options:
     -internal   Generate code that is to be built as part of OpenSSL itself.
                 Also scans internal list of files.
 
+    -module M   Only useful with -internal!
+                Only write files for library module M.  Whether files are
+                actually written or not depends on other options, such as
+                -rebuild.
+                Note: this option is cumulative.  If not given at all, all
+                internal modules will be considered.
+
     -nowrite    Do not write the header/source files, even if changed.
 
     -rebuild    Rebuild all header and C source files, even if there
@@ -86,6 +94,9 @@ while ( @ARGV ) {
     } elsif ( $arg eq "-unref" ) {
         $unref = 1;
         $nowrite = 1;
+    } elsif ( $arg eq "-module" ) {
+        shift @ARGV;
+        $modules{uc $ARGV[0]} = 1;
     } elsif ( $arg =~ /-*h(elp)?/ ) {
         &help();
         exit;
@@ -102,6 +113,7 @@ if ( $internal ) {
     @source = ( glob('crypto/*.c'), glob('crypto/*/*.c'),
                 glob('ssl/*.c'), glob('ssl/*/*.c') );
 } else {
+    die "-module isn't useful without -internal\n" if scalar keys %modules > 0;
     @source = @ARGV;
 }
 
@@ -121,6 +133,7 @@ my %rcodes;     # reason-name -> value
 my %ftrans;     # old name -> #define-friendly name (all caps)
 my %fcodes;     # function-name -> value
 my $statefile;  # state file with assigned reason and function codes
+my %strings;    # define -> text
 
 # Read and parse the config file
 open(IN, "$config") || die "Can't open config file $config, $!,";
@@ -167,18 +180,32 @@ if ( ! $reindex && $statefile ) {
     # maximum code used.
     while ( <STATE> ) {
         next if /^#/ || /^$/;
-        die "Bad line in $statefile:\n$_\n" unless /(\S+)\s+(\d+)/;
-        my $name = $1;
-        my $code = $2;
+        my $name;
+        my $code;
+        if ( /^(.+):(\d+):\\$/ ) {
+            $name = $1;
+            $code = $2;
+            my $next = <STATE>;
+            $next =~ s/^\s*(.*)\s*$/$1/;
+            die "Duplicate define $name" if exists $strings{$name};
+            $strings{$name} = $next;
+        } elsif ( /^(\S+):(\d+):(.*)$/ ) {
+            $name = $1;
+            $code = $2;
+            die "Duplicate define $name" if exists $strings{$name};
+            $strings{$name} = $3;
+        } else {
+            die "Bad line in $statefile:\n$_\n";
+        }
         my $lib = $name;
-        $lib =~ s/_.*//;
+        $lib =~ s/^((?:OSSL_|OPENSSL_)?[^_]{2,}).*$/$1/;
         $lib = "SSL" if $lib =~ /TLS/;
         if ( !defined $errorfile{$lib} ) {
             print "Skipping $_";
             $skippedstate++;
             next;
         }
-        if ( $name =~ /^[A-Z0-9]+_R_/ ) {
+        if ( $name =~ /^(?:OSSL_|OPENSSL_)?[A-Z0-9]{2,}_R_/ ) {
             die "$lib reason code $code collision at $name\n"
                 if $rassigned{$lib} =~ /:$code:/;
             $rassigned{$lib} .= "$code:";
@@ -186,7 +213,7 @@ if ( ! $reindex && $statefile ) {
                 $rmax{$lib} = $code if $code > $rmax{$lib};
             }
             $rcodes{$name} = $code;
-        } elsif ( $name =~ /^[A-Z0-9]+_F_/ ) {
+        } elsif ( $name =~ /^(?:OSSL_|OPENSSL_)?[A-Z0-9]{2,}_F_/ ) {
             die "$lib function code $code collision at $name\n"
                 if $fassigned{$lib} =~ /:$code:/;
             $fassigned{$lib} .= "$code:";
@@ -299,8 +326,9 @@ while ( ( my $hdr, my $lib ) = each %libinc ) {
         # pretend as we didn't use curly braces: {} -> ()
         s/\{\}/\(\)/gs;
 
-        if ( /(\w+)\s*\(\).*/s ) {    # first token prior [first] () is
-            my $name = $1;          # a function name!
+        # Last token just before the first () is a function name.
+        if ( /(\w+)\s*\(\).*/s ) {
+            my $name = $1;
             $name =~ tr/[a-z]/[A-Z]/;
             $ftrans{$name} = $1;
         } elsif ( /[\(\)]/ and not(/=/) ) {
@@ -350,7 +378,7 @@ foreach my $file ( @source ) {
             $func = $1;
         }
 
-        if ( /(([A-Z0-9]+)_F_([A-Z0-9_]+))/ ) {
+        if ( /(((?:OSSL_|OPENSSL_)?[A-Z0-9]{2,})_F_([A-Z0-9_]+))/ ) {
             next unless exists $errorfile{$2};
             next if $1 eq "BIO_F_BUFFER_CTX";
             $usedfuncs{$1} = 1;
@@ -367,7 +395,7 @@ foreach my $file ( @source ) {
             print STDERR "  Function $1 = $fcodes{$1}\n"
               if $debug;
         }
-        if ( /(([A-Z0-9]+)_R_[A-Z0-9_]+)/ ) {
+        if ( /(((?:OSSL_|OPENSSL_)?[A-Z0-9]{2,})_R_[A-Z0-9_]+)/ ) {
             next unless exists $errorfile{$2};
             $usedreasons{$1} = 1;
             if ( !exists $rcodes{$1} ) {
@@ -389,6 +417,7 @@ foreach my $lib ( keys %errorfile ) {
     if ( ! $fnew{$lib} && ! $rnew{$lib} ) {
         next unless $rebuild;
     }
+    next if scalar keys %modules > 0 && !$modules{$lib};
     next if $nowrite;
     print STDERR "$lib: $fnew{$lib} new functions\n" if $fnew{$lib};
     print STDERR "$lib: $rnew{$lib} new reasons\n" if $rnew{$lib};
@@ -494,31 +523,7 @@ EOF
     # Rewrite the C source file containing the error details.
 
     # First, read any existing reason string definitions:
-    my %err_reason_strings;
     my $cfile = $errorfile{$lib};
-    if ( open( IN, "<$cfile" ) ) {
-        my $line = "";
-        while ( <IN> ) {
-            s|\R$||;    # Better chomp
-            $_    = $line . $_;
-            $line = "";
-            if ( /{ERR_(PACK|FUNC|REASON)\(/ ) {
-                if ( /\b(${lib}_R_\w*)\b.*\"(.*)\"/ ) {
-                    $err_reason_strings{$1} = $2;
-                } elsif ( /\b${lib}_F_(\w*)\b.*\"(.*)\"/ ) {
-                    if ( !exists $ftrans{$1} && $1 ne $2 ) {
-#                       Don't print warning, too noisy. :(
-#                       print STDERR "WARNING: Mismatched/unused function $2\n";
-                        $ftrans{$1} = $2;
-                    }
-                } else {
-                    $line = $_;
-                }
-            }
-        }
-        close(IN);
-    }
-
     my $pack_lib = $internal ? "ERR_LIB_${lib}" : "0";
     my $hincf = $hfile;
     $hincf =~ s|.*include/||;
@@ -555,9 +560,15 @@ EOF
     # Add each function code: if a function name is found then use it.
     foreach my $i ( @function ) {
         my $fn;
-        $i =~ /^${lib}_F_(\S+)$/;
-        $fn = $1;
-        $fn = $ftrans{$fn} if exists $ftrans{$fn};
+        if ( exists $strings{$i} and $strings{$i} ne '' ) {
+            $fn = $strings{$i};
+            $fn = "" if $fn eq '*';
+        } else {
+            $i =~ /^${lib}_F_(\S+)$/;
+            $fn = $1;
+            $fn = $ftrans{$fn} if exists $ftrans{$fn};
+            $strings{$i} = $fn;
+        }
         my $short = "    {ERR_PACK($pack_lib, $i, 0), \"$fn\"},";
         if ( length($short) <= 80 ) {
             print OUT "$short\n";
@@ -575,12 +586,14 @@ EOF
     # Add each reason code.
     foreach my $i ( @reasons ) {
         my $rn;
-        if ( exists $err_reason_strings{$i} ) {
-            $rn = $err_reason_strings{$i};
+        if ( exists $strings{$i} ) {
+            $rn = $strings{$i};
+            $rn = "" if $rn eq '*';
         } else {
             $i =~ /^${lib}_R_(\S+)$/;
             $rn = $1;
             $rn =~ tr/_[A-Z]/ [a-z]/;
+            $strings{$i} = $rn;
         }
         my $short = "    {ERR_PACK($pack_lib, 0, $i), \"$rn\"},";
         if ( length($short) <= 80 ) {
@@ -653,7 +666,6 @@ EOF
     }
 
     close OUT;
-    undef %err_reason_strings;
 }
 
 &phase("Ending");
@@ -687,11 +699,27 @@ die "Found $errors errors, quitting" if $errors;
 if ( $newstate )  {
     open(OUT, ">$statefile.new")
         || die "Can't write $statefile.new, $!";
+    print OUT <<"EOF";
+# Copyright 1999-2017 The OpenSSL Project Authors. All Rights Reserved.
+#
+# Licensed under the OpenSSL license (the "License").  You may not use
+# this file except in compliance with the License.  You can obtain a copy
+# in the file LICENSE in the source distribution or at
+# https://www.openssl.org/source/license.html
+EOF
+    print OUT "\n# Function codes\n";
     foreach my $i ( sort keys %fcodes ) {
-        print OUT "$i $fcodes{$i}\n";
+        my $short = "$i:$fcodes{$i}:";
+        my $t = exists $strings{$i} ? $strings{$i} : "";
+        $t = "\\\n\t" . $t if length($short) + length($t) > 80;
+        print OUT "$short$t\n";
     }
+    print OUT "\n#Reason codes\n";
     foreach my $i ( sort keys %rcodes ) {
-        print OUT "$i $rcodes{$i}\n" if !exists $rextra{$i};
+        my $short = "$i:$rcodes{$i}:";
+        my $t = exists $strings{$i} ? "$strings{$i}" : "";
+        $t = "\\\n\t" . $t if length($short) + length($t) > 80;
+        print OUT "$short$t\n" if !exists $rextra{$i};
     }
     close(OUT);
     if ( $skippedstate ) {
@@ -703,4 +731,5 @@ if ( $newstate )  {
             || die "Can't rename $statefile to $statefile.new, $!";
     }
 }
+
 exit;