r21029: - Remove now unused failure checking code
[Samba.git] / source / script / build_smb_interfaces.pl
blob5fac94ca6e4ab12c9767a9082db16a7d7d3b0281
1 #!/usr/bin/perl
3 # Create ejs interfaces for structures in a C header file
6 use File::Basename;
7 use Data::Dumper;
10 # Generate parse tree for header file
13 my $file = shift;
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($) {
23 my $obj = shift;
24 my $s = { %$obj };
26 # Map NAME, STRUCT_NAME and UNION_NAME elements into a more likeable
27 # property.
29 if (defined($obj->{STRUCT_NAME}) or defined($obj->{UNION_NAME})) {
31 $s->{TYPE_DEFINED} = defined($obj->{STRUCT_NAME}) ? $obj->{STRUCT_NAME}
32 : $obj->{UNION_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));
49 delete $s->{DATA};
51 return $s;
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
71 print FILE "\n";
73 sub transfer_element($$$) {
74 my $dir = shift;
75 my $prefix = shift;
76 my $elt = shift;
78 $type = $elt->{TYPE};
79 $type =~ s/_t$//;
81 print FILE "\tNDR_CHECK(ejs_${dir}_$type(ejs, v, \"$prefix.$elt->{NAME}\"));\n";
84 sub transfer_struct($$) {
85 my $dir = shift;
86 my $struct = shift;
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);
96 } else {
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";
111 print FILE "{\n";
113 transfer_struct("pull", $s);
115 print FILE "\n\treturn NT_STATUS_OK;\n";
116 print FILE "}\n\n";
118 print FILE "NTSTATUS ejs_push_$s->{TYPE_DEFINED}(struct ejs_rpc *ejs, struct MprVar *v, const struct $s->{TYPE_DEFINED} *r)\n";
119 print FILE "{\n";
121 transfer_struct("push", $s);
123 print FILE "\n\treturn NT_STATUS_OK;\n";
124 print FILE "}\n\n";
126 # Function call
128 print FILE "static int ejs_$s->{TYPE_DEFINED}(int eid, int argc, struct MprVar **argv)\n";
129 print FILE "{\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";
131 print FILE "}\n\n";
133 } else {
135 # Top level union
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";
142 print FILE "{\n";
144 transfer_struct("pull", $arm);
146 print FILE "\n\treturn NT_STATUS_OK;\n";
147 print FILE "}\n\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";
150 print FILE "{\n";
152 transfer_struct("push", $arm);
154 print FILE "\n\treturn NT_STATUS_OK;\n";
155 print FILE "}\n\n";
161 close(FILE);