Do not crash if private key not found
[heimdal.git] / cf / make-proto.pl
blob5b2c03d48d1890d569d92daa3d827934409b74ed
1 # Make prototypes from .c files
2 # $Id$
4 use Getopt::Std;
5 use File::Compare;
7 use JSON;
9 my $comment = 0;
10 my $doxygen = 0;
11 my $funcdoc = 0;
12 my $if_0 = 0;
13 my $brace = 0;
14 my $line = "";
15 my $debug = 0;
16 my $oproto = 1;
17 my $private_func_re = "^_";
18 my %depfunction;
19 my %exported;
20 my %deprecated;
21 my $apple = 0;
22 my %documentation;
24 getopts('x:m:o:p:dqE:R:P:') || die "foo";
25 if($opt_a) {
26 $apple = 1;
29 if($opt_a) {
30 $apple = 1;
33 if($opt_d) {
34 $debug = 1;
37 if($opt_q) {
38 $oproto = 0;
41 if($opt_R) {
42 $private_func_re = $opt_R;
44 my %flags = (
45 'multiline-proto' => 1,
46 'header' => 1,
47 'function-blocking' => 0,
48 'gnuc-attribute' => 1,
49 'cxx' => 1
51 if($opt_m) {
52 foreach $i (split(/,/, $opt_m)) {
53 if($i eq "roken") {
54 $flags{"multiline-proto"} = 0;
55 $flags{"header"} = 0;
56 $flags{"function-blocking"} = 0;
57 $flags{"gnuc-attribute"} = 0;
58 $flags{"cxx"} = 0;
59 } else {
60 if(substr($i, 0, 3) eq "no-") {
61 $flags{substr($i, 3)} = 0;
62 } else {
63 $flags{$i} = 1;
69 if($opt_x) {
70 my $EXP;
71 local $/;
72 open(EXP, '<', $opt_x) || die "open ${opt_x}";
73 my $obj = JSON->new->utf8->decode(<EXP>);
74 close $EXP;
76 foreach my $x (keys %$obj) {
77 if (defined $obj->{$x}->{"export"}) {
78 $exported{$x} = $obj->{$x};
80 if (defined $obj->{$x}->{"deprecated"}) {
81 $deprecated{$x} = $obj->{$x}->{"deprecated"};
86 while(<>) {
87 print $brace, " ", $_ if($debug);
89 # Handle C comments
90 s@/\*.*\*/@@;
91 s@//.*/@@;
92 if ( s@/\*\*(.*)@@) { $comment = 1; $doxygen = 1; $funcdoc = $1;
93 } elsif ( s@/\*.*@@) { $comment = 1;
94 } elsif ($comment && s@.*\*/@@) { $comment = 0; $doxygen = 0;
95 } elsif ($doxygen) { $funcdoc .= $_; next;
96 } elsif ($comment) { next; }
98 if(/^\#if 0/) {
99 $if_0 = 1;
101 if($if_0 && /^\#endif/) {
102 $if_0 = 0;
104 if($if_0) { next }
105 if(/^\s*\#/) {
106 next;
108 if(/^\s*$/) {
109 $line = "";
110 next;
112 if(/\{/){
113 if (!/\}/) {
114 $brace++;
116 $_ = $line;
117 while(s/\*\//\ca/){
118 s/\/\*(.|\n)*\ca//;
120 s/^\s*//;
121 s/\s*$//;
122 s/\s+/ /g;
123 if($_ =~ /\)$/){
124 if(!/^static/ && !/^PRIVATE/){
125 $attr = "";
126 if(m/(.*)(__attribute__\s?\(.*\))/) {
127 $attr .= " $2";
128 $_ = $1;
130 if(m/(.*)\s(\w+DEPRECATED_FUNCTION)\s?(\(.*\))(.*)/) {
131 $depfunction{$2} = 1;
132 $attr .= " $2$3";
133 $_ = "$1 $4";
135 if(m/(.*)\s(\w+DEPRECATED)(.*)/) {
136 $attr .= " $2";
137 $_ = "$1 $3";
139 if(m/(.*)\s(HEIMDAL_\w+_ATTRIBUTE)\s?(\(.*\))?(.*)/) {
140 $attr .= " $2$3";
141 $_ = "$1 $4";
143 # remove outer ()
144 s/\s*\(/</;
145 s/\)\s?$/>/;
146 # remove , within ()
147 while(s/\(([^()]*),(.*)\)/($1\$$2)/g){}
148 s/\<\s*void\s*\>/<>/;
149 # remove parameter names
150 if($opt_P eq "remove") {
151 s/(\s*)([a-zA-Z0-9_]+)([,>])/$3/g;
152 s/\s+\*/*/g;
153 s/\(\*(\s*)([a-zA-Z0-9_]+)\)/(*)/g;
154 } elsif($opt_P eq "comment") {
155 s/([a-zA-Z0-9_]+)([,>])/\/\*$1\*\/$2/g;
156 s/\(\*([a-zA-Z0-9_]+)\)/(*\/\*$1\*\/)/g;
158 s/\<\>/<void>/;
159 # add newlines before parameters
160 if($flags{"multiline-proto"}) {
161 s/,\s*/,\n\t/g;
162 } else {
163 s/,\s*/, /g;
165 # fix removed ,
166 s/\$/,/g;
167 # match function name
168 /([a-zA-Z0-9_]+)\s*\</;
169 $f = $1;
170 if($oproto) {
171 $LP = "__P((";
172 $RP = "))";
173 } else {
174 $LP = "(";
175 $RP = ")";
177 # only add newline if more than one parameter
178 if($flags{"multiline-proto"} && /,/){
179 s/\</ $LP\n\t/;
180 }else{
181 s/\</ $LP/;
183 s/\>/$RP/;
184 # insert newline before function name
185 if($flags{"multiline-proto"}) {
186 s/(.*)\s([a-zA-Z0-9_]+ \Q$LP\E)/$1\n$2/;
188 if($attr ne "") {
189 $_ .= "\n $attr";
191 if ($funcdoc) {
192 $documentation{$f} = $funcdoc;
194 $funcdoc = undef;
195 if ($apple && exists $exported{$f}) {
196 $ios = $exported{$f}{ios};
197 $ios = "NA" if (!defined $ios);
198 $mac = $exported{$f}{macos};
199 $mac = "NA" if (!defined $mac);
200 die "$f neither" if ($mac eq "NA" and $ios eq "NA");
201 $_ = $_ . " __OSX_AVAILABLE_STARTING(__MAC_${mac}, __IPHONE_${ios})";
203 if (exists $deprecated{$f}) {
204 $_ = $_ . " GSSAPI_DEPRECATED_FUNCTION(\"$deprecated{$f}\")";
205 $depfunction{GSSAPI_DEPRECATED_FUNCTION} = 1;
207 $_ = $_ . ";";
208 $funcs{$f} = $_;
211 $line = "";
213 if(/\}/){
214 $brace--;
216 if(/^\}/){
217 $brace = 0;
219 if($brace == 0) {
220 $line = $line . " " . $_;
224 die "reached end of code and still in doxygen comment" if ($doxygen);
225 die "reached end of code and still in comment" if ($comment);
227 sub foo {
228 local ($arg) = @_;
229 $_ = $arg;
230 s/.*\/([^\/]*)/$1/;
231 s/.*\\([^\\]*)/$1/;
232 s/[^a-zA-Z0-9]/_/g;
233 "__" . $_ . "__";
236 if($opt_o) {
237 open(OUT, ">${opt_o}.new");
238 $block = &foo($opt_o);
239 } else {
240 $block = "__public_h__";
243 if($opt_p) {
244 open(PRIV, ">${opt_p}.new");
245 $private = &foo($opt_p);
246 } else {
247 $private = "__private_h__";
250 $public_h = "";
251 $private_h = "";
253 $public_h_header .= "/* This is a generated file */
254 #ifndef $block
255 #define $block
258 if ($oproto) {
259 $public_h_header .= "#ifdef __STDC__
260 #include <stdarg.h>
261 #ifndef __P
262 #define __P(x) x
263 #endif
264 #else
265 #ifndef __P
266 #define __P(x) ()
267 #endif
268 #endif
271 } else {
272 $public_h_header .= "#include <stdarg.h>
276 $public_h_trailer = "";
278 $private_h_header = "/* This is a generated file */
279 #ifndef $private
280 #define $private
283 if($oproto) {
284 $private_h_header .= "#ifdef __STDC__
285 #include <stdarg.h>
286 #ifndef __P
287 #define __P(x) x
288 #endif
289 #else
290 #ifndef __P
291 #define __P(x) ()
292 #endif
293 #endif
296 } else {
297 $private_h_header .= "#include <stdarg.h>
301 $private_h_trailer = "";
304 foreach(sort keys %funcs){
305 if(/^(DllMain|main)$/) { next }
306 if ($funcs{$_} =~ /\^/) {
307 $beginblock = "#ifdef __BLOCKS__\n";
308 $endblock = "#endif /* __BLOCKS__ */\n";
309 } else {
310 $beginblock = $endblock = "";
312 # if we have an export table and doesn't have content, or matches private RE
313 if((scalar(keys(%exported)) ne 0 && !exists $exported{$_} ) || /$private_func_re/) {
314 $private_h .= $beginblock;
315 # if ($apple and not /$private_func_re/) {
316 # $private_h .= "#define $_ __ApplePrivate_${_}\n";
318 $private_h .= $funcs{$_} . "\n" ;
319 $private_h .= $endblock . "\n";
320 if($funcs{$_} =~ /__attribute__/) {
321 $private_attribute_seen = 1;
323 } else {
324 if($documentation{$_}) {
325 $public_h .= "/**\n";
326 $public_h .= "$documentation{$_}";
327 $public_h .= " */\n\n";
329 if($flags{"function-blocking"}) {
330 $fupper = uc $_;
331 if($exported{$_} =~ /proto/) {
332 $public_h .= "#if !defined(HAVE_$fupper) || defined(NEED_${fupper}_PROTO)\n";
333 } else {
334 $public_h .= "#ifndef HAVE_$fupper\n";
337 $public_h .= $beginblock . $funcs{$_} . "\n" . $endblock;
338 if($funcs{$_} =~ /__attribute__/) {
339 $public_attribute_seen = 1;
341 if($flags{"function-blocking"}) {
342 $public_h .= "#endif\n";
344 $public_h .= "\n";
348 if($flags{"gnuc-attribute"}) {
349 if ($public_attribute_seen) {
350 $public_h_header .= "#if !defined(__GNUC__) && !defined(__attribute__)
351 #define __attribute__(x)
352 #endif
357 if ($private_attribute_seen) {
358 $private_h_header .= "#if !defined(__GNUC__) && !defined(__attribute__)
359 #define __attribute__(x)
360 #endif
366 my $depstr = "";
367 my $undepstr = "";
368 foreach (keys %depfunction) {
369 $depstr .= "#ifndef $_
370 #ifndef __has_extension
371 #define __has_extension(x) 0
372 #define ${_}has_extension 1
373 #endif
374 #if __has_extension(attribute_deprecated_with_message)
375 #define $_(x) __attribute__((__deprecated__(x)))
376 #elif defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1 )))
377 #define $_(X) __attribute__((__deprecated__))
378 #else
379 #define $_(X)
380 #endif
381 #ifdef ${_}has_extension
382 #undef __has_extension
383 #undef ${_}has_extension
384 #endif
385 #endif /* $_ */
389 $public_h_trailer .= "#undef $_
392 $private_h_trailer .= "#undef $_
393 #define $_(X)
398 $public_h_header .= $depstr;
399 $private_h_header .= $depstr;
402 if($flags{"cxx"}) {
403 $public_h_header .= "#ifdef __cplusplus
404 extern \"C\" {
405 #endif
408 $public_h_trailer = "#ifdef __cplusplus
410 #endif
412 " . $public_h_trailer;
415 if ($opt_E) {
416 $public_h_header .= "#ifndef $opt_E
417 #ifndef ${opt_E}_FUNCTION
418 #if defined(_WIN32)
419 #define ${opt_E}_FUNCTION __declspec(dllimport)
420 #define ${opt_E}_CALL __stdcall
421 #define ${opt_E}_VARIABLE __declspec(dllimport)
422 #else
423 #define ${opt_E}_FUNCTION
424 #define ${opt_E}_CALL
425 #define ${opt_E}_VARIABLE
426 #endif
427 #endif
428 #endif
431 $private_h_header .= "#ifndef $opt_E
432 #ifndef ${opt_E}_FUNCTION
433 #if defined(_WIN32)
434 #define ${opt_E}_FUNCTION __declspec(dllimport)
435 #define ${opt_E}_CALL __stdcall
436 #define ${opt_E}_VARIABLE __declspec(dllimport)
437 #else
438 #define ${opt_E}_FUNCTION
439 #define ${opt_E}_CALL
440 #define ${opt_E}_VARIABLE
441 #endif
442 #endif
443 #endif
448 $public_h_trailer .= $undepstr;
449 $private_h_trailer .= $undepstr;
451 if ($public_h ne "" && $flags{"header"}) {
452 $public_h = $public_h_header . $public_h .
453 $public_h_trailer . "#endif /* $block */\n";
455 if ($private_h ne "" && $flags{"header"}) {
456 $private_h = $private_h_header . $private_h .
457 $private_h_trailer . "#endif /* $private */\n";
460 if($opt_o) {
461 print OUT $public_h;
463 if($opt_p) {
464 print PRIV $private_h;
467 close OUT;
468 close PRIV;
470 if ($opt_o) {
472 if (compare("${opt_o}.new", ${opt_o}) != 0) {
473 printf("updating ${opt_o}\n");
474 rename("${opt_o}.new", ${opt_o});
475 } else {
476 unlink("${opt_o}.new");
480 if ($opt_p) {
481 if (compare("${opt_p}.new", ${opt_p}) != 0) {
482 printf("updating ${opt_p}\n");
483 rename("${opt_p}.new", ${opt_p});
484 } else {
485 unlink("${opt_p}.new");