2008-03-25 Roland McGrath <roland@redhat.com>
[glibc.git] / sunrpc / rpc_sample.c
blob00b58d5bc2ae0bd9dd761573e86b6653f8a9196c
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
36 * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
39 #include <stdio.h>
40 #include <string.h>
41 #include "rpc_parse.h"
42 #include "rpc_util.h"
43 #include "proto.h"
46 static const char RQSTP[] = "rqstp";
48 static void write_sample_client (const char *program_name, version_list * vp);
49 static void write_sample_server (definition * def);
50 static void return_type (proc_list * plist);
53 void
54 write_sample_svc (definition * def)
57 if (def->def_kind != DEF_PROGRAM)
58 return;
59 write_sample_server (def);
63 int
64 write_sample_clnt (definition * def)
66 version_list *vp;
67 int count = 0;
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)
74 write_sample_client (def->def_name, vp);
75 ++count;
77 return count;
81 static void
82 write_sample_client (const char *program_name, version_list * vp)
84 proc_list *proc;
85 int i;
86 decl_list *l;
88 f_print (fout, "\n\nvoid\n");
89 pvname (program_name, vp->vers_num);
90 if (Cflag)
91 f_print (fout, "(char *host)\n{\n");
92 else
93 f_print (fout, "(host)\nchar *host;\n{\n");
94 f_print (fout, "\tCLIENT *clnt;\n");
96 i = 0;
97 for (proc = vp->procs; proc != NULL; proc = proc->next)
99 f_print (fout, "\t");
100 ++i;
101 if (mtflag)
103 f_print (fout, "enum clnt_stat retval_%d;\n\t", i);
104 ptype (proc->res_prefix, proc->res_type, 1);
105 if (!streq (proc->res_type, "void"))
106 f_print (fout, "result_%d;\n", i);
107 else
108 fprintf (fout, "*result_%d;\n", i);
110 else
112 ptype (proc->res_prefix, proc->res_type, 1);
113 f_print (fout, " *result_%d;\n", i);
115 /* print out declarations for arguments */
116 if (proc->arg_num < 2 && !newstyle)
118 f_print (fout, "\t");
119 if (!streq (proc->args.decls->decl.type, "void"))
121 ptype (proc->args.decls->decl.prefix,
122 proc->args.decls->decl.type, 1);
123 f_print (fout, " ");
125 else
126 f_print (fout, "char *"); /* cannot have "void" type */
127 pvname (proc->proc_name, vp->vers_num);
128 f_print (fout, "_arg;\n");
130 else if (!streq (proc->args.decls->decl.type, "void"))
132 for (l = proc->args.decls; l != NULL; l = l->next)
134 f_print (fout, "\t");
135 ptype (l->decl.prefix, l->decl.type, 1);
136 if (strcmp (l->decl.type, "string") == 1)
137 f_print (fout, " ");
138 pvname (proc->proc_name, vp->vers_num);
139 f_print (fout, "_%s;\n", l->decl.name);
144 /* generate creation of client handle */
145 f_print(fout, "\n#ifndef\tDEBUG\n");
146 f_print (fout, "\tclnt = clnt_create (host, %s, %s, \"%s\");\n",
147 program_name, vp->vers_name, tirpcflag ? "netpath" : "udp");
148 f_print (fout, "\tif (clnt == NULL) {\n");
149 f_print (fout, "\t\tclnt_pcreateerror (host);\n");
150 f_print (fout, "\t\texit (1);\n\t}\n");
151 f_print(fout, "#endif\t/* DEBUG */\n\n");
153 /* generate calls to procedures */
154 i = 0;
155 for (proc = vp->procs; proc != NULL; proc = proc->next)
157 if (mtflag)
158 f_print(fout, "\tretval_%d = ",++i);
159 else
160 f_print (fout, "\tresult_%d = ", ++i);
161 pvname (proc->proc_name, vp->vers_num);
162 if (proc->arg_num < 2 && !newstyle)
164 f_print (fout, "(");
165 if (streq (proc->args.decls->decl.type, "void"))/* cast to void* */
166 f_print (fout, "(void*)");
167 f_print (fout, "&");
168 pvname (proc->proc_name, vp->vers_num);
169 if (mtflag)
170 f_print(fout, "_arg, &result_%d, clnt);\n", i);
171 else
172 f_print (fout, "_arg, clnt);\n");
174 else if (streq (proc->args.decls->decl.type, "void"))
176 if (mtflag)
177 f_print (fout, "(&result_%d, clnt);\n", i);
178 else
179 f_print (fout, "(clnt);\n");
181 else
183 f_print (fout, "(");
184 for (l = proc->args.decls; l != NULL; l = l->next)
186 pvname (proc->proc_name, vp->vers_num);
187 f_print (fout, "_%s, ", l->decl.name);
189 if (mtflag)
190 f_print(fout, "&result_%d, ", i);
191 f_print (fout, "clnt);\n");
193 if (mtflag)
195 f_print(fout, "\tif (retval_%d != RPC_SUCCESS) {\n", i);
197 else
199 f_print(fout, "\tif (result_%d == (", i);
200 ptype(proc->res_prefix, proc->res_type, 1);
201 f_print(fout, "*) NULL) {\n");
203 f_print(fout, "\t\tclnt_perror (clnt, \"call failed\");\n");
204 f_print(fout, "\t}\n");
207 f_print (fout, "#ifndef\tDEBUG\n");
208 f_print (fout, "\tclnt_destroy (clnt);\n");
209 f_print (fout, "#endif\t /* DEBUG */\n");
210 f_print (fout, "}\n");
213 static void
214 write_sample_server (definition * def)
216 version_list *vp;
217 proc_list *proc;
219 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
221 for (proc = vp->procs; proc != NULL; proc = proc->next)
223 f_print (fout, "\n");
224 if (!mtflag)
226 return_type (proc);
227 f_print (fout, "*\n");
229 else
230 f_print (fout, "bool_t\n");
231 if (Cflag || mtflag)
232 pvname_svc (proc->proc_name, vp->vers_num);
233 else
234 pvname(proc->proc_name, vp->vers_num);
235 printarglist(proc, "result", RQSTP, "struct svc_req *");
236 f_print(fout, "{\n");
237 if (!mtflag)
239 f_print(fout, "\tstatic ");
240 if(!streq(proc->res_type, "void"))
241 return_type(proc);
242 else
243 f_print(fout, "char *");
244 /* cannot have void type */
245 /* f_print(fout, " result;\n", proc->res_type); */
246 f_print(fout, " result;\n");
248 else
249 f_print(fout, "\tbool_t retval;\n");
250 fprintf (fout, "\n\t/*\n\t * insert server code here\n\t */\n\n");
252 if (!mtflag)
254 if (!streq(proc->res_type, "void"))
255 f_print(fout, "\treturn &result;\n}\n");
256 else /* cast back to void * */
257 f_print(fout, "\treturn (void *) &result;\n}\n");
259 else
260 f_print(fout, "\treturn retval;\n}\n");
263 /* put in sample freeing routine */
264 if (mtflag)
266 f_print(fout, "\nint\n");
267 pvname(def->def_name, vp->vers_num);
268 if (Cflag)
269 f_print(fout,"_freeresult (SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)\n");
270 else
272 f_print(fout,"_freeresult (transp, xdr_result, result)\n");
273 f_print(fout,"\tSVCXPRT *transp;\n");
274 f_print(fout,"\txdrproc_t xdr_result;\n");
275 f_print(fout,"\tcaddr_t result;\n");
277 f_print(fout, "{\n");
278 f_print(fout, "\txdr_free (xdr_result, result);\n");
279 f_print(fout,
280 "\n\t/*\n\t * Insert additional freeing code here, if needed\n\t */\n");
281 f_print(fout, "\n\treturn 1;\n}\n");
288 static void
289 return_type (proc_list * plist)
291 ptype (plist->res_prefix, plist->res_type, 1);
294 void
295 add_sample_msg (void)
297 f_print (fout, "/*\n");
298 f_print (fout, " * This is sample code generated by rpcgen.\n");
299 f_print (fout, " * These are only templates and you can use them\n");
300 f_print (fout, " * as a guideline for developing your own functions.\n");
301 f_print (fout, " */\n\n");
304 void
305 write_sample_clnt_main (void)
307 list *l;
308 definition *def;
309 version_list *vp;
311 f_print (fout, "\n\n");
312 if (Cflag)
313 f_print (fout, "int\nmain (int argc, char *argv[])\n{\n");
314 else
315 f_print (fout, "int\nmain (argc, argv)\nint argc;\nchar *argv[];\n{\n");
317 f_print (fout, "\tchar *host;");
318 f_print (fout, "\n\n\tif (argc < 2) {");
319 f_print (fout, "\n\t\tprintf (\"usage: %%s server_host\\n\", argv[0]);\n");
320 f_print (fout, "\t\texit (1);\n\t}");
321 f_print (fout, "\n\thost = argv[1];\n");
323 for (l = defined; l != NULL; l = l->next)
325 def = l->val;
326 if (def->def_kind != DEF_PROGRAM)
328 continue;
330 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
332 f_print (fout, "\t");
333 pvname (def->def_name, vp->vers_num);
334 f_print (fout, " (host);\n");
337 f_print (fout, "exit (0);\n}\n");