blob: 8489580343c434deeb9944be5f91d6225b6ed67f [file] [log] [blame]
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +01001#!/usr/bin/perl
2
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +02003# rename identifiers (functions, types, enum constant, etc)
4# on upgrades of major version according to a list
5
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +01006use warnings;
7use strict;
8
9use utf8;
10use open qw(:std utf8);
11
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020012my $usage = "Usage: $0 [-f datafile] [-s] [--] [filenames...]\n";
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010013
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020014(my $datafile = $0) =~ s/(rename-1\.3-2\.0)\.pl$/data_files\/$1.txt/;
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020015my $do_strings = 0;
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020016
17while( @ARGV && $ARGV[0] =~ /^-/ ) {
18 my $opt = shift;
19 if( $opt eq '--' ) {
20 last;
21 } elsif( $opt eq '-f' ) {
22 $datafile = shift;
23 } elsif( $opt eq '-s' ) {
24 $do_strings = 1; shift;
25 } else {
26 die $usage;
27 }
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020028}
29
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020030open my $nfh, '<', $datafile or die "Could not read $datafile\n";
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010031my @names = <$nfh>;
32close $nfh or die;
33
34my %subst;
35for my $name (@names) {
36 chomp $name;
37 my ($old, $new) = split / /, $name;
38 $subst{$old} = $new;
39}
40
41my $string = qr/".*?(?<!\\)"/;
42my $space = qr/\s+/;
43my $idnum = qr/[a-zA-Z0-9_]+/;
44my $symbols = qr/[!#%&'()*+,-.:;<=>?@^_`{|}~\$\/\[\\\]]+|"/;
45
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020046# if we replace inside strings, we don't consider them a token
47my $token = $do_strings ? qr/$space|$idnum|$symbols/
48 : qr/$string|$space|$idnum|$symbols/;
49
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010050my %warnings;
51
52while( my $filename = shift )
53{
54 print STDERR "$filename... ";
Manuel Pégourié-Gonnardf7d945f2015-04-03 15:21:50 +020055 if( -d $filename ) { print STDERR "skip (directory)\n"; next }
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010056
57 open my $rfh, '<', $filename or die;
58 my @lines = <$rfh>;
59 close $rfh or die;
60
61 my @out;
62 for my $line (@lines) {
Manuel Pégourié-Gonnardfb9f2a02015-04-03 18:37:07 +020063 if( $line =~ /#include/ ) {
64 $line =~ s/polarssl/mbedtls/;
65 $line =~ s/POLARSSL/MBEDTLS/;
66 push( @out, $line );
67 next;
68 }
69
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020070 my @words = ($line =~ /$token/g);
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010071 my $checkline = join '', @words;
72 if( $checkline eq $line ) {
73 my @new = map { exists $subst{$_} ? $subst{$_} : $_ } @words;
74 push( @out, join '', @new );
75 } else {
76 $warnings{$filename} = [] unless $warnings{$filename};
77 push @{ $warnings{$filename} }, $line;
78 push( @out, $line );
79 }
80 }
81
82 open my $wfh, '>', $filename or die;
83 print $wfh $_ for @out;
84 close $wfh or die;
85 print STDERR "done\n";
86}
87
88if( %warnings ) {
89 print "\nWarning: lines skipped due to unexpected charaacters:\n";
90 for my $filename (sort keys %warnings) {
91 print "in $filename:\n";
92 print for @{ $warnings{$filename} };
93 }
94}