r19049: Samba 3 client fixes
[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 "";
56         pidl "if (DEBUGLEVEL >= 10)";
57         pidl "\tNDR_PRINT_IN_DEBUG($fn->{NAME}, &r);";
58         pidl "";
59         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});";
60         pidl "";
61
62         pidl "if ( !NT_STATUS_IS_OK(status) ) {";
63         indent;
64         pidl "return status;";
65         deindent;
66         pidl "}";
67
68         pidl "";
69         pidl "if (DEBUGLEVEL >= 10)";
70         pidl "\tNDR_PRINT_OUT_DEBUG($fn->{NAME}, &r);";
71         pidl "";
72         pidl "if (NT_STATUS_IS_ERR(status)) {";
73         pidl "\treturn status;";
74         pidl "}";
75         pidl "";
76         pidl "/* Return variables */";
77         foreach my $e (@{$fn->{ELEMENTS}}) {
78                 next unless (grep(/out/, @{$e->{DIRECTION}}));
79
80                 fatal($e, "[out] argument is not a pointer or array") if ($e->{LEVELS}[0]->{TYPE} ne "POINTER" and $e->{LEVELS}[0]->{TYPE} ne "ARRAY");
81
82                 if ( ($e->{LEVELS}[0]->{TYPE} eq "POINTER") && ($e->{LEVELS}[0]->{POINTER_TYPE} eq "unique") ) {
83                         pidl "if ( $e->{NAME} ) {";
84                         indent;
85                         pidl "*$e->{NAME} = *r.out.$e->{NAME};";
86                         deindent;
87                         pidl "}";
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;