r24557: rename 'dcerpc_table_' -> 'ndr_table_'
[jelmer/samba4-debian.git] / source / pidl / lib / Parse / Pidl / Samba4 / NDR / Client.pm
1 ###################################################
2 # client calls generator
3 # Copyright tridge@samba.org 2003
4 # Copyright jelmer@samba.org 2005-2006
5 # released under the GNU GPL
6
7 package Parse::Pidl::Samba4::NDR::Client;
8
9 use Parse::Pidl::Samba4 qw(choose_header is_intree);
10
11 use vars qw($VERSION);
12 $VERSION = '0.01';
13
14 use strict;
15
16 my($res,$res_hdr);
17
18 #####################################################################
19 # parse a function
20 sub ParseFunction($$)
21 {
22         my ($interface, $fn) = @_;
23         my $name = $fn->{NAME};
24         my $uname = uc $name;
25
26         $res_hdr .= "\nstruct rpc_request *dcerpc_$name\_send(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct $name *r);
27 NTSTATUS dcerpc_$name(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct $name *r);
28 ";
29
30         $res .= "
31 struct rpc_request *dcerpc_$name\_send(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct $name *r)
32 {
33         if (p->conn->flags & DCERPC_DEBUG_PRINT_IN) {
34                 NDR_PRINT_IN_DEBUG($name, r);
35         }
36         
37         return dcerpc_ndr_request_send(p, NULL, &ndr_table_$interface->{NAME}, DCERPC_$uname, mem_ctx, r);
38 }
39
40 NTSTATUS dcerpc_$name(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct $name *r)
41 {
42         struct rpc_request *req;
43         NTSTATUS status;
44         
45         req = dcerpc_$name\_send(p, mem_ctx, r);
46         if (req == NULL) return NT_STATUS_NO_MEMORY;
47
48         status = dcerpc_ndr_request_recv(req);
49
50         if (NT_STATUS_IS_OK(status) && (p->conn->flags & DCERPC_DEBUG_PRINT_OUT)) {
51                 NDR_PRINT_OUT_DEBUG($name, r);          
52         }
53 ";
54     
55         if (defined($fn->{RETURN_TYPE}) and $fn->{RETURN_TYPE} eq "NTSTATUS") {
56              $res .= "\tif (NT_STATUS_IS_OK(status)) status = r->out.result;\n";
57         }
58         $res .= 
59 "
60         return status;
61 }
62 ";
63 }
64
65 my %done;
66
67 #####################################################################
68 # parse the interface definitions
69 sub ParseInterface($)
70 {
71         my($interface) = shift;
72
73         $res_hdr .= "#ifndef _HEADER_RPC_$interface->{NAME}\n";
74         $res_hdr .= "#define _HEADER_RPC_$interface->{NAME}\n\n";
75
76         if (defined $interface->{PROPERTIES}->{uuid}) {
77                 $res_hdr .= "extern const struct ndr_interface_table ndr_table_$interface->{NAME};\n";
78         }
79
80         $res .= "/* $interface->{NAME} - client functions generated by pidl */\n\n";
81
82         foreach my $fn (@{$interface->{FUNCTIONS}}) {
83                 next if not defined($fn->{OPNUM});
84                 next if defined($done{$fn->{NAME}});
85                 ParseFunction($interface, $fn);
86                 $done{$fn->{NAME}} = 1;
87         }
88
89         $res_hdr .= "#endif /* _HEADER_RPC_$interface->{NAME} */\n";
90
91         return $res;
92 }
93
94 sub Parse($$$$)
95 {
96         my($ndr,$header,$ndr_header,$client_header) = @_;
97
98         $res = "";
99         $res_hdr = "";
100
101         $res .= "/* client functions auto-generated by pidl */\n";
102         $res .= "\n";
103         if (is_intree()) {
104                 $res .= "#include \"includes.h\"\n";
105         } else {
106                 $res .= "#define _GNU_SOURCE\n";
107                 $res .= "#include <stdio.h>\n";
108                 $res .= "#include <stdbool.h>\n";
109                 $res .= "#include <stdlib.h>\n";
110                 $res .= "#include <stdint.h>\n";
111                 $res .= "#include <stdarg.h>\n";
112                 $res .= "#include <core/nterr.h>\n";
113         }
114         $res .= "#include \"$ndr_header\"\n";
115         $res .= "#include \"$client_header\"\n";
116         $res .= "\n";
117
118         $res_hdr .= choose_header("librpc/rpc/dcerpc.h", "dcerpc.h")."\n";
119         $res_hdr .= "#include \"$header\"\n";
120
121         foreach my $x (@{$ndr}) {
122                 ($x->{TYPE} eq "INTERFACE") && ParseInterface($x);
123         }
124
125         return ($res,$res_hdr);
126 }
127
128 1;