Fix #3741, simplifying things in the process
[ghc-hetmet.git] / utils / count_lines / count_lines
1 #! /usr/bin/perl
2
3 use FindBin;
4
5 %DirCount = ();
6 %ModCount = ();
7 %DirComments = ();
8 %ModComments = ();
9
10 my $binPath = $FindBin::Bin;
11
12 foreach $f ( @ARGV ) {
13
14     if ( $f =~ /\.lhs$/ ) {
15         open(INF, "$binPath/../../inplace/lib/unlit $f - |") || die "Couldn't unlit $f!\n";
16     } else {
17         open(INF, "< $f") || die "Couldn't open $f!\n";
18     }
19     $cnt = 0;
20     while (<INF>) {
21         s/--.*//;
22         s/{-.*-}//;
23         next if /^\s*$/;
24         $cnt++;
25     }
26     close(INF);
27
28     $f_wc = `wc $f`; die "wc failed: $f\n" if $? != 0;
29     if ( $f_wc =~ /\s*(\d+)\s*(\d+)\s*(\d+)/ ) {
30         $comments = $1 - $cnt;
31     } else {
32         die "Can't grok wc format: $f_wc";
33     }
34
35     if ( $f =~ /(.*)\/(.*)/ ) {
36         local($dir) = $1;
37         local($mod) = $2;
38         $DirCount{$dir} += $cnt;
39         $ModCount{$mod} += $cnt;
40         $DirComments{$dir} += $comments;
41         $ModComments{$mod} += $comments;
42     } else {
43         print STDERR "not counted in a directory: $f\n";
44         $ModCount{$f} += $cnt;
45         $ModComments{$f} += $comments;
46     }
47 }
48
49 # print the info
50 $tot = 0;
51 $totcmts = 0;
52 foreach $d (sort (keys %DirCount)) {
53     printf "%-20s %6d %6d\n", $d, $DirCount{$d}, $DirComments{$d};
54     $tot     += $DirCount{$d};
55     $totcmts += $DirComments{$d};
56 }
57 printf "\n%-20s %6d %6d\n\n\n", 'TOTAL:', $tot, $totcmts;
58
59 $tot = 0;
60 $totcmts = 0;
61 printf "\n                      Code  Comments\n";
62 foreach $m (sort (keys %ModCount)) {
63     printf "%-20s %6d %6d\n", $m, $ModCount{$m}, $ModComments{$m};
64     $tot += $ModCount{$m};
65     $totcmts += $ModComments{$m};
66 }
67 printf "\n%-20s %6d %6d\n", 'TOTAL:', $tot, $totcmts;