This commit was manufactured by cvs2svn to create tag
[heimdal.git] / cf / make-proto.pl
blob0cff8f67345477df7e3742ef046112a4d78f5456
1 # Make prototypes from .c files
2 # $Id$
4 ##use Getopt::Std;
5 require 'getopts.pl';
7 $brace = 0;
8 $line = "";
9 $debug = 0;
11 do Getopts('o:p:d') || die "foo";
13 if($opt_d) {
14 $debug = 1;
17 while(<>) {
18 print $brace, " ", $_ if($debug);
19 if(/^\#if 0/) {
20 $if_0 = 1;
22 if($if_0 && /^\#endif/) {
23 $if_0 = 0;
25 if($if_0) { next }
26 if(/^\s*\#/) {
27 next;
29 if(/^\s*$/) {
30 $line = "";
31 next;
33 if(/\{/){
34 if (!/\}/) {
35 $brace++;
37 $_ = $line;
38 while(s/\*\//\ca/){
39 s/\/\*(.|\n)*\ca//;
41 s/^\s*//;
42 s/\s$//;
43 s/\s+/ /g;
44 if($line =~ /\)\s$/){
45 if(!/^static/ && !/^PRIVATE/){
46 if(/(.*)(__attribute__\s?\(.*\))/) {
47 $attr = $2;
48 $_ = $1;
49 } else {
50 $attr = "";
52 # remove outer ()
53 s/\s*\(/@/;
54 s/\)\s?$/@/;
55 # remove , within ()
56 while(s/\(([^()]*),(.*)\)/($1\$$2)/g){}
57 s/,\s*/,\n\t/g;
58 # fix removed ,
59 s/\$/,/g;
60 # match function name
61 /([a-zA-Z0-9_]+)\s*@/;
62 $f = $1;
63 # only add newline if more than one parameter
64 $LP = "(("; # XXX workaround for indentation bug in emacs
65 $RP = "))";
66 $P = "__P((";
67 if(/,/){
68 s/@/ __P$LP\n\t/;
69 }else{
70 s/@/ __P$LP/;
72 s/@/$RP/;
73 # insert newline before function name
74 s/(.*)\s([a-zA-Z0-9_]+ __P)/$1\n$2/;
75 if($attr ne "") {
76 $_ .= "\n $attr";
78 $_ = $_ . ";";
79 $funcs{$f} = $_;
82 $line = "";
84 if(/\}/){
85 $brace--;
87 if(/^\}/){
88 $brace = 0;
90 if($brace == 0) {
91 $line = $line . " " . $_;
95 sub foo {
96 local ($arg) = @_;
97 $_ = $arg;
98 s/.*\/([^\/]*)/$1/;
99 s/[^a-zA-Z0-9]/_/g;
100 "__" . $_ . "__";
103 if($opt_o) {
104 open(OUT, ">$opt_o");
105 $block = &foo($opt_o);
106 } else {
107 $block = "__public_h__";
110 if($opt_p) {
111 open(PRIV, ">$opt_p");
112 $private = &foo($opt_p);
113 } else {
114 $private = "__private_h__";
117 $public_h = "";
118 $private_h = "";
120 $public_h_header = "/* This is a generated file */
121 #ifndef $block
122 #define $block
124 #ifdef __STDC__
125 #include <stdarg.h>
126 #ifndef __P
127 #define __P(x) x
128 #endif
129 #else
130 #ifndef __P
131 #define __P(x) ()
132 #endif
133 #endif
137 $private_h_header = "/* This is a generated file */
138 #ifndef $private
139 #define $private
141 #ifdef __STDC__
142 #include <stdarg.h>
143 #ifndef __P
144 #define __P(x) x
145 #endif
146 #else
147 #ifndef __P
148 #define __P(x) ()
149 #endif
150 #endif
154 foreach(sort keys %funcs){
155 if(/^(main)$/) { next }
156 if(/^_/) {
157 $private_h .= $funcs{$_} . "\n\n";
158 if($funcs{$_} =~ /__attribute__/) {
159 $private_attribute_seen = 1;
161 } else {
162 $public_h .= $funcs{$_} . "\n\n";
163 if($funcs{$_} =~ /__attribute__/) {
164 $public_attribute_seen = 1;
169 if ($public_attribute_seen) {
170 $public_h_header .= "#if !defined(__GNUC__) && !defined(__attribute__)
171 #define __attribute__(x)
172 #endif
177 if ($private_attribute_seen) {
178 $private_h_header .= "#if !defined(__GNUC__) && !defined(__attribute__)
179 #define __attribute__(x)
180 #endif
186 if ($public_h ne "") {
187 $public_h = $public_h_header . $public_h . "#endif /* $block */\n";
189 if ($private_h ne "") {
190 $private_h = $private_h_header . $private_h . "#endif /* $private */\n";
193 if($opt_o) {
194 print OUT $public_h;
196 if($opt_p) {
197 print PRIV $private_h;
200 close OUT;
201 close PRIV;