1e1051a39Sopenharmony_ci#! /usr/bin/env perl
2e1051a39Sopenharmony_ci
3e1051a39Sopenharmony_ciuse strict;
4e1051a39Sopenharmony_ciuse warnings;
5e1051a39Sopenharmony_ci
6e1051a39Sopenharmony_cimy $debug = $ENV{DEBUG};
7e1051a39Sopenharmony_ci
8e1051a39Sopenharmony_ci# This scripts finds DEPRECATEDIN declarations and converts them to
9e1051a39Sopenharmony_ci# C declarations with the corresponding OSSL_DEPRECATEDIN attribute
10e1051a39Sopenharmony_ci# macro.  It also makes sure they are guarded them with a corresponding
11e1051a39Sopenharmony_ci# '#ifndef OPENSSL_NO_DEPRECATED', and pays extra attention to only have
12e1051a39Sopenharmony_ci# one such guard around a group of deprecations for the same version.
13e1051a39Sopenharmony_ci
14e1051a39Sopenharmony_cimy $parens_re =
15e1051a39Sopenharmony_ci    qr/(
16e1051a39Sopenharmony_ci           \(                   # The start of what we recurse on
17e1051a39Sopenharmony_ci               (?:
18e1051a39Sopenharmony_ci                   (?> [^()]+ )     # Non-parens, without backtracking
19e1051a39Sopenharmony_ci               |
20e1051a39Sopenharmony_ci                   (?-1)            # Recurse to start of parens group
21e1051a39Sopenharmony_ci               )*
22e1051a39Sopenharmony_ci           \)                   # The end of what we recurse on
23e1051a39Sopenharmony_ci       )/x;
24e1051a39Sopenharmony_ci
25e1051a39Sopenharmony_cimy $deprecated_kw_re = qr/(DEPRECATEDIN)_(\d+_\d+(?:_\d+)?)/;
26e1051a39Sopenharmony_cimy $deprecated_re =
27e1051a39Sopenharmony_ci    qr/
28e1051a39Sopenharmony_ci          $deprecated_kw_re
29e1051a39Sopenharmony_ci          \(
30e1051a39Sopenharmony_ci          (
31e1051a39Sopenharmony_ci              (?:
32e1051a39Sopenharmony_ci                  (?> [^()]+ )
33e1051a39Sopenharmony_ci              |
34e1051a39Sopenharmony_ci                  $parens_re
35e1051a39Sopenharmony_ci              )*
36e1051a39Sopenharmony_ci          )
37e1051a39Sopenharmony_ci          \)
38e1051a39Sopenharmony_ci    /x;
39e1051a39Sopenharmony_cimy $headertext;
40e1051a39Sopenharmony_ci{
41e1051a39Sopenharmony_ci    local $/;
42e1051a39Sopenharmony_ci    $headertext = <STDIN>;
43e1051a39Sopenharmony_ci}
44e1051a39Sopenharmony_ci$headertext =~ s/\R/\n/g;
45e1051a39Sopenharmony_ci
46e1051a39Sopenharmony_cimy $cppspaces = '';
47e1051a39Sopenharmony_cimy $last_cppspaces = '';
48e1051a39Sopenharmony_cimy $currentguard = "";
49e1051a39Sopenharmony_cimy $cnt = 0;
50e1051a39Sopenharmony_ciwhile ( $headertext =~ m/(.*?)                          # $1
51e1051a39Sopenharmony_ci                         (                              # $2
52e1051a39Sopenharmony_ci                             ^
53e1051a39Sopenharmony_ci                             (?|
54e1051a39Sopenharmony_ci                                 (\#)(\s*)(if)?.*?      # $3 ('#')
55e1051a39Sopenharmony_ci                                                        # $4 (spaces)
56e1051a39Sopenharmony_ci                                                        # $5 ('if'?)
57e1051a39Sopenharmony_ci                             |
58e1051a39Sopenharmony_ci                                 \s*$deprecated_kw_re\(.*?
59e1051a39Sopenharmony_ci                                                        # $3 = 'DEPRECATEDIN'
60e1051a39Sopenharmony_ci                                                        # $4 (vers)
61e1051a39Sopenharmony_ci                             )
62e1051a39Sopenharmony_ci                             \n
63e1051a39Sopenharmony_ci                         )
64e1051a39Sopenharmony_ci                        /msx ) {
65e1051a39Sopenharmony_ci    my $before = $1;
66e1051a39Sopenharmony_ci    my $capture = $2;
67e1051a39Sopenharmony_ci    my $after = $';
68e1051a39Sopenharmony_ci
69e1051a39Sopenharmony_ci    my $deprecation = '';
70e1051a39Sopenharmony_ci    my $test = $capture.$';
71e1051a39Sopenharmony_ci    my $version = undef;
72e1051a39Sopenharmony_ci
73e1051a39Sopenharmony_ci    print STDERR "DEBUG: captured:\n$capture"
74e1051a39Sopenharmony_ci        if $debug;
75e1051a39Sopenharmony_ci
76e1051a39Sopenharmony_ci    if ($3 eq '#') {
77e1051a39Sopenharmony_ci        # Treat preprocessor lines (count spaces)
78e1051a39Sopenharmony_ci        $cppspaces = $4;
79e1051a39Sopenharmony_ci        $cppspaces .= ' ' if (defined $5 && $5 eq 'if');
80e1051a39Sopenharmony_ci        print STDERR "DEBUG: cpp spaces set to ", length($cppspaces), "\n"
81e1051a39Sopenharmony_ci            if $debug;
82e1051a39Sopenharmony_ci        $before .= $capture;
83e1051a39Sopenharmony_ci    } elsif ($test =~ m/^\s*$deprecated_re(.*?\n)/) {
84e1051a39Sopenharmony_ci        # Treat DEPRECATEDIN_...
85e1051a39Sopenharmony_ci        $version = $2;
86e1051a39Sopenharmony_ci        $deprecation = "OSSL_DEPRECATEDIN_$version $3;$5";
87e1051a39Sopenharmony_ci        $after = $';            # Different from the previous!
88e1051a39Sopenharmony_ci        print STDERR "DEBUG: changed to:\n$deprecation\n"
89e1051a39Sopenharmony_ci            if $debug;
90e1051a39Sopenharmony_ci    }
91e1051a39Sopenharmony_ci
92e1051a39Sopenharmony_ci    if ($currentguard ne ''
93e1051a39Sopenharmony_ci        && (defined $version && $currentguard ne $version
94e1051a39Sopenharmony_ci            || $before !~ /^\s*$/s)) {
95e1051a39Sopenharmony_ci        print "#${last_cppspaces}endif\n";
96e1051a39Sopenharmony_ci        $cppspaces = substr($cppspaces, 0, -1);
97e1051a39Sopenharmony_ci        $currentguard = "";
98e1051a39Sopenharmony_ci    }
99e1051a39Sopenharmony_ci    print $before;
100e1051a39Sopenharmony_ci    if ($deprecation) {
101e1051a39Sopenharmony_ci        if ($currentguard eq '' && defined $version) {
102e1051a39Sopenharmony_ci            $currentguard = $version;
103e1051a39Sopenharmony_ci            print "#${cppspaces}ifndef OPENSSL_NO_DEPRECATED_$version\n";
104e1051a39Sopenharmony_ci            $last_cppspaces = $cppspaces;
105e1051a39Sopenharmony_ci            $cppspaces .= ' ';
106e1051a39Sopenharmony_ci            print STDERR "DEBUG: cpp spaces set to ", length($cppspaces), "\n"
107e1051a39Sopenharmony_ci                if $debug;
108e1051a39Sopenharmony_ci        }
109e1051a39Sopenharmony_ci        print $deprecation;
110e1051a39Sopenharmony_ci    }
111e1051a39Sopenharmony_ci    $headertext = $after;
112e1051a39Sopenharmony_ci}
113e1051a39Sopenharmony_ciprint "#endif\n" if $currentguard ne '';
114e1051a39Sopenharmony_ciprint $headertext;
115