passdb: Use dom_sid_str_buf
[amitay/samba.git] / source4 / heimdal / cf / make-proto.pl
1 # Make prototypes from .c files
2 # $Id$
3
4 use Getopt::Std;
5
6 my $comment = 0;
7 my $if_0 = 0;
8 my $brace = 0;
9 my $line = "";
10 my $debug = 0;
11 my $oproto = 1;
12 my $private_func_re = "^_";
13 my %depfunction = ();
14
15 getopts('x:m:o:p:dqE:R:P:') || die "foo";
16
17 if($opt_d) {
18     $debug = 1;
19 }
20
21 if($opt_q) {
22     $oproto = 0;
23 }
24
25 if($opt_R) {
26     $private_func_re = $opt_R;
27 }
28 my %flags = (
29           'multiline-proto' => 1,
30           'header' => 1,
31           'function-blocking' => 0,
32           'gnuc-attribute' => 1,
33           'cxx' => 1
34           );
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;
48             }
49         }
50     }
51 }
52
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";
63         }
64     }
65     close EXP;
66 }
67
68 while(<>) {
69     print $brace, " ", $_ if($debug);
70     
71     # Handle C comments
72     s@/\*.*\*/@@;
73     s@//.*/@@;
74     if ( s@/\*.*@@) { $comment = 1;
75     } elsif ($comment && s@.*\*/@@) { $comment = 0;
76     } elsif ($comment) { next; }
77
78     if(/^\#if 0/) {
79         $if_0 = 1;
80     }
81     if($if_0 && /^\#endif/) {
82         $if_0 = 0;
83     }
84     if($if_0) { next }
85     if(/^\s*\#/) {
86         next;
87     }
88     if(/^\s*$/) {
89         $line = "";
90         next;
91     }
92     if(/\{/){
93         if (!/\}/) {
94             $brace++;
95         }
96         $_ = $line;
97         while(s/\*\//\ca/){
98             s/\/\*(.|\n)*\ca//;
99         }
100         s/^\s*//;
101         s/\s*$//;
102         s/\s+/ /g;
103         if($_ =~ /\)$/){
104             if(!/^static/ && !/^PRIVATE/){
105                 $attr = "";
106                 if(m/(.*)(__attribute__\s?\(.*\))/) {
107                     $attr .= " $2";
108                     $_ = $1;
109                 }
110                 if(m/(.*)\s(\w+DEPRECATED_FUNCTION)\s?(\(.*\))(.*)/) {
111                     $depfunction{$2} = 1;
112                     $attr .= " $2$3";
113                     $_ = "$1 $4";
114                 }
115                 if(m/(.*)\s(\w+DEPRECATED)(.*)/) {
116                     $attr .= " $2";
117                     $_ = "$1 $3";
118                 }
119                 # remove outer ()
120                 s/\s*\(/</;
121                 s/\)\s?$/>/;
122                 # remove , within ()
123                 while(s/\(([^()]*),(.*)\)/($1\$$2)/g){}
124                 s/\<\s*void\s*\>/<>/;
125                 # remove parameter names 
126                 if($opt_P eq "remove") {
127                     s/(\s*)([a-zA-Z0-9_]+)([,>])/$3/g;
128                     s/\s+\*/*/g;
129                     s/\(\*(\s*)([a-zA-Z0-9_]+)\)/(*)/g;
130                 } elsif($opt_P eq "comment") {
131                     s/([a-zA-Z0-9_]+)([,>])/\/\*$1\*\/$2/g;
132                     s/\(\*([a-zA-Z0-9_]+)\)/(*\/\*$1\*\/)/g;
133                 }
134                 s/\<\>/<void>/;
135                 # add newlines before parameters
136                 if($flags{"multiline-proto"}) {
137                     s/,\s*/,\n\t/g;
138                 } else {
139                     s/,\s*/, /g;
140                 }
141                 # fix removed ,
142                 s/\$/,/g;
143                 # match function name
144                 /([a-zA-Z0-9_]+)\s*\</;
145                 $f = $1;
146                 if($oproto) {
147                     $LP = "__P((";
148                     $RP = "))";
149                 } else {
150                     $LP = "(";
151                     $RP = ")";
152                 }
153                 # only add newline if more than one parameter
154                 if($flags{"multiline-proto"} && /,/){ 
155                     s/\</ $LP\n\t/;
156                 }else{
157                     s/\</ $LP/;
158                 }
159                 s/\>/$RP/;
160                 # insert newline before function name
161                 if($flags{"multiline-proto"}) {
162                     s/(.*)\s([a-zA-Z0-9_]+ \Q$LP\E)/$1\n$2/;
163                 }
164                 if($attr ne "") {
165                     $_ .= "\n    $attr";
166                 }
167                 $_ = $_ . ";";
168                 $funcs{$f} = $_;
169             }
170         }
171         $line = "";
172     }
173     if(/\}/){
174         $brace--;
175     }
176     if(/^\}/){
177         $brace = 0;
178     }
179     if($brace == 0) {
180         $line = $line . " " . $_;
181     }
182 }
183
184 sub foo {
185     local ($arg) = @_;
186     $_ = $arg;
187     s/.*\/([^\/]*)/$1/;
188     s/.*\\([^\\]*)/$1/;
189     s/[^a-zA-Z0-9]/_/g;
190     "__" . $_ . "__";
191 }
192
193 if($opt_o) {
194     open(OUT, ">$opt_o");
195     $block = &foo($opt_o);
196 } else {
197     $block = "__public_h__";
198 }
199
200 if($opt_p) {
201     open(PRIV, ">$opt_p");
202     $private = &foo($opt_p);
203 } else {
204     $private = "__private_h__";
205 }
206
207 $public_h = "";
208 $private_h = "";
209
210 $public_h_header .= "/* This is a generated file */
211 #ifndef $block
212 #define $block
213
214 ";
215 if ($oproto) {
216     $public_h_header .= "#ifdef __STDC__
217 #include <stdarg.h>
218 #ifndef __P
219 #define __P(x) x
220 #endif
221 #else
222 #ifndef __P
223 #define __P(x) ()
224 #endif
225 #endif
226
227 ";
228 } else {
229     $public_h_header .= "#include <stdarg.h>
230
231 ";
232 }
233 $public_h_trailer = "";
234
235 $private_h_header = "/* This is a generated file */
236 #ifndef $private
237 #define $private
238
239 ";
240 if($oproto) {
241     $private_h_header .= "#ifdef __STDC__
242 #include <stdarg.h>
243 #ifndef __P
244 #define __P(x) x
245 #endif
246 #else
247 #ifndef __P
248 #define __P(x) ()
249 #endif
250 #endif
251
252 ";
253 } else {
254     $private_h_header .= "#include <stdarg.h>
255
256 ";
257 }
258 $private_h_trailer = "";
259
260 foreach(sort keys %funcs){
261     if(/^(main)$/) { next }
262     if ($funcs{$_} =~ /\^/) {
263         $beginblock = "#ifdef __BLOCKS__\n";
264         $endblock = "#endif /* __BLOCKS__ */\n";
265     } else {
266         $beginblock = $endblock = "";
267     }
268     if(!defined($exported{$_}) && /$private_func_re/) {
269         $private_h .= $beginblock . $funcs{$_} . "\n" . $endblock . "\n";
270         if($funcs{$_} =~ /__attribute__/) {
271             $private_attribute_seen = 1;
272         }
273     } else {
274         if($flags{"function-blocking"}) {
275             $fupper = uc $_;
276             if($exported{$_} =~ /proto/) {
277                 $public_h .= "#if !defined(HAVE_$fupper) || defined(NEED_${fupper}_PROTO)\n";
278             } else {
279                 $public_h .= "#ifndef HAVE_$fupper\n";
280             }
281         }
282         $public_h .= $beginblock . $funcs{$_} . "\n" . $endblock;
283         if($funcs{$_} =~ /__attribute__/) {
284             $public_attribute_seen = 1;
285         }
286         if($flags{"function-blocking"}) {
287             $public_h .= "#endif\n";
288         }
289         $public_h .= "\n";
290     }
291 }
292
293 if($flags{"gnuc-attribute"}) {
294     if ($public_attribute_seen) {
295         $public_h_header .= "#if !defined(__GNUC__) && !defined(__attribute__)
296 #define __attribute__(x)
297 #endif
298
299 ";
300     }
301
302     if ($private_attribute_seen) {
303         $private_h_header .= "#if !defined(__GNUC__) && !defined(__attribute__)
304 #define __attribute__(x)
305 #endif
306
307 ";
308     }
309 }
310
311 my $depstr = "";
312 my $undepstr = "";
313 foreach (keys %depfunction) {
314     $depstr .= "#ifndef $_
315 #if defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1 )))
316 #define $_(X) __attribute__((__deprecated__))
317 #else
318 #define $_(X)
319 #endif
320 #endif
321
322
323 ";
324     $public_h_trailer .= "#undef $_
325
326 ";
327     $private_h_trailer .= "#undef $_
328 #define $_(X)
329
330 ";
331 }
332
333 $public_h_header .= $depstr;
334 $private_h_header .= $depstr;
335
336
337 if($flags{"cxx"}) {
338     $public_h_header .= "#ifdef __cplusplus
339 extern \"C\" {
340 #endif
341
342 ";
343     $public_h_trailer = "#ifdef __cplusplus
344 }
345 #endif
346
347 " . $public_h_trailer;
348
349 }
350 if ($opt_E) {
351     $public_h_header .= "#ifndef $opt_E
352 #ifndef ${opt_E}_FUNCTION
353 #if defined(_WIN32)
354 #define ${opt_E}_FUNCTION __declspec(dllimport)
355 #define ${opt_E}_CALL __stdcall
356 #define ${opt_E}_VARIABLE __declspec(dllimport)
357 #else
358 #define ${opt_E}_FUNCTION
359 #define ${opt_E}_CALL
360 #define ${opt_E}_VARIABLE
361 #endif
362 #endif
363 #endif
364 ";
365     
366     $private_h_header .= "#ifndef $opt_E
367 #ifndef ${opt_E}_FUNCTION
368 #if defined(_WIN32)
369 #define ${opt_E}_FUNCTION __declspec(dllimport)
370 #define ${opt_E}_CALL __stdcall
371 #define ${opt_E}_VARIABLE __declspec(dllimport)
372 #else
373 #define ${opt_E}_FUNCTION
374 #define ${opt_E}_CALL
375 #define ${opt_E}_VARIABLE
376 #endif
377 #endif
378 #endif
379
380 ";
381 }
382     
383 $public_h_trailer .= $undepstr;
384 $private_h_trailer .= $undepstr;
385
386 if ($public_h ne "" && $flags{"header"}) {
387     $public_h = $public_h_header . $public_h . 
388         $public_h_trailer . "#endif /* $block */\n";
389 }
390 if ($private_h ne "" && $flags{"header"}) {
391     $private_h = $private_h_header . $private_h .
392         $private_h_trailer . "#endif /* $private */\n";
393 }
394
395 if($opt_o) {
396     print OUT $public_h;
397
398 if($opt_p) {
399     print PRIV $private_h;
400
401
402 close OUT;
403 close PRIV;