pidl: fix generated s3-server in,out,ref qualifiers that are used for buffer allocation.
[vlendec/samba-autobuild/.git] / pidl / lib / Parse / Pidl / Samba3 / ServerNDR.pm
1 ###################################################
2 # Samba3 server 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::ServerNDR;
8
9 use Exporter;
10 @ISA = qw(Exporter);
11 @EXPORT_OK = qw(DeclLevel);
12
13 use strict;
14 use Parse::Pidl qw(warning fatal);
15 use Parse::Pidl::Typelist qw(mapTypeName scalar_is_reference);
16 use Parse::Pidl::Util qw(ParseExpr has_property is_constant);
17 use Parse::Pidl::NDR qw(GetNextLevel);
18 use Parse::Pidl::Samba4 qw(ElementStars DeclLong);
19 use Parse::Pidl::Samba4::Header qw(GenerateFunctionOutEnv);
20
21 use vars qw($VERSION);
22 $VERSION = '0.01';
23
24 my $res;
25 my $res_hdr;
26 my $tabs = "";
27 sub indent() { $tabs.="\t"; }
28 sub deindent() { $tabs = substr($tabs, 1); }
29 sub pidl($) { my ($txt) = @_; $res .= $txt?$tabs.(shift)."\n":"\n"; }
30 sub pidl_hdr($) { $res_hdr .= (shift)."\n"; }
31 sub fn_declare($) { my ($n) = @_; pidl $n; pidl_hdr "$n;"; }
32
33 sub DeclLevel($$)
34 {
35         my ($e, $l) = @_;
36         my $res = "";
37
38         if (has_property($e, "charset")) {
39                 $res .= "const char";
40         } else {
41                 $res .= mapTypeName($e->{TYPE});
42         }
43
44         my $stars = ElementStars($e, $l);
45
46         $res .= " ".$stars unless ($stars eq "");
47
48         return $res;
49 }
50
51 sub AllocOutVar($$$$)
52 {
53         my ($e, $mem_ctx, $name, $env) = @_;
54
55         my $l = $e->{LEVELS}[0];
56
57         # we skip pointer to arrays
58         if ($l->{TYPE} eq "POINTER") {
59                 my $nl = GetNextLevel($e, $l);
60                 $l = $nl if ($nl->{TYPE} eq "ARRAY");
61         }
62
63         # we don't support multi-dimentional arrays yet
64         if ($l->{TYPE} eq "ARRAY") {
65                 my $nl = GetNextLevel($e, $l);
66                 if ($nl->{TYPE} eq "ARRAY") {
67                         fatal($e->{ORIGINAL},"multi-dimentional [out] arrays are not supported!");
68                 }
69         }
70
71         if ($l->{TYPE} eq "ARRAY") {
72                 my $size = ParseExpr($l->{SIZE_IS}, $env, $e);
73                 pidl "$name = talloc_zero_array($mem_ctx, " . DeclLevel($e, 1) . ", $size);";
74         } else {
75                 pidl "$name = talloc_zero($mem_ctx, " . DeclLevel($e, 1) . ");";
76         }
77
78         pidl "if ($name == NULL) {";
79         pidl "\ttalloc_free($mem_ctx);";
80         pidl "\treturn false;";
81         pidl "}";
82         pidl "";
83 }
84
85 sub ParseFunction($$)
86 {
87         my ($if,$fn) = @_;
88
89         my $op = "NDR_".uc($fn->{NAME});
90
91         pidl "static bool api_$fn->{NAME}(pipes_struct *p)";
92         pidl "{";
93         indent;
94         pidl "const struct ndr_interface_call *call;";
95         pidl "struct ndr_pull *pull;";
96         pidl "struct ndr_push *push;";
97         pidl "enum ndr_err_code ndr_err;";
98         pidl "DATA_BLOB blob;";
99         pidl "struct $fn->{NAME} *r;";
100         pidl "";
101         pidl "call = &ndr_table_$if->{NAME}.calls[$op];";
102         pidl "";
103         pidl "r = talloc(talloc_tos(), struct $fn->{NAME});";
104         pidl "if (r == NULL) {";
105         pidl "\treturn false;";
106         pidl "}";
107         pidl "";
108         pidl "if (!prs_data_blob(&p->in_data.data, &blob, r)) {";
109         pidl "\ttalloc_free(r);";
110         pidl "\treturn false;";
111         pidl "}";
112         pidl "";
113         pidl "pull = ndr_pull_init_blob(&blob, r, NULL);";
114         pidl "if (pull == NULL) {";
115         pidl "\ttalloc_free(r);";
116         pidl "\treturn false;";
117         pidl "}";
118         pidl "";
119         pidl "pull->flags |= LIBNDR_FLAG_REF_ALLOC;";
120         pidl "ndr_err = call->ndr_pull(pull, NDR_IN, r);";
121         pidl "if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {";
122         pidl "\ttalloc_free(r);";
123         pidl "\treturn false;";
124         pidl "}";
125         pidl "";
126         pidl "if (DEBUGLEVEL >= 10) {";
127         pidl "\tNDR_PRINT_IN_DEBUG($fn->{NAME}, r);";
128         pidl "}";
129         pidl "";
130
131         my $env = GenerateFunctionOutEnv($fn);
132         my $hasout = 0;
133         foreach (@{$fn->{ELEMENTS}}) {
134                 if (grep(/out/, @{$_->{DIRECTION}})) { $hasout = 1; }
135         }
136
137         pidl "ZERO_STRUCT(r->out);" if ($hasout);
138
139         my $proto = "_$fn->{NAME}(pipes_struct *p, struct $fn->{NAME} *r";
140         my $ret = "_$fn->{NAME}(p, r";
141         foreach (@{$fn->{ELEMENTS}}) {
142                 my @dir = @{$_->{DIRECTION}};
143                 if (grep(/in/, @dir) and grep(/out/, @dir)) {
144                         pidl "r->out.$_->{NAME} = r->in.$_->{NAME};";
145                 }
146         }
147
148         foreach (@{$fn->{ELEMENTS}}) {
149                 my @dir = @{$_->{DIRECTION}};
150                 if (grep(/in/, @dir) and grep(/out/, @dir)) {
151                         # noop
152                 } elsif (grep(/out/, @dir) and not
153                                  has_property($_, "represent_as")) {
154                         AllocOutVar($_, "r", "r->out.$_->{NAME}", $env);
155                 }
156         }
157         $ret .= ")";
158         $proto .= ");";
159
160         if ($fn->{RETURN_TYPE}) {
161                 $ret = "r->out.result = $ret";
162                 $proto = "$fn->{RETURN_TYPE} $proto";
163         } else {
164                 $proto = "void $proto";
165         }
166
167         pidl_hdr "$proto";
168         pidl "$ret;";
169
170         pidl "";
171         pidl "if (p->rng_fault_state) {";
172         pidl "\ttalloc_free(r);";
173         pidl "\t/* Return true here, srv_pipe_hnd.c will take care */";
174         pidl "\treturn true;";
175         pidl "}";
176         pidl "";
177         pidl "if (DEBUGLEVEL >= 10) {";
178         pidl "\tNDR_PRINT_OUT_DEBUG($fn->{NAME}, r);";
179         pidl "}";
180         pidl "";
181         pidl "push = ndr_push_init_ctx(r, NULL);";
182         pidl "if (push == NULL) {";
183         pidl "\ttalloc_free(r);";
184         pidl "\treturn false;";
185         pidl "}";
186         pidl "";
187         pidl "ndr_err = call->ndr_push(push, NDR_OUT, r);";
188         pidl "if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {";
189         pidl "\ttalloc_free(r);";
190         pidl "\treturn false;";
191         pidl "}";
192         pidl "";
193         pidl "blob = ndr_push_blob(push);";
194         pidl "if (!prs_copy_data_in(&p->out_data.rdata, (const char *)blob.data, (uint32_t)blob.length)) {";
195         pidl "\ttalloc_free(r);";
196         pidl "\treturn false;";
197         pidl "}";
198         pidl "";
199         pidl "talloc_free(r);";
200         pidl "";
201         pidl "return true;";
202         deindent;
203         pidl "}";
204         pidl "";
205 }
206
207 sub ParseInterface($)
208 {
209         my $if = shift;
210
211         my $uif = uc($if->{NAME});
212
213         pidl_hdr "#ifndef __SRV_$uif\__";
214         pidl_hdr "#define __SRV_$uif\__";
215
216         foreach (@{$if->{FUNCTIONS}}) {
217                 next if ($_->{PROPERTIES}{noopnum});
218                 ParseFunction($if, $_);
219         }
220
221         pidl "";
222         pidl "/* Tables */";
223         pidl "static struct api_struct api_$if->{NAME}_cmds[] = ";
224         pidl "{";
225         indent;
226
227         foreach (@{$if->{FUNCTIONS}}) {
228                 next if ($_->{PROPERTIES}{noopnum});
229                 pidl "{\"" . uc($_->{NAME}) . "\", NDR_" . uc($_->{NAME}) . ", api_$_->{NAME}},";
230         }
231
232         deindent;
233         pidl "};";
234
235         pidl "";
236
237         pidl_hdr "void $if->{NAME}_get_pipe_fns(struct api_struct **fns, int *n_fns);";
238         pidl "void $if->{NAME}_get_pipe_fns(struct api_struct **fns, int *n_fns)";
239         pidl "{";
240         indent;
241         pidl "*fns = api_$if->{NAME}_cmds;";
242         pidl "*n_fns = sizeof(api_$if->{NAME}_cmds) / sizeof(struct api_struct);";
243         deindent;
244         pidl "}";
245         pidl "";
246
247         pidl_hdr "NTSTATUS rpc_$if->{NAME}_init(void);";
248         pidl "NTSTATUS rpc_$if->{NAME}_init(void)";
249         pidl "{";
250         pidl "\treturn rpc_pipe_register_commands(SMB_RPC_INTERFACE_VERSION, \"$if->{NAME}\", \"$if->{NAME}\", \&ndr_table_$if->{NAME}.syntax_id, api_$if->{NAME}_cmds, sizeof(api_$if->{NAME}_cmds) / sizeof(struct api_struct));";
251         pidl "}";
252
253         pidl_hdr "#endif /* __SRV_$uif\__ */";
254 }
255
256 sub Parse($$$)
257 {
258         my($ndr,$header,$ndr_header) = @_;
259
260         $res = "";
261         $res_hdr = "";
262
263         pidl "/*";
264         pidl " * Unix SMB/CIFS implementation.";
265         pidl " * server auto-generated by pidl. DO NOT MODIFY!";
266         pidl " */";
267         pidl "";
268         pidl "#include \"includes.h\"";
269         pidl "#include \"$header\"";
270         pidl_hdr "#include \"$ndr_header\"";
271         pidl "";
272
273         foreach (@$ndr) {
274                 ParseInterface($_) if ($_->{TYPE} eq "INTERFACE");
275         }
276
277         return ($res, $res_hdr);
278 }
279
280 1;