No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / cf / make-proto.pl
blob40bd9394e9d286dddf9bf0f3f19c91a682115f86
1 # Make prototypes from .c files
2 # $Heimdal: make-proto.pl 14183 2004-09-03 08:50:57Z lha $
3 # $NetBSD$
5 ##use Getopt::Std;
6 require 'getopts.pl';
8 $brace = 0;
9 $line = "";
10 $debug = 0;
11 $oproto = 1;
12 $private_func_re = "^_";
14 do Getopts('x:m:o:p:dqE:R:P:') || die "foo";
16 if($opt_d) {
17 $debug = 1;
20 if($opt_q) {
21 $oproto = 0;
24 if($opt_R) {
25 $private_func_re = $opt_R;
27 %flags = (
28 'multiline-proto' => 1,
29 'header' => 1,
30 'function-blocking' => 0,
31 'gnuc-attribute' => 1,
32 'cxx' => 1
34 if($opt_m) {
35 foreach $i (split(/,/, $opt_m)) {
36 if($i eq "roken") {
37 $flags{"multiline-proto"} = 0;
38 $flags{"header"} = 0;
39 $flags{"function-blocking"} = 0;
40 $flags{"gnuc-attribute"} = 0;
41 $flags{"cxx"} = 0;
42 } else {
43 if(substr($i, 0, 3) eq "no-") {
44 $flags{substr($i, 3)} = 0;
45 } else {
46 $flags{$i} = 1;
52 if($opt_x) {
53 open(EXP, $opt_x);
54 while(<EXP>) {
55 chomp;
56 s/\#.*//g;
57 s/\s+/ /g;
58 if(/^([a-zA-Z0-9_]+)\s?(.*)$/) {
59 $exported{$1} = $2;
60 } else {
61 print $_, "\n";
64 close EXP;
67 while(<>) {
68 print $brace, " ", $_ if($debug);
69 if(/^\#if 0/) {
70 $if_0 = 1;
72 if($if_0 && /^\#endif/) {
73 $if_0 = 0;
75 if($if_0) { next }
76 if(/^\s*\#/) {
77 next;
79 if(/^\s*$/) {
80 $line = "";
81 next;
83 if(/\{/){
84 if (!/\}/) {
85 $brace++;
87 $_ = $line;
88 while(s/\*\//\ca/){
89 s/\/\*(.|\n)*\ca//;
91 s/^\s*//;
92 s/\s*$//;
93 s/\s+/ /g;
94 if($_ =~ /\)$/){
95 if(!/^static/ && !/^PRIVATE/){
96 if(/(.*)(__attribute__\s?\(.*\))/) {
97 $attr = $2;
98 $_ = $1;
99 } else {
100 $attr = "";
102 # remove outer ()
103 s/\s*\(/</;
104 s/\)\s?$/>/;
105 # remove , within ()
106 while(s/\(([^()]*),(.*)\)/($1\$$2)/g){}
107 s/\<\s*void\s*\>/<>/;
108 # remove parameter names
109 if($opt_P eq "remove") {
110 s/(\s*)([a-zA-Z0-9_]+)([,>])/$3/g;
111 s/\s+\*/*/g;
112 s/\(\*(\s*)([a-zA-Z0-9_]+)\)/(*)/g;
113 } elsif($opt_P eq "comment") {
114 s/([a-zA-Z0-9_]+)([,>])/\/\*$1\*\/$2/g;
115 s/\(\*([a-zA-Z0-9_]+)\)/(*\/\*$1\*\/)/g;
117 s/\<\>/<void>/;
118 # add newlines before parameters
119 if($flags{"multiline-proto"}) {
120 s/,\s*/,\n\t/g;
121 } else {
122 s/,\s*/, /g;
124 # fix removed ,
125 s/\$/,/g;
126 # match function name
127 /([a-zA-Z0-9_]+)\s*\</;
128 $f = $1;
129 if($oproto) {
130 $LP = "__P((";
131 $RP = "))";
132 } else {
133 $LP = "(";
134 $RP = ")";
136 # only add newline if more than one parameter
137 if($flags{"multiline-proto"} && /,/){
138 s/\</ $LP\n\t/;
139 }else{
140 s/\</ $LP/;
142 s/\>/$RP/;
143 # insert newline before function name
144 if($flags{"multiline-proto"}) {
145 s/(.*)\s([a-zA-Z0-9_]+ \Q$LP\E)/$1\n$2/;
147 if($attr ne "") {
148 $_ .= "\n $attr";
150 $_ = $_ . ";";
151 $funcs{$f} = $_;
154 $line = "";
156 if(/\}/){
157 $brace--;
159 if(/^\}/){
160 $brace = 0;
162 if($brace == 0) {
163 $line = $line . " " . $_;
167 sub foo {
168 local ($arg) = @_;
169 $_ = $arg;
170 s/.*\/([^\/]*)/$1/;
171 s/[^a-zA-Z0-9]/_/g;
172 "__" . $_ . "__";
175 if($opt_o) {
176 open(OUT, ">$opt_o");
177 $block = &foo($opt_o);
178 } else {
179 $block = "__public_h__";
182 if($opt_p) {
183 open(PRIV, ">$opt_p");
184 $private = &foo($opt_p);
185 } else {
186 $private = "__private_h__";
189 $public_h = "";
190 $private_h = "";
192 $public_h_header .= "/* This is a generated file */
193 #ifndef $block
194 #define $block
197 if ($oproto) {
198 $public_h_header .= "#ifdef __STDC__
199 #include <stdarg.h>
200 #ifndef __P
201 #define __P(x) x
202 #endif
203 #else
204 #ifndef __P
205 #define __P(x) ()
206 #endif
207 #endif
210 } else {
211 $public_h_header .= "#include <stdarg.h>
215 $public_h_trailer = "";
217 $private_h_header = "/* This is a generated file */
218 #ifndef $private
219 #define $private
222 if($oproto) {
223 $private_h_header .= "#ifdef __STDC__
224 #include <stdarg.h>
225 #ifndef __P
226 #define __P(x) x
227 #endif
228 #else
229 #ifndef __P
230 #define __P(x) ()
231 #endif
232 #endif
235 } else {
236 $private_h_header .= "#include <stdarg.h>
240 $private_h_trailer = "";
242 foreach(sort keys %funcs){
243 if(/^(main)$/) { next }
244 if(!defined($exported{$_}) && /$private_func_re/) {
245 $private_h .= $funcs{$_} . "\n\n";
246 if($funcs{$_} =~ /__attribute__/) {
247 $private_attribute_seen = 1;
249 } else {
250 if($flags{"function-blocking"}) {
251 $fupper = uc $_;
252 if($exported{$_} =~ /proto/) {
253 $public_h .= "#if !defined(HAVE_$fupper) || defined(NEED_${fupper}_PROTO)\n";
254 } else {
255 $public_h .= "#ifndef HAVE_$fupper\n";
258 $public_h .= $funcs{$_} . "\n";
259 if($funcs{$_} =~ /__attribute__/) {
260 $public_attribute_seen = 1;
262 if($flags{"function-blocking"}) {
263 $public_h .= "#endif\n";
265 $public_h .= "\n";
269 if($flags{"gnuc-attribute"}) {
270 if ($public_attribute_seen) {
271 $public_h_header .= "#if !defined(__GNUC__) && !defined(__attribute__)
272 #define __attribute__(x)
273 #endif
278 if ($private_attribute_seen) {
279 $private_h_header .= "#if !defined(__GNUC__) && !defined(__attribute__)
280 #define __attribute__(x)
281 #endif
286 if($flags{"cxx"}) {
287 $public_h_header .= "#ifdef __cplusplus
288 extern \"C\" {
289 #endif
292 $public_h_trailer .= "#ifdef __cplusplus
294 #endif
299 if ($opt_E) {
300 $public_h_header .= "#ifndef $opt_E
301 #if defined(_WIN32)
302 #define $opt_E _stdcall
303 #else
304 #define $opt_E
305 #endif
306 #endif
310 $private_h_header .= "#ifndef $opt_E
311 #if defined(_WIN32)
312 #define $opt_E _stdcall
313 #else
314 #define $opt_E
315 #endif
316 #endif
321 if ($public_h ne "" && $flags{"header"}) {
322 $public_h = $public_h_header . $public_h .
323 $public_h_trailer . "#endif /* $block */\n";
325 if ($private_h ne "" && $flags{"header"}) {
326 $private_h = $private_h_header . $private_h .
327 $private_h_trailer . "#endif /* $private */\n";
330 if($opt_o) {
331 print OUT $public_h;
333 if($opt_p) {
334 print PRIV $private_h;
337 close OUT;
338 close PRIV;