3 # Create ejs interfaces for structures in a C header file
10 # Generate parse tree for header file
14 require smb_interfaces
;
15 my $parser = new smb_interfaces
;
16 $header = $parser->parse($file);
19 # Make second pass over tree to make it easier to process.
22 sub flatten_structs
($) {
26 # Map NAME, STRUCT_NAME and UNION_NAME elements into a more likeable
29 if (defined($obj->{STRUCT_NAME
}) or defined($obj->{UNION_NAME
})) {
31 $s->{TYPE_DEFINED
} = defined($obj->{STRUCT_NAME
}) ?
$obj->{STRUCT_NAME
}
34 delete $s->{STRUCT_NAME
};
35 delete $s->{UNION_NAME
};
38 # Create a new list of structure fields with flattened names
40 foreach my $elt (@
{$obj->{DATA
}}) {
41 foreach my $name (@
{$elt->{NAME
}}) {
42 my $new_elt = { %$elt };
43 $new_elt->{NAME
} = $name;
44 # $new_elt->{PARENT} = $s;
45 push(@
{$s->{FIELDS
}}, flatten_structs
($new_elt));
54 @newheader = map { flatten_structs
($_) } @
{$header};
57 # Generate implementation
60 my $basename = basename
($file, ".h");
61 stat "libcli/gen_raw" || mkdir("libcli/gen_raw") || die("mkdir");
63 open(FILE
, ">libcli/gen_raw/ejs_${basename}.c");
65 print FILE
"/* EJS wrapper functions auto-generated by build_smb_interfaces.pl */\n\n";
67 print FILE
"#include \"includes.h\"\n";
68 print FILE
"#include \"scripting/ejs/smbcalls.h\"\n";
69 print FILE
"#include \"lib/appweb/ejs/ejs.h\"\n";
70 print FILE
"#include \"scripting/ejs/ejsrpc.h\"\n"; # TODO: remove this
73 sub transfer_element
($$$) {
81 print FILE
"\tNDR_CHECK(ejs_${dir}_$type(ejs, v, \"$prefix.$elt->{NAME}\"));\n";
84 sub transfer_struct
($$) {
88 foreach my $field (@
{$struct->{FIELDS
}}) {
89 next if $dir eq "pull" and $field->{NAME
} eq "out";
90 next if $dir eq "push" and $field->{NAME
} eq "in";
92 if ($field->{TYPE
} eq "struct") {
93 foreach $subfield (@
{$field->{FIELDS
}}) {
94 transfer_element
($dir, $field->{NAME
}, $subfield);
97 transfer_element
($dir, $struct->{NAME
}, $field);
102 # Top level call functions
104 foreach my $s (@newheader) {
106 if ($s->{TYPE
} eq "struct") {
108 # Push/pull top level struct
110 print FILE
"NTSTATUS ejs_pull_$s->{TYPE_DEFINED}(struct ejs_rpc *ejs, struct MprVar *v, struct $s->{TYPE_DEFINED} *r)\n";
113 transfer_struct
("pull", $s);
115 print FILE
"\n\treturn NT_STATUS_OK;\n";
118 print FILE
"NTSTATUS ejs_push_$s->{TYPE_DEFINED}(struct ejs_rpc *ejs, struct MprVar *v, const struct $s->{TYPE_DEFINED} *r)\n";
121 transfer_struct
("push", $s);
123 print FILE
"\n\treturn NT_STATUS_OK;\n";
128 print FILE
"static int ejs_$s->{TYPE_DEFINED}(int eid, int argc, struct MprVar **argv)\n";
130 print FILE
"\treturn ejs_raw_call(eid, argc, argv, (ejs_pull_function_t)ejs_pull_$s->{TYPE_DEFINED}, (ejs_push_function_t)ejs_push_$s->{TYPE_DEFINED});\n";
137 foreach my $arm (@
{$s->{FIELDS
}}) {
139 # Push/pull union arm
141 print FILE
"NTSTATUS ejs_pull_$s->{TYPE_DEFINED}_$arm->{NAME}(struct ejs_rpc *ejs, struct MprVar *v, union $s->{TYPE_DEFINED} *r)\n";
144 transfer_struct
("pull", $arm);
146 print FILE
"\n\treturn NT_STATUS_OK;\n";
149 print FILE
"NTSTATUS ejs_push_$s->{TYPE_DEFINED}_$arm->{NAME}(struct ejs_rpc *ejs, struct MprVar *v, const union $s->{TYPE_DEFINED} *r)\n";
152 transfer_struct
("push", $arm);
154 print FILE
"\n\treturn NT_STATUS_OK;\n";