1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
###################################################
# EJS function wrapper generator
# Copyright jelmer@samba.org 2005
# Copyright Andrew Tridgell 2005
# released under the GNU GPL
package EjsClient;
use strict;
use pidl::typelist;
my($res);
sub pidl ($)
{
$res .= shift;
}
# this should probably be in ndr.pm
sub GenerateStructEnv($)
{
my $x = shift;
my %env;
foreach my $e (@{$x->{ELEMENTS}}) {
$env{$e->{NAME}} = "r->$e->{NAME}";
}
$env{"this"} = "r";
return \%env;
}
sub GenerateFunctionInEnv($)
{
my $fn = shift;
my %env;
foreach my $e (@{$fn->{ELEMENTS}}) {
if (grep (/in/, @{$e->{DIRECTION}})) {
$env{$e->{NAME}} = "r->in.$e->{NAME}";
}
}
return \%env;
}
sub GenerateFunctionOutEnv($)
{
my $fn = shift;
my %env;
foreach my $e (@{$fn->{ELEMENTS}}) {
if (grep (/out/, @{$e->{DIRECTION}})) {
$env{$e->{NAME}} = "r->out.$e->{NAME}";
} elsif (grep (/in/, @{$e->{DIRECTION}})) {
$env{$e->{NAME}} = "r->in.$e->{NAME}";
}
}
return \%env;
}
sub get_pointer_to($)
{
my $var_name = shift;
if ($var_name =~ /^\*(.*)$/) {
return $1;
} elsif ($var_name =~ /^\&(.*)$/) {
return "&($var_name)";
} else {
return "&$var_name";
}
}
sub get_value_of($)
{
my $var_name = shift;
if ($var_name =~ /^\&(.*)$/) {
return $1;
} else {
return "*$var_name";
}
}
###########################
# pull a scalar element
sub EjsPullScalar($$)
{
my $e = shift;
my $env = shift;
my $var = util::ParseExpr($e->{NAME}, $env);
my $ptr = get_pointer_to($var);
pidl "\tNDR_CHECK(ejs_pull_$e->{TYPE}(ejs, v, \"$e->{NAME}\", $ptr));\n";
}
###########################
# pull an arrar element
# only handles a very limited range of array types so far
sub EjsPullArray($$$)
{
my $e = shift;
my $l = shift;
my $env = shift;
my $length = util::ParseExpr($l->{LENGTH_IS}, $env);
my $var = util::ParseExpr($e->{NAME}, $env);
my $ptr = get_pointer_to($var);
pidl "\tNDR_CHECK(ejs_pull_array(ejs, v, \"$e->{NAME}\", $length, sizeof($ptr\[0]), (void **)$ptr, (ejs_pull_t)ejs_pull_$e->{TYPE}));\n";
}
###########################
# pull a structure element
sub EjsPullElement($$)
{
my $e = shift;
my $env = shift;
my $l = $e->{LEVELS}[0];
($l->{TYPE} eq "ARRAY") && EjsPullArray($e, $l, $env);
($l->{TYPE} eq "DATA") && EjsPullScalar($e, $env);
}
###########################
# pull a struct
sub EjsStructPull($$)
{
my $name = shift;
my $d = shift;
my $env = GenerateStructEnv($d);
pidl "\nstatic NTSTATUS ejs_pull_$name(struct ejs_rpc *ejs, struct MprVar *v, const char *name, struct $name *r)\n{\n";
pidl "\tNDR_CHECK(ejs_pull_struct_start(ejs, &v, name));\n";
foreach my $e (@{$d->{ELEMENTS}}) {
EjsPullElement($e, $env);
}
pidl "\treturn NT_STATUS_OK;\n";
pidl "}\n\n";
}
###########################
# pull a enum
sub EjsEnumPull($$)
{
my $name = shift;
my $d = shift;
pidl "\nstatic NTSTATUS ejs_pull_$name(struct ejs_rpc *ejs, struct MprVar *v, const char *name, enum $name *r)\n{\n";
pidl "\tunsigned e;\n";
pidl "\tNDR_CHECK(ejs_pull_enum(ejs, v, name, &e));\n";
pidl "\t*r = e;\n";
pidl "\treturn NT_STATUS_OK;\n";
pidl "}\n\n";
}
###########################
# generate a structure pull
sub EjsTypedefPull($)
{
my $d = shift;
$d->{DATA}->{TYPE} eq 'STRUCT' && EjsStructPull($d->{NAME}, $d->{DATA});
$d->{DATA}->{TYPE} eq 'ENUM' && EjsEnumPull($d->{NAME}, $d->{DATA});
}
#####################
# generate a function
sub EjsPullFunction($)
{
my $d = shift;
my $env = GenerateFunctionInEnv($d);
pidl "\nstatic NTSTATUS ejs_pull_$d->{NAME}(struct ejs_rpc *ejs, struct MprVar *v, struct $d->{NAME} *r)\n";
pidl "{\n";
pidl "\tNDR_CHECK(ejs_pull_struct_start(ejs, &v, \"in\"));\n";
foreach my $e (@{$d->{ELEMENTS}}) {
next unless (grep(/in/, @{$e->{DIRECTION}}));
EjsPullElement($e, $env);
}
pidl "\treturn NT_STATUS_OK;\n";
pidl "}\n\n";
}
###########################
# pull a scalar element
sub EjsPushScalar($$$)
{
my $e = shift;
my $l = shift;
my $env = shift;
my $var = util::ParseExpr($e->{NAME}, $env);
if (not typelist::is_scalar($l->{DATA_TYPE}) or
typelist::scalar_is_reference($l->{DATA_TYPE})) {
$var = get_pointer_to($var);
}
pidl "\tNDR_CHECK(ejs_push_$e->{TYPE}(ejs, v, \"$e->{NAME}\", $var));\n";
}
###########################
# pull a pointer element
sub EjsPushPointer($$$)
{
my $e = shift;
my $l = shift;
my $env = shift;
my $var = util::ParseExpr($e->{NAME}, $env);
while ($l->{TYPE} eq "POINTER") {
$var = get_value_of($var);
$l = Ndr::GetNextLevel($e, $l);
}
if (not typelist::is_scalar($l->{DATA_TYPE})) {
$var = get_pointer_to($var);
}
pidl "\tNDR_CHECK(ejs_push_$e->{TYPE}(ejs, v, \"$e->{NAME}\", $var));\n";
}
###########################
# push an arrar element
# only handles a very limited range of array types so far
sub EjsPushArray($$$)
{
my $e = shift;
my $l = shift;
my $env = shift;
my $length = util::ParseExpr($l->{LENGTH_IS}, $env);
my $var = util::ParseExpr($e->{NAME}, $env);
pidl "\tNDR_CHECK(ejs_push_array(ejs, v, \"$e->{NAME}\", $length, sizeof($var\[0]), (void *)$var, (ejs_push_t)ejs_push_$e->{TYPE}));\n";
}
###########################
# push a structure element
sub EjsPushElement($$)
{
my $e = shift;
my $env = shift;
my $l = $e->{LEVELS}[0];
($l->{TYPE} eq "ARRAY") && EjsPushArray($e, $l, $env);
($l->{TYPE} eq "DATA") && EjsPushScalar($e, $l, $env);
($l->{TYPE} eq "POINTER") && EjsPushPointer($e, $l, $env);
}
###########################
# push a struct
sub EjsStructPush($$)
{
my $name = shift;
my $d = shift;
my $env = GenerateStructEnv($d);
pidl "\nstatic NTSTATUS ejs_push_$name(struct ejs_rpc *ejs, struct MprVar *v, const char *name, struct $name *r)\n{\n";
pidl "\tNDR_CHECK(ejs_push_struct_start(ejs, &v, name));\n";
foreach my $e (@{$d->{ELEMENTS}}) {
EjsPushElement($e, $env);
}
pidl "\treturn NT_STATUS_OK;\n";
pidl "}\n\n";
}
###########################
# push a union
sub EjsUnionPush($$)
{
my $name = shift;
my $d = shift;
pidl "\nstatic NTSTATUS ejs_push_$name(struct ejs_rpc *ejs, struct MprVar *v, const char *name, union $name *r)\n{\n";
pidl "\treturn NT_STATUS_OK;\n";
pidl "}\n\n";
}
###########################
# push a enum
sub EjsEnumPush($$)
{
my $name = shift;
my $d = shift;
pidl "\nstatic NTSTATUS ejs_push_$name(struct ejs_rpc *ejs, struct MprVar *v, const char *name, enum $name r)\n{\n";
pidl "\tNDR_CHECK(ejs_push_enum(ejs, v, name, r));\n";
pidl "\treturn NT_STATUS_OK;\n";
pidl "}\n\n";
}
###########################
# generate a structure push
sub EjsTypedefPush($)
{
my $d = shift;
$d->{DATA}->{TYPE} eq 'STRUCT' && EjsStructPush($d->{NAME}, $d->{DATA});
$d->{DATA}->{TYPE} eq 'UNION' && EjsUnionPush($d->{NAME}, $d->{DATA});
$d->{DATA}->{TYPE} eq 'ENUM' && EjsEnumPush($d->{NAME}, $d->{DATA});
}
#####################
# generate a function
sub EjsPushFunction($)
{
my $d = shift;
my $env = GenerateFunctionOutEnv($d);
pidl "\nstatic NTSTATUS ejs_push_$d->{NAME}(struct ejs_rpc *ejs, struct MprVar *v, const struct $d->{NAME} *r)\n";
pidl "{\n";
pidl "\tNDR_CHECK(ejs_push_struct_start(ejs, &v, \"out\"));\n";
foreach my $e (@{$d->{ELEMENTS}}) {
next unless (grep(/out/, @{$e->{DIRECTION}}));
EjsPushElement($e, $env);
}
pidl "\treturn NT_STATUS_OK;\n";
pidl "}\n\n";
}
#################################
# generate a ejs mapping function
sub EjsFunction($)
{
my $d = shift;
my $name = $d->{NAME};
pidl "static int ejs_$name(int eid, int argc, struct MprVar **argv)\n";
pidl "{\n";
pidl "\treturn ejs_rpc_call(eid, argc, argv, \"$name\", (ejs_pull_function_t)ejs_pull_$name, (ejs_push_function_t)ejs_push_$name);\n";
pidl "}\n\n";
}
#####################################################################
# parse the interface definitions
sub EjsInterface($)
{
my($interface) = shift;
my @fns = ();
my $name = $interface->{NAME};
foreach my $d (@{$interface->{TYPEDEFS}}) {
EjsTypedefPush($d);
EjsTypedefPull($d);
}
foreach my $d (@{$interface->{FUNCTIONS}}) {
next if not defined($d->{OPNUM});
EjsPullFunction($d);
EjsPushFunction($d);
EjsFunction($d);
push (@fns, $d->{NAME});
}
pidl "void setup_ejs_$name(void)\n";
pidl "{\n";
foreach (@fns) {
pidl "\tejsDefineCFunction(-1, \"dcerpc_$_\", ejs_$_, NULL, MPR_VAR_SCRIPT_HANDLE);\n";
}
pidl "}\n";
}
#####################################################################
# parse a parsed IDL into a C header
sub Parse($$)
{
my($ndr,$hdr) = @_;
$res = "";
pidl "
/* EJS wrapper functions auto-generated by pidl */
#include \"includes.h\"
#include \"lib/ejs/ejs.h\"
#include \"$hdr\"
#include \"scripting/ejs/ejsrpc.h\"
";
foreach my $x (@{$ndr}) {
if ($x->{TYPE} eq "INTERFACE") {
($x->{TYPE} eq "INTERFACE") && EjsInterface($x);
}
}
return $res;
}
1;
|