r13355: check controls are correctly exported
[Samba/aatanasov.git] / source4 / script / mkproto.pl
blob9939fe0bf7cfdd1bf3c749bf314b5dca908dd78d
1 #!/usr/bin/perl
3 use strict;
5 # don't use warnings module as it is not portable enough
6 # use warnings;
9 use Getopt::Long;
11 my $public_file = undef;
12 my $private_file = undef;
13 my $public_define = undef;
14 my $private_define = undef;
15 my $public_fd = \*STDOUT;
16 my $private_fd = \*STDOUT;
18 sub usage()
20 print "Usage: mkproto.pl [options] [c files]\n";
21 print "OPTIONS:\n";
22 print " --public=FILE Write prototypes for public functions to FILE\n";
23 print " --private=FILE Write prototypes for private functions to FILE\n";
24 print " --define=DEF Use DEF to check whether header was already included\n";
25 print " --public-define=DEF Same as --define, but just for public header\n";
26 print " --private-define=DEF Same as --define, but just for private header\n";
27 print " --help Print this help message\n\n";
28 exit 0;
31 GetOptions(
32 'public=s' => sub { my ($f,$v) = @_; $public_file = $v; },
33 'private=s' => sub { my ($f,$v) = @_; $private_file = $v; },
34 'define=s' => sub {
35 my ($f,$v) = @_;
36 $public_define = $v;
37 $private_define = "$v\_PRIVATE";
39 'public-define=s' => \$public_define,
40 'private-define=s' => \$private_define,
41 'help' => \&usage
42 ) or exit(1);
44 if (not defined($public_define) and defined($public_file)) {
45 $public_define = ".." . uc($public_file) . "__";
46 $public_define =~ tr{./}{__};
47 } elsif (not defined($public_define)) {
48 $public_define = '_PROTO_H_';
51 if (not defined($private_define) and defined($private_file)) {
52 $private_define = "__" . uc($private_file) . "__";
53 $private_define =~ tr{./}{__};
54 } elsif (not defined($public_define)) {
55 $public_define = '_PROTO_H_';
58 if (defined($public_file)) {
59 open PUBLIC, ">$public_file" or die("Can't open `$public_file': $!");
60 $public_fd = \*PUBLIC;
63 if ($private_file eq $public_file) {
64 $private_fd = $public_fd;
65 } elsif (defined($private_file)) {
66 open PRIVATE, ">$private_file" or die("Can't open `$private_file': $!"); ;
67 $private_fd = \*PRIVATE;
70 sub print_header($$)
72 my ($file, $header_name) = @_;
73 print $file "#ifndef $header_name\n";
74 print $file "#define $header_name\n\n";
75 print $file "#undef _PRINTF_ATTRIBUTE\n";
76 print $file "#define _PRINTF_ATTRIBUTE(a1, a2) PRINTF_ATTRIBUTE(a1, a2)\n";
77 print $file "/* This file was automatically generated by mkproto.pl. DO NOT EDIT */\n\n";
80 sub print_footer($$)
82 my ($file, $header_name) = @_;
83 print $file "#undef _PRINTF_ATTRIBUTE\n";
84 print $file "#define _PRINTF_ATTRIBUTE(a1, a2)\n";
85 print $file "\n#endif /* $header_name */\n\n";
88 sub handle_loadparm($$)
90 my ($file,$line) = @_;
92 if ($line =~ /^FN_(GLOBAL|LOCAL)_(CONST_STRING|STRING|BOOL|CHAR|INTEGER|LIST)\((\w+),.*\)/o) {
93 my $scope = $1;
94 my $type = $2;
95 my $name = $3;
97 my %tmap = (
98 "BOOL" => "BOOL ",
99 "CONST_STRING" => "const char *",
100 "STRING" => "const char *",
101 "INTEGER" => "int ",
102 "CHAR" => "char ",
103 "LIST" => "const char **",
106 my %smap = (
107 "GLOBAL" => "void",
108 "LOCAL" => "int "
111 print $file "$tmap{$type}$name($smap{$scope});\n";
115 sub process_file($$$)
117 my ($public_file, $private_file, $filename) = @_;
119 $filename =~ s/\.o$/\.c/g;
121 open(FH, "< $filename") || die "Failed to open $filename";
123 print $private_file "\n/* The following definitions come from $filename */\n\n";
125 while (my $line = <FH>) {
126 my $target = $private_file;
127 my $is_public = 0;
129 # these are ordered for maximum speed
130 next if ($line =~ /^\s/);
132 next unless ($line =~ /\(/);
134 next if ($line =~ /^\/|[;]/);
136 if ($line =~ s/^_PUBLIC_[\t ]/extern /) {
137 $target = $public_file;
138 $is_public = 1;
141 next unless ( $is_public || $line =~ /
142 ^void|^BOOL|^int|^struct|^char|^const|^\w+_[tT]\s|^uint|^unsigned|^long|
143 ^NTSTATUS|^ADS_STATUS|^enum\s.*\(|^DATA_BLOB|^WERROR|^XFILE|^FILE|^DIR|
144 ^double|^TDB_CONTEXT|^TDB_DATA|^TALLOC_CTX|^NTTIME|^FN_|^init_module|
145 ^GtkWidget|^GType|^smb_ucs2_t
146 /xo);
148 next if ($line =~ /^int\s*main/);
150 if ($line =~ /^FN_/) {
151 handle_loadparm($public_file, $line);
152 next;
155 if ( $line =~ /\(.*\)\s*$/o ) {
156 chomp $line;
157 print $target "$line;\n";
158 next;
161 print $target $line;
163 while ($line = <FH>) {
164 if ($line =~ /\)\s*$/o) {
165 chomp $line;
166 print $target "$line;\n";
167 last;
169 print $target $line;
173 close(FH);
176 print_header($public_fd, $public_define);
177 if ($public_file ne $private_file) {
178 print_header($private_fd, $private_define);
180 print $private_fd "/* this file contains prototypes for functions that " .
181 "are private \n * to this subsystem or library. These functions " .
182 "should not be \n * used outside this particular subsystem! */\n\n";
184 print $public_fd "/* this file contains prototypes for functions that " .
185 "are part of \n * the public API of this subsystem or library. */\n\n";
187 process_file($public_fd, $private_fd, $_) foreach (@ARGV);
188 print_footer($public_fd, $public_define);
189 if ($public_file ne $private_file) {
190 print_footer($private_fd, $private_define);