diff options
| author | H. Peter Anvin <hpa@zytor.com> | 2007-05-25 20:58:26 -0400 |
|---|---|---|
| committer | Sam Ravnborg <sam@ravnborg.org> | 2007-07-16 15:15:50 -0400 |
| 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 f1ba8aa58a40..cefd29e52298 100755 --- a/scripts/cleanfile +++ b/scripts/cleanfile | |||
| @@ -7,7 +7,9 @@ | |||
| 7 | use bytes; | 7 | use bytes; |
| 8 | use File::Basename; | 8 | use File::Basename; |
| 9 | 9 | ||
| 10 | # | 10 | # Default options |
| 11 | $max_width = 79; | ||
| 12 | |||
| 11 | # Clean up space-tab sequences, either by removing spaces or | 13 | # Clean up space-tab sequences, either by removing spaces or |
| 12 | # replacing them with tabs. | 14 | # replacing them with tabs. |
| 13 | sub clean_space_tabs($) | 15 | sub clean_space_tabs($) |
| @@ -48,9 +50,49 @@ sub clean_space_tabs($) | |||
| 48 | return $lo; | 50 | return $lo; |
| 49 | } | 51 | } |
| 50 | 52 | ||
| 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 | |||
| 51 | $name = basename($0); | 78 | $name = basename($0); |
| 52 | 79 | ||
| 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 ) { | ||
| 54 | print STDERR "$name: $f\n"; | 96 | print STDERR "$name: $f\n"; |
| 55 | 97 | ||
| 56 | if (! -f $f) { | 98 | if (! -f $f) { |
| @@ -90,8 +132,10 @@ foreach $f ( @ARGV ) { | |||
| 90 | 132 | ||
| 91 | @blanks = (); | 133 | @blanks = (); |
| 92 | @lines = (); | 134 | @lines = (); |
| 135 | $lineno = 0; | ||
| 93 | 136 | ||
| 94 | while ( defined($line = <FILE>) ) { | 137 | while ( defined($line = <FILE>) ) { |
| 138 | $lineno++; | ||
| 95 | $in_bytes += length($line); | 139 | $in_bytes += length($line); |
| 96 | $line =~ s/[ \t\r]*$//; # Remove trailing spaces | 140 | $line =~ s/[ \t\r]*$//; # Remove trailing spaces |
| 97 | $line = clean_space_tabs($line); | 141 | $line = clean_space_tabs($line); |
| @@ -107,6 +151,12 @@ foreach $f ( @ARGV ) { | |||
| 107 | @blanks = (); | 151 | @blanks = (); |
| 108 | $blank_bytes = 0; | 152 | $blank_bytes = 0; |
| 109 | } | 153 | } |
| 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 | } | ||
| 110 | } | 160 | } |
| 111 | 161 | ||
| 112 | # Any blanks at the end of the file are discarded | 162 | # Any blanks at the end of the file are discarded |
