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
|
#!/usr/bin/env perl
#
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2015-2020, Intel Corporation
#
#
# check_whitespace -- scrub source tree for whitespace errors
#
use strict;
use warnings;
use File::Basename;
use File::Find;
use Encode;
use v5.10;
my $Me = $0;
$Me =~ s,.*/,,;
$SIG{HUP} = $SIG{INT} = $SIG{TERM} = $SIG{__DIE__} = sub {
die @_ if $^S;
my $errstr = shift;
die "$Me: ERROR: $errstr";
};
my $Errcount = 0;
#
# err -- emit error, keep total error count
#
sub err {
warn @_, "\n";
$Errcount++;
}
#
# decode_file_as_string -- slurp an entire file into memory and decode
#
sub decode_file_as_string {
my ($full, $file) = @_;
my $fh;
open($fh, '<', $full) or die "$full $!\n";
local $/;
$_ = <$fh>;
close $fh;
# check known encodings or die
my $decoded;
my @encodings = ("UTF-8", "UTF-16", "UTF-16LE", "UTF-16BE");
foreach my $enc (@encodings) {
eval { $decoded = decode( $enc, $_, Encode::FB_CROAK ) };
if (!$@) {
$decoded =~ s/\R/\n/g;
return $decoded;
}
}
die "$Me: ERROR: Unknown file encoding";
}
#
# check_whitespace -- run the checks on the given file
#
sub check_whitespace {
my ($full, $file) = @_;
my $line = 0;
my $eol;
my $nf = 0;
my $fstr = decode_file_as_string($full, $file);
for (split /^/, $fstr) {
$line++;
$eol = /[\n]/s;
if (/^\.nf$/) {
err("$full:$line: ERROR: nested .nf") if $nf;
$nf = 1;
} elsif (/^\.fi$/) {
$nf = 0;
} elsif ($nf == 0) {
chomp;
err("$full:$line: ERROR: trailing whitespace") if /\s$/;
err("$full:$line: ERROR: spaces before tabs") if / \t/;
}
}
err("$full:$line: .nf without .fi") if $nf;
err("$full:$line: noeol") unless $eol;
}
sub check_whitespace_with_exc {
my ($full) = @_;
$_ = $full;
$_ = basename($full);
return 0 unless /^(README.*|LICENSE.*|CMakeLists.txt|.gitignore|check_whitespace|.*\.([chp13s]|cc|sh|map|cpp|hpp|inc|md|cmake))$/;
return 0 if -z;
check_whitespace($full, $_);
return 1;
}
my $verbose = 0;
my $force = 0;
my $recursive = 0;
sub check {
my ($file) = @_;
my $r;
if ($force) {
$r = check_whitespace($file, basename($file));
} else {
$r = check_whitespace_with_exc($file);
}
if ($verbose) {
if ($r == 0) {
printf("skipped $file\n");
} else {
printf("checked $file\n");
}
}
}
my @files = ();
foreach my $arg (@ARGV) {
if ($arg eq '-v') {
$verbose = 1;
next;
}
if ($arg eq '-f') {
$force = 1;
next;
}
if ($arg eq '-r') {
$recursive = 1;
next;
}
if ($arg eq '-g') {
@files = `git ls-tree -r --name-only HEAD`;
chomp(@files);
next;
}
if ($arg eq '-h') {
printf "Options:
-g - check all files tracked by git
-r dir - recursively check all files in specified directory
-v verbose - print whether file was checked or not
-f force - disable blacklist\n";
exit 1;
}
if ($recursive == 1) {
find(sub {
my $full = $File::Find::name;
if (!$force &&
($full eq './.git')) {
$File::Find::prune = 1;
return;
}
return unless -f;
push @files, $full;
}, $arg);
$recursive = 0;
next;
}
push @files, $arg;
}
if (!@files) {
printf "Empty file list!\n";
}
foreach (@files) {
check($_);
}
exit $Errcount;
|