summaryrefslogtreecommitdiff
path: root/source4/pidl/lib/Parse/Pidl/Samba3/Header.pm
blob: 1e0e2c72708d61ea0594d89769e115dd4aa8c300 (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
###################################################
# Samba3 NDR header generator for IDL structures
# Copyright jelmer@samba.org 2005
# released under the GNU GPL

package Parse::Pidl::Samba3::Header;

use strict;
use Parse::Pidl::Typelist qw(hasType getType);
use Parse::Pidl::Util qw(has_property ParseExpr);
use Parse::Pidl::NDR qw(GetPrevLevel GetNextLevel ContainsDeferred);
use Parse::Pidl::Samba3::Types qw(DeclShort);

use vars qw($VERSION);
$VERSION = '0.01';

my $res = "";
sub pidl($) { my $x = shift; $res .= "$x\n"; }
sub fatal($$) { my ($e,$s) = @_; die("$e->{FILE}:$e->{LINE}: $s\n"); }
sub warning($$) { my ($e,$s) = @_; warn("$e->{FILE}:$e->{LINE}: $s\n"); }

sub ParseElement($)
{
	my $e = shift;

	foreach my $l (@{$e->{LEVELS}}) {
		if ($l->{TYPE} eq "POINTER") {
			return if ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "top");
			pidl "\tuint32 ptr_$e->{NAME};";
		} elsif ($l->{TYPE} eq "SWITCH") {
			pidl "\tuint32 level_$e->{NAME};";
		} elsif ($l->{TYPE} eq "DATA") {
			pidl "\t" . DeclShort($e) . ";";
		}
	}
}

sub CreateStruct($$$$)
{
	my ($if,$fn,$n,$t) = @_;

	pidl "typedef struct $n {";
	ParseElement($_) foreach (@$t);

	if (not @$t) {
		# Some compilers don't like empty structs
		pidl "\tuint32 dummy;";
	}

	pidl "} " . uc($n) . ";";
	pidl "";
}

sub ParseFunction($$)
{
	my ($if,$fn) = @_;

	my @in = ();
	my @out = ();

	foreach (@{$fn->{ELEMENTS}}) {
		push (@in, $_) if (grep(/in/, @{$_->{DIRECTION}}));
		push (@out, $_) if (grep(/out/, @{$_->{DIRECTION}}));
	}

	if (defined($fn->{RETURN_TYPE})) {
		push (@out, { 
			NAME => "status", 
			TYPE => $fn->{RETURN_TYPE},
			LEVELS => [
				{
					TYPE => "DATA",
					DATA_TYPE => $fn->{RETURN_TYPE}
				}
			]
		} );
	}

	#  define Q + R structures for functions

	CreateStruct($if, $fn, "$if->{NAME}_q_$fn->{NAME}", \@in);
	CreateStruct($if, $fn, "$if->{NAME}_r_$fn->{NAME}", \@out);
}

sub ParseStruct($$$)
{
	my ($if,$s,$n) = @_;

	CreateStruct($if, $s, "$if->{NAME}_$n", $s->{ELEMENTS});
}

sub ParseUnion($$$)
{
	my ($if,$u,$n) = @_;

	pidl "typedef union $if->{NAME}_$n {";
	#FIXME: What about elements that require more then one variable?
	ParseElement($_) foreach (@{$u->{ELEMENTS}});
	pidl "} ".uc($if->{NAME}."_".$n) .";";
	pidl "";
}

sub ParseEnum($$$)
{
	my ($if,$s,$n) = @_;

	pidl "typedef enum {";
	pidl "$_," foreach (@{$s->{ELEMENTS}});
	pidl "} $n;";
}

sub ParseBitmap($$$)
{
	my ($if,$s,$n) = @_;

	pidl "#define $_" foreach (@{$s->{ELEMENTS}});
}

sub ParseInterface($)
{
	my $if = shift;

	my $def = "_RPC_" . uc($if->{NAME}) . "_H";

	pidl "";

	pidl "\#ifndef $def";
	pidl "\#define $def";

	pidl "";
	
	foreach (@{$if->{FUNCTIONS}}) {
		pidl "\#define " . uc($_->{NAME}) . " $_->{OPNUM}" ;
	}

	pidl "";

	foreach (@{$if->{TYPEDEFS}}) {
		ParseStruct($if, $_->{DATA}, $_->{NAME}) if ($_->{DATA}->{TYPE} eq "STRUCT");
		ParseEnum($if, $_->{DATA}, $_->{NAME}) if ($_->{DATA}->{TYPE} eq "ENUM");
		ParseBitmap($if, $_->{DATA}, $_->{NAME}) if ($_->{DATA}->{TYPE} eq "BITMAP");
		ParseUnion($if, $_->{DATA}, $_->{NAME}) if ($_->{DATA}->{TYPE} eq "UNION");
	}

	ParseFunction($if, $_) foreach (@{$if->{FUNCTIONS}});

	foreach (@{$if->{CONSTS}}) {
		pidl "$_->{NAME} ($_->{VALUE})";
	}

	pidl "\#endif /* $def */";
}

sub Parse($$)
{
	my($ndr,$filename) = @_;

	$res = "";

	pidl "/*";
	pidl " * Unix SMB/CIFS implementation.";
	pidl " * header auto-generated by pidl. DO NOT MODIFY!";
	pidl " */";
	pidl "";

	# Loop over interfaces
	foreach (@{$ndr}) {
		ParseInterface($_) if ($_->{TYPE} eq "INTERFACE");
	}
	return $res;
}

1;