blob: 1e8dbf4f7f2a77440787722a34ae0bedae1e3dbf [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úti51b41d52020-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#
18# This file is part of Mbed TLS (https://tls.mbed.org)
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010019#
20# Purpose
21#
22# This script migrates application source code from the mbed TLS 1.3 API to the
23# mbed TLS 2.0 API.
24#
25# The script processes the given source code and renames identifiers - functions
26# types, enums etc, as
27#
28# Usage: rename.pl [-f datafile] [-s] [--] [filenames...]
29#
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020030
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010031use warnings;
32use strict;
33
34use utf8;
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010035use Path::Class;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010036use open qw(:std utf8);
37
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020038my $usage = "Usage: $0 [-f datafile] [-s] [--] [filenames...]\n";
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010039
Manuel Pégourié-Gonnard2aa81cc2015-04-10 11:19:10 +020040(my $datafile = $0) =~ s/rename.pl$/data_files\/rename-1.3-2.0.txt/;
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020041my $do_strings = 0;
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020042
43while( @ARGV && $ARGV[0] =~ /^-/ ) {
44 my $opt = shift;
45 if( $opt eq '--' ) {
46 last;
47 } elsif( $opt eq '-f' ) {
48 $datafile = shift;
49 } elsif( $opt eq '-s' ) {
50 $do_strings = 1; shift;
51 } else {
52 die $usage;
53 }
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020054}
55
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010056my %subst;
Manuel Pégourié-Gonnard2aa81cc2015-04-10 11:19:10 +020057open my $nfh, '<', $datafile or die "Could not read $datafile\n";
58my $ident = qr/[_A-Za-z][_A-Za-z0-9]*/;
59while( my $line = <$nfh> ) {
60 chomp $line;
61 my ( $old, $new ) = ( $line =~ /^($ident)\s+($ident)$/ );
62 if( ! $old || ! $new ) {
63 die "$0: $datafile:$.: bad input '$line'\n";
64 }
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010065 $subst{$old} = $new;
66}
Manuel Pégourié-Gonnard2aa81cc2015-04-10 11:19:10 +020067close $nfh or die;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010068
Manuel Pégourié-Gonnardc559f042015-04-08 20:10:16 +020069my $string = qr/"(?:\\.|[^\\"])*"/;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010070my $space = qr/\s+/;
71my $idnum = qr/[a-zA-Z0-9_]+/;
Manuel Pégourié-Gonnard5f29a732015-04-20 12:27:12 +010072my $symbols = qr/[-!#\$%&'()*+,.\/:;<=>?@[\\\]^_`{|}~]+|"/;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010073
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010074my $lib_include_dir = dir($0)->parent->parent->subdir('include', 'mbedtls');
75my $lib_source_dir = dir($0)->parent->parent->subdir('library');
76
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020077# if we replace inside strings, we don't consider them a token
78my $token = $do_strings ? qr/$space|$idnum|$symbols/
79 : qr/$string|$space|$idnum|$symbols/;
80
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010081my %warnings;
82
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010083# If no files were passed, exit...
84if ( not defined($ARGV[0]) ){ die $usage; }
85
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010086while( my $filename = shift )
87{
88 print STDERR "$filename... ";
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010089
90 if( dir($filename)->parent eq $lib_include_dir ||
91 dir($filename)->parent eq $lib_source_dir )
92 {
93 die "Script cannot be executed on the mbed TLS library itself.";
94 }
95
Manuel Pégourié-Gonnardf7d945f2015-04-03 15:21:50 +020096 if( -d $filename ) { print STDERR "skip (directory)\n"; next }
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010097
98 open my $rfh, '<', $filename or die;
99 my @lines = <$rfh>;
100 close $rfh or die;
101
102 my @out;
103 for my $line (@lines) {
Manuel Pégourié-Gonnardfb9f2a02015-04-03 18:37:07 +0200104 if( $line =~ /#include/ ) {
105 $line =~ s/polarssl/mbedtls/;
106 $line =~ s/POLARSSL/MBEDTLS/;
107 push( @out, $line );
108 next;
109 }
110
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +0200111 my @words = ($line =~ /$token/g);
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +0100112 my $checkline = join '', @words;
113 if( $checkline eq $line ) {
114 my @new = map { exists $subst{$_} ? $subst{$_} : $_ } @words;
115 push( @out, join '', @new );
116 } else {
117 $warnings{$filename} = [] unless $warnings{$filename};
118 push @{ $warnings{$filename} }, $line;
119 push( @out, $line );
120 }
121 }
122
123 open my $wfh, '>', $filename or die;
124 print $wfh $_ for @out;
125 close $wfh or die;
126 print STDERR "done\n";
127}
128
129if( %warnings ) {
Manuel Pégourié-Gonnardc559f042015-04-08 20:10:16 +0200130 print "\nWarning: lines skipped due to unexpected characters:\n";
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +0100131 for my $filename (sort keys %warnings) {
132 print "in $filename:\n";
133 print for @{ $warnings{$filename} };
134 }
135}