Commit cb3ed5b7 authored by H. Peter Anvin's avatar H. Peter Anvin Committed by Sam Ravnborg

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: default avatarH. Peter Anvin <hpa@zytor.com>
Signed-off-by: default avatarSam Ravnborg <sam@ravnborg.org>
parent d72e5edb
...@@ -7,7 +7,9 @@ ...@@ -7,7 +7,9 @@
use bytes; use bytes;
use File::Basename; use File::Basename;
# # Default options
$max_width = 79;
# Clean up space-tab sequences, either by removing spaces or # Clean up space-tab sequences, either by removing spaces or
# replacing them with tabs. # replacing them with tabs.
sub clean_space_tabs($) sub clean_space_tabs($)
...@@ -48,9 +50,49 @@ sub clean_space_tabs($) ...@@ -48,9 +50,49 @@ sub clean_space_tabs($)
return $lo; return $lo;
} }
# Compute the visual width of a string
sub strwidth($) {
no bytes; # Tab alignment depends on characters
my($li) = @_;
my($c, $i);
my $pos = 0;
my $mlen = 0;
for ($i = 0; $i < length($li); $i++) {
$c = substr($li,$i,1);
if ($c eq "\t") {
$pos = ($pos+8) & ~7;
} elsif ($c eq "\n") {
$mlen = $pos if ($pos > $mlen);
$pos = 0;
} else {
$pos++;
}
}
$mlen = $pos if ($pos > $mlen);
return $mlen;
}
$name = basename($0); $name = basename($0);
foreach $f ( @ARGV ) { @files = ();
while (defined($a = shift(@ARGV))) {
if ($a =~ /^-/) {
if ($a eq '-width' || $a eq '-w') {
$max_width = shift(@ARGV)+0;
} else {
print STDERR "Usage: $name [-width #] files...\n";
exit 1;
}
} else {
push(@files, $a);
}
}
foreach $f ( @files ) {
print STDERR "$name: $f\n"; print STDERR "$name: $f\n";
if (! -f $f) { if (! -f $f) {
...@@ -90,8 +132,10 @@ foreach $f ( @ARGV ) { ...@@ -90,8 +132,10 @@ foreach $f ( @ARGV ) {
@blanks = (); @blanks = ();
@lines = (); @lines = ();
$lineno = 0;
while ( defined($line = <FILE>) ) { while ( defined($line = <FILE>) ) {
$lineno++;
$in_bytes += length($line); $in_bytes += length($line);
$line =~ s/[ \t\r]*$//; # Remove trailing spaces $line =~ s/[ \t\r]*$//; # Remove trailing spaces
$line = clean_space_tabs($line); $line = clean_space_tabs($line);
...@@ -107,6 +151,12 @@ foreach $f ( @ARGV ) { ...@@ -107,6 +151,12 @@ foreach $f ( @ARGV ) {
@blanks = (); @blanks = ();
$blank_bytes = 0; $blank_bytes = 0;
} }
$l_width = strwidth($line);
if ($max_width && $l_width > $max_width) {
print STDERR
"$f:$lineno: line exceeds $max_width characters ($l_width)\n";
}
} }
# Any blanks at the end of the file are discarded # Any blanks at the end of the file are discarded
......
...@@ -7,7 +7,9 @@ ...@@ -7,7 +7,9 @@
use bytes; use bytes;
use File::Basename; use File::Basename;
# # Default options
$max_width = 79;
# Clean up space-tab sequences, either by removing spaces or # Clean up space-tab sequences, either by removing spaces or
# replacing them with tabs. # replacing them with tabs.
sub clean_space_tabs($) sub clean_space_tabs($)
...@@ -48,9 +50,49 @@ sub clean_space_tabs($) ...@@ -48,9 +50,49 @@ sub clean_space_tabs($)
return $lo; return $lo;
} }
# Compute the visual width of a string
sub strwidth($) {
no bytes; # Tab alignment depends on characters
my($li) = @_;
my($c, $i);
my $pos = 0;
my $mlen = 0;
for ($i = 0; $i < length($li); $i++) {
$c = substr($li,$i,1);
if ($c eq "\t") {
$pos = ($pos+8) & ~7;
} elsif ($c eq "\n") {
$mlen = $pos if ($pos > $mlen);
$pos = 0;
} else {
$pos++;
}
}
$mlen = $pos if ($pos > $mlen);
return $mlen;
}
$name = basename($0); $name = basename($0);
foreach $f ( @ARGV ) { @files = ();
while (defined($a = shift(@ARGV))) {
if ($a =~ /^-/) {
if ($a eq '-width' || $a eq '-w') {
$max_width = shift(@ARGV)+0;
} else {
print STDERR "Usage: $name [-width #] files...\n";
exit 1;
}
} else {
push(@files, $a);
}
}
foreach $f ( @files ) {
print STDERR "$name: $f\n"; print STDERR "$name: $f\n";
if (! -f $f) { if (! -f $f) {
...@@ -86,6 +128,7 @@ foreach $f ( @ARGV ) { ...@@ -86,6 +128,7 @@ foreach $f ( @ARGV ) {
$in_bytes = 0; $in_bytes = 0;
$out_bytes = 0; $out_bytes = 0;
$lineno = 0;
@lines = (); @lines = ();
...@@ -93,10 +136,12 @@ foreach $f ( @ARGV ) { ...@@ -93,10 +136,12 @@ foreach $f ( @ARGV ) {
$err = 0; $err = 0;
while ( defined($line = <FILE>) ) { while ( defined($line = <FILE>) ) {
$lineno++;
$in_bytes += length($line); $in_bytes += length($line);
if (!$in_hunk) { if (!$in_hunk) {
if ($line =~ /^\@\@\s+\-([0-9]+),([0-9]+)\s+\+([0-9]+),([0-9]+)\s\@\@/) { if ($line =~
/^\@\@\s+\-([0-9]+),([0-9]+)\s+\+([0-9]+),([0-9]+)\s\@\@/) {
$minus_lines = $2; $minus_lines = $2;
$plus_lines = $4; $plus_lines = $4;
if ($minus_lines || $plus_lines) { if ($minus_lines || $plus_lines) {
...@@ -117,6 +162,13 @@ foreach $f ( @ARGV ) { ...@@ -117,6 +162,13 @@ foreach $f ( @ARGV ) {
$text =~ s/[ \t\r]*$//; # Remove trailing spaces $text =~ s/[ \t\r]*$//; # Remove trailing spaces
$text = clean_space_tabs($text); $text = clean_space_tabs($text);
$l_width = strwidth($text);
if ($max_width && $l_width > $max_width) {
print STDERR
"$f:$lineno: adds line exceeds $max_width ",
"characters ($l_width)\n";
}
push(@hunk_lines, '+'.$text); push(@hunk_lines, '+'.$text);
} elsif ($line =~ /^\-/) { } elsif ($line =~ /^\-/) {
$minus_lines--; $minus_lines--;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment