Perl Script to Indent Verilog Files

Here’s a perl script to indent verilog files.

Download here. vindent.pdf

Add your own list of keywords to indent any source code

For example, this script could be used to indent C/C++/Java source codes.

This script uses two spaces for each indent.  This could be changed as required.

#!/usr/bin/perl
# Filename    : vindent.pl
# Author      : A.G.Raja
# Website     : https://agraja.wordpress.com
#
# Usage       : ./vindent.pl verilogfile.v
#             : ./vindent.pl *.*v*
# Help        : ./vindent.pl

# Verilog File indenter

# Space for each indent
my $Space = ”  “;

# Block begin.  For indenting C/C++/Java program use “{”
my $Open = “begin”;
# Block end.  For indenting C/C++/Java program use “}”
my $Close = “end”;

# Block statements.
# Note: Each Open keyword must have corresponding Close keyword
my @Openkeywords = qw{ module program package interface class task function };
my @Closekeywords = qw{ endmodule endprogram endpackage endinterface endclass endtask endfunction };

sub indent {
my $indent = 0;
my($infile) = @_;
open(FILE, “$infile”) or die “Couldn’t open file \”$infile\”\n”;
open(TFILE, “>temp”) or die “Couldn’t create temp file\n”;

while($line = <FILE>) {
chomp $line;
$line =~ s/^\s*//;
print TFILE “$line \n”;
}
close TFILE;
close FILE;
open(TFILE, “<temp”);
open(FILE, “>$infile”);

while($line = <TFILE>) {
foreach $Closekey (@Closekeywords) {
if($line =~ /^$Closekey\s/) { $indent–; }
}
if (($line =~ /.*\s+$Close\s/ ) || ($line =~ /^$Close\s/))
{ $indent–; }
for($i=0;$i<$indent;$i++) { print FILE $Space; }
print FILE $line;
foreach $Openkey (@Openkeywords) {
if($line =~ /^$Openkey\s/) { $indent++; }
}
if (($line =~ /.*\s+$Open\s/ )|| ($line =~ /^$Open\s/))
{ $indent++; }
}
close FILE; close TFILE;
print “Indented File \”$infile\”\n”;
}

my $argnum;
if($ARGV[0] eq “”) {
print “Usage : vindent.pl verilogfile.v\n”;
print ”        vindent.pl *.v\n”;
print ”        vindent.pl *.*v*\n”;
print “Help  :\n”;
print ”        vindent.pl\n”;
}
foreach $argnum (0 .. $#ARGV) {
my $ifile = $ARGV[$argnum];
&indent($ifile);
}
unlink “temp”;
Download here. vindent.pdf

Perl script to display comments

Download here. c_help.pdf

 

#!/usr/bin/perl

# Filename: chelp.pl

# Author : A. G. Raja

# Website : agraja.wordpress.com

# License : GPL

# pass c file as argument to this script

# this script displays all the comments

use strict;

use warnings;

open(FILE,$ARGV[0]) or die “File not Found”;

my $line;

my $start=0;

while ( $line = <FILE> ) {

if($line =~ /\/\//)

{

$line=~ s/(.*)\/\/(.*)/$2/;

print $line;

}

if($line =~ /\/\*/)

{

$start=1;

$line=~ s/(.*)\/\*(.*)/$2/;

}

if($line =~ /\*\//)

{

$line=~ s/(.*)\*\//$1/;

print $line;

$start=0;

}

if($start) { print $line; }

}

close FILE;

 

Tips:

Use a shell script to redirect the output

of the above script to another program.

Example

#!/bin/bash

# Filename: help.sh

# Author : A.G.Raja

# Website : agraja.wordpress.com

# License : GPL

# rename chelp.pl to chelp

# Install chelp and add to path

chelp $1 | less

Demo:

 

Below is a C file

 

// Filename : demo.c

// Author : cfile.pl

// License : GPL

#include <stdio.h>

#define print(x) printf(“str=%s\n”,x);

int main(int argc, char **argv)

{

/* this is a multi-

line comment */

char *str=”File name:”;// this will print file name

print(argv[0])

return 0;

}

 

Type at the terminal:

[raja@agraja ]$ chelp demo.c

 

The following will be printed to stdout

 

Filename : demo.c

Author : cfile.pl

License : GPL

this is a multi-

line comment

this will print file name

Download here. c_help.pdf

Perl script to add Header to files

The following script will add text to the beginning of files.

Donwload here. perlheader.pdf

This is done in three steps

1) This script copies the contents of a file to local variable by slurping.

2) The necessary text that needs to be added at the beginning of the file is added.

3) Then the original contents of the file are added using the local variable.

 

 

#!/usr/bin/perl

# Filename: header.pl

# Author : A.G.Raja

# Website : agraja.wordpress.com

# License : GPL

# pass files as arguments to this script

# this script will add the header text

use strict;

use warnings;

sub addheader{

my($infile,$header)=@_;

my $text = do { local( @ARGV, $/ ) = $infile ; <> } ;

open(FILE,”>$infile”);

print FILE $header;

print FILE $text;

close FILE

}

my $argnum;

foreach $argnum (0 .. $#ARGV){

my $infile = $ARGV[$argnum];

# Edit line below to change header text

my $header = “Header line 1nHeader line 2nHeader line 3n”;

&addheader($infile,$header);

}

# chmod +x header.pl

# ./header.pl *.txt

 

Donwload here. perlheader.pdf

Perl Script to Generate Makefile

Download here. createmakefile.pdf
#!/usr/bin/perl
# File name: writemake.pl
# Author: A.G.Raja
# Website: agraja.wordpress.com
# License: GPL
use strict; use warnings; open(FH,">Makefile") or die "Cannot create Makefile"; print FH ("all: buildnn"); print FH ("build: " . $ARGV[0] .".onn"); print FH ("shared: " . $ARGV[0] . ".sonn"); print FH ("clean: n"); print FH ("trm ".$ARGV[0].".*o nn"); print FH ($ARGV[0].".o: ".$ARGV[0].".cn"); print FH ("tgcc -o ".$ARGV[0].".o ".$ARGV[0].".cnn"); print FH ($ARGV[0].".so: ".$ARGV[0].".cn"); print FH ("tgcc -fPIC -shared -o ".$ARGV[0].".so ".$ARGV[0].".cn"); close FH;
# Usage:
# chmod +x writemake.pl
# writenake.pl hello
Below is the Makefile generated:
all: build build: hello.o shared: hello.so clean: rm hello.*o hello.o: hello.c gcc -o hello.o hello.c hello.so: hello.c gcc -fPIC -shared -o hello.so hello.c
Download here. createmakefile.pdf

Monitor Terminals Using script Command

Open a terminal and type as below

[raja@AGRAJA ~]$ mkfifo output
[raja@AGRAJA ~]$ script -f output

Open another terminal and type as below

[raja@AGRAJA ~]$ cat output

Now return to first terminal and execute any command.

Everything on the first terminal is updated on the second terminal also.

script.JPG


Never type ls Again

It is compelling to type “ls” every time after

1) changing directory

2) copy/remove files

3) program is run; output file is created

etc. etc. …
Here’s a script that runs on an infinite loop that

1) allows type any command as usual (no auto-complete is available)

2) automatically “ls” after every command

# save file as cdls
# chmod 755 cdls
# type "exit" to terminate
while [ 1 ]
do
    ls
    read shell_command
    $shell_command
done

cdls_usage.JPG

A more useful form of the script is given below

# save as cdls
# chmod 755 cdls
# cp cdls /bin  
clear
tput cup 24 0
while [ 1 ] do
tput setb 9
tput setf 0
echo --------------------------------------------------------------------------------
ls -F --color=auto
echo [`whoami` `pwd` `date`]$
tput setb 0
tput setf 7
read cmd1
$cmd1
tput cup 24 0
done

Suppress Error Message in Shell Script

stderr

Redirect the output of stderr (descriptor 2) to /dev/null:

# save this file as testnull
# chmod 755 testnull
ls -garbage 2>/dev/null
# try ./testnull

Diagram below shows a demo

shell_error.JPG

Notice that

1) redirecting to /dev/null from command line has no effect

2) redirecting error from shell script works !!

stdout

[raja@AGRAJA ~] ls 1>file_list.txt
[raja@AGRAJA ~] ls > same_file_list.txt

This is the default option during redirecting output.

i.e “1>” and “>” mean the same.

stdin

[raja@AGRAJA ~] ls 0>empty_file
[raja@AGRAJA ~] ls

The above prints to standard terminal.

Note that “empty_file” will be emptied.

Redirect everything to file

Redirect all console output texts (stdout, stderr, stdlog …) to file

For sh shell 
at command line:
[raja@AGRAJA ~] make > & ! make.log
inside shell script:
illegal junk text as command > junk.spam 2>&1
[or]
illegal junk text as command > & ! junk.spam
For csh shell
Note: The below works on the command line, as well as inside a script
[raja@AGRAJA ~] make > & ! make.log
[raja@AGRAJA ~] ls > & ! files.lst
[raja@AGRAJA ~] illegal junk text as command > & ! junk.spam