1 ###################################################
2 # create C header files for an IDL structure
3 # Copyright tridge@samba.org 2000
4 # released under the GNU GPL
16 for (my($i)=0; $i < $tab_depth; $i++) {
21 #####################################################################
22 # parse a properties list
23 sub HeaderProperties($)
29 foreach my $d (@{$props}) {
30 if (ref($d) ne "HASH") {
31 $res .= "/* [$d] */ ";
33 foreach my $k (keys %{$d}) {
34 $res .= "/* [$k($d->{$k})] */ ";
40 #####################################################################
41 # parse a structure element
46 (defined $element->{PROPERTIES}) && HeaderProperties($element->{PROPERTIES});
48 HeaderType($element, $element->{TYPE}, "");
50 if ($element->{POINTERS} &&
51 $element->{TYPE} ne "string") {
52 my($n) = $element->{POINTERS};
53 for (my($i)=$n; $i > 0; $i--) {
57 if (defined $element->{ARRAY_LEN} &&
58 !util::is_constant($element->{ARRAY_LEN}) &&
59 !$element->{POINTERS}) {
60 # conformant arrays are ugly! I choose to implement them with
61 # pointers instead of the [1] method
64 $res .= "$element->{NAME}";
65 if (defined $element->{ARRAY_LEN} && util::is_constant($element->{ARRAY_LEN})) {
66 $res .= "[$element->{ARRAY_LEN}]";
71 #####################################################################
77 $res .= "\nstruct $name {\n";
80 if (defined $struct->{ELEMENTS}) {
81 foreach my $e (@{$struct->{ELEMENTS}}) {
87 # some compilers can't handle empty structures
88 $res .= "\tchar _empty_;\n";
94 #####################################################################
101 util::register_enum($enum, $name);
103 $res .= "\nenum $name {\n";
105 my $els = \@{$enum->{ELEMENTS}};
106 foreach my $i (0 .. $#{$els}-1) {
113 my $e = ${$els}[$#{$els}];
116 if ($e !~ /^(.*?)\s*$/) {
117 die "Bad enum $name\n";
124 #####################################################################
131 util::register_bitmap($bitmap, $name);
133 $res .= "\n/* bitmap $name */\n";
135 my $els = \@{$bitmap->{ELEMENTS}};
136 foreach my $i (0 .. $#{$els}) {
139 $res .= "#define $e\n";
145 #####################################################################
153 (defined $union->{PROPERTIES}) && HeaderProperties($union->{PROPERTIES});
154 $res .= "\nunion $name {\n";
156 foreach my $e (@{$union->{DATA}}) {
157 if ($e->{TYPE} eq "UNION_ELEMENT") {
158 if (! defined $done{$e->{DATA}->{NAME}}) {
159 HeaderElement($e->{DATA});
161 $done{$e->{DATA}->{NAME}} = 1;
168 #####################################################################
175 if (ref($data) eq "HASH") {
176 ($data->{TYPE} eq "ENUM") &&
177 HeaderEnum($data, $name);
178 ($data->{TYPE} eq "BITMAP") &&
179 HeaderBitmap($data, $name);
180 ($data->{TYPE} eq "STRUCT") &&
181 HeaderStruct($data, $name);
182 ($data->{TYPE} eq "UNION") &&
183 HeaderUnion($data, $name);
186 if ($data =~ "string") {
187 $res .= "const char *";
188 } elsif (util::is_enum($e->{TYPE})) {
189 $res .= "enum $data";
190 } elsif (util::is_bitmap($e->{TYPE})) {
191 my $bitmap = util::get_bitmap($e->{TYPE});
192 $res .= util::bitmap_type_decl($bitmap);
193 } elsif (util::is_scalar_type($data)) {
195 } elsif (util::has_property($e, "switch_is")) {
196 $res .= "union $data";
198 $res .= "struct $data";
202 #####################################################################
206 my($typedef) = shift;
207 HeaderType($typedef, $typedef->{DATA}, $typedef->{NAME});
208 $res .= ";\n" unless ($typedef->{DATA}->{TYPE} eq "BITMAP");
211 #####################################################################
212 # prototype a typedef
213 sub HeaderTypedefProto($)
217 if (needed::is_needed("ndr_size_$d->{NAME}")) {
218 $res .= "size_t ndr_size_$d->{NAME}(const struct $d->{NAME} *r, int flags);\n";
221 if (!util::has_property($d, "public")) {
225 if ($d->{DATA}{TYPE} eq "STRUCT") {
226 $res .= "NTSTATUS ndr_push_$d->{NAME}(struct ndr_push *ndr, int ndr_flags, struct $d->{NAME} *r);\n";
227 $res .= "NTSTATUS ndr_pull_$d->{NAME}(struct ndr_pull *ndr, int ndr_flags, struct $d->{NAME} *r);\n";
228 if (!util::has_property($d, "noprint")) {
229 $res .= "void ndr_print_$d->{NAME}(struct ndr_print *ndr, const char *name, struct $d->{NAME} *r);\n";
233 if ($d->{DATA}{TYPE} eq "UNION") {
234 $res .= "NTSTATUS ndr_push_$d->{NAME}(struct ndr_push *ndr, int ndr_flags, int level, union $d->{NAME} *r);\n";
235 $res .= "NTSTATUS ndr_pull_$d->{NAME}(struct ndr_pull *ndr, int ndr_flags, int level, union $d->{NAME} *r);\n";
236 if (!util::has_property($d, "noprint")) {
237 $res .= "void ndr_print_$d->{NAME}(struct ndr_print *ndr, const char *name, int level, union $d->{NAME} *r);\n";
242 #####################################################################
247 if (!defined($const->{ARRAY_LEN})) {
248 $res .= "#define $const->{NAME}\t( $const->{VALUE} )\n";
250 $res .= "#define $const->{NAME}\t $const->{VALUE}\n";
254 #####################################################################
256 sub HeaderFunctionInOut($$)
261 foreach my $e (@{$fn->{DATA}}) {
262 if (util::has_property($e, $prop)) {
268 #####################################################################
269 # determine if we need an "in" or "out" section
270 sub HeaderFunctionInOut_needed($$)
275 if ($prop eq "out" && $fn->{RETURN_TYPE} && $fn->{RETURN_TYPE} ne "void") {
279 foreach my $e (@{$fn->{DATA}}) {
280 if (util::has_property($e, $prop)) {
289 #####################################################################
291 sub HeaderFunction($)
295 $res .= "\nstruct $fn->{NAME} {\n";
299 if (HeaderFunctionInOut_needed($fn, "in")) {
301 $res .= "struct {\n";
303 HeaderFunctionInOut($fn, "in");
310 if (HeaderFunctionInOut_needed($fn, "out")) {
312 $res .= "struct {\n";
314 HeaderFunctionInOut($fn, "out");
315 if ($fn->{RETURN_TYPE} && $fn->{RETURN_TYPE} ne "void") {
317 $res .= "$fn->{RETURN_TYPE} result;\n";
321 $res .= "} out;\n\n";
326 # sigh - some compilers don't like empty structures
328 $res .= "int _dummy_element;\n";
335 #####################################################################
336 # output prototypes for a IDL function
337 sub HeaderFnProto($$)
339 my $interface = shift;
341 my $name = $fn->{NAME};
343 $res .= "void ndr_print_$name(struct ndr_print *ndr, const char *name, int flags, struct $name *r);\n";
345 if (util::has_property($interface, "object")) {
346 $res .= "NTSTATUS dcom_$interface->{NAME}_$name (struct dcom_interface_p *d, TALLOC_CTX *mem_ctx, struct $name *r);\n";
348 $res .= "NTSTATUS dcerpc_$name(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct $name *r);\n";
349 $res .= "struct rpc_request *dcerpc_$name\_send(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct $name *r);\n";
355 #####################################################################
356 # generate vtable structure for DCOM interface
359 my $interface = shift;
360 $res .= "struct dcom_$interface->{NAME}_vtable {\n";
361 if (defined($interface->{BASE})) {
362 $res .= "\tstruct dcom_$interface->{BASE}\_vtable base;\n";
365 my $data = $interface->{DATA};
366 foreach my $d (@{$data}) {
367 $res .= "\tNTSTATUS (*$d->{NAME}) (struct dcom_interface_p *d, TALLOC_CTX *mem_ctx, struct $d->{NAME} *r);\n" if ($d->{TYPE} eq "FUNCTION");
373 #####################################################################
374 # parse the interface definitions
375 sub HeaderInterface($)
377 my($interface) = shift;
378 my($data) = $interface->{DATA};
382 $res .= "#ifndef _HEADER_NDR_$interface->{NAME}\n";
383 $res .= "#define _HEADER_NDR_$interface->{NAME}\n\n";
385 if (defined $interface->{PROPERTIES}->{depends}) {
386 my @d = split / /, $interface->{PROPERTIES}->{depends};
388 $res .= "#include \"librpc/gen_ndr/ndr_$i\.h\"\n";
392 if (defined $interface->{PROPERTIES}->{uuid}) {
393 my $name = uc $interface->{NAME};
394 $res .= "#define DCERPC_$name\_UUID " .
395 util::make_str($interface->{PROPERTIES}->{uuid}) . "\n";
397 if(!defined $interface->{PROPERTIES}->{version}) { $interface->{PROPERTIES}->{version} = "0.0"; }
398 $res .= "#define DCERPC_$name\_VERSION $interface->{PROPERTIES}->{version}\n";
400 $res .= "#define DCERPC_$name\_NAME \"$interface->{NAME}\"\n";
402 if(!defined $interface->{PROPERTIES}->{helpstring}) { $interface->{PROPERTIES}->{helpstring} = "NULL"; }
403 $res .= "#define DCERPC_$name\_HELPSTRING $interface->{PROPERTIES}->{helpstring}\n";
405 $res .= "\nextern const struct dcerpc_interface_table dcerpc_table_$interface->{NAME};\n";
406 $res .= "NTSTATUS dcerpc_server_$interface->{NAME}_init(void);\n\n";
409 foreach my $d (@{$data}) {
410 if ($d->{TYPE} eq "FUNCTION") {
411 my $u_name = uc $d->{NAME};
412 $res .= "#define DCERPC_$u_name (";
414 if (defined($interface->{BASE})) {
415 $res .= "DCERPC_" . uc $interface->{BASE} . "_CALL_COUNT + ";
418 $res .= sprintf("0x%02x", $count) . ")\n";
423 $res .= "\n#define DCERPC_" . uc $interface->{NAME} . "_CALL_COUNT (";
425 if (defined($interface->{BASE})) {
426 $res .= "DCERPC_" . uc $interface->{BASE} . "_CALL_COUNT + ";
429 $res .= "$count)\n\n";
431 foreach my $d (@{$data}) {
432 ($d->{TYPE} eq "CONST") &&
434 ($d->{TYPE} eq "TYPEDEF") &&
436 ($d->{TYPE} eq "TYPEDEF") &&
437 HeaderTypedefProto($d);
438 ($d->{TYPE} eq "FUNCTION") &&
440 ($d->{TYPE} eq "FUNCTION") &&
441 HeaderFnProto($interface, $d);
444 (util::has_property($interface, "object")) &&
445 HeaderVTable($interface);
447 $res .= "#endif /* _HEADER_NDR_$interface->{NAME} */\n";
450 #####################################################################
451 # parse a parsed IDL into a C header
457 $res = "/* header auto-generated by pidl */\n\n";
458 foreach my $x (@{$idl}) {
459 if ($x->{TYPE} eq "INTERFACE") {
460 needed::BuildNeeded($x);