cannam@128
|
1 #!/usr/bin/perl
|
cannam@128
|
2
|
cannam@128
|
3 # Transform K&R C function definitions into ANSI equivalent.
|
cannam@128
|
4 #
|
cannam@128
|
5 # Author: Paul Marquess
|
cannam@128
|
6 # Version: 1.0
|
cannam@128
|
7 # Date: 3 October 2006
|
cannam@128
|
8
|
cannam@128
|
9 # TODO
|
cannam@128
|
10 #
|
cannam@128
|
11 # Asumes no function pointer parameters. unless they are typedefed.
|
cannam@128
|
12 # Assumes no literal strings that look like function definitions
|
cannam@128
|
13 # Assumes functions start at the beginning of a line
|
cannam@128
|
14
|
cannam@128
|
15 use strict;
|
cannam@128
|
16 use warnings;
|
cannam@128
|
17
|
cannam@128
|
18 local $/;
|
cannam@128
|
19 $_ = <>;
|
cannam@128
|
20
|
cannam@128
|
21 my $sp = qr{ \s* (?: /\* .*? \*/ )? \s* }x; # assume no nested comments
|
cannam@128
|
22
|
cannam@128
|
23 my $d1 = qr{ $sp (?: [\w\*\s]+ $sp)* $sp \w+ $sp [\[\]\s]* $sp }x ;
|
cannam@128
|
24 my $decl = qr{ $sp (?: \w+ $sp )+ $d1 }xo ;
|
cannam@128
|
25 my $dList = qr{ $sp $decl (?: $sp , $d1 )* $sp ; $sp }xo ;
|
cannam@128
|
26
|
cannam@128
|
27
|
cannam@128
|
28 while (s/^
|
cannam@128
|
29 ( # Start $1
|
cannam@128
|
30 ( # Start $2
|
cannam@128
|
31 .*? # Minimal eat content
|
cannam@128
|
32 ( ^ \w [\w\s\*]+ ) # $3 -- function name
|
cannam@128
|
33 \s* # optional whitespace
|
cannam@128
|
34 ) # $2 - Matched up to before parameter list
|
cannam@128
|
35
|
cannam@128
|
36 \( \s* # Literal "(" + optional whitespace
|
cannam@128
|
37 ( [^\)]+ ) # $4 - one or more anythings except ")"
|
cannam@128
|
38 \s* \) # optional whitespace surrounding a Literal ")"
|
cannam@128
|
39
|
cannam@128
|
40 ( (?: $dList )+ ) # $5
|
cannam@128
|
41
|
cannam@128
|
42 $sp ^ { # literal "{" at start of line
|
cannam@128
|
43 ) # Remember to $1
|
cannam@128
|
44 //xsom
|
cannam@128
|
45 )
|
cannam@128
|
46 {
|
cannam@128
|
47 my $all = $1 ;
|
cannam@128
|
48 my $prefix = $2;
|
cannam@128
|
49 my $param_list = $4 ;
|
cannam@128
|
50 my $params = $5;
|
cannam@128
|
51
|
cannam@128
|
52 StripComments($params);
|
cannam@128
|
53 StripComments($param_list);
|
cannam@128
|
54 $param_list =~ s/^\s+//;
|
cannam@128
|
55 $param_list =~ s/\s+$//;
|
cannam@128
|
56
|
cannam@128
|
57 my $i = 0 ;
|
cannam@128
|
58 my %pList = map { $_ => $i++ }
|
cannam@128
|
59 split /\s*,\s*/, $param_list;
|
cannam@128
|
60 my $pMatch = '(\b' . join('|', keys %pList) . '\b)\W*$' ;
|
cannam@128
|
61
|
cannam@128
|
62 my @params = split /\s*;\s*/, $params;
|
cannam@128
|
63 my @outParams = ();
|
cannam@128
|
64 foreach my $p (@params)
|
cannam@128
|
65 {
|
cannam@128
|
66 if ($p =~ /,/)
|
cannam@128
|
67 {
|
cannam@128
|
68 my @bits = split /\s*,\s*/, $p;
|
cannam@128
|
69 my $first = shift @bits;
|
cannam@128
|
70 $first =~ s/^\s*//;
|
cannam@128
|
71 push @outParams, $first;
|
cannam@128
|
72 $first =~ /^(\w+\s*)/;
|
cannam@128
|
73 my $type = $1 ;
|
cannam@128
|
74 push @outParams, map { $type . $_ } @bits;
|
cannam@128
|
75 }
|
cannam@128
|
76 else
|
cannam@128
|
77 {
|
cannam@128
|
78 $p =~ s/^\s+//;
|
cannam@128
|
79 push @outParams, $p;
|
cannam@128
|
80 }
|
cannam@128
|
81 }
|
cannam@128
|
82
|
cannam@128
|
83
|
cannam@128
|
84 my %tmp = map { /$pMatch/; $_ => $pList{$1} }
|
cannam@128
|
85 @outParams ;
|
cannam@128
|
86
|
cannam@128
|
87 @outParams = map { " $_" }
|
cannam@128
|
88 sort { $tmp{$a} <=> $tmp{$b} }
|
cannam@128
|
89 @outParams ;
|
cannam@128
|
90
|
cannam@128
|
91 print $prefix ;
|
cannam@128
|
92 print "(\n" . join(",\n", @outParams) . ")\n";
|
cannam@128
|
93 print "{" ;
|
cannam@128
|
94
|
cannam@128
|
95 }
|
cannam@128
|
96
|
cannam@128
|
97 # Output any trailing code.
|
cannam@128
|
98 print ;
|
cannam@128
|
99 exit 0;
|
cannam@128
|
100
|
cannam@128
|
101
|
cannam@128
|
102 sub StripComments
|
cannam@128
|
103 {
|
cannam@128
|
104
|
cannam@128
|
105 no warnings;
|
cannam@128
|
106
|
cannam@128
|
107 # Strip C & C++ coments
|
cannam@128
|
108 # From the perlfaq
|
cannam@128
|
109 $_[0] =~
|
cannam@128
|
110
|
cannam@128
|
111 s{
|
cannam@128
|
112 /\* ## Start of /* ... */ comment
|
cannam@128
|
113 [^*]*\*+ ## Non-* followed by 1-or-more *'s
|
cannam@128
|
114 (
|
cannam@128
|
115 [^/*][^*]*\*+
|
cannam@128
|
116 )* ## 0-or-more things which don't start with /
|
cannam@128
|
117 ## but do end with '*'
|
cannam@128
|
118 / ## End of /* ... */ comment
|
cannam@128
|
119
|
cannam@128
|
120 | ## OR C++ Comment
|
cannam@128
|
121 // ## Start of C++ comment //
|
cannam@128
|
122 [^\n]* ## followed by 0-or-more non end of line characters
|
cannam@128
|
123
|
cannam@128
|
124 | ## OR various things which aren't comments:
|
cannam@128
|
125
|
cannam@128
|
126 (
|
cannam@128
|
127 " ## Start of " ... " string
|
cannam@128
|
128 (
|
cannam@128
|
129 \\. ## Escaped char
|
cannam@128
|
130 | ## OR
|
cannam@128
|
131 [^"\\] ## Non "\
|
cannam@128
|
132 )*
|
cannam@128
|
133 " ## End of " ... " string
|
cannam@128
|
134
|
cannam@128
|
135 | ## OR
|
cannam@128
|
136
|
cannam@128
|
137 ' ## Start of ' ... ' string
|
cannam@128
|
138 (
|
cannam@128
|
139 \\. ## Escaped char
|
cannam@128
|
140 | ## OR
|
cannam@128
|
141 [^'\\] ## Non '\
|
cannam@128
|
142 )*
|
cannam@128
|
143 ' ## End of ' ... ' string
|
cannam@128
|
144
|
cannam@128
|
145 | ## OR
|
cannam@128
|
146
|
cannam@128
|
147 . ## Anything other char
|
cannam@128
|
148 [^/"'\\]* ## Chars which doesn't start a comment, string or escape
|
cannam@128
|
149 )
|
cannam@128
|
150 }{$2}gxs;
|
cannam@128
|
151
|
cannam@128
|
152 }
|