rpc_clntout.c revision 1.13 1 1.13 jmc /* $NetBSD: rpc_clntout.c,v 1.13 2004/06/20 22:20:16 jmc Exp $ */
2 1.1 glass /*
3 1.1 glass * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 1.1 glass * unrestricted use provided that this legend is included on all tape
5 1.1 glass * media and as a part of the software program in whole or part. Users
6 1.1 glass * may copy or modify Sun RPC without charge, but are not authorized
7 1.1 glass * to license or distribute it to anyone else except as part of a product or
8 1.4 pk * program developed by the user or with the express written consent of
9 1.4 pk * Sun Microsystems, Inc.
10 1.4 pk *
11 1.1 glass * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 1.1 glass * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 1.1 glass * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 1.4 pk *
15 1.1 glass * Sun RPC is provided with no support and without any obligation on the
16 1.1 glass * part of Sun Microsystems, Inc. to assist in its use, correction,
17 1.1 glass * modification or enhancement.
18 1.4 pk *
19 1.1 glass * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 1.1 glass * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 1.1 glass * OR ANY PART THEREOF.
22 1.4 pk *
23 1.1 glass * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 1.1 glass * or profits or other special, indirect and consequential damages, even if
25 1.1 glass * Sun has been advised of the possibility of such damages.
26 1.4 pk *
27 1.1 glass * Sun Microsystems, Inc.
28 1.1 glass * 2550 Garcia Avenue
29 1.1 glass * Mountain View, California 94043
30 1.1 glass */
31 1.4 pk
32 1.13 jmc #if HAVE_NBTOOL_CONFIG_H
33 1.13 jmc #include "nbtool_config.h"
34 1.13 jmc #endif
35 1.13 jmc
36 1.7 christos #include <sys/cdefs.h>
37 1.12 tv #if defined(__RCSID) && !defined(lint)
38 1.7 christos #if 0
39 1.4 pk static char sccsid[] = "@(#)rpc_clntout.c 1.11 89/02/22 (C) 1987 SMI";
40 1.7 christos #else
41 1.13 jmc __RCSID("$NetBSD: rpc_clntout.c,v 1.13 2004/06/20 22:20:16 jmc Exp $");
42 1.7 christos #endif
43 1.1 glass #endif
44 1.1 glass
45 1.1 glass /*
46 1.1 glass * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler
47 1.1 glass * Copyright (C) 1987, Sun Microsytsems, Inc.
48 1.1 glass */
49 1.1 glass #include <stdio.h>
50 1.4 pk #include <string.h>
51 1.4 pk #include <rpc/types.h>
52 1.7 christos #include "rpc_scan.h"
53 1.1 glass #include "rpc_parse.h"
54 1.1 glass #include "rpc_util.h"
55 1.1 glass
56 1.7 christos static void write_program __P((definition *));
57 1.7 christos static char *ampr __P((char *));
58 1.9 mycroft static char *aster __P((char *));
59 1.4 pk static void printbody __P((proc_list *));
60 1.4 pk
61 1.1 glass #define DEFAULT_TIMEOUT 25 /* in seconds */
62 1.4 pk static char RESULT[] = "clnt_res";
63 1.1 glass
64 1.1 glass
65 1.1 glass void
66 1.1 glass write_stubs()
67 1.1 glass {
68 1.8 lukem list *l;
69 1.1 glass definition *def;
70 1.1 glass
71 1.8 lukem f_print(fout,
72 1.8 lukem "\n/* Default timeout can be changed using clnt_control() */\n");
73 1.4 pk f_print(fout, "static struct timeval TIMEOUT = { %d, 0 };\n",
74 1.8 lukem DEFAULT_TIMEOUT);
75 1.1 glass for (l = defined; l != NULL; l = l->next) {
76 1.1 glass def = (definition *) l->val;
77 1.1 glass if (def->def_kind == DEF_PROGRAM) {
78 1.1 glass write_program(def);
79 1.1 glass }
80 1.1 glass }
81 1.1 glass }
82 1.1 glass
83 1.7 christos static void
84 1.1 glass write_program(def)
85 1.1 glass definition *def;
86 1.1 glass {
87 1.1 glass version_list *vp;
88 1.1 glass proc_list *proc;
89 1.1 glass
90 1.1 glass for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
91 1.1 glass for (proc = vp->procs; proc != NULL; proc = proc->next) {
92 1.1 glass f_print(fout, "\n");
93 1.9 mycroft if (Mflag)
94 1.9 mycroft f_print(fout, "enum clnt_stat\n");
95 1.9 mycroft else {
96 1.9 mycroft ptype(proc->res_prefix, proc->res_type, 1);
97 1.9 mycroft f_print(fout, "*\n");
98 1.9 mycroft }
99 1.1 glass pvname(proc->proc_name, vp->vers_num);
100 1.9 mycroft printarglist(proc, RESULT, "clnt", "CLIENT *");
101 1.1 glass f_print(fout, "{\n");
102 1.1 glass printbody(proc);
103 1.4 pk f_print(fout, "}\n");
104 1.1 glass }
105 1.1 glass }
106 1.1 glass }
107 1.4 pk /* Writes out declarations of procedure's argument list.
108 1.4 pk In either ANSI C style, in one of old rpcgen style (pass by reference),
109 1.4 pk or new rpcgen style (multiple arguments, pass by value);
110 1.4 pk */
111 1.4 pk
112 1.4 pk /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */
113 1.4 pk
114 1.8 lukem void
115 1.9 mycroft printarglist(proc, result, addargname, addargtype)
116 1.8 lukem proc_list *proc;
117 1.9 mycroft char *result, *addargname, *addargtype;
118 1.4 pk {
119 1.4 pk
120 1.8 lukem decl_list *l;
121 1.4 pk
122 1.8 lukem if (!newstyle) { /* old style: always pass argument by
123 1.8 lukem * reference */
124 1.8 lukem if (Cflag) { /* C++ style heading */
125 1.8 lukem f_print(fout, "(");
126 1.8 lukem ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
127 1.9 mycroft f_print(fout, "*argp, ");
128 1.9 mycroft if (Mflag) {
129 1.9 mycroft if (streq(proc->res_type, "void"))
130 1.9 mycroft f_print(fout, "char ");
131 1.9 mycroft else
132 1.9 mycroft ptype(proc->res_prefix, proc->res_type, 0);
133 1.9 mycroft f_print(fout, "%s%s, ", aster(proc->res_type),
134 1.9 mycroft result);
135 1.9 mycroft }
136 1.9 mycroft f_print(fout, "%s%s)\n", addargtype, addargname);
137 1.8 lukem } else {
138 1.9 mycroft f_print(fout, "(argp, ");
139 1.9 mycroft if (Mflag)
140 1.9 mycroft f_print(fout, "%s, ", result);
141 1.9 mycroft f_print(fout, "%s)\n", addargname);
142 1.8 lukem f_print(fout, "\t");
143 1.8 lukem ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
144 1.8 lukem f_print(fout, "*argp;\n");
145 1.9 mycroft if (Mflag) {
146 1.9 mycroft f_print(fout, "\t");
147 1.9 mycroft if (streq(proc->res_type, "void"))
148 1.9 mycroft f_print(fout, "char ");
149 1.9 mycroft else
150 1.9 mycroft ptype(proc->res_prefix, proc->res_type, 0);
151 1.9 mycroft f_print(fout, "%s%s;\n", aster(proc->res_type),
152 1.9 mycroft result);
153 1.9 mycroft }
154 1.8 lukem }
155 1.10 mycroft } else {
156 1.10 mycroft f_print(fout, "(");
157 1.10 mycroft if (!streq(proc->args.decls->decl.type, "void")) {
158 1.8 lukem /* new style, 1 or multiple arguments */
159 1.8 lukem if (!Cflag) {
160 1.10 mycroft for (l = proc->args.decls; l != NULL;
161 1.10 mycroft l = l->next)
162 1.8 lukem f_print(fout, "%s, ", l->decl.name);
163 1.8 lukem } else {/* C++ style header */
164 1.10 mycroft for (l = proc->args.decls; l != NULL;
165 1.10 mycroft l = l->next)
166 1.10 mycroft pdeclaration(proc->args.argname,
167 1.10 mycroft &l->decl, 0, ", ");
168 1.10 mycroft }
169 1.10 mycroft }
170 1.10 mycroft if (!Cflag) {
171 1.10 mycroft if (Mflag) {
172 1.10 mycroft f_print(fout, "\t");
173 1.10 mycroft if (streq(proc->res_type, "void"))
174 1.10 mycroft f_print(fout, "char ");
175 1.10 mycroft else
176 1.10 mycroft ptype(proc->res_prefix, proc->res_type, 0);
177 1.10 mycroft f_print(fout, "%s%s;\n", aster(proc->res_type),
178 1.10 mycroft result);
179 1.10 mycroft }
180 1.10 mycroft f_print(fout, "%s)\n", addargname);
181 1.10 mycroft if (!streq(proc->args.decls->decl.type, "void")) {
182 1.10 mycroft for (l = proc->args.decls; l != NULL;
183 1.10 mycroft l = l->next)
184 1.10 mycroft pdeclaration(proc->args.argname,
185 1.10 mycroft &l->decl, 1, ";\n");
186 1.10 mycroft }
187 1.10 mycroft } else {
188 1.10 mycroft if (Mflag) {
189 1.10 mycroft if (streq(proc->res_type, "void"))
190 1.10 mycroft f_print(fout, "char ");
191 1.10 mycroft else
192 1.10 mycroft ptype(proc->res_prefix, proc->res_type, 0);
193 1.10 mycroft f_print(fout, "%s%s, ", aster(proc->res_type),
194 1.10 mycroft result);
195 1.8 lukem }
196 1.10 mycroft f_print(fout, "%s%s)\n", addargtype, addargname);
197 1.8 lukem }
198 1.10 mycroft }
199 1.4 pk
200 1.8 lukem if (!Cflag)
201 1.8 lukem f_print(fout, "\t%s%s;\n", addargtype, addargname);
202 1.4 pk }
203 1.4 pk
204 1.4 pk
205 1.1 glass static char *
206 1.1 glass ampr(type)
207 1.8 lukem char *type;
208 1.1 glass {
209 1.1 glass if (isvectordef(type, REL_ALIAS)) {
210 1.1 glass return ("");
211 1.1 glass } else {
212 1.1 glass return ("&");
213 1.1 glass }
214 1.1 glass }
215 1.1 glass
216 1.9 mycroft static char *
217 1.9 mycroft aster(type)
218 1.9 mycroft char *type;
219 1.9 mycroft {
220 1.9 mycroft if (isvectordef(type, REL_ALIAS)) {
221 1.9 mycroft return ("");
222 1.9 mycroft } else {
223 1.9 mycroft return ("*");
224 1.9 mycroft }
225 1.9 mycroft }
226 1.9 mycroft
227 1.4 pk static void
228 1.1 glass printbody(proc)
229 1.1 glass proc_list *proc;
230 1.1 glass {
231 1.8 lukem decl_list *l;
232 1.8 lukem bool_t args2 = (proc->arg_num > 1);
233 1.4 pk
234 1.8 lukem /* For new style with multiple arguments, need a structure in which to
235 1.8 lukem * stuff the arguments. */
236 1.8 lukem if (newstyle && args2) {
237 1.4 pk f_print(fout, "\t%s", proc->args.argname);
238 1.8 lukem f_print(fout, " arg;\n");
239 1.4 pk }
240 1.9 mycroft if (!Mflag) {
241 1.9 mycroft f_print(fout, "\tstatic ");
242 1.9 mycroft if (streq(proc->res_type, "void"))
243 1.9 mycroft f_print(fout, "char ");
244 1.9 mycroft else
245 1.9 mycroft ptype(proc->res_prefix, proc->res_type, 0);
246 1.9 mycroft f_print(fout, "%s;\n", RESULT);
247 1.1 glass }
248 1.1 glass f_print(fout, "\n");
249 1.9 mycroft if (!Mflag)
250 1.9 mycroft f_print(fout, "\tmemset((char *)%s%s, 0, sizeof(%s));\n",
251 1.9 mycroft ampr(proc->res_type), RESULT, RESULT);
252 1.8 lukem if (newstyle && !args2 && (streq(proc->args.decls->decl.type, "void"))) {
253 1.8 lukem /* newstyle, 0 arguments */
254 1.9 mycroft if (Mflag) {
255 1.9 mycroft f_print(fout, "\treturn (clnt_call(clnt, %s, xdr_void",
256 1.9 mycroft proc->proc_name);
257 1.9 mycroft f_print(fout, ", NULL, xdr_%s, %s, TIMEOUT));\n",
258 1.9 mycroft stringfix(proc->res_type), RESULT);
259 1.9 mycroft } else {
260 1.9 mycroft f_print(fout, "\tif (clnt_call(clnt, %s, xdr_void, ",
261 1.9 mycroft proc->proc_name);
262 1.9 mycroft f_print(fout,
263 1.11 kleink "NULL, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS)\n",
264 1.9 mycroft stringfix(proc->res_type), ampr(proc->res_type),
265 1.9 mycroft RESULT);
266 1.9 mycroft }
267 1.9 mycroft } else {
268 1.8 lukem if (newstyle && args2) {
269 1.8 lukem /* newstyle, multiple arguments: stuff arguments into
270 1.8 lukem * structure */
271 1.8 lukem for (l = proc->args.decls; l != NULL; l = l->next) {
272 1.8 lukem f_print(fout, "\targ.%s = %s;\n",
273 1.8 lukem l->decl.name, l->decl.name);
274 1.8 lukem }
275 1.9 mycroft if (Mflag) {
276 1.9 mycroft f_print(fout,
277 1.9 mycroft "\treturn (clnt_call(clnt, %s, xdr_%s, &arg, xdr_%s, %s, TIMEOUT));\n",
278 1.9 mycroft proc->proc_name, proc->args.argname,
279 1.9 mycroft stringfix(proc->res_type), RESULT);
280 1.9 mycroft } else {
281 1.9 mycroft f_print(fout,
282 1.9 mycroft "\tif (clnt_call(clnt, %s, xdr_%s, &arg, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS)\n",
283 1.9 mycroft proc->proc_name, proc->args.argname,
284 1.9 mycroft stringfix(proc->res_type),
285 1.9 mycroft ampr(proc->res_type), RESULT);
286 1.9 mycroft }
287 1.9 mycroft } else { /* single argument, new or old style */
288 1.9 mycroft if (Mflag) {
289 1.9 mycroft f_print(fout,
290 1.9 mycroft "\treturn (clnt_call(clnt, %s, xdr_%s, %s%s, xdr_%s, %s, TIMEOUT));\n",
291 1.9 mycroft proc->proc_name,
292 1.9 mycroft stringfix(proc->args.decls->decl.type),
293 1.9 mycroft (newstyle ? "&" : ""),
294 1.9 mycroft (newstyle ? proc->args.decls->decl.name : "argp"),
295 1.9 mycroft stringfix(proc->res_type), RESULT);
296 1.9 mycroft } else {
297 1.9 mycroft f_print(fout,
298 1.9 mycroft "\tif (clnt_call(clnt, %s, xdr_%s, %s%s, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS)\n",
299 1.9 mycroft proc->proc_name,
300 1.9 mycroft stringfix(proc->args.decls->decl.type),
301 1.9 mycroft (newstyle ? "&" : ""),
302 1.9 mycroft (newstyle ? proc->args.decls->decl.name : "argp"),
303 1.9 mycroft stringfix(proc->res_type),
304 1.9 mycroft ampr(proc->res_type), RESULT);
305 1.9 mycroft }
306 1.9 mycroft }
307 1.9 mycroft }
308 1.9 mycroft if (!Mflag) {
309 1.9 mycroft f_print(fout, "\t\treturn (NULL);\n");
310 1.9 mycroft if (streq(proc->res_type, "void"))
311 1.9 mycroft f_print(fout, "\treturn ((void *)%s%s);\n",
312 1.8 lukem ampr(proc->res_type), RESULT);
313 1.9 mycroft else
314 1.9 mycroft f_print(fout, "\treturn (%s%s);\n",
315 1.8 lukem ampr(proc->res_type), RESULT);
316 1.1 glass }
317 1.1 glass }
318