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