public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 08/19] scripts: Make cleanfile/cleanpatch warn about long lines
@ 2007-06-03 20:47 Sam Ravnborg
  2007-06-04 16:07 ` Jan Engelhardt
  2007-06-05  7:33 ` [kbuild-devel] " Oleg Verych
  0 siblings, 2 replies; 23+ messages in thread
From: Sam Ravnborg @ 2007-06-03 20:47 UTC (permalink / raw)
  To: LKML, Andrew Morton, kbuild-devel

Subject: [PATCH 08/19] scripts: Make cleanfile/cleanpatch warn about long lines
From: H. Peter Anvin <hpa@zytor.com>
Date: Fri, 25 May 2007 17:58:26 -0700

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>
---
 scripts/cleanfile  |   54 ++++++++++++++++++++++++++++++++++++++++++++++-
 scripts/cleanpatch |   58 +++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 107 insertions(+), 5 deletions(-)

diff --git a/scripts/cleanfile b/scripts/cleanfile
index f1ba8aa..cefd29e 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
diff --git a/scripts/cleanpatch b/scripts/cleanpatch
index a53f987..9680d03 100755
--- a/scripts/cleanpatch
+++ b/scripts/cleanpatch
@@ -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) {
@@ -86,6 +128,7 @@ foreach $f ( @ARGV ) {
 
     $in_bytes = 0;
     $out_bytes = 0;
+    $lineno = 0;
 
     @lines  = ();
 
@@ -93,10 +136,12 @@ foreach $f ( @ARGV ) {
     $err = 0;
 
     while ( defined($line = <FILE>) ) {
+	$lineno++;
 	$in_bytes += length($line);
 
 	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;
 		$plus_lines = $4;
 		if ($minus_lines || $plus_lines) {
@@ -117,6 +162,13 @@ foreach $f ( @ARGV ) {
 		$text =~ s/[ \t\r]*$//;		# Remove trailing spaces
 		$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);
 	    } elsif ($line =~ /^\-/) {
 		$minus_lines--;
-- 
1.5.1.rc3.1544.g8a923


----- End forwarded message -----

^ permalink raw reply related	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2007-06-08 20:55 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-06-03 20:47 [PATCH 08/19] scripts: Make cleanfile/cleanpatch warn about long lines Sam Ravnborg
2007-06-04 16:07 ` Jan Engelhardt
2007-06-05  7:33 ` [kbuild-devel] " Oleg Verych
2007-06-05  8:19   ` Sam Ravnborg
2007-06-05 12:48     ` Oleg Verych
2007-06-05 13:38     ` Oleg Verych
2007-06-05 14:12       ` Sam Ravnborg
2007-06-05 14:57         ` Oleg Verych
2007-06-05 15:11           ` Oleg Verych
2007-06-06 17:45             ` Another version of cleanfile/cleanpatch (Re: [PATCH 08/19] scripts: Make cleanfile/cleanpatch warn about long lines) Oleg Verych
2007-06-06 17:50               ` Sam Ravnborg
2007-06-06 19:14                 ` Another version of cleanfile/cleanpatch Oleg Verych
2007-06-07 14:36                   ` Jan Engelhardt
2007-06-07 23:06                     ` Oleg Verych
2007-06-07 23:19                       ` [kbuild-devel] " H. Peter Anvin
2007-06-08  1:35                         ` Oleg Verych
2007-06-08  6:40                         ` [patch] scripts: clean-whitespace.sh Oleg Verych
2007-06-08  6:44                           ` Andrew Morton
2007-06-08 14:28                             ` Sam Ravnborg
2007-06-08 15:02                               ` Oleg Verych
2007-06-08 19:05                                 ` Sam Ravnborg
2007-06-08 21:07                                   ` Oleg Verych
2007-06-08  5:27                       ` Another version of cleanfile/cleanpatch Jan Engelhardt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox