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