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