1381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#!/usr/bin/perl
2381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
3381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes# Transform K&R C function definitions into ANSI equivalent.
4381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#
5381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes# Author: Paul Marquess
6381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes# Version: 1.0
7381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes# Date: 3 October 2006
8381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
9381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes# TODO
10381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#
11381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes# Asumes no function pointer parameters. unless they are typedefed.
12381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes# Assumes no literal strings that look like function definitions
13381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes# Assumes functions start at the beginning of a line
14381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
15381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughesuse strict;
16381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughesuse warnings;
17381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
18381716e9396b55b1adb8235b020c37344f60ab07Elliott Hugheslocal $/;
19381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes$_ = <>;
20381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
21381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughesmy $sp = qr{ \s* (?: /\* .*? \*/ )? \s* }x; # assume no nested comments
22381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
23381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughesmy $d1    = qr{ $sp (?: [\w\*\s]+ $sp)* $sp \w+ $sp [\[\]\s]* $sp }x ;
24381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughesmy $decl  = qr{ $sp (?: \w+ $sp )+ $d1 }xo ;
25381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughesmy $dList = qr{ $sp $decl (?: $sp , $d1 )* $sp ; $sp }xo ;
26381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
27381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
28381716e9396b55b1adb8235b020c37344f60ab07Elliott Hugheswhile (s/^
29381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes            (                  # Start $1
30381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                (              #   Start $2
31381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                    .*?        #     Minimal eat content
32381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                    ( ^ \w [\w\s\*]+ )    #     $3 -- function name
33381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                    \s*        #     optional whitespace
34381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                )              # $2 - Matched up to before parameter list
35381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
36381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                \( \s*         # Literal "(" + optional whitespace
37381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                ( [^\)]+ )     # $4 - one or more anythings except ")"
38381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                \s* \)         # optional whitespace surrounding a Literal ")"
39381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
40381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                ( (?: $dList )+ ) # $5
41381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
42381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                $sp ^ {        # literal "{" at start of line
43381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes            )                  # Remember to $1
44381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        //xsom
45381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes      )
46381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes{
47381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    my $all = $1 ;
48381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    my $prefix = $2;
49381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    my $param_list = $4 ;
50381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    my $params = $5;
51381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
52381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    StripComments($params);
53381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    StripComments($param_list);
54381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    $param_list =~ s/^\s+//;
55381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    $param_list =~ s/\s+$//;
56381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
57381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    my $i = 0 ;
58381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    my %pList = map { $_ => $i++ }
59381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                split /\s*,\s*/, $param_list;
60381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    my $pMatch = '(\b' . join('|', keys %pList) . '\b)\W*$' ;
61381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
62381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    my @params = split /\s*;\s*/, $params;
63381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    my @outParams = ();
64381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    foreach my $p (@params)
65381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    {
66381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        if ($p =~ /,/)
67381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        {
68381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes            my @bits = split /\s*,\s*/, $p;
69381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes            my $first = shift @bits;
70381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes            $first =~ s/^\s*//;
71381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes            push @outParams, $first;
72381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes            $first =~ /^(\w+\s*)/;
73381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes            my $type = $1 ;
74381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes            push @outParams, map { $type . $_ } @bits;
75381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        }
76381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        else
77381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        {
78381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes            $p =~ s/^\s+//;
79381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes            push @outParams, $p;
80381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        }
81381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    }
82381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
83381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
84381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    my %tmp = map { /$pMatch/;  $_ => $pList{$1}  }
85381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes              @outParams ;
86381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
87381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    @outParams = map  { "    $_" }
88381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                 sort { $tmp{$a} <=> $tmp{$b} }
89381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                 @outParams ;
90381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
91381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    print $prefix ;
92381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    print "(\n" . join(",\n", @outParams) . ")\n";
93381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    print "{" ;
94381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
95381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes}
96381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
97381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes# Output any trailing code.
98381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughesprint ;
99381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughesexit 0;
100381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
101381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
102381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughessub StripComments
103381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes{
104381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
105381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes  no warnings;
106381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
107381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes  # Strip C & C++ coments
108381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes  # From the perlfaq
109381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes  $_[0] =~
110381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
111381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    s{
112381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes       /\*         ##  Start of /* ... */ comment
113381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes       [^*]*\*+    ##  Non-* followed by 1-or-more *'s
114381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes       (
115381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes         [^/*][^*]*\*+
116381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes       )*          ##  0-or-more things which don't start with /
117381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                   ##    but do end with '*'
118381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes       /           ##  End of /* ... */ comment
119381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
120381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes     |         ##     OR  C++ Comment
121381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes       //          ## Start of C++ comment //
122381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes       [^\n]*      ## followed by 0-or-more non end of line characters
123381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
124381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes     |         ##     OR  various things which aren't comments:
125381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
126381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes       (
127381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes         "           ##  Start of " ... " string
128381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes         (
129381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes           \\.           ##  Escaped char
130381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes         |               ##    OR
131381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes           [^"\\]        ##  Non "\
132381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes         )*
133381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes         "           ##  End of " ... " string
134381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
135381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes       |         ##     OR
136381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
137381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes         '           ##  Start of ' ... ' string
138381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes         (
139381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes           \\.           ##  Escaped char
140381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes         |               ##    OR
141381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes           [^'\\]        ##  Non '\
142381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes         )*
143381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes         '           ##  End of ' ... ' string
144381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
145381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes       |         ##     OR
146381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
147381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes         .           ##  Anything other char
148381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes         [^/"'\\]*   ##  Chars which doesn't start a comment, string or escape
149381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes       )
150381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes     }{$2}gxs;
151381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
152381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes}
153