xref: /openbsd/usr.bin/rpcgen/rpc_sample.c (revision 8932bfb7)
1 /*	$OpenBSD: rpc_sample.c,v 1.17 2010/09/01 14:43:34 millert Exp $	*/
2 /*	$NetBSD: rpc_sample.c,v 1.2 1995/06/11 21:50:01 pk Exp $	*/
3 
4 /*
5  * Copyright (c) 2010, Oracle America, Inc.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above
14  *       copyright notice, this list of conditions and the following
15  *       disclaimer in the documentation and/or other materials
16  *       provided with the distribution.
17  *     * Neither the name of the "Oracle America, Inc." nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
28  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
37  */
38 
39 #include <sys/cdefs.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include "rpc_parse.h"
43 #include "rpc_util.h"
44 
45 static char RQSTP[] = "rqstp";
46 
47 static void write_sample_client(char *, version_list *);
48 static void write_sample_server(definition *);
49 static void return_type(proc_list *);
50 
51 void
52 write_sample_svc(def)
53 	definition *def;
54 {
55 
56 	if (def->def_kind != DEF_PROGRAM)
57 		return;
58 	write_sample_server(def);
59 }
60 
61 
62 int
63 write_sample_clnt(def)
64 	definition *def;
65 {
66 	version_list *vp;
67 	int count = 0;
68 
69 	if (def->def_kind != DEF_PROGRAM)
70 		return(0);
71 	/* generate sample code for each version */
72 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
73 		write_sample_client(def->def_name, vp);
74 		++count;
75 	}
76 	return(count);
77 }
78 
79 
80 static void
81 write_sample_client(program_name, vp)
82 	char *program_name;
83 	version_list *vp;
84 {
85 	proc_list *proc;
86 	int i;
87 	decl_list *l;
88 
89 	fprintf(fout, "\n\nvoid\n");
90 	pvname(program_name, vp->vers_num);
91 	if (Cflag)
92 		fprintf(fout,"(char *host)\n{\n");
93 	else
94 		fprintf(fout, "(host)\nchar *host;\n{\n");
95 	fprintf(fout, "\tCLIENT *clnt;\n");
96 
97 	i = 0;
98 	for (proc = vp->procs; proc != NULL; proc = proc->next) {
99 		fprintf(fout, "\t");
100 		ptype(proc->res_prefix, proc->res_type, 1);
101 		fprintf(fout, " *result_%d;\n",++i);
102 		/* print out declarations for arguments */
103 		if (proc->arg_num < 2 && !newstyle) {
104 			fprintf(fout, "\t");
105 			if (!streq(proc->args.decls->decl.type, "void"))
106 				ptype(proc->args.decls->decl.prefix,
107 				    proc->args.decls->decl.type, 1);
108 			else
109 				fprintf(fout, "char *"); /* cannot have "void" type */
110 			fprintf(fout, " ");
111 			pvname(proc->proc_name, vp->vers_num);
112 			fprintf(fout, "_arg;\n");
113 		} else if (!streq(proc->args.decls->decl.type, "void")) {
114 			for (l = proc->args.decls; l != NULL; l = l->next) {
115 				fprintf(fout, "\t");
116 				ptype(l->decl.prefix, l->decl.type, 1);
117 				fprintf(fout, " ");
118 				pvname(proc->proc_name, vp->vers_num);
119 				fprintf(fout, "_%s;\n", l->decl.name);
120 		/*		pdeclaration(proc->args.argname, &l->decl, 1, ";\n");*/
121 			}
122 		}
123 	}
124 
125 	/* generate creation of client handle */
126 	fprintf(fout, "\tclnt = clnt_create(host, %s, %s, \"%s\");\n",
127 	    program_name, vp->vers_name, tirpcflag? "netpath" : "udp");
128 	fprintf(fout, "\tif (clnt == NULL) {\n");
129 	fprintf(fout, "\t\tclnt_pcreateerror(host);\n");
130 	fprintf(fout, "\t\texit(1);\n\t}\n");
131 
132 	/* generate calls to procedures */
133 	i = 0;
134 	for (proc = vp->procs; proc != NULL; proc = proc->next) {
135 		fprintf(fout, "\tresult_%d = ",++i);
136 		pvname(proc->proc_name, vp->vers_num);
137 		if (proc->arg_num < 2 && !newstyle) {
138 			fprintf(fout, "(");
139 			if (streq(proc->args.decls->decl.type, "void"))
140 				fprintf(fout, "(void*)");
141 			fprintf(fout, "&");
142 			pvname(proc->proc_name, vp->vers_num);
143 			fprintf(fout, "_arg, clnt);\n");
144 		} else if (streq(proc->args.decls->decl.type, "void")) {
145 			fprintf(fout, "(clnt);\n");
146 		} else {
147 			fprintf(fout, "(");
148 			for (l = proc->args.decls;	l != NULL; l = l->next) {
149 				pvname(proc->proc_name, vp->vers_num);
150 				fprintf(fout, "_%s, ", l->decl.name);
151 			}
152 			fprintf(fout, "clnt);\n");
153 		}
154 		fprintf(fout, "\tif (result_%d == NULL) {\n", i);
155 		fprintf(fout, "\t\tclnt_perror(clnt, \"call failed:\");\n");
156 		fprintf(fout, "\t}\n");
157 	}
158 
159 	fprintf(fout, "\tclnt_destroy(clnt);\n");
160 	fprintf(fout, "}\n");
161 }
162 
163 static void
164 write_sample_server(def)
165 	definition *def;
166 {
167 	version_list *vp;
168 	proc_list *proc;
169 
170 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
171 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
172 			fprintf(fout, "\n");
173 /*			if (Cflag)
174 				fprintf(fout, "extern \"C\"{\n");
175 */
176 			return_type(proc);
177 			fprintf(fout, "* \n");
178 			pvname_svc(proc->proc_name, vp->vers_num);
179 			printarglist(proc, RQSTP, "struct svc_req *");
180 
181 			fprintf(fout, "{\n");
182 			fprintf(fout, "\n\tstatic ");
183 			if (!streq(proc->res_type, "void"))
184 				return_type(proc);
185 			else
186 				fprintf(fout, "char*");  /* cannot have void type */
187 			fprintf(fout, " result;\n");
188 			fprintf(fout,
189 			    "\n\t/*\n\t * insert server code here\n\t */\n\n");
190 			if (!streq(proc->res_type, "void"))
191 				fprintf(fout, "\treturn(&result);\n}\n");
192 			else  /* cast back to void * */
193 				fprintf(fout, "\treturn((void*) &result);\n}\n");
194 /*			if (Cflag)
195 				fprintf(fout, "}\n");
196 */
197 		}
198 	}
199 }
200 
201 static void
202 return_type(plist)
203 	proc_list *plist;
204 {
205 	ptype(plist->res_prefix, plist->res_type, 1);
206 }
207 
208 void
209 add_sample_msg(void)
210 {
211 	fprintf(fout, "/*\n");
212 	fprintf(fout, " * This is sample code generated by rpcgen.\n");
213 	fprintf(fout, " * These are only templates and you can use them\n");
214 	fprintf(fout, " * as a guideline for developing your own functions.\n");
215 	fprintf(fout, " */\n\n");
216 }
217 
218 void
219 write_sample_clnt_main()
220 {
221 	list *l;
222 	definition *def;
223 	version_list *vp;
224 
225 	fprintf(fout, "\n\n");
226 	if (Cflag)
227 		fprintf(fout,"main(int argc, char *argv[])\n{\n");
228 	else
229 		fprintf(fout, "main(argc, argv)\nint argc;\nchar *argv[];\n{\n");
230 
231 	fprintf(fout, "\tchar *host;");
232 	fprintf(fout, "\n\n\tif (argc < 2) {");
233 	fprintf(fout, "\n\t\tprintf(\"usage: %%s server_host\\n\", argv[0]);\n");
234 	fprintf(fout, "\t\texit(1);\n\t}");
235 	fprintf(fout, "\n\thost = argv[1];\n");
236 
237 	for (l = defined; l != NULL; l = l->next) {
238 		def = l->val;
239 		if (def->def_kind != DEF_PROGRAM)
240 			continue;
241 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
242 			fprintf(fout, "\t");
243 			pvname(def->def_name, vp->vers_num);
244 			fprintf(fout, "(host);\n");
245 		}
246 	}
247 	fprintf(fout, "}\n");
248 }
249