1
0
mirror of https://github.com/samba-team/samba.git synced 2025-11-29 16:23:52 +03:00

r9202: OK now we are getting somewhere. Generate js stubs for top level structs

and each arm of top level unions.  Don't generate a header file (duh) as
the namespace stuff is done in the module init.
This commit is contained in:
Tim Potter
2005-08-07 22:14:30 +00:00
committed by Gerald (Jerry) Carter
parent a6541a0702
commit dde7f8be97

View File

@@ -19,8 +19,6 @@ $header = $parser->parse($file);
# Make second pass over tree to make it easier to process.
#
my @structs;
sub flatten_structs($) {
my $obj = shift;
my $s = { %$obj };
@@ -37,10 +35,13 @@ sub flatten_structs($) {
delete $s->{UNION_NAME};
}
# Create a new list of structure fields with flattened names
foreach my $elt (@{$obj->{DATA}}) {
foreach my $name (@{$elt->{NAME}}) {
my $new_elt = { %$elt };
$new_elt->{NAME} = $name;
$new_elt->{PARENT} = $s;
push(@{$s->{FIELDS}}, flatten_structs($new_elt));
}
}
@@ -50,111 +51,7 @@ sub flatten_structs($) {
return $s;
}
foreach my $s (@{$header}) { # For each parsed structure
print Dumper(flatten_structs($s));
}
print Dumper(@structs);
exit;
#
# Generate header
#
my $basename = basename($file, ".h");
stat "libcli/gen_raw" || mkdir("libcli/gen_raw") || die("mkdir");
open(FILE, ">libcli/gen_raw/ejs_${basename}.h");
print FILE "/* header auto-generated by build_smb_interfaces.pl */\n\n";
print FILE "#ifndef _ejs_${basename}_h\n";
print FILE "#define _ejs_${basename}_h\n\n";
# Generate a push/pull prototype for every top level structure, as
# well as every non-anonymous nested structure (i.e TYPE_NAME element
# is undefined.
foreach my $s (@{$header}) {
# Top level
print FILE "NTSTATUS ejs_push_$s->{TYPE_NAME}(struct ejs_rpc *, struct MprVar *, const char *, const uint32_t *);\n";
print FILE "NTSTATUS ejs_pull_$s->{TYPE_NAME}(struct ejs_rpc *, struct MprVar *, const char *, const uint32_t *);\n";
sub header_for($$) {
my $prefix = shift;
my $obj = shift;
return if !($obj->{TYPE} eq "struct" or $obj->{TYPE} eq "union");
return if ($obj->{NAME} eq "in" or $obj->{NAME} eq "out");
print FILE "NTSTATUS ejs_push_${prefix}_$obj->{NAME}(struct ejs_rpc *, struct MprVar *, const char *, const uint32_t *);\n";
print FILE "NTSTATUS ejs_pull_${prefix}_$obj->{NAME}(struct ejs_rpc *, struct MprVar *, const char *, const uint32_t *);\n";
foreach my $key (%{$obj->{FIELDS}}) {
header_for("${prefix}.$obj->{TYPE_NAME}", $obj->{FIELDS}{$key});
}
}
}
exit;
sub struct_name($)
{
my $obj = shift;
return defined($obj->{STRUCT_NAE}) ? $obj->{STRUCT_NAME} : $obj->{UNION_NAME};
}
sub prototypes_for($)
{
my $obj = shift;
my $name = struct_name($obj);
print FILE "NTSTATUS ejs_push_$name(struct ejs_rpc *, struct MprVar *, const char *, const uint32_t *);\n";
print FILE "NTSTATUS ejs_pull_$name(struct ejs_rpc *, struct MprVar *, const char *, const uint32_t *);\n";
}
foreach my $x (@{$header}) {
# Prototypes for top level structures and unions
prototypes_for($x);
foreach my $e1 (@{$x->{DATA}}) {
foreach my $e2 (@{$e1->{DATA}}) {
# Prototypes for non-anonymous nested structures and unions:
#
# e.g struct foo {...};
if (defined($e2->{STRUCT_NAME}) or defined($e2->{UNION_NAME})) {
prototypes_for($e2);
}
# We also would like to push/pull nested structures and unions:
#
# e.g struct foo {
# struct {...} bar;
# };
if ($e2->{TYPE} eq "struct") {
if (defined($e2->{NAME}) and !defined($e2->{STRUCT_NAME})) {
foreach my $x (@{$e2->{NAME}}) {
$name = "$e1->{NAME}[0]_$x";
print FILE "NTSTATUS ejs_push_$name(struct ejs_rpc *, struct MprVar *, const char *, const uint32_t *);\n";
print FILE "NTSTATUS ejs_pull_$name(struct ejs_rpc *, struct MprVar *, const char *, const uint32_t *);\n";
}
}
}
}
}
}
print FILE "#endif\n";
close(FILE);
@newheader = map { flatten_structs($_) } @{$header};
#
# Generate implementation
@@ -169,107 +66,60 @@ print FILE "#include \"lib/appweb/ejs/ejs.h\"\n";
print FILE "#include \"scripting/ejs/ejsrpc.h\"\n"; # TODO: remove this
print FILE "\n";
# Top level push/pull functions
sub print_field($$) {
my $f = shift;
my $suffix = shift;
my $type = $f->{TYPE};
if ($f->{TYPE} eq "char" and $f->{POINTERS} == 1) {
$type = "string";
}
if ($f->{TYPE} =~ /_t$/) {
$type = $f->{TYPE};
$type =~ s/_t$//;
}
my $deref = "&";
if ($f->{POINTERS} == 1 && $type ne "string") {
$deref = "";
}
foreach my $x (@{$f->{NAME}}) {
if ($f->{POINTERS} > 0) {
print FILE "\t// alloc $x?\n";
}
if ($f->{TYPE} eq "struct") {
$type = $f->{STRUCT_NAME};
}
print FILE "\tNDR_CHECK(ejs_pull_$type(ejs, v, \"$x\", ${deref}r->$suffix.$x));\n";
}
}
foreach my $x (@{$header}) {
next, if $x->{STRUCT_NAME} eq "";
# Pull in to struct.in
print FILE "static NTSTATUS ejs_pull_$x->{STRUCT_NAME}(struct ejs_rpc *ejs, struct MprVar *v, struct $x->{STRUCT_NAME} *r)\n";
print FILE "{\n";
print FILE "\tNDR_CHECK(ejs_pull_struct_start(ejs, &v, \"input\"));\n";
foreach my $e (@{$x->{DATA}}) {
next, if $e->{NAME}[0] ne 'in';
foreach my $f (@{$e->{DATA}}) {
print_field($f, "in");
}
}
print FILE "\n\treturn NT_STATUS_OK;\n";
print FILE "}\n\n";
# Push from struct.out
print FILE "static NTSTATUS ejs_push_$x->{STRUCT_NAME}(struct ejs_rpc *ejs, struct MprVar *v, struct $x->{STRUCT_NAME} *r)\n\n";
print FILE "{\n";
print FILE "\tNDR_CHECK(ejs_push_struct_start(ejs, &v, \"output\"));\n";
foreach my $e (@{$x->{DATA}}) {
next, if $e->{NAME}[0] ne 'out';
foreach my $f (@{$e->{DATA}}) {
print_field($f, "out");
}
}
print FILE "\n\treturn NT_STATUS_OK;\n";
print FILE "}\n\n";
}
# Nested anonymous structures
foreach my $x (@{$header}) {
foreach my $e1 (@{$x->{DATA}}) {
foreach my $e2 (@{$e1->{DATA}}) {
if ($e2->{TYPE} eq "struct") {
if (defined($e2->{NAME}) and !defined($e2->{STRUCT_NAME})) {
foreach my $x (@{$e2->{NAME}}) {
$name = "$e1->{NAME}[0]_$x";
print FILE "static NTSTATUS ejs_push_$name(struct ejs_rpc *ejs, struct MprVar *v, const char *name, const uint32_t *r)\n";
print FILE "{\n";
print FILE "\treturn NT_STATUS_OK;\n";
print FILE "}\n\n";
print FILE "static NTSTATUS ejs_pull_$name(struct ejs_rpc *ejs, struct MprVar *v, const char *name, const uint32_t *r)\n";
print FILE "{\n";
print FILE "\treturn NT_STATUS_OK;\n";
print FILE "}\n\n";
}
}
}
}
}
}
# Top level call functions
foreach my $s (@newheader) {
if ($s->{TYPE} eq "struct") {
# Top level struct
print FILE "static NTSTATUS ejs_$s->{TYPE_DEFINED}(int eid, int argc, struct MprVar **argv)\n";
print FILE "{\n";
print FILE "\treturn NT_STATUS_OK;\n";
print FILE "}\n\n";
} else {
# Top level union
foreach my $arm (@{$s->{FIELDS}}) {
print FILE "static NTSTATUS ejs_$s->{TYPE_DEFINED}_$arm->{NAME}(int eid, int argc, struct MprVar **argv)\n";
print FILE "{\n";
print FILE "\treturn NT_STATUS_OK;\n";
print FILE "}\n\n";
}
}
}
# Module initialisation
print FILE "static int ejs_${basename}_init(int eid, int argc, struct MprVar **argv)\n";
print FILE "{\n";
print FILE "\tstruct MprVar *obj = mprInitObject(eid, \"${basename}\", argc, argv);\n\n";
foreach my $s (@newheader) {
if ($s->{TYPE} eq "struct") {
print FILE "\tmprSetCFunction(obj, \"$s->{TYPE_DEFINED}\", ejs_$s->{TYPE_DEFINED});\n";
} else {
foreach my $arm (@{$s->{FIELDS}}) {
print FILE "\tmprSetCFunction(obj, \"$s->{TYPE_DEFINED}_$arm->{NAME}\", ejs_$s->{TYPE_DEFINED});\n";
}
}
}
print FILE "}\n\n";
print FILE "NTSTATUS ejs_init_${basename}(void)\n";
print FILE "{\n";
print FILE "\treturn smbcalls_register_ejs(\"${basename}_init\", ejs_${basename}_init);\n";
print FILE "}\n";
close(FILE);
exit;
foreach my $x (@{$header}) {
next, if $x->{STRUCT_NAME} eq "";
@@ -309,22 +159,3 @@ __HERE__
print FILE "}\n\n";
}
# Module initialisation
print FILE "static int ejs_${basename}_init(int eid, int argc, struct MprVar **argv)\n";
print FILE "{\n";
print FILE "\tstruct MprVar *obj = mprInitObject(eid, \"${basename}\", argc, argv);\n\n";
foreach my $x (@{$header}) {
next, if $x->{STRUCT_NAME} eq "";
print FILE "\tmprSetCFunction(obj, \"$x->{STRUCT_NAME}\", ejs_$x->{STRUCT_NAME});\n";
}
print FILE "}\n\n";
print FILE "NTSTATUS ejs_init_${basename}(void)\n";
print FILE "{\n";
print FILE "\treturn smbcalls_register_ejs(\"${basename}_init\", ejs_${basename}_init);\n";
print FILE "}\n";
close(FILE);