Merge from vendor branch BIND:
[dragonfly.git] / crypto / heimdal / cf / make-proto.pl
1 # Make prototypes from .c files
2 # $Id: make-proto.pl,v 1.15 2002/08/12 16:23:58 joda Exp $
3 # $DragonFly: src/crypto/heimdal/cf/Attic/make-proto.pl,v 1.2 2003/11/14 03:54:29 dillon Exp $
4
5 ##use Getopt::Std;
6 require 'getopts.pl';
7
8 $brace = 0;
9 $line = "";
10 $debug = 0;
11 $oproto = 0;
12 $private_func_re = "^_";
13
14 do Getopts('o:p:dqR:P:') || die "foo";
15
16 if($opt_d) {
17     $debug = 1;
18 }
19
20 if($opt_q) {
21     $oproto = 0;
22 }
23
24 if($opt_R) {
25     $private_func_re = $opt_R;
26 }
27
28 while(<>) {
29     print $brace, " ", $_ if($debug);
30     if(/^\#if 0/) {
31         $if_0 = 1;
32     }
33     if($if_0 && /^\#endif/) {
34         $if_0 = 0;
35     }
36     if($if_0) { next }
37     if(/^\s*\#/) {
38         next;
39     }
40     if(/^\s*$/) {
41         $line = "";
42         next;
43     }
44     if(/\{/){
45         if (!/\}/) {
46             $brace++;
47         }
48         $_ = $line;
49         while(s/\*\//\ca/){
50             s/\/\*(.|\n)*\ca//;
51         }
52         s/^\s*//;
53         s/\s$//;
54         s/\s+/ /g;
55         if($line =~ /\)\s$/){
56             if(!/^static/ && !/^PRIVATE/){
57                 if(/(.*)(__attribute__\s?\(.*\))/) {
58                     $attr = $2;
59                     $_ = $1;
60                 } else {
61                     $attr = "";
62                 }
63                 # remove outer ()
64                 s/\s*\(/</;
65                 s/\)\s?$/>/;
66                 # remove , within ()
67                 while(s/\(([^()]*),(.*)\)/($1\$$2)/g){}
68                 s/\<\s*void\s*\>/<>/;
69                 # remove parameter names 
70                 if($opt_P eq "remove") {
71                     s/(\s*)([a-zA-Z0-9_]+)([,>])/$3/g;
72                     s/\(\*(\s*)([a-zA-Z0-9_]+)\)/(*)/g;
73                 } elsif($opt_P eq "comment") {
74                     s/([a-zA-Z0-9_]+)([,>])/\/\*$1\*\/$2/g;
75                     s/\(\*([a-zA-Z0-9_]+)\)/(*\/\*$1\*\/)/g;
76                 }
77                 s/\<\>/<void>/;
78                 # add newlines before parameters
79                 s/,\s*/,\n\t/g;
80                 # fix removed ,
81                 s/\$/,/g;
82                 # match function name
83                 /([a-zA-Z0-9_]+)\s*\</;
84                 $f = $1;
85                 if($oproto) {
86                     $LP = "__P((";
87                     $RP = "))";
88                 } else {
89                     $LP = "(";
90                     $RP = ")";
91                 }
92                 # only add newline if more than one parameter
93                 if(/,/){ 
94                     s/\</ $LP\n\t/;
95                 }else{
96                     s/\</ $LP/;
97                 }
98                 s/\>/$RP/;
99                 # insert newline before function name
100                 s/(.*)\s([a-zA-Z0-9_]+ \Q$LP\E)/$1\n$2/;
101                 if($attr ne "") {
102                     $_ .= "\n    $attr";
103                 }
104                 $_ = $_ . ";";
105                 $funcs{$f} = $_;
106             }
107         }
108         $line = "";
109     }
110     if(/\}/){
111         $brace--;
112     }
113     if(/^\}/){
114         $brace = 0;
115     }
116     if($brace == 0) {
117         $line = $line . " " . $_;
118     }
119 }
120
121 sub foo {
122     local ($arg) = @_;
123     $_ = $arg;
124     s/.*\/([^\/]*)/$1/;
125     s/[^a-zA-Z0-9]/_/g;
126     "__" . $_ . "__";
127 }
128
129 if($opt_o) {
130     open(OUT, ">$opt_o");
131     $block = &foo($opt_o);
132 } else {
133     $block = "__public_h__";
134 }
135
136 if($opt_p) {
137     open(PRIV, ">$opt_p");
138     $private = &foo($opt_p);
139 } else {
140     $private = "__private_h__";
141 }
142
143 $public_h = "";
144 $private_h = "";
145
146 $public_h_header = "/* This is a generated file */
147 #ifndef $block
148 #define $block
149
150 ";
151 if ($oproto) {
152 $public_h_header .= "#ifdef __STDC__
153 #include <stdarg.h>
154 #ifndef __P
155 #define __P(x) x
156 #endif
157 #else
158 #ifndef __P
159 #define __P(x) ()
160 #endif
161 #endif
162
163 ";
164 } else {
165     $public_h_header .= "#include <stdarg.h>
166
167 ";
168 }
169
170 $private_h_header = "/* This is a generated file */
171 #ifndef $private
172 #define $private
173
174 ";
175 if($oproto) {
176 $private_h_header .= "#ifdef __STDC__
177 #include <stdarg.h>
178 #ifndef __P
179 #define __P(x) x
180 #endif
181 #else
182 #ifndef __P
183 #define __P(x) ()
184 #endif
185 #endif
186
187 ";
188 } else {
189     $private_h_header .= "#include <stdarg.h>
190
191 ";
192 }
193 foreach(sort keys %funcs){
194     if(/^(main)$/) { next }
195     if(/$private_func_re/) {
196         $private_h .= $funcs{$_} . "\n\n";
197         if($funcs{$_} =~ /__attribute__/) {
198             $private_attribute_seen = 1;
199         }
200     } else {
201         $public_h .= $funcs{$_} . "\n\n";
202         if($funcs{$_} =~ /__attribute__/) {
203             $public_attribute_seen = 1;
204         }
205     }
206 }
207
208 if ($public_attribute_seen) {
209     $public_h_header .= "#if !defined(__GNUC__) && !defined(__attribute__)
210 #define __attribute__(x)
211 #endif
212
213 ";
214 }
215
216 if ($private_attribute_seen) {
217     $private_h_header .= "#if !defined(__GNUC__) && !defined(__attribute__)
218 #define __attribute__(x)
219 #endif
220
221 ";
222 }
223
224
225 if ($public_h ne "") {
226     $public_h = $public_h_header . $public_h . "#endif /* $block */\n";
227 }
228 if ($private_h ne "") {
229     $private_h = $private_h_header . $private_h . "#endif /* $private */\n";
230 }
231
232 if($opt_o) {
233     print OUT $public_h;
234
235 if($opt_p) {
236     print PRIV $private_h;
237
238
239 close OUT;
240 close PRIV;