MFC r1.28:
[dragonfly.git] / usr.bin / rpcgen / rpc_sample.c
blobd61d057dc278dbfb56b9320206016c4e0e49ab99
1 /*
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.
8 *
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.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
28 * $DragonFly: src/usr.bin/rpcgen/rpc_sample.c,v 1.4 2004/06/19 16:40:36 joerg Exp $
31 #pragma ident "@(#)rpc_sample.c 1.9 94/04/25 SMI"
34 * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
35 * Copyright (C) 1987, Sun Microsystems, Inc.
38 #include <stdio.h>
39 #include <string.h>
40 #include "rpc_parse.h"
41 #include "rpc_util.h"
43 static char RQSTP[] = "rqstp";
45 extern void printarglist(proc_list *, char *, char *, char *);
46 static void write_sample_client(char *, version_list *);
47 static void write_sample_server(definition *);
48 static void return_type(proc_list *);
50 void
51 write_sample_svc(definition *def)
53 if (def->def_kind != DEF_PROGRAM)
54 return;
55 write_sample_server(def);
59 int
60 write_sample_clnt(definition *def)
62 version_list *vp;
63 int count = 0;
65 if (def->def_kind != DEF_PROGRAM)
66 return(0);
67 /* generate sample code for each version */
68 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
69 write_sample_client(def->def_name, vp);
70 ++count;
72 return(count);
76 static void
77 write_sample_client(char *program_name, version_list *vp)
79 proc_list *proc;
80 int i;
81 decl_list *l;
83 f_print(fout, "\n\nvoid\n");
84 pvname(program_name, vp->vers_num);
85 if(Cflag)
86 f_print(fout,"(char *host)\n{\n");
87 else
88 f_print(fout, "(host)\n\tchar *host;\n{\n");
89 f_print(fout, "\tCLIENT *clnt;\n");
91 i = 0;
92 for (proc = vp->procs; proc != NULL; proc = proc->next) {
93 f_print(fout, "\t");
94 if (mtflag) {
95 f_print(fout, "enum clnt_stat retval_%d;\n\t", ++i);
96 ptype(proc->res_prefix, proc->res_type, 1);
97 f_print(fout, "result_%d;\n", i);
98 } else {
99 ptype(proc->res_prefix, proc->res_type, 1);
100 f_print(fout, " *result_%d;\n",++i);
102 /* print out declarations for arguments */
103 if(proc->arg_num < 2 && !newstyle) {
104 f_print(fout, "\t");
105 if(!streq(proc->args.decls->decl.type, "void"))
106 ptype(proc->args.decls->decl.prefix,
107 proc->args.decls->decl.type, 1);
108 else
109 f_print(fout, "char * "); /* cannot have "void" type */
110 f_print(fout, " ");
111 pvname(proc->proc_name, vp->vers_num);
112 f_print(fout, "_arg;\n");
113 } else if (!streq(proc->args.decls->decl.type, "void")) {
114 for (l = proc->args.decls; l != NULL; l = l->next) {
115 f_print(fout, "\t");
116 ptype(l->decl.prefix, l->decl.type, 1);
117 if (strcmp(l->decl.type,"string") == 1)
118 f_print(fout, " ");
119 pvname(proc->proc_name, vp->vers_num);
120 f_print(fout, "_%s;\n", l->decl.name);
125 /* generate creation of client handle */
126 f_print(fout, "\n#ifndef\tDEBUG\n");
127 f_print(fout, "\tclnt = clnt_create(host, %s, %s, \"%s\");\n",
128 program_name, vp->vers_name, tirpcflag? "netpath" : "udp");
129 f_print(fout, "\tif (clnt == (CLIENT *) NULL) {\n");
130 f_print(fout, "\t\tclnt_pcreateerror(host);\n");
131 f_print(fout, "\t\texit(1);\n\t}\n");
132 f_print(fout, "#endif\t/* DEBUG */\n\n");
134 /* generate calls to procedures */
135 i = 0;
136 for (proc = vp->procs; proc != NULL; proc = proc->next) {
137 if (mtflag)
138 f_print(fout, "\tretval_%d = ",++i);
139 else
140 f_print(fout, "\tresult_%d = ",++i);
141 pvname(proc->proc_name, vp->vers_num);
142 if (proc->arg_num < 2 && !newstyle) {
143 f_print(fout, "(");
144 if(streq(proc->args.decls->decl.type, "void")) {
145 /* cast to void * */
146 f_print(fout, "(void *)");
148 f_print(fout, "&");
149 pvname(proc->proc_name, vp->vers_num);
150 if (mtflag)
151 f_print(fout, "_arg, &result_%d, clnt);\n", i);
152 else
153 f_print(fout, "_arg, clnt);\n");
154 } else if (streq(proc->args.decls->decl.type, "void")) {
155 if (mtflag)
156 f_print(fout, "(&result_%d, clnt);\n", i);
157 else
158 f_print(fout, "(clnt);\n");
160 else {
161 f_print(fout, "(");
162 for (l = proc->args.decls; l != NULL; l = l->next) {
163 pvname(proc->proc_name, vp->vers_num);
164 f_print(fout, "_%s, ", l->decl.name);
166 if (mtflag)
167 f_print(fout, "&result_%d, ", i);
169 f_print(fout, "clnt);\n");
171 if (mtflag) {
172 f_print(fout, "\tif (retval_%d != RPC_SUCCESS) {\n", i);
173 } else {
174 f_print(fout, "\tif (result_%d == (", i);
175 ptype(proc->res_prefix, proc->res_type, 1);
176 f_print(fout, "*) NULL) {\n");
178 f_print(fout, "\t\tclnt_perror(clnt, \"call failed\");\n");
179 f_print(fout, "\t}\n");
182 f_print(fout, "#ifndef\tDEBUG\n");
183 f_print(fout, "\tclnt_destroy(clnt);\n");
184 f_print(fout, "#endif\t /* DEBUG */\n");
185 f_print(fout, "}\n");
188 static void
189 write_sample_server(definition *def)
191 version_list *vp;
192 proc_list *proc;
194 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
195 for (proc = vp->procs; proc != NULL; proc = proc->next) {
196 f_print(fout, "\n");
197 if (!mtflag) {
198 return_type(proc);
199 f_print(fout, "*\n");
200 } else
201 f_print(fout, "bool_t\n");
202 if (Cflag || mtflag)
203 pvname_svc(proc->proc_name, vp->vers_num);
204 else
205 pvname(proc->proc_name, vp->vers_num);
206 printarglist(proc, "result", RQSTP, "struct svc_req *");
208 f_print(fout, "{\n");
209 if (!mtflag) {
210 f_print(fout, "\tstatic ");
211 if(!streq(proc->res_type, "void"))
212 return_type(proc);
213 else
214 f_print(fout, "char *");
215 /* cannot have void type */
216 f_print(fout, " result;\n");
218 else {
219 f_print(fout, "\tbool_t retval;\n");
221 f_print(fout,
222 "\n\t/*\n\t * insert server code here\n\t */\n\n");
224 if (!mtflag) {
225 if(!streq(proc->res_type, "void"))
226 f_print(fout, "\treturn (&result);\n}\n");
227 else /* cast back to void * */
228 f_print(fout, "\treturn((void *) &result);\n}\n");
229 } else {
230 f_print(fout, "\treturn (retval);\n}\n");
233 /* put in sample freeing routine */
234 if (mtflag) {
235 f_print(fout, "\nint\n");
236 pvname(def->def_name, vp->vers_num);
237 if (Cflag) {
238 f_print(fout,"_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)\n");
239 } else {
240 f_print(fout,"_freeresult(transp, xdr_result, result)\n");
241 f_print(fout,"\tSVCXPRT *transp;\n");
242 f_print(fout,"\txdrproc_t xdr_result;\n");
243 f_print(fout,"\tcaddr_t result;\n");
245 f_print(fout, "{\n");
246 f_print(fout, "\t(void) xdr_free(xdr_result, result);\n");
247 f_print(fout,
248 "\n\t/*\n\t * Insert additional freeing code here, if needed\n\t */\n");
249 f_print(fout, "\n}\n");
256 static void
257 return_type(proc_list *plist)
259 ptype(plist->res_prefix, plist->res_type, 1);
262 void
263 add_sample_msg(void)
265 f_print(fout, "/*\n");
266 f_print(fout, " * This is sample code generated by rpcgen.\n");
267 f_print(fout, " * These are only templates and you can use them\n");
268 f_print(fout, " * as a guideline for developing your own functions.\n");
269 f_print(fout, " */\n\n");
272 void
273 write_sample_clnt_main(void)
275 list *l;
276 definition *def;
277 version_list *vp;
279 f_print(fout, "\n\n");
280 if(Cflag)
281 f_print(fout,"main(int argc, char *argv[])\n{\n");
282 else
283 f_print(fout, "main(argc, argv)\n\tint argc;\n"
284 "\tchar *argv[];\n{\n");
286 f_print(fout, "\tchar *host;");
287 f_print(fout, "\n\n\tif (argc < 2) {");
288 f_print(fout, "\n\t\tprintf(\"usage: %%s server_host\\n\", argv[0]);\n");
289 f_print(fout, "\t\texit(1);\n\t}");
290 f_print(fout, "\n\thost = argv[1];\n");
292 for (l = defined; l != NULL; l = l->next) {
293 def = l->val;
294 if (def->def_kind != DEF_PROGRAM)
295 continue;
296 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
297 f_print(fout, "\t");
298 pvname(def->def_name, vp->vers_num);
299 f_print(fout, "(host);\n");
302 f_print(fout, "}\n");