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
38 * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
43 #include "rpc_parse.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
);
56 write_sample_svc (definition
* def
)
59 if (def
->def_kind
!= DEF_PROGRAM
)
61 write_sample_server (def
);
66 write_sample_clnt (definition
* def
)
71 if (def
->def_kind
!= DEF_PROGRAM
)
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
);
84 write_sample_client (const char *program_name
, version_list
* vp
)
90 f_print (fout
, "\n\nvoid\n");
91 pvname (program_name
, vp
->vers_num
);
93 f_print (fout
, "(char *host)\n{\n");
95 f_print (fout
, "(host)\nchar *host;\n{\n");
96 f_print (fout
, "\tCLIENT *clnt;\n");
99 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
101 f_print (fout
, "\t");
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
);
110 fprintf (fout
, "*result_%d;\n", i
);
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);
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)
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 */
157 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
160 f_print(fout
, "\tretval_%d = ",++i
);
162 f_print (fout
, "\tresult_%d = ", ++i
);
163 pvname (proc
->proc_name
, vp
->vers_num
);
164 if (proc
->arg_num
< 2 && !newstyle
)
167 if (streq (proc
->args
.decls
->decl
.type
, "void"))/* cast to void* */
168 f_print (fout
, "(void*)");
170 pvname (proc
->proc_name
, vp
->vers_num
);
172 f_print(fout
, "_arg, &result_%d, clnt);\n", i
);
174 f_print (fout
, "_arg, clnt);\n");
176 else if (streq (proc
->args
.decls
->decl
.type
, "void"))
179 f_print (fout
, "(&result_%d, clnt);\n", i
);
181 f_print (fout
, "(clnt);\n");
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
);
192 f_print(fout
, "&result_%d, ", i
);
193 f_print (fout
, "clnt);\n");
197 f_print(fout
, "\tif (retval_%d != RPC_SUCCESS) {\n", i
);
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");
216 write_sample_server (definition
* def
)
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");
229 f_print (fout
, "*\n");
232 f_print (fout
, "bool_t\n");
234 pvname_svc (proc
->proc_name
, vp
->vers_num
);
236 pvname(proc
->proc_name
, vp
->vers_num
);
237 printarglist(proc
, "result", RQSTP
, "struct svc_req *");
238 f_print(fout
, "{\n");
241 f_print(fout
, "\tstatic ");
242 if(!streq(proc
->res_type
, "void"))
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");
251 f_print(fout
, "\tbool_t retval;\n");
252 fprintf (fout
, "\n\t/*\n\t * insert server code here\n\t */\n\n");
255 if(!streq(proc
->res_type
, "void"))
256 f_print(fout
, "\treturn &result;\n}\n");
257 else /* cast back to void * */
258 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");