3f9d1f0464aa2c9906eb7708602a24ff7644963a
[ira/wip.git] / source / pidl / lib / Parse / Pidl / Samba3 / ClientNDR.pm
1 ###################################################
2 # Samba3 client generator for IDL structures
3 # on top of Samba4 style NDR functions
4 # Copyright jelmer@samba.org 2005-2006
5 # released under the GNU GPL
6
7 package Parse::Pidl::Samba3::ClientNDR;
8
9 use Exporter;
10 @ISA = qw(Exporter);
11 @EXPORT_OK = qw(GenerateFunctionInEnv ParseFunction $res $res_hdr);
12
13 use strict;
14 use Parse::Pidl qw(fatal warning);
15 use Parse::Pidl::Typelist qw(hasType getType mapTypeName scalar_is_reference);
16 use Parse::Pidl::Util qw(has_property is_constant ParseExpr);
17 use Parse::Pidl::NDR qw(GetPrevLevel GetNextLevel ContainsDeferred);
18 use Parse::Pidl::Samba4 qw(DeclLong);
19 use Parse::Pidl::Samba4::NDR::Parser qw(GenerateFunctionInEnv);
20
21 use vars qw($VERSION);
22 $VERSION = '0.01';
23
24 sub indent($) { my ($self) = @_; $self->{tabs}.="\t"; }
25 sub deindent($) { my ($self) = @_; $self->{tabs} = substr($self->{tabs}, 1); }
26 sub pidl($$) { my ($self,$txt) = @_; $self->{res} .= "$self->{tabs}$txt\n"; }
27 sub pidl_hdr($$) { my ($self, $txt) = @_; $self->{res_hdr} .= "$txt\n"; } 
28 sub fn_declare($$) { my ($self,$n) = @_; $self->pidl($n); $self->pidl_hdr("$n;"); }
29
30 sub new($)
31 {
32         my ($class) = shift;
33         my $self = { res => "", res_hdr => "", tabs => "" };
34         bless($self, $class);
35 }
36
37 sub ParseFunction($$$)
38 {
39         my ($self, $uif, $fn) = @_;
40
41         my $inargs = "";
42         my $defargs = "";
43         my $ufn = "DCERPC_".uc($fn->{NAME});
44
45         foreach (@{$fn->{ELEMENTS}}) {
46                 $defargs .= ", " . DeclLong($_);
47         }
48         $self->fn_declare("NTSTATUS rpccli_$fn->{NAME}(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx$defargs)");
49         $self->pidl("{");
50         $self->indent;
51         $self->pidl("struct $fn->{NAME} r;");
52         $self->pidl("NTSTATUS status;");
53         $self->pidl("");
54         $self->pidl("/* In parameters */");
55
56         foreach (@{$fn->{ELEMENTS}}) {
57                 if (grep(/in/, @{$_->{DIRECTION}})) {
58                         $self->pidl("r.in.$_->{NAME} = $_->{NAME};");
59                 } 
60         }
61
62         $self->pidl("");
63         $self->pidl("if (DEBUGLEVEL >= 10)");
64         $self->pidl("\tNDR_PRINT_IN_DEBUG($fn->{NAME}, &r);");
65         $self->pidl("");
66         $self->pidl("status = cli_do_rpc_ndr(cli, mem_ctx, PI_$uif, $ufn, &r, (ndr_pull_flags_fn_t)ndr_pull_$fn->{NAME}, (ndr_push_flags_fn_t)ndr_push_$fn->{NAME});");
67         $self->pidl("");
68
69         $self->pidl("if (!NT_STATUS_IS_OK(status)) {");
70         $self->indent;
71         $self->pidl("return status;");
72         $self->deindent;
73         $self->pidl("}");
74
75         $self->pidl("");
76         $self->pidl("if (DEBUGLEVEL >= 10)");
77         $self->pidl("\tNDR_PRINT_OUT_DEBUG($fn->{NAME}, &r);");
78         $self->pidl("");
79         $self->pidl("if (NT_STATUS_IS_ERR(status)) {");
80         $self->pidl("\treturn status;");
81         $self->pidl("}");
82         $self->pidl("");
83         $self->pidl("/* Return variables */");
84         foreach my $e (@{$fn->{ELEMENTS}}) {
85                 next unless (grep(/out/, @{$e->{DIRECTION}}));
86                 my $level = 0;
87
88                 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");
89
90                 if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") {
91                         $level = 1;
92                         if ($e->{LEVELS}[0]->{POINTER_TYPE} ne "ref") {
93                                 $self->pidl("if ( $e->{NAME} ) {");
94                                 $self->indent;
95                         }
96                 }
97
98                 if ($e->{LEVELS}[$level]->{TYPE} eq "ARRAY") {
99                         # This is a call to GenerateFunctionInEnv intentionally. 
100                         # Since the data is being copied into a user-provided data 
101                         # structure, the user should be able to know the size beforehand 
102                         # to allocate a structure of the right size.
103                         my $env = GenerateFunctionInEnv($fn, "r.");
104                         my $size_is = ParseExpr($e->{LEVELS}[$level]->{SIZE_IS}, $env, $e->{ORIGINAL});
105                         $self->pidl("memcpy($e->{NAME}, r.out.$e->{NAME}, $size_is);");
106                 } else {
107                         $self->pidl("*$e->{NAME} = *r.out.$e->{NAME};");
108                 }
109
110                 if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") {
111                         if ($e->{LEVELS}[0]->{POINTER_TYPE} ne "ref") {
112                                 $self->deindent;
113                                 $self->pidl("}");
114                         }
115                 }
116         }
117
118         $self->pidl("");
119         $self->pidl("/* Return result */");
120         if (not $fn->{RETURN_TYPE}) {
121                 $self->pidl("return NT_STATUS_OK;");
122         } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") {
123                 $self->pidl("return r.out.result;");
124         } elsif ($fn->{RETURN_TYPE} eq "WERROR") {
125                 $self->pidl("return werror_to_ntstatus(r.out.result);");
126         } else {
127                 warning($fn->{ORIGINAL}, "Unable to convert $fn->{RETURN_TYPE} to NTSTATUS");
128                 $self->pidl("return NT_STATUS_OK;");
129         }
130
131         $self->deindent;
132         $self->pidl("}");
133         $self->pidl("");
134 }
135
136 sub ParseInterface($$)
137 {
138         my ($self, $if) = @_;
139
140         my $uif = uc($if->{NAME});
141
142         $self->pidl_hdr("#ifndef __CLI_$uif\__");
143         $self->pidl_hdr("#define __CLI_$uif\__");
144         $self->ParseFunction(uc($if->{NAME}), $_) foreach (@{$if->{FUNCTIONS}});
145         $self->pidl_hdr("#endif /* __CLI_$uif\__ */");
146 }
147
148 sub Parse($$$$)
149 {
150         my($self,$ndr,$header,$ndr_header) = @_;
151
152         $self->pidl("/*");
153         $self->pidl(" * Unix SMB/CIFS implementation.");
154         $self->pidl(" * client auto-generated by pidl. DO NOT MODIFY!");
155         $self->pidl(" */");
156         $self->pidl("");
157         $self->pidl("#include \"includes.h\"");
158         $self->pidl("#include \"$header\"");
159         $self->pidl_hdr("#include \"$ndr_header\"");
160         $self->pidl("");
161         
162         foreach (@$ndr) {
163                 $self->ParseInterface($_) if ($_->{TYPE} eq "INTERFACE");
164         }
165
166         return ($self->{res}, $self->{res_hdr});
167 }
168
169 1;