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