]> git.decadent.org.uk Git - nfs-utils.git/blob - tools/rpcgen/rpc_clntout.c
Autogen update
[nfs-utils.git] / tools / 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 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 #ifndef lint
32 static char sccsid[] = "@(#)rpc_clntout.c 1.11 89/02/22 (C) 1987 SMI";
33 #endif
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 #include "rpc_output.h"
45
46 /* extern pdeclaration(); */
47 /* void printarglist(); */
48
49 #define DEFAULT_TIMEOUT 25      /* in seconds */
50 static char RESULT[] = "clnt_res";
51
52 static void     write_program(definition *def);
53 static void     printbody(proc_list *proc);
54
55
56 void
57 write_stubs(void)
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(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                         ptype(proc->res_prefix, proc->res_type, 1);
84                         f_print(fout, "*\n");
85                         pvname(proc->proc_name, vp->vers_num);
86                         printarglist(proc, "clnt", "CLIENT *");
87                         f_print(fout, "{\n");
88                         printbody(proc);
89                         f_print(fout, "}\n");
90                 }
91         }
92 }
93
94 /*
95  * Writes out declarations of procedure's argument list.
96  * In either ANSI C style, in one of old rpcgen style (pass by reference),
97  * or new rpcgen style (multiple arguments, pass by value);
98  */
99
100 /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */
101
102 void
103 printarglist(proc_list *proc, char *addargname, char *addargtype)
104 {
105
106         decl_list      *l;
107
108         if (!newstyle) {        /* old style: always pass arg by reference */
109                 if (Cflag) {    /* C++ style heading */
110                         f_print(fout, "(");
111                         ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
112                         f_print(fout, "*argp, %s%s)\n", addargtype, addargname);
113                 } else {
114                         f_print(fout, "(argp, %s)\n", addargname);
115                         f_print(fout, "\t");
116                         ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
117                         f_print(fout, "*argp;\n");
118                 }
119         } else if (streq(proc->args.decls->decl.type, "void")) {
120                 /* newstyle, 0 argument */
121                 if (Cflag)
122                         f_print(fout, "(%s%s)\n", addargtype, addargname);
123                 else
124                         f_print(fout, "(%s)\n", addargname);
125         } else {
126                 /* new style, 1 or multiple arguments */
127                 if (!Cflag) {
128                         f_print(fout, "(");
129                         for (l = proc->args.decls; l != NULL; l = l->next)
130                                 f_print(fout, "%s, ", l->decl.name);
131                         f_print(fout, "%s)\n", addargname);
132                         for (l = proc->args.decls; l != NULL; l = l->next) {
133                                 pdeclaration(proc->args.argname, &l->decl, 1, ";\n");
134                         }
135                 } else {        /* C++ style header */
136                         f_print(fout, "(");
137                         for (l = proc->args.decls; l != NULL; l = l->next) {
138                                 pdeclaration(proc->args.argname, &l->decl, 0, ", ");
139                         }
140                         f_print(fout, " %s%s)\n", addargtype, addargname);
141                 }
142         }
143
144         if (!Cflag)
145                 f_print(fout, "\t%s%s;\n", addargtype, addargname);
146 }
147
148
149
150 static char *
151 ampr(char *type)
152 {
153         if (isvectordef(type, REL_ALIAS)) {
154                 return ("");
155         } else {
156                 return ("&");
157         }
158 }
159
160 static void
161 printbody(proc_list *proc)
162 {
163         decl_list      *l;
164         bool_t          args2 = (proc->arg_num > 1);
165
166         /* For new style with multiple arguments, need a structure in which
167          * to stuff the arguments. */
168         if (newstyle && args2) {
169                 f_print(fout, "\t%s", proc->args.argname);
170                 f_print(fout, " arg;\n");
171         }
172         f_print(fout, "\tstatic ");
173         if (streq(proc->res_type, "void")) {
174                 f_print(fout, "char ");
175         } else {
176                 ptype(proc->res_prefix, proc->res_type, 0);
177         }
178         f_print(fout, "%s;\n", RESULT);
179         f_print(fout, "\n");
180         f_print(fout, "\tmemset((char *)%s%s, 0, sizeof(%s));\n",
181                 ampr(proc->res_type), RESULT, RESULT);
182         if (newstyle && !args2 && (streq(proc->args.decls->decl.type, "void"))) {
183                 /* newstyle, 0 arguments */
184                 f_print(fout,
185                         "\tif (clnt_call(clnt, %s, (xdrproc_t) xdr_void, (caddr_t) NULL, "
186                         "(xdrproc_t) xdr_%s, (caddr_t) %s%s, TIMEOUT) != RPC_SUCCESS) {\n",
187                         proc->proc_name,
188                         stringfix(proc->res_type), ampr(proc->res_type), RESULT);
189
190         } else if (newstyle && args2) {
191                 /* newstyle, multiple arguments:  stuff arguments into structure */
192                 for (l = proc->args.decls; l != NULL; l = l->next) {
193                         f_print(fout, "\targ.%s = %s;\n",
194                                 l->decl.name, l->decl.name);
195                 }
196                 f_print(fout,
197                         "\tif (clnt_call(clnt, %s, (xdrproc_t) xdr_%s, (caddr_t) &arg, "
198                         "(xdrproc_t) xdr_%s, (caddr_t) %s%s, TIMEOUT) != RPC_SUCCESS) {\n",
199                         proc->proc_name, proc->args.argname,
200                         stringfix(proc->res_type), ampr(proc->res_type), RESULT);
201         } else {                /* single argument, new or old style */
202                 f_print(fout,
203                         "\tif (clnt_call(clnt, %s, (xdrproc_t) xdr_%s, "
204                         "(caddr_t) %s%s, (xdrproc_t) xdr_%s, (caddr_t) %s%s, TIMEOUT) != RPC_SUCCESS) {\n",
205                         proc->proc_name,
206                         stringfix(proc->args.decls->decl.type),
207                         (newstyle ? "&" : ""),
208                         (newstyle ? proc->args.decls->decl.name : "argp"),
209                         stringfix(proc->res_type), ampr(proc->res_type), RESULT);
210         }
211         f_print(fout, "\t\treturn (NULL);\n");
212         f_print(fout, "\t}\n");
213         if (streq(proc->res_type, "void")) {
214                 f_print(fout, "\treturn ((void *)%s%s);\n",
215                         ampr(proc->res_type), RESULT);
216         } else {
217                 f_print(fout, "\treturn (%s%s);\n", ampr(proc->res_type), RESULT);
218         }
219 }