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