Updated to fedora-glibc-20041018T0940
[glibc.git] / sunrpc / rpc_sample.c
blob50c3d5554ba32fcfebb9fc10cb5e714cd4de994b
1 /*
2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user or with the express written consent of
8 * Sun Microsystems, Inc.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
32 * From: @(#)rpc_sample.c 1.1 90/08/30 (C) 1987 SMI
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static const char sample_rcsid[] =
36 "$Id$";
37 #endif
40 * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
43 #include <stdio.h>
44 #include <string.h>
45 #include "rpc_parse.h"
46 #include "rpc_util.h"
47 #include "proto.h"
50 static const char RQSTP[] = "rqstp";
52 static void write_sample_client (const char *program_name, version_list * vp);
53 static void write_sample_server (definition * def);
54 static void return_type (proc_list * plist);
57 void
58 write_sample_svc (definition * def)
61 if (def->def_kind != DEF_PROGRAM)
62 return;
63 write_sample_server (def);
67 int
68 write_sample_clnt (definition * def)
70 version_list *vp;
71 int count = 0;
73 if (def->def_kind != DEF_PROGRAM)
74 return 0;
75 /* generate sample code for each version */
76 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
78 write_sample_client (def->def_name, vp);
79 ++count;
81 return count;
85 static void
86 write_sample_client (const char *program_name, version_list * vp)
88 proc_list *proc;
89 int i;
90 decl_list *l;
92 f_print (fout, "\n\nvoid\n");
93 pvname (program_name, vp->vers_num);
94 if (Cflag)
95 f_print (fout, "(char *host)\n{\n");
96 else
97 f_print (fout, "(host)\nchar *host;\n{\n");
98 f_print (fout, "\tCLIENT *clnt;\n");
100 i = 0;
101 for (proc = vp->procs; proc != NULL; proc = proc->next)
103 f_print (fout, "\t");
104 ++i;
105 if (mtflag)
107 f_print (fout, "enum clnt_stat retval_%d;\n\t", i);
108 ptype (proc->res_prefix, proc->res_type, 1);
109 if (!streq (proc->res_type, "void"))
110 f_print (fout, "result_%d;\n", i);
111 else
112 fprintf (fout, "*result_%d;\n", i);
114 else
116 ptype (proc->res_prefix, proc->res_type, 1);
117 f_print (fout, " *result_%d;\n", i);
119 /* print out declarations for arguments */
120 if (proc->arg_num < 2 && !newstyle)
122 f_print (fout, "\t");
123 if (!streq (proc->args.decls->decl.type, "void"))
125 ptype (proc->args.decls->decl.prefix,
126 proc->args.decls->decl.type, 1);
127 f_print (fout, " ");
129 else
130 f_print (fout, "char *"); /* cannot have "void" type */
131 pvname (proc->proc_name, vp->vers_num);
132 f_print (fout, "_arg;\n");
134 else if (!streq (proc->args.decls->decl.type, "void"))
136 for (l = proc->args.decls; l != NULL; l = l->next)
138 f_print (fout, "\t");
139 ptype (l->decl.prefix, l->decl.type, 1);
140 if (strcmp (l->decl.type, "string") == 1)
141 f_print (fout, " ");
142 pvname (proc->proc_name, vp->vers_num);
143 f_print (fout, "_%s;\n", l->decl.name);
148 /* generate creation of client handle */
149 f_print(fout, "\n#ifndef\tDEBUG\n");
150 f_print (fout, "\tclnt = clnt_create (host, %s, %s, \"%s\");\n",
151 program_name, vp->vers_name, tirpcflag ? "netpath" : "udp");
152 f_print (fout, "\tif (clnt == NULL) {\n");
153 f_print (fout, "\t\tclnt_pcreateerror (host);\n");
154 f_print (fout, "\t\texit (1);\n\t}\n");
155 f_print(fout, "#endif\t/* DEBUG */\n\n");
157 /* generate calls to procedures */
158 i = 0;
159 for (proc = vp->procs; proc != NULL; proc = proc->next)
161 if (mtflag)
162 f_print(fout, "\tretval_%d = ",++i);
163 else
164 f_print (fout, "\tresult_%d = ", ++i);
165 pvname (proc->proc_name, vp->vers_num);
166 if (proc->arg_num < 2 && !newstyle)
168 f_print (fout, "(");
169 if (streq (proc->args.decls->decl.type, "void"))/* cast to void* */
170 f_print (fout, "(void*)");
171 f_print (fout, "&");
172 pvname (proc->proc_name, vp->vers_num);
173 if (mtflag)
174 f_print(fout, "_arg, &result_%d, clnt);\n", i);
175 else
176 f_print (fout, "_arg, clnt);\n");
178 else if (streq (proc->args.decls->decl.type, "void"))
180 if (mtflag)
181 f_print (fout, "(&result_%d, clnt);\n", i);
182 else
183 f_print (fout, "(clnt);\n");
185 else
187 f_print (fout, "(");
188 for (l = proc->args.decls; l != NULL; l = l->next)
190 pvname (proc->proc_name, vp->vers_num);
191 f_print (fout, "_%s, ", l->decl.name);
193 if (mtflag)
194 f_print(fout, "&result_%d, ", i);
195 f_print (fout, "clnt);\n");
197 if (mtflag)
199 f_print(fout, "\tif (retval_%d != RPC_SUCCESS) {\n", i);
201 else
203 f_print(fout, "\tif (result_%d == (", i);
204 ptype(proc->res_prefix, proc->res_type, 1);
205 f_print(fout, "*) NULL) {\n");
207 f_print(fout, "\t\tclnt_perror (clnt, \"call failed\");\n");
208 f_print(fout, "\t}\n");
211 f_print (fout, "#ifndef\tDEBUG\n");
212 f_print (fout, "\tclnt_destroy (clnt);\n");
213 f_print (fout, "#endif\t /* DEBUG */\n");
214 f_print (fout, "}\n");
217 static void
218 write_sample_server (definition * def)
220 version_list *vp;
221 proc_list *proc;
223 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
225 for (proc = vp->procs; proc != NULL; proc = proc->next)
227 f_print (fout, "\n");
228 if (!mtflag)
230 return_type (proc);
231 f_print (fout, "*\n");
233 else
234 f_print (fout, "bool_t\n");
235 if (Cflag || mtflag)
236 pvname_svc (proc->proc_name, vp->vers_num);
237 else
238 pvname(proc->proc_name, vp->vers_num);
239 printarglist(proc, "result", RQSTP, "struct svc_req *");
240 f_print(fout, "{\n");
241 if (!mtflag)
243 f_print(fout, "\tstatic ");
244 if(!streq(proc->res_type, "void"))
245 return_type(proc);
246 else
247 f_print(fout, "char *");
248 /* cannot have void type */
249 /* f_print(fout, " result;\n", proc->res_type); */
250 f_print(fout, " result;\n");
252 else
253 f_print(fout, "\tbool_t retval;\n");
254 fprintf (fout, "\n\t/*\n\t * insert server code here\n\t */\n\n");
256 if (!mtflag)
258 if (!streq(proc->res_type, "void"))
259 f_print(fout, "\treturn &result;\n}\n");
260 else /* cast back to void * */
261 f_print(fout, "\treturn (void *) &result;\n}\n");
263 else
264 f_print(fout, "\treturn retval;\n}\n");
267 /* put in sample freeing routine */
268 if (mtflag)
270 f_print(fout, "\nint\n");
271 pvname(def->def_name, vp->vers_num);
272 if (Cflag)
273 f_print(fout,"_freeresult (SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)\n");
274 else
276 f_print(fout,"_freeresult (transp, xdr_result, result)\n");
277 f_print(fout,"\tSVCXPRT *transp;\n");
278 f_print(fout,"\txdrproc_t xdr_result;\n");
279 f_print(fout,"\tcaddr_t result;\n");
281 f_print(fout, "{\n");
282 f_print(fout, "\txdr_free (xdr_result, result);\n");
283 f_print(fout,
284 "\n\t/*\n\t * Insert additional freeing code here, if needed\n\t */\n");
285 f_print(fout, "\n\treturn 1;\n}\n");
292 static void
293 return_type (proc_list * plist)
295 ptype (plist->res_prefix, plist->res_type, 1);
298 void
299 add_sample_msg (void)
301 f_print (fout, "/*\n");
302 f_print (fout, " * This is sample code generated by rpcgen.\n");
303 f_print (fout, " * These are only templates and you can use them\n");
304 f_print (fout, " * as a guideline for developing your own functions.\n");
305 f_print (fout, " */\n\n");
308 void
309 write_sample_clnt_main (void)
311 list *l;
312 definition *def;
313 version_list *vp;
315 f_print (fout, "\n\n");
316 if (Cflag)
317 f_print (fout, "int\nmain (int argc, char *argv[])\n{\n");
318 else
319 f_print (fout, "int\nmain (argc, argv)\nint argc;\nchar *argv[];\n{\n");
321 f_print (fout, "\tchar *host;");
322 f_print (fout, "\n\n\tif (argc < 2) {");
323 f_print (fout, "\n\t\tprintf (\"usage: %%s server_host\\n\", argv[0]);\n");
324 f_print (fout, "\t\texit (1);\n\t}");
325 f_print (fout, "\n\thost = argv[1];\n");
327 for (l = defined; l != NULL; l = l->next)
329 def = l->val;
330 if (def->def_kind != DEF_PROGRAM)
332 continue;
334 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
336 f_print (fout, "\t");
337 pvname (def->def_name, vp->vers_num);
338 f_print (fout, " (host);\n");
341 f_print (fout, "exit (0);\n}\n");