summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordes <des@FreeBSD.org>2005-12-13 22:09:50 +0000
committerdes <des@FreeBSD.org>2005-12-13 22:09:50 +0000
commit7f2789bdd6489fb226a9d5ec35869a04f9bfbf1c (patch)
tree0fcbd4de8d3b6b0a362057cf10bf5c43698c3dd1
parente1faa6b5ba7aef38278abc4c0ae7df0b06550019 (diff)
downloadFreeBSD-src-7f2789bdd6489fb226a9d5ec35869a04f9bfbf1c.zip
FreeBSD-src-7f2789bdd6489fb226a9d5ec35869a04f9bfbf1c.tar.gz
Add a script that converts K&R-style function definitions to ANSI style.
-rw-r--r--tools/tools/README1
-rw-r--r--tools/tools/ansify/Makefile6
-rw-r--r--tools/tools/ansify/ansify.pl140
3 files changed, 147 insertions, 0 deletions
diff --git a/tools/tools/README b/tools/tools/README
index 313fe47..ddb3389 100644
--- a/tools/tools/README
+++ b/tools/tools/README
@@ -8,6 +8,7 @@ the other categories.
Please make a subdir per program, and add a brief description to this
file.
+ansify Convert K&R-style function definitions to ANSI style
ath Tools specific to the Atheros 802.11 support
backout_commit A tool for reading in a commit message and generating
a script that will backout the commit.
diff --git a/tools/tools/ansify/Makefile b/tools/tools/ansify/Makefile
new file mode 100644
index 0000000..9519a4c
--- /dev/null
+++ b/tools/tools/ansify/Makefile
@@ -0,0 +1,6 @@
+# $FreeBSD$
+
+SCRIPTS= ansify.pl
+BINDIR?= /usr/local/bin
+
+.include <bsd.prog.mk>
diff --git a/tools/tools/ansify/ansify.pl b/tools/tools/ansify/ansify.pl
new file mode 100644
index 0000000..4ae8de9
--- /dev/null
+++ b/tools/tools/ansify/ansify.pl
@@ -0,0 +1,140 @@
+#!/usr/bin/perl -w
+#-
+# Copyright (c) 2005 Dag-Erling Coïdan Smørgrav
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer
+# in this position and unchanged.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+#
+# $FreeBSD$
+#
+
+use v5.6.0;
+use strict;
+
+sub ansify($$$) {
+ my $ifh = shift;
+ my $ofh = shift;
+ my $fn = shift;
+
+ my $line = 0;
+ OUTER:
+ while (<$ifh>) {
+ # look for K&R-style function definitions
+ if (m/^(\w+)\s*\(([\w,\s]+)\)$/) {
+ my @saved = ($_);
+ my $func = $1;
+ my @args = split(/\s*,\s*/, $2);
+ my $arglist = "";
+ # capture K&R-style argument list
+ while (<$ifh>) {
+ push(@saved, $_);
+ last if (m/^\{\s*$/);
+ $arglist .= $_;
+ }
+ # remove comments (common in vfs code)
+ $arglist =~ s/\/\*([^*]|\*[^\/])*\*\// /gs;
+ # identify type of each argument
+ my %type;
+ foreach (split('\n', $arglist)) {
+ s/\s+/ /g;
+ if (!/^\s*([\w\s\*]+?)\s*(\w+?);\s*$/) {
+ warn("[$fn:$line] $func(): can't parse argument list\n");
+ print $ofh @saved;
+ $line += @saved;
+ next OUTER;
+ }
+ $type{$2} = $1;
+ }
+ # construct ANSI-style function definition
+ my $repl = "$func(";
+ foreach my $arg (@args) {
+ # missing arguments in argument list
+ if (!exists($type{$arg})) {
+ warn("[$fn:$line] $func(): unknown type for '$arg' argument\n");
+ print $ofh @saved;
+ $line += @saved;
+ next OUTER;
+ }
+# $type{$arg} = "void *"
+# if $type{$arg} eq "caddr_t";
+ $repl .= $type{$arg};
+ $repl .= " "
+ unless ($type{$arg} =~ m/\*$/);
+ $repl .= $arg;
+ $repl .= ", "
+ unless $arg eq $args[-1];
+ delete $type{$arg};
+ }
+ $repl .= ")";
+ # extra arguments in argument list
+ if (%type) {
+ warn("[$fn:$line] $func(): too many arguments\n");
+ print $ofh @saved;
+ $line += @saved;
+ next OUTER;
+ }
+ print $ofh "$repl\n";
+ ++$line;
+ # warn about long lines so they can be fixed up manually
+ warn("[$fn:$line] $func(): definition exceeds 80 characters\n")
+ if length($repl) >= 80;
+ print $ofh "{\n";
+ ++$line;
+ } else {
+ print $ofh $_;
+ ++$line;
+ }
+ }
+}
+
+sub ansify_file($) {
+ my $fn = shift;
+
+ my $tfn = "$fn.ansify";
+ local *IN;
+ local *OUT;
+
+ if (open(IN, "<", $fn)) {
+ if (open(OUT, ">", $tfn)) {
+ ansify(*IN{IO}, *OUT{IO}, $fn);
+ if (!rename($tfn, $fn)) {
+ warn("$fn: $!\n");
+ unlink($tfn);
+ }
+ } else {
+ warn("$fn.ansify: $!\n");
+ }
+ } else {
+ warn("$fn: $!\n");
+ }
+}
+
+MAIN:{
+ if (@ARGV) {
+ foreach (@ARGV) {
+ ansify_file($_);
+ }
+ } else {
+ ansify(*STDIN{IO}, *STDOUT{IO}, "(stdin)");
+ }
+}
OpenPOWER on IntegriCloud