xref: /netbsd-src/usr.bin/rpcgen/rpc_clntout.c (revision 0213edf018b9d386c4469b3704eebd74f481f642)
1*0213edf0Schristos /*	$NetBSD: rpc_clntout.c,v 1.15 2013/12/15 00:40:17 christos Exp $	*/
28f0abce1Sglass /*
38f0abce1Sglass  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
48f0abce1Sglass  * unrestricted use provided that this legend is included on all tape
58f0abce1Sglass  * media and as a part of the software program in whole or part.  Users
68f0abce1Sglass  * may copy or modify Sun RPC without charge, but are not authorized
78f0abce1Sglass  * to license or distribute it to anyone else except as part of a product or
871a0fb45Spk  * program developed by the user or with the express written consent of
971a0fb45Spk  * Sun Microsystems, Inc.
108f0abce1Sglass  *
118f0abce1Sglass  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
128f0abce1Sglass  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
138f0abce1Sglass  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
148f0abce1Sglass  *
158f0abce1Sglass  * Sun RPC is provided with no support and without any obligation on the
168f0abce1Sglass  * part of Sun Microsystems, Inc. to assist in its use, correction,
178f0abce1Sglass  * modification or enhancement.
188f0abce1Sglass  *
198f0abce1Sglass  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
208f0abce1Sglass  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
218f0abce1Sglass  * OR ANY PART THEREOF.
228f0abce1Sglass  *
238f0abce1Sglass  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
248f0abce1Sglass  * or profits or other special, indirect and consequential damages, even if
258f0abce1Sglass  * Sun has been advised of the possibility of such damages.
268f0abce1Sglass  *
278f0abce1Sglass  * Sun Microsystems, Inc.
288f0abce1Sglass  * 2550 Garcia Avenue
298f0abce1Sglass  * Mountain View, California  94043
308f0abce1Sglass  */
3171a0fb45Spk 
32b2f78261Sjmc #if HAVE_NBTOOL_CONFIG_H
33b2f78261Sjmc #include "nbtool_config.h"
34b2f78261Sjmc #endif
35b2f78261Sjmc 
3693579481Schristos #include <sys/cdefs.h>
3776834aefStv #if defined(__RCSID) && !defined(lint)
3893579481Schristos #if 0
3971a0fb45Spk static char sccsid[] = "@(#)rpc_clntout.c 1.11 89/02/22 (C) 1987 SMI";
4093579481Schristos #else
41*0213edf0Schristos __RCSID("$NetBSD: rpc_clntout.c,v 1.15 2013/12/15 00:40:17 christos Exp $");
4293579481Schristos #endif
438f0abce1Sglass #endif
448f0abce1Sglass 
458f0abce1Sglass /*
468f0abce1Sglass  * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler
478f0abce1Sglass  * Copyright (C) 1987, Sun Microsytsems, Inc.
488f0abce1Sglass  */
498f0abce1Sglass #include <stdio.h>
5071a0fb45Spk #include <string.h>
5171a0fb45Spk #include <rpc/types.h>
5293579481Schristos #include "rpc_scan.h"
538f0abce1Sglass #include "rpc_parse.h"
548f0abce1Sglass #include "rpc_util.h"
558f0abce1Sglass 
56*0213edf0Schristos static void write_program(definition *);
57*0213edf0Schristos static const char *ampr(const char *);
58*0213edf0Schristos static const char *aster(const char *);
59*0213edf0Schristos static void printbody(proc_list *);
608f0abce1Sglass 
6171a0fb45Spk #define DEFAULT_TIMEOUT 25	/* in seconds */
6271a0fb45Spk static char RESULT[] = "clnt_res";
638f0abce1Sglass 
648f0abce1Sglass 
658f0abce1Sglass void
write_stubs(void)66e9067f11Sdholland write_stubs(void)
678f0abce1Sglass {
688f0abce1Sglass 	list   *l;
698f0abce1Sglass 	definition *def;
708f0abce1Sglass 
718f0abce1Sglass 	f_print(fout,
728f0abce1Sglass 	    "\n/* Default timeout can be changed using clnt_control() */\n");
738f0abce1Sglass 	f_print(fout, "static struct timeval TIMEOUT = { %d, 0 };\n",
748f0abce1Sglass 	    DEFAULT_TIMEOUT);
758f0abce1Sglass 	for (l = defined; l != NULL; l = l->next) {
768f0abce1Sglass 		def = (definition *) l->val;
778f0abce1Sglass 		if (def->def_kind == DEF_PROGRAM) {
788f0abce1Sglass 			write_program(def);
798f0abce1Sglass 		}
808f0abce1Sglass 	}
818f0abce1Sglass }
828f0abce1Sglass 
8393579481Schristos static void
write_program(definition * def)84e9067f11Sdholland write_program(definition *def)
858f0abce1Sglass {
868f0abce1Sglass 	version_list *vp;
878f0abce1Sglass 	proc_list *proc;
888f0abce1Sglass 
898f0abce1Sglass 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
908f0abce1Sglass 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
918f0abce1Sglass 			f_print(fout, "\n");
92b1bad8f6Smycroft 			if (Mflag)
93b1bad8f6Smycroft 				f_print(fout, "enum clnt_stat\n");
94b1bad8f6Smycroft 			else {
958f0abce1Sglass 				ptype(proc->res_prefix, proc->res_type, 1);
968f0abce1Sglass 				f_print(fout, "*\n");
97b1bad8f6Smycroft 			}
988f0abce1Sglass 			pvname(proc->proc_name, vp->vers_num);
99b1bad8f6Smycroft 			printarglist(proc, RESULT, "clnt", "CLIENT *");
1008f0abce1Sglass 			f_print(fout, "{\n");
1018f0abce1Sglass 			printbody(proc);
10271a0fb45Spk 			f_print(fout, "}\n");
1038f0abce1Sglass 		}
1048f0abce1Sglass 	}
1058f0abce1Sglass }
10671a0fb45Spk /* Writes out declarations of procedure's argument list.
10771a0fb45Spk    In either ANSI C style, in one of old rpcgen style (pass by reference),
10871a0fb45Spk    or new rpcgen style (multiple arguments, pass by value);
10971a0fb45Spk    */
11071a0fb45Spk 
11171a0fb45Spk /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */
11271a0fb45Spk 
11399071d1bSlukem void
printarglist(proc_list * proc,const char * result,const char * addargname,const char * addargtype)114e9067f11Sdholland printarglist(proc_list *proc, const char *result,
115e9067f11Sdholland 	     const char *addargname, const char *addargtype)
11671a0fb45Spk {
11771a0fb45Spk 
11871a0fb45Spk 	decl_list *l;
11971a0fb45Spk 
12099071d1bSlukem 	if (!newstyle) {	/* old style: always pass argument by
12199071d1bSlukem 				 * reference */
12271a0fb45Spk 		f_print(fout, "(");
12371a0fb45Spk 		ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
124b1bad8f6Smycroft 		f_print(fout, "*argp, ");
125b1bad8f6Smycroft 		if (Mflag) {
126b1bad8f6Smycroft 			if (streq(proc->res_type, "void"))
127b1bad8f6Smycroft 				f_print(fout, "char ");
128b1bad8f6Smycroft 			else
129b1bad8f6Smycroft 				ptype(proc->res_prefix, proc->res_type, 0);
130b1bad8f6Smycroft 			f_print(fout, "%s%s, ", aster(proc->res_type),
131b1bad8f6Smycroft 			    result);
132b1bad8f6Smycroft 		}
133b1bad8f6Smycroft 		f_print(fout, "%s%s)\n", addargtype, addargname);
13471a0fb45Spk 	} else {
1350c00d4b5Smycroft 		f_print(fout, "(");
1360c00d4b5Smycroft 		if (!streq(proc->args.decls->decl.type, "void")) {
13771a0fb45Spk 			/* new style, 1 or multiple arguments */
138*0213edf0Schristos 			for (l = proc->args.decls; l != NULL; l = l->next)
1390c00d4b5Smycroft 				pdeclaration(proc->args.argname,
1400c00d4b5Smycroft 				    &l->decl, 0, ", ");
1410c00d4b5Smycroft 		}
1420c00d4b5Smycroft 		if (Mflag) {
1430c00d4b5Smycroft 			if (streq(proc->res_type, "void"))
1440c00d4b5Smycroft 				f_print(fout, "char ");
1450c00d4b5Smycroft 			else
1460c00d4b5Smycroft 				ptype(proc->res_prefix, proc->res_type, 0);
1470c00d4b5Smycroft 			f_print(fout, "%s%s, ", aster(proc->res_type),
1480c00d4b5Smycroft 			    result);
14971a0fb45Spk 		}
15071a0fb45Spk 		f_print(fout, "%s%s)\n", addargtype, addargname);
15171a0fb45Spk 	}
15271a0fb45Spk }
15371a0fb45Spk 
15471a0fb45Spk 
155e9067f11Sdholland static const char *
ampr(const char * type)156e9067f11Sdholland ampr(const char *type)
1578f0abce1Sglass {
1588f0abce1Sglass 	if (isvectordef(type, REL_ALIAS)) {
1598f0abce1Sglass 		return ("");
1608f0abce1Sglass 	} else {
1618f0abce1Sglass 		return ("&");
1628f0abce1Sglass 	}
1638f0abce1Sglass }
1648f0abce1Sglass 
165e9067f11Sdholland static const char *
aster(const char * type)166e9067f11Sdholland aster(const char *type)
167b1bad8f6Smycroft {
168b1bad8f6Smycroft 	if (isvectordef(type, REL_ALIAS)) {
169b1bad8f6Smycroft 		return ("");
170b1bad8f6Smycroft 	} else {
171b1bad8f6Smycroft 		return ("*");
172b1bad8f6Smycroft 	}
173b1bad8f6Smycroft }
174b1bad8f6Smycroft 
17571a0fb45Spk static void
printbody(proc_list * proc)176e9067f11Sdholland printbody(proc_list *proc)
1778f0abce1Sglass {
17871a0fb45Spk 	decl_list *l;
17971a0fb45Spk 	bool_t  args2 = (proc->arg_num > 1);
18071a0fb45Spk 
18199071d1bSlukem 	/* For new style with multiple arguments, need a structure in which to
18299071d1bSlukem 	 * stuff the arguments. */
18371a0fb45Spk 	if (newstyle && args2) {
18471a0fb45Spk 		f_print(fout, "\t%s", proc->args.argname);
18571a0fb45Spk 		f_print(fout, " arg;\n");
18671a0fb45Spk 	}
187b1bad8f6Smycroft 	if (!Mflag) {
1888f0abce1Sglass 		f_print(fout, "\tstatic ");
189b1bad8f6Smycroft 		if (streq(proc->res_type, "void"))
1908f0abce1Sglass 			f_print(fout, "char ");
191b1bad8f6Smycroft 		else
1928f0abce1Sglass 			ptype(proc->res_prefix, proc->res_type, 0);
19371a0fb45Spk 		f_print(fout, "%s;\n", RESULT);
194b1bad8f6Smycroft 	}
1958f0abce1Sglass 	f_print(fout, "\n");
196b1bad8f6Smycroft 	if (!Mflag)
19771a0fb45Spk 		f_print(fout, "\tmemset((char *)%s%s, 0, sizeof(%s));\n",
19871a0fb45Spk 		    ampr(proc->res_type), RESULT, RESULT);
19971a0fb45Spk 	if (newstyle && !args2 && (streq(proc->args.decls->decl.type, "void"))) {
20071a0fb45Spk 		/* newstyle, 0 arguments */
201b1bad8f6Smycroft 		if (Mflag) {
202b1bad8f6Smycroft 			f_print(fout, "\treturn (clnt_call(clnt, %s, xdr_void",
203b1bad8f6Smycroft 			    proc->proc_name);
204b1bad8f6Smycroft 			f_print(fout, ", NULL, xdr_%s, %s, TIMEOUT));\n",
205b1bad8f6Smycroft 			    stringfix(proc->res_type), RESULT);
206b1bad8f6Smycroft 		} else {
207b1bad8f6Smycroft 			f_print(fout, "\tif (clnt_call(clnt, %s, xdr_void, ",
208b1bad8f6Smycroft 			    proc->proc_name);
2098f0abce1Sglass 			f_print(fout,
2101cb980bdSkleink 			    "NULL, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS)\n",
211b1bad8f6Smycroft 			    stringfix(proc->res_type), ampr(proc->res_type),
212b1bad8f6Smycroft 			    RESULT);
213b1bad8f6Smycroft 		}
214b1bad8f6Smycroft 	} else {
21599071d1bSlukem 		if (newstyle && args2) {
21699071d1bSlukem 			/* newstyle, multiple arguments:  stuff arguments into
21799071d1bSlukem 			 * structure */
21871a0fb45Spk 			for (l = proc->args.decls; l != NULL; l = l->next) {
21971a0fb45Spk 				f_print(fout, "\targ.%s = %s;\n",
22071a0fb45Spk 				    l->decl.name, l->decl.name);
22171a0fb45Spk 			}
222b1bad8f6Smycroft 			if (Mflag) {
223b1bad8f6Smycroft 				f_print(fout,
224b1bad8f6Smycroft 				    "\treturn (clnt_call(clnt, %s, xdr_%s, &arg, xdr_%s, %s, TIMEOUT));\n",
225b1bad8f6Smycroft 				    proc->proc_name, proc->args.argname,
226b1bad8f6Smycroft 				    stringfix(proc->res_type), RESULT);
227b1bad8f6Smycroft 			} else {
22871a0fb45Spk 				f_print(fout,
2297b80581cSmycroft 				    "\tif (clnt_call(clnt, %s, xdr_%s, &arg, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS)\n",
230b1bad8f6Smycroft 				    proc->proc_name, proc->args.argname,
23171a0fb45Spk 				    stringfix(proc->res_type),
23271a0fb45Spk 				    ampr(proc->res_type), RESULT);
233b1bad8f6Smycroft 			}
23471a0fb45Spk 		} else {	/* single argument, new or old style */
235b1bad8f6Smycroft 			if (Mflag) {
236b1bad8f6Smycroft 				f_print(fout,
237b1bad8f6Smycroft 				    "\treturn (clnt_call(clnt, %s, xdr_%s, %s%s, xdr_%s, %s, TIMEOUT));\n",
238b1bad8f6Smycroft 				    proc->proc_name,
239b1bad8f6Smycroft 				    stringfix(proc->args.decls->decl.type),
240b1bad8f6Smycroft 				    (newstyle ? "&" : ""),
241b1bad8f6Smycroft 				    (newstyle ? proc->args.decls->decl.name : "argp"),
242b1bad8f6Smycroft 				    stringfix(proc->res_type), RESULT);
243b1bad8f6Smycroft 			} else {
24471a0fb45Spk 				f_print(fout,
2457b80581cSmycroft 				    "\tif (clnt_call(clnt, %s, xdr_%s, %s%s, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS)\n",
24671a0fb45Spk 				    proc->proc_name,
24771a0fb45Spk 				    stringfix(proc->args.decls->decl.type),
24871a0fb45Spk 				    (newstyle ? "&" : ""),
24971a0fb45Spk 				    (newstyle ? proc->args.decls->decl.name : "argp"),
25071a0fb45Spk 				    stringfix(proc->res_type),
25171a0fb45Spk 				    ampr(proc->res_type), RESULT);
25271a0fb45Spk 			}
253b1bad8f6Smycroft 		}
254b1bad8f6Smycroft 	}
255b1bad8f6Smycroft 	if (!Mflag) {
2568f0abce1Sglass 		f_print(fout, "\t\treturn (NULL);\n");
257b1bad8f6Smycroft 		if (streq(proc->res_type, "void"))
25871a0fb45Spk 			f_print(fout, "\treturn ((void *)%s%s);\n",
25971a0fb45Spk 			    ampr(proc->res_type), RESULT);
260b1bad8f6Smycroft 		else
261b1bad8f6Smycroft 			f_print(fout, "\treturn (%s%s);\n",
262b1bad8f6Smycroft 			    ampr(proc->res_type), RESULT);
2638f0abce1Sglass 	}
2648f0abce1Sglass }
265