aoqi@0: #!/usr/bin/perl aoqi@0: aoqi@0: # aoqi@0: # Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: # aoqi@0: # This code is free software; you can redistribute it and/or modify it aoqi@0: # under the terms of the GNU General Public License version 2 only, as aoqi@0: # published by the Free Software Foundation. aoqi@0: # aoqi@0: # This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: # version 2 for more details (a copy is included in the LICENSE file that aoqi@0: # accompanied this code). aoqi@0: # aoqi@0: # You should have received a copy of the GNU General Public License version aoqi@0: # 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: # aoqi@0: # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: # or visit www.oracle.com if you need additional information or have any aoqi@0: # questions. aoqi@0: # aoqi@0: aoqi@0: # aoqi@0: # Parses java files: aoqi@0: # 1. Removes from the end of lines spaces and TABs aoqi@0: # 2. Replaces TABs by spaces aoqi@0: # 3. Replaces all NewLine separators by Unix NewLine separators aoqi@0: # 4. Makes one and only one empty line at the end of each file aoqi@0: aoqi@0: if ($#ARGV < 0) { aoqi@0: &usage; aoqi@0: aoqi@0: die; aoqi@0: } aoqi@0: aoqi@0: use Cwd 'abs_path'; aoqi@0: aoqi@0: my @extensions = ("java"); aoqi@0: aoqi@0: # Read options aoqi@0: my $dirpos = 0; aoqi@0: aoqi@0: while ($dirpos < $#ARGV) { aoqi@0: if ($ARGV[$dirpos] eq "-e") { aoqi@0: @extensions = split(/,/, $ARGV[$dirpos + 1]); aoqi@0: } else { aoqi@0: last; aoqi@0: } aoqi@0: aoqi@0: $dirpos += 2; aoqi@0: } aoqi@0: aoqi@0: if ($dirpos > $#ARGV) { aoqi@0: &usage; aoqi@0: aoqi@0: die; aoqi@0: } aoqi@0: aoqi@0: use Cwd; aoqi@0: my $currdir = getcwd; aoqi@0: aoqi@0: my $allfiles = 0; aoqi@0: aoqi@0: my $filecount = 0; aoqi@0: aoqi@0: my @tabvalues; aoqi@0: aoqi@0: # Init tabvalues aoqi@0: push (@tabvalues, " "); aoqi@0: aoqi@0: for (my $i = 1; $i < 8; $i++) { aoqi@0: push(@tabvalues, $tabvalues[$i - 1] . " "); aoqi@0: } aoqi@0: aoqi@0: open(FILELIST, ">$currdir/filelist") or die "Failed while open $currdir/filelist: $!\n"; aoqi@0: aoqi@0: while ($dirpos <= $#ARGV) { aoqi@0: use File::Find; aoqi@0: aoqi@0: find(\&parse_file, abs_path($ARGV[$dirpos])); aoqi@0: aoqi@0: $dirpos += 1; aoqi@0: } aoqi@0: aoqi@0: close(FILELIST); aoqi@0: aoqi@0: use Cwd 'chdir'; aoqi@0: chdir $currdir; aoqi@0: aoqi@0: print "Checked $allfiles file(s)\n"; aoqi@0: print "Modified $filecount file(s)\n"; aoqi@0: print "See results in the file $currdir/filelist\n"; aoqi@0: aoqi@0: sub parse_file { aoqi@0: my $filename = $File::Find::name; aoqi@0: aoqi@0: # Skip directories aoqi@0: return if -d; aoqi@0: aoqi@0: # Skip SCCS files aoqi@0: return if ($filename =~ /\/SCCS\//); aoqi@0: aoqi@0: # Skip files with invalid extensions aoqi@0: my $accepted = 0; aoqi@0: foreach my $ext (@extensions) { aoqi@0: if ($_ =~ /\.$ext$/i) { aoqi@0: $accepted = 1; aoqi@0: aoqi@0: last; aoqi@0: } aoqi@0: } aoqi@0: return if ($accepted == 0); aoqi@0: aoqi@0: use File::Basename; aoqi@0: my $dirname = dirname($filename); aoqi@0: aoqi@0: use Cwd 'chdir'; aoqi@0: chdir $dirname; aoqi@0: aoqi@0: open(FILE, $filename) or die "Failed while open $filename: $!\n"; aoqi@0: aoqi@0: # Read file aoqi@0: my @content; aoqi@0: my $line; aoqi@0: my $emptylinescount = 0; aoqi@0: my $modified = 0; aoqi@0: aoqi@0: while ($line = ) { aoqi@0: my $originalline = $line; aoqi@0: aoqi@0: # Process line aoqi@0: aoqi@0: # Remove from the end of the line spaces and return character aoqi@0: while ($line =~ /\s$/) { aoqi@0: chop($line); aoqi@0: } aoqi@0: aoqi@0: # Replace TABs aoqi@0: for (my $i = 0; $i < length($line); $i++) { aoqi@0: if (substr($line, $i, 1) =~ /\t/) { aoqi@0: $line = substr($line, 0, $i) . $tabvalues[7 - ($i % 8)] . substr($line, $i + 1); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (length($line) == 0) { aoqi@0: $emptylinescount++; aoqi@0: } else { aoqi@0: while ($emptylinescount > 0) { aoqi@0: push(@content, ""); aoqi@0: aoqi@0: $emptylinescount--; aoqi@0: } aoqi@0: aoqi@0: push(@content, $line); aoqi@0: } aoqi@0: aoqi@0: if ($originalline ne ($line . "\n")) { aoqi@0: $modified = 1; aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: $allfiles++; aoqi@0: aoqi@0: if ($emptylinescount > 0) { aoqi@0: $modified = 1; aoqi@0: } aoqi@0: aoqi@0: close(FILE); aoqi@0: aoqi@0: if ($modified != 0) { aoqi@0: # Write file aoqi@0: open(FILE, ">$filename") or die "Failed while open $filename: $!\n"; aoqi@0: aoqi@0: for (my $i = 0; $i <= $#content; $i++) { aoqi@0: print FILE "$content[$i]\n"; aoqi@0: } aoqi@0: aoqi@0: close(FILE); aoqi@0: aoqi@0: # Print name from current dir aoqi@0: if (index($filename, $currdir) == 0) { aoqi@0: print FILELIST substr($filename, length($currdir) + 1); aoqi@0: } else { aoqi@0: print FILELIST $filename; aoqi@0: } aoqi@0: print FILELIST "\n"; aoqi@0: aoqi@0: $filecount++; aoqi@0: aoqi@0: print "$filename: modified\n"; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: sub usage { aoqi@0: print "Usage:\n"; aoqi@0: print " normalizer.pl [-options] [dir2 dir3 ...]\n"; aoqi@0: print " Available options:\n"; aoqi@0: print " -e comma separated files extensions. By default accepts only java files\n"; aoqi@0: print "\n"; aoqi@0: print "Examples:\n"; aoqi@0: print " normalizer.pl -e c,cpp,h,hpp .\n"; aoqi@0: } aoqi@0: aoqi@0: