xref: /openbsd-src/usr.bin/rpcgen/rpc_clntout.c (revision 5054e3e78af0749a9bb00ba9a024b3ee2d90290f)
1 /*	$OpenBSD: rpc_clntout.c,v 1.14 2009/10/27 23:59:42 deraadt Exp $	*/
2 /*	$NetBSD: rpc_clntout.c,v 1.4 1995/06/11 21:49:52 pk Exp $	*/
3 /*
4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5  * unrestricted use provided that this legend is included on all tape
6  * media and as a part of the software program in whole or part.  Users
7  * may copy or modify Sun RPC without charge, but are not authorized
8  * to license or distribute it to anyone else except as part of a product or
9  * program developed by the user or with the express written consent of
10  * Sun Microsystems, Inc.
11  *
12  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
13  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
14  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15  *
16  * Sun RPC is provided with no support and without any obligation on the
17  * part of Sun Microsystems, Inc. to assist in its use, correction,
18  * modification or enhancement.
19  *
20  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
21  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
22  * OR ANY PART THEREOF.
23  *
24  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
25  * or profits or other special, indirect and consequential damages, even if
26  * Sun has been advised of the possibility of such damages.
27  *
28  * Sun Microsystems, Inc.
29  * 2550 Garcia Avenue
30  * Mountain View, California  94043
31  */
32 
33 /*
34  * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler
35  * Copyright (C) 1987, Sun Microsytsems, Inc.
36  */
37 #include <stdio.h>
38 #include <string.h>
39 #include <rpc/types.h>
40 #include "rpc_parse.h"
41 #include "rpc_util.h"
42 
43 static void write_program(definition *);
44 static void printbody(proc_list *);
45 
46 #define DEFAULT_TIMEOUT 25	/* in seconds */
47 static char RESULT[] = "clnt_res";
48 
49 
50 void
51 write_stubs()
52 {
53 	list *l;
54 	definition *def;
55 
56 	fprintf(fout,
57 		"\n/* Default timeout can be changed using clnt_control() */\n");
58 	fprintf(fout, "static struct timeval TIMEOUT = { %d, 0 };\n",
59 		DEFAULT_TIMEOUT);
60 	for (l = defined; l != NULL; l = l->next) {
61 		def = (definition *) l->val;
62 		if (def->def_kind == DEF_PROGRAM) {
63 			write_program(def);
64 		}
65 	}
66 }
67 
68 static void
69 write_program(def)
70 	definition *def;
71 {
72 	version_list *vp;
73 	proc_list *proc;
74 
75 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
76 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
77 			fprintf(fout, "\n");
78 			ptype(proc->res_prefix, proc->res_type, 1);
79 			fprintf(fout, "*\n");
80 			pvname(proc->proc_name, vp->vers_num);
81 			printarglist(proc, "clnt", "CLIENT *");
82 			fprintf(fout, "{\n");
83 			printbody(proc);
84 			fprintf(fout, "}\n");
85 		}
86 	}
87 }
88 
89 /*
90  * Writes out declarations of procedure's argument list.
91  * In either ANSI C style, in one of old rpcgen style (pass by reference),
92  * or new rpcgen style (multiple arguments, pass by value);
93  */
94 
95 /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */
96 
97 void printarglist(proc, addargname, addargtype)
98 	proc_list *proc;
99 	char *addargname, *addargtype;
100 {
101 	decl_list *l;
102 
103 	if (!newstyle) {	/* old style: always pass argument by reference */
104 		if (Cflag) {			/* C++ style heading */
105 			fprintf(fout, "(");
106 			ptype(proc->args.decls->decl.prefix,
107 			    proc->args.decls->decl.type, 1);
108 			fprintf(fout, "*argp, %s%s)\n", addargtype, addargname);
109 		} else {
110 			fprintf(fout, "(argp, %s)\n", addargname);
111 			fprintf(fout, "\t");
112 			ptype(proc->args.decls->decl.prefix,
113 			    proc->args.decls->decl.type, 1);
114 			fprintf(fout, "*argp;\n");
115 		}
116 	} else if (streq(proc->args.decls->decl.type, "void")) {
117 		/* newstyle, 0 argument */
118 		if (Cflag)
119 			fprintf(fout, "(%s%s)\n", addargtype, addargname);
120 		else
121 			fprintf(fout, "(%s)\n", addargname);
122 	} else {
123 		/* new style, 1 or multiple arguments */
124 		if (!Cflag) {
125 			fprintf(fout, "(");
126 			for (l = proc->args.decls; l != NULL; l = l->next)
127 				fprintf(fout, "%s, ", l->decl.name);
128 			fprintf(fout, "%s)\n", addargname);
129 			for (l = proc->args.decls; l != NULL; l = l->next)
130 				pdeclaration(proc->args.argname, &l->decl, 1, ";\n");
131 		} else {	/* C++ style header */
132 			fprintf(fout, "(");
133 			for (l = proc->args.decls; l != NULL; l = l->next)
134 				pdeclaration(proc->args.argname, &l->decl, 0, ", ");
135 			fprintf(fout, " %s%s)\n", addargtype, addargname);
136 		}
137 	}
138 
139 	if (!Cflag)
140 		fprintf(fout, "\t%s%s;\n", addargtype, addargname);
141 }
142 
143 static char *
144 ampr(char *type)
145 {
146 	if (isvectordef(type, REL_ALIAS)) {
147 		return ("");
148 	} else {
149 		return ("&");
150 	}
151 }
152 
153 static void
154 printbody(proc)
155 	proc_list *proc;
156 {
157 	decl_list *l;
158 	bool_t args2 = (proc->arg_num > 1);
159 
160 	/*
161 	 * For new style with multiple arguments, need a structure in which
162 	 * to stuff the arguments.
163 	 */
164 	if (newstyle && args2) {
165 		fprintf(fout, "\t%s", proc->args.argname);
166 		fprintf(fout, " arg;\n");
167 	}
168 	fprintf(fout, "\tstatic ");
169 	if (streq(proc->res_type, "void")) {
170 		fprintf(fout, "char ");
171 	} else {
172 		ptype(proc->res_prefix, proc->res_type, 0);
173 	}
174 	fprintf(fout, "%s;\n",RESULT);
175 	fprintf(fout, "\n");
176 	fprintf(fout, "\tmemset((char *)%s%s, 0, sizeof(%s));\n",
177 	    ampr(proc->res_type), RESULT, RESULT);
178 	if (newstyle && !args2 && (streq(proc->args.decls->decl.type, "void"))) {
179 		/* newstyle, 0 arguments */
180 		fprintf(fout,
181 		    "\tif (clnt_call(clnt, %s, xdr_void", proc->proc_name);
182 		fprintf(fout,
183 		    ", NULL, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS) {\n",
184 		    stringfix(proc->res_type), ampr(proc->res_type), RESULT);
185 	} else if (newstyle && args2) {
186 		/* newstyle, multiple arguments:  stuff arguments into structure */
187 		for (l = proc->args.decls;  l != NULL; l = l->next) {
188 			fprintf(fout, "\targ.%s = %s;\n",
189 			    l->decl.name, l->decl.name);
190 		}
191 		fprintf(fout,
192 		    "\tif (clnt_call(clnt, %s, xdr_%s", proc->proc_name,
193 		    proc->args.argname);
194 		fprintf(fout,
195 		    ", &arg, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS) {\n",
196 		    stringfix(proc->res_type),
197 		    ampr(proc->res_type), RESULT);
198 	} else {  /* single argument, new or old style */
199 		fprintf(fout,
200 		    "\tif (clnt_call(clnt, %s, xdr_%s, %s%s, xdr_%s, "
201 		    "%s%s, TIMEOUT) != RPC_SUCCESS) {\n",
202 		    proc->proc_name,
203 		    stringfix(proc->args.decls->decl.type),
204 		    (newstyle ? "&" : ""),
205 		    (newstyle ? proc->args.decls->decl.name : "argp"),
206 		    stringfix(proc->res_type),
207 		    ampr(proc->res_type),RESULT);
208 	}
209 	fprintf(fout, "\t\treturn (NULL);\n");
210 	fprintf(fout, "\t}\n");
211 	if (streq(proc->res_type, "void")) {
212 		fprintf(fout, "\treturn ((void *)%s%s);\n",
213 		    ampr(proc->res_type),RESULT);
214 	} else {
215 		fprintf(fout, "\treturn (%s%s);\n", ampr(proc->res_type),RESULT);
216 	}
217 }
218 
219