xref: /netbsd-src/usr.bin/rpcgen/rpc_clntout.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: rpc_clntout.c,v 1.4 1995/06/11 21:49:52 pk Exp $	*/
2 /*
3  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4  * unrestricted use provided that this legend is included on all tape
5  * media and as a part of the software program in whole or part.  Users
6  * may copy or modify Sun RPC without charge, but are not authorized
7  * to license or distribute it to anyone else except as part of a product or
8  * program developed by the user or with the express written consent of
9  * Sun Microsystems, Inc.
10  *
11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14  *
15  * Sun RPC is provided with no support and without any obligation on the
16  * part of Sun Microsystems, Inc. to assist in its use, correction,
17  * modification or enhancement.
18  *
19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21  * OR ANY PART THEREOF.
22  *
23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24  * or profits or other special, indirect and consequential damages, even if
25  * Sun has been advised of the possibility of such damages.
26  *
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
30  */
31 
32 #ifndef lint
33 static char sccsid[] = "@(#)rpc_clntout.c 1.11 89/02/22 (C) 1987 SMI";
34 #endif
35 
36 /*
37  * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler
38  * Copyright (C) 1987, Sun Microsytsems, Inc.
39  */
40 #include <stdio.h>
41 #include <string.h>
42 #include <rpc/types.h>
43 #include "rpc_parse.h"
44 #include "rpc_util.h"
45 
46 static write_program __P((definition *));
47 static void printbody __P((proc_list *));
48 
49 extern pdeclaration();
50 
51 #define DEFAULT_TIMEOUT 25	/* in seconds */
52 static char RESULT[] = "clnt_res";
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
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 			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 /* Writes out declarations of procedure's argument list.
95    In either ANSI C style, in one of old rpcgen style (pass by reference),
96    or new rpcgen style (multiple arguments, pass by value);
97    */
98 
99 /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */
100 
101 void printarglist( proc, addargname, addargtype )
102      proc_list *proc;
103      char *addargname, *addargtype;
104 {
105 
106   decl_list *l;
107 
108   if (!newstyle) {    /* old style: always pass argument 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(type)
152 	char *type;
153 {
154 	if (isvectordef(type, REL_ALIAS)) {
155 		return ("");
156 	} else {
157 		return ("&");
158 	}
159 }
160 
161 static void
162 printbody(proc)
163 	proc_list *proc;
164 {
165   decl_list *l;
166   bool_t args2 = (proc->arg_num > 1);
167   int i;
168 
169   /* For new style with multiple arguments, need a structure in which
170      to stuff the arguments. */
171 	if ( newstyle && args2) {
172 		f_print(fout, "\t%s", proc->args.argname);
173 		f_print(fout, " arg;\n");
174 	}
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 	}
181 	f_print(fout, "%s;\n",RESULT);
182 	f_print(fout, "\n");
183         f_print(fout, "\tmemset((char *)%s%s, 0, sizeof(%s));\n",
184 		ampr(proc->res_type ), RESULT, RESULT);
185 	if (newstyle && !args2 && (streq( proc->args.decls->decl.type, "void"))) {
186 	  /* newstyle, 0 arguments */
187 	  f_print(fout,
188 		    "\tif (clnt_call(clnt, %s, xdr_void", proc->proc_name);
189 	  f_print(fout,
190  		  ", NULL, xdr_%s, %s,%s, TIMEOUT) != RPC_SUCCESS) {\n",
191  		  stringfix(proc->res_type), ampr(proc->res_type), RESULT);
192 
193 	} else if ( newstyle && args2) {
194 	  /* newstyle, multiple arguments:  stuff arguments into structure */
195 	  for (l = proc->args.decls;  l != NULL; l = l->next) {
196 	    f_print(fout, "\targ.%s = %s;\n",
197 		    l->decl.name, l->decl.name);
198 	  }
199 	  f_print(fout,
200 		  "\tif (clnt_call(clnt, %s, xdr_%s", proc->proc_name,
201 		  proc->args.argname);
202 	  f_print(fout,
203  		  ", &arg, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS) {\n",
204  		  stringfix(proc->res_type),
205 		  ampr(proc->res_type), RESULT);
206 	} else {  /* single argument, new or old style */
207 	      f_print(fout,
208  		      "\tif (clnt_call(clnt, %s, xdr_%s, %s%s, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS) {\n",
209 		      proc->proc_name,
210 		      stringfix(proc->args.decls->decl.type),
211 		      (newstyle ? "&" : ""),
212 		      (newstyle ? proc->args.decls->decl.name : "argp"),
213 		      stringfix(proc->res_type),
214 		      ampr(proc->res_type),RESULT);
215 	    }
216 	f_print(fout, "\t\treturn (NULL);\n");
217 	f_print(fout, "\t}\n");
218 	if (streq(proc->res_type, "void")) {
219 		f_print(fout, "\treturn ((void *)%s%s);\n",
220 			ampr(proc->res_type),RESULT);
221 	} else {
222 		f_print(fout, "\treturn (%s%s);\n", ampr(proc->res_type),RESULT);
223 	}
224 }
225 
226