r15591: Generate function calls correctly as well.
[samba.git] / source4 / 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 strict;
10 use Parse::Pidl::Typelist qw(hasType getType mapType scalar_is_reference);
11 use Parse::Pidl::Util qw(has_property ParseExpr is_constant);
12 use Parse::Pidl::NDR qw(GetPrevLevel GetNextLevel ContainsDeferred);
13 use Parse::Pidl::Samba4 qw(DeclLong);
14
15 use vars qw($VERSION);
16 $VERSION = '0.01';
17
18 my $res;
19 my $res_hdr;
20 my $tabs = "";
21 sub indent() { $tabs.="\t"; }
22 sub deindent() { $tabs = substr($tabs, 1); }
23 sub pidl($) { $res .= $tabs.(shift)."\n"; }
24 sub pidl_hdr($) { $res_hdr .= (shift)."\n"; }
25 sub fatal($$) { my ($e,$s) = @_; die("$e->{ORIGINAL}->{FILE}:$e->{ORIGINAL}->{LINE}: $s\n"); }
26 sub warning($$) { my ($e,$s) = @_; warn("$e->{ORIGINAL}->{FILE}:$e->{ORIGINAL}->{LINE}: $s\n"); }
27 sub fn_declare($) { my ($n) = @_; pidl $n; pidl_hdr "$n;"; }
28
29 sub ParseFunction($$)
30 {
31         my ($if,$fn) = @_;
32
33         my $inargs = "";
34         my $defargs = "";
35         my $uif = uc($if->{NAME});
36         my $ufn = "DCERPC_".uc($fn->{NAME});
37
38         foreach (@{$fn->{ELEMENTS}}) {
39                 $defargs .= ", " . DeclLong($_);
40         }
41         fn_declare "NTSTATUS rpccli_$fn->{NAME}(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx$defargs)";
42         pidl "{";
43         indent;
44         pidl "struct $fn->{NAME} r;";
45         pidl "NTSTATUS status;";
46         pidl "";
47         pidl "/* In parameters */";
48
49         foreach (@{$fn->{ELEMENTS}}) {
50                 if (grep(/in/, @{$_->{DIRECTION}})) {
51                         pidl "r.in.$_->{NAME} = $_->{NAME};";
52                 } 
53         }
54
55         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});";
56         pidl "if (NT_STATUS_IS_ERR(status)) {";
57         pidl "\treturn status;";
58         pidl "}";
59         pidl "";
60         pidl "/* Return variables */";
61         foreach my $e (@{$fn->{ELEMENTS}}) {
62                 next unless (grep(/out/, @{$e->{DIRECTION}}));
63
64                 fatal($e, "[out] argument is not a pointer") if ($e->{LEVELS}[0]->{TYPE} ne "POINTER");
65
66                 pidl "*$e->{NAME} = *r.out.$e->{NAME};";
67         }
68
69         pidl"";
70         pidl "/* Return result */";
71         if (not $fn->{RETURN_TYPE}) {
72                 pidl "return NT_STATUS_OK;";
73         } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") {
74                 pidl "return r.out.result;";
75         } elsif ($fn->{RETURN_TYPE} eq "WERROR") {
76                 pidl "return werror_to_ntstatus(r.out.result);";
77         } else {
78                 pidl "/* Sorry, don't know how to convert $fn->{RETURN_TYPE} to NTSTATUS */";
79                 pidl "return NT_STATUS_OK;";
80         }
81
82         deindent;
83         pidl "}";
84         pidl "";
85 }
86
87 sub ParseInterface($)
88 {
89         my $if = shift;
90
91         my $uif = uc($if->{NAME});
92
93         pidl_hdr "#ifndef __CLI_$uif\__";
94         pidl_hdr "#define __CLI_$uif\__";
95         ParseFunction($if, $_) foreach (@{$if->{FUNCTIONS}});
96         pidl_hdr "#endif /* __CLI_$uif\__ */";
97 }
98
99 sub Parse($$$)
100 {
101         my($ndr,$header,$ndr_header) = @_;
102
103         $res = "";
104         $res_hdr = "";
105
106         pidl "/*";
107         pidl " * Unix SMB/CIFS implementation.";
108         pidl " * client auto-generated by pidl. DO NOT MODIFY!";
109         pidl " */";
110         pidl "";
111         pidl "#include \"includes.h\"";
112         pidl "#include \"$header\"";
113         pidl_hdr "#include \"$ndr_header\"";
114         pidl "";
115         
116         foreach (@$ndr) {
117                 ParseInterface($_) if ($_->{TYPE} eq "INTERFACE");
118         }
119
120         return ($res, $res_hdr);
121 }
122
123 1;