#!/usr/bin/perl -0777
# This is a script to count the number of lines of source code in a  file

my @lines = ();
my $numofifs = 1;
my $commentstarted = 0;
my $notacfile = 0;

if($ARGV[0] !~ /\.[cCh]$/) {
  $notacfile = 1;
}

while(<>) {
     s/\/\*(.*?)\*\//cOmMeNt/gms;
     s/<!--(.*?)-->/cOmMeNt/gms;
     @tmp = split(/\n/);
     for(@tmp) { 
 
     if (/cOmMeNt/) {
                unless(/cOmMeNt/ ) {
			$_ = $_ . "\n";
                        push(@lines, $_);
			next;
                 } 
                 else {
                    if((/[\S]+[\s]*cOmMeNt/) || (/cOmMeNt[\s]*[\S]+/)) {
                       $_ = $_ . "\n";
                       push(@lines, $_);
                    }
	            					
                 }
         }
          else {
#slurp #if 0 comments
            if( (/^[\s]*#[\s]*if[\s]+0/) || ($commentstarted == 1) ) {
                if ( $commentstarted == 0) {
                  $commentstarted = 1;
                  next;
                }	
                if(/^[\s]*#[\s]*if/) {
                 $numofifs = $numofifs + 1;
                 next; 
                }
                unless(/^[\s]*#[\s]*endif/) {
             	next; # slurp the comments
                }
                 else { # we found an endif, but we don't know if it is the right one...
                  $numofifs = $numofifs - 1;
                  if($numofifs == 0) {
                    $commentstarted = 0;
                    $numofifs = 1;
                  # found the end of comment , so just fall thro'
                  }
                  next;
                 }
            }
 
#leave out // comments 
		if(/^[\s]*\/\//) {
	         next;		
		}
# leaave out # style comments if it is not a C/C++ file taking care to count #! line
               if($notacfile == 1) {
                unless(/^#!/) {
                  if  (/^[\s]*#/) {
		    next;
                  } 
                }
               }
#leave out blank lines ,vhdl  and assembly type comments
		if( (/^[\s]*$/) || (/^[\s]*--/) || (/^[\s]*;/) ){
	         next;		
		}

# Now leave out =head and =pod section till =cut in perl scripts
if( (/^=pod/) || (/^=head/) || ($commentstarted == 1) ) {
                if ( $commentstarted == 0) {
                  $commentstarted = 1;
                  next;
                }	
               unless(/^=cut/) {
             	next; # slurp the comments
                }
            # Now found cut, just fall thro'
            }
		$_ = $_ . "\n";
                push(@lines, $_);
my @lines = ();
          }

      }
}
print "The number of lines of code in file is [1m " . @lines . "[0m\n";
exit @lines;

=head1 countlines.pl

Countlines - A script to count the number of lines of code in a C,C++,Java,perl,shell,html,VHDL,Verilog or Python file

Usage: ./countlines.pl <sourcefile>

=head1 DESCRIPTION

This script leaves out the blank lines, C style comments, C++ style comments and #if 0 style comments and counts the remaining lines in the  C/C++ source file.

Something similar is done for perl,shell scripts and others.

  The result is printed to the terminal. The input is STDIN which means that a file can be redirected through a pipe or given in the command line.

=head1 README

This is a simple script to figure out the actual lines of code in a C,C++,perl,shell,html,VHDL,Verilog,Java or Python file

=head1 PREREQUISITES

None

=head1 COREQUISITES

None

=pod OSNAMES

any

=pod SCRIPT CATEGORIES

Educational/ComputerScience

=cut

