2 * Copyright (c) 2009, Sun Microsystems, Inc.
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.
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.
30 static char sccsid[] = "@(#)rpc_clntout.c 1.11 89/02/22 (C) 1987 SMI";
34 * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler
35 * Copyright (C) 1987, Sun Microsytsems, Inc.
39 #include <rpc/types.h>
40 #include "rpc_parse.h"
42 #include "rpc_output.h"
44 /* extern pdeclaration(); */
45 /* void printarglist(); */
47 #define DEFAULT_TIMEOUT 25 /* in seconds */
48 static char RESULT[] = "clnt_res";
50 static void write_program(definition *def);
51 static void printbody(proc_list *proc);
61 "\n/* Default timeout can be changed using clnt_control() */\n");
62 f_print(fout, "static struct timeval TIMEOUT = { %d, 0 };\n",
64 for (l = defined; l != NULL; l = l->next) {
65 def = (definition *) l->val;
66 if (def->def_kind == DEF_PROGRAM) {
73 write_program(definition *def)
78 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
79 for (proc = vp->procs; proc != NULL; proc = proc->next) {
81 ptype(proc->res_prefix, proc->res_type, 1);
83 pvname(proc->proc_name, vp->vers_num);
84 printarglist(proc, "clnt", "CLIENT *");
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);
98 /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */
101 printarglist(proc_list *proc, char *addargname, char *addargtype)
106 if (!newstyle) { /* old style: always pass arg by reference */
107 if (Cflag) { /* C++ style heading */
109 ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
110 f_print(fout, "*argp, %s%s)\n", addargtype, addargname);
112 f_print(fout, "(argp, %s)\n", addargname);
114 ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
115 f_print(fout, "*argp;\n");
117 } else if (streq(proc->args.decls->decl.type, "void")) {
118 /* newstyle, 0 argument */
120 f_print(fout, "(%s%s)\n", addargtype, addargname);
122 f_print(fout, "(%s)\n", addargname);
124 /* new style, 1 or multiple arguments */
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");
133 } else { /* C++ style header */
135 for (l = proc->args.decls; l != NULL; l = l->next) {
136 pdeclaration(proc->args.argname, &l->decl, 0, ", ");
138 f_print(fout, " %s%s)\n", addargtype, addargname);
143 f_print(fout, "\t%s%s;\n", addargtype, addargname);
151 if (isvectordef(type, REL_ALIAS)) {
159 printbody(proc_list *proc)
162 bool_t args2 = (proc->arg_num > 1);
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");
170 f_print(fout, "\tstatic ");
171 if (streq(proc->res_type, "void")) {
172 f_print(fout, "char ");
174 ptype(proc->res_prefix, proc->res_type, 0);
176 f_print(fout, "%s;\n", RESULT);
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 */
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",
186 stringfix(proc->res_type), ampr(proc->res_type), RESULT);
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);
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 */
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",
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);
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);
215 f_print(fout, "\treturn (%s%s);\n", ampr(proc->res_type), RESULT);