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