479d53da32575aa1a05cf7db82f4066b8e57ed94
[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 sub usage()
19 {
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;
29 }
30
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"; 
38         },
39         'public-define=s' => \$public_define,
40         'private-define=s' => \$private_define,
41         'help' => \&usage
42 ) or exit(1);
43
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_';
49 }
50
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_';
56 }
57
58 if (defined($public_file)) {
59         open PUBLIC, ">$public_file"; 
60         $public_fd = \*PUBLIC;
61 }
62
63 if ($private_file eq $public_file) {
64         $private_fd = $public_fd;
65 } elsif (defined($private_file)) {
66         open PRIVATE, ">$private_file"; 
67         $private_fd = \*PRIVATE;
68 }
69
70 sub print_header($$)
71 {
72         my ($file, $header_name) = @_;
73         print $file "#ifndef $header_name\n";
74         print $file "#define $header_name\n\n";
75         print $file "/* This file was automatically generated by mkproto.pl. DO NOT EDIT */\n\n";
76 }
77
78 sub print_footer($$) 
79 {
80         my ($file, $header_name) = @_;
81         printf $file "\n#endif /*  %s  */\n", $header_name;
82 }
83
84 sub handle_loadparm($$) 
85 {
86         my ($file,$line) = @_;
87
88         if ($line =~ /^FN_(GLOBAL|LOCAL)_(CONST_STRING|STRING|BOOL|CHAR|INTEGER|LIST)\((\w+),.*\)/o) {
89                 my $scope = $1;
90                 my $type = $2;
91                 my $name = $3;
92
93                 my %tmap = (
94                             "BOOL" => "BOOL ",
95                             "CONST_STRING" => "const char *",
96                             "STRING" => "const char *",
97                             "INTEGER" => "int ",
98                             "CHAR" => "char ",
99                             "LIST" => "const char **",
100                             );
101
102                 my %smap = (
103                             "GLOBAL" => "void",
104                             "LOCAL" => "int "
105                             );
106
107                 print $file "$tmap{$type}$name($smap{$scope});\n";
108         }
109 }
110
111 sub process_file($$$) 
112 {
113         my ($public_file, $private_file, $filename) = @_;
114
115         $filename =~ s/\.o$/\.c/g;
116
117         open(FH, "< $filename") || die "Failed to open $filename";
118
119         print $private_file "\n/* The following definitions come from $filename  */\n\n";
120
121         while (my $line = <FH>) {             
122                 my $target = $private_file;
123
124                 # these are ordered for maximum speed
125                 next if ($line =~ /^\s/);
126               
127                 next unless ($line =~ /\(/);
128
129                 next if ($line =~ /^\/|[;]/);
130
131                 next unless ( $line =~ /
132                               ^void|^BOOL|^int|^struct|^char|^const|^\w+_[tT]\s|^uint|^unsigned|^long|
133                               ^NTSTATUS|^ADS_STATUS|^enum\s.*\(|^DATA_BLOB|^WERROR|^XFILE|^FILE|^DIR|
134                               ^double|^TDB_CONTEXT|^TDB_DATA|^TALLOC_CTX|^NTTIME|^FN_|^init_module|
135                               ^GtkWidget|^GType|^smb_ucs2_t
136                               /xo);
137
138                 next if ($line =~ /^int\s*main/);
139
140                 if ($line =~ /^FN_/) {
141                         handle_loadparm($public_file, $line);
142                         next;
143                 }
144
145                 if ($line =~ s/_PUBLIC_//xo) {
146                         $target = $public_file;
147                 }
148
149                 if ( $line =~ /\(.*\)\s*$/o ) {
150                         chomp $line;
151                         print $target "$line;\n";
152                         next;
153                 }
154
155                 print $target $line;
156
157                 while ($line = <FH>) {
158                         if ($line =~ /\)\s*$/o) {
159                                 chomp $line;
160                                 print $target "$line;\n";
161                                 last;
162                         }
163                         print $target $line;
164                 }
165         }
166
167         close(FH);
168 }
169
170 print_header($public_fd, $public_define);
171 if ($public_file ne $private_file) {
172         print_header($private_fd, $private_define);
173
174         print $private_fd "/* this file contains prototypes for functions that " .
175                         "are private \n * to this subsystem or library. These functions " .
176                         "should not be \n * used outside this particular subsystem! */\n\n";
177
178         print $public_fd "/* this file contains prototypes for functions that " . 
179                         "are part of \n * the public API of this subsystem or library. */\n\n";
180 }
181 process_file($public_fd, $private_fd, $_) foreach (@ARGV);
182 print_footer($public_fd, $public_define);
183 if ($public_file ne $private_file) {
184         print_footer($private_fd, $private_define);
185 }