xref: /freebsd/usr.bin/rpcgen/rpc_clntout.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
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 
30 #if 0
31 #ifndef lint
32 #ident	"@(#)rpc_clntout.c	1.15	94/04/25 SMI"
33 static char sccsid[] = "@(#)rpc_clntout.c 1.11 89/02/22 (C) 1987 SMI";
34 #endif
35 #endif
36 
37 #include <sys/cdefs.h>
38 /*
39  * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler
40  * Copyright (C) 1987, Sun Microsytsems, Inc.
41  */
42 #include <stdio.h>
43 #include <string.h>
44 #include <rpc/types.h>
45 #include "rpc_parse.h"
46 #include "rpc_scan.h"
47 #include "rpc_util.h"
48 
49 static void write_program( definition * );
50 static void printbody( proc_list * );
51 
52 static char RESULT[] = "clnt_res";
53 
54 
55 #define DEFAULT_TIMEOUT 25	/* in seconds */
56 
57 
58 void
59 write_stubs(void)
60 {
61 	list *l;
62 	definition *def;
63 
64 	f_print(fout,
65 		"\n/* Default timeout can be changed using clnt_control() */\n");
66 	f_print(fout, "static struct timeval TIMEOUT = { %d, 0 };\n",
67 		DEFAULT_TIMEOUT);
68 	for (l = defined; l != NULL; l = l->next) {
69 		def = (definition *) l->val;
70 		if (def->def_kind == DEF_PROGRAM) {
71 			write_program(def);
72 		}
73 	}
74 }
75 
76 static void
77 write_program(definition *def)
78 {
79 	version_list *vp;
80 	proc_list *proc;
81 
82 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
83 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
84 			f_print(fout, "\n");
85 			if (mtflag == 0) {
86 				ptype(proc->res_prefix, proc->res_type, 1);
87 				f_print(fout, "*\n");
88 				pvname(proc->proc_name, vp->vers_num);
89 				printarglist(proc, RESULT, "clnt", "CLIENT *");
90 			} else {
91 				f_print(fout, "enum clnt_stat \n");
92 				pvname(proc->proc_name, vp->vers_num);
93 				printarglist(proc, RESULT,  "clnt", "CLIENT *");
94 
95 			}
96 			f_print(fout, "{\n");
97 			printbody(proc);
98 
99 			f_print(fout, "}\n");
100 		}
101 	}
102 }
103 
104 /*
105  * Writes out declarations of procedure's argument list.
106  * In either ANSI C style, in one of old rpcgen style (pass by reference),
107  * or new rpcgen style (multiple arguments, pass by value);
108  */
109 
110 /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */
111 
112 void
113 printarglist(proc_list *proc, const char *result, const char *addargname,
114     const char *addargtype)
115 {
116 
117 	decl_list *l;
118 
119 	if (!newstyle) {
120 		/* old style: always pass argument by reference */
121 		f_print(fout, "(");
122 		ptype(proc->args.decls->decl.prefix,
123 		      proc->args.decls->decl.type, 1);
124 
125 		if (mtflag) {/* Generate result field */
126 			f_print(fout, "*argp, ");
127 			ptype(proc->res_prefix, proc->res_type, 1);
128 			f_print(fout, "*%s, %s%s)\n",
129 				result, addargtype, addargname);
130 		} else
131 			f_print(fout, "*argp, %s%s)\n", addargtype, addargname);
132 	} else if (streq(proc->args.decls->decl.type, "void")) {
133 		/* newstyle, 0 argument */
134 		if (mtflag) {
135 			f_print(fout, "(");
136 			ptype(proc->res_prefix, proc->res_type, 1);
137 			f_print(fout, "*%s, %s%s)\n",
138 				result, addargtype, addargname);
139 		} else
140 			f_print(fout, "(%s%s)\n", addargtype, addargname);
141 	} else {
142 		/* new style, 1 or multiple arguments */
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 		if (mtflag) {
148 			ptype(proc->res_prefix, proc->res_type, 1);
149 			f_print(fout, "*%s, ", result);
150 
151 		}
152 		f_print(fout, "%s%s)\n", addargtype, addargname);
153 	}
154 }
155 
156 
157 
158 static const char *
159 ampr(const char *type)
160 {
161 	if (isvectordef(type, REL_ALIAS)) {
162 		return ("");
163 	} else {
164 		return ("&");
165 	}
166 }
167 
168 static void
169 printbody(proc_list *proc)
170 {
171 	decl_list *l;
172 	bool_t args2 = (proc->arg_num > 1);
173 
174 	/*
175 	 * For new style with multiple arguments, need a structure in which
176 	 *  to stuff the arguments.
177 	 */
178 
179 
180 	if (newstyle && args2) {
181 		f_print(fout, "\t%s", proc->args.argname);
182 		f_print(fout, " arg;\n");
183 	}
184 	if (!mtflag) {
185 		f_print(fout, "\tstatic ");
186 		if (streq(proc->res_type, "void")) {
187 			f_print(fout, "char ");
188 		} else {
189 			ptype(proc->res_prefix, proc->res_type, 0);
190 		}
191 		f_print(fout, "%s;\n", RESULT);
192 		f_print(fout, "\n");
193 		f_print(fout, "\tmemset((char *)%s%s, 0, sizeof (%s));\n",
194 			ampr(proc->res_type), RESULT, RESULT);
195 
196 	}
197 	if (newstyle && !args2 &&
198 	    (streq(proc->args.decls->decl.type, "void"))) {
199 		/* newstyle, 0 arguments */
200 
201 		if (mtflag)
202 			f_print(fout, "\t return ");
203 		else
204 			f_print(fout, "\t if ");
205 
206 		f_print(fout,
207 			"(clnt_call(clnt, %s,\n\t\t(xdrproc_t) xdr_void, ",
208 			proc->proc_name);
209 		f_print(fout,
210 			"(caddr_t) NULL,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,",
211 			stringfix(proc->res_type), (mtflag)?"":ampr(proc->res_type),
212 			RESULT);
213 
214 		if (mtflag)
215 			f_print(fout, "\n\t\tTIMEOUT));\n");
216 		else
217 			f_print(fout, "\n\t\tTIMEOUT) != RPC_SUCCESS) {\n");
218 
219 	} else if (newstyle && args2) {
220 		/*
221 		 * Newstyle, multiple arguments
222 		 * stuff arguments into structure
223 		 */
224 		for (l = proc->args.decls;  l != NULL; l = l->next) {
225 			f_print(fout, "\targ.%s = %s;\n",
226 				l->decl.name, l->decl.name);
227 		}
228 		if (mtflag)
229 			f_print(fout, "\treturn ");
230 		else
231 			f_print(fout, "\tif ");
232 		f_print(fout,
233 			"(clnt_call(clnt, %s,\n\t\t(xdrproc_t) xdr_%s",
234 			proc->proc_name,proc->args.argname);
235 		f_print(fout,
236 			", (caddr_t) &arg,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,",
237 			stringfix(proc->res_type), (mtflag)?"":ampr(proc->res_type),
238 			RESULT);
239 		if (mtflag)
240 			f_print(fout, "\n\t\tTIMEOUT));\n");
241 		else
242 			f_print(fout, "\n\t\tTIMEOUT) != RPC_SUCCESS) {\n");
243 	} else {		/* single argument, new or old style */
244 		if (!mtflag)
245 			f_print(fout,
246 			"\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",
247 			proc->proc_name,
248 			stringfix(proc->args.decls->decl.type),
249 			(newstyle ? "&" : ""),
250 			(newstyle ? proc->args.decls->decl.name : "argp"),
251 			stringfix(proc->res_type), ampr(proc->res_type),
252 			RESULT);
253 		else
254 
255 		f_print(fout,
256 			"\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",
257 			proc->proc_name,
258 			stringfix(proc->args.decls->decl.type),
259 			(newstyle ? "&" : ""),
260 			(newstyle ? proc->args.decls->decl.name : "argp"),
261 			stringfix(proc->res_type), "",
262 			RESULT);
263 	}
264 	if (!mtflag) {
265 		f_print(fout, "\t\treturn (NULL);\n");
266 		f_print(fout, "\t}\n");
267 
268 		if (streq(proc->res_type, "void")) {
269 			f_print(fout, "\treturn ((void *)%s%s);\n",
270 				ampr(proc->res_type), RESULT);
271 		} else {
272 			f_print(fout, "\treturn (%s%s);\n",
273 				ampr(proc->res_type), RESULT);
274 		}
275 	}
276 }
277