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