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