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.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
30 /* #pragma ident "@(#)rpc_sample.c 1.9 94/04/25 SMI" */
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
37 * Copyright (C) 1987, Sun Microsystems, Inc.
42 #include "rpc_parse.h"
47 static char RQSTP
[] = "rqstp";
49 static void write_sample_client(const char *, version_list
* );
50 static void write_sample_server( definition
* );
51 static void return_type( proc_list
* );
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
) {
73 write_sample_client(def
->def_name
, vp
);
81 write_sample_client(const char *program_name
, version_list
*vp
)
87 f_print(fout
, "\n\nvoid\n");
88 pvname(program_name
, vp
->vers_num
);
89 f_print(fout
, "(char *host)\n{\n");
90 f_print(fout
, "\tCLIENT *clnt;\n");
93 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
96 f_print(fout
, "enum clnt_stat retval_%d;\n\t", ++i
);
97 ptype(proc
->res_prefix
, proc
->res_type
, 1);
98 f_print(fout
, "result_%d;\n", i
);
100 ptype(proc
->res_prefix
, proc
->res_type
, 1);
101 f_print(fout
, " *result_%d;\n",++i
);
103 /* print out declarations for arguments */
104 if(proc
->arg_num
< 2 && !newstyle
) {
106 if(!streq(proc
->args
.decls
->decl
.type
, "void"))
107 ptype(proc
->args
.decls
->decl
.prefix
,
108 proc
->args
.decls
->decl
.type
, 1);
110 f_print(fout
, "char * "); /* cannot have "void" type */
112 pvname(proc
->proc_name
, vp
->vers_num
);
113 f_print(fout
, "_arg;\n");
114 } else if (!streq(proc
->args
.decls
->decl
.type
, "void")) {
115 for (l
= proc
->args
.decls
; l
!= NULL
; l
= l
->next
) {
117 ptype(l
->decl
.prefix
, l
->decl
.type
, 1);
118 if (strcmp(l
->decl
.type
,"string") == 1)
120 pvname(proc
->proc_name
, vp
->vers_num
);
121 f_print(fout
, "_%s;\n", l
->decl
.name
);
126 /* generate creation of client handle */
127 f_print(fout
, "\n#ifndef\tDEBUG\n");
128 f_print(fout
, "\tclnt = clnt_create(host, %s, %s, \"%s\");\n",
129 program_name
, vp
->vers_name
, tirpcflag
? "netpath" : "udp");
130 f_print(fout
, "\tif (clnt == (CLIENT *) NULL) {\n");
131 f_print(fout
, "\t\tclnt_pcreateerror(host);\n");
132 f_print(fout
, "\t\texit(1);\n\t}\n");
133 f_print(fout
, "#endif\t/* DEBUG */\n\n");
135 /* generate calls to procedures */
137 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
139 f_print(fout
, "\tretval_%d = ",++i
);
141 f_print(fout
, "\tresult_%d = ",++i
);
142 pvname(proc
->proc_name
, vp
->vers_num
);
143 if (proc
->arg_num
< 2 && !newstyle
) {
145 if(streq(proc
->args
.decls
->decl
.type
, "void"))
147 f_print(fout
, "(void *)");
149 pvname(proc
->proc_name
, vp
->vers_num
);
151 f_print(fout
, "_arg, &result_%d, clnt);\n",
154 f_print(fout
, "_arg, clnt);\n");
156 } else if (streq(proc
->args
.decls
->decl
.type
, "void")) {
158 f_print(fout
, "(&result_%d, clnt);\n", i
);
160 f_print(fout
, "(clnt);\n");
164 for (l
= proc
->args
.decls
; l
!= NULL
; l
= l
->next
) {
165 pvname(proc
->proc_name
, vp
->vers_num
);
166 f_print(fout
, "_%s, ", l
->decl
.name
);
169 f_print(fout
, "&result_%d, ", i
);
171 f_print(fout
, "clnt);\n");
174 f_print(fout
, "\tif (retval_%d != RPC_SUCCESS) {\n", i
);
177 f_print(fout
, "\tif (result_%d == (", i
);
178 ptype(proc
->res_prefix
, proc
->res_type
, 1);
179 f_print(fout
, "*) NULL) {\n");
181 f_print(fout
, "\t\tclnt_perror(clnt, \"call failed\");\n");
182 f_print(fout
, "\t}\n");
185 f_print(fout
, "#ifndef\tDEBUG\n");
186 f_print(fout
, "\tclnt_destroy(clnt);\n");
187 f_print(fout
, "#endif\t /* DEBUG */\n");
188 f_print(fout
, "}\n");
192 write_sample_server(definition
*def
)
197 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
198 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
202 f_print(fout
, "*\n");
204 f_print(fout
, "bool_t\n");
205 pvname_svc(proc
->proc_name
, vp
->vers_num
);
206 printarglist(proc
, "result", RQSTP
, "struct svc_req *");
208 f_print(fout
, "{\n");
210 f_print(fout
, "\tstatic ");
211 if(!streq(proc
->res_type
, "void"))
214 f_print(fout
, "char *");
215 /* cannot have void type */
216 f_print(fout
, " result;\n");
219 f_print(fout
, "\tbool_t retval;\n");
221 "\n\t/*\n\t * insert server code here\n\t */\n\n");
224 if(!streq(proc
->res_type
, "void"))
225 f_print(fout
, "\treturn (&result);\n}\n");
226 else /* cast back to void * */
227 f_print(fout
, "\treturn((void *) &result);\n}\n");
229 f_print(fout
, "\treturn (retval);\n}\n");
231 /* put in sample freeing routine */
233 f_print(fout
, "\nint\n");
234 pvname(def
->def_name
, vp
->vers_num
);
235 f_print(fout
,"_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)\n");
236 f_print(fout
, "{\n");
237 f_print(fout
, "\t(void) xdr_free(xdr_result, result);\n");
239 "\n\t/*\n\t * Insert additional freeing code here, if needed\n\t */\n");
240 f_print(fout
, "\n}\n");
250 return_type(proc_list
*plist
)
252 ptype(plist
->res_prefix
, plist
->res_type
, 1);
258 f_print(fout
, "/*\n");
259 f_print(fout
, " * This is sample code generated by rpcgen.\n");
260 f_print(fout
, " * These are only templates and you can use them\n");
261 f_print(fout
, " * as a guideline for developing your own functions.\n");
262 f_print(fout
, " */\n\n");
266 write_sample_clnt_main(void)
272 f_print(fout
, "\n\n");
273 f_print(fout
, "main(int argc, char *argv[])\n{\n");
275 f_print(fout
, "\tchar *host;");
276 f_print(fout
, "\n\n\tif (argc < 2) {");
277 f_print(fout
, "\n\t\tprintf(\"usage: %%s server_host\\n\", argv[0]);\n");
278 f_print(fout
, "\t\texit(1);\n\t}");
279 f_print(fout
, "\n\thost = argv[1];\n");
281 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
283 if (def
->def_kind
!= DEF_PROGRAM
) {
286 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
288 pvname(def
->def_name
, vp
->vers_num
);
289 f_print(fout
, "(host);\n");
292 f_print(fout
, "}\n");