summaryrefslogtreecommitdiff
path: root/bin/count_lines
blob: e2ba66d945f1260b0af7becc0eb269c5f803b83a (plain)
1
2
3
4
5
6
7
8
9
10
11
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
    & eval 'exec perl -S $0 $argv:q'
    if 0;

# -*- perl -*-
# $Id$
#

use File::Basename;

$cmd= basename($0);

if ($#ARGV < 0) {
    die "Usage: $cmd module...\n";
}

@match_order = ();
%typeRE = ();

&initfiletypes;

foreach $module (@ARGV) {
    if ( ! -d $module ) {
	warn "$cmd: no such directory $module\n";
	next;
    }

    open(DIRS, "find $module -type d|");
    @dirs = grep {if(!/CVS/) {chop;}} <DIRS>;
    close(DIRS);

    open(FIND, "find $module -type f|");
    @files = grep {if (!/build/
		       && !/CVS/
		       && !/rpc\+\+/
		       && !/\.bpr$/
		       && !/\.mdp$/
		       && !/\.MDP$/
		       && !/\.dsp$/
		       && !/\.DSP$/
		       && !/\.dsw$/
		       && !/\.DSW$/
		       && !/\.mak$/
		       && !/\.MAK$/
		       && !/\.o$/
		       && !/\.sl$/
		       && !/\.a$/) {chop;}} <FIND>;
    close(FIND);

    $totdirs = $#dirs;
    $totfiles = $#files;
    $totlines = 0;
    %lines = ();

    foreach $file (@files) {
	$n = 0;
	if (!open(IN, $file)) {
	    warn "$cmd: cannot open '$file' for reading\n";
	    next;
	}
	while(<IN>) {
	    $n++;
	}
	close(IN);
	$lines{$file} = $n;
	$totlines += $n;
    }

    
    # Define two associative arrays to keep the results for each kind
    # of file. 
    %linespertype = ();
    %filespertype = ();
    foreach $type (keys %typeRE) {
	$linespertype{$type} = 0;
	$filespertype{$type} = 0;
    }
    # The file is classified and added to the corresponding variable.
    FILE: while (($file, $l) = each %lines) {
	foreach $type (@match_order) {
	    $re = $typeRE{$type};
	    if($file =~ m/$re/) {
		$linespertype{$type} += $l;
		$filespertype{$type}++;
		next FILE;
	    }
	}
	print STDERR "Unmatched file: $file\n";
    }

    format STDOUT_TOP=
                                      @|||
                                       $%

                Lines of code in module @<<<<<<<<<<<<<<<<<<<<
                                              $module

Files:                 @>>>>>>>>
                       $totfiles
Directories:           @>>>>>>>>
                       $totdirs
Lines:                 @>>>>>>>>
                       $totlines

File type                    lines              files
---------------------------- ------------------ -----------------
.
    format STDOUT=
@<<<<<<<<<<<<<<<<<<<<<<<<<<< @>>>>>>>>>>>>>>>>> @>>>>>>>>>>>>>>>>
$type,                       $typelines,        $typefiles
.

    
    foreach $type (@match_order) {
	$typelines = $linespertype{$type};
	$typefiles = $filespertype{$type};
	if ($typefiles != 0) {
	    write;
	}
    }
    # Forzamos un newpage para cada modulo.
    $- = 0;
}

sub updateRE {
    local $desc = shift;
    local $re = shift;
    
    if (exists $typeRE{$desc}) {
	local $mix = $typeRE{$desc} . '|(' . $re . ')';
	$typeRE{$desc} = $mix; 
    } else {
	$typeRE{$desc} = '(' . $re . ')';
	push @match_order, $desc;
    }
}

sub initfiletypes {
    # Here we define the regular expressions for each kind of file.
    # This RE must be mutually exclusive, a file will not be counted
    # twice, but it could be unproperly classified.

    local %filenames =
	('Makefile'     => 'Makefile',
	 'Makefile.*'   => 'Makefile',
	 'README'       => 'README files',
	 'COPYING'      => 'Licenses',
	 'LICENSE.*'    => 'Licenses',
	 'ChangeLog.*'  => 'ChangeLog',
	 'ChangeLog-.*' => 'ChangeLog',
	 '.cvsignore'   => 'Control CVS',
	 'run_test.pl'  => 'Test driver',
	 'run_test'     => 'Test driver',
	 'run_tests'    => 'Test driver',
	 'run_test.sh'  => 'Test driver');
    local %fileexts =
	('cc'    => 'C++ sources',
	 'cpp'   => 'C++ sources',
	 'inl'   => 'C++ sources',
	 'i'     => 'C++ sources',
	 'h'     => 'Headers',
	 'hh'    => 'Headers',
	 'c'     => 'C/C++ sources',
	 'idl'   => 'IDL sources',
	 'IDL'   => 'IDL sources',
	 'pidl'  => 'IDL sources',
	 'y'     => 'yacc source',
	 'yy'    => 'yacc source',
	 'l'     => 'lex source',
	 'll'    => 'lex source',
	 'pm'    => 'perl script',
	 'pl'    => 'perl script',
	 'perl'  => 'perl script',
	 'GNU'   => 'GNU make config',
	 'tex'   => '(La)TeX',
	 'txt'   => 'Text files',
	 '1'     => 'man pages',
	 '3'     => 'man pages',
	 'html'  => 'HTML',
	 'bib'   => 'BibTeX',
	 'sty'   => 'TeX styles',
	 'bld'   => 'VxWorks build file',
	 'am'    => 'Automake file',
	 'icc'   => 'VisualAge project files',
	 'icp'   => 'VisualAge project files',
	 'vac'   => 'VisualAge project files',
	 'vcp'   => 'Microsof eMbedded Visual Tools project files',
	 'vcw'   => 'Microsof eMbedded Visual Tools project files',
	 'bpr'   => 'Borland project files',
	 'bor'   => 'Borland project files',
	 'dsp'   => 'DevStudio project files',
	 'DSP'   => 'DevStudio project files',
	 'mdp'   => 'MSVC project files',
	 'MDP'   => 'MSVC project files',
	 'dsw'   => 'MSVC workspaces',
	 'DSW'   => 'MSVC workspaces',
	 'mak'   => 'MSVC MAK files',
	 'MAK'   => 'MSVC MAK files',
	 'java'  => 'JAVA source',
	 'class' => 'JAVA class',
	 'cccc'  => 'codecount output',
	 'gif'   => 'GIF images',
	 'conf'  => 'Svc_Config files',
	 'diff'  => 'patches',
	 'zip'   => 'Compressed files',
	 'gz'    => 'Compressed files',
	 'EXE'   => 'Win32 executable',
	 'shar'  => 'Shar archive',
	 'mib'   => 'MIB definition files',
	 'gperf' => 'GPERF input',
	 'phil.*'=> 'Test driver'
	 );
    local %paths = ();

    local ($desc, $reseed);
    while (($reseed, $desc) = each %filenames) {
	local $re = '/' . $reseed . '$';
	updateRE($desc, $re);
    }
    while (($reseed, $desc) = each %fileexts) {
	local $re = '/[^/]*\.' . $reseed . '$';
	updateRE($desc, $re);
    }
    while (($reseed, $desc) = each %paths) {
	local $re = $reseed;
	updateRE($desc, $re);
    }

     updateRE('Others', '.*');

#    while (($desc, $reseed) = each %typeRE) {
#	print STDERR $desc, " ==> ", $reseed, "\n";
#    }
}