r1611: Put a brace on a new line.
[samba.git] / source / build / pidl / parser.pm
index b616a85f055b2298e80ac8e46b98b1fdae8eef2b..aa05491bb9bd421b91b4873cd6b009f1b9105192 100644 (file)
@@ -448,7 +448,8 @@ 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";
+               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";
                }
@@ -524,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";
@@ -1108,7 +1109,7 @@ sub ParseFunctionPrint($)
 
        pidl "\tif (flags & NDR_SET_VALUES) {\n";
        pidl "\t\tndr->flags |= LIBNDR_PRINT_SET_VALUES;\n";
-       pidl "}\n";
+       pidl "\t}\n";
 
        pidl "\tif (flags & NDR_IN) {\n";
        pidl "\t\tndr_print_struct(ndr, \"in\", \"$fn->{NAME}\");\n";
@@ -1209,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";
@@ -1235,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($)
@@ -1253,6 +1288,18 @@ sub ParseFunctionPull($)
        }
 
        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");
@@ -1260,13 +1307,7 @@ sub ParseFunctionPull($)
                # 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")) {
-                       my $asize = util::array_size($e);
-                       if (defined $asize) {
-                               my $size = find_size_var($e, $asize, "r->out.");
-                               pidl "\tNDR_ALLOC_N(ndr, r->out.$e->{NAME}, MAX(1, $size));\n";
-                       } else {
-                               pidl "\tNDR_ALLOC(ndr, r->out.$e->{NAME});\n";
-                       }
+                       AllocateRefVars($e);
                }
        }