Path: blob/aarch64-shenandoah-jdk8u272-b10/make/scripts/normalizer.pl
32284 views
#!/usr/bin/perl12#3# Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.4# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.5#6# This code is free software; you can redistribute it and/or modify it7# under the terms of the GNU General Public License version 2 only, as8# published by the Free Software Foundation.9#10# This code is distributed in the hope that it will be useful, but WITHOUT11# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13# version 2 for more details (a copy is included in the LICENSE file that14# accompanied this code).15#16# You should have received a copy of the GNU General Public License version17# 2 along with this work; if not, write to the Free Software Foundation,18# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19#20# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21# or visit www.oracle.com if you need additional information or have any22# questions.23#2425#26# Parses java files:27# 1. Removes from the end of lines spaces and TABs28# 2. Replaces TABs by spaces29# 3. Replaces all NewLine separators by Unix NewLine separators30# 4. Makes one and only one empty line at the end of each file3132if ($#ARGV < 0) {33&usage;3435die;36}3738use Cwd 'abs_path';3940my @extensions = ("java");4142# Read options43my $dirpos = 0;4445while ($dirpos < $#ARGV) {46if ($ARGV[$dirpos] eq "-e") {47@extensions = split(/,/, $ARGV[$dirpos + 1]);48} else {49last;50}5152$dirpos += 2;53}5455if ($dirpos > $#ARGV) {56&usage;5758die;59}6061use Cwd;62my $currdir = getcwd;6364my $allfiles = 0;6566my $filecount = 0;6768my @tabvalues;6970# Init tabvalues71push (@tabvalues, " ");7273for (my $i = 1; $i < 8; $i++) {74push(@tabvalues, $tabvalues[$i - 1] . " ");75}7677open(FILELIST, ">$currdir/filelist") or die "Failed while open $currdir/filelist: $!\n";7879while ($dirpos <= $#ARGV) {80use File::Find;8182find(\&parse_file, abs_path($ARGV[$dirpos]));8384$dirpos += 1;85}8687close(FILELIST);8889use Cwd 'chdir';90chdir $currdir;9192print "Checked $allfiles file(s)\n";93print "Modified $filecount file(s)\n";94print "See results in the file $currdir/filelist\n";9596sub parse_file {97my $filename = $File::Find::name;9899# Skip directories100return if -d;101102# Skip SCCS files103return if ($filename =~ /\/SCCS\//);104105# Skip files with invalid extensions106my $accepted = 0;107foreach my $ext (@extensions) {108if ($_ =~ /\.$ext$/i) {109$accepted = 1;110111last;112}113}114return if ($accepted == 0);115116use File::Basename;117my $dirname = dirname($filename);118119use Cwd 'chdir';120chdir $dirname;121122open(FILE, $filename) or die "Failed while open $filename: $!\n";123124# Read file125my @content;126my $line;127my $emptylinescount = 0;128my $modified = 0;129130while ($line = <FILE>) {131my $originalline = $line;132133# Process line134135# Remove from the end of the line spaces and return character136while ($line =~ /\s$/) {137chop($line);138}139140# Replace TABs141for (my $i = 0; $i < length($line); $i++) {142if (substr($line, $i, 1) =~ /\t/) {143$line = substr($line, 0, $i) . $tabvalues[7 - ($i % 8)] . substr($line, $i + 1);144}145}146147if (length($line) == 0) {148$emptylinescount++;149} else {150while ($emptylinescount > 0) {151push(@content, "");152153$emptylinescount--;154}155156push(@content, $line);157}158159if ($originalline ne ($line . "\n")) {160$modified = 1;161}162163}164165$allfiles++;166167if ($emptylinescount > 0) {168$modified = 1;169}170171close(FILE);172173if ($modified != 0) {174# Write file175open(FILE, ">$filename") or die "Failed while open $filename: $!\n";176177for (my $i = 0; $i <= $#content; $i++) {178print FILE "$content[$i]\n";179}180181close(FILE);182183# Print name from current dir184if (index($filename, $currdir) == 0) {185print FILELIST substr($filename, length($currdir) + 1);186} else {187print FILELIST $filename;188}189print FILELIST "\n";190191$filecount++;192193print "$filename: modified\n";194}195}196197sub usage {198print "Usage:\n";199print " normalizer.pl [-options] <dir> [dir2 dir3 ...]\n";200print " Available options:\n";201print " -e comma separated files extensions. By default accepts only java files\n";202print "\n";203print "Examples:\n";204print " normalizer.pl -e c,cpp,h,hpp .\n";205}206207208209210