rename.pl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env perl
  2. #
  3. # Copyright The Mbed TLS Contributors
  4. # 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:
  11. #
  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. #
  24. # **********
  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. #
  45. # Purpose
  46. #
  47. # This script migrates application source code from the mbed TLS 1.3 API to the
  48. # mbed TLS 2.0 API.
  49. #
  50. # The script processes the given source code and renames identifiers - functions
  51. # types, enums etc, as
  52. #
  53. # Usage: rename.pl [-f datafile] [-s] [--] [filenames...]
  54. #
  55. use warnings;
  56. use strict;
  57. use utf8;
  58. use Path::Class;
  59. use open qw(:std utf8);
  60. my $usage = "Usage: $0 [-f datafile] [-s] [--] [filenames...]\n";
  61. (my $datafile = $0) =~ s/rename.pl$/data_files\/rename-1.3-2.0.txt/;
  62. my $do_strings = 0;
  63. while( @ARGV && $ARGV[0] =~ /^-/ ) {
  64. my $opt = shift;
  65. if( $opt eq '--' ) {
  66. last;
  67. } elsif( $opt eq '-f' ) {
  68. $datafile = shift;
  69. } elsif( $opt eq '-s' ) {
  70. $do_strings = 1; shift;
  71. } else {
  72. die $usage;
  73. }
  74. }
  75. my %subst;
  76. open my $nfh, '<', $datafile or die "Could not read $datafile\n";
  77. my $ident = qr/[_A-Za-z][_A-Za-z0-9]*/;
  78. while( my $line = <$nfh> ) {
  79. chomp $line;
  80. my ( $old, $new ) = ( $line =~ /^($ident)\s+($ident)$/ );
  81. if( ! $old || ! $new ) {
  82. die "$0: $datafile:$.: bad input '$line'\n";
  83. }
  84. $subst{$old} = $new;
  85. }
  86. close $nfh or die;
  87. my $string = qr/"(?:\\.|[^\\"])*"/;
  88. my $space = qr/\s+/;
  89. my $idnum = qr/[a-zA-Z0-9_]+/;
  90. my $symbols = qr/[-!#\$%&'()*+,.\/:;<=>?@[\\\]^_`{|}~]+|"/;
  91. my $lib_include_dir = dir($0)->parent->parent->subdir('include', 'mbedtls');
  92. my $lib_source_dir = dir($0)->parent->parent->subdir('library');
  93. # if we replace inside strings, we don't consider them a token
  94. my $token = $do_strings ? qr/$space|$idnum|$symbols/
  95. : qr/$string|$space|$idnum|$symbols/;
  96. my %warnings;
  97. # If no files were passed, exit...
  98. if ( not defined($ARGV[0]) ){ die $usage; }
  99. while( my $filename = shift )
  100. {
  101. print STDERR "$filename... ";
  102. if( dir($filename)->parent eq $lib_include_dir ||
  103. dir($filename)->parent eq $lib_source_dir )
  104. {
  105. die "Script cannot be executed on the mbed TLS library itself.";
  106. }
  107. if( -d $filename ) { print STDERR "skip (directory)\n"; next }
  108. open my $rfh, '<', $filename or die;
  109. my @lines = <$rfh>;
  110. close $rfh or die;
  111. my @out;
  112. for my $line (@lines) {
  113. if( $line =~ /#include/ ) {
  114. $line =~ s/polarssl/mbedtls/;
  115. $line =~ s/POLARSSL/MBEDTLS/;
  116. push( @out, $line );
  117. next;
  118. }
  119. my @words = ($line =~ /$token/g);
  120. my $checkline = join '', @words;
  121. if( $checkline eq $line ) {
  122. my @new = map { exists $subst{$_} ? $subst{$_} : $_ } @words;
  123. push( @out, join '', @new );
  124. } else {
  125. $warnings{$filename} = [] unless $warnings{$filename};
  126. push @{ $warnings{$filename} }, $line;
  127. push( @out, $line );
  128. }
  129. }
  130. open my $wfh, '>', $filename or die;
  131. print $wfh $_ for @out;
  132. close $wfh or die;
  133. print STDERR "done\n";
  134. }
  135. if( %warnings ) {
  136. print "\nWarning: lines skipped due to unexpected characters:\n";
  137. for my $filename (sort keys %warnings) {
  138. print "in $filename:\n";
  139. print for @{ $warnings{$filename} };
  140. }
  141. }