Add an option to disable the block-private-addresses feature
[tor/rransom.git] / contrib / checkSpace.pl
blob6eb32e562074d2f9743a80ad8849983c2e4251a7
1 #!/usr/bin/perl -w
3 if ($ARGV[0] =~ /^-/) {
4 $lang = shift @ARGV;
5 $C = ($lang eq '-C');
6 # $TXT = ($lang eq '-txt');
9 for $fn (@ARGV) {
10 open(F, "$fn");
11 $lastnil = 0;
12 $lastline = "";
13 $incomment = 0;
14 while (<F>) {
15 ## Warn about windows-style newlines.
16 if (/\r/) {
17 print " CR:$fn:$.\n";
19 ## Warn about tabs.
20 if (/\t/) {
21 print " TAB:$fn:$.\n";
23 ## Warn about markers that don't have a space in front of them
24 if (/^[a-zA-Z_][a-zA-Z_0-9]*:/) {
25 print "nosplabel:$fn:$.\n";
27 ## Warn about trailing whitespace.
28 if (/ +$/) {
29 print "Space\@EOL:$fn:$.\n";
31 ## Warn about control keywords without following space.
32 if ($C && /\s(?:if|while|for|switch)\(/) {
33 print " KW(:$fn:$.\n";
35 ## Warn about #else #if instead of #elif.
36 if (($lastline =~ /^\# *else/) and ($_ =~ /^\# *if/)) {
37 print " #else#if:$fn:$.\n";
39 $lastline = $_;
40 ## Warn about unnecessary empty lines.
41 if ($lastnil && /^\s*}\n/) {
42 print " UnnecNL:$fn:$.\n";
44 ## Warn about multiple empty lines.
45 if ($lastnil && /^$/) {
46 print " DoubleNL:$fn:$.\n";
47 } elsif (/^$/) {
48 $lastnil = 1;
49 } else {
50 $lastnil = 0;
52 ## Terminals are still 80 columns wide in my world. I refuse to
53 ## accept double-line lines.
54 if (/^.{80}/) {
55 print " Wide:$fn:$.\n";
57 ### Juju to skip over comments and strings, since the tests
58 ### we're about to do are okay there.
59 if ($C) {
60 if ($incomment) {
61 if (m!\*/!) {
62 s!.*?\*/!!;
63 $incomment = 0;
64 } else {
65 next;
68 if (m!/\*.*?\*/!) {
69 s!\s*/\*.*?\*/!!;
70 } elsif (m!/\*!) {
71 s!\s*/\*!!;
72 $incomment = 1;
73 next;
75 s!"(?:[^\"]+|\\.)*"!"X"!g;
76 next if /^\#/;
77 ## Warn about C++-style comments.
78 if (m!//!) {
79 # print " //:$fn:$.\n";
80 s!//.*!!;
82 ## Warn about unquoted braces preceded by non-space.
83 if (/([^\s'])\{/) {
84 print " $1\{:$fn:$.\n";
86 ## Warn about multiple internal spaces.
87 #if (/[^\s,:]\s{2,}[^\s\\=]/) {
88 # print " X X:$fn:$.\n";
90 ## Warn about { with stuff after.
91 #s/\s+$//;
92 #if (/\{[^\}\\]+$/) {
93 # print " {X:$fn:$.\n";
95 ## Warn about function calls with space before parens.
96 if (/(\w+)\s\(([A-Z]*)/) {
97 if ($1 ne "if" and $1 ne "while" and $1 ne "for" and
98 $1 ne "switch" and $1 ne "return" and $1 ne "int" and
99 $1 ne "elsif" and $1 ne "WINAPI" and $2 ne "WINAPI" and
100 $1 ne "void" and $1 ne "__attribute__") {
101 print " fn ():$fn:$.\n";
104 ## Warn about functions not declared at start of line.
105 if ($in_func_head ||
106 ($fn !~ /\.h$/ && /^[a-zA-Z0-9_]/ &&
107 ! /^(?:const |static )*(?:typedef|struct|union)[^\(]*$/ &&
108 ! /= *\{$/ && ! /;$/)) {
109 if (/.\{$/){
110 print "fn() {:$fn:$.\n";
111 $in_func_head = 0;
112 } elsif (/^\S[^\(]* +\**[a-zA-Z0-9_]+\(/) {
113 $in_func_head = -1; # started with tp fn
114 } elsif (/;$/) {
115 $in_func_head = 0;
116 } elsif (/\{/) {
117 if ($in_func_head == -1) {
118 print "tp fn():$fn:$.\n";
120 $in_func_head = 0;
125 if (! $lastnil) {
126 print " EOL\@EOF:$fn:$.\n";
128 close(F);