]> git.decadent.org.uk Git - nfs-utils.git/blob - tools/rpcgen/rpc_clntout.c
rpc.statd: Fix socket binding loop.
[nfs-utils.git] / tools / rpcgen / rpc_clntout.c
1 /*
2  * Copyright (c) 2009, Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * - Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  *   this list of conditions and the following disclaimer in the documentation
11  *   and/or other materials provided with the distribution.
12  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13  *   contributors may be used to endorse or promote products derived
14  *   from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #if 0
30 static char sccsid[] = "@(#)rpc_clntout.c 1.11 89/02/22 (C) 1987 SMI";
31 #endif
32
33 /*
34  * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler
35  * Copyright (C) 1987, Sun Microsytsems, Inc.
36  */
37 #include <stdio.h>
38 #include <string.h>
39 #include <rpc/types.h>
40 #include "rpc_parse.h"
41 #include "rpc_util.h"
42 #include "rpc_output.h"
43
44 /* extern pdeclaration(); */
45 /* void printarglist(); */
46
47 #define DEFAULT_TIMEOUT 25      /* in seconds */
48 static char RESULT[] = "clnt_res";
49
50 static void     write_program(definition *def);
51 static void     printbody(proc_list *proc);
52
53
54 void
55 write_stubs(void)
56 {
57         list *l;
58         definition *def;
59
60         f_print(fout, 
61                 "\n/* Default timeout can be changed using clnt_control() */\n");
62         f_print(fout, "static struct timeval TIMEOUT = { %d, 0 };\n",
63                 DEFAULT_TIMEOUT);
64         for (l = defined; l != NULL; l = l->next) {
65                 def = (definition *) l->val;
66                 if (def->def_kind == DEF_PROGRAM) {
67                         write_program(def);
68                 }
69         }
70 }
71
72 static void
73 write_program(definition *def)
74 {
75         version_list   *vp;
76         proc_list      *proc;
77
78         for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
79                 for (proc = vp->procs; proc != NULL; proc = proc->next) {
80                         f_print(fout, "\n");
81                         ptype(proc->res_prefix, proc->res_type, 1);
82                         f_print(fout, "*\n");
83                         pvname(proc->proc_name, vp->vers_num);
84                         printarglist(proc, "clnt", "CLIENT *");
85                         f_print(fout, "{\n");
86                         printbody(proc);
87                         f_print(fout, "}\n");
88                 }
89         }
90 }
91
92 /*
93  * Writes out declarations of procedure's argument list.
94  * In either ANSI C style, in one of old rpcgen style (pass by reference),
95  * or new rpcgen style (multiple arguments, pass by value);
96  */
97
98 /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */
99
100 void
101 printarglist(proc_list *proc, char *addargname, char *addargtype)
102 {
103
104         decl_list      *l;
105
106         if (!newstyle) {        /* old style: always pass arg by reference */
107                 if (Cflag) {    /* C++ style heading */
108                         f_print(fout, "(");
109                         ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
110                         f_print(fout, "*argp, %s%s)\n", addargtype, addargname);
111                 } else {
112                         f_print(fout, "(argp, %s)\n", addargname);
113                         f_print(fout, "\t");
114                         ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
115                         f_print(fout, "*argp;\n");
116                 }
117         } else if (streq(proc->args.decls->decl.type, "void")) {
118                 /* newstyle, 0 argument */
119                 if (Cflag)
120                         f_print(fout, "(%s%s)\n", addargtype, addargname);
121                 else
122                         f_print(fout, "(%s)\n", addargname);
123         } else {
124                 /* new style, 1 or multiple arguments */
125                 if (!Cflag) {
126                         f_print(fout, "(");
127                         for (l = proc->args.decls; l != NULL; l = l->next)
128                                 f_print(fout, "%s, ", l->decl.name);
129                         f_print(fout, "%s)\n", addargname);
130                         for (l = proc->args.decls; l != NULL; l = l->next) {
131                                 pdeclaration(proc->args.argname, &l->decl, 1, ";\n");
132                         }
133                 } else {        /* C++ style header */
134                         f_print(fout, "(");
135                         for (l = proc->args.decls; l != NULL; l = l->next) {
136                                 pdeclaration(proc->args.argname, &l->decl, 0, ", ");
137                         }
138                         f_print(fout, " %s%s)\n", addargtype, addargname);
139                 }
140         }
141
142         if (!Cflag)
143                 f_print(fout, "\t%s%s;\n", addargtype, addargname);
144 }
145
146
147
148 static char *
149 ampr(char *type)
150 {
151         if (isvectordef(type, REL_ALIAS)) {
152                 return ("");
153         } else {
154                 return ("&");
155         }
156 }
157
158 static void
159 printbody(proc_list *proc)
160 {
161         decl_list      *l;
162         bool_t          args2 = (proc->arg_num > 1);
163
164         /* For new style with multiple arguments, need a structure in which
165          * to stuff the arguments. */
166         if (newstyle && args2) {
167                 f_print(fout, "\t%s", proc->args.argname);
168                 f_print(fout, " arg;\n");
169         }
170         f_print(fout, "\tstatic ");
171         if (streq(proc->res_type, "void")) {
172                 f_print(fout, "char ");
173         } else {
174                 ptype(proc->res_prefix, proc->res_type, 0);
175         }
176         f_print(fout, "%s;\n", RESULT);
177         f_print(fout, "\n");
178         f_print(fout, "\tmemset((char *)%s%s, 0, sizeof(%s));\n",
179                 ampr(proc->res_type), RESULT, RESULT);
180         if (newstyle && !args2 && (streq(proc->args.decls->decl.type, "void"))) {
181                 /* newstyle, 0 arguments */
182                 f_print(fout,
183                         "\tif (clnt_call(clnt, %s, (xdrproc_t) xdr_void, (caddr_t) NULL, "
184                         "(xdrproc_t) xdr_%s, (caddr_t) %s%s, TIMEOUT) != RPC_SUCCESS) {\n",
185                         proc->proc_name,
186                         stringfix(proc->res_type), ampr(proc->res_type), RESULT);
187
188         } else if (newstyle && args2) {
189                 /* newstyle, multiple arguments:  stuff arguments into structure */
190                 for (l = proc->args.decls; l != NULL; l = l->next) {
191                         f_print(fout, "\targ.%s = %s;\n",
192                                 l->decl.name, l->decl.name);
193                 }
194                 f_print(fout,
195                         "\tif (clnt_call(clnt, %s, (xdrproc_t) xdr_%s, (caddr_t) &arg, "
196                         "(xdrproc_t) xdr_%s, (caddr_t) %s%s, TIMEOUT) != RPC_SUCCESS) {\n",
197                         proc->proc_name, proc->args.argname,
198                         stringfix(proc->res_type), ampr(proc->res_type), RESULT);
199         } else {                /* single argument, new or old style */
200                 f_print(fout,
201                         "\tif (clnt_call(clnt, %s, (xdrproc_t) xdr_%s, "
202                         "(caddr_t) %s%s, (xdrproc_t) xdr_%s, (caddr_t) %s%s, TIMEOUT) != RPC_SUCCESS) {\n",
203                         proc->proc_name,
204                         stringfix(proc->args.decls->decl.type),
205                         (newstyle ? "&" : ""),
206                         (newstyle ? proc->args.decls->decl.name : "argp"),
207                         stringfix(proc->res_type), ampr(proc->res_type), RESULT);
208         }
209         f_print(fout, "\t\treturn (NULL);\n");
210         f_print(fout, "\t}\n");
211         if (streq(proc->res_type, "void")) {
212                 f_print(fout, "\treturn ((void *)%s%s);\n",
213                         ampr(proc->res_type), RESULT);
214         } else {
215                 f_print(fout, "\treturn (%s%s);\n", ampr(proc->res_type), RESULT);
216         }
217 }