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
# Copyright tridge@samba.org 2000
# released under the GNU GPL
use strict ;
use Getopt::Long ;
use Data::Dumper ;
use Parse::RecDescent ;
use dump ;
2000-12-14 07:41:45 +03:00
use header ;
2000-12-15 02:53:19 +03:00
use parser ;
2001-11-25 02:35:58 +03:00
use eparser ;
2000-12-14 07:09:29 +03:00
use util ;
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 ;
2000-12-15 02:53:19 +03:00
my ( $ opt_parser ) = 0 ;
2001-11-25 02:35:58 +03:00
my ( $ opt_eparser ) = 0 ;
2000-12-14 07:09:29 +03:00
#####################################################################
# parse an IDL file returning a structure containing all the data
sub IdlParse ($)
{
# this autoaction allows us to handle simple nodes without an action
# $::RD_TRACE = 1;
$ ::RD_AUTOACTION = q {
$# item == 1 && ref ( $ item [ 1 ] ) eq "" ?
$ item [ 1 ] :
"XX_" . $ item [ 0 ] . "_XX[$#item]" } ;
my ( $ filename ) = shift ;
my ( $ grammer ) = util:: FileLoad ( "idl.gram" ) ;
my ( $ parser ) = Parse::RecDescent - > new ( $ grammer ) ;
2000-12-15 02:53:19 +03:00
my ( $ saved_sep ) = $/ ;
2000-12-14 07:09:29 +03:00
undef $/ ;
my ( $ idl ) = $ parser - > idl ( `cpp $filename` ) ;
2000-12-15 02:53:19 +03:00
$/ = $ saved_sep ;
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
Copyright tridge \ @ samba . org
Usage: pidl . pl [ options ] <idlfile>
Options:
- - help this help page
- - 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
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
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 ,
'parse' = > \ $ opt_parse ,
'dump' = > \ $ opt_dump ,
2000-12-14 07:41:45 +03:00
'header' = > \ $ opt_header ,
2000-12-15 02:53:19 +03:00
'parser' = > \ $ opt_parser ,
2001-11-25 02:35:58 +03:00
'eparser' = > \ $ opt_eparser ,
2000-12-14 07:09:29 +03:00
'diff' = > \ $ opt_diff
) ;
2000-12-14 07:41:45 +03:00
if ( $ opt_help ) {
ShowHelp ( ) ;
exit ( 0 ) ;
}
2000-12-14 07:09:29 +03:00
my ( $ idl_file ) = shift ;
die "ERROR: You must specify an idl file to process" unless ( $ idl_file ) ;
my ( $ pidl_file ) = util:: ChangeExtension ( $ idl_file , "pidl" ) ;
if ( $ opt_parse ) {
2000-12-15 02:53:19 +03:00
print "Generating $pidl_file\n" ;
2000-12-14 07:09:29 +03:00
my ( $ idl ) = IdlParse ( $ idl_file ) ;
util:: SaveStructure ( $ pidl_file , $ idl ) || die "Failed to save $pidl_file" ;
}
if ( $ opt_dump ) {
my ( $ idl ) = util:: LoadStructure ( $ pidl_file ) ;
print IdlDump:: Dump ( $ idl ) ;
}
2000-12-14 07:41:45 +03:00
if ( $ opt_header ) {
my ( $ idl ) = util:: LoadStructure ( $ pidl_file ) ;
my ( $ header ) = util:: ChangeExtension ( $ idl_file , "h" ) ;
2000-12-15 02:53:19 +03:00
print "Generating $header\n" ;
2000-12-14 07:41:45 +03:00
util:: FileSave ( $ header , IdlHeader:: Dump ( $ idl ) ) ;
}
2000-12-15 02:53:19 +03:00
if ( $ opt_parser ) {
my ( $ idl ) = util:: LoadStructure ( $ pidl_file ) ;
my ( $ parser ) = util:: ChangeExtension ( $ idl_file , "c" ) ;
print "Generating $parser\n" ;
2001-11-25 02:35:58 +03:00
util:: FileSave ( $ parser , IdlParser:: Parse ( $ idl ) ) ;
}
if ( $ opt_eparser ) {
my ( $ idl ) = util:: LoadStructure ( $ pidl_file ) ;
my ( $ parser ) = util:: ChangeExtension ( $ idl_file , "c" ) ;
print "Generating $parser for ethereal\n" ;
util:: FileSave ( $ parser , IdlEParser:: Parse ( $ idl ) ) ;
2000-12-15 02:53:19 +03:00
}
2000-12-14 07:09:29 +03:00
if ( $ opt_diff ) {
my ( $ idl ) = util:: LoadStructure ( $ pidl_file ) ;
my ( $ tempfile ) = util:: ChangeExtension ( $ idl_file , "tmp" ) ;
util:: FileSave ( $ tempfile , IdlDump:: Dump ( $ idl ) ) ;
system ( "diff -wu $idl_file $tempfile" ) ;
unlink ( $ tempfile ) ;
}