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