r15260: Don't dereference NULL pointers to obtain array lengths - found by
[samba.git] / source / pidl / lib / Parse / Pidl / Samba3 / Server.pm
1 ###################################################
2 # Samba3 NDR server generator for IDL structures
3 # Copyright jelmer@samba.org 2005
4 # released under the GNU GPL
5
6 package Parse::Pidl::Samba3::Server;
7
8 use strict;
9 use Parse::Pidl::Typelist qw(hasType getType mapType);
10 use Parse::Pidl::Util qw(has_property ParseExpr);
11 use Parse::Pidl::NDR qw(GetPrevLevel GetNextLevel ContainsDeferred);
12
13 my $res = "";
14 my $tabs = "";
15
16 sub indent() { $tabs.="\t"; }
17 sub deindent() { $tabs = substr($tabs, 1); }
18 sub pidl($) { $res .= $tabs.(shift)."\n"; }
19
20 use vars qw($VERSION);
21 $VERSION = '0.01';
22
23 sub ParseFunction($$)
24 {
25         my ($if,$fn) = @_;
26
27         pidl "/******************************************************************";
28         pidl " api_$fn->{NAME}";
29         pidl " *****************************************************************/";
30         pidl "";
31         pidl "static BOOL api_$fn->{NAME}(pipes_struct *p)";
32         pidl "{";
33         indent;
34         pidl uc("$if->{NAME}_q_$fn->{NAME}") . " q_u;";
35         pidl uc("$if->{NAME}_r_$fn->{NAME}") . " r_u;";
36         pidl "prs_struct *data = &p->in_data.data;";
37         pidl "prs_struct *rdata = &p->out_data.rdata;";
38         pidl "";
39         pidl "ZERO_STRUCT(q_u);";
40         pidl "ZERO_STRUCT(r_u);";
41         pidl "";
42         pidl "if (!$if->{NAME}_io_q_$fn->{NAME}(\"\", &q_u, data, 0))";
43         pidl "\treturn False;";
44         pidl "";
45         if ($fn->{RETURN_TYPE}) {
46                 pidl "r_u.status = _$fn->{NAME}(p, &q_u, &r_u);";
47         } else {
48                 pidl "_$fn->{NAME}(p, &q_u, &r_u);";
49         }
50         pidl "";
51         pidl "if (!$if->{NAME}_io_r_$fn->{NAME}(\"\", &r_u, rdata, 0))";
52         pidl "\treturn False;";
53         pidl "";
54         pidl "return True;";
55         deindent;
56         pidl "}";
57 }
58
59 sub ParseInterface($)
60 {
61         my $if = shift;
62
63         ParseFunction($if, $_) foreach (@{$if->{FUNCTIONS}});
64         
65         pidl "";
66         pidl "/* Tables */";
67         pidl "static struct api_struct api_$if->{NAME}_cmds[] = ";
68         pidl "{";
69         indent;
70         foreach (@{$if->{FUNCTIONS}}) {
71                 pidl "{\"" . uc($_->{NAME}) . "\", " . uc($_->{NAME}) . ", api_$_->{NAME}},";
72         }
73         deindent;
74         pidl "};";
75
76         pidl "";
77         
78         pidl "void $if->{NAME}_get_pipe_fns(struct api_struct **fns, int *n_fns)";
79         pidl "{";
80         indent;
81         pidl "*fns = api_$if->{NAME}_cmds;";
82         pidl "*n_fns = sizeof(api_$if->{NAME}_cmds) / sizeof(struct api_struct);";
83         deindent;
84         pidl "}";
85
86         pidl "";
87
88         pidl "NTSTATUS rpc_$if->{NAME}_init(void)";
89         pidl "{";
90         indent;
91         pidl "return rpc_pipe_register_commands(SMB_RPC_INTERFACE_VERSION, \"$if->{NAME}\", \"$if->{NAME}\", api_$if->{NAME}_cmds, sizeof(api_$if->{NAME}_cmds) / sizeof(struct api_struct));";
92         deindent;
93         pidl "}";
94 }
95
96 sub Parse($$)
97 {
98         my($ndr,$filename) = @_;
99
100         $tabs = "";
101         $res = "";
102
103         pidl "/*";
104         pidl " * Unix SMB/CIFS implementation.";
105         pidl " * server auto-generated by pidl. DO NOT MODIFY!";
106         pidl " */";
107         pidl "";
108         pidl "#include \"includes.h\"";
109         pidl "#include \"nterr.h\"";
110         pidl "";
111         pidl "#undef DBGC_CLASS";
112         pidl "#define DBGC_CLASS DBGC_RPC";
113         pidl "";
114
115         foreach (@$ndr) {
116                 ParseInterface($_) if ($_->{TYPE} eq "INTERFACE");
117         }
118
119         return $res;
120 }
121
122 1;