xref: /titanic_44/usr/src/cmd/rpcgen/rpc_clntout.c (revision 414388d7cb2ee98771e2ac7c2338c460abd44304)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 /*
30  * University Copyright- Copyright (c) 1982, 1986, 1988
31  * The Regents of the University of California
32  * All Rights Reserved
33  *
34  * University Acknowledgment- Portions of this document are derived from
35  * software developed by the University of California, Berkeley, and its
36  * contributors.
37  */
38 
39 #pragma ident	"%Z%%M%	%I%	%E% SMI"
40 
41 /*
42  * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler
43  */
44 #include <stdio.h>
45 #include <string.h>
46 #include <rpc/types.h>
47 #include "rpc_parse.h"
48 #include "rpc_util.h"
49 
50 extern void pdeclaration(char *, declaration *, int, char *);
51 extern void printarglist(proc_list *, char *, char *, char *);
52 
53 static void write_program(definition *);
54 static void printbody(proc_list *);
55 
56 static char RESULT[] = "clnt_res";
57 
58 #define	DEFAULT_TIMEOUT 25	/* in seconds */
59 
60 void
61 write_stubs(void)
62 {
63 	list *l;
64 	definition *def;
65 
66 	f_print(fout,
67 	    "\n/* Default timeout can be changed using clnt_control() */\n");
68 	f_print(fout, "static struct timeval TIMEOUT = { %d, 0 };\n",
69 		DEFAULT_TIMEOUT);
70 	for (l = defined; l != NULL; l = l->next) {
71 		def = (definition *) l->val;
72 		if (def->def_kind == DEF_PROGRAM) {
73 			write_program(def);
74 		}
75 	}
76 }
77 
78 static void
79 write_program(definition *def)
80 {
81 	version_list *vp;
82 	proc_list *proc;
83 
84 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
85 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
86 			f_print(fout, "\n");
87 			if (mtflag == 0) {
88 				ptype(proc->res_prefix, proc->res_type, 1);
89 				f_print(fout, "*\n");
90 				pvname(proc->proc_name, vp->vers_num);
91 				printarglist(proc, RESULT, "clnt", "CLIENT *");
92 			} else {
93 				f_print(fout, "enum clnt_stat \n");
94 				pvname(proc->proc_name, vp->vers_num);
95 				printarglist(proc, RESULT,  "clnt", "CLIENT *");
96 
97 			}
98 			f_print(fout, "{\n");
99 			printbody(proc);
100 
101 			f_print(fout, "}\n");
102 		}
103 	}
104 }
105 
106 /*
107  * Writes out declarations of procedure's argument list.
108  * In either ANSI C style, in one of old rpcgen style (pass by reference),
109  * or new rpcgen style (multiple arguments, pass by value);
110  */
111 
112 /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */
113 
114 void
115 printarglist(proc_list *proc, char *result, char *addargname, char *addargtype)
116 {
117 	bool_t oneway = streq(proc->res_type, "oneway");
118 	decl_list *l;
119 
120 	if (!newstyle) {
121 		/* old style: always pass argument by reference */
122 		if (Cflag) {	/* C++ style heading */
123 			f_print(fout, "(");
124 			ptype(proc->args.decls->decl.prefix,
125 						proc->args.decls->decl.type, 1);
126 
127 			if (mtflag) {	/* Generate result field */
128 				f_print(fout, "*argp, ");
129 				if (!oneway) {
130 					ptype(proc->res_prefix,
131 					    proc->res_type, 1);
132 					f_print(fout, "*%s, ", result);
133 				}
134 				f_print(fout, "%s%s)\n",
135 					addargtype, addargname);
136 			} else
137 				f_print(fout, "*argp, %s%s)\n",
138 					addargtype, addargname);
139 		} else {
140 			if (!mtflag)
141 				f_print(fout, "(argp, %s)\n", addargname);
142 			else {
143 				f_print(fout, "(argp, ");
144 				if (!oneway) {
145 					f_print(fout, "%s, ",
146 					    result);
147 				}
148 				f_print(fout, "%s)\n",
149 				    addargname);
150 			}
151 			f_print(fout, "\t");
152 			ptype(proc->args.decls->decl.prefix,
153 			    proc->args.decls->decl.type, 1);
154 			f_print(fout, "*argp;\n");
155 			if (mtflag && !oneway) {
156 				f_print(fout, "\t");
157 				ptype(proc->res_prefix, proc->res_type, 1);
158 				f_print(fout, "*%s;\n", result);
159 			}
160 		}
161 	} else if (streq(proc->args.decls->decl.type, "void")) {
162 		/* newstyle, 0 argument */
163 		if (mtflag) {
164 			f_print(fout, "(");
165 
166 			if (Cflag) {
167 				if (!oneway) {
168 					ptype(proc->res_prefix,
169 					    proc->res_type, 1);
170 					f_print(fout, "*%s, ", result);
171 				}
172 				f_print(fout, "%s%s)\n",
173 				    addargtype, addargname);
174 			} else
175 				f_print(fout, "(%s)\n", addargname);
176 
177 		} else
178 		if (Cflag)
179 			f_print(fout, "(%s%s)\n", addargtype, addargname);
180 		else
181 			f_print(fout, "(%s)\n", addargname);
182 	} else {
183 		/* new style, 1 or multiple arguments */
184 		if (!Cflag) {
185 			f_print(fout, "(");
186 			for (l = proc->args.decls;  l != NULL; l = l->next)
187 				f_print(fout, "%s, ", l->decl.name);
188 			if (mtflag && !oneway)
189 				f_print(fout, "%s, ", result);
190 
191 			f_print(fout, "%s)\n", addargname);
192 			for (l = proc->args.decls; l != NULL; l = l->next) {
193 				pdeclaration(proc->args.argname,
194 							&l->decl, 1, ";\n");
195 			}
196 			if (mtflag && !oneway) {
197 				f_print(fout, "\t");
198 				ptype(proc->res_prefix, proc->res_type, 1);
199 				f_print(fout, "*%s;\n", result);
200 			}
201 
202 		} else {	/* C++ style header */
203 			f_print(fout, "(");
204 			for (l = proc->args.decls; l != NULL; l = l->next) {
205 				pdeclaration(proc->args.argname, &l->decl, 0,
206 									", ");
207 			}
208 			if (mtflag && !oneway) {
209 				ptype(proc->res_prefix, proc->res_type, 1);
210 				f_print(fout, "*%s, ", result);
211 
212 			}
213 			f_print(fout, "%s%s)\n", addargtype, addargname);
214 		}
215 	}
216 
217 	if (!Cflag)
218 		f_print(fout, "\t%s%s;\n", addargtype, addargname);
219 }
220 
221 
222 
223 static char *
224 ampr(char *type)
225 {
226 	if (isvectordef(type, REL_ALIAS)) {
227 		return ("");
228 	} else {
229 		return ("&");
230 	}
231 }
232 
233 static void
234 printbody(proc_list *proc)
235 {
236 	decl_list *l;
237 	bool_t args2 = (proc->arg_num > 1);
238 	bool_t oneway = streq(proc->res_type, "oneway");
239 
240 	/*
241 	 * For new style with multiple arguments, need a structure in which
242 	 *  to stuff the arguments.
243 	 */
244 	if (newstyle && args2) {
245 		f_print(fout, "\t%s", proc->args.argname);
246 		f_print(fout, " arg;\n");
247 	}
248 	if (!oneway) {
249 		if (!mtflag) {
250 			f_print(fout, "\tstatic ");
251 			if (streq(proc->res_type, "void")) {
252 				f_print(fout, "char ");
253 			} else {
254 				ptype(proc->res_prefix, proc->res_type, 0);
255 			}
256 			f_print(fout, "%s;\n", RESULT);
257 			f_print(fout, "\n");
258 			f_print(fout,
259 			    "\t(void) memset(%s%s, 0, sizeof (%s));\n",
260 			    ampr(proc->res_type), RESULT, RESULT);
261 
262 		}
263 		if (newstyle && !args2 &&
264 		    (streq(proc->args.decls->decl.type, "void"))) {
265 			/* newstyle, 0 arguments */
266 
267 			if (mtflag)
268 				f_print(fout, "\t return ");
269 			else
270 				f_print(fout, "\t if ");
271 
272 			f_print(fout,
273 			    "(clnt_call(clnt, %s,\n\t\t(xdrproc_t)xdr_void, ",
274 			    proc->proc_name);
275 			f_print(fout,
276 			    "NULL,\n\t\t(xdrproc_t)xdr_%s, "
277 			    "(caddr_t)%s%s,",
278 			    stringfix(proc->res_type),
279 			    (mtflag)?"":ampr(proc->res_type),
280 			    RESULT);
281 
282 			if (mtflag)
283 				f_print(fout, "\n\t\tTIMEOUT));\n");
284 			else
285 				f_print(fout,
286 				    "\n\t\tTIMEOUT) != RPC_SUCCESS) {\n");
287 
288 		} else if (newstyle && args2) {
289 			/*
290 			 * Newstyle, multiple arguments
291 			 * stuff arguments into structure
292 			 */
293 			for (l = proc->args.decls;  l != NULL; l = l->next) {
294 				f_print(fout, "\targ.%s = %s;\n",
295 				    l->decl.name, l->decl.name);
296 			}
297 			if (mtflag)
298 				f_print(fout, "\treturn ");
299 			else
300 				f_print(fout, "\tif ");
301 			f_print(fout,
302 			    "(clnt_call(clnt, %s,\n\t\t(xdrproc_t)xdr_%s",
303 			    proc->proc_name, proc->args.argname);
304 			f_print(fout,
305 			    ", (caddr_t)&arg,\n\t\t(xdrproc_t)xdr_%s, "
306 			    "(caddr_t)%s%s,",
307 			    stringfix(proc->res_type),
308 			    (mtflag)?"":ampr(proc->res_type),
309 			    RESULT);
310 			if (mtflag)
311 				f_print(fout, "\n\t\tTIMEOUT));\n");
312 			else
313 				f_print(fout,
314 				    "\n\t\tTIMEOUT) != RPC_SUCCESS) {\n");
315 		} else {		/* single argument, new or old style */
316 			if (!mtflag)
317 				f_print(fout,
318 				    "\tif (clnt_call(clnt, "
319 				    "%s,\n\t\t(xdrproc_t)xdr_%s, "
320 				    "(caddr_t)%s%s,\n\t\t(xdrproc_t)xdr_%s, "
321 				    "(caddr_t)%s%s,\n\t\tTIMEOUT) != "
322 				    "RPC_SUCCESS) {\n",
323 				    proc->proc_name,
324 				    stringfix(proc->args.decls->decl.type),
325 				    (newstyle ? "&" : ""),
326 				    (newstyle ?
327 					proc->args.decls->decl.name :
328 					"argp"),
329 				    stringfix(proc->res_type),
330 				    ampr(proc->res_type),
331 				    RESULT);
332 			else
333 				f_print(fout,
334 				    "\treturn (clnt_call(clnt, "
335 				    "%s,\n\t\t(xdrproc_t)xdr_%s, "
336 				    "(caddr_t)%s%s,\n\t\t(xdrproc_t)xdr_%s, "
337 				    "(caddr_t)%s%s,\n\t\tTIMEOUT));\n",
338 				    proc->proc_name,
339 				    stringfix(proc->args.decls->decl.type),
340 				    (newstyle ? "&" : ""),
341 				    (newstyle ?
342 					proc->args.decls->decl.name :
343 					"argp"),
344 				    stringfix(proc->res_type), "",
345 				    RESULT);
346 		}
347 		if (!mtflag) {
348 			f_print(fout, "\t\treturn (NULL);\n");
349 			f_print(fout, "\t}\n");
350 
351 			if (streq(proc->res_type, "void")) {
352 				f_print(fout, "\treturn ((void *)%s%s);\n",
353 				    ampr(proc->res_type), RESULT);
354 			} else {
355 				f_print(fout, "\treturn (%s%s);\n",
356 				    ampr(proc->res_type), RESULT);
357 			}
358 		}
359 	} else {
360 		/* oneway call */
361 		if (!mtflag) {
362 			f_print(fout, "\tstatic enum clnt_stat ");
363 			f_print(fout, "%s;\n", RESULT);
364 			f_print(fout, "\n");
365 			f_print(fout,
366 			    "\t(void) memset(&%s, 0, sizeof (%s));\n",
367 			    RESULT, RESULT);
368 
369 		}
370 		if (newstyle && !args2 &&
371 		    (streq(proc->args.decls->decl.type, "void"))) {
372 			/* newstyle, 0 arguments */
373 
374 			if (mtflag)
375 				f_print(fout, "\t return (");
376 			else
377 				f_print(fout, "\t if ((%s = ", RESULT);
378 
379 			f_print(fout,
380 			    "clnt_send(clnt, %s,\n\t\t(xdrproc_t)xdr_void, ",
381 			    proc->proc_name);
382 			f_print(fout, "NULL)");
383 
384 			if (mtflag)
385 				f_print(fout, ");\n");
386 			else
387 				f_print(fout, ") != RPC_SUCCESS) {\n");
388 
389 		} else if (newstyle && args2) {
390 			/*
391 			 * Newstyle, multiple arguments
392 			 * stuff arguments into structure
393 			 */
394 			for (l = proc->args.decls;  l != NULL; l = l->next) {
395 				f_print(fout, "\targ.%s = %s;\n",
396 				    l->decl.name, l->decl.name);
397 			}
398 			if (mtflag)
399 				f_print(fout, "\treturn (");
400 			else
401 				f_print(fout, "\tif ((%s =", RESULT);
402 			f_print(fout,
403 			    "clnt_send(clnt, %s,\n\t\t(xdrproc_t)xdr_%s",
404 			    proc->proc_name, proc->args.argname);
405 			f_print(fout,
406 			    ", (caddr_t)&arg)");
407 			if (mtflag)
408 				f_print(fout, ");\n");
409 			else
410 				f_print(fout, ") != RPC_SUCCESS) {\n");
411 		} else {		/* single argument, new or old style */
412 			if (!mtflag)
413 				f_print(fout,
414 				    "\tif ((%s = clnt_send(clnt, "
415 				    "%s,\n\t\t(xdrproc_t)xdr_%s, "
416 				    "(caddr_t)%s%s)) != RPC_SUCCESS) {\n",
417 				    RESULT,
418 				    proc->proc_name,
419 				    stringfix(proc->args.decls->decl.type),
420 				    (newstyle ? "&" : ""),
421 				    (newstyle ?
422 					proc->args.decls->decl.name :
423 					"argp"));
424 			else
425 
426 				f_print(fout,
427 				    "\treturn (clnt_send(clnt, "
428 				    "%s,\n\t\t(xdrproc_t)xdr_%s, "
429 				    "(caddr_t)%s%s));\n",
430 				    proc->proc_name,
431 				    stringfix(proc->args.decls->decl.type),
432 				    (newstyle ? "&" : ""),
433 				    (newstyle ?
434 					proc->args.decls->decl.name :
435 					"argp"));
436 		}
437 		if (!mtflag) {
438 			f_print(fout, "\t\treturn (NULL);\n");
439 			f_print(fout, "\t}\n");
440 
441 			f_print(fout, "\treturn ((void *)&%s);\n",
442 			    RESULT);
443 		}
444 	}
445 }
446