1 # Make prototypes from .c files
13 my $private_func_re = "^_";
16 getopts
('x:m:o:p:dqE:R:P:') || die "foo";
27 $private_func_re = $opt_R;
30 'multiline-proto' => 1,
32 'function-blocking' => 0,
33 'gnuc-attribute' => 1,
37 foreach $i (split(/,/, $opt_m)) {
39 $flags{"multiline-proto"} = 0;
41 $flags{"function-blocking"} = 0;
42 $flags{"gnuc-attribute"} = 0;
45 if(substr($i, 0, 3) eq "no-") {
46 $flags{substr($i, 3)} = 0;
60 if(/^([a-zA-Z0-9_]+)\s?(.*)$/) {
70 print $brace, " ", $_ if($debug);
75 if ( s@
/\
*.*@@
) { $comment = 1;
76 } elsif ($comment && s@
.*\
*/@@
) { $comment = 0;
77 } elsif ($comment) { next; }
82 if($if_0 && /^\#endif/) {
105 if(!/^static/ && !/^PRIVATE/){
107 if(m/(.*)(__attribute__\s?\(.*\))/) {
111 if(m/(.*)\s(\w+DEPRECATED_FUNCTION)\s?(\(.*\))(.*)/) {
112 $depfunction{$2} = 1;
116 if(m/(.*)\s(\w+DEPRECATED)(.*)/) {
124 while(s/\(([^()]*),(.*)\)/($1\$$2)/g){}
125 s/\<\s*void\s*\>/<>/;
126 # remove parameter names
127 if($opt_P eq "remove") {
128 s/(\s*)([a-zA-Z0-9_]+)([,>])/$3/g;
130 s/\(\*(\s*)([a-zA-Z0-9_]+)\)/(*)/g;
131 } elsif($opt_P eq "comment") {
132 s/([a-zA-Z0-9_]+)([,>])/\/\
*$1\
*\
/$2/g;
133 s/\(\*([a-zA-Z0-9_]+)\)/(*\/\
*$1\
*\
/)/g;
136 # add newlines before parameters
137 if($flags{"multiline-proto"}) {
144 # match function name
145 /([a-zA-Z0-9_]+)\s*\</;
154 # only add newline if more than one parameter
155 if($flags{"multiline-proto"} && /,/){
161 # insert newline before function name
162 if($flags{"multiline-proto"}) {
163 s/(.*)\s([a-zA-Z0-9_]+ \Q$LP\E)/$1\n$2/;
181 $line = $line . " " . $_;
195 open(OUT
, ">${opt_o}.new");
196 $block = &foo
($opt_o);
198 $block = "__public_h__";
202 open(PRIV
, ">${opt_p}.new");
203 $private = &foo
($opt_p);
205 $private = "__private_h__";
211 $public_h_header .= "/* This is a generated file */
217 $public_h_header .= "#ifdef __STDC__
230 $public_h_header .= "#include <stdarg.h>
234 $public_h_trailer = "";
236 $private_h_header = "/* This is a generated file */
242 $private_h_header .= "#ifdef __STDC__
255 $private_h_header .= "#include <stdarg.h>
259 $private_h_trailer = "";
261 foreach(sort keys %funcs){
262 if(/^(main)$/) { next }
263 if ($funcs{$_} =~ /\^/) {
264 $beginblock = "#ifdef __BLOCKS__\n";
265 $endblock = "#endif /* __BLOCKS__ */\n";
267 $beginblock = $endblock = "";
269 if(!defined($exported{$_}) && /$private_func_re/) {
270 $private_h .= $beginblock . $funcs{$_} . "\n" . $endblock . "\n";
271 if($funcs{$_} =~ /__attribute__/) {
272 $private_attribute_seen = 1;
275 if($flags{"function-blocking"}) {
277 if($exported{$_} =~ /proto/) {
278 $public_h .= "#if !defined(HAVE_$fupper) || defined(NEED_${fupper}_PROTO)\n";
280 $public_h .= "#ifndef HAVE_$fupper\n";
283 $public_h .= $beginblock . $funcs{$_} . "\n" . $endblock;
284 if($funcs{$_} =~ /__attribute__/) {
285 $public_attribute_seen = 1;
287 if($flags{"function-blocking"}) {
288 $public_h .= "#endif\n";
294 if($flags{"gnuc-attribute"}) {
295 if ($public_attribute_seen) {
296 $public_h_header .= "#if !defined(__GNUC__) && !defined(__attribute__)
297 #define __attribute__(x)
303 if ($private_attribute_seen) {
304 $private_h_header .= "#if !defined(__GNUC__) && !defined(__attribute__)
305 #define __attribute__(x)
314 foreach (keys %depfunction) {
315 $depstr .= "#ifndef $_
316 #ifndef __has_extension
317 #define __has_extension(x) 0
318 #define ${_}has_extension 1
320 #if __has_extension(attribute_deprecated_with_message)
321 #define $_(x) __attribute__((__deprecated__(x)))
322 #elif defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1 )))
323 #define $_(X) __attribute__((__deprecated__))
327 #ifdef ${_}has_extension
328 #undef __has_extension
329 #undef ${_}has_extension
335 $public_h_trailer .= "#undef $_
338 $private_h_trailer .= "#undef $_
344 $public_h_header .= $depstr;
345 $private_h_header .= $depstr;
349 $public_h_header .= "#ifdef __cplusplus
354 $public_h_trailer = "#ifdef __cplusplus
358 " . $public_h_trailer;
362 $public_h_header .= "#ifndef $opt_E
363 #ifndef ${opt_E}_FUNCTION
365 #define ${opt_E}_FUNCTION __declspec(dllimport)
366 #define ${opt_E}_CALL __stdcall
367 #define ${opt_E}_VARIABLE __declspec(dllimport)
369 #define ${opt_E}_FUNCTION
370 #define ${opt_E}_CALL
371 #define ${opt_E}_VARIABLE
377 $private_h_header .= "#ifndef $opt_E
378 #ifndef ${opt_E}_FUNCTION
380 #define ${opt_E}_FUNCTION __declspec(dllimport)
381 #define ${opt_E}_CALL __stdcall
382 #define ${opt_E}_VARIABLE __declspec(dllimport)
384 #define ${opt_E}_FUNCTION
385 #define ${opt_E}_CALL
386 #define ${opt_E}_VARIABLE
394 $public_h_trailer .= $undepstr;
395 $private_h_trailer .= $undepstr;
397 if ($public_h ne "" && $flags{"header"}) {
398 $public_h = $public_h_header . $public_h .
399 $public_h_trailer . "#endif /* $block */\n";
401 if ($private_h ne "" && $flags{"header"}) {
402 $private_h = $private_h_header . $private_h .
403 $private_h_trailer . "#endif /* $private */\n";
410 print PRIV
$private_h;
418 if (compare
("${opt_o}.new", ${opt_o
}) != 0) {
419 printf("updating ${opt_o}\n");
420 rename("${opt_o}.new", ${opt_o
});
422 unlink("${opt_o}.new");
427 if (compare
("${opt_p}.new", ${opt_p
}) != 0) {
428 printf("updating ${opt_p}\n");
429 rename("${opt_p}.new", ${opt_p
});
431 unlink("${opt_p}.new");