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 # concetenate function prototypes that stretch over multiple lines
124 while (($REST =~ /[\(\)]/) or ($parenthesis)) {
125 while ($REST =~ /[\(\)]/) {
127 while ($REST =~ /^[^\(\)]*\((.*)$/) {
133 while ($REST =~ /^[^\(\)]*\)(.*)$/) {
139 # concatenate if not balanced
141 if (my $LINE2 = <>) {
142 $LINE2 =~ s/^\s*(.*)$/$1/;
144 $LINE .= " " . $LINE2;
146 $REST .= " " . $LINE2;
148 print "ERROR: unbalanced parantheses ($parenthesis)\n";
154 next if ($LINE =~ /^typedef\s/);
155 next if ($LINE =~ /^enum\s+[^\{\(]+\s+\{/);
156 next if ($LINE =~ /^struct\s+[^\{\(]+\s+\{.*\}\s*;/);
157 next if ($LINE =~ /^struct\s+[a-zA-Z0-9_]+\s*;/);
159 # remove trailing spaces
160 $LINE =~ s/(.*?)\s*$/$1/;
162 $LINE =~ s/^(.*\))\s+PRINTF_ATTRIBUTE\([^\)]*\)(\s*[;,])/$1$2/;
163 $LINE =~ s/^(.*\))\s*[a-zA-Z0-9_]+\s*;$/$1;/;
165 # remove parameter names - slightly too coarse probably
166 $LINE =~ s/([\s\(]\*?)[_0-9a-zA-Z]+\s*([,\)])/$1$2/g;
168 # remedy (void) from last line
169 $LINE =~ s/\(\)/(void)/g;
172 $LINE =~ s/\s*\)\s*/)/g;
173 $LINE =~ s/\s*\(\s*/ (/g;
174 $LINE =~ s/\s*,\s*/, /g;
177 $LINE =~ s/([\s,\(])unsigned([,\)])/$1unsigned int$2/g;
180 $LINE =~ s/(\b)bool(\b)/_Bool/g;