Purge GHC of literate Perl
[ghc.git] / utils / count_lines / count_lines.pl
1 #!/usr/bin/env 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 s/\/\/.*//;
24 next if /^\s*$/;
25 $cnt++;
26 }
27 close(INF);
28
29 $f_wc = `wc $f`; die "wc failed: $f\n" if $? != 0;
30 if ( $f_wc =~ /\s*(\d+)\s*(\d+)\s*(\d+)/ ) {
31 $comments = $1 - $cnt;
32 } else {
33 die "Can't grok wc format: $f_wc";
34 }
35
36 if ( $f =~ /(.*)\/(.*)/ ) {
37 local($dir) = $1;
38 local($mod) = $2;
39 $DirCount{$dir} += $cnt;
40 $ModCount{$mod} += $cnt;
41 $DirComments{$dir} += $comments;
42 $ModComments{$mod} += $comments;
43 } else {
44 print STDERR "not counted in a directory: $f\n";
45 $ModCount{$f} += $cnt;
46 $ModComments{$f} += $comments;
47 }
48 }
49
50 # print the info
51 $tot = 0;
52 $totcmts = 0;
53 printf "\n Code Comments\n";
54 foreach $d (sort (keys %DirCount)) {
55 printf "%-20s %6d %6d\n", $d, $DirCount{$d}, $DirComments{$d};
56 $tot += $DirCount{$d};
57 $totcmts += $DirComments{$d};
58 }
59 printf "\n%-20s %6d %6d\n\n\n", 'TOTAL:', $tot, $totcmts;
60
61 $tot = 0;
62 $totcmts = 0;
63 printf "\n Code Comments\n";
64 foreach $m (sort (keys %ModCount)) {
65 printf "%-20s %6d %6d\n", $m, $ModCount{$m}, $ModComments{$m};
66 $tot += $ModCount{$m};
67 $totcmts += $ModComments{$m};
68 }
69 printf "\n%-20s %6d %6d\n", 'TOTAL:', $tot, $totcmts;