xref: /dragonfly/usr.bin/rpcgen/rpc_clntout.c (revision c9f721c2)
1 /*
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.
8  *
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.
12  *
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.
16  *
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.
20  *
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.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  *
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 $
31  */
32 
33 #ident	"@(#)rpc_clntout.c	1.15	94/04/25 SMI"
34 
35 /*
36  * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler
37  * Copyright (C) 1987, Sun Microsytsems, Inc.
38  */
39 #include <stdio.h>
40 #include <string.h>
41 #include <rpc/types.h>
42 #include "rpc_parse.h"
43 #include "rpc_util.h"
44 
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 *);
49 
50 static char RESULT[] = "clnt_res";
51 
52 #define DEFAULT_TIMEOUT 25	/* in seconds */
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 static void
72 write_program(definition *def)
73 {
74 	version_list *vp;
75 	proc_list *proc;
76 
77 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
78 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
79 			f_print(fout, "\n");
80 			if (mtflag == 0) {
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, RESULT, "clnt", "CLIENT *");
85 			} else {
86 				f_print(fout, "enum clnt_stat \n");
87 				pvname(proc->proc_name, vp->vers_num);
88 				printarglist(proc, RESULT,  "clnt", "CLIENT *");
89 
90 			}
91 			f_print(fout, "{\n");
92 			printbody(proc);
93 
94 			f_print(fout, "}\n");
95 		}
96 	}
97 }
98 
99 /*
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);
103  */
104 
105 /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */
106 
107 void
108 printarglist(proc_list *proc, char *result, char *addargname, char *addargtype)
109 {
110 	decl_list *l;
111 
112 	if (!newstyle) {
113 		/* old style: always pass argument by reference */
114 		if (Cflag) {	/* C++ style heading */
115 			f_print(fout, "(");
116 			ptype(proc->args.decls->decl.prefix,
117 			      proc->args.decls->decl.type, 1);
118 
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);
124 			} else
125 				f_print(fout, "*argp, %s%s)\n", addargtype,
126 					addargname);
127 		} else {
128 			if (!mtflag)
129 				f_print(fout, "(argp, %s)\n", addargname);
130 			else
131 				f_print(fout, "(argp, %s, %s)\n",
132 					result, addargname);
133 			f_print(fout, "\t");
134 			ptype(proc->args.decls->decl.prefix,
135 			      proc->args.decls->decl.type, 1);
136 			f_print(fout, "*argp;\n");
137 			if (mtflag) {
138 				f_print(fout, "\t");
139 				ptype(proc->res_prefix, proc->res_type, 1);
140 				f_print(fout, "*%s;\n", result);
141 			}
142 		}
143 	} else if (streq(proc->args.decls->decl.type, "void")) {
144 		/* newstyle, 0 argument */
145 		if (mtflag) {
146 			f_print(fout, "(");
147 
148 			if (Cflag) {
149 				ptype(proc->res_prefix, proc->res_type, 1);
150 				f_print(fout, "*%s, %s%s)\n",
151 					result, addargtype, addargname);
152 			}
153 			else {
154 				f_print(fout, "(%s)\n", addargname);
155 			}
156 		} else if (Cflag) {
157 			f_print(fout, "(%s%s)\n", addargtype, addargname);
158 		} else {
159 			f_print(fout, "(%s)\n", addargname);
160 		}
161 	} else {
162 		/* new style, 1 or multiple arguments */
163 		if (!Cflag) {
164 			f_print(fout, "(");
165 			for (l = proc->args.decls;  l != NULL; l = l->next)
166 				f_print(fout, "%s, ", l->decl.name);
167 			if (mtflag)
168 				f_print(fout, "%s, ", result);
169 
170 			f_print(fout, "%s)\n", addargname);
171 			for (l = proc->args.decls; l != NULL; l = l->next) {
172 				pdeclaration(proc->args.argname,
173 					     &l->decl, 1, ";\n");
174 			}
175 			if (mtflag) {
176 				f_print(fout, "\t");
177 				ptype(proc->res_prefix, proc->res_type, 1);
178 				f_print(fout, "*%s;\n", result);
179 			}
180 
181 		} else {	/* C++ style header */
182 			f_print(fout, "(");
183 			for (l = proc->args.decls; l != NULL; l = l->next) {
184 				pdeclaration(proc->args.argname, &l->decl, 0,
185 					     ", ");
186 			}
187 			if (mtflag) {
188 				ptype(proc->res_prefix, proc->res_type, 1);
189 				f_print(fout, "*%s, ", result);
190 
191 			}
192 			f_print(fout, "%s%s)\n", addargtype, addargname);
193 		}
194 	}
195 
196 	if (!Cflag)
197 		f_print(fout, "\t%s%s;\n", addargtype, addargname);
198 }
199 
200 
201 
202 static char *
203 ampr(char *type)
204 {
205 	if (isvectordef(type, REL_ALIAS))
206 		return ("");
207 	else
208 		return ("&");
209 }
210 
211 static void
212 printbody(proc_list *proc)
213 {
214 	decl_list *l;
215 	bool_t args2 = (proc->arg_num > 1);
216 
217 	/*
218 	 * For new style with multiple arguments, need a structure in which
219 	 *  to stuff the arguments.
220 	 */
221 
222 
223 	if (newstyle && args2) {
224 		f_print(fout, "\t%s", proc->args.argname);
225 		f_print(fout, " arg;\n");
226 	}
227 	if (!mtflag) {
228 		f_print(fout, "\tstatic ");
229 		if (streq(proc->res_type, "void"))
230 			f_print(fout, "char ");
231 		else
232 			ptype(proc->res_prefix, proc->res_type, 0);
233 		f_print(fout, "%s;\n", RESULT);
234 		f_print(fout, "\n");
235 		f_print(fout, "\tmemset((char *)%s%s, 0, sizeof (%s));\n",
236 			ampr(proc->res_type), RESULT, RESULT);
237 	}
238 	if (newstyle && !args2 &&
239 	    (streq(proc->args.decls->decl.type, "void"))) {
240 		/* newstyle, 0 arguments */
241 
242 		if (mtflag)
243 			f_print(fout, "\t return ");
244 		else
245 			f_print(fout, "\t if ");
246 
247 		f_print(fout,
248 			"(clnt_call(clnt, %s,\n\t\t(xdrproc_t) xdr_void, ",
249 			proc->proc_name);
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);
253 
254 		if (mtflag)
255 			f_print(fout, "\n\t\tTIMEOUT));\n");
256 		else
257 			f_print(fout, "\n\t\tTIMEOUT) != RPC_SUCCESS) {\n");
258 
259 	} else if (newstyle && args2) {
260 		/*
261 		 * Newstyle, multiple arguments
262 		 * stuff arguments into structure
263 		 */
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);
267 		}
268 		if (mtflag)
269 			f_print(fout, "\treturn ");
270 		else
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);
277 		if (mtflag)
278 			f_print(fout, "\n\t\tTIMEOUT));\n");
279 		else
280 			f_print(fout, "\n\t\tTIMEOUT) != RPC_SUCCESS) {\n");
281 	} else {		/* single argument, new or old style */
282 		if (!mtflag) {
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",
287 				proc->proc_name,
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);
293 		} else {
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);
302 		}
303 	}
304 	if (!mtflag) {
305 		f_print(fout, "\t\treturn (NULL);\n");
306 		f_print(fout, "\t}\n");
307 
308 		if (streq(proc->res_type, "void")) {
309 			f_print(fout, "\treturn ((void *)%s%s);\n",
310 				ampr(proc->res_type), RESULT);
311 		} else {
312 			f_print(fout, "\treturn (%s%s);\n",
313 				ampr(proc->res_type), RESULT);
314 		}
315 	}
316 }
317