blob: 8e08ad0cbbb84d36970e9565d2ebc0c6b60384fe [file] [log] [blame]
fbrosson533407a2018-04-04 21:44:29 +00001#!/usr/bin/env perl
Simon Butcher6dc7c9c2016-06-19 22:49:58 +01002#
Simon Butcher6dc7c9c2016-06-19 22:49:58 +01003# Copyright (c) 2015-2016, ARM Limited, All Rights Reserved
Bence Szépkútif744bd72020-06-05 13:02:18 +02004# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
5#
6# This file is provided under the Apache License 2.0, or the
7# GNU General Public License v2.0 or later.
8#
9# **********
10# Apache License 2.0:
Bence Szépkúti51b41d52020-05-26 01:54:15 +020011#
12# Licensed under the Apache License, Version 2.0 (the "License"); you may
13# not use this file except in compliance with the License.
14# You may obtain a copy of the License at
15#
16# http://www.apache.org/licenses/LICENSE-2.0
17#
18# Unless required by applicable law or agreed to in writing, software
19# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
20# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21# See the License for the specific language governing permissions and
22# limitations under the License.
23#
Bence Szépkútif744bd72020-06-05 13:02:18 +020024# **********
25#
26# **********
27# GNU General Public License v2.0 or later:
28#
29# This program is free software; you can redistribute it and/or modify
30# it under the terms of the GNU General Public License as published by
31# the Free Software Foundation; either version 2 of the License, or
32# (at your option) any later version.
33#
34# This program is distributed in the hope that it will be useful,
35# but WITHOUT ANY WARRANTY; without even the implied warranty of
36# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37# GNU General Public License for more details.
38#
39# You should have received a copy of the GNU General Public License along
40# with this program; if not, write to the Free Software Foundation, Inc.,
41# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
42#
43# **********
44#
Bence Szépkúti51b41d52020-05-26 01:54:15 +020045# This file is part of Mbed TLS (https://tls.mbed.org)
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010046#
47# Purpose
48#
49# This script migrates application source code from the mbed TLS 1.3 API to the
50# mbed TLS 2.0 API.
51#
52# The script processes the given source code and renames identifiers - functions
53# types, enums etc, as
54#
55# Usage: rename.pl [-f datafile] [-s] [--] [filenames...]
56#
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020057
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010058use warnings;
59use strict;
60
61use utf8;
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010062use Path::Class;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010063use open qw(:std utf8);
64
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020065my $usage = "Usage: $0 [-f datafile] [-s] [--] [filenames...]\n";
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010066
Manuel Pégourié-Gonnard2aa81cc2015-04-10 11:19:10 +020067(my $datafile = $0) =~ s/rename.pl$/data_files\/rename-1.3-2.0.txt/;
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020068my $do_strings = 0;
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020069
70while( @ARGV && $ARGV[0] =~ /^-/ ) {
71 my $opt = shift;
72 if( $opt eq '--' ) {
73 last;
74 } elsif( $opt eq '-f' ) {
75 $datafile = shift;
76 } elsif( $opt eq '-s' ) {
77 $do_strings = 1; shift;
78 } else {
79 die $usage;
80 }
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020081}
82
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010083my %subst;
Manuel Pégourié-Gonnard2aa81cc2015-04-10 11:19:10 +020084open my $nfh, '<', $datafile or die "Could not read $datafile\n";
85my $ident = qr/[_A-Za-z][_A-Za-z0-9]*/;
86while( my $line = <$nfh> ) {
87 chomp $line;
88 my ( $old, $new ) = ( $line =~ /^($ident)\s+($ident)$/ );
89 if( ! $old || ! $new ) {
90 die "$0: $datafile:$.: bad input '$line'\n";
91 }
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010092 $subst{$old} = $new;
93}
Manuel Pégourié-Gonnard2aa81cc2015-04-10 11:19:10 +020094close $nfh or die;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010095
Manuel Pégourié-Gonnardc559f042015-04-08 20:10:16 +020096my $string = qr/"(?:\\.|[^\\"])*"/;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010097my $space = qr/\s+/;
98my $idnum = qr/[a-zA-Z0-9_]+/;
Manuel Pégourié-Gonnard5f29a732015-04-20 12:27:12 +010099my $symbols = qr/[-!#\$%&'()*+,.\/:;<=>?@[\\\]^_`{|}~]+|"/;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +0100100
Simon Butcher6dc7c9c2016-06-19 22:49:58 +0100101my $lib_include_dir = dir($0)->parent->parent->subdir('include', 'mbedtls');
102my $lib_source_dir = dir($0)->parent->parent->subdir('library');
103
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +0200104# if we replace inside strings, we don't consider them a token
105my $token = $do_strings ? qr/$space|$idnum|$symbols/
106 : qr/$string|$space|$idnum|$symbols/;
107
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +0100108my %warnings;
109
Simon Butcher6dc7c9c2016-06-19 22:49:58 +0100110# If no files were passed, exit...
111if ( not defined($ARGV[0]) ){ die $usage; }
112
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +0100113while( my $filename = shift )
114{
115 print STDERR "$filename... ";
Simon Butcher6dc7c9c2016-06-19 22:49:58 +0100116
117 if( dir($filename)->parent eq $lib_include_dir ||
118 dir($filename)->parent eq $lib_source_dir )
119 {
120 die "Script cannot be executed on the mbed TLS library itself.";
121 }
122
Manuel Pégourié-Gonnardf7d945f2015-04-03 15:21:50 +0200123 if( -d $filename ) { print STDERR "skip (directory)\n"; next }
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +0100124
125 open my $rfh, '<', $filename or die;
126 my @lines = <$rfh>;
127 close $rfh or die;
128
129 my @out;
130 for my $line (@lines) {
Manuel Pégourié-Gonnardfb9f2a02015-04-03 18:37:07 +0200131 if( $line =~ /#include/ ) {
132 $line =~ s/polarssl/mbedtls/;
133 $line =~ s/POLARSSL/MBEDTLS/;
134 push( @out, $line );
135 next;
136 }
137
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +0200138 my @words = ($line =~ /$token/g);
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +0100139 my $checkline = join '', @words;
140 if( $checkline eq $line ) {
141 my @new = map { exists $subst{$_} ? $subst{$_} : $_ } @words;
142 push( @out, join '', @new );
143 } else {
144 $warnings{$filename} = [] unless $warnings{$filename};
145 push @{ $warnings{$filename} }, $line;
146 push( @out, $line );
147 }
148 }
149
150 open my $wfh, '>', $filename or die;
151 print $wfh $_ for @out;
152 close $wfh or die;
153 print STDERR "done\n";
154}
155
156if( %warnings ) {
Manuel Pégourié-Gonnardc559f042015-04-08 20:10:16 +0200157 print "\nWarning: lines skipped due to unexpected characters:\n";
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +0100158 for my $filename (sort keys %warnings) {
159 print "in $filename:\n";
160 print for @{ $warnings{$filename} };
161 }
162}