Add a script that converts K&R-style function definitions to ANSI style.
This commit is contained in:
parent
ec761d7501
commit
d44231a0ce
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=153389
@ -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.
|
||||
|
6
tools/tools/ansify/Makefile
Normal file
6
tools/tools/ansify/Makefile
Normal file
@ -0,0 +1,6 @@
|
||||
# $FreeBSD$
|
||||
|
||||
SCRIPTS= ansify.pl
|
||||
BINDIR?= /usr/local/bin
|
||||
|
||||
.include <bsd.prog.mk>
|
140
tools/tools/ansify/ansify.pl
Normal file
140
tools/tools/ansify/ansify.pl
Normal file
@ -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)");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user