xref: /netbsd-src/usr.bin/rpcgen/rpc_sample.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: rpc_sample.c,v 1.5 1997/10/18 10:54:01 lukem Exp $	*/
2 /*
3  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4  * unrestricted use provided that this legend is included on all tape
5  * media and as a part of the software program in whole or part.  Users
6  * may copy or modify Sun RPC without charge, but are not authorized
7  * to license or distribute it to anyone else except as part of a product or
8  * program developed by the user or with the express written consent of
9  * Sun Microsystems, Inc.
10  *
11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14  *
15  * Sun RPC is provided with no support and without any obligation on the
16  * part of Sun Microsystems, Inc. to assist in its use, correction,
17  * modification or enhancement.
18  *
19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21  * OR ANY PART THEREOF.
22  *
23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24  * or profits or other special, indirect and consequential damages, even if
25  * Sun has been advised of the possibility of such damages.
26  *
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)rpc_sample.c  1.1  90/08/30  (C) 1987 SMI";
36 #else
37 __RCSID("$NetBSD: rpc_sample.c,v 1.5 1997/10/18 10:54:01 lukem Exp $");
38 #endif
39 #endif
40 
41 /*
42  * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
43  */
44 
45 #include <stdio.h>
46 #include <string.h>
47 #include "rpc_scan.h"
48 #include "rpc_parse.h"
49 #include "rpc_util.h"
50 
51 static char RQSTP[] = "rqstp";
52 
53 static void write_sample_client __P((char *, version_list *));
54 static void write_sample_server __P((definition *));
55 static void return_type __P((proc_list *));
56 
57 void
58 write_sample_svc(def)
59 	definition *def;
60 {
61 
62 	if (def->def_kind != DEF_PROGRAM)
63 		return;
64 	write_sample_server(def);
65 }
66 
67 
68 int
69 write_sample_clnt(def)
70 	definition *def;
71 {
72 	version_list *vp;
73 	int     count = 0;
74 
75 	if (def->def_kind != DEF_PROGRAM)
76 		return (0);
77 	/* generate sample code for each version */
78 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
79 		write_sample_client(def->def_name, vp);
80 		++count;
81 	}
82 	return (count);
83 }
84 
85 
86 static void
87 write_sample_client(program_name, vp)
88 	char   *program_name;
89 	version_list *vp;
90 {
91 	proc_list *proc;
92 	int     i;
93 	decl_list *l;
94 
95 	f_print(fout, "\n\nvoid\n");
96 	pvname(program_name, vp->vers_num);
97 	if (Cflag)
98 		f_print(fout, "( char* host )\n{\n");
99 	else
100 		f_print(fout, "(host)\nchar *host;\n{\n");
101 	f_print(fout, "\tCLIENT *clnt;\n");
102 
103 	i = 0;
104 	for (proc = vp->procs; proc != NULL; proc = proc->next) {
105 		f_print(fout, "\t");
106 		ptype(proc->res_prefix, proc->res_type, 1);
107 		f_print(fout, " *result_%d;\n", ++i);
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, proc->args.decls->decl.type, 1);
113 			else
114 				f_print(fout, "char* ");	/* cannot have "void"
115 								 * type */
116 			f_print(fout, " ");
117 			pvname(proc->proc_name, vp->vers_num);
118 			f_print(fout, "_arg;\n");
119 		} else
120 			if (!streq(proc->args.decls->decl.type, "void")) {
121 				for (l = proc->args.decls; l != NULL; l = l->next) {
122 					f_print(fout, "\t");
123 					ptype(l->decl.prefix, l->decl.type, 1);
124 					f_print(fout, " ");
125 					pvname(proc->proc_name, vp->vers_num);
126 					f_print(fout, "_%s;\n", l->decl.name);
127 /*	  pdeclaration(proc->args.argname, &l->decl, 1, ";\n" );*/
128 				}
129 			}
130 	}
131 
132 	/* generate creation of client handle */
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 == NULL) {\n");
136 	f_print(fout, "\t\tclnt_pcreateerror(host);\n");
137 	f_print(fout, "\t\texit(1);\n\t}\n");
138 
139 	/* generate calls to procedures */
140 	i = 0;
141 	for (proc = vp->procs; proc != NULL; proc = proc->next) {
142 		f_print(fout, "\tresult_%d = ", ++i);
143 		pvname(proc->proc_name, vp->vers_num);
144 		if (proc->arg_num < 2 && !newstyle) {
145 			f_print(fout, "(");
146 			if (streq(proc->args.decls->decl.type, "void"))	/* cast to void* */
147 				f_print(fout, "(void*)");
148 			f_print(fout, "&");
149 			pvname(proc->proc_name, vp->vers_num);
150 			f_print(fout, "_arg, clnt);\n");
151 		} else
152 			if (streq(proc->args.decls->decl.type, "void")) {
153 				f_print(fout, "(clnt);\n");
154 			} else {
155 				f_print(fout, "(");
156 				for (l = proc->args.decls; l != NULL; l = l->next) {
157 					pvname(proc->proc_name, vp->vers_num);
158 					f_print(fout, "_%s, ", l->decl.name);
159 				}
160 				f_print(fout, "clnt);\n");
161 			}
162 		f_print(fout, "\tif (result_%d == NULL) {\n", i);
163 		f_print(fout, "\t\tclnt_perror(clnt, \"call failed:\");\n");
164 		f_print(fout, "\t}\n");
165 	}
166 
167 	f_print(fout, "\tclnt_destroy( clnt );\n");
168 	f_print(fout, "}\n");
169 }
170 
171 static void
172 write_sample_server(def)
173 	definition *def;
174 {
175 	version_list *vp;
176 	proc_list *proc;
177 
178 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
179 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
180 			f_print(fout, "\n");
181 /*			if( Cflag )
182 			  f_print( fout, "extern \"C\"{\n");
183 */
184 			return_type(proc);
185 			f_print(fout, "*\n");
186 			if (Cflag)
187 				pvname_svc(proc->proc_name, vp->vers_num);
188 			else
189 				pvname(proc->proc_name, vp->vers_num);
190 			printarglist(proc, RQSTP, "struct svc_req *");
191 
192 			f_print(fout, "{\n");
193 			f_print(fout, "\n\tstatic ");
194 			if (!streq(proc->res_type, "void"))
195 				return_type(proc);
196 			else
197 				f_print(fout, "char*");	/* cannot have void type */
198 			f_print(fout, " result;\n");
199 			f_print(fout,
200 			    "\n\t/*\n\t * insert server code here\n\t */\n\n");
201 			if (!streq(proc->res_type, "void"))
202 				f_print(fout, "\treturn(&result);\n}\n");
203 			else	/* cast back to void * */
204 				f_print(fout, "\treturn((void*) &result);\n}\n");
205 /*			if( Cflag)
206 			  f_print( fout, "};\n");
207 */
208 
209 		}
210 	}
211 }
212 
213 static void
214 return_type(plist)
215 	proc_list *plist;
216 {
217 	ptype(plist->res_prefix, plist->res_type, 1);
218 }
219 
220 void
221 add_sample_msg()
222 {
223 	f_print(fout, "/*\n");
224 	f_print(fout, " * This is sample code generated by rpcgen.\n");
225 	f_print(fout, " * These are only templates and you can use them\n");
226 	f_print(fout, " * as a guideline for developing your own functions.\n");
227 	f_print(fout, " */\n\n");
228 }
229 
230 void
231 write_sample_clnt_main()
232 {
233 	list   *l;
234 	definition *def;
235 	version_list *vp;
236 
237 	f_print(fout, "\n\n");
238 	if (Cflag)
239 		f_print(fout, "main( int argc, char* argv[] )\n{\n");
240 	else
241 		f_print(fout, "main(argc, argv)\nint argc;\nchar *argv[];\n{\n");
242 
243 	f_print(fout, "\tchar *host;");
244 	f_print(fout, "\n\n\tif(argc < 2) {");
245 	f_print(fout, "\n\t\tprintf(\"usage: %%s server_host\\n\", argv[0]);\n");
246 	f_print(fout, "\t\texit(1);\n\t}");
247 	f_print(fout, "\n\thost = argv[1];\n");
248 
249 	for (l = defined; l != NULL; l = l->next) {
250 		def = l->val;
251 		if (def->def_kind != DEF_PROGRAM) {
252 			continue;
253 		}
254 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
255 			f_print(fout, "\t");
256 			pvname(def->def_name, vp->vers_num);
257 			f_print(fout, "( host );\n");
258 		}
259 	}
260 	f_print(fout, "}\n");
261 }
262