use strict;
use warnings;
sub checkcmd {
my ($cmd) = @_;
my @paths;
if($^O eq 'MSWin32' || $^O eq 'dos' || $^O eq 'os2') {
@paths = (split(';', $ENV{'PATH'}));
}
else {
@paths = (split(':', $ENV{'PATH'}), "/usr/sbin", "/usr/local/sbin",
"/sbin", "/usr/bin", "/usr/local/bin");
}
for(@paths) {
if(-x "$_/$cmd" && ! -d "$_/$cmd") {
return "$_/$cmd";
}
}
return "";
}
my $pmccabe = checkcmd("pmccabe");
if(!$pmccabe) {
print "Make sure 'pmccabe' exists in your PATH\n";
exit 1;
}
if(! -r "lib/url.c" || ! -r "lib/urldata.h") {
print "Invoke this script in the curl source tree root\n";
exit 1;
}
my @files;
open(my $git, "-|", "git", "ls-files", "*.c") or die "git ls-files failed: $!";
while(<$git>) {
chomp $_;
my $file = $_;
if($file =~ /^(lib|src)/) {
push @files, $file;
}
}
close($git);
open(my $pmc, "-|", $pmccabe, @files) or die "pmccabe failed: $!";
my @output = <$pmc>;
close($pmc);
my %whitelist = (
);
my $cutoff = 500;
my $top = $ARGV[0] ? $ARGV[0] : 25;
my $error = 0;
my %where;
my %perm;
my $funcs = 0;
my $alllines = 0;
for my $l (@output) {
chomp $l;
if($l =~ /^(\d+)\t\d+\t\d+\t\d+\t(\d+)\t([^\(]+).*: ([^ ]*)/) {
my ($score, $length, $path, $func) = ($1, $2, $3, $4);
my $allow = 0;
if($whitelist{$func} &&
($length <= $whitelist{$func})) {
$allow = 1;
}
$where{"$path:$func"} = $length;
$perm{"$path:$func"} = $allow;
if(($length > $cutoff) && !$allow) {
$error++;
}
$alllines += $length;
$funcs++;
}
}
my $showncutoff;
for my $e (sort {$where{$b} <=> $where{$a}} keys %where) {
if(!$showncutoff &&
($where{$e} <= $cutoff)) {
print "\n---- threshold: $cutoff ----\n\n";
$showncutoff = 1;
}
printf "%-5d %s%s\n", $where{$e}, $e,
$perm{$e} ? " [ALLOWED]" : "";
if(!--$top) {
last;
}
}
printf "\nAverage function length: %.2f lines\n", $alllines/$funcs;
exit $error;