blob: fb428098c82508eea98c169e33b7f36aebbc231c [file] [log] [blame]
fbrosson533407a2018-04-04 21:44:29 +00001#!/usr/bin/env perl
Simon Butcher6dc7c9c2016-06-19 22:49:58 +01002#
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é-Gonnardb20a70f2015-04-08 14:56:51 +020017
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010018use warnings;
19use strict;
20
21use utf8;
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010022use Path::Class;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010023use open qw(:std utf8);
24
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020025my $usage = "Usage: $0 [-f datafile] [-s] [--] [filenames...]\n";
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010026
Manuel Pégourié-Gonnard2aa81cc2015-04-10 11:19:10 +020027(my $datafile = $0) =~ s/rename.pl$/data_files\/rename-1.3-2.0.txt/;
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020028my $do_strings = 0;
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020029
30while( @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é-Gonnard88323c72015-04-03 14:38:02 +020041}
42
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010043my %subst;
Manuel Pégourié-Gonnard2aa81cc2015-04-10 11:19:10 +020044open my $nfh, '<', $datafile or die "Could not read $datafile\n";
45my $ident = qr/[_A-Za-z][_A-Za-z0-9]*/;
46while( 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é-Gonnard3385cf42015-04-02 17:59:30 +010052 $subst{$old} = $new;
53}
Manuel Pégourié-Gonnard2aa81cc2015-04-10 11:19:10 +020054close $nfh or die;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010055
Manuel Pégourié-Gonnardc559f042015-04-08 20:10:16 +020056my $string = qr/"(?:\\.|[^\\"])*"/;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010057my $space = qr/\s+/;
58my $idnum = qr/[a-zA-Z0-9_]+/;
Manuel Pégourié-Gonnard5f29a732015-04-20 12:27:12 +010059my $symbols = qr/[-!#\$%&'()*+,.\/:;<=>?@[\\\]^_`{|}~]+|"/;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010060
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010061my $lib_include_dir = dir($0)->parent->parent->subdir('include', 'mbedtls');
62my $lib_source_dir = dir($0)->parent->parent->subdir('library');
63
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020064# if we replace inside strings, we don't consider them a token
65my $token = $do_strings ? qr/$space|$idnum|$symbols/
66 : qr/$string|$space|$idnum|$symbols/;
67
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010068my %warnings;
69
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010070# If no files were passed, exit...
71if ( not defined($ARGV[0]) ){ die $usage; }
72
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010073while( my $filename = shift )
74{
75 print STDERR "$filename... ";
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010076
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é-Gonnardf7d945f2015-04-03 15:21:50 +020083 if( -d $filename ) { print STDERR "skip (directory)\n"; next }
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010084
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é-Gonnardfb9f2a02015-04-03 18:37:07 +020091 if( $line =~ /#include/ ) {
92 $line =~ s/polarssl/mbedtls/;
93 $line =~ s/POLARSSL/MBEDTLS/;
94 push( @out, $line );
95 next;
96 }
97
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020098 my @words = ($line =~ /$token/g);
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010099 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
116if( %warnings ) {
Manuel Pégourié-Gonnardc559f042015-04-08 20:10:16 +0200117 print "\nWarning: lines skipped due to unexpected characters:\n";
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +0100118 for my $filename (sort keys %warnings) {
119 print "in $filename:\n";
120 print for @{ $warnings{$filename} };
121 }
122}