Skip to content

Commit cb3ed5b

Browse files
H. Peter Anvinsravnborg
authored andcommitted
scripts: Make cleanfile/cleanpatch warn about long lines
Make the "cleanfile" and "cleanpatch" script warn about long lines, by default lines whose visual width exceeds 79 characters. Per suggestion from Auke Kok. Signed-off-by: H. Peter Anvin <hpa@zytor.com> Signed-off-by: Sam Ravnborg <sam@ravnborg.org>
1 parent d72e5ed commit cb3ed5b

2 files changed

Lines changed: 107 additions & 5 deletions

File tree

scripts/cleanfile

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
use bytes;
88
use File::Basename;
99

10-
#
10+
# Default options
11+
$max_width = 79;
12+
1113
# Clean up space-tab sequences, either by removing spaces or
1214
# replacing them with tabs.
1315
sub clean_space_tabs($)
@@ -48,9 +50,49 @@ sub clean_space_tabs($)
4850
return $lo;
4951
}
5052

53+
# Compute the visual width of a string
54+
sub strwidth($) {
55+
no bytes; # Tab alignment depends on characters
56+
57+
my($li) = @_;
58+
my($c, $i);
59+
my $pos = 0;
60+
my $mlen = 0;
61+
62+
for ($i = 0; $i < length($li); $i++) {
63+
$c = substr($li,$i,1);
64+
if ($c eq "\t") {
65+
$pos = ($pos+8) & ~7;
66+
} elsif ($c eq "\n") {
67+
$mlen = $pos if ($pos > $mlen);
68+
$pos = 0;
69+
} else {
70+
$pos++;
71+
}
72+
}
73+
74+
$mlen = $pos if ($pos > $mlen);
75+
return $mlen;
76+
}
77+
5178
$name = basename($0);
5279

53-
foreach $f ( @ARGV ) {
80+
@files = ();
81+
82+
while (defined($a = shift(@ARGV))) {
83+
if ($a =~ /^-/) {
84+
if ($a eq '-width' || $a eq '-w') {
85+
$max_width = shift(@ARGV)+0;
86+
} else {
87+
print STDERR "Usage: $name [-width #] files...\n";
88+
exit 1;
89+
}
90+
} else {
91+
push(@files, $a);
92+
}
93+
}
94+
95+
foreach $f ( @files ) {
5496
print STDERR "$name: $f\n";
5597

5698
if (! -f $f) {
@@ -90,8 +132,10 @@ foreach $f ( @ARGV ) {
90132

91133
@blanks = ();
92134
@lines = ();
135+
$lineno = 0;
93136

94137
while ( defined($line = <FILE>) ) {
138+
$lineno++;
95139
$in_bytes += length($line);
96140
$line =~ s/[ \t\r]*$//; # Remove trailing spaces
97141
$line = clean_space_tabs($line);
@@ -107,6 +151,12 @@ foreach $f ( @ARGV ) {
107151
@blanks = ();
108152
$blank_bytes = 0;
109153
}
154+
155+
$l_width = strwidth($line);
156+
if ($max_width && $l_width > $max_width) {
157+
print STDERR
158+
"$f:$lineno: line exceeds $max_width characters ($l_width)\n";
159+
}
110160
}
111161

112162
# Any blanks at the end of the file are discarded

scripts/cleanpatch

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
use bytes;
88
use File::Basename;
99

10-
#
10+
# Default options
11+
$max_width = 79;
12+
1113
# Clean up space-tab sequences, either by removing spaces or
1214
# replacing them with tabs.
1315
sub clean_space_tabs($)
@@ -48,9 +50,49 @@ sub clean_space_tabs($)
4850
return $lo;
4951
}
5052

53+
# Compute the visual width of a string
54+
sub strwidth($) {
55+
no bytes; # Tab alignment depends on characters
56+
57+
my($li) = @_;
58+
my($c, $i);
59+
my $pos = 0;
60+
my $mlen = 0;
61+
62+
for ($i = 0; $i < length($li); $i++) {
63+
$c = substr($li,$i,1);
64+
if ($c eq "\t") {
65+
$pos = ($pos+8) & ~7;
66+
} elsif ($c eq "\n") {
67+
$mlen = $pos if ($pos > $mlen);
68+
$pos = 0;
69+
} else {
70+
$pos++;
71+
}
72+
}
73+
74+
$mlen = $pos if ($pos > $mlen);
75+
return $mlen;
76+
}
77+
5178
$name = basename($0);
5279

53-
foreach $f ( @ARGV ) {
80+
@files = ();
81+
82+
while (defined($a = shift(@ARGV))) {
83+
if ($a =~ /^-/) {
84+
if ($a eq '-width' || $a eq '-w') {
85+
$max_width = shift(@ARGV)+0;
86+
} else {
87+
print STDERR "Usage: $name [-width #] files...\n";
88+
exit 1;
89+
}
90+
} else {
91+
push(@files, $a);
92+
}
93+
}
94+
95+
foreach $f ( @files ) {
5496
print STDERR "$name: $f\n";
5597

5698
if (! -f $f) {
@@ -86,17 +128,20 @@ foreach $f ( @ARGV ) {
86128

87129
$in_bytes = 0;
88130
$out_bytes = 0;
131+
$lineno = 0;
89132

90133
@lines = ();
91134

92135
$in_hunk = 0;
93136
$err = 0;
94137

95138
while ( defined($line = <FILE>) ) {
139+
$lineno++;
96140
$in_bytes += length($line);
97141

98142
if (!$in_hunk) {
99-
if ($line =~ /^\@\@\s+\-([0-9]+),([0-9]+)\s+\+([0-9]+),([0-9]+)\s\@\@/) {
143+
if ($line =~
144+
/^\@\@\s+\-([0-9]+),([0-9]+)\s+\+([0-9]+),([0-9]+)\s\@\@/) {
100145
$minus_lines = $2;
101146
$plus_lines = $4;
102147
if ($minus_lines || $plus_lines) {
@@ -117,6 +162,13 @@ foreach $f ( @ARGV ) {
117162
$text =~ s/[ \t\r]*$//; # Remove trailing spaces
118163
$text = clean_space_tabs($text);
119164

165+
$l_width = strwidth($text);
166+
if ($max_width && $l_width > $max_width) {
167+
print STDERR
168+
"$f:$lineno: adds line exceeds $max_width ",
169+
"characters ($l_width)\n";
170+
}
171+
120172
push(@hunk_lines, '+'.$text);
121173
} elsif ($line =~ /^\-/) {
122174
$minus_lines--;

0 commit comments

Comments
 (0)