2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)rpc_clntout.c 1.11 89/02/22 (C) 1987 SMI
30 * $DragonFly: src/usr.bin/rpcgen/rpc_clntout.c,v 1.5 2004/06/19 16:40:36 joerg Exp $
33 #ident "@(#)rpc_clntout.c 1.15 94/04/25 SMI"
36 * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler
37 * Copyright (C) 1987, Sun Microsytsems, Inc.
41 #include <rpc/types.h>
42 #include "rpc_parse.h"
45 extern void pdeclaration(char *, declaration
*, int, char *);
46 void printarglist(proc_list
*, char *, char *, char *);
47 static void write_program(definition
*);
48 static void printbody(proc_list
*);
50 static char RESULT
[] = "clnt_res";
52 #define DEFAULT_TIMEOUT 25 /* in seconds */
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
)
72 write_program(definition
*def
)
77 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
78 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
, RESULT
, "clnt", "CLIENT *");
86 f_print(fout
, "enum clnt_stat \n");
87 pvname(proc
->proc_name
, vp
->vers_num
);
88 printarglist(proc
, RESULT
, "clnt", "CLIENT *");
100 * Writes out declarations of procedure's argument list.
101 * In either ANSI C style, in one of old rpcgen style (pass by reference),
102 * or new rpcgen style (multiple arguments, pass by value);
105 /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */
108 printarglist(proc_list
*proc
, char *result
, char *addargname
, char *addargtype
)
113 /* old style: always pass argument by reference */
114 if (Cflag
) { /* C++ style heading */
116 ptype(proc
->args
.decls
->decl
.prefix
,
117 proc
->args
.decls
->decl
.type
, 1);
119 if (mtflag
) {/* Generate result field */
120 f_print(fout
, "*argp, ");
121 ptype(proc
->res_prefix
, proc
->res_type
, 1);
122 f_print(fout
, "*%s, %s%s)\n",
123 result
, addargtype
, addargname
);
125 f_print(fout
, "*argp, %s%s)\n", addargtype
,
129 f_print(fout
, "(argp, %s)\n", addargname
);
131 f_print(fout
, "(argp, %s, %s)\n",
134 ptype(proc
->args
.decls
->decl
.prefix
,
135 proc
->args
.decls
->decl
.type
, 1);
136 f_print(fout
, "*argp;\n");
139 ptype(proc
->res_prefix
, proc
->res_type
, 1);
140 f_print(fout
, "*%s;\n", result
);
143 } else if (streq(proc
->args
.decls
->decl
.type
, "void")) {
144 /* newstyle, 0 argument */
149 ptype(proc
->res_prefix
, proc
->res_type
, 1);
150 f_print(fout
, "*%s, %s%s)\n",
151 result
, addargtype
, addargname
);
154 f_print(fout
, "(%s)\n", addargname
);
157 f_print(fout
, "(%s%s)\n", addargtype
, addargname
);
159 f_print(fout
, "(%s)\n", addargname
);
162 /* new style, 1 or multiple arguments */
165 for (l
= proc
->args
.decls
; l
!= NULL
; l
= l
->next
)
166 f_print(fout
, "%s, ", l
->decl
.name
);
168 f_print(fout
, "%s, ", result
);
170 f_print(fout
, "%s)\n", addargname
);
171 for (l
= proc
->args
.decls
; l
!= NULL
; l
= l
->next
) {
172 pdeclaration(proc
->args
.argname
,
177 ptype(proc
->res_prefix
, proc
->res_type
, 1);
178 f_print(fout
, "*%s;\n", result
);
181 } else { /* C++ style header */
183 for (l
= proc
->args
.decls
; l
!= NULL
; l
= l
->next
) {
184 pdeclaration(proc
->args
.argname
, &l
->decl
, 0,
188 ptype(proc
->res_prefix
, proc
->res_type
, 1);
189 f_print(fout
, "*%s, ", result
);
192 f_print(fout
, "%s%s)\n", addargtype
, addargname
);
197 f_print(fout
, "\t%s%s;\n", addargtype
, addargname
);
205 if (isvectordef(type
, REL_ALIAS
))
212 printbody(proc_list
*proc
)
215 bool_t args2
= (proc
->arg_num
> 1);
218 * For new style with multiple arguments, need a structure in which
219 * to stuff the arguments.
223 if (newstyle
&& args2
) {
224 f_print(fout
, "\t%s", proc
->args
.argname
);
225 f_print(fout
, " arg;\n");
228 f_print(fout
, "\tstatic ");
229 if (streq(proc
->res_type
, "void"))
230 f_print(fout
, "char ");
232 ptype(proc
->res_prefix
, proc
->res_type
, 0);
233 f_print(fout
, "%s;\n", RESULT
);
235 f_print(fout
, "\tmemset((char *)%s%s, 0, sizeof (%s));\n",
236 ampr(proc
->res_type
), RESULT
, RESULT
);
238 if (newstyle
&& !args2
&&
239 (streq(proc
->args
.decls
->decl
.type
, "void"))) {
240 /* newstyle, 0 arguments */
243 f_print(fout
, "\t return ");
245 f_print(fout
, "\t if ");
248 "(clnt_call(clnt, %s,\n\t\t(xdrproc_t) xdr_void, ",
250 f_print(fout
, "(caddr_t) NULL,\n\t\t(xdrproc_t) xdr_%s, "
251 "(caddr_t) %s%s,", stringfix(proc
->res_type
),
252 (mtflag
) ? "" : ampr(proc
->res_type
), RESULT
);
255 f_print(fout
, "\n\t\tTIMEOUT));\n");
257 f_print(fout
, "\n\t\tTIMEOUT) != RPC_SUCCESS) {\n");
259 } else if (newstyle
&& args2
) {
261 * Newstyle, multiple arguments
262 * stuff arguments into structure
264 for (l
= proc
->args
.decls
; l
!= NULL
; l
= l
->next
) {
265 f_print(fout
, "\targ.%s = %s;\n",
266 l
->decl
.name
, l
->decl
.name
);
269 f_print(fout
, "\treturn ");
271 f_print(fout
, "\tif ");
272 f_print(fout
, "(clnt_call(clnt, %s,\n\t\t(xdrproc_t) xdr_%s",
273 proc
->proc_name
,proc
->args
.argname
);
274 f_print(fout
, ", (caddr_t) &arg,\n\t\t(xdrproc_t) xdr_%s, "
275 "(caddr_t) %s%s,", stringfix(proc
->res_type
),
276 (mtflag
) ? "" : ampr(proc
->res_type
), RESULT
);
278 f_print(fout
, "\n\t\tTIMEOUT));\n");
280 f_print(fout
, "\n\t\tTIMEOUT) != RPC_SUCCESS) {\n");
281 } else { /* single argument, new or old style */
283 f_print(fout
, "\tif (clnt_call(clnt, %s,\n"
284 "\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n"
285 "\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n"
286 "\t\tTIMEOUT) != RPC_SUCCESS) {\n",
288 stringfix(proc
->args
.decls
->decl
.type
),
289 (newstyle
? "&" : ""),
290 (newstyle
) ? proc
->args
.decls
->decl
.name
: "argp",
291 stringfix(proc
->res_type
),
292 ampr(proc
->res_type
), RESULT
);
294 f_print(fout
, "\treturn (clnt_call(clnt, %s,\n"
295 "\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n"
296 "\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n"
297 "\t\tTIMEOUT));\n", proc
->proc_name
,
298 stringfix(proc
->args
.decls
->decl
.type
),
299 (newstyle
? "&" : ""),
300 (newstyle
) ? proc
->args
.decls
->decl
.name
: "argp",
301 stringfix(proc
->res_type
), "", RESULT
);
305 f_print(fout
, "\t\treturn (NULL);\n");
306 f_print(fout
, "\t}\n");
308 if (streq(proc
->res_type
, "void")) {
309 f_print(fout
, "\treturn ((void *)%s%s);\n",
310 ampr(proc
->res_type
), RESULT
);
312 f_print(fout
, "\treturn (%s%s);\n",
313 ampr(proc
->res_type
), RESULT
);