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