5 # don't use warnings module as it is not portable enough
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;
20 print "Usage: mkproto.pl [options] [c files]\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";
32 'public=s' => sub { my ($f,$v) = @_; $public_file = $v; },
33 'private=s' => sub { my ($f,$v) = @_; $private_file = $v; },
37 $private_define = "$v\_PRIVATE";
39 'public-define=s' => \$public_define,
40 'private-define=s' => \$private_define,
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";
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";
67 $private_fd = \*PRIVATE;
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";
80 my ($file, $header_name) = @_;
81 printf $file "\n#endif /* %s */\n", $header_name;
84 sub handle_loadparm($$)
86 my ($file,$line) = @_;
88 if ($line =~ /^FN_(GLOBAL|LOCAL)_(CONST_STRING|STRING|BOOL|CHAR|INTEGER|LIST)\((\w+),.*\)/o) {
95 "CONST_STRING" => "const char *",
96 "STRING" => "const char *",
99 "LIST" => "const char **",
107 print $file "$tmap{$type}$name($smap{$scope});\n";
111 sub process_file($$$)
113 my ($public_file, $private_file, $filename) = @_;
115 $filename =~ s/\.o$/\.c/g;
117 open(FH, "< $filename") || die "Failed to open $filename";
119 print $private_file "\n/* The following definitions come from $filename */\n\n";
121 while (my $line = <FH>) {
122 my $target = $private_file;
124 # these are ordered for maximum speed
125 next if ($line =~ /^\s/);
127 next unless ($line =~ /\(/);
129 next if ($line =~ /^\/|[;]/);
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
138 next if ($line =~ /^int\s*main/);
140 if ($line =~ /^FN_/) {
141 handle_loadparm($public_file, $line);
145 if ($line =~ s/_PUBLIC_//xo) {
146 $target = $public_file;
149 if ( $line =~ /\(.*\)\s*$/o ) {
151 print $target "$line;\n";
157 while ($line = <FH>) {
158 if ($line =~ /\)\s*$/o) {
160 print $target "$line;\n";
170 print_header($public_fd, $public_define);
171 if ($public_file ne $private_file) {
172 print_header($private_fd, $private_define);
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";
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";
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);