r26696: Some more minor pidl samba3 client cosmetics.
[samba.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} .= $txt ? "$self->{tabs}$txt\n" : "\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 (defined($fn->{RETURN_TYPE}) && ($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->indent;
71         $self->pidl("NDR_PRINT_IN_DEBUG($fn->{NAME}, &r);");
72         $self->deindent;
73         $self->pidl("}");
74         $self->pidl("");
75         $self->pidl("status = cli_do_rpc_ndr(cli, mem_ctx, PI_$uif, &ndr_table_$if, $ufn, &r);");
76         $self->pidl("");
77
78         $self->pidl("if (!NT_STATUS_IS_OK(status)) {");
79         $self->indent;
80         $self->pidl("return status;");
81         $self->deindent;
82         $self->pidl("}");
83
84         $self->pidl("");
85         $self->pidl("if (DEBUGLEVEL >= 10) {");
86         $self->indent;
87         $self->pidl("NDR_PRINT_OUT_DEBUG($fn->{NAME}, &r);");
88         $self->deindent;
89         $self->pidl("}");
90         $self->pidl("");
91         $self->pidl("if (NT_STATUS_IS_ERR(status)) {");
92         $self->indent;
93         $self->pidl("return status;");
94         $self->deindent;
95         $self->pidl("}");
96         $self->pidl("");
97         $self->pidl("/* Return variables */");
98         foreach my $e (@{$fn->{ELEMENTS}}) {
99                 next unless (grep(/out/, @{$e->{DIRECTION}}));
100                 my $level = 0;
101
102                 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");
103
104                 if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") {
105                         $level = 1;
106                         if ($e->{LEVELS}[0]->{POINTER_TYPE} ne "ref") {
107                                 $self->pidl("if ($e->{NAME} && r.out.$e->{NAME}) {");
108                                 $self->indent;
109                         }
110                 }
111
112                 if ($e->{LEVELS}[$level]->{TYPE} eq "ARRAY") {
113                         # This is a call to GenerateFunctionInEnv intentionally. 
114                         # Since the data is being copied into a user-provided data 
115                         # structure, the user should be able to know the size beforehand 
116                         # to allocate a structure of the right size.
117                         my $env = GenerateFunctionInEnv($fn, "r.");
118                         my $size_is = ParseExpr($e->{LEVELS}[$level]->{SIZE_IS}, $env, $e->{ORIGINAL});
119                         $self->pidl("memcpy($e->{NAME}, r.out.$e->{NAME}, $size_is);");
120                 } else {
121                         $self->pidl("*$e->{NAME} = *r.out.$e->{NAME};");
122                 }
123
124                 if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") {
125                         if ($e->{LEVELS}[0]->{POINTER_TYPE} ne "ref") {
126                                 $self->deindent;
127                                 $self->pidl("}");
128                         }
129                 }
130         }
131
132         $self->pidl("");
133         $self->pidl("/* Return result */");
134         if (not $fn->{RETURN_TYPE}) {
135                 $self->pidl("return NT_STATUS_OK;");
136         } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") {
137                 $self->pidl("return r.out.result;");
138         } elsif ($fn->{RETURN_TYPE} eq "WERROR") {
139                 $self->pidl("if (werror) {");
140                 $self->indent;
141                 $self->pidl("*werror = r.out.result;");
142                 $self->deindent;
143                 $self->pidl("}");
144                 $self->pidl("");
145                 $self->pidl("return werror_to_ntstatus(r.out.result);");
146         } else {
147                 warning($fn->{ORIGINAL}, "Unable to convert $fn->{RETURN_TYPE} to NTSTATUS");
148                 $self->pidl("return NT_STATUS_OK;");
149         }
150
151         $self->deindent;
152         $self->pidl("}");
153         $self->pidl("");
154 }
155
156 sub ParseInterface($$)
157 {
158         my ($self, $if) = @_;
159
160         my $uif = uc($if->{NAME});
161
162         $self->pidl_hdr("#ifndef __CLI_$uif\__");
163         $self->pidl_hdr("#define __CLI_$uif\__");
164         $self->ParseFunction($if->{NAME}, $_) foreach (@{$if->{FUNCTIONS}});
165         $self->pidl_hdr("#endif /* __CLI_$uif\__ */");
166 }
167
168 sub Parse($$$$)
169 {
170         my($self,$ndr,$header,$ndr_header) = @_;
171
172         $self->pidl("/*");
173         $self->pidl(" * Unix SMB/CIFS implementation.");
174         $self->pidl(" * client auto-generated by pidl. DO NOT MODIFY!");
175         $self->pidl(" */");
176         $self->pidl("");
177         $self->pidl("#include \"includes.h\"");
178         $self->pidl("#include \"$header\"");
179         $self->pidl_hdr("#include \"$ndr_header\"");
180         $self->pidl("");
181         
182         foreach (@$ndr) {
183                 $self->ParseInterface($_) if ($_->{TYPE} eq "INTERFACE");
184         }
185
186         return ($self->{res}, $self->{res_hdr});
187 }
188
189 1;