1 ###################################################
2 # create C header files for an IDL structure
3 # Copyright tridge@samba.org 2000
4 # released under the GNU GPL
22 for (my($i)=0; $i < $tab_depth; $i++) {
27 #####################################################################
28 # parse a properties list
29 sub HeaderProperties($$)
37 foreach my $d (keys %{$props}) {
38 next if ($ignores->{$d});
39 if($props->{$d} ne "1") {
40 $ret.= "$d(" . $props->{$d} . "),";
47 pidl "/* [" . substr($ret, 0, -1) . "] */";
51 #####################################################################
52 # parse a structure element
57 if (defined $element->{PROPERTIES}) {
58 HeaderProperties($element->{PROPERTIES}, {"in" => 1, "out" => 1});
61 HeaderType($element, $element->{TYPE}, "");
63 if ($element->{POINTERS} && $element->{TYPE} ne "string") {
64 my($n) = $element->{POINTERS};
65 for (my($i)=$n; $i > 0; $i--) {
69 if (defined $element->{ARRAY_LEN} &&
70 !util::is_constant($element->{ARRAY_LEN}) &&
71 !$element->{POINTERS}) {
72 # conformant arrays are ugly! I choose to implement them with
73 # pointers instead of the [1] method
76 pidl "$element->{NAME}";
77 if (defined $element->{ARRAY_LEN} && util::is_constant($element->{ARRAY_LEN})) {
78 pidl "[$element->{ARRAY_LEN}]";
83 #####################################################################
89 pidl "\nstruct $name {\n";
92 if (defined $struct->{ELEMENTS}) {
93 foreach my $e (@{$struct->{ELEMENTS}}) {
99 # some compilers can't handle empty structures
100 pidl "\tchar _empty_;\n";
106 #####################################################################
113 pidl "\nenum $name {\n";
115 my $els = \@{$enum->{ELEMENTS}};
116 foreach my $i (0 .. $#{$els}-1) {
123 my $e = ${$els}[$#{$els}];
126 if ($e !~ /^(.*?)\s*$/) {
127 die "Bad enum $name\n";
134 #####################################################################
141 pidl "\n/* bitmap $name */\n";
143 my $els = \@{$bitmap->{ELEMENTS}};
144 foreach my $i (0 .. $#{$els}) {
153 #####################################################################
161 if (defined $union->{PROPERTIES}) {
162 HeaderProperties($union->{PROPERTIES}, {});
164 pidl "\nunion $name {\n";
166 foreach my $e (@{$union->{ELEMENTS}}) {
167 if ($e->{TYPE} ne "EMPTY") {
168 if (! defined $done{$e->{NAME}}) {
171 $done{$e->{NAME}} = 1;
178 #####################################################################
185 if (ref($data) eq "HASH") {
186 ($data->{TYPE} eq "ENUM") &&
187 HeaderEnum($data, $name);
188 ($data->{TYPE} eq "BITMAP") &&
189 HeaderBitmap($data, $name);
190 ($data->{TYPE} eq "STRUCT") &&
191 HeaderStruct($data, $name);
192 ($data->{TYPE} eq "UNION") &&
193 HeaderUnion($data, $name);
197 pidl typelist::mapType($e);
200 #####################################################################
204 my($typedef) = shift;
205 HeaderType($typedef, $typedef->{DATA}, $typedef->{NAME});
206 pidl ";\n" unless ($typedef->{DATA}->{TYPE} eq "BITMAP");
209 #####################################################################
210 # prototype a typedef
211 sub HeaderTypedefProto($)
215 if (needed::is_needed("ndr_size_$d->{NAME}")) {
216 if ($d->{DATA}{TYPE} eq "STRUCT") {
217 pidl "size_t ndr_size_$d->{NAME}(const struct $d->{NAME} *r, int flags);\n";
219 if ($d->{DATA}{TYPE} eq "UNION") {
220 pidl "size_t ndr_size_$d->{NAME}(const union $d->{NAME} *r, uint32_t level, int flags);\n";
224 return unless util::has_property($d, "public");
226 my $tf = NdrParser::get_typefamily($d->{DATA}{TYPE});
228 my $pull_args = $tf->{PULL_FN_ARGS}->($d);
229 my $push_args = $tf->{PUSH_FN_ARGS}->($d);
230 my $print_args = $tf->{PRINT_FN_ARGS}->($d);
231 pidl "NTSTATUS ndr_push_$d->{NAME}($push_args);\n";
232 pidl "NTSTATUS ndr_pull_$d->{NAME}($pull_args);\n";
233 if (!util::has_property($d, "noprint")) {
234 pidl "void ndr_print_$d->{NAME}($print_args);\n";
238 #####################################################################
243 if (!defined($const->{ARRAY_LEN})) {
244 pidl "#define $const->{NAME}\t( $const->{VALUE} )\n";
246 pidl "#define $const->{NAME}\t $const->{VALUE}\n";
250 #####################################################################
252 sub HeaderFunctionInOut($$)
257 foreach my $e (@{$fn->{ELEMENTS}}) {
258 if (util::has_property($e, $prop)) {
264 #####################################################################
265 # determine if we need an "in" or "out" section
266 sub HeaderFunctionInOut_needed($$)
271 if ($prop eq "out" && $fn->{RETURN_TYPE} && $fn->{RETURN_TYPE} ne "void") {
275 foreach my $e (@{$fn->{ELEMENTS}}) {
276 if (util::has_property($e, $prop)) {
285 #####################################################################
287 sub HeaderFunction($)
291 pidl "\nstruct $fn->{NAME} {\n";
295 if (HeaderFunctionInOut_needed($fn, "in")) {
299 HeaderFunctionInOut($fn, "in");
306 if (HeaderFunctionInOut_needed($fn, "out")) {
310 HeaderFunctionInOut($fn, "out");
311 if ($fn->{RETURN_TYPE} && $fn->{RETURN_TYPE} ne "void") {
313 pidl typelist::mapScalarType($fn->{RETURN_TYPE}) . " result;\n";
322 # sigh - some compilers don't like empty structures
324 pidl "int _dummy_element;\n";
331 #####################################################################
332 # output prototypes for a IDL function
333 sub HeaderFnProto($$)
335 my $interface = shift;
337 my $name = $fn->{NAME};
339 pidl "void ndr_print_$name(struct ndr_print *ndr, const char *name, int flags, struct $name *r);\n";
341 if (util::has_property($interface, "object")) {
342 pidl "NTSTATUS dcom_$interface->{NAME}_$name (struct dcom_interface_p *d, TALLOC_CTX *mem_ctx, struct $name *r);\n";
344 pidl "NTSTATUS dcerpc_$name(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct $name *r);\n";
345 pidl "struct rpc_request *dcerpc_$name\_send(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct $name *r);\n";
351 #####################################################################
352 # generate vtable structure for DCOM interface
355 my $interface = shift;
356 pidl "struct dcom_$interface->{NAME}_vtable {\n";
357 if (defined($interface->{BASE})) {
358 pidl "\tstruct dcom_$interface->{BASE}\_vtable base;\n";
361 my $data = $interface->{DATA};
362 foreach my $d (@{$data}) {
363 pidl "\tNTSTATUS (*$d->{NAME}) (struct dcom_interface_p *d, TALLOC_CTX *mem_ctx, struct $d->{NAME} *r);\n" if ($d->{TYPE} eq "FUNCTION");
369 #####################################################################
370 # parse the interface definitions
371 sub HeaderInterface($)
373 my($interface) = shift;
374 my($data) = $interface->{DATA};
378 pidl "#ifndef _HEADER_NDR_$interface->{NAME}\n";
379 pidl "#define _HEADER_NDR_$interface->{NAME}\n\n";
381 if (defined $interface->{PROPERTIES}->{depends}) {
382 my @d = split / /, $interface->{PROPERTIES}->{depends};
384 pidl "#include \"librpc/gen_ndr/ndr_$i\.h\"\n";
388 # Object interfaces use ORPC
389 if (util::has_property($interface, "object")) {
390 pidl "#include \"librpc/gen_ndr/ndr_orpc.h\"\n";
393 if (defined $interface->{PROPERTIES}->{uuid}) {
394 my $name = uc $interface->{NAME};
395 pidl "#define DCERPC_$name\_UUID " .
396 util::make_str($interface->{PROPERTIES}->{uuid}) . "\n";
398 if(!defined $interface->{PROPERTIES}->{version}) { $interface->{PROPERTIES}->{version} = "0.0"; }
399 pidl "#define DCERPC_$name\_VERSION $interface->{PROPERTIES}->{version}\n";
401 pidl "#define DCERPC_$name\_NAME \"$interface->{NAME}\"\n";
403 if(!defined $interface->{PROPERTIES}->{helpstring}) { $interface->{PROPERTIES}->{helpstring} = "NULL"; }
404 pidl "#define DCERPC_$name\_HELPSTRING $interface->{PROPERTIES}->{helpstring}\n";
406 pidl "\nextern const struct dcerpc_interface_table dcerpc_table_$interface->{NAME};\n";
407 pidl "NTSTATUS dcerpc_server_$interface->{NAME}_init(void);\n\n";
410 foreach my $d (@{$data}) {
411 if ($d->{TYPE} eq "FUNCTION") {
412 my $u_name = uc $d->{NAME};
413 pidl "#define DCERPC_$u_name (";
415 if (defined($interface->{BASE})) {
416 pidl "DCERPC_" . uc $interface->{BASE} . "_CALL_COUNT + ";
419 pidl sprintf("0x%02x", $count) . ")\n";
424 pidl "\n#define DCERPC_" . uc $interface->{NAME} . "_CALL_COUNT (";
426 if (defined($interface->{BASE})) {
427 pidl "DCERPC_" . uc $interface->{BASE} . "_CALL_COUNT + ";
432 foreach my $d (@{$data}) {
433 ($d->{TYPE} eq "CONST") &&
435 ($d->{TYPE} eq "TYPEDEF") &&
437 ($d->{TYPE} eq "TYPEDEF") &&
438 HeaderTypedefProto($d);
439 ($d->{TYPE} eq "FUNCTION") &&
441 ($d->{TYPE} eq "FUNCTION") &&
442 HeaderFnProto($interface, $d);
445 (util::has_property($interface, "object")) &&
446 HeaderVTable($interface);
448 pidl "#endif /* _HEADER_NDR_$interface->{NAME} */\n";
451 #####################################################################
452 # parse a parsed IDL into a C header
458 NdrParser::Load($idl);
461 pidl "/* header auto-generated by pidl */\n\n";
462 foreach my $x (@{$idl}) {
463 if ($x->{TYPE} eq "INTERFACE") {
464 needed::BuildNeeded($x);