]> git.decadent.org.uk Git - nfs-utils.git/blob - tools/rpcgen/rpc_sample.c
2000-08-30 H.J. Lu <hjl@lucon.org>
[nfs-utils.git] / tools / 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 or with the express written consent of
8  * Sun Microsystems, Inc.
9  *
10  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13  *
14  * Sun RPC is provided with no support and without any obligation on the
15  * part of Sun Microsystems, Inc. to assist in its use, correction,
16  * modification or enhancement.
17  *
18  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20  * OR ANY PART THEREOF.
21  *
22  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23  * or profits or other special, indirect and consequential damages, even if
24  * Sun has been advised of the possibility of such damages.
25  *
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  */
30
31 #ifndef lint
32 static char sccsid[] = "@(#)rpc_sample.c  1.1  90/08/30  (C) 1987 SMI";
33
34 #endif
35
36 /*
37  * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
38  */
39
40 #include <stdio.h>
41 #include <string.h>
42 #include "rpc_parse.h"
43 #include "rpc_util.h"
44
45
46 static char RQSTP[] = "rqstp";
47
48 static void     write_sample_client(char *program_name, version_list *vp);
49 static void     write_sample_server(definition * def);
50 static void     return_type(proc_list *plist);
51
52 void
53 write_sample_svc(definition *def)
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(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         if (Cflag)
88                 f_print(fout, "( char* host )\n{\n");
89         else
90                 f_print(fout, "(host)\nchar *host;\n{\n");
91         f_print(fout, "\tCLIENT *clnt;\n");
92
93         i = 0;
94         for (proc = vp->procs; proc != NULL; proc = proc->next) {
95                 f_print(fout, "\t");
96                 ptype(proc->res_prefix, proc->res_type, 1);
97                 f_print(fout, " *result_%d;\n", ++i);
98                 /* print out declarations for arguments */
99                 if (proc->arg_num < 2 && !newstyle) {
100                         f_print(fout, "\t");
101                         if (!streq(proc->args.decls->decl.type, "void"))
102                                 ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
103                         else
104                                 f_print(fout, "char* ");        /* cannot have "void" type */
105                         f_print(fout, " ");
106                         pvname(proc->proc_name, vp->vers_num);
107                         f_print(fout, "_arg;\n");
108                 } else if (!streq(proc->args.decls->decl.type, "void")) {
109                         for (l = proc->args.decls; l != NULL; l = l->next) {
110                                 f_print(fout, "\t");
111                                 ptype(l->decl.prefix, l->decl.type, 1);
112                                 f_print(fout, " ");
113                                 pvname(proc->proc_name, vp->vers_num);
114                                 f_print(fout, "_%s;\n", l->decl.name);
115                                 /*        pdeclaration(proc->args.argname, &l->decl, 1, ";\n" );*/
116                         }
117                 }
118         }
119
120         /* generate creation of client handle */
121         f_print(fout, "\tclnt = clnt_create(host, %s, %s, \"%s\");\n",
122                 program_name, vp->vers_name, tirpcflag ? "netpath" : "udp");
123         f_print(fout, "\tif (clnt == NULL) {\n");
124         f_print(fout, "\t\tclnt_pcreateerror(host);\n");
125         f_print(fout, "\t\texit(1);\n\t}\n");
126
127         /* generate calls to procedures */
128         i = 0;
129         for (proc = vp->procs; proc != NULL; proc = proc->next) {
130                 f_print(fout, "\tresult_%d = ", ++i);
131                 pvname(proc->proc_name, vp->vers_num);
132                 if (proc->arg_num < 2 && !newstyle) {
133                         f_print(fout, "(");
134                         if (streq(proc->args.decls->decl.type, "void")) /* cast to void* */
135                                 f_print(fout, "(void*)");
136                         f_print(fout, "&");
137                         pvname(proc->proc_name, vp->vers_num);
138                         f_print(fout, "_arg, clnt);\n");
139                 } else if (streq(proc->args.decls->decl.type, "void")) {
140                         f_print(fout, "(clnt);\n");
141                 } else {
142                         f_print(fout, "(");
143                         for (l = proc->args.decls; l != NULL; l = l->next) {
144                                 pvname(proc->proc_name, vp->vers_num);
145                                 f_print(fout, "_%s, ", l->decl.name);
146                         }
147                         f_print(fout, "clnt);\n");
148                 }
149                 f_print(fout, "\tif (result_%d == NULL) {\n", i);
150                 f_print(fout, "\t\tclnt_perror(clnt, \"call failed:\");\n");
151                 f_print(fout, "\t}\n");
152         }
153
154         f_print(fout, "\tclnt_destroy( clnt );\n");
155         f_print(fout, "}\n");
156 }
157
158 static void
159 write_sample_server(definition * def)
160 {
161         version_list   *vp;
162         proc_list      *proc;
163
164         for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
165                 for (proc = vp->procs; proc != NULL; proc = proc->next) {
166                         f_print(fout, "\n");
167                         /*                      if( Cflag )
168                           f_print( fout, "extern \"C\"{\n");
169 */
170                         return_type(proc);
171                         f_print(fout, "* \n");
172                         if (Cflag)
173                                 pvname_svc(proc->proc_name, vp->vers_num);
174                         else
175                                 pvname(proc->proc_name, vp->vers_num);
176                         printarglist(proc, RQSTP, "struct svc_req *");
177
178                         f_print(fout, "{\n");
179                         f_print(fout, "\n\tstatic ");
180                         if (!streq(proc->res_type, "void"))
181                                 return_type(proc);
182                         else
183                                 f_print(fout, "char*"); /* cannot have void type */
184                         /* f_print(fout, " result;\n", proc->res_type); */
185                         f_print(fout, " result;\n");
186                         f_print(fout,
187                                 "\n\t/*\n\t * insert server code here\n\t */\n\n");
188                         if (!streq(proc->res_type, "void"))
189                                 f_print(fout, "\treturn(&result);\n}\n");
190                         else    /* cast back to void * */
191                                 f_print(fout, "\treturn((void*) &result);\n}\n");
192                         /*                      if( Cflag)
193                           f_print( fout, "};\n");
194 */
195
196                 }
197         }
198 }
199
200
201
202 static void
203 return_type(proc_list *plist)
204 {
205         ptype( plist->res_prefix, plist->res_type, 1 );
206 }
207
208 void
209 add_sample_msg(void)
210 {
211         f_print(fout, "/*\n");
212         f_print(fout, " * This is sample code generated by rpcgen.\n");
213         f_print(fout, " * These are only templates and you can use them\n");
214         f_print(fout, " * as a guideline for developing your own functions.\n");
215         f_print(fout, " */\n\n");
216 }
217
218 void
219 write_sample_clnt_main(void)
220 {
221   list *l;
222   definition *def;
223   version_list *vp;
224
225   f_print(fout, "\n\n" );
226   if( Cflag )
227     f_print(fout,"main( int argc, char* argv[] )\n{\n" );
228   else
229     f_print(fout, "main(argc, argv)\nint argc;\nchar *argv[];\n{\n" );
230
231   f_print(fout, "\tchar *host;");
232   f_print(fout, "\n\n\tif(argc < 2) {");
233   f_print(fout, "\n\t\tprintf(\"usage: %%s server_host\\n\", argv[0]);\n" );
234   f_print(fout, "\t\texit(1);\n\t}");
235   f_print(fout, "\n\thost = argv[1];\n");
236
237   for (l = defined; l != NULL; l = l->next) {
238                 def = l->val;
239                 if (def->def_kind != DEF_PROGRAM) {
240                         continue;
241                 }
242                 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
243                         f_print( fout, "\t" );
244                         pvname(def->def_name, vp->vers_num);
245                         f_print( fout, "( host );\n" );
246                       }
247                 }
248   f_print(fout, "}\n");
249 }