pidl:Samba3/ServerNDR: normalize {RETURN_TYPE} by using mapTypeName()
[amitay/samba.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 error 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 ContainsPipe);
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, $fail) = @_;
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         } elsif
62
63         # we don't support multi-dimentional arrays yet
64         ($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         } else {
70                 # neither pointer nor array, no need to alloc something.
71                 return;
72         }
73
74         if ($l->{TYPE} eq "ARRAY") {
75                 unless(defined($l->{SIZE_IS})) {
76                         error($e->{ORIGINAL}, "No size known for array `$e->{NAME}'");
77                         pidl "#error No size known for array `$e->{NAME}'";
78                 } else {
79                         my $size = ParseExpr($l->{SIZE_IS}, $env, $e);
80                         pidl "$name = talloc_zero_array($mem_ctx, " . DeclLevel($e, 1) . ", $size);";
81                 }
82         } else {
83                 pidl "$name = talloc_zero($mem_ctx, " . DeclLevel($e, 1) . ");";
84         }
85
86         pidl "if ($name == NULL) {";
87         $fail->();
88         pidl "}";
89         pidl "";
90 }
91
92 sub CallWithStruct($$$$)
93 {
94         my ($pipes_struct, $mem_ctx, $fn, $fail) = @_;
95         my $env = GenerateFunctionOutEnv($fn);
96         my $hasout = 0;
97         foreach (@{$fn->{ELEMENTS}}) {
98                 if (grep(/out/, @{$_->{DIRECTION}})) { $hasout = 1; }
99         }
100
101         pidl "ZERO_STRUCT(r->out);" if ($hasout);
102
103         my $proto = "_$fn->{NAME}(struct pipes_struct *p, struct $fn->{NAME} *r";
104         my $ret = "_$fn->{NAME}($pipes_struct, r";
105         foreach (@{$fn->{ELEMENTS}}) {
106                 my @dir = @{$_->{DIRECTION}};
107                 if (grep(/in/, @dir) and grep(/out/, @dir)) {
108                         pidl "r->out.$_->{NAME} = r->in.$_->{NAME};";
109                 }
110         }
111
112         foreach (@{$fn->{ELEMENTS}}) {
113                 next if ContainsPipe($_, $_->{LEVELS}[0]);
114                 my @dir = @{$_->{DIRECTION}};
115                 if (grep(/in/, @dir) and grep(/out/, @dir)) {
116                         # noop
117                 } elsif (grep(/out/, @dir) and not
118                                  has_property($_, "represent_as")) {
119                         AllocOutVar($_, $mem_ctx, "r->out.$_->{NAME}", $env, $fail);
120                 }
121         }
122         $ret .= ")";
123         $proto .= ");";
124
125         if ($fn->{RETURN_TYPE}) {
126                 $ret = "r->out.result = $ret";
127                 $proto = mapTypeName($fn->{RETURN_TYPE})." $proto";
128         } else {
129                 $proto = "void $proto";
130         }
131
132         pidl_hdr "$proto";
133         pidl "$ret;";
134 }
135
136 sub ParseFunction($$)
137 {
138         my ($if,$fn) = @_;
139
140         my $op = "NDR_".uc($fn->{NAME});
141
142         pidl "static bool api_$fn->{NAME}(struct pipes_struct *p)";
143         pidl "{";
144         indent;
145         pidl "const struct ndr_interface_call *call;";
146         pidl "struct ndr_pull *pull;";
147         pidl "struct ndr_push *push;";
148         pidl "enum ndr_err_code ndr_err;";
149         pidl "struct $fn->{NAME} *r;";
150         pidl "";
151         pidl "call = &ndr_table_$if->{NAME}.calls[$op];";
152         pidl "";
153         pidl "r = talloc(talloc_tos(), struct $fn->{NAME});";
154         pidl "if (r == NULL) {";
155         pidl "\treturn false;";
156         pidl "}";
157         pidl "";
158         pidl "pull = ndr_pull_init_blob(&p->in_data.data, r);";
159         pidl "if (pull == NULL) {";
160         pidl "\ttalloc_free(r);";
161         pidl "\treturn false;";
162         pidl "}";
163         pidl "";
164         pidl "pull->flags |= LIBNDR_FLAG_REF_ALLOC;";
165         pidl "if (p->endian) {";
166         pidl "\tpull->flags |= LIBNDR_FLAG_BIGENDIAN;";
167         pidl "}";
168         pidl "ndr_err = call->ndr_pull(pull, NDR_IN, r);";
169         pidl "if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {";
170         pidl "\ttalloc_free(r);";
171         pidl "\treturn false;";
172         pidl "}";
173         pidl "";
174         pidl "if (DEBUGLEVEL >= 10) {";
175         pidl "\tNDR_PRINT_FUNCTION_DEBUG($fn->{NAME}, NDR_IN, r);";
176         pidl "}";
177         pidl "";
178
179         CallWithStruct("p", "r", $fn, 
180         sub { 
181                         pidl "\ttalloc_free(r);";
182                         pidl "\treturn false;";
183                 }
184         );
185
186         pidl "";
187         pidl "if (p->fault_state) {";
188         pidl "\ttalloc_free(r);";
189         pidl "\t/* Return true here, srv_pipe_hnd.c will take care */";
190         pidl "\treturn true;";
191         pidl "}";
192         pidl "";
193         pidl "if (DEBUGLEVEL >= 10) {";
194         pidl "\tNDR_PRINT_FUNCTION_DEBUG($fn->{NAME}, NDR_OUT | NDR_SET_VALUES, r);";
195         pidl "}";
196         pidl "";
197         pidl "push = ndr_push_init_ctx(r);";
198         pidl "if (push == NULL) {";
199         pidl "\ttalloc_free(r);";
200         pidl "\treturn false;";
201         pidl "}";
202         pidl "";
203         pidl "/*";
204         pidl " * carry over the pointer count to the reply in case we are";
205         pidl " * using full pointer. See NDR specification for full pointers";
206         pidl " */";
207         pidl "push->ptr_count = pull->ptr_count;";
208         pidl "";
209         pidl "ndr_err = call->ndr_push(push, NDR_OUT, r);";
210         pidl "if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {";
211         pidl "\ttalloc_free(r);";
212         pidl "\treturn false;";
213         pidl "}";
214         pidl "";
215         pidl "p->out_data.rdata = ndr_push_blob(push);";
216         pidl "talloc_steal(p->mem_ctx, p->out_data.rdata.data);";
217         pidl "";
218         pidl "talloc_free(r);";
219         pidl "";
220         pidl "return true;";
221         deindent;
222         pidl "}";
223         pidl "";
224 }
225
226 sub ParseInterface($)
227 {
228         my $if = shift;
229
230         my $uif = uc($if->{NAME});
231
232         pidl_hdr "#ifndef __SRV_$uif\__";
233         pidl_hdr "#define __SRV_$uif\__";
234
235         foreach (@{$if->{FUNCTIONS}}) {
236                 next if ($_->{PROPERTIES}{noopnum});
237                 ParseFunction($if, $_);
238         }
239
240         pidl "";
241         pidl "/* Tables */";
242         pidl "static struct api_struct api_$if->{NAME}_cmds[] = ";
243         pidl "{";
244         indent;
245
246         foreach (@{$if->{FUNCTIONS}}) {
247                 next if ($_->{PROPERTIES}{noopnum});
248                 pidl "{\"" . uc($_->{NAME}) . "\", NDR_" . uc($_->{NAME}) . ", api_$_->{NAME}},";
249         }
250
251         deindent;
252         pidl "};";
253
254         pidl "";
255
256         pidl_hdr "void $if->{NAME}_get_pipe_fns(struct api_struct **fns, int *n_fns);";
257         pidl "void $if->{NAME}_get_pipe_fns(struct api_struct **fns, int *n_fns)";
258         pidl "{";
259         indent;
260         pidl "*fns = api_$if->{NAME}_cmds;";
261         pidl "*n_fns = sizeof(api_$if->{NAME}_cmds) / sizeof(struct api_struct);";
262         deindent;
263         pidl "}";
264         pidl "";
265
266         if (not has_property($if, "no_srv_register")) {
267             pidl_hdr "struct rpc_srv_callbacks;";
268             pidl_hdr "NTSTATUS rpc_$if->{NAME}_init(const struct rpc_srv_callbacks *rpc_srv_cb);";
269             pidl "NTSTATUS rpc_$if->{NAME}_init(const struct rpc_srv_callbacks *rpc_srv_cb)";
270             pidl "{";
271             pidl "\treturn rpc_srv_register(SMB_RPC_INTERFACE_VERSION, \"$if->{NAME}\", \"$if->{NAME}\", \&ndr_table_$if->{NAME}, api_$if->{NAME}_cmds, sizeof(api_$if->{NAME}_cmds) / sizeof(struct api_struct), rpc_srv_cb);";
272             pidl "}";
273
274             pidl "";
275
276             pidl_hdr "NTSTATUS rpc_$if->{NAME}_shutdown(void);";
277             pidl "NTSTATUS rpc_$if->{NAME}_shutdown(void)";
278             pidl "{";
279             pidl "\treturn rpc_srv_unregister(\&ndr_table_$if->{NAME});";
280             pidl "}";
281         }
282         pidl_hdr "#endif /* __SRV_$uif\__ */";
283 }
284
285 sub Parse($$$)
286 {
287         my($ndr,$header,$ndr_header) = @_;
288
289         $res = "";
290         $res_hdr = "";
291
292         pidl "/*";
293         pidl " * Unix SMB/CIFS implementation.";
294         pidl " * server auto-generated by pidl. DO NOT MODIFY!";
295         pidl " */";
296         pidl "";
297         pidl "#include \"includes.h\"";
298         pidl "#include \"ntdomain.h\"";
299         pidl "#include \"$header\"";
300         pidl_hdr "#include \"$ndr_header\"";
301         pidl "";
302
303         foreach (@$ndr) {
304                 ParseInterface($_) if ($_->{TYPE} eq "INTERFACE");
305         }
306
307         return ($res, $res_hdr);
308 }
309
310 1;