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