Select Git revision

Alam Ed Arias authored and
Alam Arias
committed
zlib2ansi 3.80 KiB
#!/usr/bin/perl
# Transform K&R C function definitions into ANSI equivalent.
#
# Author: Paul Marquess
# Version: 1.0
# Date: 3 October 2006
# TODO
#
# Asumes no function pointer parameters. unless they are typedefed.
# Assumes no literal strings that look like function definitions
# Assumes functions start at the beginning of a line
use strict;
use warnings;
local $/;
$_ = <>;
my $sp = qr{ \s* (?: /\* .*? \*/ )? \s* }x; # assume no nested comments
my $d1 = qr{ $sp (?: [\w\*\s]+ $sp)* $sp \w+ $sp [\[\]\s]* $sp }x ;
my $decl = qr{ $sp (?: \w+ $sp )+ $d1 }xo ;
my $dList = qr{ $sp $decl (?: $sp , $d1 )* $sp ; $sp }xo ;
while (s/^
( # Start $1
( # Start $2
.*? # Minimal eat content
( ^ \w [\w\s\*]+ ) # $3 -- function name
\s* # optional whitespace
) # $2 - Matched up to before parameter list
\( \s* # Literal "(" + optional whitespace
( [^\)]+ ) # $4 - one or more anythings except ")"
\s* \) # optional whitespace surrounding a Literal ")"
( (?: $dList )+ ) # $5
$sp ^ { # literal "{" at start of line
) # Remember to $1
//xsom
)
{
my $all = $1 ;
my $prefix = $2;
my $param_list = $4 ;
my $params = $5;
StripComments($params);
StripComments($param_list);
$param_list =~ s/^\s+//;
$param_list =~ s/\s+$//;
my $i = 0 ;
my %pList = map { $_ => $i++ }
split /\s*,\s*/, $param_list;
my $pMatch = '(\b' . join('|', keys %pList) . '\b)\W*$' ;
my @params = split /\s*;\s*/, $params;
my @outParams = ();
foreach my $p (@params)
{
if ($p =~ /,/)
{
my @bits = split /\s*,\s*/, $p;
my $first = shift @bits;
$first =~ s/^\s*//;