Bring in a transport-independent RPC (TI-RPC).
[dragonfly.git] / usr.bin / rpcgen / rpc_clntout.c
CommitLineData
984263bc
MD
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.
ce0e08e2 8 *
984263bc
MD
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.
ce0e08e2 12 *
984263bc
MD
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.
ce0e08e2 16 *
984263bc
MD
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.
ce0e08e2 20 *
984263bc
MD
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.
ce0e08e2 24 *
984263bc
MD
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
1de703da 28 *
ce0e08e2
PA
29 * @(#)rpc_clntout.c 1.15 94/04/25 SMI; 1.11 89/02/22 (C) 1987 SMI
30 * $FreeBSD: src/usr.bin/rpcgen/rpc_clntout.c,v 1.12 2005/11/13 21:17:24 dwmalone Exp $
cb0ca794 31 * $DragonFly: src/usr.bin/rpcgen/rpc_clntout.c,v 1.5 2004/06/19 16:40:36 joerg Exp $
984263bc
MD
32 */
33
984263bc
MD
34/*
35 * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler
36 * Copyright (C) 1987, Sun Microsytsems, Inc.
37 */
38#include <stdio.h>
39#include <string.h>
40#include <rpc/types.h>
41#include "rpc_parse.h"
ce0e08e2 42#include "rpc_scan.h"
984263bc
MD
43#include "rpc_util.h"
44
cb0ca794
JS
45static void write_program(definition *);
46static void printbody(proc_list *);
984263bc
MD
47
48static char RESULT[] = "clnt_res";
49
984263bc
MD
50#define DEFAULT_TIMEOUT 25 /* in seconds */
51
984263bc 52void
cb0ca794 53write_stubs(void)
984263bc
MD
54{
55 list *l;
56 definition *def;
57
58 f_print(fout,
59 "\n/* Default timeout can be changed using clnt_control() */\n");
60 f_print(fout, "static struct timeval TIMEOUT = { %d, 0 };\n",
61 DEFAULT_TIMEOUT);
62 for (l = defined; l != NULL; l = l->next) {
63 def = (definition *) l->val;
cb0ca794 64 if (def->def_kind == DEF_PROGRAM)
984263bc 65 write_program(def);
984263bc
MD
66 }
67}
68
69static void
cb0ca794 70write_program(definition *def)
984263bc
MD
71{
72 version_list *vp;
73 proc_list *proc;
74
75 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
76 for (proc = vp->procs; proc != NULL; proc = proc->next) {
77 f_print(fout, "\n");
78 if (mtflag == 0) {
79 ptype(proc->res_prefix, proc->res_type, 1);
80 f_print(fout, "*\n");
81 pvname(proc->proc_name, vp->vers_num);
82 printarglist(proc, RESULT, "clnt", "CLIENT *");
83 } else {
84 f_print(fout, "enum clnt_stat \n");
85 pvname(proc->proc_name, vp->vers_num);
86 printarglist(proc, RESULT, "clnt", "CLIENT *");
87
88 }
89 f_print(fout, "{\n");
90 printbody(proc);
91
92 f_print(fout, "}\n");
93 }
94 }
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
cb0ca794 105void
ce0e08e2
PA
106printarglist(proc_list *proc, const char *result, const char *addargname,
107 const char *addargtype)
984263bc 108{
984263bc
MD
109 decl_list *l;
110
111 if (!newstyle) {
112 /* old style: always pass argument by reference */
ce0e08e2
PA
113 f_print(fout, "(");
114 ptype(proc->args.decls->decl.prefix,
115 proc->args.decls->decl.type, 1);
116
117 if (mtflag) {/* Generate result field */
118 f_print(fout, "*argp, ");
119 ptype(proc->res_prefix, proc->res_type, 1);
120 f_print(fout, "*%s, %s%s)\n",
121 result, addargtype, addargname);
122 } else
123 f_print(fout, "*argp, %s%s)\n", addargtype, addargname);
984263bc
MD
124 } else if (streq(proc->args.decls->decl.type, "void")) {
125 /* newstyle, 0 argument */
126 if (mtflag) {
cb0ca794 127 f_print(fout, "(");
ce0e08e2
PA
128 ptype(proc->res_prefix, proc->res_type, 1);
129 f_print(fout, "*%s, %s%s)\n",
130 result, addargtype, addargname);
131 } else
984263bc 132 f_print(fout, "(%s%s)\n", addargtype, addargname);
984263bc
MD
133 } else {
134 /* new style, 1 or multiple arguments */
ce0e08e2
PA
135 f_print(fout, "(");
136 for (l = proc->args.decls; l != NULL; l = l->next) {
137 pdeclaration(proc->args.argname, &l->decl, 0, ", ");
138 }
139 if (mtflag) {
140 ptype(proc->res_prefix, proc->res_type, 1);
141 f_print(fout, "*%s, ", result);
984263bc 142
984263bc 143 }
ce0e08e2 144 f_print(fout, "%s%s)\n", addargtype, addargname);
984263bc 145 }
984263bc
MD
146}
147
148
149
ce0e08e2
PA
150static const char *
151ampr(const char *type)
984263bc 152{
cb0ca794 153 if (isvectordef(type, REL_ALIAS))
984263bc 154 return ("");
cb0ca794 155 else
984263bc 156 return ("&");
984263bc
MD
157}
158
159static void
cb0ca794 160printbody(proc_list *proc)
984263bc
MD
161{
162 decl_list *l;
163 bool_t args2 = (proc->arg_num > 1);
164
165 /*
166 * For new style with multiple arguments, need a structure in which
167 * to stuff the arguments.
168 */
169
170
171 if (newstyle && args2) {
172 f_print(fout, "\t%s", proc->args.argname);
173 f_print(fout, " arg;\n");
174 }
175 if (!mtflag) {
176 f_print(fout, "\tstatic ");
cb0ca794 177 if (streq(proc->res_type, "void"))
984263bc 178 f_print(fout, "char ");
cb0ca794 179 else
984263bc 180 ptype(proc->res_prefix, proc->res_type, 0);
984263bc
MD
181 f_print(fout, "%s;\n", RESULT);
182 f_print(fout, "\n");
183 f_print(fout, "\tmemset((char *)%s%s, 0, sizeof (%s));\n",
184 ampr(proc->res_type), RESULT, RESULT);
984263bc
MD
185 }
186 if (newstyle && !args2 &&
187 (streq(proc->args.decls->decl.type, "void"))) {
188 /* newstyle, 0 arguments */
189
190 if (mtflag)
191 f_print(fout, "\t return ");
192 else
193 f_print(fout, "\t if ");
194
195 f_print(fout,
ce0e08e2 196 "(clnt_call(clnt, %s,\n\t\t(xdrproc_t) xdr_void, ",
984263bc 197 proc->proc_name);
cb0ca794
JS
198 f_print(fout, "(caddr_t) NULL,\n\t\t(xdrproc_t) xdr_%s, "
199 "(caddr_t) %s%s,", stringfix(proc->res_type),
200 (mtflag) ? "" : ampr(proc->res_type), RESULT);
984263bc
MD
201
202 if (mtflag)
203 f_print(fout, "\n\t\tTIMEOUT));\n");
204 else
205 f_print(fout, "\n\t\tTIMEOUT) != RPC_SUCCESS) {\n");
206
207 } else if (newstyle && args2) {
208 /*
209 * Newstyle, multiple arguments
210 * stuff arguments into structure
211 */
212 for (l = proc->args.decls; l != NULL; l = l->next) {
213 f_print(fout, "\targ.%s = %s;\n",
214 l->decl.name, l->decl.name);
215 }
216 if (mtflag)
217 f_print(fout, "\treturn ");
218 else
219 f_print(fout, "\tif ");
cb0ca794 220 f_print(fout, "(clnt_call(clnt, %s,\n\t\t(xdrproc_t) xdr_%s",
984263bc 221 proc->proc_name,proc->args.argname);
cb0ca794
JS
222 f_print(fout, ", (caddr_t) &arg,\n\t\t(xdrproc_t) xdr_%s, "
223 "(caddr_t) %s%s,", stringfix(proc->res_type),
224 (mtflag) ? "" : ampr(proc->res_type), RESULT);
984263bc
MD
225 if (mtflag)
226 f_print(fout, "\n\t\tTIMEOUT));\n");
227 else
228 f_print(fout, "\n\t\tTIMEOUT) != RPC_SUCCESS) {\n");
229 } else { /* single argument, new or old style */
cb0ca794
JS
230 if (!mtflag) {
231 f_print(fout, "\tif (clnt_call(clnt, %s,\n"
232 "\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n"
233 "\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n"
234 "\t\tTIMEOUT) != RPC_SUCCESS) {\n",
235 proc->proc_name,
236 stringfix(proc->args.decls->decl.type),
237 (newstyle ? "&" : ""),
238 (newstyle) ? proc->args.decls->decl.name : "argp",
239 stringfix(proc->res_type),
240 ampr(proc->res_type), RESULT);
241 } else {
242 f_print(fout, "\treturn (clnt_call(clnt, %s,\n"
243 "\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n"
244 "\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n"
245 "\t\tTIMEOUT));\n", proc->proc_name,
246 stringfix(proc->args.decls->decl.type),
247 (newstyle ? "&" : ""),
248 (newstyle) ? proc->args.decls->decl.name : "argp",
249 stringfix(proc->res_type), "", RESULT);
250 }
984263bc
MD
251 }
252 if (!mtflag) {
253 f_print(fout, "\t\treturn (NULL);\n");
254 f_print(fout, "\t}\n");
255
256 if (streq(proc->res_type, "void")) {
257 f_print(fout, "\treturn ((void *)%s%s);\n",
258 ampr(proc->res_type), RESULT);
259 } else {
260 f_print(fout, "\treturn (%s%s);\n",
261 ampr(proc->res_type), RESULT);
262 }
263 }
264}