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