mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: LKML <linux-kernel@vger.kernel.org>
Cc: Greg Kroah-Hartman <gregkh@suse.de>,
	devel <devel@driverdev.osuosl.org>,
	Andy Whitcroft <apw@shadowen.org>
Subject: Tuxradar patching article and [PATCH] scripts/cvt_kernel_style.pl
Date: Tue, 09 Mar 2010 15:32:06 -0800	[thread overview]
Message-ID: <1268177526.1545.62.camel@Joe-Laptop.home> (raw)

There was an article published recently:
http://www.tuxradar.com/content/newbies-guide-hacking-linux-kernel
that seems to have prompted several new contributors (welcome)
to create style reformatting patches.

The article recommends running checkpatch and fixing the various
non-conforming style elements the output produces.

A better solution might be to enhance checkpatch to rewrite the
patch or a file with -f, but that's more than I'd like to do.

Maybe Andy Whitcroft might find a little of his copious spare
time available to start something like that.

For now, here's a little script that reformats source code to
be more a very little more kernel style compatible.

Maybe something like this could be useful to initial contributors
and could be extended as appropriate.

scripts/cvt_kernel_style.pl: Change a source file to more match kernel style

Trivial source file formatter.

Changes a few things to be more kernel style

Convert printk(KERN_<level> to pr_<level>(
Removes unnecessary parenthesis from return
Add space after if, for and while
Convert "for (foo;bar;baz)" to "for (foo; bar; baz)"
Removes multiple semicolons
Convert leading spaces to tabs

Use --source-indent to specify the #space->tab conversion (default 8)

Signed-off-by: Joe Perches <joe@perches.com>
---
diff --git a/scripts/cvt_kernel_style.pl b/scripts/cvt_kernel_style.pl
new file mode 100755
index 0000000..39a8ab7
--- /dev/null
+++ b/scripts/cvt_kernel_style.pl
@@ -0,0 +1,134 @@
+#!/usr/bin/perl -w
+
+# Change some style elements of a source file
+# An imperfect source code rewriter.
+# Might make trivial patches a bit easier.
+#
+# usage: perl scripts/cvt_kernel_style.pl <files>
+#
+# Licensed under the terms of the GNU GPL License version 2
+
+use strict;
+use Getopt::Long qw(:config no_auto_abbrev);
+
+my $P = $0;
+my $V = '0.1';
+
+my $source_indent = 8;
+
+my $version = 0;
+my $help = 0;
+
+if (!GetOptions(
+		'source-indent=i' => \$source_indent,
+		'v|version' => \$version,
+		'h|help|usage' => \$help,
+		)) {
+    die "$P: invalid argument - use --help if necessary\n";
+}
+
+if ($help != 0) {
+    usage();
+    exit 0;
+}
+
+sub usage {
+    print <<EOT;
+usage: $P [options] <files>
+version: $V
+
+Input source file descriptions:
+  --source-indent => How many spaces are used for an indent (default: 8)
+
+Other options:
+  --version => show version
+  --help => show this help information
+EOT
+}
+
+sub check_label {
+    my ($leading, $label) = @_;
+
+    if ($label == "default") {
+	return "$leading$label:";
+    }
+    return "$label:";
+}
+
+sub check_for {
+    my ($leading, $test1, $test2, $test3) = @_;
+
+    $test1 =~ s/^\s+|\s+$//g;
+    $test2 =~ s/^\s+|\s+$//g;
+    $test3 =~ s/^\s+|\s+$//g;
+
+    return "\n${leading}for ($test1; $test2; $test3)";
+}
+
+sub tabify {
+    my ($leading) = @_;
+
+    my $max_spaces_before_tab = $source_indent - 1;
+    my $spaces_to_tab = sprintf("%*s", $source_indent, "");
+#convert leading spaces to tabs
+    1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
+#Remove spaces before a tab
+    1 while $leading =~ s@^([\t]*)([ ]{1,$max_spaces_before_tab})\t@$1\t@g;
+
+    return "\n$leading";
+}
+
+foreach my $file (@ARGV) {
+    my $f;
+    my $text;
+    my $oldtext;
+
+# read the file
+
+    open($f, '<', $file)
+	or die "$P: Can't open $file for read\n";
+    $oldtext = do { local($/) ; <$f> };
+    close($f);
+
+    $text = $oldtext;
+
+# Convert printk(KERN_<level> to pr_<level>(
+    $text =~ s@\bprintk\s*\(\s*KERN_(INFO|WARNING|ERR|ALERT|CRIT|EMERG|NOTICE|CONT)\s*@pr_\L$1\(@g;
+
+# Coalesce long formats
+    1 while $text =~ s@\b((dev_|pr_|netif_|netdev_)[^;]+)\"\s*\n\s*\"@$1@g;
+
+# Remove spaces and tabs before quoted newlines
+    $text =~ s@(\"[^\"]*)[ \t]+\\n@$1\\n@g;
+
+# Add space between KERN_<LEVEL> and open quote
+    $text =~ s@KERN_([A-Z]+)\"@KERN_$1 \"@g;
+
+# Remove unnecessary parentheses around return
+    $text =~ s@\breturn\s+\(([^\)]+)\s*\)\s*;@return $1;@g;
+
+## This doesn't work very well, too many comments modified
+## Put labels (but not "default:" on column 1
+#    $text =~ s@^([ \t]+)([A-Za-z0-9_]+)\s*:[ \t]*:[ \t]*$@check_label($1, $2)@ge;
+
+# Add space after (if, for, or while) and open parenthesis
+    $text =~ s@\b(if|while|for)\(@$1 \(@g;
+
+# Convert "for (foo;bar;baz)" to "for (foo; bar; baz)"
+    $text =~ s@\n([ \t]+)for\s*\([ \t]*([^;]+);[ \t]*([^;]+);[ \t]*([^\)]+)\)@check_for($1, $2, $3, $4)@ge;
+
+# Remove multiple semicolons at end-of-line
+    $text =~ s@;[ \t]*;[ \t]*\n@;\n@g;
+
+# Convert leading spaces to tabs
+    $text =~ s@\n([ \t]+)@tabify($1)@ge;
+
+# write the file if something was changed
+
+    if ($text ne $oldtext) {
+	open($f, '>', $file)
+	    or die "$P: Can't open $file for write\n";
+	print $f $text;
+	close($f);
+    }
+}




             reply	other threads:[~2010-03-09 23:32 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-09 23:32 Joe Perches [this message]
2010-03-09 23:41 ` Greg KH
2010-03-10  0:06   ` Joe Perches
2010-03-10  0:16     ` Greg KH
2010-03-10  0:34       ` Joe Perches
2010-03-10 18:35     ` Stefan Richter
2010-03-10 18:38       ` Joe Perches
2010-03-10  1:22 ` Frans Pop
2010-03-10  3:25   ` [PATCH V2] scripts/cvt_kernel_style.pl: partial "kernel style" pretty-printing Joe Perches
2010-03-24 20:01     ` [PATCH] scripts/cvt_kernel_style.pl: kernel style source code reformatter Joe Perches
2010-03-24 22:31       ` Frans Pop
2010-03-10 18:32 ` Tuxradar patching article and [PATCH] scripts/cvt_kernel_style.pl Stefan Richter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1268177526.1545.62.camel@Joe-Laptop.home \
    --to=joe@perches.com \
    --cc=apw@shadowen.org \
    --cc=devel@driverdev.osuosl.org \
    --cc=gregkh@suse.de \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®