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