#!/bin/perl $fp_bool = open(FPW, ">2.vhd"); #vlog if( $fp_bool == 0 ) { print ("by PK open fail file is 2.vhd\n");#vlog exit(1); } select(FPW); $fp_bool = open(FPR, $ARGV[0]); if( $fp_bool == 0 ) { print ("open fail $ARGV[0]\n"); } $ind_cnt=0; #indent counter - current $ind_cntW=0; #indent counter - current *2 $ind_cntWadj=0; #same as above adjusted to special cases #buffer that stores words that cause an indent increment. @ind_inc= split(/ /, "entity ENTITY BEGIN begin THEN then component COMPONENT case CASE package PACKAGE loop LOOP"); #buffer that stores words that cause an indent decrement. @ind_dec= split(/ /, "end END ELSIF elsif"); $cnt_p=0; $line=; while( eof(FPR) != 1 ) { #update the indent counter do ind_cnt_update($ind_cnt); $ind_cntW=2*$ind_cnt; #remove leading spaces and tabs do rm_l_spaces(); #print the line do adj_special_cases($ind_cntW, $ind_cntWadj); prt_spaces($ind_cntWadj); # print("($ind_cntW)$line"); # print("($cnt_p $ind_cntW)$line"); print("$line"); $line=; }# end while sub prt_spaces { my($i)=""; for($i=0; $i<$_[0]; $i++) { print(" "); }#of for } #Works on the the global line: file line buffer. #Removes leading spaces and tabs. sub rm_l_spaces { while($line =~ /^\t/) {$line =~ s/^\t//g;} while($line =~ /^ / ) {$line =~ s/^ //g;} #To make sure we get cases with mixed leading space and TAB characters. while($line =~ /^\t/) {$line =~ s/^\t//g;} while($line =~ /^ / ) {$line =~ s/^ //g;} } sub ind_cnt_update { my($wd)=""; my($cnt)=""; my($line_c)=""; #copy of original line my(@line_a)=""; my($cnt_braces)=""; #remove remark $line_c=$line; #save original @line_a=split(/--/, $line); $line =$line_a[0]; # if($line =~ /--/) {die("dbg $line_a[0]-=$line");} #count braces do match_braces($cnt_braces); #increment $cnt=0; foreach $wd (@ind_inc) { do match_wd_in_line($wd, $cnt); }#of foreach $_[0] += $cnt_p; #delay one line the increment value. if($cnt_braces > 0) {$cnt += $cnt_braces;} $cnt_p = $cnt; #print("dbg+ $cnt\n");#dbg #decrement $cnt=0; foreach $wd (@ind_dec) { do match_wd_in_line($wd, $cnt); }#of foreach $_[0] -= $cnt; if($cnt_braces < 0) { $_[0] += $cnt_braces; } #print("dbg- $cnt\n");#dbg #restore line original value $line=$line_c; #save original } #Gets a word to look for in the global buffer line. sub match_wd_in_line { my(@a)=""; my($line_c)=""; #copy of original line #We miss cases where a char under search is right at the end for instance #consider:"(\n". $line_c=$line; #save original chomp($line); $line=$line . " \n"; #The following two are mainly for braces at if/elsif statements. $line =~ s/\(/ \( /i; $line =~ s/\)/ \) /i; $line =~ s/;/ ;/; #This is to help find cases like this "END;". #Remove tabs - instead use one space. $line =~ s/\t/ /g; #component is used to an increment, therefor an "end component" statement #causes a bug. if($line =~ /(^.*end\s*)(component)(.*)/i) { $line = $1 . $3; } #Same as above with end case. if($line =~ /(^.*end\s*)(case)(.*)/i) { $line = $1 . $3; } #Same as above with end loop. if($line =~ /(^.*end\s*)(loop)(.*)/i) { $line = $1 . $3; } @a=split(/ /, $line); foreach $wd_line (@a) { if($_[0] eq $wd_line) {$_[1]+=1;} }#of foreach $line=$line_c; #restore original } sub match_braces { my($l)=""; my($i)=""; my($ch)="";#char my($cnti)=""; my($cntd)=""; #count number of ( - number of ) $l=length($line); $c=0; for($i=0; $i<=$l; $i++) { $ch=substr($line, $i, 1); if ($ch eq "(") {$cnti++;} elsif($ch eq ")") {$cntd++;} }#of for $_[0] = $cnti - $cntd; } #In regular indent number #Out adjusted indent number according to special cases sub adj_special_cases { $_[1]=$_[0]; #default operation #else if ($line =~ /^\s*else\s*$/) {$_[1]=$_[0]-2;} elsif($line =~ /^.*\sthen\s.*end\s*if/) {$_[1]=$_[0]+2;} }