count_lines 1.38 KB
Newer Older
1
#! /usr/bin/perl
2
3
4
5
6
7
8
9
10
#
%DirCount = ();
%ModCount = ();
%DirComments = ();
%ModComments = ();

foreach $f ( @ARGV ) {

    if ( $f =~ /\.lhs$/ ) {
11
	open(INF, "../utils/unlit/unlit $f - |") || die "Couldn't unlit $f!\n";
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    } else {
	open(INF, "< $f") || die "Couldn't open $f!\n";
    }
    $cnt = 0;
    while (<INF>) {
	s/--.*//;
	s/{-.*-}//;
    	next if /^\s*$/;
	$cnt++;
    }
    close(INF);

    $f_wc = `wc $f`; die "wc failed: $f\n" if $? != 0;
    if ( $f_wc =~ /\s*(\d+)\s*(\d+)\s*(\d+)/ ) {
	$comments = $1 - $cnt;
    } else {
	die "Can't grok wc format: $f_wc";
    }

    if ( $f =~ /(.*)\/(.*)/ ) {
	local($dir) = $1;
	local($mod) = $2;
	$DirCount{$dir} += $cnt;
	$ModCount{$mod} += $cnt;
	$DirComments{$dir} += $comments;
	$ModComments{$mod} += $comments;
    } else {
	print STDERR "not counted in a directory: $f\n";
	$ModCount{$f} += $cnt;
41
	$ModComments{$f} += $comments;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    }
}

# print the info
$tot = 0;
$totcmts = 0;
foreach $d (sort (keys %DirCount)) {
    printf "%-20s %6d %6d\n", $d, $DirCount{$d}, $DirComments{$d};
    $tot     += $DirCount{$d};
    $totcmts += $DirComments{$d};
}
printf "\n%-20s %6d %6d\n\n\n", 'TOTAL:', $tot, $totcmts;

$tot = 0;
$totcmts = 0;
foreach $m (sort (keys %ModCount)) {
    printf "%-20s %6d %6d\n", $m, $ModCount{$m}, $ModComments{$m};
    $tot += $ModCount{$m};
    $totcmts += $ModComments{$m};
}
printf "\n%-20s %6d %6d\n", 'TOTAL:', $tot, $totcmts;