|
kusano |
fc6ab3 |
#!/usr/bin/perl
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
# Transform K&R C function definitions into ANSI equivalent.
|
|
kusano |
fc6ab3 |
#
|
|
kusano |
fc6ab3 |
# Author: Paul Marquess
|
|
kusano |
fc6ab3 |
# Version: 1.0
|
|
kusano |
fc6ab3 |
# Date: 3 October 2006
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
# TODO
|
|
kusano |
fc6ab3 |
#
|
|
kusano |
fc6ab3 |
# Asumes no function pointer parameters. unless they are typedefed.
|
|
kusano |
fc6ab3 |
# Assumes no literal strings that look like function definitions
|
|
kusano |
fc6ab3 |
# Assumes functions start at the beginning of a line
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
use strict;
|
|
kusano |
fc6ab3 |
use warnings;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
local $/;
|
|
kusano |
fc6ab3 |
$_ = <>;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
my $sp = qr{ \s* (?: /\* .*? \*/ )? \s* }x; # assume no nested comments
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
my $d1 = qr{ $sp (?: [\w\*\s]+ $sp)* $sp \w+ $sp [\[\]\s]* $sp }x ;
|
|
kusano |
fc6ab3 |
my $decl = qr{ $sp (?: \w+ $sp )+ $d1 }xo ;
|
|
kusano |
fc6ab3 |
my $dList = qr{ $sp $decl (?: $sp , $d1 )* $sp ; $sp }xo ;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
while (s/^
|
|
kusano |
fc6ab3 |
( # Start $1
|
|
kusano |
fc6ab3 |
( # Start $2
|
|
kusano |
fc6ab3 |
.*? # Minimal eat content
|
|
kusano |
fc6ab3 |
( ^ \w [\w\s\*]+ ) # $3 -- function name
|
|
kusano |
fc6ab3 |
\s* # optional whitespace
|
|
kusano |
fc6ab3 |
) # $2 - Matched up to before parameter list
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
\( \s* # Literal "(" + optional whitespace
|
|
kusano |
fc6ab3 |
( [^\)]+ ) # $4 - one or more anythings except ")"
|
|
kusano |
fc6ab3 |
\s* \) # optional whitespace surrounding a Literal ")"
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
( (?: $dList )+ ) # $5
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
$sp ^ { # literal "{" at start of line
|
|
kusano |
fc6ab3 |
) # Remember to $1
|
|
kusano |
fc6ab3 |
//xsom
|
|
kusano |
fc6ab3 |
)
|
|
kusano |
fc6ab3 |
{
|
|
kusano |
fc6ab3 |
my $all = $1 ;
|
|
kusano |
fc6ab3 |
my $prefix = $2;
|
|
kusano |
fc6ab3 |
my $param_list = $4 ;
|
|
kusano |
fc6ab3 |
my $params = $5;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
StripComments($params);
|
|
kusano |
fc6ab3 |
StripComments($param_list);
|
|
kusano |
fc6ab3 |
$param_list =~ s/^\s+//;
|
|
kusano |
fc6ab3 |
$param_list =~ s/\s+$//;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
my $i = 0 ;
|
|
kusano |
fc6ab3 |
my %pList = map { $_ => $i++ }
|
|
kusano |
fc6ab3 |
split /\s*,\s*/, $param_list;
|
|
kusano |
fc6ab3 |
my $pMatch = '(\b' . join('|', keys %pList) . '\b)\W*$' ;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
my @params = split /\s*;\s*/, $params;
|
|
kusano |
fc6ab3 |
my @outParams = ();
|
|
kusano |
fc6ab3 |
foreach my $p (@params)
|
|
kusano |
fc6ab3 |
{
|
|
kusano |
fc6ab3 |
if ($p =~ /,/)
|
|
kusano |
fc6ab3 |
{
|
|
kusano |
fc6ab3 |
my @bits = split /\s*,\s*/, $p;
|
|
kusano |
fc6ab3 |
my $first = shift @bits;
|
|
kusano |
fc6ab3 |
$first =~ s/^\s*//;
|
|
kusano |
fc6ab3 |
push @outParams, $first;
|
|
kusano |
fc6ab3 |
$first =~ /^(\w+\s*)/;
|
|
kusano |
fc6ab3 |
my $type = $1 ;
|
|
kusano |
fc6ab3 |
push @outParams, map { $type . $_ } @bits;
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
else
|
|
kusano |
fc6ab3 |
{
|
|
kusano |
fc6ab3 |
$p =~ s/^\s+//;
|
|
kusano |
fc6ab3 |
push @outParams, $p;
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
my %tmp = map { /$pMatch/; $_ => $pList{$1} }
|
|
kusano |
fc6ab3 |
@outParams ;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
@outParams = map { " $_" }
|
|
kusano |
fc6ab3 |
sort { $tmp{$a} <=> $tmp{$b} }
|
|
kusano |
fc6ab3 |
@outParams ;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
print $prefix ;
|
|
kusano |
fc6ab3 |
print "(\n" . join(",\n", @outParams) . ")\n";
|
|
kusano |
fc6ab3 |
print "{" ;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
# Output any trailing code.
|
|
kusano |
fc6ab3 |
print ;
|
|
kusano |
fc6ab3 |
exit 0;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
sub StripComments
|
|
kusano |
fc6ab3 |
{
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
no warnings;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
# Strip C & C++ coments
|
|
kusano |
fc6ab3 |
# From the perlfaq
|
|
kusano |
fc6ab3 |
$_[0] =~
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
s{
|
|
kusano |
fc6ab3 |
/\* ## Start of /* ... */ comment
|
|
kusano |
fc6ab3 |
[^*]*\*+ ## Non-* followed by 1-or-more *'s
|
|
kusano |
fc6ab3 |
(
|
|
kusano |
fc6ab3 |
[^/*][^*]*\*+
|
|
kusano |
fc6ab3 |
)* ## 0-or-more things which don't start with /
|
|
kusano |
fc6ab3 |
## but do end with '*'
|
|
kusano |
fc6ab3 |
/ ## End of /* ... */ comment
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
| ## OR C++ Comment
|
|
kusano |
fc6ab3 |
// ## Start of C++ comment //
|
|
kusano |
fc6ab3 |
[^\n]* ## followed by 0-or-more non end of line characters
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
| ## OR various things which aren't comments:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
(
|
|
kusano |
fc6ab3 |
" ## Start of " ... " string
|
|
kusano |
fc6ab3 |
(
|
|
kusano |
fc6ab3 |
\\. ## Escaped char
|
|
kusano |
fc6ab3 |
| ## OR
|
|
kusano |
fc6ab3 |
[^"\\] ## Non "\
|
|
kusano |
fc6ab3 |
)*
|
|
kusano |
fc6ab3 |
" ## End of " ... " string
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
| ## OR
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
' ## Start of ' ... ' string
|
|
kusano |
fc6ab3 |
(
|
|
kusano |
fc6ab3 |
\\. ## Escaped char
|
|
kusano |
fc6ab3 |
| ## OR
|
|
kusano |
fc6ab3 |
[^'\\] ## Non '\
|
|
kusano |
fc6ab3 |
)*
|
|
kusano |
fc6ab3 |
' ## End of ' ... ' string
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
| ## OR
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
. ## Anything other char
|
|
kusano |
fc6ab3 |
[^/"'\\]* ## Chars which doesn't start a comment, string or escape
|
|
kusano |
fc6ab3 |
)
|
|
kusano |
fc6ab3 |
}{$2}gxs;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
}
|