Merge from vendor branch BINUTILS:
[dragonfly.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  * @(#)rpc_clntout.c 1.11 89/02/22 (C) 1987 SMI
30  * $DragonFly: src/usr.bin/rpcgen/rpc_clntout.c,v 1.4 2003/11/03 19:31:32 eirikn Exp $
31  */
32
33 #ident  "@(#)rpc_clntout.c      1.15    94/04/25 SMI"
34
35 /*
36  * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler
37  * Copyright (C) 1987, Sun Microsytsems, 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
45 extern void pdeclaration( char *, declaration *, int, char * );
46 void printarglist( proc_list *, char *, char *, char *);
47 static void write_program( definition * );
48 static void printbody( proc_list * );
49
50 static char RESULT[] = "clnt_res";
51
52
53 #define DEFAULT_TIMEOUT 25      /* in seconds */
54
55
56 void
57 write_stubs()
58 {
59         list *l;
60         definition *def;
61
62         f_print(fout,
63                 "\n/* Default timeout can be changed using clnt_control() */\n");
64         f_print(fout, "static struct timeval TIMEOUT = { %d, 0 };\n",
65                 DEFAULT_TIMEOUT);
66         for (l = defined; l != NULL; l = l->next) {
67                 def = (definition *) l->val;
68                 if (def->def_kind == DEF_PROGRAM) {
69                         write_program(def);
70                 }
71         }
72 }
73
74 static void
75 write_program(def)
76         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                 for (proc = vp->procs; proc != NULL; proc = proc->next) {
83                         f_print(fout, "\n");
84                         if (mtflag == 0) {
85                                 ptype(proc->res_prefix, proc->res_type, 1);
86                                 f_print(fout, "*\n");
87                                 pvname(proc->proc_name, vp->vers_num);
88                                 printarglist(proc, RESULT, "clnt", "CLIENT *");
89                         } else {
90                                 f_print(fout, "enum clnt_stat \n");
91                                 pvname(proc->proc_name, vp->vers_num);
92                                 printarglist(proc, RESULT,  "clnt", "CLIENT *");
93
94                         }
95                         f_print(fout, "{\n");
96                         printbody(proc);
97
98                         f_print(fout, "}\n");
99                 }
100         }
101 }
102
103 /*
104  * Writes out declarations of procedure's argument list.
105  * In either ANSI C style, in one of old rpcgen style (pass by reference),
106  * or new rpcgen style (multiple arguments, pass by value);
107  */
108
109 /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */
110
111 void printarglist(proc, result, addargname, addargtype)
112         proc_list *proc;
113         char *result;
114         char* addargname, * addargtype;
115 {
116
117         decl_list *l;
118
119         if (!newstyle) {
120                 /* old style: always pass argument by reference */
121                 if (Cflag) {    /* C++ style heading */
122                         f_print(fout, "(");
123                         ptype(proc->args.decls->decl.prefix,
124                               proc->args.decls->decl.type, 1);
125
126                         if (mtflag) {/* Generate result field */
127                                 f_print(fout, "*argp, ");
128                                 ptype(proc->res_prefix, proc->res_type, 1);
129                                 f_print(fout, "*%s, %s%s)\n",
130                                         result, addargtype, addargname);
131                         } else
132                                 f_print(fout, "*argp, %s%s)\n", addargtype, addargname);
133                 } else {
134                         if (!mtflag)
135                                 f_print(fout, "(argp, %s)\n", addargname);
136                         else
137                                 f_print(fout, "(argp, %s, %s)\n",
138                                         result, addargname);
139                         f_print(fout, "\t");
140                         ptype(proc->args.decls->decl.prefix,
141                               proc->args.decls->decl.type, 1);
142                         f_print(fout, "*argp;\n");
143                         if (mtflag) {
144                                 f_print(fout, "\t");
145                                 ptype(proc->res_prefix, proc->res_type, 1);
146                                 f_print(fout, "*%s;\n", result);
147                         }
148                 }
149         } else if (streq(proc->args.decls->decl.type, "void")) {
150                 /* newstyle, 0 argument */
151                 if (mtflag) {
152                                 f_print(fout, "(");
153
154                                 
155                         if (Cflag) {
156                                 ptype(proc->res_prefix, proc->res_type, 1);
157                                 f_print(fout, "*%s, %s%s)\n",
158                                         result, addargtype, addargname);
159                         }
160                         else
161                                 f_print(fout, "(%s)\n", addargname);                    
162
163                 } else
164                 if (Cflag)
165                         f_print(fout, "(%s%s)\n", addargtype, addargname);
166                 else
167                         f_print(fout, "(%s)\n", addargname);
168         } else {
169                 /* new style, 1 or multiple arguments */
170                 if (!Cflag) {
171                         f_print(fout, "(");
172                         for (l = proc->args.decls;  l != NULL; l = l->next)
173                                 f_print(fout, "%s, ", l->decl.name);
174                         if (mtflag)
175                                 f_print(fout, "%s, ", result); 
176
177                         f_print(fout, "%s)\n", addargname);
178                         for (l = proc->args.decls; l != NULL; l = l->next) {
179                                 pdeclaration(proc->args.argname,
180                                              &l->decl, 1, ";\n");
181                         }
182                         if (mtflag) {
183                                 f_print(fout, "\t");
184                                 ptype(proc->res_prefix, proc->res_type, 1);
185                                 f_print(fout, "*%s;\n", result);
186                         }
187
188                 } else {        /* C++ style header */
189                         f_print(fout, "(");
190                         for (l = proc->args.decls; l != NULL; l = l->next) {
191                                 pdeclaration(proc->args.argname, &l->decl, 0,
192                                              ", ");
193                         }
194                         if (mtflag) {
195                                 ptype(proc->res_prefix, proc->res_type, 1);
196                                 f_print(fout, "*%s, ", result);
197
198                         }
199                         f_print(fout, "%s%s)\n", addargtype, addargname);
200                 }
201         }
202
203         if (!Cflag)
204                 f_print(fout, "\t%s%s;\n", addargtype, addargname);
205 }
206
207
208
209 static char *
210 ampr(type)
211         char *type;
212 {
213         if (isvectordef(type, REL_ALIAS)) {
214                 return ("");
215         } else {
216                 return ("&");
217         }
218 }
219
220 static void
221 printbody(proc)
222         proc_list *proc;
223 {
224         decl_list *l;
225         bool_t args2 = (proc->arg_num > 1);
226
227         /*
228          * For new style with multiple arguments, need a structure in which
229          *  to stuff the arguments.
230          */
231         
232
233         if (newstyle && args2) {
234                 f_print(fout, "\t%s", proc->args.argname);
235                 f_print(fout, " arg;\n");
236         }
237         if (!mtflag) {
238                 f_print(fout, "\tstatic ");
239                 if (streq(proc->res_type, "void")) {
240                         f_print(fout, "char ");
241                 } else {
242                         ptype(proc->res_prefix, proc->res_type, 0);
243                 }
244                 f_print(fout, "%s;\n", RESULT);
245                 f_print(fout, "\n");
246                 f_print(fout, "\tmemset((char *)%s%s, 0, sizeof (%s));\n",
247                         ampr(proc->res_type), RESULT, RESULT);
248
249         }
250         if (newstyle && !args2 &&
251             (streq(proc->args.decls->decl.type, "void"))) {
252                 /* newstyle, 0 arguments */
253
254                 if (mtflag)
255                         f_print(fout, "\t return ");
256                 else
257                         f_print(fout, "\t if ");
258
259                 f_print(fout,
260                         "(clnt_call(clnt, %s,\n\t\t(xdrproc_t) xdr_void, ", 
261                         proc->proc_name);
262                 f_print(fout,
263                         "(caddr_t) NULL,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,",
264                         stringfix(proc->res_type), (mtflag)?"":ampr(proc->res_type),
265                         RESULT);
266
267                 if (mtflag)
268                         f_print(fout, "\n\t\tTIMEOUT));\n");
269                 else
270                         f_print(fout, "\n\t\tTIMEOUT) != RPC_SUCCESS) {\n");
271
272         } else if (newstyle && args2) {
273                 /*
274                  * Newstyle, multiple arguments
275                  * stuff arguments into structure
276                  */
277                 for (l = proc->args.decls;  l != NULL; l = l->next) {
278                         f_print(fout, "\targ.%s = %s;\n",
279                                 l->decl.name, l->decl.name);
280                 }
281                 if (mtflag)
282                         f_print(fout, "\treturn ");
283                 else
284                         f_print(fout, "\tif ");                 
285                 f_print(fout,
286                         "(clnt_call(clnt, %s,\n\t\t(xdrproc_t) xdr_%s",
287                         proc->proc_name,proc->args.argname);
288                 f_print(fout,
289                         ", (caddr_t) &arg,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,",
290                         stringfix(proc->res_type), (mtflag)?"":ampr(proc->res_type),
291                         RESULT);
292                 if (mtflag)
293                         f_print(fout, "\n\t\tTIMEOUT));\n");
294                 else
295                         f_print(fout, "\n\t\tTIMEOUT) != RPC_SUCCESS) {\n");
296         } else {                /* single argument, new or old style */
297                 if (!mtflag)
298                         f_print(fout,
299                         "\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",
300                         proc->proc_name,
301                         stringfix(proc->args.decls->decl.type),
302                         (newstyle ? "&" : ""),
303                         (newstyle ? proc->args.decls->decl.name : "argp"),
304                         stringfix(proc->res_type), ampr(proc->res_type),
305                         RESULT);
306                 else
307                         
308                 f_print(fout,
309                         "\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",
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), "",
315                         RESULT);
316         }
317         if (!mtflag) {
318                 f_print(fout, "\t\treturn (NULL);\n");
319                 f_print(fout, "\t}\n");
320
321                 if (streq(proc->res_type, "void")) {
322                         f_print(fout, "\treturn ((void *)%s%s);\n",
323                                 ampr(proc->res_type), RESULT);
324                 } else {
325                         f_print(fout, "\treturn (%s%s);\n",
326                                 ampr(proc->res_type), RESULT);
327                 }
328         }
329 }