xref: /netbsd-src/usr.bin/rpcgen/rpc_clntout.c (revision 0213edf018b9d386c4469b3704eebd74f481f642)
1 /*	$NetBSD: rpc_clntout.c,v 1.15 2013/12/15 00:40:17 christos Exp $	*/
2 /*
3  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4  * unrestricted use provided that this legend is included on all tape
5  * media and as a part of the software program in whole or part.  Users
6  * may copy or modify Sun RPC without charge, but are not authorized
7  * to license or distribute it to anyone else except as part of a product or
8  * program developed by the user or with the express written consent of
9  * Sun Microsystems, Inc.
10  *
11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14  *
15  * Sun RPC is provided with no support and without any obligation on the
16  * part of Sun Microsystems, Inc. to assist in its use, correction,
17  * modification or enhancement.
18  *
19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21  * OR ANY PART THEREOF.
22  *
23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24  * or profits or other special, indirect and consequential damages, even if
25  * Sun has been advised of the possibility of such damages.
26  *
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
30  */
31 
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35 
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)rpc_clntout.c 1.11 89/02/22 (C) 1987 SMI";
40 #else
41 __RCSID("$NetBSD: rpc_clntout.c,v 1.15 2013/12/15 00:40:17 christos Exp $");
42 #endif
43 #endif
44 
45 /*
46  * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler
47  * Copyright (C) 1987, Sun Microsytsems, Inc.
48  */
49 #include <stdio.h>
50 #include <string.h>
51 #include <rpc/types.h>
52 #include "rpc_scan.h"
53 #include "rpc_parse.h"
54 #include "rpc_util.h"
55 
56 static void write_program(definition *);
57 static const char *ampr(const char *);
58 static const char *aster(const char *);
59 static void printbody(proc_list *);
60 
61 #define DEFAULT_TIMEOUT 25	/* in seconds */
62 static char RESULT[] = "clnt_res";
63 
64 
65 void
write_stubs(void)66 write_stubs(void)
67 {
68 	list   *l;
69 	definition *def;
70 
71 	f_print(fout,
72 	    "\n/* Default timeout can be changed using clnt_control() */\n");
73 	f_print(fout, "static struct timeval TIMEOUT = { %d, 0 };\n",
74 	    DEFAULT_TIMEOUT);
75 	for (l = defined; l != NULL; l = l->next) {
76 		def = (definition *) l->val;
77 		if (def->def_kind == DEF_PROGRAM) {
78 			write_program(def);
79 		}
80 	}
81 }
82 
83 static void
write_program(definition * def)84 write_program(definition *def)
85 {
86 	version_list *vp;
87 	proc_list *proc;
88 
89 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
90 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
91 			f_print(fout, "\n");
92 			if (Mflag)
93 				f_print(fout, "enum clnt_stat\n");
94 			else {
95 				ptype(proc->res_prefix, proc->res_type, 1);
96 				f_print(fout, "*\n");
97 			}
98 			pvname(proc->proc_name, vp->vers_num);
99 			printarglist(proc, RESULT, "clnt", "CLIENT *");
100 			f_print(fout, "{\n");
101 			printbody(proc);
102 			f_print(fout, "}\n");
103 		}
104 	}
105 }
106 /* Writes out declarations of procedure's argument list.
107    In either ANSI C style, in one of old rpcgen style (pass by reference),
108    or new rpcgen style (multiple arguments, pass by value);
109    */
110 
111 /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */
112 
113 void
printarglist(proc_list * proc,const char * result,const char * addargname,const char * addargtype)114 printarglist(proc_list *proc, const char *result,
115 	     const char *addargname, const char *addargtype)
116 {
117 
118 	decl_list *l;
119 
120 	if (!newstyle) {	/* old style: always pass argument by
121 				 * reference */
122 		f_print(fout, "(");
123 		ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
124 		f_print(fout, "*argp, ");
125 		if (Mflag) {
126 			if (streq(proc->res_type, "void"))
127 				f_print(fout, "char ");
128 			else
129 				ptype(proc->res_prefix, proc->res_type, 0);
130 			f_print(fout, "%s%s, ", aster(proc->res_type),
131 			    result);
132 		}
133 		f_print(fout, "%s%s)\n", addargtype, addargname);
134 	} else {
135 		f_print(fout, "(");
136 		if (!streq(proc->args.decls->decl.type, "void")) {
137 			/* new style, 1 or multiple arguments */
138 			for (l = proc->args.decls; l != NULL; l = l->next)
139 				pdeclaration(proc->args.argname,
140 				    &l->decl, 0, ", ");
141 		}
142 		if (Mflag) {
143 			if (streq(proc->res_type, "void"))
144 				f_print(fout, "char ");
145 			else
146 				ptype(proc->res_prefix, proc->res_type, 0);
147 			f_print(fout, "%s%s, ", aster(proc->res_type),
148 			    result);
149 		}
150 		f_print(fout, "%s%s)\n", addargtype, addargname);
151 	}
152 }
153 
154 
155 static const char *
ampr(const char * type)156 ampr(const char *type)
157 {
158 	if (isvectordef(type, REL_ALIAS)) {
159 		return ("");
160 	} else {
161 		return ("&");
162 	}
163 }
164 
165 static const char *
aster(const char * type)166 aster(const char *type)
167 {
168 	if (isvectordef(type, REL_ALIAS)) {
169 		return ("");
170 	} else {
171 		return ("*");
172 	}
173 }
174 
175 static void
printbody(proc_list * proc)176 printbody(proc_list *proc)
177 {
178 	decl_list *l;
179 	bool_t  args2 = (proc->arg_num > 1);
180 
181 	/* For new style with multiple arguments, need a structure in which to
182 	 * stuff the arguments. */
183 	if (newstyle && args2) {
184 		f_print(fout, "\t%s", proc->args.argname);
185 		f_print(fout, " arg;\n");
186 	}
187 	if (!Mflag) {
188 		f_print(fout, "\tstatic ");
189 		if (streq(proc->res_type, "void"))
190 			f_print(fout, "char ");
191 		else
192 			ptype(proc->res_prefix, proc->res_type, 0);
193 		f_print(fout, "%s;\n", RESULT);
194 	}
195 	f_print(fout, "\n");
196 	if (!Mflag)
197 		f_print(fout, "\tmemset((char *)%s%s, 0, sizeof(%s));\n",
198 		    ampr(proc->res_type), RESULT, RESULT);
199 	if (newstyle && !args2 && (streq(proc->args.decls->decl.type, "void"))) {
200 		/* newstyle, 0 arguments */
201 		if (Mflag) {
202 			f_print(fout, "\treturn (clnt_call(clnt, %s, xdr_void",
203 			    proc->proc_name);
204 			f_print(fout, ", NULL, xdr_%s, %s, TIMEOUT));\n",
205 			    stringfix(proc->res_type), RESULT);
206 		} else {
207 			f_print(fout, "\tif (clnt_call(clnt, %s, xdr_void, ",
208 			    proc->proc_name);
209 			f_print(fout,
210 			    "NULL, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS)\n",
211 			    stringfix(proc->res_type), ampr(proc->res_type),
212 			    RESULT);
213 		}
214 	} else {
215 		if (newstyle && args2) {
216 			/* newstyle, multiple arguments:  stuff arguments into
217 			 * structure */
218 			for (l = proc->args.decls; l != NULL; l = l->next) {
219 				f_print(fout, "\targ.%s = %s;\n",
220 				    l->decl.name, l->decl.name);
221 			}
222 			if (Mflag) {
223 				f_print(fout,
224 				    "\treturn (clnt_call(clnt, %s, xdr_%s, &arg, xdr_%s, %s, TIMEOUT));\n",
225 				    proc->proc_name, proc->args.argname,
226 				    stringfix(proc->res_type), RESULT);
227 			} else {
228 				f_print(fout,
229 				    "\tif (clnt_call(clnt, %s, xdr_%s, &arg, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS)\n",
230 				    proc->proc_name, proc->args.argname,
231 				    stringfix(proc->res_type),
232 				    ampr(proc->res_type), RESULT);
233 			}
234 		} else {	/* single argument, new or old style */
235 			if (Mflag) {
236 				f_print(fout,
237 				    "\treturn (clnt_call(clnt, %s, xdr_%s, %s%s, xdr_%s, %s, TIMEOUT));\n",
238 				    proc->proc_name,
239 				    stringfix(proc->args.decls->decl.type),
240 				    (newstyle ? "&" : ""),
241 				    (newstyle ? proc->args.decls->decl.name : "argp"),
242 				    stringfix(proc->res_type), RESULT);
243 			} else {
244 				f_print(fout,
245 				    "\tif (clnt_call(clnt, %s, xdr_%s, %s%s, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS)\n",
246 				    proc->proc_name,
247 				    stringfix(proc->args.decls->decl.type),
248 				    (newstyle ? "&" : ""),
249 				    (newstyle ? proc->args.decls->decl.name : "argp"),
250 				    stringfix(proc->res_type),
251 				    ampr(proc->res_type), RESULT);
252 			}
253 		}
254 	}
255 	if (!Mflag) {
256 		f_print(fout, "\t\treturn (NULL);\n");
257 		if (streq(proc->res_type, "void"))
258 			f_print(fout, "\treturn ((void *)%s%s);\n",
259 			    ampr(proc->res_type), RESULT);
260 		else
261 			f_print(fout, "\treturn (%s%s);\n",
262 			    ampr(proc->res_type), RESULT);
263 	}
264 }
265