blob: 9ea5f09c9aa96663a2ccd923f995cf7a7d89c744 [file] [log] [blame]
fbrosson533407a2018-04-04 21:44:29 +00001#!/usr/bin/env perl
Simon Butcher6dc7c9c2016-06-19 22:49:58 +01002#
Bence Szépkúti1e148272020-08-07 13:07:28 +02003# Copyright The Mbed TLS Contributors
Bence Szépkútic7da1fe2020-05-26 01:54:15 +02004# SPDX-License-Identifier: Apache-2.0
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010018# Purpose
19#
20# This script migrates application source code from the mbed TLS 1.3 API to the
21# mbed TLS 2.0 API.
22#
23# The script processes the given source code and renames identifiers - functions
24# types, enums etc, as
25#
26# Usage: rename.pl [-f datafile] [-s] [--] [filenames...]
27#
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020028
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010029use warnings;
30use strict;
31
32use utf8;
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010033use Path::Class;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010034use open qw(:std utf8);
35
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020036my $usage = "Usage: $0 [-f datafile] [-s] [--] [filenames...]\n";
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010037
Manuel Pégourié-Gonnard2aa81cc2015-04-10 11:19:10 +020038(my $datafile = $0) =~ s/rename.pl$/data_files\/rename-1.3-2.0.txt/;
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020039my $do_strings = 0;
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020040
41while( @ARGV && $ARGV[0] =~ /^-/ ) {
42 my $opt = shift;
43 if( $opt eq '--' ) {
44 last;
45 } elsif( $opt eq '-f' ) {
46 $datafile = shift;
47 } elsif( $opt eq '-s' ) {
48 $do_strings = 1; shift;
49 } else {
50 die $usage;
51 }
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020052}
53
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010054my %subst;
Manuel Pégourié-Gonnard2aa81cc2015-04-10 11:19:10 +020055open my $nfh, '<', $datafile or die "Could not read $datafile\n";
56my $ident = qr/[_A-Za-z][_A-Za-z0-9]*/;
57while( my $line = <$nfh> ) {
58 chomp $line;
59 my ( $old, $new ) = ( $line =~ /^($ident)\s+($ident)$/ );
60 if( ! $old || ! $new ) {
61 die "$0: $datafile:$.: bad input '$line'\n";
62 }
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010063 $subst{$old} = $new;
64}
Manuel Pégourié-Gonnard2aa81cc2015-04-10 11:19:10 +020065close $nfh or die;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010066
Manuel Pégourié-Gonnardc559f042015-04-08 20:10:16 +020067my $string = qr/"(?:\\.|[^\\"])*"/;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010068my $space = qr/\s+/;
69my $idnum = qr/[a-zA-Z0-9_]+/;
Manuel Pégourié-Gonnard5f29a732015-04-20 12:27:12 +010070my $symbols = qr/[-!#\$%&'()*+,.\/:;<=>?@[\\\]^_`{|}~]+|"/;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010071
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010072my $lib_include_dir = dir($0)->parent->parent->subdir('include', 'mbedtls');
73my $lib_source_dir = dir($0)->parent->parent->subdir('library');
74
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020075# if we replace inside strings, we don't consider them a token
76my $token = $do_strings ? qr/$space|$idnum|$symbols/
77 : qr/$string|$space|$idnum|$symbols/;
78
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010079my %warnings;
80
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010081# If no files were passed, exit...
82if ( not defined($ARGV[0]) ){ die $usage; }
83
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010084while( my $filename = shift )
85{
86 print STDERR "$filename... ";
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010087
88 if( dir($filename)->parent eq $lib_include_dir ||
89 dir($filename)->parent eq $lib_source_dir )
90 {
91 die "Script cannot be executed on the mbed TLS library itself.";
92 }
93
Manuel Pégourié-Gonnardf7d945f2015-04-03 15:21:50 +020094 if( -d $filename ) { print STDERR "skip (directory)\n"; next }
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010095
96 open my $rfh, '<', $filename or die;
97 my @lines = <$rfh>;
98 close $rfh or die;
99
100 my @out;
101 for my $line (@lines) {
Manuel Pégourié-Gonnardfb9f2a02015-04-03 18:37:07 +0200102 if( $line =~ /#include/ ) {
103 $line =~ s/polarssl/mbedtls/;
104 $line =~ s/POLARSSL/MBEDTLS/;
105 push( @out, $line );
106 next;
107 }
108
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +0200109 my @words = ($line =~ /$token/g);
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +0100110 my $checkline = join '', @words;
111 if( $checkline eq $line ) {
112 my @new = map { exists $subst{$_} ? $subst{$_} : $_ } @words;
113 push( @out, join '', @new );
114 } else {
115 $warnings{$filename} = [] unless $warnings{$filename};
116 push @{ $warnings{$filename} }, $line;
117 push( @out, $line );
118 }
119 }
120
121 open my $wfh, '>', $filename or die;
122 print $wfh $_ for @out;
123 close $wfh or die;
124 print STDERR "done\n";
125}
126
127if( %warnings ) {
Manuel Pégourié-Gonnardc559f042015-04-08 20:10:16 +0200128 print "\nWarning: lines skipped due to unexpected characters:\n";
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +0100129 for my $filename (sort keys %warnings) {
130 print "in $filename:\n";
131 print for @{ $warnings{$filename} };
132 }
133}