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