s3: Fix vfs_xattr_tdb.c
[Samba/gebeck_regimport.git] / lib / talloc / script / mksigs.pl
blobdfe36bc138f48c7ce01e04cde0aa7079a35bb945
1 #!/usr/bin/perl
3 # mksigs.pl - extract signatures from C headers
5 # Copyright (C) Michael Adam 2009
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.
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.
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/>.
20 # USAGE: cat $header_files | mksigs.pl > $signature_file
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.
26 use strict;
27 use warnings;
29 my $in_comment = 0;
30 my $in_doxygen = 0;
31 my $extern_C_block = 0;
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;
45 # find end of DOXYGEN section
46 if ($in_doxygen) {
47 if ($LINE =~ /^#\s*else(?:\s+.*)?$/) {
48 $in_doxygen = 0;
50 next;
53 # strip C++-style comments
54 $LINE =~ s/^(.*?)\/\/.*$/$1/;
56 # strip in-line-comments:
57 while ($LINE =~ /\/\*.*?\*\//) {
58 $LINE =~ s/\/\*.*?\*\///;
61 # find starts of multi-line-comments
62 if ($LINE =~ /^(.*)\/\*/) {
63 $in_comment = 1;
64 $LINE = $1;
67 # skip empty lines
68 next if $LINE =~ /^\s*$/;
70 # remove leading spaces
71 $LINE =~ s/^\s*(.*)$/$1/;
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;
81 # remove DOXYGEN sections
82 if ($LINE =~ /^#\s*ifdef\s+DOXYGEN(?:\s+.*)?$/) {
83 $in_doxygen = 1;
84 next;
88 # remove all preprocessor directives
89 next if ($LINE =~ /^#/);
91 if ($LINE =~ /^extern\s+"C"\s+\{/) {
92 $extern_C_block = 1;
93 next;
96 if (($LINE =~ /^[^\{]*\}/) and $extern_C_block) {
97 $extern_C_block = 0;
98 next;
101 $LINE =~ s/^extern\s//;
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;
115 # collect closing
116 while ($REST =~ /^[^\{\}]*\}(.*)$/) {
117 $braces--;
118 $REST = $1;
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;
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;
148 # collect closing
149 while ($REST =~ /^[^\(\)]*\)(.*)$/) {
150 $parenthesis--;
151 $REST = $1;
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;
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*;/);
175 # remove trailing spaces
176 $LINE =~ s/(.*?)\s*$/$1/;
178 $LINE =~ s/^(.*\))\s+PRINTF_ATTRIBUTE\([^\)]*\)(\s*[;,])/$1$2/;
179 $LINE =~ s/^(.*\))\s*[a-zA-Z0-9_]+\s*;$/$1;/;
181 # remove parameter names - slightly too coarse probably
182 $LINE =~ s/([\s\(]\*?)[_0-9a-zA-Z]+\s*([,\)])/$1$2/g;
184 # remedy (void) from last line
185 $LINE =~ s/\(\)/(void)/g;
187 # normalize spaces
188 $LINE =~ s/\s*\)\s*/)/g;
189 $LINE =~ s/\s*\(\s*/ (/g;
190 $LINE =~ s/\s*,\s*/, /g;
192 # normalize unsigned
193 $LINE =~ s/([\s,\(])unsigned([,\)])/$1unsigned int$2/g;
195 # normalize bool
196 $LINE =~ s/(\b)bool(\b)/_Bool/g;
198 print $LINE . "\n";