1 /* $NetBSD: rpc_sample.c,v 1.10 2004/06/20 22:20:16 jmc Exp $ */
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user or with the express written consent of
9 * Sun Microsystems, Inc.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
39 static char sccsid
[] = "@(#)rpc_sample.c 1.1 90/08/30 (C) 1987 SMI";
41 __RCSID("$NetBSD: rpc_sample.c,v 1.10 2004/06/20 22:20:16 jmc Exp $");
46 * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
52 #include "rpc_parse.h"
55 static char RQSTP
[] = "rqstp";
57 static void write_sample_client
__P((char *, version_list
*));
58 static void write_sample_server
__P((definition
*));
59 static void return_type
__P((proc_list
*));
66 if (def
->def_kind
!= DEF_PROGRAM
)
68 write_sample_server(def
);
73 write_sample_clnt(def
)
79 if (def
->def_kind
!= DEF_PROGRAM
)
81 /* generate sample code for each version */
82 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
83 write_sample_client(def
->def_name
, vp
);
91 write_sample_client(program_name
, vp
)
98 f_print(fout
, "\n\nvoid\n");
99 pvname(program_name
, vp
->vers_num
);
101 f_print(fout
, "(char *host)\n{\n");
103 f_print(fout
, "(host)\n\tchar *host;\n{\n");
104 f_print(fout
, "\tCLIENT *clnt;\n");
106 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
107 /* print out declarations for arguments */
108 if (proc
->arg_num
< 2 && !newstyle
) {
110 if (streq(proc
->args
.decls
->decl
.type
, "void"))
111 f_print(fout
, "char "); /* cannot have "void"
114 ptype(proc
->args
.decls
->decl
.prefix
, proc
->args
.decls
->decl
.type
, 1);
115 pvname(proc
->proc_name
, vp
->vers_num
);
116 f_print(fout
, "_arg;\n");
118 if (!streq(proc
->args
.decls
->decl
.type
, "void")) {
119 for (l
= proc
->args
.decls
; l
!= NULL
; l
= l
->next
) {
121 ptype(l
->decl
.prefix
, l
->decl
.type
, 1);
122 pvname(proc
->proc_name
, vp
->vers_num
);
123 f_print(fout
, "_%s;\n", l
->decl
.name
);
124 /* pdeclaration(proc->args.argname, &l->decl, 1, ";\n" );*/
128 /* print out declarations for results */
130 if (streq(proc
->res_type
, "void"))
131 f_print(fout
, "char"); /* cannot have "void"
134 ptype(proc
->res_prefix
, proc
->res_type
, 1);
137 pvname(proc
->proc_name
, vp
->vers_num
);
138 f_print(fout
, "_res;\n");
142 /* generate creation of client handle */
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");
149 /* generate calls to procedures */
150 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
152 f_print(fout
, "\tif (");
155 pvname(proc
->proc_name
, vp
->vers_num
);
156 f_print(fout
, "_res = ");
158 pvname(proc
->proc_name
, vp
->vers_num
);
159 if (proc
->arg_num
< 2 && !newstyle
) {
161 if (streq(proc
->args
.decls
->decl
.type
, "void")) /* cast to void* */
162 f_print(fout
, "(void *)");
164 pvname(proc
->proc_name
, vp
->vers_num
);
165 f_print(fout
, "_arg, ");
167 if (streq(proc
->args
.decls
->decl
.type
, "void")) {
168 f_print(fout
, "(clnt);\n");
171 for (l
= proc
->args
.decls
; l
!= NULL
; l
= l
->next
) {
172 pvname(proc
->proc_name
, vp
->vers_num
);
173 f_print(fout
, "_%s, ", l
->decl
.name
);
179 pvname(proc
->proc_name
, vp
->vers_num
);
180 f_print(fout
, "_res, clnt) != RPC_SUCCESS)\n");
182 f_print(fout
, "clnt);\n");
183 f_print(fout
, "\tif (");
184 pvname(proc
->proc_name
, vp
->vers_num
);
185 f_print(fout
, "_res == NULL)\n");
187 f_print(fout
, "\t\tclnt_perror(clnt, \"call failed\");\n");
190 f_print(fout
, "\tclnt_destroy(clnt);\n");
191 f_print(fout
, "}\n");
195 write_sample_server(def
)
201 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
202 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
205 f_print(fout
, "bool_t\n");
208 f_print(fout
, "*\n");
210 pvname_svc(proc
->proc_name
, vp
->vers_num
);
211 printarglist(proc
, "result", RQSTP
, "struct svc_req *");
213 f_print(fout
, "{\n");
215 f_print(fout
, "\tbool_t retval = TRUE;\n");
217 f_print(fout
, "\tstatic ");
218 if (streq(proc
->res_type
, "void"))
219 f_print(fout
, "char "); /* cannot have void type */
222 f_print(fout
, "result;\n");
225 "\n\t/*\n\t * insert server code here\n\t */\n\n");
227 f_print(fout
, "\treturn (retval);\n");
229 if (streq(proc
->res_type
, "void"))
230 f_print(fout
, "\treturn ((void *)&result);\n");
232 f_print(fout
, "\treturn (&result);\n");
234 f_print(fout
, "}\n");
243 ptype(plist
->res_prefix
, plist
->res_type
, 1);
249 f_print(fout
, "/*\n");
250 f_print(fout
, " * This is sample code generated by rpcgen.\n");
251 f_print(fout
, " * These are only templates and you can use them\n");
252 f_print(fout
, " * as a guideline for developing your own functions.\n");
253 f_print(fout
, " */\n\n");
257 write_sample_clnt_main()
263 f_print(fout
, "\n\n");
265 f_print(fout
, "int\nmain(int argc, char *argv[])\n{\n");
267 f_print(fout
, "int\nmain(argc, argv)\n\tint argc;\n\tchar *argv[];\n{\n");
269 f_print(fout
, "\tchar *host;");
270 f_print(fout
, "\n\n\tif (argc < 2) {");
271 f_print(fout
, "\n\t\tprintf(\"usage: %%s server_host\\n\", argv[0]);\n");
272 f_print(fout
, "\t\texit(1);\n\t}");
273 f_print(fout
, "\n\thost = argv[1];\n");
275 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
277 if (def
->def_kind
!= DEF_PROGRAM
) {
280 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
282 pvname(def
->def_name
, vp
->vers_num
);
283 f_print(fout
, "(host);\n");
286 f_print(fout
, "\texit(0);\n");
287 f_print(fout
, "}\n");