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
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static const char sample_rcsid
[] =
40 * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
45 #include "rpc_parse.h"
50 static const char RQSTP
[] = "rqstp";
52 static void write_sample_client (const char *program_name
, version_list
* vp
);
53 static void write_sample_server (definition
* def
);
54 static void return_type (proc_list
* plist
);
58 write_sample_svc (definition
* def
)
61 if (def
->def_kind
!= DEF_PROGRAM
)
63 write_sample_server (def
);
68 write_sample_clnt (definition
* def
)
73 if (def
->def_kind
!= DEF_PROGRAM
)
75 /* generate sample code for each version */
76 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
78 write_sample_client (def
->def_name
, vp
);
86 write_sample_client (const char *program_name
, version_list
* vp
)
92 f_print (fout
, "\n\nvoid\n");
93 pvname (program_name
, vp
->vers_num
);
95 f_print (fout
, "(char *host)\n{\n");
97 f_print (fout
, "(host)\nchar *host;\n{\n");
98 f_print (fout
, "\tCLIENT *clnt;\n");
101 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
103 f_print (fout
, "\t");
107 f_print (fout
, "enum clnt_stat retval_%d;\n\t", i
);
108 ptype (proc
->res_prefix
, proc
->res_type
, 1);
109 if (!streq (proc
->res_type
, "void"))
110 f_print (fout
, "result_%d;\n", i
);
112 fprintf (fout
, "*result_%d;\n", i
);
116 ptype (proc
->res_prefix
, proc
->res_type
, 1);
117 f_print (fout
, " *result_%d;\n", i
);
119 /* print out declarations for arguments */
120 if (proc
->arg_num
< 2 && !newstyle
)
122 f_print (fout
, "\t");
123 if (!streq (proc
->args
.decls
->decl
.type
, "void"))
125 ptype (proc
->args
.decls
->decl
.prefix
,
126 proc
->args
.decls
->decl
.type
, 1);
130 f_print (fout
, "char *"); /* cannot have "void" type */
131 pvname (proc
->proc_name
, vp
->vers_num
);
132 f_print (fout
, "_arg;\n");
134 else if (!streq (proc
->args
.decls
->decl
.type
, "void"))
136 for (l
= proc
->args
.decls
; l
!= NULL
; l
= l
->next
)
138 f_print (fout
, "\t");
139 ptype (l
->decl
.prefix
, l
->decl
.type
, 1);
140 if (strcmp (l
->decl
.type
, "string") == 1)
142 pvname (proc
->proc_name
, vp
->vers_num
);
143 f_print (fout
, "_%s;\n", l
->decl
.name
);
148 /* generate creation of client handle */
149 f_print(fout
, "\n#ifndef\tDEBUG\n");
150 f_print (fout
, "\tclnt = clnt_create (host, %s, %s, \"%s\");\n",
151 program_name
, vp
->vers_name
, tirpcflag
? "netpath" : "udp");
152 f_print (fout
, "\tif (clnt == NULL) {\n");
153 f_print (fout
, "\t\tclnt_pcreateerror (host);\n");
154 f_print (fout
, "\t\texit (1);\n\t}\n");
155 f_print(fout
, "#endif\t/* DEBUG */\n\n");
157 /* generate calls to procedures */
159 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
162 f_print(fout
, "\tretval_%d = ",++i
);
164 f_print (fout
, "\tresult_%d = ", ++i
);
165 pvname (proc
->proc_name
, vp
->vers_num
);
166 if (proc
->arg_num
< 2 && !newstyle
)
169 if (streq (proc
->args
.decls
->decl
.type
, "void"))/* cast to void* */
170 f_print (fout
, "(void*)");
172 pvname (proc
->proc_name
, vp
->vers_num
);
174 f_print(fout
, "_arg, &result_%d, clnt);\n", i
);
176 f_print (fout
, "_arg, clnt);\n");
178 else if (streq (proc
->args
.decls
->decl
.type
, "void"))
181 f_print (fout
, "(&result_%d, clnt);\n", i
);
183 f_print (fout
, "(clnt);\n");
188 for (l
= proc
->args
.decls
; l
!= NULL
; l
= l
->next
)
190 pvname (proc
->proc_name
, vp
->vers_num
);
191 f_print (fout
, "_%s, ", l
->decl
.name
);
194 f_print(fout
, "&result_%d, ", i
);
195 f_print (fout
, "clnt);\n");
199 f_print(fout
, "\tif (retval_%d != RPC_SUCCESS) {\n", i
);
203 f_print(fout
, "\tif (result_%d == (", i
);
204 ptype(proc
->res_prefix
, proc
->res_type
, 1);
205 f_print(fout
, "*) NULL) {\n");
207 f_print(fout
, "\t\tclnt_perror (clnt, \"call failed\");\n");
208 f_print(fout
, "\t}\n");
211 f_print (fout
, "#ifndef\tDEBUG\n");
212 f_print (fout
, "\tclnt_destroy (clnt);\n");
213 f_print (fout
, "#endif\t /* DEBUG */\n");
214 f_print (fout
, "}\n");
218 write_sample_server (definition
* def
)
223 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
225 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
227 f_print (fout
, "\n");
231 f_print (fout
, "*\n");
234 f_print (fout
, "bool_t\n");
236 pvname_svc (proc
->proc_name
, vp
->vers_num
);
238 pvname(proc
->proc_name
, vp
->vers_num
);
239 printarglist(proc
, "result", RQSTP
, "struct svc_req *");
240 f_print(fout
, "{\n");
243 f_print(fout
, "\tstatic ");
244 if(!streq(proc
->res_type
, "void"))
247 f_print(fout
, "char *");
248 /* cannot have void type */
249 /* f_print(fout, " result;\n", proc->res_type); */
250 f_print(fout
, " result;\n");
253 f_print(fout
, "\tbool_t retval;\n");
254 fprintf (fout
, "\n\t/*\n\t * insert server code here\n\t */\n\n");
258 if (!streq(proc
->res_type
, "void"))
259 f_print(fout
, "\treturn &result;\n}\n");
260 else /* cast back to void * */
261 f_print(fout
, "\treturn (void *) &result;\n}\n");
264 f_print(fout
, "\treturn retval;\n}\n");
267 /* put in sample freeing routine */
270 f_print(fout
, "\nint\n");
271 pvname(def
->def_name
, vp
->vers_num
);
273 f_print(fout
,"_freeresult (SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)\n");
276 f_print(fout
,"_freeresult (transp, xdr_result, result)\n");
277 f_print(fout
,"\tSVCXPRT *transp;\n");
278 f_print(fout
,"\txdrproc_t xdr_result;\n");
279 f_print(fout
,"\tcaddr_t result;\n");
281 f_print(fout
, "{\n");
282 f_print(fout
, "\txdr_free (xdr_result, result);\n");
284 "\n\t/*\n\t * Insert additional freeing code here, if needed\n\t */\n");
285 f_print(fout
, "\n\treturn 1;\n}\n");
293 return_type (proc_list
* plist
)
295 ptype (plist
->res_prefix
, plist
->res_type
, 1);
299 add_sample_msg (void)
301 f_print (fout
, "/*\n");
302 f_print (fout
, " * This is sample code generated by rpcgen.\n");
303 f_print (fout
, " * These are only templates and you can use them\n");
304 f_print (fout
, " * as a guideline for developing your own functions.\n");
305 f_print (fout
, " */\n\n");
309 write_sample_clnt_main (void)
315 f_print (fout
, "\n\n");
317 f_print (fout
, "int\nmain (int argc, char *argv[])\n{\n");
319 f_print (fout
, "int\nmain (argc, argv)\nint argc;\nchar *argv[];\n{\n");
321 f_print (fout
, "\tchar *host;");
322 f_print (fout
, "\n\n\tif (argc < 2) {");
323 f_print (fout
, "\n\t\tprintf (\"usage: %%s server_host\\n\", argv[0]);\n");
324 f_print (fout
, "\t\texit (1);\n\t}");
325 f_print (fout
, "\n\thost = argv[1];\n");
327 for (l
= defined
; l
!= NULL
; l
= l
->next
)
330 if (def
->def_kind
!= DEF_PROGRAM
)
334 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
336 f_print (fout
, "\t");
337 pvname (def
->def_name
, vp
->vers_num
);
338 f_print (fout
, " (host);\n");
341 f_print (fout
, "exit (0);\n}\n");