2000-12-14 04:09:29 +00:00
#!/usr/bin/perl -w
###################################################
2004-05-09 07:10:12 +00:00
# package to parse IDL files and generate code for
2000-12-14 04:09:29 +00:00
# rpc functions in Samba
2003-11-28 00:48:05 +00:00
# Copyright tridge@samba.org 2000-2003
2000-12-14 04:09:29 +00:00
# released under the GNU GPL
use strict ;
2003-11-06 12:29:23 +00:00
2003-11-20 05:39:54 +00:00
use FindBin qw( $RealBin ) ;
use lib "$RealBin" ;
2003-11-22 01:40:24 +00:00
use lib "$RealBin/lib" ;
2000-12-14 04:09:29 +00:00
use Getopt::Long ;
2003-11-23 03:42:20 +00:00
use File::Basename ;
2003-11-20 04:36:09 +00:00
use idl ;
2000-12-14 04:09:29 +00:00
use dump ;
2000-12-14 04:41:45 +00:00
use header ;
2003-12-14 13:22:12 +00:00
use server ;
2004-11-12 00:48:24 +00:00
use stub ;
2000-12-14 23:53:19 +00:00
use parser ;
2001-11-24 23:35:58 +00:00
use eparser ;
2003-11-22 12:25:20 +00:00
use validator ;
2000-12-14 04:09:29 +00:00
use util ;
2003-12-15 10:55:10 +00:00
use template ;
2004-09-08 11:54:01 +00:00
use swig ;
2000-12-14 04:09:29 +00:00
my ( $ opt_help ) = 0 ;
my ( $ opt_parse ) = 0 ;
my ( $ opt_dump ) = 0 ;
my ( $ opt_diff ) = 0 ;
2000-12-14 04:41:45 +00:00
my ( $ opt_header ) = 0 ;
2003-12-15 10:55:10 +00:00
my ( $ opt_template ) = 0 ;
2003-12-14 13:22:12 +00:00
my ( $ opt_server ) = 0 ;
2000-12-14 23:53:19 +00:00
my ( $ opt_parser ) = 0 ;
2001-11-24 23:35:58 +00:00
my ( $ opt_eparser ) = 0 ;
2003-11-08 13:24:14 +00:00
my ( $ opt_keep ) = 0 ;
2004-09-08 11:54:01 +00:00
my ( $ opt_swig ) = 0 ;
2003-11-08 13:24:14 +00:00
my ( $ opt_output ) ;
2000-12-14 04:09:29 +00:00
2003-12-07 13:38:07 +00:00
my $ idl_parser = new idl ;
2000-12-14 04:09:29 +00:00
#####################################################################
# parse an IDL file returning a structure containing all the data
sub IdlParse ($)
{
2003-12-07 13:38:07 +00:00
my $ filename = shift ;
my $ idl = $ idl_parser - > parse_idl ( $ filename ) ;
2000-12-14 04:09:29 +00:00
util:: CleanData ( $ idl ) ;
return $ idl ;
}
#########################################
# display help text
sub ShowHelp ()
{
print "
perl IDL parser and code generator
2003-11-09 08:28:47 +00:00
Copyright ( C ) tridge \ @ samba . org
2000-12-14 04:09:29 +00:00
Usage: pidl . pl [ options ] <idlfile>
Options:
- - help this help page
2003-11-08 13:24:14 +00:00
- - output OUTNAME put output in OUTNAME . *
2000-12-14 04:09:29 +00:00
- - parse parse a idl file to a . pidl file
- - dump dump a pidl file back to idl
2000-12-14 23:53:19 +00:00
- - header create a C header file
- - parser create a C parser
2004-05-31 17:14:27 +00:00
- - server create server boilerplate
2003-12-15 10:55:10 +00:00
- - template print a template for a pipe
2001-11-24 23:35:58 +00:00
- - eparser create an ethereal parser
2004-11-01 21:10:46 +00:00
- - swig create swig wrapper file
2000-12-14 04:09:29 +00:00
- - diff run diff on the idl and dumped output
2003-11-08 13:24:14 +00:00
- - keep keep the . pidl file
2000-12-14 04:41:45 +00:00
\ n " ;
2000-12-14 04:09:29 +00:00
exit ( 0 ) ;
}
# main program
GetOptions (
'help|h|?' = > \ $ opt_help ,
2003-11-08 13:24:14 +00:00
'output=s' = > \ $ opt_output ,
2000-12-14 04:09:29 +00:00
'parse' = > \ $ opt_parse ,
'dump' = > \ $ opt_dump ,
2000-12-14 04:41:45 +00:00
'header' = > \ $ opt_header ,
2003-12-14 13:22:12 +00:00
'server' = > \ $ opt_server ,
2003-12-15 10:55:10 +00:00
'template' = > \ $ opt_template ,
2000-12-14 23:53:19 +00:00
'parser' = > \ $ opt_parser ,
2001-11-24 23:35:58 +00:00
'eparser' = > \ $ opt_eparser ,
2003-11-08 13:24:14 +00:00
'diff' = > \ $ opt_diff ,
2004-09-08 11:54:01 +00:00
'keep' = > \ $ opt_keep ,
'swig' = > \ $ opt_swig
2000-12-14 04:09:29 +00:00
) ;
2000-12-14 04:41:45 +00:00
if ( $ opt_help ) {
ShowHelp ( ) ;
exit ( 0 ) ;
}
2003-11-23 03:42:20 +00:00
sub process_file ($)
{
my $ idl_file = shift ;
my $ output ;
2003-12-07 23:47:35 +00:00
my $ pidl ;
2003-11-23 03:42:20 +00:00
my $ basename = basename ( $ idl_file , ".idl" ) ;
if ( ! defined ( $ opt_output ) ) {
$ output = $ idl_file ;
} else {
$ output = $ opt_output . $ basename ;
}
2003-12-14 13:22:12 +00:00
my ( $ pidl_file ) = util:: ChangeExtension ( $ output , ".pidl" ) ;
2003-11-23 03:42:20 +00:00
2003-12-07 13:39:45 +00:00
print "Compiling $idl_file\n" ;
2003-11-23 03:42:20 +00:00
if ( $ opt_parse ) {
2003-12-07 23:47:35 +00:00
$ pidl = IdlParse ( $ idl_file ) ;
2004-10-02 06:32:53 +00:00
defined @$ pidl || die "Failed to parse $idl_file" ;
2004-09-28 01:11:40 +00:00
IdlValidator:: Validate ( $ pidl ) ;
2003-12-07 23:47:35 +00:00
if ( $ opt_keep && ! util:: SaveStructure ( $ pidl_file , $ pidl ) ) {
die "Failed to save $pidl_file\n" ;
2004-05-09 07:10:12 +00:00
}
2003-12-07 23:47:35 +00:00
} else {
$ pidl = util:: LoadStructure ( $ pidl_file ) ;
2004-04-26 02:05:48 +00:00
defined $ pidl || die "Failed to load $pidl_file - maybe you need --parse\n" ;
2003-11-23 03:42:20 +00:00
}
2004-04-26 02:05:48 +00:00
2003-11-23 03:42:20 +00:00
if ( $ opt_dump ) {
2003-12-07 23:47:35 +00:00
print IdlDump:: Dump ( $ pidl ) ;
2003-11-23 03:42:20 +00:00
}
2004-05-09 07:10:12 +00:00
2003-11-23 03:42:20 +00:00
if ( $ opt_header ) {
2003-12-14 13:22:12 +00:00
my ( $ header ) = util:: ChangeExtension ( $ output , ".h" ) ;
2003-12-07 23:47:35 +00:00
util:: FileSave ( $ header , IdlHeader:: Parse ( $ pidl ) ) ;
2003-11-23 03:42:20 +00:00
}
2003-12-14 13:22:12 +00:00
if ( $ opt_server ) {
my ( $ server ) = util:: ChangeExtension ( $ output , "_s.c" ) ;
2004-11-12 00:48:24 +00:00
my $ res = "" ;
foreach my $ x ( @ { $ pidl } ) {
next if ( $ x - > { TYPE } ne "INTERFACE" ) ;
if ( util:: has_property ( $ x , "object" ) ) {
$ res . = IdlStub:: ParseInterface ( $ x ) ;
} else {
$ res . = IdlServer:: ParseInterface ( $ x ) ;
}
}
util:: FileSave ( $ server , $ res ) ;
2003-12-14 13:22:12 +00:00
}
2004-05-09 07:10:12 +00:00
2003-11-23 03:42:20 +00:00
if ( $ opt_parser ) {
2003-12-14 13:22:12 +00:00
my ( $ parser ) = util:: ChangeExtension ( $ output , ".c" ) ;
2003-12-07 23:47:35 +00:00
IdlParser:: Parse ( $ pidl , $ parser ) ;
2003-11-23 03:42:20 +00:00
}
2004-05-09 07:10:12 +00:00
2003-11-23 03:42:20 +00:00
if ( $ opt_eparser ) {
2004-05-09 10:03:55 +00:00
my ( $ parser ) = dirname ( $ output ) . "/packet-dcerpc-$basename.c" ;
2004-05-30 12:02:57 +00:00
IdlEParser:: Parse ( $ pidl , $ parser ) ;
2004-06-08 12:33:47 +00:00
$ parser = dirname ( $ output ) . "/packet-dcerpc-proto-$basename.h" ;
IdlEParser:: ParseHeader ( $ pidl , $ parser ) ;
my ( $ header ) = dirname ( $ output ) . "/packet-dcerpc-proto.h" ;
open ( OUT , ">>$header" ) || die "can't open $header" ;
2004-09-04 01:53:20 +00:00
print OUT "#include \"ndr_$basename.h\"\n" ;
2004-06-08 12:33:47 +00:00
print OUT "#include \"packet-dcerpc-proto-$basename.h\"\n" ;
close ( OUT ) ;
2003-11-23 03:42:20 +00:00
}
2004-05-09 07:10:12 +00:00
2004-09-08 11:54:01 +00:00
if ( $ opt_swig ) {
my ( $ filename ) = $ output ;
$ filename =~ s/\/ndr_/\// ;
$ filename = util:: ChangeExtension ( $ filename , ".i" ) ;
util:: FileSave ( $ filename , IdlSwig:: Parse ( $ pidl ) ) ;
}
2003-11-23 03:42:20 +00:00
if ( $ opt_diff ) {
2003-12-14 13:22:12 +00:00
my ( $ tempfile ) = util:: ChangeExtension ( $ output , ".tmp" ) ;
2003-12-07 23:47:35 +00:00
util:: FileSave ( $ tempfile , IdlDump:: Dump ( $ pidl ) ) ;
2003-11-23 03:42:20 +00:00
system ( "diff -wu $idl_file $tempfile" ) ;
unlink ( $ tempfile ) ;
}
2003-12-15 10:55:10 +00:00
if ( $ opt_template ) {
print IdlTemplate:: Parse ( $ pidl ) ;
}
2003-11-09 08:28:47 +00:00
}
2003-11-08 13:24:14 +00:00
2003-11-23 03:42:20 +00:00
foreach my $ filename ( @ ARGV ) {
process_file ( $ filename ) ;
2003-11-08 13:24:14 +00:00
}