xref: /netbsd-src/usr.bin/rpcgen/rpc_clntout.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: rpc_clntout.c,v 1.8 1997/10/18 10:53:37 lukem 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 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)rpc_clntout.c 1.11 89/02/22 (C) 1987 SMI";
36 #else
37 __RCSID("$NetBSD: rpc_clntout.c,v 1.8 1997/10/18 10:53:37 lukem Exp $");
38 #endif
39 #endif
40 
41 /*
42  * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler
43  * Copyright (C) 1987, Sun Microsytsems, Inc.
44  */
45 #include <stdio.h>
46 #include <string.h>
47 #include <rpc/types.h>
48 #include "rpc_scan.h"
49 #include "rpc_parse.h"
50 #include "rpc_util.h"
51 
52 static void write_program __P((definition *));
53 static char *ampr __P((char *));
54 static void printbody __P((proc_list *));
55 
56 #define DEFAULT_TIMEOUT 25	/* in seconds */
57 static char RESULT[] = "clnt_res";
58 
59 
60 void
61 write_stubs()
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(def)
80 	definition *def;
81 {
82 	version_list *vp;
83 	proc_list *proc;
84 
85 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
86 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
87 			f_print(fout, "\n");
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, "clnt", "CLIENT *");
92 			f_print(fout, "{\n");
93 			printbody(proc);
94 			f_print(fout, "}\n");
95 		}
96 	}
97 }
98 /* Writes out declarations of procedure's argument list.
99    In either ANSI C style, in one of old rpcgen style (pass by reference),
100    or new rpcgen style (multiple arguments, pass by value);
101    */
102 
103 /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */
104 
105 void
106 printarglist(proc, addargname, addargtype)
107 	proc_list *proc;
108 	char   *addargname, *addargtype;
109 {
110 
111 	decl_list *l;
112 
113 	if (!newstyle) {	/* old style: always pass argument by
114 				 * reference */
115 		if (Cflag) {	/* C++ style heading */
116 			f_print(fout, "(");
117 			ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
118 			f_print(fout, "*argp, %s%s)\n", addargtype, addargname);
119 		} else {
120 			f_print(fout, "(argp, %s)\n", addargname);
121 			f_print(fout, "\t");
122 			ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
123 			f_print(fout, "*argp;\n");
124 		}
125 	} else
126 		if (streq(proc->args.decls->decl.type, "void")) {
127 			/* newstyle, 0 argument */
128 			if (Cflag)
129 				f_print(fout, "(%s%s)\n", addargtype, addargname);
130 			else
131 				f_print(fout, "(%s)\n", addargname);
132 		} else {
133 			/* new style, 1 or multiple arguments */
134 			if (!Cflag) {
135 				f_print(fout, "(");
136 				for (l = proc->args.decls; l != NULL; l = l->next)
137 					f_print(fout, "%s, ", l->decl.name);
138 				f_print(fout, "%s)\n", addargname);
139 				for (l = proc->args.decls; l != NULL; l = l->next) {
140 					pdeclaration(proc->args.argname, &l->decl, 1, ";\n");
141 				}
142 			} else {/* C++ style header */
143 				f_print(fout, "(");
144 				for (l = proc->args.decls; l != NULL; l = l->next) {
145 					pdeclaration(proc->args.argname, &l->decl, 0, ", ");
146 				}
147 				f_print(fout, " %s%s)\n", addargtype, addargname);
148 			}
149 		}
150 
151 	if (!Cflag)
152 		f_print(fout, "\t%s%s;\n", addargtype, addargname);
153 }
154 
155 
156 
157 static char *
158 ampr(type)
159 	char   *type;
160 {
161 	if (isvectordef(type, REL_ALIAS)) {
162 		return ("");
163 	} else {
164 		return ("&");
165 	}
166 }
167 
168 static void
169 printbody(proc)
170 	proc_list *proc;
171 {
172 	decl_list *l;
173 	bool_t  args2 = (proc->arg_num > 1);
174 
175 	/* For new style with multiple arguments, need a structure in which to
176 	 * stuff the arguments. */
177 	if (newstyle && args2) {
178 		f_print(fout, "\t%s", proc->args.argname);
179 		f_print(fout, " arg;\n");
180 	}
181 	f_print(fout, "\tstatic ");
182 	if (streq(proc->res_type, "void")) {
183 		f_print(fout, "char ");
184 	} else {
185 		ptype(proc->res_prefix, proc->res_type, 0);
186 	}
187 	f_print(fout, "%s;\n", RESULT);
188 	f_print(fout, "\n");
189 	f_print(fout, "\tmemset((char *)%s%s, 0, sizeof(%s));\n",
190 	    ampr(proc->res_type), RESULT, RESULT);
191 	if (newstyle && !args2 && (streq(proc->args.decls->decl.type, "void"))) {
192 		/* newstyle, 0 arguments */
193 		f_print(fout,
194 		    "\tif (clnt_call(clnt, %s, xdr_void", proc->proc_name);
195 		f_print(fout,
196 		    ", NULL, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS) {\n",
197 		    stringfix(proc->res_type), ampr(proc->res_type), RESULT);
198 
199 	} else
200 		if (newstyle && args2) {
201 			/* newstyle, multiple arguments:  stuff arguments into
202 			 * structure */
203 			for (l = proc->args.decls; l != NULL; l = l->next) {
204 				f_print(fout, "\targ.%s = %s;\n",
205 				    l->decl.name, l->decl.name);
206 			}
207 			f_print(fout,
208 			    "\tif (clnt_call(clnt, %s, xdr_%s, &arg, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS)\n",
209 			    proc->proc_name,
210 			    proc->args.argname,
211 			    stringfix(proc->res_type),
212 			    ampr(proc->res_type), RESULT);
213 		} else {	/* single argument, new or old style */
214 			f_print(fout,
215 			    "\tif (clnt_call(clnt, %s, xdr_%s, %s%s, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS)\n",
216 			    proc->proc_name,
217 			    stringfix(proc->args.decls->decl.type),
218 			    (newstyle ? "&" : ""),
219 			    (newstyle ? proc->args.decls->decl.name : "argp"),
220 			    stringfix(proc->res_type),
221 			    ampr(proc->res_type), RESULT);
222 		}
223 	f_print(fout, "\t\treturn (NULL);\n");
224 	if (streq(proc->res_type, "void")) {
225 		f_print(fout, "\treturn ((void *)%s%s);\n",
226 		    ampr(proc->res_type), RESULT);
227 	} else {
228 		f_print(fout, "\treturn (%s%s);\n", ampr(proc->res_type), RESULT);
229 	}
230 }
231