2 * From: @(#)rpc_sample.c 1.1 90/08/30 (C) 1987 SMI
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 * * Neither the name of Sun Microsystems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
38 #include "rpc_parse.h"
43 static const char RQSTP[] = "rqstp";
45 static void write_sample_client (const char *program_name, version_list * vp);
46 static void write_sample_server (definition * def);
47 static void return_type (proc_list * plist);
51 write_sample_svc (definition * def)
54 if (def->def_kind != DEF_PROGRAM)
56 write_sample_server (def);
61 write_sample_clnt (definition * def)
66 if (def->def_kind != DEF_PROGRAM)
68 /* generate sample code for each version */
69 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
71 write_sample_client (def->def_name, vp);
79 write_sample_client (const char *program_name, version_list * vp)
85 f_print (fout, "\n\nvoid\n");
86 pvname (program_name, vp->vers_num);
88 f_print (fout, "(char *host)\n{\n");
90 f_print (fout, "(host)\nchar *host;\n{\n");
91 f_print (fout, "\tCLIENT *clnt;\n");
94 for (proc = vp->procs; proc != NULL; proc = proc->next)
100 f_print (fout, "enum clnt_stat retval_%d;\n\t", i);
101 ptype (proc->res_prefix, proc->res_type, 1);
102 if (!streq (proc->res_type, "void"))
103 f_print (fout, "result_%d;\n", i);
105 fprintf (fout, "*result_%d;\n", i);
109 ptype (proc->res_prefix, proc->res_type, 1);
110 f_print (fout, " *result_%d;\n", i);
112 /* print out declarations for arguments */
113 if (proc->arg_num < 2 && !newstyle)
115 f_print (fout, "\t");
116 if (!streq (proc->args.decls->decl.type, "void"))
118 ptype (proc->args.decls->decl.prefix,
119 proc->args.decls->decl.type, 1);
123 f_print (fout, "char *"); /* cannot have "void" type */
124 pvname (proc->proc_name, vp->vers_num);
125 f_print (fout, "_arg;\n");
127 else if (!streq (proc->args.decls->decl.type, "void"))
129 for (l = proc->args.decls; l != NULL; l = l->next)
131 f_print (fout, "\t");
132 ptype (l->decl.prefix, l->decl.type, 1);
133 if (strcmp (l->decl.type, "string") == 1)
135 pvname (proc->proc_name, vp->vers_num);
136 f_print (fout, "_%s;\n", l->decl.name);
141 /* generate creation of client handle */
142 f_print(fout, "\n#ifndef\tDEBUG\n");
143 f_print (fout, "\tclnt = clnt_create (host, %s, %s, \"%s\");\n",
144 program_name, vp->vers_name, tirpcflag ? "netpath" : "udp");
145 f_print (fout, "\tif (clnt == NULL) {\n");
146 f_print (fout, "\t\tclnt_pcreateerror (host);\n");
147 f_print (fout, "\t\texit (1);\n\t}\n");
148 f_print(fout, "#endif\t/* DEBUG */\n\n");
150 /* generate calls to procedures */
152 for (proc = vp->procs; proc != NULL; proc = proc->next)
155 f_print(fout, "\tretval_%d = ",++i);
157 f_print (fout, "\tresult_%d = ", ++i);
158 pvname (proc->proc_name, vp->vers_num);
159 if (proc->arg_num < 2 && !newstyle)
162 if (streq (proc->args.decls->decl.type, "void"))/* cast to void* */
163 f_print (fout, "(void*)");
165 pvname (proc->proc_name, vp->vers_num);
167 f_print(fout, "_arg, &result_%d, clnt);\n", i);
169 f_print (fout, "_arg, clnt);\n");
171 else if (streq (proc->args.decls->decl.type, "void"))
174 f_print (fout, "(&result_%d, clnt);\n", i);
176 f_print (fout, "(clnt);\n");
181 for (l = proc->args.decls; l != NULL; l = l->next)
183 pvname (proc->proc_name, vp->vers_num);
184 f_print (fout, "_%s, ", l->decl.name);
187 f_print(fout, "&result_%d, ", i);
188 f_print (fout, "clnt);\n");
192 f_print(fout, "\tif (retval_%d != RPC_SUCCESS) {\n", i);
196 f_print(fout, "\tif (result_%d == (", i);
197 ptype(proc->res_prefix, proc->res_type, 1);
198 f_print(fout, "*) NULL) {\n");
200 f_print(fout, "\t\tclnt_perror (clnt, \"call failed\");\n");
201 f_print(fout, "\t}\n");
204 f_print (fout, "#ifndef\tDEBUG\n");
205 f_print (fout, "\tclnt_destroy (clnt);\n");
206 f_print (fout, "#endif\t /* DEBUG */\n");
207 f_print (fout, "}\n");
211 write_sample_server (definition * def)
216 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
218 for (proc = vp->procs; proc != NULL; proc = proc->next)
220 f_print (fout, "\n");
224 f_print (fout, "*\n");
227 f_print (fout, "bool_t\n");
229 pvname_svc (proc->proc_name, vp->vers_num);
231 pvname(proc->proc_name, vp->vers_num);
232 printarglist(proc, "result", RQSTP, "struct svc_req *");
233 f_print(fout, "{\n");
236 f_print(fout, "\tstatic ");
237 if(!streq(proc->res_type, "void"))
240 f_print(fout, "char *");
241 /* cannot have void type */
242 /* f_print(fout, " result;\n", proc->res_type); */
243 f_print(fout, " result;\n");
246 f_print(fout, "\tbool_t retval;\n");
247 fprintf (fout, "\n\t/*\n\t * insert server code here\n\t */\n\n");
251 if (!streq(proc->res_type, "void"))
252 f_print(fout, "\treturn &result;\n}\n");
253 else /* cast back to void * */
254 f_print(fout, "\treturn (void *) &result;\n}\n");
257 f_print(fout, "\treturn retval;\n}\n");
260 /* put in sample freeing routine */
263 f_print(fout, "\nint\n");
264 pvname(def->def_name, vp->vers_num);
266 f_print(fout,"_freeresult (SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)\n");
269 f_print(fout,"_freeresult (transp, xdr_result, result)\n");
270 f_print(fout,"\tSVCXPRT *transp;\n");
271 f_print(fout,"\txdrproc_t xdr_result;\n");
272 f_print(fout,"\tcaddr_t result;\n");
274 f_print(fout, "{\n");
275 f_print(fout, "\txdr_free (xdr_result, result);\n");
277 "\n\t/*\n\t * Insert additional freeing code here, if needed\n\t */\n");
278 f_print(fout, "\n\treturn 1;\n}\n");
286 return_type (proc_list * plist)
288 ptype (plist->res_prefix, plist->res_type, 1);
292 add_sample_msg (void)
294 f_print (fout, "/*\n");
295 f_print (fout, " * This is sample code generated by rpcgen.\n");
296 f_print (fout, " * These are only templates and you can use them\n");
297 f_print (fout, " * as a guideline for developing your own functions.\n");
298 f_print (fout, " */\n\n");
302 write_sample_clnt_main (void)
308 f_print (fout, "\n\n");
310 f_print (fout, "int\nmain (int argc, char *argv[])\n{\n");
312 f_print (fout, "int\nmain (argc, argv)\nint argc;\nchar *argv[];\n{\n");
314 f_print (fout, "\tchar *host;");
315 f_print (fout, "\n\n\tif (argc < 2) {");
316 f_print (fout, "\n\t\tprintf (\"usage: %%s server_host\\n\", argv[0]);\n");
317 f_print (fout, "\t\texit (1);\n\t}");
318 f_print (fout, "\n\thost = argv[1];\n");
320 for (l = defined; l != NULL; l = l->next)
323 if (def->def_kind != DEF_PROGRAM)
327 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
329 f_print (fout, "\t");
330 pvname (def->def_name, vp->vers_num);
331 f_print (fout, " (host);\n");
334 f_print (fout, "exit (0);\n}\n");