r12449: Fix some warnings
[samba.git] / source4 / script / mkproto.pl
1 #!/usr/bin/perl
2
3 use strict;
4
5 # don't use warnings module as it is not portable enough
6 # use warnings;
7
8
9 use Getopt::Long;
10
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;
17
18 GetOptions(
19         'public=s' => sub { my ($f,$v) = @_; $public_file = $v; },
20         'private=s' => sub { my ($f,$v) = @_; $private_file = $v; },
21         'define=s' => sub { 
22                 my ($f,$v) = @_; 
23                 $public_define = $v; 
24                 $private_define = "$v\_PRIVATE"; 
25         },
26         'public-define=s' => \$public_define,
27         'private-define=s' => \$private_define
28 );
29
30 if (not defined($public_define) and defined($public_file)) {
31         $public_define = $public_file;
32         $public_define =~ tr{./}{__};
33 } elsif (not defined($public_define)) {
34         $public_define = '_PROTO_H_';
35 }
36
37 if (not defined($private_define) and defined($private_file)) {
38         $private_define = $private_file;
39         $private_define =~ tr{./}{__};
40 } elsif (not defined($public_define)) {
41         $public_define = '_PROTO_H_';
42 }
43
44 if (defined($public_file)) {
45         open PUBLIC, ">$public_file"; 
46         $public_fd = \*PUBLIC;
47 }
48
49 if ($private_file eq $public_file) {
50         $private_fd = $public_fd;
51 } elsif (not defined($private_file)) {
52         open PRIVATE, ">$private_file"; 
53         $private_fd = \*PRIVATE;
54 }
55
56 sub print_header($$)
57 {
58         my ($file, $header_name) = @_;
59         print $file "#ifndef $header_name\n";
60         print $file "#define $header_name\n\n";
61         print $file "/* This file is automatically generated with \"make proto\". DO NOT EDIT */\n\n";
62 }
63
64 sub print_footer($$) 
65 {
66         my ($file, $header_name) = @_;
67         printf $file "\n#endif /*  %s  */\n", $header_name;
68 }
69
70 sub handle_loadparm($$) 
71 {
72         my ($file,$line) = @_;
73
74         if ($line =~ /^FN_(GLOBAL|LOCAL)_(CONST_STRING|STRING|BOOL|CHAR|INTEGER|LIST)\((\w+),.*\)/o) {
75                 my $scope = $1;
76                 my $type = $2;
77                 my $name = $3;
78
79                 my %tmap = (
80                             "BOOL" => "BOOL ",
81                             "CONST_STRING" => "const char *",
82                             "STRING" => "const char *",
83                             "INTEGER" => "int ",
84                             "CHAR" => "char ",
85                             "LIST" => "const char **",
86                             );
87
88                 my %smap = (
89                             "GLOBAL" => "void",
90                             "LOCAL" => "int "
91                             );
92
93                 print $file "$tmap{$type}$name($smap{$scope});\n";
94         }
95 }
96
97 sub process_file($$$) 
98 {
99         my ($public_file, $private_file, $filename) = @_;
100
101         $filename =~ s/\.o$/\.c/g;
102
103         open(FH, "< $filename") || die "Failed to open $filename";
104
105         print $private_file "\n/* The following definitions come from $filename  */\n\n";
106
107         while (my $line = <FH>) {             
108                 my $target = $private_file;
109
110                 # these are ordered for maximum speed
111                 next if ($line =~ /^\s/);
112               
113                 next unless ($line =~ /\(/);
114
115                 next if ($line =~ /^\/|[;]/);
116
117                 next unless ( $line =~ /
118                               ^void|^BOOL|^int|^struct|^char|^const|^\w+_[tT]\s|^uint|^unsigned|^long|
119                               ^NTSTATUS|^ADS_STATUS|^enum\s.*\(|^DATA_BLOB|^WERROR|^XFILE|^FILE|^DIR|
120                               ^double|^TDB_CONTEXT|^TDB_DATA|^TALLOC_CTX|^NTTIME|^FN_|^REG_KEY|^REG_HANDLE|^REG_VAL|
121                               ^GtkWidget|^GType|^smb_ucs2_t
122                               /xo);
123
124                 next if ($line =~ /^int\s*main/);
125
126                 if ($line =~ /^FN_/) {
127                         handle_loadparm($public_file, $line);
128                         next;
129                 }
130
131                 if ($line =~ s/_PUBLIC_//xo) {
132                         $target = $public_file;
133                 }
134
135                 if ( $line =~ /\(.*\)\s*$/o ) {
136                         chomp $line;
137                         print $target "$line;\n";
138                         next;
139                 }
140
141                 print $target $line;
142
143                 while ($line = <FH>) {
144                         if ($line =~ /\)\s*$/o) {
145                                 chomp $line;
146                                 print $target "$line;\n";
147                                 last;
148                         }
149                         print $target $line;
150                 }
151         }
152
153         close(FH);
154 }
155
156 if ($public_file ne $private_file) {
157         print_header($private_fd, $private_define);
158 }
159 print_header($public_fd, $public_define);
160 process_file($public_fd, $private_fd, $_) foreach (@ARGV);
161 print_footer($public_fd, $public_define);
162 if ($public_file ne $private_file) {
163         print_footer($private_fd, $private_define);
164 }