first version
[sfrench/samba-autobuild/.git] / source4 / build / pidl / pidl.pl
1 #!/usr/bin/perl -w
2
3 ###################################################
4 # package to parse IDL files and generate code for 
5 # rpc functions in Samba
6 # Copyright tridge@samba.org 2000
7 # released under the GNU GPL
8
9 use strict;
10 use Getopt::Long;
11 use Data::Dumper;
12 use Parse::RecDescent;
13 use dump;
14 use util;
15
16 my($opt_help) = 0;
17 my($opt_parse) = 0;
18 my($opt_dump) = 0;
19 my($opt_diff) = 0;
20
21 #####################################################################
22 # parse an IDL file returning a structure containing all the data
23 sub IdlParse($)
24 {
25     # this autoaction allows us to handle simple nodes without an action
26 #    $::RD_TRACE = 1;
27     $::RD_AUTOACTION = q { 
28                           $#item==1 && ref($item[1]) eq "" ? 
29                           $item[1] : 
30                           "XX_" . $item[0] . "_XX[$#item]"  };
31     my($filename) = shift;
32     my($grammer) = util::FileLoad("idl.gram");    
33     my($parser) = Parse::RecDescent->new($grammer);
34     undef $/;
35     my($idl) = $parser->idl(`cpp $filename`);
36     util::CleanData($idl);
37     return $idl;
38 }
39
40
41 #########################################
42 # display help text
43 sub ShowHelp()
44 {
45     print "
46            perl IDL parser and code generator
47            Copyright tridge\@samba.org
48
49            Usage: pidl.pl [options] <idlfile>
50
51            Options:
52              --help                this help page
53              --parse               parse a idl file to a .pidl file
54              --dump                dump a pidl file back to idl
55              --diff                run diff on the idl and dumped output
56            ";
57     exit(0);
58 }
59
60 # main program
61 GetOptions (
62             'help|h|?' => \$opt_help, 
63             'parse' => \$opt_parse,
64             'dump' => \$opt_dump,
65             'diff' => \$opt_diff
66             );
67
68 my($idl_file) = shift;
69 die "ERROR: You must specify an idl file to process" unless ($idl_file);
70
71 my($pidl_file) = util::ChangeExtension($idl_file, "pidl");
72
73 if ($opt_help) {
74     ShowHelp();
75 }
76
77 if ($opt_parse) {
78     print "Parsing $idl_file\n";
79     my($idl) = IdlParse($idl_file);
80     print "Saving $pidl_file\n";
81     util::SaveStructure($pidl_file, $idl) || die "Failed to save $pidl_file";
82 }
83
84 if ($opt_dump) {
85     my($idl) = util::LoadStructure($pidl_file);
86     print IdlDump::Dump($idl);
87 }
88
89 if ($opt_diff) {
90     my($idl) = util::LoadStructure($pidl_file);
91     my($tempfile) = util::ChangeExtension($idl_file, "tmp");
92     util::FileSave($tempfile, IdlDump::Dump($idl));
93     system("diff -wu $idl_file $tempfile");
94     unlink($tempfile);
95 }