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