(gaih_inet): If NAME is a numerical IP address and AI_CANONNAME is set, return copy...
[glibc.git] / sunrpc / rpc_sample.c
blob79214a6a8323472e8598f53fd3f781da5438fa87
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 char sample_rcsid[] =
35 "$Id$";
38 * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
41 #include <stdio.h>
42 #include <string.h>
43 #include "rpc_parse.h"
44 #include "rpc_util.h"
45 #include "proto.h"
48 static const char RQSTP[] = "rqstp";
50 static void write_sample_client (const char *program_name, version_list * vp);
51 static void write_sample_server (definition * def);
52 static void return_type (proc_list * plist);
55 void
56 write_sample_svc (definition * def)
59 if (def->def_kind != DEF_PROGRAM)
60 return;
61 write_sample_server (def);
65 int
66 write_sample_clnt (definition * def)
68 version_list *vp;
69 int count = 0;
71 if (def->def_kind != DEF_PROGRAM)
72 return 0;
73 /* generate sample code for each version */
74 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
76 write_sample_client (def->def_name, vp);
77 ++count;
79 return count;
83 static void
84 write_sample_client (const char *program_name, version_list * vp)
86 proc_list *proc;
87 int i;
88 decl_list *l;
90 f_print (fout, "\n\nvoid\n");
91 pvname (program_name, vp->vers_num);
92 if (Cflag)
93 f_print (fout, "(char *host)\n{\n");
94 else
95 f_print (fout, "(host)\nchar *host;\n{\n");
96 f_print (fout, "\tCLIENT *clnt;\n");
98 i = 0;
99 for (proc = vp->procs; proc != NULL; proc = proc->next)
101 f_print (fout, "\t");
102 ++i;
103 if (mtflag)
105 f_print (fout, "enum clnt_stat retval_%d;\n\t", i);
106 ptype (proc->res_prefix, proc->res_type, 1);
107 if (!streq (proc->res_type, "void"))
108 f_print (fout, "result_%d;\n", i);
109 else
110 fprintf (fout, "*result_%d;\n", i);
112 else
114 ptype (proc->res_prefix, proc->res_type, 1);
115 f_print (fout, " *result_%d;\n", i);
117 /* print out declarations for arguments */
118 if (proc->arg_num < 2 && !newstyle)
120 f_print (fout, "\t");
121 if (!streq (proc->args.decls->decl.type, "void"))
123 ptype (proc->args.decls->decl.prefix,
124 proc->args.decls->decl.type, 1);
125 f_print (fout, " ");
127 else
128 f_print (fout, "char *"); /* cannot have "void" type */
129 pvname (proc->proc_name, vp->vers_num);
130 f_print (fout, "_arg;\n");
132 else if (!streq (proc->args.decls->decl.type, "void"))
134 for (l = proc->args.decls; l != NULL; l = l->next)
136 f_print (fout, "\t");
137 ptype (l->decl.prefix, l->decl.type, 1);
138 if (strcmp (l->decl.type, "string") == 1)
139 f_print (fout, " ");
140 pvname (proc->proc_name, vp->vers_num);
141 f_print (fout, "_%s;\n", l->decl.name);
146 /* generate creation of client handle */
147 f_print(fout, "\n#ifndef\tDEBUG\n");
148 f_print (fout, "\tclnt = clnt_create (host, %s, %s, \"%s\");\n",
149 program_name, vp->vers_name, tirpcflag ? "netpath" : "udp");
150 f_print (fout, "\tif (clnt == NULL) {\n");
151 f_print (fout, "\t\tclnt_pcreateerror (host);\n");
152 f_print (fout, "\t\texit (1);\n\t}\n");
153 f_print(fout, "#endif\t/* DEBUG */\n\n");
155 /* generate calls to procedures */
156 i = 0;
157 for (proc = vp->procs; proc != NULL; proc = proc->next)
159 if (mtflag)
160 f_print(fout, "\tretval_%d = ",++i);
161 else
162 f_print (fout, "\tresult_%d = ", ++i);
163 pvname (proc->proc_name, vp->vers_num);
164 if (proc->arg_num < 2 && !newstyle)
166 f_print (fout, "(");
167 if (streq (proc->args.decls->decl.type, "void"))/* cast to void* */
168 f_print (fout, "(void*)");
169 f_print (fout, "&");
170 pvname (proc->proc_name, vp->vers_num);
171 if (mtflag)
172 f_print(fout, "_arg, &result_%d, clnt);\n", i);
173 else
174 f_print (fout, "_arg, clnt);\n");
176 else if (streq (proc->args.decls->decl.type, "void"))
178 if (mtflag)
179 f_print (fout, "(&result_%d, clnt);\n", i);
180 else
181 f_print (fout, "(clnt);\n");
183 else
185 f_print (fout, "(");
186 for (l = proc->args.decls; l != NULL; l = l->next)
188 pvname (proc->proc_name, vp->vers_num);
189 f_print (fout, "_%s, ", l->decl.name);
191 if (mtflag)
192 f_print(fout, "&result_%d, ", i);
193 f_print (fout, "clnt);\n");
195 if (mtflag)
197 f_print(fout, "\tif (retval_%d != RPC_SUCCESS) {\n", i);
199 else
201 f_print(fout, "\tif (result_%d == (", i);
202 ptype(proc->res_prefix, proc->res_type, 1);
203 f_print(fout, "*) NULL) {\n");
205 f_print(fout, "\t\tclnt_perror (clnt, \"call failed\");\n");
206 f_print(fout, "\t}\n");
209 f_print (fout, "#ifndef\tDEBUG\n");
210 f_print (fout, "\tclnt_destroy (clnt);\n");
211 f_print (fout, "#endif\t /* DEBUG */\n");
212 f_print (fout, "}\n");
215 static void
216 write_sample_server (definition * def)
218 version_list *vp;
219 proc_list *proc;
221 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
223 for (proc = vp->procs; proc != NULL; proc = proc->next)
225 f_print (fout, "\n");
226 if (!mtflag)
228 return_type (proc);
229 f_print (fout, "*\n");
231 else
232 f_print (fout, "bool_t\n");
233 if (Cflag || mtflag)
234 pvname_svc (proc->proc_name, vp->vers_num);
235 else
236 pvname(proc->proc_name, vp->vers_num);
237 printarglist(proc, "result", RQSTP, "struct svc_req *");
238 f_print(fout, "{\n");
239 if (!mtflag)
241 f_print(fout, "\tstatic ");
242 if(!streq(proc->res_type, "void"))
243 return_type(proc);
244 else
245 f_print(fout, "char *");
246 /* cannot have void type */
247 /* f_print(fout, " result;\n", proc->res_type); */
248 f_print(fout, " result;\n");
250 else
251 f_print(fout, "\tbool_t retval;\n");
252 fprintf (fout, "\n\t/*\n\t * insert server code here\n\t */\n\n");
254 if (!mtflag)
256 if (!streq(proc->res_type, "void"))
257 f_print(fout, "\treturn &result;\n}\n");
258 else /* cast back to void * */
259 f_print(fout, "\treturn (void *) &result;\n}\n");
261 else
262 f_print(fout, "\treturn retval;\n}\n");
265 /* put in sample freeing routine */
266 if (mtflag)
268 f_print(fout, "\nint\n");
269 pvname(def->def_name, vp->vers_num);
270 if (Cflag)
271 f_print(fout,"_freeresult (SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)\n");
272 else
274 f_print(fout,"_freeresult (transp, xdr_result, result)\n");
275 f_print(fout,"\tSVCXPRT *transp;\n");
276 f_print(fout,"\txdrproc_t xdr_result;\n");
277 f_print(fout,"\tcaddr_t result;\n");
279 f_print(fout, "{\n");
280 f_print(fout, "\txdr_free (xdr_result, result);\n");
281 f_print(fout,
282 "\n\t/*\n\t * Insert additional freeing code here, if needed\n\t */\n");
283 f_print(fout, "\n\treturn 1;\n}\n");
290 static void
291 return_type (proc_list * plist)
293 ptype (plist->res_prefix, plist->res_type, 1);
296 void
297 add_sample_msg (void)
299 f_print (fout, "/*\n");
300 f_print (fout, " * This is sample code generated by rpcgen.\n");
301 f_print (fout, " * These are only templates and you can use them\n");
302 f_print (fout, " * as a guideline for developing your own functions.\n");
303 f_print (fout, " */\n\n");
306 void
307 write_sample_clnt_main (void)
309 list *l;
310 definition *def;
311 version_list *vp;
313 f_print (fout, "\n\n");
314 if (Cflag)
315 f_print (fout, "int\nmain (int argc, char *argv[])\n{\n");
316 else
317 f_print (fout, "int\nmain (argc, argv)\nint argc;\nchar *argv[];\n{\n");
319 f_print (fout, "\tchar *host;");
320 f_print (fout, "\n\n\tif (argc < 2) {");
321 f_print (fout, "\n\t\tprintf (\"usage: %%s server_host\\n\", argv[0]);\n");
322 f_print (fout, "\t\texit (1);\n\t}");
323 f_print (fout, "\n\thost = argv[1];\n");
325 for (l = defined; l != NULL; l = l->next)
327 def = l->val;
328 if (def->def_kind != DEF_PROGRAM)
330 continue;
332 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
334 f_print (fout, "\t");
335 pvname (def->def_name, vp->vers_num);
336 f_print (fout, " (host);\n");
339 f_print (fout, "exit (0);\n}\n");