2003-11-23 04:29:42 +03:00
#!/usr/bin/perl
use strict ;
2003-12-16 06:34:23 +03:00
# don't use warnings module as it is not portable enough
# use warnings;
2003-11-23 04:29:42 +03:00
2005-12-23 15:29:13 +03:00
use Getopt::Long ;
my $ public_file = undef ;
my $ private_file = undef ;
my $ public_define = undef ;
my $ private_define = undef ;
my $ public_fd = \ * STDOUT ;
my $ private_fd = \ * STDOUT ;
GetOptions (
'public=s' = > sub { my ( $ f , $ v ) = @ _ ; $ public_file = $ v ; } ,
'private=s' = > sub { my ( $ f , $ v ) = @ _ ; $ private_file = $ v ; } ,
'define=s' = > sub {
my ( $ f , $ v ) = @ _ ;
$ public_define = $ v ;
$ private_define = "$v\_PRIVATE" ;
} ,
'public-define=s' = > \ $ public_define ,
'private-define=s' = > \ $ private_define
) ;
if ( $ public_define eq undef and $ public_file ne undef ) {
$ public_define = $ public_file ;
$ public_define =~ tr { . / } { __ } ;
} elsif ( $ public_define eq undef ) {
$ public_define = '_PROTO_H_' ;
2003-11-23 04:29:42 +03:00
}
2005-12-23 15:29:13 +03:00
if ( $ private_define eq undef and $ private_file ne undef ) {
$ private_define = $ private_file ;
$ private_define =~ tr { . / } { __ } ;
} elsif ( $ public_define eq undef ) {
$ public_define = '_PROTO_H_' ;
2003-11-23 04:29:42 +03:00
}
2005-12-23 15:29:13 +03:00
if ( $ public_file ne undef ) {
open PUBLIC , ">$public_file" ;
$ public_fd = \ * PUBLIC ;
2003-11-23 04:29:42 +03:00
}
2005-12-23 15:29:13 +03:00
if ( $ private_file eq $ public_file ) {
$ private_fd = $ public_fd ;
} elsif ( $ private_file ne undef ) {
open PRIVATE , ">$private_file" ;
$ private_fd = \ * PRIVATE ;
}
sub print_header ($$)
{
my ( $ file , $ header_name ) = @ _ ;
print $ file "#ifndef $header_name\n" ;
print $ file "#define $header_name\n\n" ;
print $ file "/* This file is automatically generated with \"make proto\". DO NOT EDIT */\n\n" ;
}
2003-11-23 04:53:54 +03:00
2005-12-23 15:29:13 +03:00
sub print_footer ($$)
{
my ( $ file , $ header_name ) = @ _ ;
printf $ file "\n#endif /* %s */\n" , $ header_name ;
}
sub handle_loadparm ($$)
{
my ( $ file , $ line ) = @ _ ;
2003-11-23 05:00:40 +03:00
2003-11-23 05:33:46 +03:00
if ( $ line =~ /^FN_(GLOBAL|LOCAL)_(CONST_STRING|STRING|BOOL|CHAR|INTEGER|LIST)\((\w+),.*\)/o ) {
2003-11-23 05:11:55 +03:00
my $ scope = $ 1 ;
my $ type = $ 2 ;
my $ name = $ 3 ;
my % tmap = (
"BOOL" = > "BOOL " ,
"CONST_STRING" = > "const char *" ,
2004-06-14 03:50:55 +04:00
"STRING" = > "const char *" ,
2003-11-23 05:11:55 +03:00
"INTEGER" = > "int " ,
"CHAR" = > "char " ,
"LIST" = > "const char **" ,
) ;
my % smap = (
"GLOBAL" = > "void" ,
"LOCAL" = > "int "
) ;
2005-12-23 15:29:13 +03:00
print $ file "$tmap{$type}$name($smap{$scope});\n" ;
2003-11-23 04:53:54 +03:00
}
}
2005-12-23 15:29:13 +03:00
sub process_file ($$$)
2003-11-23 05:33:46 +03:00
{
2005-12-23 15:29:13 +03:00
my ( $ public_file , $ private_file , $ filename ) = @ _ ;
$ filename =~ s/\.o$/\.c/g ;
2003-11-23 04:29:42 +03:00
2003-11-23 05:33:46 +03:00
open ( FH , "< $filename" ) || die "Failed to open $filename" ;
2003-11-23 04:29:42 +03:00
2005-12-23 15:29:13 +03:00
print $ private_file "\n/* The following definitions come from $filename */\n\n" ;
2003-11-23 04:29:42 +03:00
2003-11-23 05:44:23 +03:00
while ( my $ line = <FH> ) {
2005-12-23 15:29:13 +03:00
my $ target = $ private_file ;
2003-11-23 05:44:23 +03:00
# these are ordered for maximum speed
2003-11-23 05:33:46 +03:00
next if ( $ line =~ /^\s/ ) ;
2003-11-23 05:44:23 +03:00
next unless ( $ line =~ /\(/ ) ;
next if ( $ line =~ /^\/|[;]/ ) ;
next unless ( $ line =~ /
^ void | ^ BOOL | ^ int | ^ struct | ^ char | ^ const | ^ \ w + _ [ tT ] \ s | ^ uint | ^ unsigned | ^ long |
^ NTSTATUS | ^ ADS_STATUS | ^ enum \ s . * \ ( | ^ DATA_BLOB | ^ WERROR | ^ XFILE | ^ FILE | ^ DIR |
2004-08-19 11:58:02 +04:00
^ double | ^ TDB_CONTEXT | ^ TDB_DATA | ^ TALLOC_CTX | ^ NTTIME | ^ FN_ | ^ REG_KEY | ^ REG_HANDLE | ^ REG_VAL |
2004-09-18 12:18:44 +04:00
^ GtkWidget | ^ GType | ^ smb_ucs2_t
2003-11-23 05:44:23 +03:00
/ xo ) ;
2003-11-23 05:33:46 +03:00
2004-09-24 09:51:29 +04:00
next if ( $ line =~ /^int\s*main/ ) ;
2003-11-23 06:03:27 +03:00
if ( $ line =~ /^FN_/ ) {
2005-12-23 15:29:13 +03:00
handle_loadparm ( $ public_file , $ line ) ;
2003-11-23 06:03:27 +03:00
next ;
}
2005-12-23 15:29:13 +03:00
if ( $ line =~ s/_PUBLIC_//xo ) {
$ target = $ public_file ;
}
2003-11-23 05:33:46 +03:00
if ( $ line =~ /\(.*\)\s*$/o ) {
chomp $ line ;
2005-12-23 15:29:13 +03:00
print $ target "$line;\n" ;
2003-11-23 05:33:46 +03:00
next ;
}
2003-11-23 04:29:42 +03:00
2005-12-23 15:29:13 +03:00
print $ target $ line ;
2003-11-23 04:29:42 +03:00
2003-11-23 05:33:46 +03:00
while ( $ line = <FH> ) {
if ( $ line =~ /\)\s*$/o ) {
2003-11-23 06:03:27 +03:00
chomp $ line ;
2005-12-23 15:29:13 +03:00
print $ target "$line;\n" ;
2003-11-23 05:33:46 +03:00
last ;
2003-11-23 04:29:42 +03:00
}
2005-12-23 15:29:13 +03:00
print $ target $ line ;
2003-11-23 04:29:42 +03:00
}
}
2003-11-23 05:44:23 +03:00
close ( FH ) ;
2003-11-23 04:29:42 +03:00
}
2005-12-23 15:29:13 +03:00
if ( $ public_file != $ private_file ) {
print_header ( $ private_fd , $ private_define ) ;
}
print_header ( $ public_fd , $ public_define ) ;
process_file ( $ public_fd , $ private_fd , $ _ ) foreach ( @ ARGV ) ;
print_footer ( $ public_fd , $ public_define ) ;
if ( $ public_file != $ private_file ) {
print_footer ( $ private_fd , $ private_define ) ;
2003-11-23 05:33:46 +03:00
}