* support multiple interfaces in one IDL file in pidl
[samba.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-2003
7 # released under the GNU GPL
8
9 use strict;
10
11 use FindBin qw($RealBin);
12 use lib "$RealBin";
13 use lib "$RealBin/lib";
14 use Getopt::Long;
15 use File::Basename;
16 use idl;
17 use dump;
18 use header;
19 use parser;
20 use eparser;
21 use validator;
22 use util;
23
24 my($opt_help) = 0;
25 my($opt_parse) = 0;
26 my($opt_dump) = 0;
27 my($opt_diff) = 0;
28 my($opt_header) = 0;
29 my($opt_parser) = 0;
30 my($opt_eparser) = 0;
31 my($opt_keep) = 0;
32 my($opt_output);
33
34 #####################################################################
35 # parse an IDL file returning a structure containing all the data
36 sub IdlParse($)
37 {
38     # this autoaction allows us to handle simple nodes without an action
39 #    $::RD_TRACE = 1;
40     $::RD_AUTOACTION = q { 
41                           $#item==1 && ref($item[1]) eq "" ? 
42                           $item[1] : 
43                           "XX_" . $item[0] . "_XX[$#item]"  };
44     my($filename) = shift;
45     my($parser) = idl->new;
46     my($saved_sep) = $/;
47
48     undef $/;
49     my($idl) = $parser->idl(`cpp $filename | grep -v '^#'`);
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 (C) 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 sub process_file($)
99 {
100         my $idl_file = shift;
101         my $output;
102
103         my $basename = basename($idl_file, ".idl");
104
105         if (!defined($opt_output)) {
106                 $output = $idl_file;
107         } else {
108                 $output = $opt_output . $basename;
109         }
110
111         my($pidl_file) = util::ChangeExtension($output, "pidl");
112
113         if ($opt_parse) {
114                 print "Generating $pidl_file from $idl_file\n";
115                 my($idl) = IdlParse($idl_file);
116                 defined $idl || die "Failed to parse $idl_file";
117                 util::SaveStructure($pidl_file, $idl) || die "Failed to save $pidl_file";
118                 
119                 IdlValidator::Validate($idl);
120         }
121         
122         if ($opt_dump) {
123                 my($idl) = util::LoadStructure($pidl_file);
124                 print IdlDump::Dump($idl);
125         }
126         
127         if ($opt_header) {
128                 my($idl) = util::LoadStructure($pidl_file);
129                 my($header) = util::ChangeExtension($output, "h");
130                 print "Generating $header\n";
131                 util::FileSave($header, IdlHeader::Parse($idl));
132         }
133         
134         if ($opt_parser) {
135                 my($idl) = util::LoadStructure($pidl_file);
136                 my($parser) = util::ChangeExtension($output, "c");
137                 print "Generating $parser\n";
138                 IdlParser::Parse($idl, $parser);
139         }
140         
141         if ($opt_eparser) {
142                 my($idl) = util::LoadStructure($pidl_file);
143                 my($parser) = util::ChangeExtension($output, "c");
144                 print "Generating $parser for ethereal\n";
145                 util::FileSave($parser, IdlEParser::Parse($idl));
146         }
147         
148         if ($opt_diff) {
149                 my($idl) = util::LoadStructure($pidl_file);
150                 my($tempfile) = util::ChangeExtension($output, "tmp");
151                 util::FileSave($tempfile, IdlDump::Dump($idl));
152                 system("diff -wu $idl_file $tempfile");
153                 unlink($tempfile);
154         }
155         
156         if (!$opt_keep) {
157                 system("rm -f $pidl_file");
158         }
159 }
160
161
162 foreach my $filename (@ARGV) {
163         process_file($filename);
164 }