summaryrefslogtreecommitdiff
path: root/source4/build/pidl/eparser.pm
blob: 672768d4577f9e58a756296b8805de558c237578 (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
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
###################################################
# Ethereal parser generator for IDL structures
# Copyright tpot@samba.org 2001
# Copyright tridge@samba.org 2000
# released under the GNU GPL

package IdlEParser;

use Data::Dumper;

my($res);

#####################################################################
# parse a properties list
sub ParseProperties($)
{
    my($props) = shift;
    foreach my $d (@{$props}) {
	if (ref($d) ne "HASH") {
	    $res .= "[$d] ";
	} else {
	    foreach my $k (keys %{$d}) {
		$res .= "[$k($d->{$k})] ";
	    }
	}
    }
}

#####################################################################
# parse a structure element
sub ParseElement($)
{
    my($element) = shift;
    (defined $element->{PROPERTIES}) && ParseProperties($element->{PROPERTIES});
    ParseType($element->{TYPE});
    $res .= " ";
    if ($element->{POINTERS}) {
	for (my($i)=0; $i < $element->{POINTERS}; $i++) {
	    $res .= "*";
	}
    }
    $res .= "$element->{NAME}";
    (defined $element->{ARRAY_LEN}) && ($res .= "[$element->{ARRAY_LEN}]");
}

#####################################################################
# parse a struct
sub ParseStruct($)
{
    my($struct) = shift;

    if (defined $struct->{ELEMENTS}) {

	# Parse scalars

	$res .= "\t/* Parse scalars */\n\n";

	foreach my $e (@{$struct->{ELEMENTS}}) {
	    if (defined $e->{POINTERS}) {
		$res .= "\tptr_$e->{NAME} = prs_$e->{TYPE}_ptr(); /* $e->{NAME} */\n";
	    } else {
		$res .= "\tprs_$e->{TYPE}(); /* $e->{NAME} */\n";
	    }
	}	

	# Parse buffers

	$res .= "\n\t/* Parse buffers */\n\n";

	foreach my $e (@{$struct->{ELEMENTS}}) {
	    $res .= "\tif (ptr_$e->{NAME})\n\t\tprs_$e->{TYPE}(); /* $e->{NAME} */\n\n",
	    if (defined $e->{POINTERS});
	}
    }
}


#####################################################################
# parse a union element
sub ParseUnionElement($)
{
    my($element) = shift;
    
#    $res .= "void prs_$element->{DATA}->{TYPE}()\n{\n";

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

    $res .= "\tcase $element->{DATA}->{NAME}: \n";
    $res .= "\t\tprs_$element->{DATA}->{TYPE}();\n\t\tbreak;\n";

#    $res .= "[case($element->{CASE})] ";
#    ParseElement($element->{DATA});
#    $res .= ";\n";
}

#####################################################################
# parse a union
sub ParseUnion($)
{
    my($union) = shift;

#    print Dumper($union);

    $res .= "\tswitch (level) {\n";

    (defined $union->{PROPERTIES}) && ParseProperties($union->{PROPERTIES});
    foreach my $e (@{$union->{DATA}}) {
	ParseUnionElement($e);
    }
    
    $res .= "\t}\n";
}

#####################################################################
# parse a type
sub ParseType($)
{
    my($data) = shift;

    print Dumper $data;

    if (ref($data) eq "HASH") {
	($data->{TYPE} eq "STRUCT") &&
	    ParseStruct($data);
	($data->{TYPE} eq "UNION") &&
	    ParseUnion($data);
    } else {
	$res .= "$data";
    }
}

#####################################################################
# parse a typedef
sub ParseTypedef($)
{
    my($typedef) = shift;

    $res .= "void prs_$typedef->{NAME}(void)\n{\n";
    ParseType($typedef->{DATA});
    $res .= "}\n\n";
}

#####################################################################
# parse a function
sub ParseFunctionArg($$)
{ 
    my($arg) = shift;
    my($io) = shift;		# "in" or "out"

    if (@{$arg->{PROPERTIES}}[0] =~ /$io/) {
	my $is_pol = 0;
	    
	# Arg is a policy handle - no pointer
	    
	foreach my $prop (@{$arg->{PROPERTIES}}) {
	    if ($prop =~ /context_handle/) {
		$res .= "\tprs_policy_hnd();";
		$is_pol = 1;
	    }
	}
	
	if (!$is_pol) {
	    if ($arg->{POINTERS}) {
		$res .= "\tptr_$arg->{NAME} = prs_ptr();\n";
		$res .= "\tif (ptr_$arg->{NAME})\n\t\tprs_$arg->{TYPE}();";
	    } else {
		$res .= "\tprs_$arg->{TYPE}();";
	    }
	}
	
	$res .= "\t/* $arg->{NAME} */\n";
    }
}
    
#####################################################################
# parse a function
sub ParseFunction($)
{ 
    my($function) = shift;

    # Input function

    $res .= "void $function->{NAME}_q(void)\n{\n";

    foreach my $arg (@{$function->{DATA}}) {
	ParseFunctionArg($arg, "in");
    }
    
    $res .= "}\n\n";
    
    # Output function

    $res .= "void $function->{NAME}_r(void)\n{\n";

    foreach my $arg (@{$function->{DATA}}) {
	ParseFunctionArg($arg, "out");
    }

    $res .= "\tprs_$function->{RETURN_TYPE}();\t/* Return value */\n";

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

}

#####################################################################
# parse the interface definitions
sub ParseInterface($)
{
    my($interface) = shift;
    my($data) = $interface->{DATA};
    foreach my $d (@{$data}) {
	($d->{TYPE} eq "TYPEDEF") &&
	    ParseTypedef($d);
	($d->{TYPE} eq "FUNCTION") && 
	    ParseFunction($d);
    }
}


#####################################################################
# parse a parsed IDL structure back into an IDL file
sub Parse($)
{
    my($idl) = shift;
    $res = "/* parser auto-generated by pidl */\n\n";
    foreach my $x (@{$idl}) {
	($x->{TYPE} eq "INTERFACE") && 
	    ParseInterface($x);
    }
    return $res;
}

1;