Last time I was bugged by a totally messed up configuration file and therefore I wrote (better say 'hacked') a little perl script that just lists the nesting level of each IF, ELSE, ENDIF statement.
Here's the full code - enjoy. There is room for improvements and hints/comments are welcome (I'm definitely not a perl guru :-)
@goto ;#DOS
##
# Visualize nesting level of IF/ELSE/ENDIF pairs
# Dr. Jürgen Welzenbach, 2009
#
# - Constants -------------------------
my $tag_Begin = "IF";
my $tag_Mid = "ELSE";
my $tag_End = "ENDIF";
## - Preamble -------------------------------------------------------
use strict;
use Cwd;
use File::Basename;
my $nestingWidth = 5;
## - Main -----------------------------------------------------------
&filter_files( @ARGV );
## - Functions ------------------------------------------------------
sub filter_file
{
my $fname = shift;
my($filename, $directories, $suffix) = fileparse($fname, qr/\.[^.]*/);
my $nestCount = 0;
my $lineNo = 1;
print "Checking $fname\n";
my $fname_ok = open( FILE, "<", $fname ); if ( $fname_ok ) { my $line; print("Line Level Text:\n"); while ( $line =)
{
my $printIt = 0;
$line = " " . $line;
## Ok, here's room for improovement; but I'm no perl specialist and for
## a quick hack it's ok - for the moment :-)
if ( $line =~ /\W$tag_Begin/ )
{
$line =~ s/^\s+//;
my $sp = sprintf("%4d [%2d]: %*s%s", $lineNo, $nestCount, ($nestCount*$nestingWidth), " ", $line);
print $sp;
$nestCount = $nestCount + 1;
}
elsif ( $line =~ /\W$tag_Mid/ )
{
$line =~ s/^\s+//;
my $sp = sprintf("%4d [%2d]: %*s%s", $lineNo, $nestCount, (($nestCount-1)*$nestingWidth), " ", $line);
print $sp;
}
elsif ( $line =~ /\W$tag_End/ )
{
$line =~ s/^\s+//;
$nestCount = $nestCount - 1;
my $sp = sprintf("%4d [%2d]: %*s%s", $lineNo, $nestCount, ($nestCount*$nestingWidth), " ", $line);
print $sp;
}
$lineNo = $lineNo + 1;
}
close( FILE );
warn $@ if $@;
}
else
{
if( !$fname_ok )
{
print "Error opening '$fname '\n";
}
else
{
close( FILE );
}
}
}
sub filter_files
{
my @args = @_;
while ( $#args >= 0 )
{
filter_file( $args[ 0 ] );
shift @args;
}
}
__END__
:;#DOS
@perl -S %0 %*
No comments:
Post a Comment