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.
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
41 #include "rpc_parse.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
);
54 write_sample_svc (definition
* def
)
57 if (def
->def_kind
!= DEF_PROGRAM
)
59 write_sample_server (def
);
64 write_sample_clnt (definition
* def
)
69 if (def
->def_kind
!= DEF_PROGRAM
)
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
);
82 write_sample_client (const char *program_name
, version_list
* vp
)
88 f_print (fout
, "\n\nvoid\n");
89 pvname (program_name
, vp
->vers_num
);
91 f_print (fout
, "(char *host)\n{\n");
93 f_print (fout
, "(host)\nchar *host;\n{\n");
94 f_print (fout
, "\tCLIENT *clnt;\n");
97 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
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
);
108 fprintf (fout
, "*result_%d;\n", i
);
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);
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)
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 */
155 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
158 f_print(fout
, "\tretval_%d = ",++i
);
160 f_print (fout
, "\tresult_%d = ", ++i
);
161 pvname (proc
->proc_name
, vp
->vers_num
);
162 if (proc
->arg_num
< 2 && !newstyle
)
165 if (streq (proc
->args
.decls
->decl
.type
, "void"))/* cast to void* */
166 f_print (fout
, "(void*)");
168 pvname (proc
->proc_name
, vp
->vers_num
);
170 f_print(fout
, "_arg, &result_%d, clnt);\n", i
);
172 f_print (fout
, "_arg, clnt);\n");
174 else if (streq (proc
->args
.decls
->decl
.type
, "void"))
177 f_print (fout
, "(&result_%d, clnt);\n", i
);
179 f_print (fout
, "(clnt);\n");
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
);
190 f_print(fout
, "&result_%d, ", i
);
191 f_print (fout
, "clnt);\n");
195 f_print(fout
, "\tif (retval_%d != RPC_SUCCESS) {\n", i
);
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");
214 write_sample_server (definition
* def
)
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");
227 f_print (fout
, "*\n");
230 f_print (fout
, "bool_t\n");
232 pvname_svc (proc
->proc_name
, vp
->vers_num
);
234 pvname(proc
->proc_name
, vp
->vers_num
);
235 printarglist(proc
, "result", RQSTP
, "struct svc_req *");
236 f_print(fout
, "{\n");
239 f_print(fout
, "\tstatic ");
240 if(!streq(proc
->res_type
, "void"))
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");
249 f_print(fout
, "\tbool_t retval;\n");
250 fprintf (fout
, "\n\t/*\n\t * insert server code here\n\t */\n\n");
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");
260 f_print(fout
, "\treturn retval;\n}\n");
263 /* put in sample freeing routine */
266 f_print(fout
, "\nint\n");
267 pvname(def
->def_name
, vp
->vers_num
);
269 f_print(fout
,"_freeresult (SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)\n");
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");
280 "\n\t/*\n\t * Insert additional freeing code here, if needed\n\t */\n");
281 f_print(fout
, "\n\treturn 1;\n}\n");
289 return_type (proc_list
* plist
)
291 ptype (plist
->res_prefix
, plist
->res_type
, 1);
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");
305 write_sample_clnt_main (void)
311 f_print (fout
, "\n\n");
313 f_print (fout
, "int\nmain (int argc, char *argv[])\n{\n");
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
)
326 if (def
->def_kind
!= DEF_PROGRAM
)
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");