Handle DNS timeouts in old-style lookkup code.
[glibc.git] / sunrpc / rpc_sample.c
blob1f3bfd0cbb3518474c8221aeb6cd69175c8e11e2
1 /*
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
6 * met:
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
36 #include <stdio.h>
37 #include <string.h>
38 #include "rpc_parse.h"
39 #include "rpc_util.h"
40 #include "proto.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);
50 void
51 write_sample_svc (definition * def)
54 if (def->def_kind != DEF_PROGRAM)
55 return;
56 write_sample_server (def);
60 int
61 write_sample_clnt (definition * def)
63 version_list *vp;
64 int count = 0;
66 if (def->def_kind != DEF_PROGRAM)
67 return 0;
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);
72 ++count;
74 return count;
78 static void
79 write_sample_client (const char *program_name, version_list * vp)
81 proc_list *proc;
82 int i;
83 decl_list *l;
85 f_print (fout, "\n\nvoid\n");
86 pvname (program_name, vp->vers_num);
87 if (Cflag)
88 f_print (fout, "(char *host)\n{\n");
89 else
90 f_print (fout, "(host)\nchar *host;\n{\n");
91 f_print (fout, "\tCLIENT *clnt;\n");
93 i = 0;
94 for (proc = vp->procs; proc != NULL; proc = proc->next)
96 f_print (fout, "\t");
97 ++i;
98 if (mtflag)
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);
104 else
105 fprintf (fout, "*result_%d;\n", i);
107 else
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);
120 f_print (fout, " ");
122 else
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)
134 f_print (fout, " ");
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 */
151 i = 0;
152 for (proc = vp->procs; proc != NULL; proc = proc->next)
154 if (mtflag)
155 f_print(fout, "\tretval_%d = ",++i);
156 else
157 f_print (fout, "\tresult_%d = ", ++i);
158 pvname (proc->proc_name, vp->vers_num);
159 if (proc->arg_num < 2 && !newstyle)
161 f_print (fout, "(");
162 if (streq (proc->args.decls->decl.type, "void"))/* cast to void* */
163 f_print (fout, "(void*)");
164 f_print (fout, "&");
165 pvname (proc->proc_name, vp->vers_num);
166 if (mtflag)
167 f_print(fout, "_arg, &result_%d, clnt);\n", i);
168 else
169 f_print (fout, "_arg, clnt);\n");
171 else if (streq (proc->args.decls->decl.type, "void"))
173 if (mtflag)
174 f_print (fout, "(&result_%d, clnt);\n", i);
175 else
176 f_print (fout, "(clnt);\n");
178 else
180 f_print (fout, "(");
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);
186 if (mtflag)
187 f_print(fout, "&result_%d, ", i);
188 f_print (fout, "clnt);\n");
190 if (mtflag)
192 f_print(fout, "\tif (retval_%d != RPC_SUCCESS) {\n", i);
194 else
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");
210 static void
211 write_sample_server (definition * def)
213 version_list *vp;
214 proc_list *proc;
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");
221 if (!mtflag)
223 return_type (proc);
224 f_print (fout, "*\n");
226 else
227 f_print (fout, "bool_t\n");
228 if (Cflag || mtflag)
229 pvname_svc (proc->proc_name, vp->vers_num);
230 else
231 pvname(proc->proc_name, vp->vers_num);
232 printarglist(proc, "result", RQSTP, "struct svc_req *");
233 f_print(fout, "{\n");
234 if (!mtflag)
236 f_print(fout, "\tstatic ");
237 if(!streq(proc->res_type, "void"))
238 return_type(proc);
239 else
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");
245 else
246 f_print(fout, "\tbool_t retval;\n");
247 fprintf (fout, "\n\t/*\n\t * insert server code here\n\t */\n\n");
249 if (!mtflag)
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");
256 else
257 f_print(fout, "\treturn retval;\n}\n");
260 /* put in sample freeing routine */
261 if (mtflag)
263 f_print(fout, "\nint\n");
264 pvname(def->def_name, vp->vers_num);
265 if (Cflag)
266 f_print(fout,"_freeresult (SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)\n");
267 else
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");
276 f_print(fout,
277 "\n\t/*\n\t * Insert additional freeing code here, if needed\n\t */\n");
278 f_print(fout, "\n\treturn 1;\n}\n");
285 static void
286 return_type (proc_list * plist)
288 ptype (plist->res_prefix, plist->res_type, 1);
291 void
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");
301 void
302 write_sample_clnt_main (void)
304 list *l;
305 definition *def;
306 version_list *vp;
308 f_print (fout, "\n\n");
309 if (Cflag)
310 f_print (fout, "int\nmain (int argc, char *argv[])\n{\n");
311 else
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)
322 def = l->val;
323 if (def->def_kind != DEF_PROGRAM)
325 continue;
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");