libcli/dns: Rename UDP-based calls to reflect their use
[kai/samba.git] / lib / talloc / script / mksigs.pl
1 #!/usr/bin/perl
2
3 # mksigs.pl - extract signatures from C headers
4 #
5 # Copyright (C) Michael Adam 2009
6 #
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the Free
9 # Software Foundation; either version 3 of the License, or (at your option)
10 # any later version.
11 #
12 # This program is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15 # more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # this program; if not, see <http://www.gnu.org/licenses/>.
19
20 # USAGE:  cat $header_files | mksigs.pl > $signature_file
21 #
22 # The header files to parse are read from stdin.
23 # The output is in a form as produced by gcc with the -aux-info switch
24 # and printed to stdout.
25
26 use strict;
27 use warnings;
28
29 my $in_comment = 0;
30 my $in_doxygen = 0;
31 my $extern_C_block = 0;
32
33 while (my $LINE = <>) {
34         # find end of started multi-line-comment
35         if ($in_comment) {
36                 if ($LINE =~ /^.*?\*\/(.*)$/) {
37                         $LINE = $1;
38                         $in_comment = 0;
39                 } else {
40                         # whole line within comment
41                         next;
42                 }
43         }
44
45         # find end of DOXYGEN section
46         if ($in_doxygen) {
47                 if ($LINE =~ /^#\s*else(?:\s+.*)?$/) {
48                         $in_doxygen = 0;
49                 }
50                 next;
51         }
52
53         # strip C++-style comments
54         $LINE =~ s/^(.*?)\/\/.*$/$1/;
55
56         # strip in-line-comments:
57         while ($LINE =~ /\/\*.*?\*\//) {
58                 $LINE =~ s/\/\*.*?\*\///;
59         }
60
61         # find starts of multi-line-comments
62         if ($LINE =~ /^(.*)\/\*/) {
63                 $in_comment = 1;
64                 $LINE = $1;
65         }
66
67         # skip empty lines
68         next if $LINE =~ /^\s*$/;
69
70         # remove leading spaces
71         $LINE =~ s/^\s*(.*)$/$1/;
72
73         # concatenate lines split with "\" (usually macro defines)
74         while ($LINE =~ /^(.*?)\s+\\$/) {
75                 my $LINE2 = <>;
76                 $LINE = $1;
77                 $LINE2 =~ s/^\s*(.*)$/$1/;
78                 $LINE .= " " . $LINE2;
79         }
80
81         # remove DOXYGEN sections
82         if ($LINE =~ /^#\s*ifdef\s+DOXYGEN(?:\s+.*)?$/) {
83                 $in_doxygen = 1;
84                 next;
85         }
86
87
88         # remove all preprocessor directives
89         next if ($LINE =~ /^#/);
90
91         if ($LINE =~ /^extern\s+"C"\s+\{/) {
92                 $extern_C_block = 1;
93                 next;
94         }
95
96         if (($LINE =~ /^[^\{]*\}/) and $extern_C_block) {
97                 $extern_C_block = 0;
98                 next;
99         }
100
101         $LINE =~ s/^extern\s//;
102
103         # concatenate braces stretched over multiple lines
104         # (from structs or enums)
105         my $REST = $LINE;
106         my $braces = 0;
107         while (($REST =~ /[\{\}]/) or ($braces)) {
108                 while ($REST =~ /[\{\}]/) {
109                         # collect opening
110                         while ($REST =~ /^[^\{\}]*\{(.*)$/) {
111                                 $braces++;
112                                 $REST = $1;
113                         }
114
115                         # collect closing
116                         while ($REST =~ /^[^\{\}]*\}(.*)$/) {
117                                 $braces--;
118                                 $REST = $1;
119                         }
120                 }
121
122                 # concatenate if not balanced
123                 if ($braces) {
124                         if (my $LINE2 = <>) {
125                                 $LINE2 =~ s/^\s*(.*)$/$1/;
126                                 chomp($LINE);
127                                 $LINE .= " " . $LINE2;
128                                 chomp $REST;
129                                 $REST .= " " . $LINE2;
130                         } else {
131                                 print "ERROR: unbalanced braces ($braces)\n";
132                                 last;
133                         }
134                 }
135         }
136
137         # concetenate function prototypes that stretch over multiple lines
138         $REST = $LINE;
139         my $parenthesis = 0;
140         while (($REST =~ /[\(\)]/) or ($parenthesis)) {
141                 while ($REST =~ /[\(\)]/) {
142                         # collect opening
143                         while ($REST =~ /^[^\(\)]*\((.*)$/) {
144                                 $parenthesis++;
145                                 $REST = $1;
146                         }
147
148                         # collect closing
149                         while ($REST =~ /^[^\(\)]*\)(.*)$/) {
150                                 $parenthesis--;
151                                 $REST = $1;
152                         }
153                 }
154
155                 # concatenate if not balanced
156                 if ($parenthesis) {
157                         if (my $LINE2 = <>) {
158                                 $LINE2 =~ s/^\s*(.*)$/$1/;
159                                 chomp($LINE);
160                                 $LINE .= " " . $LINE2;
161                                 chomp($REST);
162                                 $REST .= " " . $LINE2;
163                         } else {
164                                 print "ERROR: unbalanced parantheses ($parenthesis)\n";
165                                 last;
166                         }
167                 }
168         }
169
170         next if ($LINE =~ /^typedef\s/);
171         next if ($LINE =~ /^enum\s+[^\{\(]+\s+\{/);
172         next if ($LINE =~ /^struct\s+[^\{\(]+\s+\{.*\}\s*;/);
173         next if ($LINE =~ /^struct\s+[a-zA-Z0-9_]+\s*;/);
174
175         # remove trailing spaces
176         $LINE =~ s/(.*?)\s*$/$1/;
177
178         $LINE =~ s/^(.*\))\s+PRINTF_ATTRIBUTE\([^\)]*\)(\s*[;,])/$1$2/;
179         $LINE =~ s/^(.*\))\s*[a-zA-Z0-9_]+\s*;$/$1;/;
180
181         # remove parameter names - slightly too coarse probably
182         $LINE =~ s/([\s\(]\*?)[_0-9a-zA-Z]+\s*([,\)])/$1$2/g;
183
184         # remedy (void) from last line
185         $LINE =~ s/\(\)/(void)/g;
186
187         # normalize spaces
188         $LINE =~ s/\s*\)\s*/)/g;
189         $LINE =~ s/\s*\(\s*/ (/g;
190         $LINE =~ s/\s*,\s*/, /g;
191
192         # normalize unsigned
193         $LINE =~ s/([\s,\(])unsigned([,\)])/$1unsigned int$2/g;
194
195         # normalize bool
196         $LINE =~ s/(\b)bool(\b)/_Bool/g;
197
198         print $LINE . "\n";
199 }