xref: /freebsd/usr.bin/rpcgen/rpc_clntout.c (revision 15df5e2d4379bc97dd1fcac1ce18e53d71fdd016)
14e115012SGarrett Wollman /*
24e115012SGarrett Wollman  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
34e115012SGarrett Wollman  * unrestricted use provided that this legend is included on all tape
44e115012SGarrett Wollman  * media and as a part of the software program in whole or part.  Users
54e115012SGarrett Wollman  * may copy or modify Sun RPC without charge, but are not authorized
64e115012SGarrett Wollman  * to license or distribute it to anyone else except as part of a product or
74e115012SGarrett Wollman  * program developed by the user.
84e115012SGarrett Wollman  *
94e115012SGarrett Wollman  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
104e115012SGarrett Wollman  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
114e115012SGarrett Wollman  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
124e115012SGarrett Wollman  *
134e115012SGarrett Wollman  * Sun RPC is provided with no support and without any obligation on the
144e115012SGarrett Wollman  * part of Sun Microsystems, Inc. to assist in its use, correction,
154e115012SGarrett Wollman  * modification or enhancement.
164e115012SGarrett Wollman  *
174e115012SGarrett Wollman  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
184e115012SGarrett Wollman  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
194e115012SGarrett Wollman  * OR ANY PART THEREOF.
204e115012SGarrett Wollman  *
214e115012SGarrett Wollman  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
224e115012SGarrett Wollman  * or profits or other special, indirect and consequential damages, even if
234e115012SGarrett Wollman  * Sun has been advised of the possibility of such damages.
244e115012SGarrett Wollman  *
254e115012SGarrett Wollman  * Sun Microsystems, Inc.
264e115012SGarrett Wollman  * 2550 Garcia Avenue
274e115012SGarrett Wollman  * Mountain View, California  94043
284e115012SGarrett Wollman  */
29ff49530fSBill Paul 
3040ad8885SAlfred Perlstein #if 0
3175863a6dSPhilippe Charnier #ifndef lint
3263f17371SStefan Farfeleder #ident	"@(#)rpc_clntout.c	1.15	94/04/25 SMI"
33ff49530fSBill Paul static char sccsid[] = "@(#)rpc_clntout.c 1.11 89/02/22 (C) 1987 SMI";
344e115012SGarrett Wollman #endif
3540ad8885SAlfred Perlstein #endif
364e115012SGarrett Wollman 
3775863a6dSPhilippe Charnier #include <sys/cdefs.h>
3875863a6dSPhilippe Charnier __FBSDID("$FreeBSD$");
3975863a6dSPhilippe Charnier 
404e115012SGarrett Wollman /*
414e115012SGarrett Wollman  * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler
424e115012SGarrett Wollman  * Copyright (C) 1987, Sun Microsytsems, Inc.
434e115012SGarrett Wollman  */
444e115012SGarrett Wollman #include <stdio.h>
45ff49530fSBill Paul #include <string.h>
46ff49530fSBill Paul #include <rpc/types.h>
474e115012SGarrett Wollman #include "rpc_parse.h"
48d0cc804bSStefan Farfeleder #include "rpc_scan.h"
494e115012SGarrett Wollman #include "rpc_util.h"
504e115012SGarrett Wollman 
51d3cb5dedSWarner Losh extern void pdeclaration( char *, declaration *, int, char * );
52d3cb5dedSWarner Losh void printarglist( proc_list *, char *, char *, char *);
53d3cb5dedSWarner Losh static void write_program( definition * );
54d3cb5dedSWarner Losh static void printbody( proc_list * );
554e115012SGarrett Wollman 
56ff49530fSBill Paul static char RESULT[] = "clnt_res";
57ff49530fSBill Paul 
58ff49530fSBill Paul 
59ff49530fSBill Paul #define DEFAULT_TIMEOUT 25	/* in seconds */
604e115012SGarrett Wollman 
614e115012SGarrett Wollman 
624e115012SGarrett Wollman void
634e115012SGarrett Wollman write_stubs()
644e115012SGarrett Wollman {
654e115012SGarrett Wollman 	list *l;
664e115012SGarrett Wollman 	definition *def;
674e115012SGarrett Wollman 
684e115012SGarrett Wollman 	f_print(fout,
694e115012SGarrett Wollman 		"\n/* Default timeout can be changed using clnt_control() */\n");
704e115012SGarrett Wollman 	f_print(fout, "static struct timeval TIMEOUT = { %d, 0 };\n",
714e115012SGarrett Wollman 		DEFAULT_TIMEOUT);
724e115012SGarrett Wollman 	for (l = defined; l != NULL; l = l->next) {
734e115012SGarrett Wollman 		def = (definition *) l->val;
744e115012SGarrett Wollman 		if (def->def_kind == DEF_PROGRAM) {
754e115012SGarrett Wollman 			write_program(def);
764e115012SGarrett Wollman 		}
774e115012SGarrett Wollman 	}
784e115012SGarrett Wollman }
794e115012SGarrett Wollman 
80526195adSJordan K. Hubbard static void
814e115012SGarrett Wollman write_program(def)
824e115012SGarrett Wollman 	definition *def;
834e115012SGarrett Wollman {
844e115012SGarrett Wollman 	version_list *vp;
854e115012SGarrett Wollman 	proc_list *proc;
864e115012SGarrett Wollman 
874e115012SGarrett Wollman 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
884e115012SGarrett Wollman 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
894e115012SGarrett Wollman 			f_print(fout, "\n");
90ff49530fSBill Paul 			if (mtflag == 0) {
914e115012SGarrett Wollman 				ptype(proc->res_prefix, proc->res_type, 1);
924e115012SGarrett Wollman 				f_print(fout, "*\n");
934e115012SGarrett Wollman 				pvname(proc->proc_name, vp->vers_num);
94ff49530fSBill Paul 				printarglist(proc, RESULT, "clnt", "CLIENT *");
95ff49530fSBill Paul 			} else {
96ff49530fSBill Paul 				f_print(fout, "enum clnt_stat \n");
97ff49530fSBill Paul 				pvname(proc->proc_name, vp->vers_num);
98ff49530fSBill Paul 				printarglist(proc, RESULT,  "clnt", "CLIENT *");
99ff49530fSBill Paul 
100ff49530fSBill Paul 			}
1014e115012SGarrett Wollman 			f_print(fout, "{\n");
1024e115012SGarrett Wollman 			printbody(proc);
103ff49530fSBill Paul 
104ff49530fSBill Paul 			f_print(fout, "}\n");
1054e115012SGarrett Wollman 		}
1064e115012SGarrett Wollman 	}
1074e115012SGarrett Wollman }
1084e115012SGarrett Wollman 
109ff49530fSBill Paul /*
110ff49530fSBill Paul  * Writes out declarations of procedure's argument list.
111ff49530fSBill Paul  * In either ANSI C style, in one of old rpcgen style (pass by reference),
112ff49530fSBill Paul  * or new rpcgen style (multiple arguments, pass by value);
113ff49530fSBill Paul  */
114ff49530fSBill Paul 
115ff49530fSBill Paul /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */
116ff49530fSBill Paul 
117ff49530fSBill Paul void printarglist(proc, result, addargname, addargtype)
118ff49530fSBill Paul 	proc_list *proc;
119ff49530fSBill Paul 	char *result;
120ff49530fSBill Paul 	char* addargname, * addargtype;
121ff49530fSBill Paul {
122ff49530fSBill Paul 
123ff49530fSBill Paul 	decl_list *l;
124ff49530fSBill Paul 
125ff49530fSBill Paul 	if (!newstyle) {
126ff49530fSBill Paul 		/* old style: always pass argument by reference */
127ff49530fSBill Paul 		f_print(fout, "(");
128ff49530fSBill Paul 		ptype(proc->args.decls->decl.prefix,
129ff49530fSBill Paul 		      proc->args.decls->decl.type, 1);
130ff49530fSBill Paul 
131ff49530fSBill Paul 		if (mtflag) {/* Generate result field */
132ff49530fSBill Paul 			f_print(fout, "*argp, ");
133ff49530fSBill Paul 			ptype(proc->res_prefix, proc->res_type, 1);
134ff49530fSBill Paul 			f_print(fout, "*%s, %s%s)\n",
135ff49530fSBill Paul 				result, addargtype, addargname);
136ff49530fSBill Paul 		} else
137ff49530fSBill Paul 			f_print(fout, "*argp, %s%s)\n", addargtype, addargname);
138ff49530fSBill Paul 	} else if (streq(proc->args.decls->decl.type, "void")) {
139ff49530fSBill Paul 		/* newstyle, 0 argument */
140ff49530fSBill Paul 		if (mtflag) {
141ff49530fSBill Paul 			f_print(fout, "(");
142ff49530fSBill Paul 			ptype(proc->res_prefix, proc->res_type, 1);
143ff49530fSBill Paul 			f_print(fout, "*%s, %s%s)\n",
144ff49530fSBill Paul 				result, addargtype, addargname);
145ff49530fSBill Paul 		} else
146ff49530fSBill Paul 			f_print(fout, "(%s%s)\n", addargtype, addargname);
147ff49530fSBill Paul 	} else {
148ff49530fSBill Paul 		/* new style, 1 or multiple arguments */
149ff49530fSBill Paul 		f_print(fout, "(");
150ff49530fSBill Paul 		for (l = proc->args.decls; l != NULL; l = l->next) {
15115df5e2dSStefan Farfeleder 			pdeclaration(proc->args.argname, &l->decl, 0, ", ");
152ff49530fSBill Paul 		}
153ff49530fSBill Paul 		if (mtflag) {
154ff49530fSBill Paul 			ptype(proc->res_prefix, proc->res_type, 1);
155ff49530fSBill Paul 			f_print(fout, "*%s, ", result);
156ff49530fSBill Paul 
157ff49530fSBill Paul 		}
158ff49530fSBill Paul 		f_print(fout, "%s%s)\n", addargtype, addargname);
159ff49530fSBill Paul 	}
160ff49530fSBill Paul }
161ff49530fSBill Paul 
162ff49530fSBill Paul 
163ff49530fSBill Paul 
1644e115012SGarrett Wollman static char *
1654e115012SGarrett Wollman ampr(type)
1664e115012SGarrett Wollman 	char *type;
1674e115012SGarrett Wollman {
1684e115012SGarrett Wollman 	if (isvectordef(type, REL_ALIAS)) {
1694e115012SGarrett Wollman 		return ("");
1704e115012SGarrett Wollman 	} else {
1714e115012SGarrett Wollman 		return ("&");
1724e115012SGarrett Wollman 	}
1734e115012SGarrett Wollman }
1744e115012SGarrett Wollman 
175526195adSJordan K. Hubbard static void
1764e115012SGarrett Wollman printbody(proc)
1774e115012SGarrett Wollman 	proc_list *proc;
1784e115012SGarrett Wollman {
179ff49530fSBill Paul 	decl_list *l;
180ff49530fSBill Paul 	bool_t args2 = (proc->arg_num > 1);
181ff49530fSBill Paul 
182ff49530fSBill Paul 	/*
183ff49530fSBill Paul 	 * For new style with multiple arguments, need a structure in which
184ff49530fSBill Paul 	 *  to stuff the arguments.
185ff49530fSBill Paul 	 */
186ff49530fSBill Paul 
187ff49530fSBill Paul 
188ff49530fSBill Paul 	if (newstyle && args2) {
189ff49530fSBill Paul 		f_print(fout, "\t%s", proc->args.argname);
190ff49530fSBill Paul 		f_print(fout, " arg;\n");
191ff49530fSBill Paul 	}
192ff49530fSBill Paul 	if (!mtflag) {
1934e115012SGarrett Wollman 		f_print(fout, "\tstatic ");
1944e115012SGarrett Wollman 		if (streq(proc->res_type, "void")) {
1954e115012SGarrett Wollman 			f_print(fout, "char ");
1964e115012SGarrett Wollman 		} else {
1974e115012SGarrett Wollman 			ptype(proc->res_prefix, proc->res_type, 0);
1984e115012SGarrett Wollman 		}
199ff49530fSBill Paul 		f_print(fout, "%s;\n", RESULT);
2004e115012SGarrett Wollman 		f_print(fout, "\n");
201ff49530fSBill Paul 		f_print(fout, "\tmemset((char *)%s%s, 0, sizeof (%s));\n",
202ff49530fSBill Paul 			ampr(proc->res_type), RESULT, RESULT);
203ff49530fSBill Paul 
204ff49530fSBill Paul 	}
205ff49530fSBill Paul 	if (newstyle && !args2 &&
206ff49530fSBill Paul 	    (streq(proc->args.decls->decl.type, "void"))) {
207ff49530fSBill Paul 		/* newstyle, 0 arguments */
208ff49530fSBill Paul 
209ff49530fSBill Paul 		if (mtflag)
210ff49530fSBill Paul 			f_print(fout, "\t return ");
211ff49530fSBill Paul 		else
212ff49530fSBill Paul 			f_print(fout, "\t if ");
213ff49530fSBill Paul 
2144e115012SGarrett Wollman 		f_print(fout,
215ff49530fSBill Paul 			"(clnt_call(clnt, %s,\n\t\t(xdrproc_t) xdr_void, ",
216ff49530fSBill Paul 			proc->proc_name);
217ff49530fSBill Paul 		f_print(fout,
218ff49530fSBill Paul 			"(caddr_t) NULL,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,",
219ff49530fSBill Paul 			stringfix(proc->res_type), (mtflag)?"":ampr(proc->res_type),
220ff49530fSBill Paul 			RESULT);
221ff49530fSBill Paul 
222ff49530fSBill Paul 		if (mtflag)
22335ab9586SDavid E. O'Brien 			f_print(fout, "\n\t\tTIMEOUT));\n");
224ff49530fSBill Paul 		else
225ff49530fSBill Paul 			f_print(fout, "\n\t\tTIMEOUT) != RPC_SUCCESS) {\n");
226ff49530fSBill Paul 
227ff49530fSBill Paul 	} else if (newstyle && args2) {
228ff49530fSBill Paul 		/*
229ff49530fSBill Paul 		 * Newstyle, multiple arguments
230ff49530fSBill Paul 		 * stuff arguments into structure
231ff49530fSBill Paul 		 */
232ff49530fSBill Paul 		for (l = proc->args.decls;  l != NULL; l = l->next) {
233ff49530fSBill Paul 			f_print(fout, "\targ.%s = %s;\n",
234ff49530fSBill Paul 				l->decl.name, l->decl.name);
235ff49530fSBill Paul 		}
236ff49530fSBill Paul 		if (mtflag)
237ff49530fSBill Paul 			f_print(fout, "\treturn ");
238ff49530fSBill Paul 		else
239ff49530fSBill Paul 			f_print(fout, "\tif ");
240ff49530fSBill Paul 		f_print(fout,
241ff49530fSBill Paul 			"(clnt_call(clnt, %s,\n\t\t(xdrproc_t) xdr_%s",
242ff49530fSBill Paul 			proc->proc_name,proc->args.argname);
243ff49530fSBill Paul 		f_print(fout,
244ff49530fSBill Paul 			", (caddr_t) &arg,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,",
245ff49530fSBill Paul 			stringfix(proc->res_type), (mtflag)?"":ampr(proc->res_type),
246ff49530fSBill Paul 			RESULT);
247ff49530fSBill Paul 		if (mtflag)
248ff49530fSBill Paul 			f_print(fout, "\n\t\tTIMEOUT));\n");
249ff49530fSBill Paul 		else
250ff49530fSBill Paul 			f_print(fout, "\n\t\tTIMEOUT) != RPC_SUCCESS) {\n");
251ff49530fSBill Paul 	} else {		/* single argument, new or old style */
252ff49530fSBill Paul 		if (!mtflag)
253ff49530fSBill Paul 			f_print(fout,
254ff49530fSBill Paul 			"\tif (clnt_call(clnt, %s,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n\t\tTIMEOUT) != RPC_SUCCESS) {\n",
255ff49530fSBill Paul 			proc->proc_name,
256ff49530fSBill Paul 			stringfix(proc->args.decls->decl.type),
257ff49530fSBill Paul 			(newstyle ? "&" : ""),
258ff49530fSBill Paul 			(newstyle ? proc->args.decls->decl.name : "argp"),
259ff49530fSBill Paul 			stringfix(proc->res_type), ampr(proc->res_type),
260ff49530fSBill Paul 			RESULT);
261ff49530fSBill Paul 		else
262ff49530fSBill Paul 
263ff49530fSBill Paul 		f_print(fout,
264ff49530fSBill Paul 			"\treturn (clnt_call(clnt, %s,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n\t\tTIMEOUT));\n",
265ff49530fSBill Paul 			proc->proc_name,
266ff49530fSBill Paul 			stringfix(proc->args.decls->decl.type),
267ff49530fSBill Paul 			(newstyle ? "&" : ""),
268ff49530fSBill Paul 			(newstyle ? proc->args.decls->decl.name : "argp"),
269ff49530fSBill Paul 			stringfix(proc->res_type), "",
270ff49530fSBill Paul 			RESULT);
271ff49530fSBill Paul 	}
272ff49530fSBill Paul 	if (!mtflag) {
2734e115012SGarrett Wollman 		f_print(fout, "\t\treturn (NULL);\n");
2744e115012SGarrett Wollman 		f_print(fout, "\t}\n");
275ff49530fSBill Paul 
2764e115012SGarrett Wollman 		if (streq(proc->res_type, "void")) {
277ff49530fSBill Paul 			f_print(fout, "\treturn ((void *)%s%s);\n",
278ff49530fSBill Paul 				ampr(proc->res_type), RESULT);
2794e115012SGarrett Wollman 		} else {
280ff49530fSBill Paul 			f_print(fout, "\treturn (%s%s);\n",
281ff49530fSBill Paul 				ampr(proc->res_type), RESULT);
282ff49530fSBill Paul 		}
2834e115012SGarrett Wollman 	}
2844e115012SGarrett Wollman }
285