1 # Make prototypes from .c files
14 my $private_func_re = "^_";
17 Getopts
('x:m:o:p:dqE:R:P:') || die "foo";
28 $private_func_re = $opt_R;
31 'multiline-proto' => 1,
33 'function-blocking' => 0,
34 'gnuc-attribute' => 1,
38 foreach $i (split(/,/, $opt_m)) {
40 $flags{"multiline-proto"} = 0;
42 $flags{"function-blocking"} = 0;
43 $flags{"gnuc-attribute"} = 0;
46 if(substr($i, 0, 3) eq "no-") {
47 $flags{substr($i, 3)} = 0;
61 if(/^([a-zA-Z0-9_]+)\s?(.*)$/) {
71 print $brace, " ", $_ if($debug);
76 if ( s@
/\
*.*@@
) { $comment = 1;
77 } elsif ($comment && s@
.*\
*/@@
) { $comment = 0;
78 } elsif ($comment) { next; }
83 if($if_0 && /^\#endif/) {
106 if(!/^static/ && !/^PRIVATE/){
108 if(m/(.*)(__attribute__\s?\(.*\))/) {
112 if(m/(.*)\s(\w+DEPRECATED_FUNCTION)\s?(\(.*\))(.*)/) {
113 $depfunction{$2} = 1;
117 if(m/(.*)\s(\w+DEPRECATED)(.*)/) {
125 while(s/\(([^()]*),(.*)\)/($1\$$2)/g){}
126 s/\<\s*void\s*\>/<>/;
127 # remove parameter names
128 if($opt_P eq "remove") {
129 s/(\s*)([a-zA-Z0-9_]+)([,>])/$3/g;
131 s/\(\*(\s*)([a-zA-Z0-9_]+)\)/(*)/g;
132 } elsif($opt_P eq "comment") {
133 s/([a-zA-Z0-9_]+)([,>])/\/\
*$1\
*\
/$2/g;
134 s/\(\*([a-zA-Z0-9_]+)\)/(*\/\
*$1\
*\
/)/g;
137 # add newlines before parameters
138 if($flags{"multiline-proto"}) {
145 # match function name
146 /([a-zA-Z0-9_]+)\s*\</;
155 # only add newline if more than one parameter
156 if($flags{"multiline-proto"} && /,/){
162 # insert newline before function name
163 if($flags{"multiline-proto"}) {
164 s/(.*)\s([a-zA-Z0-9_]+ \Q$LP\E)/$1\n$2/;
182 $line = $line . " " . $_;
196 open(OUT
, ">${opt_o}.new");
197 $block = &foo
($opt_o);
199 $block = "__public_h__";
203 open(PRIV
, ">${opt_p}.new");
204 $private = &foo
($opt_p);
206 $private = "__private_h__";
212 $public_h_header .= "/* This is a generated file */
218 $public_h_header .= "#ifdef __STDC__
231 $public_h_header .= "#include <stdarg.h>
235 $public_h_trailer = "";
237 $private_h_header = "/* This is a generated file */
243 $private_h_header .= "#ifdef __STDC__
256 $private_h_header .= "#include <stdarg.h>
260 $private_h_trailer = "";
262 foreach(sort keys %funcs){
263 if(/^(main)$/) { next }
264 if ($funcs{$_} =~ /\^/) {
265 $beginblock = "#ifdef __BLOCKS__\n";
266 $endblock = "#endif /* __BLOCKS__ */\n";
268 $beginblock = $endblock = "";
270 if(!defined($exported{$_}) && /$private_func_re/) {
271 $private_h .= $beginblock . $funcs{$_} . "\n" . $endblock . "\n";
272 if($funcs{$_} =~ /__attribute__/) {
273 $private_attribute_seen = 1;
276 if($flags{"function-blocking"}) {
278 if($exported{$_} =~ /proto/) {
279 $public_h .= "#if !defined(HAVE_$fupper) || defined(NEED_${fupper}_PROTO)\n";
281 $public_h .= "#ifndef HAVE_$fupper\n";
284 $public_h .= $beginblock . $funcs{$_} . "\n" . $endblock;
285 if($funcs{$_} =~ /__attribute__/) {
286 $public_attribute_seen = 1;
288 if($flags{"function-blocking"}) {
289 $public_h .= "#endif\n";
295 if($flags{"gnuc-attribute"}) {
296 if ($public_attribute_seen) {
297 $public_h_header .= "#if !defined(__GNUC__) && !defined(__attribute__)
298 #define __attribute__(x)
304 if ($private_attribute_seen) {
305 $private_h_header .= "#if !defined(__GNUC__) && !defined(__attribute__)
306 #define __attribute__(x)
315 foreach (keys %depfunction) {
316 $depstr .= "#ifndef $_
317 #ifndef __has_extension
318 #define __has_extension(x) 0
319 #define ${_}has_extension 1
321 #if __has_extension(attribute_deprecated_with_message)
322 #define $_(x) __attribute__((__deprecated__(x)))
323 #elif defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1 )))
324 #define $_(X) __attribute__((__deprecated__))
328 #ifdef ${_}has_extension
329 #undef __has_extension
330 #undef ${_}has_extension
336 $public_h_trailer .= "#undef $_
339 $private_h_trailer .= "#undef $_
345 $public_h_header .= $depstr;
346 $private_h_header .= $depstr;
350 $public_h_header .= "#ifdef __cplusplus
355 $public_h_trailer = "#ifdef __cplusplus
359 " . $public_h_trailer;
363 $public_h_header .= "#ifndef $opt_E
364 #ifndef ${opt_E}_FUNCTION
366 #define ${opt_E}_FUNCTION __declspec(dllimport)
367 #define ${opt_E}_CALL __stdcall
368 #define ${opt_E}_VARIABLE __declspec(dllimport)
370 #define ${opt_E}_FUNCTION
371 #define ${opt_E}_CALL
372 #define ${opt_E}_VARIABLE
378 $private_h_header .= "#ifndef $opt_E
379 #ifndef ${opt_E}_FUNCTION
381 #define ${opt_E}_FUNCTION __declspec(dllimport)
382 #define ${opt_E}_CALL __stdcall
383 #define ${opt_E}_VARIABLE __declspec(dllimport)
385 #define ${opt_E}_FUNCTION
386 #define ${opt_E}_CALL
387 #define ${opt_E}_VARIABLE
395 $public_h_trailer .= $undepstr;
396 $private_h_trailer .= $undepstr;
398 if ($public_h ne "" && $flags{"header"}) {
399 $public_h = $public_h_header . $public_h .
400 $public_h_trailer . "#endif /* $block */\n";
402 if ($private_h ne "" && $flags{"header"}) {
403 $private_h = $private_h_header . $private_h .
404 $private_h_trailer . "#endif /* $private */\n";
411 print PRIV
$private_h;
419 if (compare
("${opt_o}.new", ${opt_o
}) != 0) {
420 printf("updating ${opt_o}\n");
421 rename("${opt_o}.new", ${opt_o
});
423 unlink("${opt_o}.new");
428 if (compare
("${opt_p}.new", ${opt_p
}) != 0) {
429 printf("updating ${opt_p}\n");
430 rename("${opt_p}.new", ${opt_p
});
432 unlink("${opt_p}.new");