rpc_sample.c revision 9497:b07c573232c0
1250757Sjkim/*
2250757Sjkim * CDDL HEADER START
3250757Sjkim *
4250757Sjkim * The contents of this file are subject to the terms of the
5250757Sjkim * Common Development and Distribution License (the "License").
6250757Sjkim * You may not use this file except in compliance with the License.
7250757Sjkim *
8250757Sjkim * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9250757Sjkim * or http://www.opensolaris.org/os/licensing.
10250757Sjkim * See the License for the specific language governing permissions
11250757Sjkim * and limitations under the License.
12250757Sjkim *
13250757Sjkim * When distributing Covered Code, include this CDDL HEADER in each
14250757Sjkim * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15250757Sjkim * If applicable, add the following below this CDDL HEADER, with the
16250757Sjkim * fields enclosed by brackets "[]" replaced with your own identifying
17250757Sjkim * information: Portions Copyright [yyyy] [name of copyright owner]
18250757Sjkim *
19250757Sjkim * CDDL HEADER END
20250757Sjkim */
21250757Sjkim
22250757Sjkim/*
23250757Sjkim * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24250757Sjkim * Use is subject to license terms.
25250757Sjkim */
26250757Sjkim/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27250757Sjkim/* All Rights Reserved */
28250757Sjkim/*
29250757Sjkim * University Copyright- Copyright (c) 1982, 1986, 1988
30250757Sjkim * The Regents of the University of California
31250757Sjkim * All Rights Reserved
32250757Sjkim *
33250757Sjkim * University Acknowledgment- Portions of this document are derived from
34250757Sjkim * software developed by the University of California, Berkeley, and its
35250757Sjkim * contributors.
36250757Sjkim */
37250757Sjkim
38250757Sjkim/*
39250757Sjkim * rpc_sample.c, Sample client-server code outputter
40250757Sjkim * for the RPC protocol compiler
41250757Sjkim */
42250757Sjkim
43250757Sjkim#include <stdio.h>
44250757Sjkim#include <string.h>
45250757Sjkim#include "rpc_parse.h"
46250838Sjkim#include "rpc_util.h"
47250838Sjkim
48250838Sjkim
49250757Sjkimstatic char RQSTP[] = "rqstp";
50250757Sjkim
51250757Sjkimextern void printarglist(proc_list *, char *, char *, char *);
52250757Sjkim
53250757Sjkimstatic void write_sample_client(char *, version_list *);
54250757Sjkimstatic void write_sample_server(definition *);
55250757Sjkimstatic void return_type(proc_list *);
56250757Sjkim
57250757Sjkimvoid
58250757Sjkimwrite_sample_svc(definition *def)
59250757Sjkim{
60250757Sjkim	if (def->def_kind != DEF_PROGRAM)
61250757Sjkim		return;
62250757Sjkim	write_sample_server(def);
63250757Sjkim}
64250757Sjkim
65250757Sjkimint
66250757Sjkimwrite_sample_clnt(definition *def)
67250757Sjkim{
68250757Sjkim	version_list *vp;
69250757Sjkim	int count = 0;
70250757Sjkim
71250757Sjkim	if (def->def_kind != DEF_PROGRAM)
72250757Sjkim		return (0);
73250757Sjkim	/* generate sample code for each version */
74250757Sjkim	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
75250757Sjkim		write_sample_client(def->def_name, vp);
76250757Sjkim		++count;
77250757Sjkim	}
78250757Sjkim	return (count);
79250757Sjkim}
80250757Sjkim
81250757Sjkimstatic void
82250757Sjkimwrite_sample_client(char *program_name, version_list *vp)
83250757Sjkim{
84250757Sjkim	proc_list *proc;
85250757Sjkim	int i;
86250757Sjkim	decl_list *l;
87250757Sjkim
88250757Sjkim	f_print(fout, "\n\nvoid\n");
89250757Sjkim	pvname(program_name, vp->vers_num);
90250757Sjkim	if (Cflag)
91250757Sjkim		f_print(fout, "(char *host)\n{\n");
92250757Sjkim	else
93250757Sjkim		f_print(fout, "(host)\n\tchar *host;\n{\n");
94250757Sjkim	f_print(fout, "\tCLIENT *clnt;\n");
95250757Sjkim
96250757Sjkim	i = 0;
97250757Sjkim	for (proc = vp->procs; proc != NULL; proc = proc->next) {
98250757Sjkim		f_print(fout, "\t");
99250757Sjkim		if (mtflag) {
100250757Sjkim			f_print(fout, "enum clnt_stat retval_%d;\n", ++i);
101250757Sjkim			if (!streq(proc->res_type, "oneway")) {
102250757Sjkim				f_print(fout, "\t");
103250757Sjkim				if (!streq(proc->res_type, "void"))
104250757Sjkim					ptype(proc->res_prefix,
105250757Sjkim					    proc->res_type, 1);
106250757Sjkim				else
107250757Sjkim					f_print(fout, "void *");
108250757Sjkim				f_print(fout, "result_%d;\n", i);
109250757Sjkim			}
110250757Sjkim		} else {
111250757Sjkim			ptype(proc->res_prefix, proc->res_type, 1);
112250757Sjkim			f_print(fout, " *result_%d;\n", ++i);
113250757Sjkim		}
114250757Sjkim		/* print out declarations for arguments */
115250757Sjkim		if (proc->arg_num < 2 && !newstyle) {
116250757Sjkim			f_print(fout, "\t");
117250757Sjkim			if (!streq(proc->args.decls->decl.type, "void"))
118250757Sjkim				ptype(proc->args.decls->decl.prefix,
119250757Sjkim				    proc->args.decls->decl.type, 1);
120250757Sjkim			else
121250757Sjkim				/* cannot have "void" type */
122250757Sjkim				f_print(fout, "char * ");
123250757Sjkim			f_print(fout, " ");
124250757Sjkim			pvname(proc->proc_name, vp->vers_num);
125250757Sjkim			f_print(fout, "_arg;\n");
126250757Sjkim		} else if (!streq(proc->args.decls->decl.type, "void")) {
127250757Sjkim			for (l = proc->args.decls; l != NULL; l = l->next) {
128250757Sjkim				f_print(fout, "\t");
129250757Sjkim				ptype(l->decl.prefix, l->decl.type, 1);
130250757Sjkim				if (strcmp(l->decl.type, "string") == 1)
131250757Sjkim					f_print(fout, " ");
132250757Sjkim				pvname(proc->proc_name, vp->vers_num);
133250757Sjkim				f_print(fout, "_%s;\n", l->decl.name);
134250757Sjkim			}
135250757Sjkim		}
136250757Sjkim	}
137250757Sjkim
138250757Sjkim	/* generate creation of client handle */
139250757Sjkim	f_print(fout, "\n#ifndef\tDEBUG\n");
140250757Sjkim	f_print(fout, "\tclnt = clnt_create(host, %s, %s, \"%s\");\n",
141250757Sjkim	    program_name, vp->vers_name, tirpcflag? "netpath" : "udp");
142250757Sjkim	f_print(fout, "\tif (clnt == (CLIENT *) NULL) {\n");
143250757Sjkim	f_print(fout, "\t\tclnt_pcreateerror(host);\n");
144250757Sjkim	f_print(fout, "\t\texit(1);\n\t}\n");
145250757Sjkim	f_print(fout, "#endif\t/* DEBUG */\n\n");
146250757Sjkim
147250757Sjkim	/* generate calls to procedures */
148250757Sjkim	i = 0;
149250757Sjkim	for (proc = vp->procs; proc != NULL; proc = proc->next) {
150250757Sjkim		if (mtflag)
151250757Sjkim			f_print(fout, "\tretval_%d = ", ++i);
152250757Sjkim		else
153250757Sjkim			f_print(fout, "\tresult_%d = ", ++i);
154250757Sjkim		pvname(proc->proc_name, vp->vers_num);
155250757Sjkim		if (proc->arg_num < 2 && !newstyle) {
156250757Sjkim			f_print(fout, "(");
157250757Sjkim			if (streq(proc->args.decls->decl.type, "void"))
158250757Sjkim				/* cast to void * */
159250757Sjkim				f_print(fout, "(void *)");
160250757Sjkim			f_print(fout, "&");
161254745Sjkim			pvname(proc->proc_name, vp->vers_num);
162250757Sjkim			if (mtflag) {
163250757Sjkim				if (streq(proc->res_type, "oneway"))
164250757Sjkim					f_print(fout, "_arg, clnt);\n");
165250757Sjkim				else
166250757Sjkim					f_print(fout,
167250757Sjkim					    "_arg, &result_%d, clnt);\n", i);
168250757Sjkim			} else
169250757Sjkim				f_print(fout, "_arg, clnt);\n");
170250757Sjkim
171250757Sjkim		} else if (streq(proc->args.decls->decl.type, "void")) {
172250757Sjkim			if (mtflag) {
173250757Sjkim				if (streq(proc->res_type, "oneway"))
174250757Sjkim					f_print(fout, "(clnt);\n");
175250757Sjkim				else
176250757Sjkim					f_print(fout,
177250757Sjkim					    "(&result_%d, clnt);\n", i);
178250757Sjkim			} else
179250757Sjkim				f_print(fout, "(clnt);\n");
180250757Sjkim		} else {
181250757Sjkim			f_print(fout, "(");
182250757Sjkim			for (l = proc->args.decls;  l != NULL; l = l->next) {
183250757Sjkim				pvname(proc->proc_name, vp->vers_num);
184250757Sjkim				f_print(fout, "_%s, ", l->decl.name);
185250757Sjkim			}
186250757Sjkim			if (mtflag) {
187250757Sjkim				if (!streq(proc->res_type, "oneway"))
188250757Sjkim					f_print(fout, "&result_%d, ", i);
189250757Sjkim			}
190250757Sjkim
191250757Sjkim			f_print(fout, "clnt);\n");
192250757Sjkim		}
193250757Sjkim		if (mtflag) {
194250757Sjkim			f_print(fout, "\tif (retval_%d != RPC_SUCCESS) {\n", i);
195250757Sjkim		} else {
196250757Sjkim			f_print(fout, "\tif (result_%d == (", i);
197250757Sjkim			ptype(proc->res_prefix, proc->res_type, 1);
198250757Sjkim			f_print(fout, "*) NULL) {\n");
199250757Sjkim		}
200250757Sjkim		f_print(fout, "\t\tclnt_perror(clnt, \"call failed\");\n");
201250757Sjkim		f_print(fout, "\t}\n");
202250757Sjkim	}
203250757Sjkim
204250757Sjkim	f_print(fout, "#ifndef\tDEBUG\n");
205250757Sjkim	f_print(fout, "\tclnt_destroy(clnt);\n");
206250757Sjkim	f_print(fout, "#endif\t	/* DEBUG */\n");
207250757Sjkim	f_print(fout, "}\n");
208250757Sjkim}
209250757Sjkim
210250757Sjkimstatic void
211250757Sjkimwrite_sample_server(definition *def)
212250757Sjkim{
213250757Sjkim	version_list *vp;
214254745Sjkim	proc_list *proc;
215254745Sjkim
216254745Sjkim	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
217254745Sjkim		for (proc = vp->procs; proc != NULL; proc = proc->next) {
218254745Sjkim			f_print(fout, "\n");
219254745Sjkim			if (!mtflag) {
220254745Sjkim				return_type(proc);
221254745Sjkim				f_print(fout, "*\n");
222254745Sjkim			} else {
223254745Sjkim				f_print(fout, "bool_t\n");
224254745Sjkim			}
225250757Sjkim			if (Cflag || mtflag)
226250757Sjkim				pvname_svc(proc->proc_name, vp->vers_num);
227250757Sjkim			else
228250757Sjkim				pvname(proc->proc_name, vp->vers_num);
229250757Sjkim			printarglist(proc, "result", RQSTP, "struct svc_req *");
230250757Sjkim
231250757Sjkim			f_print(fout, "{\n");
232250757Sjkim
233250757Sjkim			if (!mtflag) {
234250757Sjkim				f_print(fout, "\tstatic ");
235250757Sjkim				if ((!streq(proc->res_type, "void")) &&
236250757Sjkim				    (!streq(proc->res_type, "oneway")))
237250757Sjkim					return_type(proc);
238250757Sjkim				else
239250757Sjkim					f_print(fout, "char *");
240250757Sjkim				/* cannot have void type */
241250757Sjkim				f_print(fout, " result;\n");
242250757Sjkim			}
243250757Sjkim
244250757Sjkim			f_print(fout, "\n\t/*\n\t * insert server code "
245250757Sjkim			    "here\n\t */\n\n");
246250757Sjkim
247250757Sjkim			if (!mtflag)
248250757Sjkim				if (!streq(proc->res_type, "void"))
249250757Sjkim					f_print(fout,
250250757Sjkim					    "\treturn (&result);\n}\n");
251250757Sjkim				else /* cast back to void * */
252250757Sjkim					f_print(fout, "\treturn((void *) "
253250757Sjkim					    "&result);\n}\n");
254250757Sjkim			else
255250757Sjkim				f_print(fout, "\treturn (retval);\n}\n");
256250757Sjkim		}
257250757Sjkim		/* put in sample freeing routine */
258250757Sjkim		if (mtflag) {
259250757Sjkim			f_print(fout, "\nint\n");
260250757Sjkim			pvname(def->def_name, vp->vers_num);
261250757Sjkim			if (Cflag)
262250757Sjkim				f_print(fout, "_freeresult(SVCXPRT *transp,"
263250757Sjkim				    " xdrproc_t xdr_result,"
264250757Sjkim				    " caddr_t result)\n");
265250757Sjkim			else {
266250757Sjkim				f_print(fout, "_freeresult(transp, xdr_result,"
267250757Sjkim				    " result)\n");
268250757Sjkim				f_print(fout, "\tSVCXPRT *transp;\n");
269250757Sjkim				f_print(fout, "\txdrproc_t xdr_result;\n");
270250757Sjkim				f_print(fout, "\tcaddr_t result;\n");
271250757Sjkim			}
272250757Sjkim			f_print(fout, "{\n"
273250757Sjkim			    "\t(void) xdr_free(xdr_result, result);\n"
274250757Sjkim			    "\n\t/*\n\t * Insert additional freeing"
275250757Sjkim			    " code here, if needed\n\t */\n"
276250757Sjkim			    "\n\n\treturn (TRUE);\n}\n");
277		}
278	}
279}
280
281static void
282return_type(proc_list *plist)
283{
284	ptype(plist->res_prefix, plist->res_type, 1);
285}
286
287void
288add_sample_msg(void)
289{
290	f_print(fout, "/*\n");
291	f_print(fout, " * This is sample code generated by rpcgen.\n");
292	f_print(fout, " * These are only templates and you can use them\n");
293	f_print(fout, " * as a guideline for developing your own functions.\n");
294	f_print(fout, " */\n\n");
295}
296
297void
298write_sample_clnt_main(void)
299{
300	list *l;
301	definition *def;
302	version_list *vp;
303
304	f_print(fout, "\n\n");
305	if (Cflag)
306		f_print(fout, "int\nmain(int argc, char *argv[])\n{\n");
307	else
308		f_print(fout, "int\nmain(argc, argv)\n\tint argc;\n"
309		    "\tchar *argv[];\n{\n");
310
311	f_print(fout, "\tchar *host;");
312	f_print(fout, "\n\n\tif (argc < 2) {");
313	f_print(fout, "\n\t\tprintf(\"usage:  %%s server_host\\n\","
314	    " argv[0]);\n");
315	f_print(fout, "\t\texit(1);\n\t}");
316	f_print(fout, "\n\thost = argv[1];\n");
317
318	for (l = defined; l != NULL; l = l->next) {
319		def = l->val;
320		if (def->def_kind != DEF_PROGRAM)
321			continue;
322		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
323			f_print(fout, "\t");
324			pvname(def->def_name, vp->vers_num);
325			f_print(fout, "(host);\n");
326		}
327	}
328	f_print(fout, "}\n");
329}
330