r1611: Put a brace on a new line.
[samba.git] / source / build / pidl / parser.pm
index 5bf5e4220f8d8c03eff884e4e61bb2a561977515..aa05491bb9bd421b91b4873cd6b009f1b9105192 100644 (file)
@@ -7,7 +7,6 @@
 package IdlParser;
 
 use strict;
-use Data::Dumper;
 use client;
 
 # the list of needed functions
@@ -139,7 +138,7 @@ sub start_flags($)
        my $e = shift;
        my $flags = util::has_property($e, "flag");
        if (defined $flags) {
-               pidl "\t{ uint32 _flags_save_$e->{TYPE} = ndr->flags;\n";
+               pidl "\t{ uint32_t _flags_save_$e->{TYPE} = ndr->flags;\n";
                pidl "\tndr->flags |= $flags;\n";
        }
 }
@@ -300,7 +299,7 @@ sub ParseArrayPull($$$)
 
                # non fixed arrays encode the size just before the array
                pidl "\t{\n";
-               pidl "\t\tuint32 _array_size;\n";
+               pidl "\t\tuint32_t _array_size;\n";
                pidl "\t\tNDR_CHECK(ndr_pull_uint32(ndr, &_array_size));\n";
                if ($size =~ /r->in/) {
                        pidl "\t\tif (!(ndr->flags & LIBNDR_FLAG_REF_ALLOC) && _array_size != $size) {\n";
@@ -309,7 +308,7 @@ sub ParseArrayPull($$$)
                }
                pidl "\t\t\treturn ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, \"Bad array size %u should be %u\", _array_size, $size);\n";
                pidl "\t\t}\n";
-               if ($size =~ /r->/) {
+               if ($size =~ /r->in/) {
                        pidl "else { $size = _array_size; }\n";
                }
                pidl "\t}\n";
@@ -318,14 +317,14 @@ sub ParseArrayPull($$$)
        if ((util::need_alloc($e) && !util::is_fixed_array($e)) ||
            ($var_prefix eq "r->in." && util::has_property($e, "ref"))) {
                if (!util::is_inline_array($e) || $ndr_flags eq "NDR_SCALARS") {
-                       pidl "\t\tNDR_ALLOC_N_SIZE(ndr, $var_prefix$e->{NAME}, MAX(1, $alloc_size), sizeof($var_prefix$e->{NAME}\[0]));\n";
+                       pidl "\t\tNDR_ALLOC_N(ndr, $var_prefix$e->{NAME}, MAX(1, $alloc_size));\n";
                }
        }
 
        if (($var_prefix eq "r->out." && util::has_property($e, "ref"))) {
                if (!util::is_inline_array($e) || $ndr_flags eq "NDR_SCALARS") {
                        pidl "\tif (ndr->flags & LIBNDR_FLAG_REF_ALLOC) {";
-                       pidl "\t\tNDR_ALLOC_N_SIZE(ndr, $var_prefix$e->{NAME}, MAX(1, $alloc_size), sizeof($var_prefix$e->{NAME}\[0]));\n";
+                       pidl "\t\tNDR_ALLOC_N(ndr, $var_prefix$e->{NAME}, MAX(1, $alloc_size));\n";
                        pidl "\t}\n";
                }
        }
@@ -334,7 +333,7 @@ sub ParseArrayPull($$$)
 
        if (my $length = util::has_property($e, "length_is")) {
                $length = find_size_var($e, $length, $var_prefix);
-               pidl "\t\tuint32 _offset, _length;\n";
+               pidl "\t\tuint32_t _offset, _length;\n";
                pidl "\t\tNDR_CHECK(ndr_pull_uint32(ndr, &_offset));\n";
                pidl "\t\tNDR_CHECK(ndr_pull_uint32(ndr, &_length));\n";
                pidl "\t\tif (_offset != 0) return ndr_pull_error(ndr, NDR_ERR_OFFSET, \"Bad array offset 0x%08x\", _offset);\n";
@@ -405,7 +404,15 @@ sub ParseElementPrintScalar($$)
                return;
        }
 
-       if (util::has_direct_buffers($e)) {
+       if (my $value = util::has_property($e, "value")) {
+               pidl "\tif (ndr->flags & LIBNDR_PRINT_SET_VALUES) {\n";
+               pidl "\t\t$cprefix$var_prefix$e->{NAME} = $value;\n";
+               pidl "\t}\n";
+       }
+
+       if (util::is_fixed_array($e)) {
+               ParseElementPrintBuffer($e, $var_prefix);
+       } elsif (util::has_direct_buffers($e)) {
                pidl "\tndr_print_ptr(ndr, \"$e->{NAME}\", $var_prefix$e->{NAME});\n";
                pidl "\tndr->depth++;\n";
                ParseElementPrintBuffer($e, $var_prefix);
@@ -441,8 +448,9 @@ sub ParseElementPullSwitch($$$$)
                } else {
                        pidl "\t\tif (_level != $switch_var) {\n";
                }
-               pidl "\t\t\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value %u in $e->{NAME}\");\t\t}\n";
-               if ($switch =~ /r->/) {
+               pidl "\t\t\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value %u in $e->{NAME}\", _level);\n";
+               pidl "\t\t}\n";
+               if ($switch_var =~ /r->/) {
                        pidl "else { $switch_var = _level; }\n";
                }
                pidl "\t}\n";
@@ -517,7 +525,7 @@ sub ParseElementPullScalar($$$)
        } elsif (util::is_inline_array($e)) {
                ParseArrayPull($e, "r->", "NDR_SCALARS");
        } elsif (util::need_wire_pointer($e)) {
-               pidl "\tNDR_CHECK(ndr_pull_uint32(ndr, &_ptr_$e->{NAME}));\n";
+               pidl "\tNDR_CHECK(ndr_pull_ptr(ndr, &_ptr_$e->{NAME}));\n";
                pidl "\tif (_ptr_$e->{NAME}) {\n";
                pidl "\t\tNDR_ALLOC(ndr, $var_prefix$e->{NAME});\n";
                pidl "\t} else {\n";
@@ -740,11 +748,15 @@ sub ParseStructPrint($)
                return;
        }
 
+       start_flags($struct);
+
        pidl "\tndr->depth++;\n";
        foreach my $e (@{$struct->{ELEMENTS}}) {
                ParseElementPrintScalar($e, "r->");
        }
        pidl "\tndr->depth--;\n";
+
+       end_flags($struct);
 }
 
 #####################################################################
@@ -758,8 +770,6 @@ sub ParseStructPull($)
                return;
        }
 
-       start_flags($struct);
-
        # see if the structure contains a conformant array. If it
        # does, then it must be the last element of the structure, and
        # we need to pull the conformant length early, as it fits on
@@ -768,7 +778,7 @@ sub ParseStructPull($)
        my $e = $struct->{ELEMENTS}[-1];
        if (defined $e->{ARRAY_LEN} && $e->{ARRAY_LEN} eq "*") {
                $conform_e = $e;
-               pidl "\tuint32 _conformant_size;\n";
+               pidl "\tuint32_t _conformant_size;\n";
                $conform_e->{CONFORMANT_SIZE} = "_conformant_size";
        }
 
@@ -776,10 +786,12 @@ sub ParseStructPull($)
        foreach my $e (@{$struct->{ELEMENTS}}) {
                if (util::need_wire_pointer($e) &&
                    !util::has_property($e, "relative")) {
-                       pidl "\tuint32 _ptr_$e->{NAME};\n";
+                       pidl "\tuint32_t _ptr_$e->{NAME};\n";
                }
        }
 
+       start_flags($struct);
+
        pidl "\tif (!(ndr_flags & NDR_SCALARS)) goto buffers;\n";
 
        pidl "\tNDR_CHECK(ndr_pull_struct_start(ndr));\n";
@@ -874,6 +886,8 @@ sub ParseUnionPrint($)
        my $e = shift;
        my $have_default = 0;
 
+       start_flags($e);
+
        pidl "\tswitch (level) {\n";
        foreach my $el (@{$e->{DATA}}) {
                if ($el->{CASE} eq "default") {
@@ -891,6 +905,8 @@ sub ParseUnionPrint($)
                pidl "\tdefault:\n\t\tndr_print_bad_level(ndr, name, level);\n";
        }
        pidl "\t}\n";
+
+       end_flags($e);
 }
 
 #####################################################################
@@ -920,7 +936,7 @@ sub ParseUnionPull($)
                if ($el->{TYPE} eq "UNION_ELEMENT") {
                        my $e2 = $el->{DATA};
                        if ($e2->{POINTERS}) {
-                               pidl "\t\tuint32 _ptr_$e2->{NAME};\n";
+                               pidl "\t\tuint32_t _ptr_$e2->{NAME};\n";
                        }
                        ParseElementPullScalar($el->{DATA}, "r->", "NDR_SCALARS");
                }
@@ -1018,7 +1034,7 @@ sub ParseTypedefPush($)
        }
 
        if ($e->{DATA}->{TYPE} eq "UNION") {
-               pidl $static . "NTSTATUS ndr_push_$e->{NAME}(struct ndr_push *ndr, int ndr_flags, uint16 level, union $e->{NAME} *r)";
+               pidl $static . "NTSTATUS ndr_push_$e->{NAME}(struct ndr_push *ndr, int ndr_flags, uint16_t level, union $e->{NAME} *r)";
                pidl "\n{\n";
                ParseTypePush($e->{DATA});
                pidl "\treturn NT_STATUS_OK;\n";
@@ -1048,7 +1064,7 @@ sub ParseTypedefPull($)
        }
 
        if ($e->{DATA}->{TYPE} eq "UNION") {
-               pidl $static . "NTSTATUS ndr_pull_$e->{NAME}(struct ndr_pull *ndr, int ndr_flags, uint16 level, union $e->{NAME} *r)";
+               pidl $static . "NTSTATUS ndr_pull_$e->{NAME}(struct ndr_pull *ndr, int ndr_flags, uint16_t level, union $e->{NAME} *r)";
                pidl "\n{\n";
                ParseTypePull($e->{DATA});
                pidl "\treturn NT_STATUS_OK;\n";
@@ -1072,7 +1088,7 @@ sub ParseTypedefPrint($)
        }
 
        if ($e->{DATA}->{TYPE} eq "UNION") {
-               pidl "void ndr_print_$e->{NAME}(struct ndr_print *ndr, const char *name, uint16 level, union $e->{NAME} *r)";
+               pidl "void ndr_print_$e->{NAME}(struct ndr_print *ndr, const char *name, uint16_t level, union $e->{NAME} *r)";
                pidl "\n{\n";
                pidl "\tndr_print_union(ndr, name, level, \"$e->{NAME}\");\n";
                ParseTypePrint($e->{DATA});
@@ -1090,7 +1106,11 @@ sub ParseFunctionPrint($)
        pidl "\n{\n";
        pidl "\tndr_print_struct(ndr, name, \"$fn->{NAME}\");\n";
        pidl "\tndr->depth++;\n";
-       
+
+       pidl "\tif (flags & NDR_SET_VALUES) {\n";
+       pidl "\t\tndr->flags |= LIBNDR_PRINT_SET_VALUES;\n";
+       pidl "\t}\n";
+
        pidl "\tif (flags & NDR_IN) {\n";
        pidl "\t\tndr_print_struct(ndr, \"in\", \"$fn->{NAME}\");\n";
        pidl "\tndr->depth++;\n";
@@ -1135,10 +1155,12 @@ sub ParseFunctionElementPush($$)
        if (util::array_size($e)) {
                if (util::need_wire_pointer($e)) {
                        pidl "\tNDR_CHECK(ndr_push_ptr(ndr, r->$inout.$e->{NAME}));\n";
+                       pidl "\tif (r->$inout.$e->{NAME}) {\n";
+                       ParseArrayPush($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
+                       pidl "\t}\n";
+               } else {
+                       ParseArrayPush($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
                }
-               pidl "\tif (r->$inout.$e->{NAME}) {\n";
-               ParseArrayPush($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
-               pidl "\t}\n";
        } else {
                ParseElementPushScalar($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
                if ($e->{POINTERS}) {
@@ -1188,7 +1210,7 @@ sub ParseFunctionElementPull($$)
 
        if (util::array_size($e)) {
                if (util::need_wire_pointer($e)) {
-                       pidl "\tNDR_CHECK(ndr_pull_uint32(ndr, &_ptr_$e->{NAME}));\n";
+                       pidl "\tNDR_CHECK(ndr_pull_ptr(ndr, &_ptr_$e->{NAME}));\n";
                        pidl "\tif (_ptr_$e->{NAME}) {\n";
                } elsif ($inout eq "out" && util::has_property($e, "ref")) {
                        pidl "\tif (r->$inout.$e->{NAME}) {\n";
@@ -1214,6 +1236,40 @@ sub ParseFunctionElementPull($$)
        }
 }
 
+
+############################################################
+# allocate ref variables
+sub AllocateRefVars($)
+{
+       my $e = shift;
+       my $asize = util::array_size($e);
+
+       # note that if the variable is also an "in"
+       # variable then we copy the initial value from
+       # the in side
+
+       if (!defined $asize) {
+               # its a simple variable
+               pidl "\tNDR_ALLOC(ndr, r->out.$e->{NAME});\n";
+               if (util::has_property($e, "in")) {
+                       pidl "\t*r->out.$e->{NAME} = *r->in.$e->{NAME};\n";
+               } else {
+                       pidl "\tZERO_STRUCTP(r->out.$e->{NAME});\n";
+               }
+               return;
+       }
+
+       # its an array
+       my $size = find_size_var($e, $asize, "r->out.");
+       pidl "\tNDR_ALLOC_N(ndr, r->out.$e->{NAME}, MAX(1, $size));\n";
+       if (util::has_property($e, "in")) {
+               pidl "\tmemcpy(r->out.$e->{NAME},r->in.$e->{NAME},$size * sizeof(*r->in.$e->{NAME}));\n";
+       } else {
+               pidl "\tmemset(r->out.$e->{NAME}, 0, $size * sizeof(*r->out.$e->{NAME}));\n";
+       }
+}
+
+
 #####################################################################
 # parse a function
 sub ParseFunctionPull($)
@@ -1227,15 +1283,32 @@ sub ParseFunctionPull($)
        # declare any internal pointers we need
        foreach my $e (@{$fn->{DATA}}) {
                if (util::need_wire_pointer($e)) {
-                       pidl "\tuint32 _ptr_$e->{NAME};\n";
+                       pidl "\tuint32_t _ptr_$e->{NAME};\n";
                }
        }
 
        pidl "\n\tif (!(flags & NDR_IN)) goto ndr_out;\n\n";
+
+       # auto-init the out section of a structure. I originally argued that
+       # this was a bad idea as it hides bugs, but coping correctly
+       # with initialisation and not wiping ref vars is turning
+       # out to be too tricky (tridge)
+       foreach my $e (@{$fn->{DATA}}) {
+               if (util::has_property($e, "out")) {
+                       pidl "\tZERO_STRUCT(r->out);\n\n";
+                       last;
+               }
+       }
+
        foreach my $e (@{$fn->{DATA}}) {
                if (util::has_property($e, "in")) {
                        ParseFunctionElementPull($e, "in");
                }
+               # we need to allocate any reference output variables, so that
+               # a dcerpc backend can be sure they are non-null
+               if (util::has_property($e, "out") && util::has_property($e, "ref")) {
+                       AllocateRefVars($e);
+               }
        }
 
        pidl "\nndr_out:\n";
@@ -1291,15 +1364,18 @@ sub FunctionTable($)
                $if_endpoints = $interface->{NAME};
        }
 
-       pidl "static const struct dcerpc_endpoint_list $interface->{NAME}\_endpoints = {\n";
        my @e = split / /, $if_endpoints;
        my $endpoint_count = $#e + 1;
-       pidl "\t$endpoint_count,\n";
-       pidl "\t{ ";
+
+       pidl "static const char * const $interface->{NAME}\_endpoint_strings[] = {\n\t";
        for (my $i=0; $i < $#e; $i++) {
                pidl "\"$e[$i]\", ";
        }
-       pidl "\"$e[$#e]\" }\n";
+       pidl "\"$e[$#e]\"\n";
+       pidl "};\n\n";
+
+       pidl "static const struct dcerpc_endpoint_list $interface->{NAME}\_endpoints = {\n";
+       pidl "\t$endpoint_count, $interface->{NAME}\_endpoint_strings\n";
        pidl "};\n\n";
 
        pidl "\nconst struct dcerpc_interface_table dcerpc_table_$interface->{NAME} = {\n";