| fbrosson | 533407a | 2018-04-04 21:44:29 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl | 
| Simon Butcher | 6dc7c9c | 2016-06-19 22:49:58 +0100 | [diff] [blame] | 2 | # | 
|  | 3 | # This file is part of mbed TLS (https://tls.mbed.org) | 
|  | 4 | # | 
|  | 5 | # Copyright (c) 2015-2016, ARM Limited, All Rights Reserved | 
|  | 6 | # | 
|  | 7 | # Purpose | 
|  | 8 | # | 
|  | 9 | # This script migrates application source code from the mbed TLS 1.3 API to the | 
|  | 10 | # mbed TLS 2.0 API. | 
|  | 11 | # | 
|  | 12 | # The script processes the given source code and renames identifiers - functions | 
|  | 13 | # types, enums etc, as | 
|  | 14 | # | 
|  | 15 | # Usage:  rename.pl [-f datafile] [-s] [--] [filenames...] | 
|  | 16 | # | 
| Manuel Pégourié-Gonnard | b20a70f | 2015-04-08 14:56:51 +0200 | [diff] [blame] | 17 |  | 
| Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 18 | use warnings; | 
|  | 19 | use strict; | 
|  | 20 |  | 
|  | 21 | use utf8; | 
| Simon Butcher | 6dc7c9c | 2016-06-19 22:49:58 +0100 | [diff] [blame] | 22 | use Path::Class; | 
| Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 23 | use open qw(:std utf8); | 
|  | 24 |  | 
| Manuel Pégourié-Gonnard | b20a70f | 2015-04-08 14:56:51 +0200 | [diff] [blame] | 25 | my $usage = "Usage: $0 [-f datafile] [-s] [--] [filenames...]\n"; | 
| Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 26 |  | 
| Manuel Pégourié-Gonnard | 2aa81cc | 2015-04-10 11:19:10 +0200 | [diff] [blame] | 27 | (my $datafile = $0) =~ s/rename.pl$/data_files\/rename-1.3-2.0.txt/; | 
| Manuel Pégourié-Gonnard | 88323c7 | 2015-04-03 14:38:02 +0200 | [diff] [blame] | 28 | my $do_strings = 0; | 
| Manuel Pégourié-Gonnard | b20a70f | 2015-04-08 14:56:51 +0200 | [diff] [blame] | 29 |  | 
|  | 30 | while( @ARGV && $ARGV[0] =~ /^-/ ) { | 
|  | 31 | my $opt = shift; | 
|  | 32 | if( $opt eq '--' ) { | 
|  | 33 | last; | 
|  | 34 | } elsif( $opt eq '-f' ) { | 
|  | 35 | $datafile = shift; | 
|  | 36 | } elsif( $opt eq '-s' ) { | 
|  | 37 | $do_strings = 1; shift; | 
|  | 38 | } else { | 
|  | 39 | die $usage; | 
|  | 40 | } | 
| Manuel Pégourié-Gonnard | 88323c7 | 2015-04-03 14:38:02 +0200 | [diff] [blame] | 41 | } | 
|  | 42 |  | 
| Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 43 | my %subst; | 
| Manuel Pégourié-Gonnard | 2aa81cc | 2015-04-10 11:19:10 +0200 | [diff] [blame] | 44 | open my $nfh, '<', $datafile or die "Could not read $datafile\n"; | 
|  | 45 | my $ident = qr/[_A-Za-z][_A-Za-z0-9]*/; | 
|  | 46 | while( my $line = <$nfh> ) { | 
|  | 47 | chomp $line; | 
|  | 48 | my ( $old, $new ) = ( $line =~ /^($ident)\s+($ident)$/ ); | 
|  | 49 | if( ! $old || ! $new ) { | 
|  | 50 | die "$0: $datafile:$.: bad input '$line'\n"; | 
|  | 51 | } | 
| Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 52 | $subst{$old} = $new; | 
|  | 53 | } | 
| Manuel Pégourié-Gonnard | 2aa81cc | 2015-04-10 11:19:10 +0200 | [diff] [blame] | 54 | close $nfh or die; | 
| Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 55 |  | 
| Manuel Pégourié-Gonnard | c559f04 | 2015-04-08 20:10:16 +0200 | [diff] [blame] | 56 | my $string = qr/"(?:\\.|[^\\"])*"/; | 
| Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 57 | my $space = qr/\s+/; | 
|  | 58 | my $idnum = qr/[a-zA-Z0-9_]+/; | 
| Manuel Pégourié-Gonnard | 5f29a73 | 2015-04-20 12:27:12 +0100 | [diff] [blame] | 59 | my $symbols = qr/[-!#\$%&'()*+,.\/:;<=>?@[\\\]^_`{|}~]+|"/; | 
| Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 60 |  | 
| Simon Butcher | 6dc7c9c | 2016-06-19 22:49:58 +0100 | [diff] [blame] | 61 | my $lib_include_dir = dir($0)->parent->parent->subdir('include', 'mbedtls'); | 
|  | 62 | my $lib_source_dir = dir($0)->parent->parent->subdir('library'); | 
|  | 63 |  | 
| Manuel Pégourié-Gonnard | 88323c7 | 2015-04-03 14:38:02 +0200 | [diff] [blame] | 64 | # if we replace inside strings, we don't consider them a token | 
|  | 65 | my $token = $do_strings ?         qr/$space|$idnum|$symbols/ | 
|  | 66 | : qr/$string|$space|$idnum|$symbols/; | 
|  | 67 |  | 
| Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 68 | my %warnings; | 
|  | 69 |  | 
| Simon Butcher | 6dc7c9c | 2016-06-19 22:49:58 +0100 | [diff] [blame] | 70 | # If no files were passed, exit... | 
|  | 71 | if ( not defined($ARGV[0]) ){ die $usage; } | 
|  | 72 |  | 
| Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 73 | while( my $filename = shift ) | 
|  | 74 | { | 
|  | 75 | print STDERR "$filename... "; | 
| Simon Butcher | 6dc7c9c | 2016-06-19 22:49:58 +0100 | [diff] [blame] | 76 |  | 
|  | 77 | if( dir($filename)->parent eq $lib_include_dir || | 
|  | 78 | dir($filename)->parent eq $lib_source_dir ) | 
|  | 79 | { | 
|  | 80 | die "Script cannot be executed on the mbed TLS library itself."; | 
|  | 81 | } | 
|  | 82 |  | 
| Manuel Pégourié-Gonnard | f7d945f | 2015-04-03 15:21:50 +0200 | [diff] [blame] | 83 | if( -d $filename ) { print STDERR "skip (directory)\n"; next } | 
| Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 84 |  | 
|  | 85 | open my $rfh, '<', $filename or die; | 
|  | 86 | my @lines = <$rfh>; | 
|  | 87 | close $rfh or die; | 
|  | 88 |  | 
|  | 89 | my @out; | 
|  | 90 | for my $line (@lines) { | 
| Manuel Pégourié-Gonnard | fb9f2a0 | 2015-04-03 18:37:07 +0200 | [diff] [blame] | 91 | if( $line =~ /#include/ ) { | 
|  | 92 | $line =~ s/polarssl/mbedtls/; | 
|  | 93 | $line =~ s/POLARSSL/MBEDTLS/; | 
|  | 94 | push( @out, $line ); | 
|  | 95 | next; | 
|  | 96 | } | 
|  | 97 |  | 
| Manuel Pégourié-Gonnard | 88323c7 | 2015-04-03 14:38:02 +0200 | [diff] [blame] | 98 | my @words = ($line =~ /$token/g); | 
| Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 99 | my $checkline = join '', @words; | 
|  | 100 | if( $checkline eq $line ) { | 
|  | 101 | my @new = map { exists $subst{$_} ? $subst{$_} : $_ } @words; | 
|  | 102 | push( @out, join '', @new ); | 
|  | 103 | } else { | 
|  | 104 | $warnings{$filename} = [] unless $warnings{$filename}; | 
|  | 105 | push @{ $warnings{$filename} }, $line; | 
|  | 106 | push( @out, $line ); | 
|  | 107 | } | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | open my $wfh, '>', $filename or die; | 
|  | 111 | print $wfh $_ for @out; | 
|  | 112 | close $wfh or die; | 
|  | 113 | print STDERR "done\n"; | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | if( %warnings ) { | 
| Manuel Pégourié-Gonnard | c559f04 | 2015-04-08 20:10:16 +0200 | [diff] [blame] | 117 | print "\nWarning: lines skipped due to unexpected characters:\n"; | 
| Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 118 | for my $filename (sort keys %warnings) { | 
|  | 119 | print "in $filename:\n"; | 
|  | 120 | print for @{ $warnings{$filename} }; | 
|  | 121 | } | 
|  | 122 | } |