2 # Simple script for generating prototypes for C functions
3 # Written by Jelmer Vernooij
4 # based on the original mkproto.sh by Andrew Tridgell
8 # don't use warnings module as it is not portable enough
15 #####################################################################
16 # read a file into a string
18 my $public_file = undef;
19 my $private_file = undef;
20 my $public_define = undef;
21 my $private_define = undef;
24 my $public_data = \$_public;
25 my $private_data = \$_private;
43 print "Usage: mkproto.pl [options] [c files]\n";
45 print " --public=FILE Write prototypes for public functions to FILE\n";
46 print " --private=FILE Write prototypes for private functions to FILE\n";
47 print " --define=DEF Use DEF to check whether header was already included\n";
48 print " --public-define=DEF Same as --define, but just for public header\n";
49 print " --private-define=DEF Same as --define, but just for private header\n";
50 print " --srcdir=path Read files relative to this directory\n";
51 print " --builddir=path Write file relative to this directory\n";
52 print " --help Print this help message\n\n";
57 'public=s' => sub { my ($f,$v) = @_; $public_file = $v; },
58 'private=s' => sub { my ($f,$v) = @_; $private_file = $v; },
62 $private_define = "$v\_PRIVATE";
64 'public-define=s' => \$public_define,
65 'private-define=s' => \$private_define,
66 'srcdir=s' => sub { my ($f,$v) = @_; $srcdir = $v; },
67 'builddir=s' => sub { my ($f,$v) = @_; $builddir = $v; },
71 sub normalize_define($$)
73 my ($define, $file) = @_;
75 if (not defined($define) and defined($file)) {
76 $define = "__" . uc($file) . "__";
77 $define =~ tr{./}{__};
79 } elsif (not defined($define)) {
80 $define = '_PROTO_H_';
86 $public_define = normalize_define($public_define, $public_file);
87 $private_define = normalize_define($private_define, $private_file);
89 if ((defined($private_file) and defined($public_file) and ($private_file eq $public_file)) or
90 (not defined($private_file) and not defined($public_file))) {
91 $private_data = $public_data;
96 my($filename) = shift;
98 open(INPUTFILE, $filename) || return undef;
99 my($saved_delim) = $/;
101 my($data) = <INPUTFILE>;
109 my ($file, $header_name) = @_;
110 $file->("#ifndef $header_name\n");
111 $file->("#define $header_name\n\n");
112 $file->("#undef _PRINTF_ATTRIBUTE\n");
113 $file->("#define _PRINTF_ATTRIBUTE(a1, a2) PRINTF_ATTRIBUTE(a1, a2)\n");
114 $file->("/* This file was automatically generated by mkproto.pl. DO NOT EDIT */\n\n");
119 my ($file, $header_name) = @_;
120 $file->("#undef _PRINTF_ATTRIBUTE\n");
121 $file->("#define _PRINTF_ATTRIBUTE(a1, a2)\n");
122 $file->("\n#endif /* $header_name */\n\n");
125 sub handle_loadparm($$)
127 my ($file,$line) = @_;
129 if ($line =~ /^_PUBLIC_ FN_(GLOBAL|LOCAL)_(CONST_STRING|STRING|BOOL|bool|CHAR|INTEGER|LIST)\((\w+),.*\)/o) {
137 "CONST_STRING" => "const char *",
138 "STRING" => "const char *",
141 "LIST" => "const char **",
145 "GLOBAL" => "struct loadparm_context *",
146 "LOCAL" => "struct loadparm_service *"
149 $file->("$tmap{$type}$name($smap{$scope});\n");
153 sub process_file($$$)
155 my ($public_file, $private_file, $filename) = @_;
157 $filename =~ s/\.o$/\.c/g;
159 if (!open(FH, "< $builddir/$filename")) {
160 open(FH, "< $srcdir/$filename") || die "Failed to open $filename";
163 $private_file->("\n/* The following definitions come from $filename */\n\n");
167 while (my $line = <FH>) {
168 my $target = \&private;
171 if ($line =~ /^\/\*\*/) {
178 if ($line =~ /\*\//) {
183 # these are ordered for maximum speed
184 next if ($line =~ /^\s/);
186 next unless ($line =~ /\(/);
188 next if ($line =~ /^\/|[;]/);
190 if ($line =~ /^_PUBLIC_ FN_/) {
191 handle_loadparm($public_file, $line);
195 if ($line =~ /^_PUBLIC_[\t ]/) {
200 next unless ( $is_public || $line =~ /
201 (_DEPRECATED_ |_NORETURN_ |_WARN_UNUSED_RESULT_ |_PURE_ )*^(
202 void|BOOL|bool|int|struct|char|const|\w+_[tT]\s|uint|unsigned|long|NTSTATUS|
203 ADS_STATUS|enum\s.*\(|DATA_BLOB|WERROR|XFILE|FILE|DIR|
204 double|TDB_CONTEXT|TDB_DATA|TALLOC_CTX|NTTIME|FN_|init_module|
205 GtkWidget|GType|smb_ucs2_t|krb5_error_code)
208 next if ($line =~ /^int\s*main/);
210 $target->("\n$comment") if (defined($comment)); $comment = undef;
212 $line =~ s/BOOL /bool /g;
213 if ( $line =~ /\(.*\)\s*$/o ) {
215 $target->("$line;\n");
221 while ($line = <FH>) {
222 $line =~ s/BOOL /bool /g;
223 if ($line =~ /\)\s*$/o) {
225 $target->("$line;\n");
236 print_header(\&public, $public_define);
237 if ($public_file ne $private_file) {
238 print_header(\&private, $private_define);
240 private("/* this file contains prototypes for functions that " .
241 "are private \n * to this subsystem or library. These functions " .
242 "should not be \n * used outside this particular subsystem! */\n\n");
244 public("/* this file contains prototypes for functions that " .
245 "are part of \n * the public API of this subsystem or library. */\n\n");
249 public("#ifndef _PUBLIC_\n#define _PUBLIC_\n#endif\n\n");
250 public("#ifndef _PURE_\n#define _PURE_\n#endif\n\n");
251 public("#ifndef _NORETURN_\n#define _NORETURN_\n#endif\n\n");
252 public("#ifndef _DEPRECATED_\n#define _DEPRECATED_\n#endif\n\n");
253 public("#ifndef _WARN_UNUSED_RESULT_\n#define _WARN_UNUSED_RESULT_\n#endif\n\n");
255 process_file(\&public, \&private, $_) foreach (@ARGV);
256 print_footer(\&public, $public_define);
257 if ($public_file ne $private_file) {
258 print_footer(\&private, $private_define);
261 if (not defined($public_file)) {
262 print STDOUT $$public_data;
265 if (not defined($private_file) and defined($public_file)) {
266 print STDOUT $$private_data;
269 my $old_public_data = file_load($public_file);
270 my $old_private_data = file_load($private_file);
272 mkpath(dirname($public_file), 0, 0755);
273 open(PUBLIC, ">$public_file") or die("Can't open `$public_file': $!");
274 print PUBLIC "$$public_data";
277 if ($public_file ne $private_file) {
278 mkpath(dirname($private_file), 0, 0755);
279 open(PRIVATE, ">$private_file") or die("Can't open `$private_file': $!");
280 print PRIVATE "$$private_data";