r26256: When generating Samba3 pidl output for WERROR based functions, make sure the
[bbaumbach/samba-autobuild/.git] / source4 / pidl / lib / Parse / Pidl / Samba3 / ClientNDR.pm
1 ###################################################
2 # Samba3 client generator for IDL structures
3 # on top of Samba4 style NDR functions
4 # Copyright jelmer@samba.org 2005-2006
5 # released under the GNU GPL
6
7 package Parse::Pidl::Samba3::ClientNDR;
8
9 use Exporter;
10 @ISA = qw(Exporter);
11 @EXPORT_OK = qw(GenerateFunctionInEnv ParseFunction $res $res_hdr);
12
13 use strict;
14 use Parse::Pidl qw(fatal warning);
15 use Parse::Pidl::Typelist qw(hasType getType mapTypeName scalar_is_reference);
16 use Parse::Pidl::Util qw(has_property is_constant ParseExpr);
17 use Parse::Pidl::NDR qw(GetPrevLevel GetNextLevel ContainsDeferred);
18 use Parse::Pidl::Samba4 qw(DeclLong);
19 use Parse::Pidl::Samba4::NDR::Parser qw(GenerateFunctionInEnv);
20
21 use vars qw($VERSION);
22 $VERSION = '0.01';
23
24 sub indent($) { my ($self) = @_; $self->{tabs}.="\t"; }
25 sub deindent($) { my ($self) = @_; $self->{tabs} = substr($self->{tabs}, 1); }
26 sub pidl($$) { my ($self,$txt) = @_; $self->{res} .= "$self->{tabs}$txt\n"; }
27 sub pidl_hdr($$) { my ($self, $txt) = @_; $self->{res_hdr} .= "$txt\n"; } 
28 sub fn_declare($$) { my ($self,$n) = @_; $self->pidl($n); $self->pidl_hdr("$n;"); }
29
30 sub new($)
31 {
32         my ($class) = shift;
33         my $self = { res => "", res_hdr => "", tabs => "" };
34         bless($self, $class);
35 }
36
37 sub ParseFunction($$$)
38 {
39         my ($self, $if, $fn) = @_;
40
41         my $inargs = "";
42         my $defargs = "";
43         my $uif = uc($if);
44         my $ufn = "NDR_".uc($fn->{NAME});
45
46         foreach (@{$fn->{ELEMENTS}}) {
47                 $defargs .= ", " . DeclLong($_);
48         }
49
50         if ($fn->{RETURN_TYPE} eq "WERROR") {
51                 $defargs .= ", WERROR *werror";
52         }
53
54         $self->fn_declare("NTSTATUS rpccli_$fn->{NAME}(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx$defargs)");
55         $self->pidl("{");
56         $self->indent;
57         $self->pidl("struct $fn->{NAME} r;");
58         $self->pidl("NTSTATUS status;");
59         $self->pidl("");
60         $self->pidl("/* In parameters */");
61
62         foreach (@{$fn->{ELEMENTS}}) {
63                 if (grep(/in/, @{$_->{DIRECTION}})) {
64                         $self->pidl("r.in.$_->{NAME} = $_->{NAME};");
65                 } 
66         }
67
68         $self->pidl("");
69         $self->pidl("if (DEBUGLEVEL >= 10)");
70         $self->pidl("\tNDR_PRINT_IN_DEBUG($fn->{NAME}, &r);");
71         $self->pidl("");
72         $self->pidl("status = cli_do_rpc_ndr(cli, mem_ctx, PI_$uif, &ndr_table_$if, $ufn, &r);");
73         $self->pidl("");
74
75         $self->pidl("if (!NT_STATUS_IS_OK(status)) {");
76         $self->indent;
77         $self->pidl("return status;");
78         $self->deindent;
79         $self->pidl("}");
80
81         $self->pidl("");
82         $self->pidl("if (DEBUGLEVEL >= 10)");
83         $self->pidl("\tNDR_PRINT_OUT_DEBUG($fn->{NAME}, &r);");
84         $self->pidl("");
85         $self->pidl("if (NT_STATUS_IS_ERR(status)) {");
86         $self->pidl("\treturn status;");
87         $self->pidl("}");
88         $self->pidl("");
89         $self->pidl("/* Return variables */");
90         foreach my $e (@{$fn->{ELEMENTS}}) {
91                 next unless (grep(/out/, @{$e->{DIRECTION}}));
92                 my $level = 0;
93
94                 fatal($e->{ORIGINAL}, "[out] argument is not a pointer or array") if ($e->{LEVELS}[0]->{TYPE} ne "POINTER" and $e->{LEVELS}[0]->{TYPE} ne "ARRAY");
95
96                 if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") {
97                         $level = 1;
98                         if ($e->{LEVELS}[0]->{POINTER_TYPE} ne "ref") {
99                                 $self->pidl("if ($e->{NAME} && r.out.$e->{NAME}) {");
100                                 $self->indent;
101                         }
102                 }
103
104                 if ($e->{LEVELS}[$level]->{TYPE} eq "ARRAY") {
105                         # This is a call to GenerateFunctionInEnv intentionally. 
106                         # Since the data is being copied into a user-provided data 
107                         # structure, the user should be able to know the size beforehand 
108                         # to allocate a structure of the right size.
109                         my $env = GenerateFunctionInEnv($fn, "r.");
110                         my $size_is = ParseExpr($e->{LEVELS}[$level]->{SIZE_IS}, $env, $e->{ORIGINAL});
111                         $self->pidl("memcpy($e->{NAME}, r.out.$e->{NAME}, $size_is);");
112                 } else {
113                         $self->pidl("*$e->{NAME} = *r.out.$e->{NAME};");
114                 }
115
116                 if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") {
117                         if ($e->{LEVELS}[0]->{POINTER_TYPE} ne "ref") {
118                                 $self->deindent;
119                                 $self->pidl("}");
120                         }
121                 }
122         }
123
124         $self->pidl("");
125         $self->pidl("/* Return result */");
126         if (not $fn->{RETURN_TYPE}) {
127                 $self->pidl("return NT_STATUS_OK;");
128         } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") {
129                 $self->pidl("return r.out.result;");
130         } elsif ($fn->{RETURN_TYPE} eq "WERROR") {
131                 $self->pidl("if (werror) {");
132                 $self->indent;
133                 $self->pidl("*werror = r.out.result;");
134                 $self->deindent;
135                 $self->pidl("}");
136                 $self->pidl("");
137                 $self->pidl("return werror_to_ntstatus(r.out.result);");
138         } else {
139                 warning($fn->{ORIGINAL}, "Unable to convert $fn->{RETURN_TYPE} to NTSTATUS");
140                 $self->pidl("return NT_STATUS_OK;");
141         }
142
143         $self->deindent;
144         $self->pidl("}");
145         $self->pidl("");
146 }
147
148 sub ParseInterface($$)
149 {
150         my ($self, $if) = @_;
151
152         my $uif = uc($if->{NAME});
153
154         $self->pidl_hdr("#ifndef __CLI_$uif\__");
155         $self->pidl_hdr("#define __CLI_$uif\__");
156         $self->ParseFunction($if->{NAME}, $_) foreach (@{$if->{FUNCTIONS}});
157         $self->pidl_hdr("#endif /* __CLI_$uif\__ */");
158 }
159
160 sub Parse($$$$)
161 {
162         my($self,$ndr,$header,$ndr_header) = @_;
163
164         $self->pidl("/*");
165         $self->pidl(" * Unix SMB/CIFS implementation.");
166         $self->pidl(" * client auto-generated by pidl. DO NOT MODIFY!");
167         $self->pidl(" */");
168         $self->pidl("");
169         $self->pidl("#include \"includes.h\"");
170         $self->pidl("#include \"$header\"");
171         $self->pidl_hdr("#include \"$ndr_header\"");
172         $self->pidl("");
173         
174         foreach (@$ndr) {
175                 $self->ParseInterface($_) if ($_->{TYPE} eq "INTERFACE");
176         }
177
178         return ($self->{res}, $self->{res_hdr});
179 }
180
181 1;