diff options
author | H. Peter Anvin <hpa@zytor.com> | 2007-05-25 17:58:26 -0700 |
---|---|---|
committer | Sam Ravnborg <sam@ravnborg.org> | 2007-07-16 21:15:50 +0200 |
commit | cb3ed5b7e09c6c0462e396d55e3fecc0980a333a (patch) | |
tree | ead4dd6f67c2350096b17791d79e414c984f79f9 /scripts/cleanfile | |
parent | d72e5edbf4d13adfe489e9e6114a4922891ddcb2 (diff) |
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>
Diffstat (limited to 'scripts/cleanfile')
-rwxr-xr-x | scripts/cleanfile | 54 |
1 files changed, 52 insertions, 2 deletions
diff --git a/scripts/cleanfile b/scripts/cleanfile index f1ba8aa58a4..cefd29e5229 100755 --- a/scripts/cleanfile +++ b/scripts/cleanfile @@ -7,7 +7,9 @@ use bytes; use File::Basename; -# +# Default options +$max_width = 79; + # Clean up space-tab sequences, either by removing spaces or # replacing them with tabs. sub clean_space_tabs($) @@ -48,9 +50,49 @@ sub clean_space_tabs($) 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); -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"; if (! -f $f) { @@ -90,8 +132,10 @@ foreach $f ( @ARGV ) { @blanks = (); @lines = (); + $lineno = 0; while ( defined($line = <FILE>) ) { + $lineno++; $in_bytes += length($line); $line =~ s/[ \t\r]*$//; # Remove trailing spaces $line = clean_space_tabs($line); @@ -107,6 +151,12 @@ foreach $f ( @ARGV ) { @blanks = (); $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 |