- include includes.h
[ira/wip.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
11 my($PIDLBASE) = "$ENV{HOME}/pidl";
12 use lib "$ENV{HOME}/pidl";
13
14 use Getopt::Long;
15 use Data::Dumper;
16 use Parse::RecDescent;
17 use dump;
18 use header;
19 use parser;
20 use eparser;
21 use util;
22
23 my($opt_help) = 0;
24 my($opt_parse) = 0;
25 my($opt_dump) = 0;
26 my($opt_diff) = 0;
27 my($opt_header) = 0;
28 my($opt_parser) = 0;
29 my($opt_eparser) = 0;
30 my($opt_keep) = 0;
31 my($opt_output);
32
33 #####################################################################
34 # parse an IDL file returning a structure containing all the data
35 sub IdlParse($)
36 {
37     # this autoaction allows us to handle simple nodes without an action
38 #    $::RD_TRACE = 1;
39     $::RD_AUTOACTION = q { 
40                           $#item==1 && ref($item[1]) eq "" ? 
41                           $item[1] : 
42                           "XX_" . $item[0] . "_XX[$#item]"  };
43     my($filename) = shift;
44     my($grammer) = util::FileLoad("$PIDLBASE/idl.gram");
45     my($parser) = Parse::RecDescent->new($grammer);
46     my($saved_sep) = $/;
47
48     undef $/;
49     my($idl) = $parser->idl(`cpp $filename`);
50     $/ = $saved_sep;
51     util::CleanData($idl);
52     return $idl;
53 }
54
55
56 #########################################
57 # display help text
58 sub ShowHelp()
59 {
60     print "
61            perl IDL parser and code generator
62            Copyright tridge\@samba.org
63
64            Usage: pidl.pl [options] <idlfile>
65
66            Options:
67              --help                this help page
68              --output OUTNAME      put output in OUTNAME.*
69              --parse               parse a idl file to a .pidl file
70              --dump                dump a pidl file back to idl
71              --header              create a C header file
72              --parser              create a C parser
73              --eparser             create an ethereal parser
74              --diff                run diff on the idl and dumped output
75              --keep                keep the .pidl file
76            \n";
77     exit(0);
78 }
79
80 # main program
81 GetOptions (
82             'help|h|?' => \$opt_help, 
83             'output=s' => \$opt_output,
84             'parse' => \$opt_parse,
85             'dump' => \$opt_dump,
86             'header' => \$opt_header,
87             'parser' => \$opt_parser,
88             'eparser' => \$opt_eparser,
89             'diff' => \$opt_diff,
90             'keep' => \$opt_keep
91             );
92
93 if ($opt_help) {
94     ShowHelp();
95     exit(0);
96 }
97
98 my($idl_file) = shift;
99 die "ERROR: You must specify an idl file to process" unless ($idl_file);
100
101 if (!defined($opt_output)) {
102         $opt_output = $idl_file;
103 }
104
105 my($pidl_file) = util::ChangeExtension($opt_output, "pidl");
106
107 if ($opt_parse) {
108     print "Generating $pidl_file from $idl_file\n";
109     my($idl) = IdlParse($idl_file);
110     util::SaveStructure($pidl_file, $idl) || die "Failed to save $pidl_file";
111 }
112
113 if ($opt_dump) {
114     my($idl) = util::LoadStructure($pidl_file);
115     print IdlDump::Dump($idl);
116 }
117
118 if ($opt_header) {
119     my($idl) = util::LoadStructure($pidl_file);
120     my($header) = util::ChangeExtension($opt_output, "h");
121     print "Generating $header\n";
122     util::FileSave($header, IdlHeader::Dump($idl));
123 }
124
125 if ($opt_parser) {
126     my($idl) = util::LoadStructure($pidl_file);
127     my($parser) = util::ChangeExtension($opt_output, "c");
128     print "Generating $parser\n";
129     util::FileSave($parser, IdlParser::Parse($idl));
130 }
131
132 if ($opt_eparser) {
133     my($idl) = util::LoadStructure($pidl_file);
134     my($parser) = util::ChangeExtension($opt_output, "c");
135     print "Generating $parser for ethereal\n";
136     util::FileSave($parser, IdlEParser::Parse($idl));
137 }
138
139 if ($opt_diff) {
140     my($idl) = util::LoadStructure($pidl_file);
141     my($tempfile) = util::ChangeExtension($opt_output, "tmp");
142     util::FileSave($tempfile, IdlDump::Dump($idl));
143     system("diff -wu $idl_file $tempfile");
144     unlink($tempfile);
145 }
146
147 if (!$opt_keep) {
148         system("rm -f $pidl_file");
149 }