1#!/usr/local/bin/perl
2# sql_count - count physical lines of code in SQL.
3
4# SQL is really screwed up in its commenting system.
5# In ANSI, "--" means start of comment, but this causes many problems
6# with automatically generated SQL queries.  For example, given:
7#   UPDATE tbl_name SET credit=credit-!payment!
8# If !payment! is automatically substituted for a negative number,
9# a comment is unexpectedly generated.
10
11# So, this program accepts "-- " (dash-dash-space) as a comment character.
12# It also supports "#" and /* .. */, which are supported by MySQL.
13
14# This is part of SLOCCount, a toolsuite that counts
15# source lines of code (SLOC).
16# Copyright (C) 2001-2004 David A. Wheeler.
17#
18# This program is free software; you can redistribute it and/or modify
19# it under the terms of the GNU General Public License as published by
20# the Free Software Foundation; either version 2 of the License, or
21# (at your option) any later version.
22#
23# This program is distributed in the hope that it will be useful,
24# but WITHOUT ANY WARRANTY; without even the implied warranty of
25# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26# GNU General Public License for more details.
27#
28# You should have received a copy of the GNU General Public License
29# along with this program; if not, write to the Free Software
30# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
31#
32# To contact David A. Wheeler, see his website at:
33#  http://www.dwheeler.com.
34#
35#
36
37$total_sloc = 0;
38
39# Do we have "-f" (read list of files from second argument)?
40if (($#ARGV >= 1) && ($ARGV[0] eq "-f")) {
41  # Yes, we have -f
42  if ($ARGV[1] eq "-") {
43    # The list of files is in STDIN
44    while (<STDIN>) {
45      chomp ($_);
46      &count_file ($_);
47    }
48  } else {
49    # The list of files is in the file $ARGV[1]
50    open (FILEWITHLIST, $ARGV[1]) || die "Error: Could not open $filewithlist\n";
51    while (<FILEWITHLIST>) {
52      chomp ($_);
53      &count_file ($_);
54    }
55    close FILEWITHLIST;
56  }
57  shift @ARGV; shift @ARGV;
58}
59# Process all (remaining) arguments as file names
60while ($file = shift @ARGV) {
61  &count_file ($file);
62}
63
64print "Total:\n";
65print "$total_sloc\n";
66
67sub count_file {
68  my ($file) = @_;
69  my $sloc = 0;
70
71 $result = `sed -e "s/#.*//" -e "s/-- .*//" < "$file" | c_count`;
72 $result =~ m/^\s*([0-9]+)/;
73 $sloc = $1;
74 print "$sloc $file\n";
75 $total_sloc += $sloc;
76}
77