summaryrefslogtreecommitdiff
path: root/source4/build/pidl/swig.pm
blob: 59eb493bf2727923d5e0a8bd8179ad1796b3a12b (plain)
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
###################################################
# Samba4 parser generator for swig wrappers
# Copyright tpot@samba.org 2004
# released under the GNU GPL

package IdlSwig;

use strict;
use Data::Dumper;

my($res);
my($name);

sub ParseFunction($)
{
    my($fn) = shift;

    $res .= "%{\n\n";

    $res .= "/* Convert Python dict to struct $fn->{NAME}.in */\n\n";

    $res .= "int python_to_$fn->{NAME}(TALLOC_CTX *mem_ctx, struct $fn->{NAME} *s, PyObject *obj)\n";
    $res .= "{\n";

    foreach my $e (@{$fn->{DATA}}) {
	if (util::has_property($e, "in")) {
	    $res .= "\t// $e->{TYPE} $e->{NAME}\n";
	}
    }

    $res .= "\n";
    $res .= "\treturn True;\n";
    $res .= "}\n\n";

    $res .= "/* Convert struct $fn->{NAME}.out to Python dict */\n\n";

    $res .= "int $fn->{NAME}_to_python(TALLOC_CTX *mem_ctx, PyObject *obj, struct $fn->{NAME} *s)\n";
    $res .= "{\n";

    foreach my $e (@{$fn->{DATA}}) {
	if (util::has_property($e, "out")) {
	    $res .= "\t// $e->{TYPE} $e->{NAME}\n";
	}
    }

    $res .= "\n";
    $res .= "\treturn True;\n";
    $res .= "}\n\n";

    $res .= "%}\n\n";

    # Input typemap

    $res .= "%typemap(in) struct $fn->{NAME} * (struct $fn->{NAME} temp) {\n";
    $res .= "\tTALLOC_CTX *mem_ctx = talloc_init(\"typemap(int) $fn->{NAME}\");\n\n";
    $res .= "\tpython_to_$fn->{NAME}(mem_ctx, &temp, \$input);\n";
    $res .= "\t\$1 = &temp;\n";
    $res .= "}\n\n";

    # Output typemap

    $res .= "%typemap(argout) struct $fn->{NAME} * {\n";
    $res .= "\tTALLOC_CTX *mem_ctx = talloc_init(\"typemap(argout) $fn->{NAME}\");\n\n";
    $res .= "\tlong status = PyLong_AsLong(resultobj);\n";
    $res .= "\tPyObject *dict;\n";
    $res .= "\n";
    $res .= "\tif (status != 0) {\n";
    $res .= "\t\tset_ntstatus_exception(status);\n";
    $res .= "\t\treturn NULL;\n";
    $res .= "\t}\n";
    $res .= "\n";
    $res .= "\tdict = PyDict_New();\n";

    $res .= "\t$fn->{NAME}_to_python(mem_ctx, dict, \$1);\n";

    $res .= "\tresultobj = dict;\n";
    $res .= "}\n\n";

    # Function definitions

    $res .= "%rename($fn->{NAME}) dcerpc_$fn->{NAME};\n";
    $res .= "$fn->{RETURN_TYPE} dcerpc_$fn->{NAME}(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct $fn->{NAME} *r);\n\n";
}

sub ParseStruct($)
{
    my($s) = shift;

    $res .= "%{\n\n";
    $res .= "/* Convert Python dict to struct $s->{NAME} */\n\n";
    
    $res .= "int python_to_$s->{NAME}(TALLOC_CTX *mem_ctx, struct $s->{NAME} *s, PyObject *obj)\n";
    $res .= "{\n";

    foreach my $e (@{$s->{DATA}{ELEMENTS}}) {
	$res .= "\t// $e->{TYPE} $e->{NAME}\n";
    }

    $res .= "\n";
    $res .= "\treturn TRUE;\n";
    $res .= "}\n\n";

    $res .= "/* Convert struct $s->{NAME} to Python dict */\n\n";

    $res .= "int $s->{NAME}_to_python(TALLOC_CTX *mem_ctx, PyObject *obj, struct $s->{NAME} *s)\n";
    $res .= "{\n";

    foreach my $e (@{$s->{DATA}{ELEMENTS}}) {
	$res .= "\t// $e->{TYPE} $e->{NAME}\n";
    }

    $res .= "\n";
    $res .= "\treturn TRUE;\n";
    $res .= "}\n";

    $res .= "\n%}\n\n";    
}

sub ParseTypedef($)
{
    my($t) = shift;

    foreach my $e ($t) {
	($e->{DATA}{TYPE} eq "STRUCT") && ParseStruct($e);
    }
}

sub ParseInheritedData($)
{
    my($data) = shift;

    foreach my $e (@{$data}) {
	($e->{TYPE} eq "FUNCTION") && ParseFunction($e);
	($e->{TYPE} eq "TYPEDEF") && ParseTypedef($e);
    }
}

sub ParseHeader($)
{
    my($hdr) = shift;

    $name = $hdr->{NAME};
    $res .= "#define DCERPC_" . uc($name) . "_UUID \"$hdr->{PROPERTIES}->{uuid}\"\n";
    $res .= "const int DCERPC_" . uc($name) . "_VERSION = " . $hdr->{PROPERTIES}->{version} . ";\n";
    $res .= "#define DCERPC_" . uc($name) . "_NAME \"" . $name . "\"\n";
    $res .= "\n";

    ParseInheritedData($hdr->{INHERITED_DATA});    
}

sub Parse($)
{
    my($idl) = shift;

    $res = "/* auto-generated by pidl */\n\n";

    foreach my $x (@{$idl}) {
	($x->{TYPE} eq "INTERFACE") && ParseHeader($x);
    }

    return $res;
}

1;