Sync usage with man page.
[netbsd-mini2440.git] / usr.bin / rpcgen / rpc_sample.c
blob9b39238f5bb6f68a67d1810b8a79e189ad4025c5
1 /* $NetBSD: rpc_sample.c,v 1.10 2004/06/20 22:20:16 jmc Exp $ */
2 /*
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.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)rpc_sample.c 1.1 90/08/30 (C) 1987 SMI";
40 #else
41 __RCSID("$NetBSD: rpc_sample.c,v 1.10 2004/06/20 22:20:16 jmc Exp $");
42 #endif
43 #endif
46 * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
49 #include <stdio.h>
50 #include <string.h>
51 #include "rpc_scan.h"
52 #include "rpc_parse.h"
53 #include "rpc_util.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 *));
61 void
62 write_sample_svc(def)
63 definition *def;
66 if (def->def_kind != DEF_PROGRAM)
67 return;
68 write_sample_server(def);
72 int
73 write_sample_clnt(def)
74 definition *def;
76 version_list *vp;
77 int count = 0;
79 if (def->def_kind != DEF_PROGRAM)
80 return (0);
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);
84 ++count;
86 return (count);
90 static void
91 write_sample_client(program_name, vp)
92 char *program_name;
93 version_list *vp;
95 proc_list *proc;
96 decl_list *l;
98 f_print(fout, "\n\nvoid\n");
99 pvname(program_name, vp->vers_num);
100 if (Cflag)
101 f_print(fout, "(char *host)\n{\n");
102 else
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) {
109 f_print(fout, "\t");
110 if (streq(proc->args.decls->decl.type, "void"))
111 f_print(fout, "char "); /* cannot have "void"
112 * type */
113 else
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");
117 } else {
118 if (!streq(proc->args.decls->decl.type, "void")) {
119 for (l = proc->args.decls; l != NULL; l = l->next) {
120 f_print(fout, "\t");
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 */
129 f_print(fout, "\t");
130 if (streq(proc->res_type, "void"))
131 f_print(fout, "char"); /* cannot have "void"
132 * type */
133 else
134 ptype(proc->res_prefix, proc->res_type, 1);
135 if (!Mflag)
136 f_print(fout, "*");
137 pvname(proc->proc_name, vp->vers_num);
138 f_print(fout, "_res;\n");
140 f_print(fout, "\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) {
151 if (Mflag)
152 f_print(fout, "\tif (");
153 else {
154 f_print(fout, "\t");
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) {
160 f_print(fout, "(");
161 if (streq(proc->args.decls->decl.type, "void")) /* cast to void* */
162 f_print(fout, "(void *)");
163 f_print(fout, "&");
164 pvname(proc->proc_name, vp->vers_num);
165 f_print(fout, "_arg, ");
166 } else {
167 if (streq(proc->args.decls->decl.type, "void")) {
168 f_print(fout, "(clnt);\n");
169 } else {
170 f_print(fout, "(");
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);
177 if (Mflag) {
178 f_print(fout, "&");
179 pvname(proc->proc_name, vp->vers_num);
180 f_print(fout, "_res, clnt) != RPC_SUCCESS)\n");
181 } else {
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");
194 static void
195 write_sample_server(def)
196 definition *def;
198 version_list *vp;
199 proc_list *proc;
201 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
202 for (proc = vp->procs; proc != NULL; proc = proc->next) {
203 f_print(fout, "\n");
204 if (Mflag)
205 f_print(fout, "bool_t\n");
206 else {
207 return_type(proc);
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");
214 if (Mflag) {
215 f_print(fout, "\tbool_t retval = TRUE;\n");
216 } else {
217 f_print(fout, "\tstatic ");
218 if (streq(proc->res_type, "void"))
219 f_print(fout, "char "); /* cannot have void type */
220 else
221 return_type(proc);
222 f_print(fout, "result;\n");
224 f_print(fout,
225 "\n\t/*\n\t * insert server code here\n\t */\n\n");
226 if (Mflag) {
227 f_print(fout, "\treturn (retval);\n");
228 } else {
229 if (streq(proc->res_type, "void"))
230 f_print(fout, "\treturn ((void *)&result);\n");
231 else
232 f_print(fout, "\treturn (&result);\n");
234 f_print(fout, "}\n");
239 static void
240 return_type(plist)
241 proc_list *plist;
243 ptype(plist->res_prefix, plist->res_type, 1);
246 void
247 add_sample_msg()
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");
256 void
257 write_sample_clnt_main()
259 list *l;
260 definition *def;
261 version_list *vp;
263 f_print(fout, "\n\n");
264 if (Cflag)
265 f_print(fout, "int\nmain(int argc, char *argv[])\n{\n");
266 else
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) {
276 def = l->val;
277 if (def->def_kind != DEF_PROGRAM) {
278 continue;
280 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
281 f_print(fout, "\t");
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");