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