1 ###################################################
2 # Samba3 client generator for IDL structures
3 # on top of Samba4 style NDR functions
4 # Copyright jelmer@samba.org 2005-2006
5 # Copyright gd@samba.org 2008
6 # released under the GNU GPL
8 package Parse::Pidl::Samba3::ClientNDR;
12 @EXPORT_OK = qw(ParseFunction $res $res_hdr ParseOutputArgument);
15 use Parse::Pidl qw(fatal warning);
16 use Parse::Pidl::Util qw(has_property ParseExpr);
17 use Parse::Pidl::Samba4 qw(DeclLong);
18 use Parse::Pidl::Samba4::Header qw(GenerateFunctionInEnv);
20 use vars qw($VERSION);
23 sub indent($) { my ($self) = @_; $self->{tabs}.="\t"; }
24 sub deindent($) { my ($self) = @_; $self->{tabs} = substr($self->{tabs}, 1); }
25 sub pidl($$) { my ($self,$txt) = @_; $self->{res} .= $txt ? "$self->{tabs}$txt\n" : "\n"; }
26 sub pidl_hdr($$) { my ($self, $txt) = @_; $self->{res_hdr} .= "$txt\n"; }
27 sub fn_declare($$) { my ($self,$n) = @_; $self->pidl($n); $self->pidl_hdr("$n;"); }
32 my $nt = int((length($s)+1)/8);
34 my $ns = (length($s)-$lt);
35 return "\t"x($nt)." "x($ns);
41 my $self = { res => "", res_hdr => "", tabs => "" };
45 sub ElementDirection($)
49 return "[in,out]" if (has_property($e, "in") and has_property($e, "out"));
50 return "[in]" if (has_property($e, "in"));
51 return "[out]" if (has_property($e, "out"));
55 sub HeaderProperties($$)
57 my($props,$ignores) = @_;
60 foreach my $d (keys %{$props}) {
61 next if (grep(/^$d$/, @$ignores));
62 if($props->{$d} ne "1") {
63 $ret.= "$d($props->{$d}),";
70 return "[" . substr($ret, 0, -1) . "]";
74 sub ParseOutputArgument($$$)
76 my ($self, $fn, $e) = @_;
79 fatal($e->{ORIGINAL}, "[out] argument is not a pointer or array") if ($e->{LEVELS}[0]->{TYPE} ne "POINTER" and $e->{LEVELS}[0]->{TYPE} ne "ARRAY");
81 if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") {
83 if ($e->{LEVELS}[0]->{POINTER_TYPE} ne "ref") {
84 $self->pidl("if ($e->{NAME} && r.out.$e->{NAME}) {");
89 if ($e->{LEVELS}[$level]->{TYPE} eq "ARRAY") {
90 # This is a call to GenerateFunctionInEnv intentionally.
91 # Since the data is being copied into a user-provided data
92 # structure, the user should be able to know the size beforehand
93 # to allocate a structure of the right size.
94 my $env = GenerateFunctionInEnv($fn, "r.");
95 my $size_is = ParseExpr($e->{LEVELS}[$level]->{SIZE_IS}, $env, $e->{ORIGINAL});
96 $self->pidl("memcpy($e->{NAME}, r.out.$e->{NAME}, $size_is * sizeof(*$e->{NAME}));");
98 $self->pidl("*$e->{NAME} = *r.out.$e->{NAME};");
101 if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") {
102 if ($e->{LEVELS}[0]->{POINTER_TYPE} ne "ref") {
109 sub ParseFunction($$$)
111 my ($self, $if, $fn) = @_;
115 my $ufn = "NDR_".uc($fn->{NAME});
116 my $fn_str = "NTSTATUS rpccli_$fn->{NAME}";
117 my $pad = genpad($fn_str);
119 $fn_args .= "struct rpc_pipe_client *cli,\n" . $pad . "TALLOC_CTX *mem_ctx";
121 foreach (@{$fn->{ELEMENTS}}) {
122 my $dir = ElementDirection($_);
123 my $prop = HeaderProperties($_->{PROPERTIES}, ["in", "out"]);
124 $fn_args .= ",\n" . $pad . DeclLong($_) . " /* $dir $prop */";
127 if (defined($fn->{RETURN_TYPE}) && ($fn->{RETURN_TYPE} eq "WERROR")) {
128 $fn_args .= ",\n" . $pad . "WERROR *werror";
131 $self->fn_declare("$fn_str($fn_args)");
134 $self->pidl("struct $fn->{NAME} r;");
135 $self->pidl("NTSTATUS status;");
137 $self->pidl("/* In parameters */");
139 foreach (@{$fn->{ELEMENTS}}) {
140 if (grep(/in/, @{$_->{DIRECTION}})) {
141 $self->pidl("r.in.$_->{NAME} = $_->{NAME};");
146 $self->pidl("if (DEBUGLEVEL >= 10) {");
148 $self->pidl("NDR_PRINT_IN_DEBUG($fn->{NAME}, &r);");
152 $self->pidl("status = cli_do_rpc_ndr(cli,");
153 $self->pidl("\t\t\tmem_ctx,");
154 $self->pidl("\t\t\t&ndr_table_$if,");
155 $self->pidl("\t\t\t$ufn,");
156 $self->pidl("\t\t\t&r);");
159 $self->pidl("if (!NT_STATUS_IS_OK(status)) {");
161 $self->pidl("return status;");
166 $self->pidl("if (DEBUGLEVEL >= 10) {");
168 $self->pidl("NDR_PRINT_OUT_DEBUG($fn->{NAME}, &r);");
172 $self->pidl("if (NT_STATUS_IS_ERR(status)) {");
174 $self->pidl("return status;");
178 $self->pidl("/* Return variables */");
179 foreach my $e (@{$fn->{ELEMENTS}}) {
180 next unless (grep(/out/, @{$e->{DIRECTION}}));
182 $self->ParseOutputArgument($fn, $e);
187 $self->pidl("/* Return result */");
188 if (not $fn->{RETURN_TYPE}) {
189 $self->pidl("return NT_STATUS_OK;");
190 } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") {
191 $self->pidl("return r.out.result;");
192 } elsif ($fn->{RETURN_TYPE} eq "WERROR") {
193 $self->pidl("if (werror) {");
195 $self->pidl("*werror = r.out.result;");
199 $self->pidl("return werror_to_ntstatus(r.out.result);");
201 warning($fn->{ORIGINAL}, "Unable to convert $fn->{RETURN_TYPE} to NTSTATUS");
202 $self->pidl("return NT_STATUS_OK;");
210 sub ParseInterface($$)
212 my ($self, $if) = @_;
214 my $uif = uc($if->{NAME});
216 $self->pidl_hdr("#ifndef __CLI_$uif\__");
217 $self->pidl_hdr("#define __CLI_$uif\__");
218 $self->ParseFunction($if->{NAME}, $_) foreach (@{$if->{FUNCTIONS}});
219 $self->pidl_hdr("#endif /* __CLI_$uif\__ */");
224 my($self,$ndr,$header,$ndr_header) = @_;
227 $self->pidl(" * Unix SMB/CIFS implementation.");
228 $self->pidl(" * client auto-generated by pidl. DO NOT MODIFY!");
231 $self->pidl("#include \"includes.h\"");
232 $self->pidl("#include \"$header\"");
233 $self->pidl_hdr("#include \"$ndr_header\"");
237 $self->ParseInterface($_) if ($_->{TYPE} eq "INTERFACE");
240 return ($self->{res}, $self->{res_hdr});