use lu and typecast to avoid warning for sizeof() between 32 and 64 bit hosts
[heimdal.git] / cf / make-proto.pl
blob12c6d07f5e524e93e1d61abdd6bc737706845401
1 # Make prototypes from .c files
2 # $Id$
4 ##use Getopt::Std;
5 require 'getopts.pl';
7 my $comment = 0;
8 my $if_0 = 0;
9 my $brace = 0;
10 my $line = "";
11 my $debug = 0;
12 my $oproto = 1;
13 my $private_func_re = "^_";
15 do Getopts('x:m:o:p:dqE:R:P:') || die "foo";
17 if($opt_d) {
18 $debug = 1;
21 if($opt_q) {
22 $oproto = 0;
25 if($opt_R) {
26 $private_func_re = $opt_R;
28 %flags = (
29 'multiline-proto' => 1,
30 'header' => 1,
31 'function-blocking' => 0,
32 'gnuc-attribute' => 1,
33 'cxx' => 1
35 if($opt_m) {
36 foreach $i (split(/,/, $opt_m)) {
37 if($i eq "roken") {
38 $flags{"multiline-proto"} = 0;
39 $flags{"header"} = 0;
40 $flags{"function-blocking"} = 0;
41 $flags{"gnuc-attribute"} = 0;
42 $flags{"cxx"} = 0;
43 } else {
44 if(substr($i, 0, 3) eq "no-") {
45 $flags{substr($i, 3)} = 0;
46 } else {
47 $flags{$i} = 1;
53 if($opt_x) {
54 open(EXP, $opt_x);
55 while(<EXP>) {
56 chomp;
57 s/\#.*//g;
58 s/\s+/ /g;
59 if(/^([a-zA-Z0-9_]+)\s?(.*)$/) {
60 $exported{$1} = $2;
61 } else {
62 print $_, "\n";
65 close EXP;
68 while(<>) {
69 print $brace, " ", $_ if($debug);
71 # Handle C comments
72 s@/\*.*\*/@@;
73 s@//.*/@@;
74 if ( s@/\*.*@@) { $comment = 1;
75 } elsif ($comment && s@.*\*/@@) { $comment = 0;
76 } elsif ($comment) { next; }
78 if(/^\#if 0/) {
79 $if_0 = 1;
81 if($if_0 && /^\#endif/) {
82 $if_0 = 0;
84 if($if_0) { next }
85 if(/^\s*\#/) {
86 next;
88 if(/^\s*$/) {
89 $line = "";
90 next;
92 if(/\{/){
93 if (!/\}/) {
94 $brace++;
96 $_ = $line;
97 while(s/\*\//\ca/){
98 s/\/\*(.|\n)*\ca//;
100 s/^\s*//;
101 s/\s*$//;
102 s/\s+/ /g;
103 if($_ =~ /\)$/ or $_ =~ /DEPRECATED$/){
104 if(!/^static/ && !/^PRIVATE/){
105 $attr = "";
106 if(m/(.*)(__attribute__\s?\(.*\))/) {
107 $attr .= " $2";
108 $_ = $1;
110 if(m/(.*)\s(\w+DEPRECATED)/) {
111 $attr .= " $2";
112 $_ = $1;
114 # remove outer ()
115 s/\s*\(/</;
116 s/\)\s?$/>/;
117 # remove , within ()
118 while(s/\(([^()]*),(.*)\)/($1\$$2)/g){}
119 s/\<\s*void\s*\>/<>/;
120 # remove parameter names
121 if($opt_P eq "remove") {
122 s/(\s*)([a-zA-Z0-9_]+)([,>])/$3/g;
123 s/\s+\*/*/g;
124 s/\(\*(\s*)([a-zA-Z0-9_]+)\)/(*)/g;
125 } elsif($opt_P eq "comment") {
126 s/([a-zA-Z0-9_]+)([,>])/\/\*$1\*\/$2/g;
127 s/\(\*([a-zA-Z0-9_]+)\)/(*\/\*$1\*\/)/g;
129 s/\<\>/<void>/;
130 # add newlines before parameters
131 if($flags{"multiline-proto"}) {
132 s/,\s*/,\n\t/g;
133 } else {
134 s/,\s*/, /g;
136 # fix removed ,
137 s/\$/,/g;
138 # match function name
139 /([a-zA-Z0-9_]+)\s*\</;
140 $f = $1;
141 if($oproto) {
142 $LP = "__P((";
143 $RP = "))";
144 } else {
145 $LP = "(";
146 $RP = ")";
148 # only add newline if more than one parameter
149 if($flags{"multiline-proto"} && /,/){
150 s/\</ $LP\n\t/;
151 }else{
152 s/\</ $LP/;
154 s/\>/$RP/;
155 # insert newline before function name
156 if($flags{"multiline-proto"}) {
157 s/(.*)\s([a-zA-Z0-9_]+ \Q$LP\E)/$1\n$2/;
159 if($attr ne "") {
160 $_ .= "\n $attr";
162 $_ = $_ . ";";
163 $funcs{$f} = $_;
166 $line = "";
168 if(/\}/){
169 $brace--;
171 if(/^\}/){
172 $brace = 0;
174 if($brace == 0) {
175 $line = $line . " " . $_;
179 sub foo {
180 local ($arg) = @_;
181 $_ = $arg;
182 s/.*\/([^\/]*)/$1/;
183 s/[^a-zA-Z0-9]/_/g;
184 "__" . $_ . "__";
187 if($opt_o) {
188 open(OUT, ">$opt_o");
189 $block = &foo($opt_o);
190 } else {
191 $block = "__public_h__";
194 if($opt_p) {
195 open(PRIV, ">$opt_p");
196 $private = &foo($opt_p);
197 } else {
198 $private = "__private_h__";
201 $public_h = "";
202 $private_h = "";
204 $public_h_header .= "/* This is a generated file */
205 #ifndef $block
206 #define $block
209 if ($oproto) {
210 $public_h_header .= "#ifdef __STDC__
211 #include <stdarg.h>
212 #ifndef __P
213 #define __P(x) x
214 #endif
215 #else
216 #ifndef __P
217 #define __P(x) ()
218 #endif
219 #endif
222 } else {
223 $public_h_header .= "#include <stdarg.h>
227 $public_h_trailer = "";
229 $private_h_header = "/* This is a generated file */
230 #ifndef $private
231 #define $private
234 if($oproto) {
235 $private_h_header .= "#ifdef __STDC__
236 #include <stdarg.h>
237 #ifndef __P
238 #define __P(x) x
239 #endif
240 #else
241 #ifndef __P
242 #define __P(x) ()
243 #endif
244 #endif
247 } else {
248 $private_h_header .= "#include <stdarg.h>
252 $private_h_trailer = "";
254 foreach(sort keys %funcs){
255 if(/^(main)$/) { next }
256 if ($funcs{$_} =~ /\^/) {
257 $beginblock = "#ifdef __BLOCKS__\n";
258 $endblock = "#endif /* __BLOCKS__ */\n";
259 } else {
260 $beginblock = $endblock = "";
262 if(!defined($exported{$_}) && /$private_func_re/) {
263 $private_h .= $beginblock . $funcs{$_} . "\n" . $endblock . "\n";
264 if($funcs{$_} =~ /__attribute__/) {
265 $private_attribute_seen = 1;
267 } else {
268 if($flags{"function-blocking"}) {
269 $fupper = uc $_;
270 if($exported{$_} =~ /proto/) {
271 $public_h .= "#if !defined(HAVE_$fupper) || defined(NEED_${fupper}_PROTO)\n";
272 } else {
273 $public_h .= "#ifndef HAVE_$fupper\n";
276 $public_h .= $beginblock . $funcs{$_} . "\n" . $endblock;
277 if($funcs{$_} =~ /__attribute__/) {
278 $public_attribute_seen = 1;
280 if($flags{"function-blocking"}) {
281 $public_h .= "#endif\n";
283 $public_h .= "\n";
287 if($flags{"gnuc-attribute"}) {
288 if ($public_attribute_seen) {
289 $public_h_header .= "#if !defined(__GNUC__) && !defined(__attribute__)
290 #define __attribute__(x)
291 #endif
296 if ($private_attribute_seen) {
297 $private_h_header .= "#if !defined(__GNUC__) && !defined(__attribute__)
298 #define __attribute__(x)
299 #endif
304 if($flags{"cxx"}) {
305 $public_h_header .= "#ifdef __cplusplus
306 extern \"C\" {
307 #endif
310 $public_h_trailer .= "#ifdef __cplusplus
312 #endif
317 if ($opt_E) {
318 $public_h_header .= "#ifndef $opt_E
319 #ifndef ${opt_E}_FUNCTION
320 #if defined(_WIN32)
321 #define ${opt_E}_FUNCTION __declspec(dllimport)
322 #define ${opt_E}_CALL __stdcall
323 #define ${opt_E}_VARIABLE __declspec(dllimport)
324 #else
325 #define ${opt_E}_FUNCTION
326 #define ${opt_E}_CALL
327 #define ${opt_E}_VARIABLE
328 #endif
329 #endif
330 #endif
333 $private_h_header .= "#ifndef $opt_E
334 #ifndef ${opt_E}_FUNCTION
335 #if defined(_WIN32)
336 #define ${opt_E}_FUNCTION __declspec(dllimport)
337 #define ${opt_E}_CALL __stdcall
338 #define ${opt_E}_VARIABLE __declspec(dllimport)
339 #else
340 #define ${opt_E}_FUNCTION
341 #define ${opt_E}_CALL
342 #define ${opt_E}_VARIABLE
343 #endif
344 #endif
345 #endif
350 if ($public_h ne "" && $flags{"header"}) {
351 $public_h = $public_h_header . $public_h .
352 $public_h_trailer . "#endif /* $block */\n";
354 if ($private_h ne "" && $flags{"header"}) {
355 $private_h = $private_h_header . $private_h .
356 $private_h_trailer . "#endif /* $private */\n";
359 if($opt_o) {
360 print OUT $public_h;
362 if($opt_p) {
363 print PRIV $private_h;
366 close OUT;
367 close PRIV;