rename.pl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/env perl
  2. #
  3. # Copyright The Mbed TLS Contributors
  4. # 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. # 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. #
  28. use warnings;
  29. use strict;
  30. use utf8;
  31. use Path::Class;
  32. use open qw(:std utf8);
  33. my $usage = "Usage: $0 [-f datafile] [-s] [--] [filenames...]\n";
  34. (my $datafile = $0) =~ s/rename.pl$/data_files\/rename-1.3-2.0.txt/;
  35. my $do_strings = 0;
  36. while( @ARGV && $ARGV[0] =~ /^-/ ) {
  37. my $opt = shift;
  38. if( $opt eq '--' ) {
  39. last;
  40. } elsif( $opt eq '-f' ) {
  41. $datafile = shift;
  42. } elsif( $opt eq '-s' ) {
  43. $do_strings = 1; shift;
  44. } else {
  45. die $usage;
  46. }
  47. }
  48. my %subst;
  49. open my $nfh, '<', $datafile or die "Could not read $datafile\n";
  50. my $ident = qr/[_A-Za-z][_A-Za-z0-9]*/;
  51. while( my $line = <$nfh> ) {
  52. chomp $line;
  53. my ( $old, $new ) = ( $line =~ /^($ident)\s+($ident)$/ );
  54. if( ! $old || ! $new ) {
  55. die "$0: $datafile:$.: bad input '$line'\n";
  56. }
  57. $subst{$old} = $new;
  58. }
  59. close $nfh or die;
  60. my $string = qr/"(?:\\.|[^\\"])*"/;
  61. my $space = qr/\s+/;
  62. my $idnum = qr/[a-zA-Z0-9_]+/;
  63. my $symbols = qr/[-!#\$%&'()*+,.\/:;<=>?@[\\\]^_`{|}~]+|"/;
  64. my $lib_include_dir = dir($0)->parent->parent->subdir('include', 'mbedtls');
  65. my $lib_source_dir = dir($0)->parent->parent->subdir('library');
  66. # if we replace inside strings, we don't consider them a token
  67. my $token = $do_strings ? qr/$space|$idnum|$symbols/
  68. : qr/$string|$space|$idnum|$symbols/;
  69. my %warnings;
  70. # If no files were passed, exit...
  71. if ( not defined($ARGV[0]) ){ die $usage; }
  72. while( my $filename = shift )
  73. {
  74. print STDERR "$filename... ";
  75. if( dir($filename)->parent eq $lib_include_dir ||
  76. dir($filename)->parent eq $lib_source_dir )
  77. {
  78. die "Script cannot be executed on the mbed TLS library itself.";
  79. }
  80. if( -d $filename ) { print STDERR "skip (directory)\n"; next }
  81. open my $rfh, '<', $filename or die;
  82. my @lines = <$rfh>;
  83. close $rfh or die;
  84. my @out;
  85. for my $line (@lines) {
  86. if( $line =~ /#include/ ) {
  87. $line =~ s/polarssl/mbedtls/;
  88. $line =~ s/POLARSSL/MBEDTLS/;
  89. push( @out, $line );
  90. next;
  91. }
  92. my @words = ($line =~ /$token/g);
  93. my $checkline = join '', @words;
  94. if( $checkline eq $line ) {
  95. my @new = map { exists $subst{$_} ? $subst{$_} : $_ } @words;
  96. push( @out, join '', @new );
  97. } else {
  98. $warnings{$filename} = [] unless $warnings{$filename};
  99. push @{ $warnings{$filename} }, $line;
  100. push( @out, $line );
  101. }
  102. }
  103. open my $wfh, '>', $filename or die;
  104. print $wfh $_ for @out;
  105. close $wfh or die;
  106. print STDERR "done\n";
  107. }
  108. if( %warnings ) {
  109. print "\nWarning: lines skipped due to unexpected characters:\n";
  110. for my $filename (sort keys %warnings) {
  111. print "in $filename:\n";
  112. print for @{ $warnings{$filename} };
  113. }
  114. }