rpc_sample.c revision 149709
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
30/* #pragma ident	"@(#)rpc_sample.c	1.9	94/04/25 SMI" */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/usr.bin/rpcgen/rpc_sample.c 149709 2005-09-02 10:23:26Z stefanf $");
34
35/*
36 * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
37 * Copyright (C) 1987, Sun Microsystems, Inc.
38 */
39
40#include <stdio.h>
41#include <string.h>
42#include "rpc_parse.h"
43#include "rpc_scan.h"
44#include "rpc_util.h"
45
46
47static char RQSTP[] = "rqstp";
48
49extern void printarglist( proc_list *, char *, char *, char *);
50static void write_sample_client( char *, version_list * );
51static void write_sample_server( definition * );
52static void return_type( proc_list * );
53
54void
55write_sample_svc(def)
56     definition *def;
57{
58
59	if (def->def_kind != DEF_PROGRAM)
60	  return;
61	write_sample_server(def);
62}
63
64
65int
66write_sample_clnt(def)
67     definition *def;
68{
69        version_list *vp;
70	int count = 0;
71
72	if (def->def_kind != DEF_PROGRAM)
73	  return(0);
74	/* generate sample code for each version */
75	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
76	  write_sample_client(def->def_name, vp);
77	  ++count;
78	}
79	return(count);
80}
81
82
83static void
84write_sample_client(program_name, vp)
85     char *program_name;
86     version_list *vp;
87{
88	proc_list *proc;
89	int i;
90	decl_list *l;
91
92	f_print(fout, "\n\nvoid\n");
93	pvname(program_name, vp->vers_num);
94	f_print(fout, "(char *host)\n{\n");
95	f_print(fout, "\tCLIENT *clnt;\n");
96
97	i = 0;
98	for (proc = vp->procs; proc != NULL; proc = proc->next) {
99		f_print(fout, "\t");
100		if (mtflag) {
101			f_print(fout, "enum clnt_stat retval_%d;\n\t", ++i);
102			ptype(proc->res_prefix, proc->res_type, 1);
103			f_print(fout, "result_%d;\n", i);
104		} else {
105			ptype(proc->res_prefix, proc->res_type, 1);
106			f_print(fout, " *result_%d;\n",++i);
107		}
108		/* print out declarations for arguments */
109		if(proc->arg_num < 2 && !newstyle) {
110			f_print(fout, "\t");
111			if(!streq(proc->args.decls->decl.type, "void"))
112				ptype(proc->args.decls->decl.prefix,
113				      proc->args.decls->decl.type, 1);
114			else
115				f_print(fout, "char * "); /* cannot have "void" type */
116			f_print(fout, " ");
117			pvname(proc->proc_name, vp->vers_num);
118			f_print(fout, "_arg;\n");
119		} else if (!streq(proc->args.decls->decl.type, "void")) {
120			for (l = proc->args.decls; l != NULL; l = l->next) {
121				f_print(fout, "\t");
122				ptype(l->decl.prefix, l->decl.type, 1);
123				if (strcmp(l->decl.type,"string") == 1)
124				    f_print(fout, " ");
125				pvname(proc->proc_name, vp->vers_num);
126				f_print(fout, "_%s;\n", l->decl.name);
127			}
128		}
129	}
130
131	/* generate creation of client handle */
132	f_print(fout, "\n#ifndef\tDEBUG\n");
133	f_print(fout, "\tclnt = clnt_create(host, %s, %s, \"%s\");\n",
134		program_name, vp->vers_name, tirpcflag? "netpath" : "udp");
135	f_print(fout, "\tif (clnt == (CLIENT *) NULL) {\n");
136	f_print(fout, "\t\tclnt_pcreateerror(host);\n");
137	f_print(fout, "\t\texit(1);\n\t}\n");
138	f_print(fout, "#endif\t/* DEBUG */\n\n");
139
140	/* generate calls to procedures */
141	i = 0;
142	for (proc = vp->procs; proc != NULL; proc = proc->next) {
143		if (mtflag)
144			f_print(fout, "\tretval_%d = ",++i);
145		else
146			f_print(fout, "\tresult_%d = ",++i);
147		pvname(proc->proc_name, vp->vers_num);
148		if (proc->arg_num < 2 && !newstyle) {
149			f_print(fout, "(");
150			if(streq(proc->args.decls->decl.type, "void"))
151				/* cast to void * */
152				f_print(fout, "(void *)");
153			f_print(fout, "&");
154			pvname(proc->proc_name, vp->vers_num);
155			if (mtflag)
156				f_print(fout, "_arg, &result_%d, clnt);\n",
157					i);
158			else
159				f_print(fout, "_arg, clnt);\n");
160
161		} else if (streq(proc->args.decls->decl.type, "void")) {
162			if (mtflag)
163				f_print(fout, "(&result_%d, clnt);\n", i);
164			else
165				f_print(fout, "(clnt);\n");
166		}
167		else {
168			f_print(fout, "(");
169			for (l = proc->args.decls;  l != NULL; l = l->next) {
170				pvname(proc->proc_name, vp->vers_num);
171				f_print(fout, "_%s, ", l->decl.name);
172			}
173			if (mtflag)
174				f_print(fout, "&result_%d, ", i);
175
176			f_print(fout, "clnt);\n");
177		}
178		if (mtflag) {
179			f_print(fout, "\tif (retval_%d != RPC_SUCCESS) {\n", i);
180
181		} else {
182			f_print(fout, "\tif (result_%d == (", i);
183			ptype(proc->res_prefix, proc->res_type, 1);
184			f_print(fout, "*) NULL) {\n");
185		}
186		f_print(fout, "\t\tclnt_perror(clnt, \"call failed\");\n");
187		f_print(fout, "\t}\n");
188	}
189
190	f_print(fout, "#ifndef\tDEBUG\n");
191	f_print(fout, "\tclnt_destroy(clnt);\n");
192	f_print(fout, "#endif\t	/* DEBUG */\n");
193	f_print(fout, "}\n");
194}
195
196static void
197write_sample_server(def)
198	definition *def;
199{
200	version_list *vp;
201	proc_list *proc;
202
203	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
204		for (proc = vp->procs; proc != NULL; proc = proc->next) {
205			f_print(fout, "\n");
206			if (!mtflag) {
207				return_type(proc);
208				f_print(fout, "*\n");
209			} else
210				f_print(fout, "bool_t\n");
211			pvname_svc(proc->proc_name, vp->vers_num);
212			printarglist(proc, "result", RQSTP, "struct svc_req *");
213
214			f_print(fout, "{\n");
215			if (!mtflag) {
216				f_print(fout, "\tstatic ");
217				if(!streq(proc->res_type, "void"))
218					return_type(proc);
219				else
220					f_print(fout, "char *");
221				/* cannot have void type */
222				f_print(fout, " result;\n");
223			}
224			else
225				f_print(fout, "\tbool_t retval;\n");
226			f_print(fout,
227				"\n\t/*\n\t * insert server code here\n\t */\n\n");
228
229			if (!mtflag)
230				if(!streq(proc->res_type, "void"))
231					f_print(fout, "\treturn (&result);\n}\n");
232				else /* cast back to void * */
233					f_print(fout, "\treturn((void *) &result);\n}\n");
234			else
235				f_print(fout, "\treturn (retval);\n}\n");
236		}
237		/* put in sample freeing routine */
238		if (mtflag) {
239		f_print(fout, "\nint\n");
240		pvname(def->def_name, vp->vers_num);
241		f_print(fout,"_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)\n");
242		f_print(fout, "{\n");
243		f_print(fout, "\t(void) xdr_free(xdr_result, result);\n");
244		f_print(fout,
245			"\n\t/*\n\t * Insert additional freeing code here, if needed\n\t */\n");
246		f_print(fout, "\n}\n");
247
248
249	}
250	}
251}
252
253
254
255static void
256return_type(plist)
257	proc_list *plist;
258{
259  ptype(plist->res_prefix, plist->res_type, 1);
260}
261
262void
263add_sample_msg()
264{
265	f_print(fout, "/*\n");
266	f_print(fout, " * This is sample code generated by rpcgen.\n");
267	f_print(fout, " * These are only templates and you can use them\n");
268	f_print(fout, " * as a guideline for developing your own functions.\n");
269	f_print(fout, " */\n\n");
270}
271
272void
273write_sample_clnt_main()
274{
275	list *l;
276	definition *def;
277	version_list *vp;
278
279	f_print(fout, "\n\n");
280	f_print(fout, "main(int argc, char *argv[])\n{\n");
281
282	f_print(fout, "\tchar *host;");
283	f_print(fout, "\n\n\tif (argc < 2) {");
284	f_print(fout, "\n\t\tprintf(\"usage:  %%s server_host\\n\", argv[0]);\n");
285	f_print(fout, "\t\texit(1);\n\t}");
286	f_print(fout, "\n\thost = argv[1];\n");
287
288	for (l = defined; l != NULL; l = l->next) {
289		def = l->val;
290		if (def->def_kind != DEF_PROGRAM) {
291			continue;
292		}
293		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
294		        f_print(fout, "\t");
295			pvname(def->def_name, vp->vers_num);
296			f_print(fout, "(host);\n");
297		}
298	}
299	f_print(fout, "}\n");
300}
301