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)
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
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.
30 my $extern_C_block = 0;
32 while (my $LINE = <>) {
33 # find end of started multi-line-comment
35 if ($LINE =~ /^.*?\*\/(.*)$/) {
39 # whole line within comment
44 # strip C++-style comments
45 $LINE =~ s/^(.*?)\/\/.*$/$1/;
47 # strip in-line-comments:
48 while ($LINE =~ /\/\
*.*?\
*\
//) {
49 $LINE =~ s/\/\*.*?\*\///;
52 # find starts of multi-line-comments
53 if ($LINE =~ /^(.*)\/\
*/) {
59 next if $LINE =~ /^\s*$/;
61 # remove leading spaces
62 $LINE =~ s/^\s*(.*)$/$1/;
64 # concatenate lines split with "\" (usually macro defines)
65 while ($LINE =~ /^(.*?)\s+\\$/) {
68 $LINE2 =~ s/^\s*(.*)$/$1/;
69 $LINE .= " " . $LINE2;
72 # remove all preprocessor directives
73 next if ($LINE =~ /^#/);
75 if ($LINE =~ /^extern\s+"C"\s+\{/) {
80 if (($LINE =~ /^[^\{]*\}/) and $extern_C_block) {
85 $LINE =~ s/^extern\s//;
87 # concatenate braces stretched over multiple lines
88 # (from structs or enums)
91 while (($REST =~ /[\{\}]/) or ($braces)) {
92 while ($REST =~ /[\{\}]/) {
94 while ($REST =~ /^[^\{\}]*\{(.*)$/) {
100 while ($REST =~ /^[^\{\}]*\}(.*)$/) {
106 # concatenate if not balanced
108 if (my $LINE2 = <>) {
109 $LINE2 =~ s/^\s*(.*)$/$1/;
111 $LINE .= " " . $LINE2;
113 $REST .= " " . $LINE2;
115 print "ERROR: unbalanced braces ($braces)\n";
121 next if ($LINE =~ /^typedef\s/);
122 next if ($LINE =~ /^enum\s+[^\{\(]+\s+\{/);
123 next if ($LINE =~ /^struct\s+[^\{\(]+\s+\{.*\}\s*;/);
125 # concetenate function prototypes that stretch over multiple lines
128 while (($REST =~ /[\(\)]/) or ($parenthesis)) {
129 while ($REST =~ /[\(\)]/) {
131 while ($REST =~ /^[^\(\)]*\((.*)$/) {
137 while ($REST =~ /^[^\(\)]*\)(.*)$/) {
143 # concatenate if not balanced
145 if (my $LINE2 = <>) {
146 $LINE2 =~ s/^\s*(.*)$/$1/;
148 $LINE .= " " . $LINE2;
150 $REST .= " " . $LINE2;
152 print "ERROR: unbalanced parantheses ($parenthesis)\n";
158 # remove trailing spaces
159 $LINE =~ s/(.*?)\s*$/$1/;
161 $LINE =~ s/^(.*\))\s+PRINTF_ATTRIBUTE\(.*\);$/$1;/;
163 # remove parameter names - slightly too coarse probably
164 $LINE =~ s/([\s\(]\*?)[_0-9a-zA-Z]+\s*([,\)])/$1$2/g;
166 # remedy (void) from last line
167 $LINE =~ s/\(\)/(void)/g;
170 $LINE =~ s/\s*\)\s*/)/g;
171 $LINE =~ s/\s*\(\s*/ (/g;
172 $LINE =~ s/\s*,\s*/, /g;
175 $LINE =~ s/([\s,\(])unsigned([,\)])/$1unsigned int$2/g;