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;
21 my $public_define = undef;
22 my $private_define = undef;
25 my $public_data = \
$_public;
26 my $private_data = \
$_private;
44 print "Usage: mkproto.pl [options] [c files]\n";
46 print " --public=FILE Write prototypes for public functions to FILE\n";
47 print " --private=FILE Write prototypes for private functions to FILE\n";
48 print " --define=DEF Use DEF to check whether header was already included\n";
49 print " --public-define=DEF Same as --define, but just for public header\n";
50 print " --private-define=DEF Same as --define, but just for private header\n";
51 print " --srcdir=path Read files relative to this directory\n";
52 print " --builddir=path Write file relative to this directory\n";
53 print " --help Print this help message\n\n";
58 'public=s' => sub { my ($f,$v) = @_; $public_file = $v; },
59 'all=s' => sub { my ($f,$v) = @_; $public_file = $v; $private_file = $v; },
60 'private=s' => sub { my ($f,$v) = @_; $private_file = $v; },
64 $private_define = "$v\_PRIVATE";
66 'public-define=s' => \
$public_define,
67 'private-define=s' => \
$private_define,
68 'srcdir=s' => sub { my ($f,$v) = @_; $srcdir = $v; },
69 'builddir=s' => sub { my ($f,$v) = @_; $builddir = $v; },
73 sub normalize_define
($$)
75 my ($define, $file) = @_;
77 if (not defined($define) and defined($file)) {
78 $define = "__" . uc($file) . "__";
79 $define =~ tr{./}{__};
81 } elsif (not defined($define)) {
82 $define = '_PROTO_H_';
88 $public_define = normalize_define
($public_define, $public_file);
89 $private_define = normalize_define
($private_define, $private_file);
91 if ((defined($private_file) and defined($public_file) and ($private_file eq $public_file)) or
92 (not defined($private_file) and not defined($public_file))) {
93 $private_data = $public_data;
100 open(INPUTFILE
, $filename) or return undef;
101 my($saved_delim) = $/;
103 my($data) = <INPUTFILE
>;
111 my ($file, $header_name) = @_;
112 $file->("#ifndef $header_name\n");
113 $file->("#define $header_name\n\n");
114 $file->("#undef _PRINTF_ATTRIBUTE\n");
115 $file->("#define _PRINTF_ATTRIBUTE(a1, a2) PRINTF_ATTRIBUTE(a1, a2)\n");
116 $file->("/* This file was automatically generated by mkproto.pl. DO NOT EDIT */\n\n");
121 my ($file, $header_name) = @_;
122 $file->("#undef _PRINTF_ATTRIBUTE\n");
123 $file->("#define _PRINTF_ATTRIBUTE(a1, a2)\n");
124 $file->("\n#endif /* $header_name */\n\n");
127 sub handle_loadparm
($$)
129 my ($file,$line) = @_;
135 if ($line =~ /^FN_(GLOBAL|LOCAL)_(CONST_STRING|STRING|BOOL|bool|CHAR|INTEGER|LIST)\((\w+),(.*)\)/o) {
140 } elsif ($line =~ /^FN_(GLOBAL|LOCAL)_PARM_(CONST_STRING|STRING|BOOL|bool|CHAR|INTEGER|LIST)\((\w+),(.*)\)/o) {
151 "CONST_STRING" => "const char *",
152 "STRING" => "const char *",
155 "LIST" => "const char **",
159 "GLOBAL" => "struct loadparm_context *",
160 "LOCAL" => "struct loadparm_service *, struct loadparm_service *"
163 $file->("$tmap{$type}lpcfg_$name($smap{$scope});\n");
166 sub process_file
($$$)
168 my ($public_file, $private_file, $filename) = @_;
170 $filename =~ s/\.o$/\.c/g;
172 if ($filename =~ /^\//) {
173 open(FH
, "<$filename") or die("Failed to open $filename");
174 } elsif (!open(FH
, "< $builddir/$filename")) {
175 open(FH
, "< $srcdir/$filename") || die "Failed to open $filename";
178 $private_file->("\n/* The following definitions come from $filename */\n\n");
182 while (my $line = <FH
>) {
183 my $target = \
&private
;
186 if ($line =~ /^\/\
*\
*/) {
193 if ($line =~ /\*\//) {
198 # these are ordered for maximum speed
199 next if ($line =~ /^\s/);
201 next unless ($line =~ /\(/);
203 next if ($line =~ /^\/|[;]/);
205 if ($line =~ /^FN_/) {
206 handle_loadparm
($public_file, $line);
207 handle_loadparm
($private_file, $line);
211 if ($line =~ /^_PUBLIC_[\t ]/) {
216 next unless ( $is_public || $line =~ /
217 ^(_DEPRECATED_
|_NORETURN_
|_WARN_UNUSED_RESULT_
|_PURE_
)*(
218 void
|bool
|int|struct
|char
|const
|\w
+_
[tT
]\s
|uint
|unsigned
|long
|NTSTATUS
|
219 ADS_STATUS
|enum\s
.*\
(|DATA_BLOB
|WERROR
|XFILE
|FILE
|DIR
|
220 double
|TDB_CONTEXT
|TDB_DATA
|TALLOC_CTX
|NTTIME
|FN_
|init_module
|
221 GtkWidget
|GType
|smb_ucs2_t
|krb5_error_code
|NET_API_STATUS
)
224 next if ($line =~ /^int\s*main/);
226 $target->("\n$comment") if (defined($comment)); $comment = undef;
228 if ( $line =~ /\(.*\)\s*$/o ) {
230 $target->("$line;\n");
236 while ($line = <FH
>) {
237 if ($line =~ /\)\s*$/o) {
239 $target->("$line;\n");
250 print_header
(\
&public
, $public_define);
251 if (defined($private_file) and defined($public_file) and $public_file ne $private_file) {
252 print_header
(\
&private
, $private_define);
254 private
("/* this file contains prototypes for functions that " .
255 "are private \n * to this subsystem or library. These functions " .
256 "should not be \n * used outside this particular subsystem! */\n\n");
258 public
("/* this file contains prototypes for functions that " .
259 "are part of \n * the public API of this subsystem or library. */\n\n");
263 public
("#ifndef _PUBLIC_\n#define _PUBLIC_\n#endif\n\n");
264 public
("#ifndef _PURE_\n#define _PURE_\n#endif\n\n");
265 public
("#ifndef _NORETURN_\n#define _NORETURN_\n#endif\n\n");
266 public
("#ifndef _DEPRECATED_\n#define _DEPRECATED_\n#endif\n\n");
267 public
("#ifndef _WARN_UNUSED_RESULT_\n#define _WARN_UNUSED_RESULT_\n#endif\n\n");
269 process_file
(\
&public
, \
&private
, $_) foreach (@ARGV);
270 print_footer
(\
&public
, $public_define);
271 if (defined($private_file) and $public_file ne $private_file) {
272 print_footer
(\
&private
, $private_define);
275 if (not defined($public_file)) {
276 print STDOUT
$$public_data;
279 if (not defined($private_file) and defined($public_file)) {
280 print STDOUT
$$private_data;
283 mkpath
(dirname
($public_file), 0, 0755);
284 open(PUBLIC
, ">$public_file") or die("Can't open `$public_file': $!");
285 print PUBLIC
"$$public_data";
288 if (defined($private_file) and $public_file ne $private_file) {
289 mkpath
(dirname
($private_file), 0, 0755);
290 open(PRIVATE
, ">$private_file") or die("Can't open `$private_file': $!");
291 print PRIVATE
"$$private_data";