1#!/usr/local/bin/perl -w
2# asm_count - count physical lines of code in Assembly programs.
3# Usage: asm_count [-f file] [list_of_files]
4#  file: file with a list of files to count (if "-", read list from stdin)
5#  list_of_files: list of files to count
6#  -f file or list_of_files can be used, or both
7# This is a trivial/naive program.
8
9# For each file, it looks at the contents to heuristically determine
10# if C comments are permitted and what the "comment" character is.
11# If /* and */ are in the file, then C comments are permitted.
12# The punctuation mark that starts the most lines must be the comment
13# character (but ignoring "/" if C comments are allowed, and
14# ignoring '#' if cpp commands appear to be used)
15
16# This is part of SLOCCount, a toolsuite that counts
17# source lines of code (SLOC).
18# Copyright (C) 2001-2004 David A. Wheeler.
19#
20# This program is free software; you can redistribute it and/or modify
21# it under the terms of the GNU General Public License as published by
22# the Free Software Foundation; either version 2 of the License, or
23# (at your option) any later version.
24#
25# This program is distributed in the hope that it will be useful,
26# but WITHOUT ANY WARRANTY; without even the implied warranty of
27# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28# GNU General Public License for more details.
29#
30# You should have received a copy of the GNU General Public License
31# along with this program; if not, write to the Free Software
32# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
33#
34# To contact David A. Wheeler, see his website at:
35#  http://www.dwheeler.com.
36
37
38
39$total_sloc = 0;
40
41# Do we have "-f" (read list of files from second argument)?
42if (($#ARGV >= 1) && ($ARGV[0] eq "-f")) {
43  # Yes, we have -f
44  if ($ARGV[1] eq "-") {
45    # The list of files is in STDIN
46    while (<STDIN>) {
47      chomp ($_);
48      &count_file ($_);
49    }
50  } else {
51    # The list of files is in the file $ARGV[1]
52    open (FILEWITHLIST, $ARGV[1]) || die "Error: Could not open $ARGV[1]\n";
53    while (<FILEWITHLIST>) {
54      chomp ($_);
55      &count_file ($_);
56    }
57    close FILEWITHLIST;
58  }
59  shift @ARGV; shift @ARGV;
60}
61# Process all (remaining) arguments as file names
62while ($file = shift @ARGV) {
63  &count_file ($file);
64}
65
66print "Total:\n";
67print "$total_sloc\n";
68
69sub count_file {
70  my ($file) = @_;
71  # First, use heuristics to determine the comment char and if it uses C comments
72  $found_c_start = 0;
73  $found_c_end = 0;
74  $cpp_suspicious = 0;
75  $cpp_likely = 0;
76  $cpp_used = 0;
77  %count = ();
78  if ($file eq "") {
79    *CURRENTFILE = *STDIN
80  } else {
81    open(CURRENTFILE, "<$file");
82  }
83  while (<CURRENTFILE>) {
84    if (m!\/\*!) { $found_c_start++;}
85    if (m!\*\/!) { $found_c_end++;}
86    if ( (m!^#\s*define\s!) || (m!^#\s*else!)) {$cpp_suspicious++;}
87    if ( (m!^#\s*ifdef\s!) || (m!^#\s*endif!) || (m!#\s*include!)) {$cpp_likely++;}
88    if (m/^\s*([;!\/#\@\|\*])/) { $count{$1}++; }  # Found a likely comment char.
89  }
90  # Done examing file, let's figure out the parameters.
91  if ($found_c_start && $found_c_end) {
92    $ccomments = 1;
93    $count{'/'} = 0;
94    # $count{'*'} = 0;  # Do this to ignore '*' if C comments are used.
95  } else {
96    $ccomments = 0;
97  }
98  if (($cpp_suspicious > 2) || ($cpp_likely >= 1)) {
99    $cpp_used = 1;
100    $count{'#'} = 0;
101  } else {
102    $cpp_used = 0;
103  }
104  $likeliest = ';';
105  $likeliest_count = 0;
106  foreach $i (keys(%count)) {
107    # print "DEBUG: key=$i count=$count{$i}\n";
108    if ($count{$i} > $likeliest_count) {
109      $likeliest = $i;
110      $likeliest_count = $count{$i};
111    }
112  }
113  # print "DEBUG: likeliest = $likeliest\n";
114  $commentchar=$likeliest;
115  close(CURRENTFILE);
116
117  # Now count SLOC.
118  $sloc = 0;
119  $isincomment = 0;
120  open(CURRENTFILE, "<$file");
121  while (<CURRENTFILE>) {
122    # We handle C comments first, so that if an EOL-comment
123    # occurs inside a C comment, it's ignored.
124    if ($ccomments) {
125      # Handle C /* */ comments; this will get fooled if they're in strings,
126      # but that would be rare in assembly.
127      while ( (m!\/\*!) || (m!\*\/!)) {  # While unprocessed C comment.
128	if ($isincomment) {
129	  s!.*?\*\/.*!!;
130	  $isincomment = 0;
131	} else {           # Not in C comment, but have end comment marker.
132	  if (! m/\/\*/) {  # Whups, there's no starting marker!
133	    print STDERR "Warning: file $file line $. has unmatched comment end\n";
134	    # Get us back to a plausible state:
135	    s/.*//; # Destroy everything
136	      $isincomment = 0;
137	  } else {
138	    if (! s!\/\*.*?\*\/!!) { # Try to delete whole comment.
139              # We couldn't delete whole comment.  Delete what's there.
140              s!\/\*.*!!;
141              $isincomment = 1;
142	    }
143	  }
144	}
145      }
146    }  # End of handling C comments.
147    # This requires $[ be unchanged.
148    $locate_comment = index($_, $commentchar);
149    if ($locate_comment >= 0) {  # We found a comment character, delete comment
150       $_ = substr($_, 0, $locate_comment);
151       # print "DEBUG New text: @",$_,"@\n";
152    }
153    # old: s/${commentchar}.*//;  # Delete leading comments.
154
155    # FOR DEBUG: print "Finally isincomment=$isincomment line=$_\n";
156    if ((! $isincomment) && (m/\S/)) {$sloc++;}
157  }
158
159  # End-of-file processing
160  print "$sloc (commentchar=$commentchar C-comments=$ccomments) $file\n";
161  $total_sloc += $sloc;
162  $sloc = 0;
163  if ($isincomment) {
164    print STDERR "Missing comment close in file $file\n";
165  }
166}
167