1#!/usr/local/bin/perl
2# lex_count
3# Usage: lex_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
8# This is part of SLOCCount, a toolsuite that counts
9# source lines of code (SLOC).
10# Copyright (C) 2001-2004 David A. Wheeler.
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25#
26# To contact David A. Wheeler, see his website at:
27#  http://www.dwheeler.com.
28#
29#
30
31$total_sloc = 0;
32
33# Do we have "-f" (read list of files from second argument)?
34if (($#ARGV >= 1) && ($ARGV[0] eq "-f")) {
35  # Yes, we have -f
36  if ($ARGV[1] eq "-") {
37    # The list of files is in STDIN
38    while (<STDIN>) {
39      chomp ($_);
40      &count_file ($_);
41    }
42  } else {
43    # The list of files is in the file $ARGV[1]
44    open (FILEWITHLIST, $ARGV[1]) || die "Error: Could not open $ARGV[1]\n";
45    while (<FILEWITHLIST>) {
46      chomp ($_);
47      &count_file ($_);
48    }
49    close FILEWITHLIST;
50  }
51  shift @ARGV; shift @ARGV;
52}
53# Process all (remaining) arguments as file names
54while ($file = shift @ARGV) {
55  &count_file ($file);
56}
57
58print "Total:\n";
59print "$total_sloc\n";
60
61sub count_file {
62  my ($file) = @_;
63  my $sloc = 0;
64
65  $sloc = `lexcount1 < "$file"`;
66  chomp($sloc);
67  print "$sloc $file\n";
68  $total_sloc += $sloc;
69}
70
71