]> git.decadent.org.uk Git - nfs-utils.git/blob - tools/rpcgen/rpc_sample.c
nfs-utils: nfs-iostat.py autofs cleanup and option to sort by ops/s
[nfs-utils.git] / tools / rpcgen / rpc_sample.c
1 /*
2  * Copyright (c) 2009, Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * - Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  *   this list of conditions and the following disclaimer in the documentation
11  *   and/or other materials provided with the distribution.
12  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13  *   contributors may be used to endorse or promote products derived
14  *   from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #if 0
30 static char sccsid[] = "@(#)rpc_sample.c  1.1  90/08/30  (C) 1987 SMI";
31
32 #endif
33
34 /*
35  * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
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 static void     write_sample_client(char *program_name, version_list *vp);
47 static void     write_sample_server(definition * def);
48 static void     return_type(proc_list *plist);
49
50 void
51 write_sample_svc(definition *def)
52 {
53         if (def->def_kind != DEF_PROGRAM)
54                 return;
55         write_sample_server(def);
56 }
57
58
59 int
60 write_sample_clnt(definition *def)
61 {
62         version_list   *vp;
63         int             count = 0;
64
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;
71         }
72         return (count);
73 }
74
75
76 static void
77 write_sample_client(char *program_name, version_list *vp)
78 {
79         proc_list      *proc;
80         int             i;
81         decl_list      *l;
82
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)\nchar *host;\n{\n");
89         f_print(fout, "\tCLIENT *clnt;\n");
90
91         i = 0;
92         for (proc = vp->procs; proc != NULL; proc = proc->next) {
93                 f_print(fout, "\t");
94                 ptype(proc->res_prefix, proc->res_type, 1);
95                 f_print(fout, " *result_%d;\n", ++i);
96                 /* print out declarations for arguments */
97                 if (proc->arg_num < 2 && !newstyle) {
98                         f_print(fout, "\t");
99                         if (!streq(proc->args.decls->decl.type, "void"))
100                                 ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
101                         else
102                                 f_print(fout, "char* ");        /* cannot have "void" type */
103                         f_print(fout, " ");
104                         pvname(proc->proc_name, vp->vers_num);
105                         f_print(fout, "_arg;\n");
106                 } else if (!streq(proc->args.decls->decl.type, "void")) {
107                         for (l = proc->args.decls; l != NULL; l = l->next) {
108                                 f_print(fout, "\t");
109                                 ptype(l->decl.prefix, l->decl.type, 1);
110                                 f_print(fout, " ");
111                                 pvname(proc->proc_name, vp->vers_num);
112                                 f_print(fout, "_%s;\n", l->decl.name);
113                                 /*        pdeclaration(proc->args.argname, &l->decl, 1, ";\n" );*/
114                         }
115                 }
116         }
117
118         /* generate creation of client handle */
119         f_print(fout, "\tclnt = clnt_create(host, %s, %s, \"%s\");\n",
120                 program_name, vp->vers_name, tirpcflag ? "netpath" : "udp");
121         f_print(fout, "\tif (clnt == NULL) {\n");
122         f_print(fout, "\t\tclnt_pcreateerror(host);\n");
123         f_print(fout, "\t\texit(1);\n\t}\n");
124
125         /* generate calls to procedures */
126         i = 0;
127         for (proc = vp->procs; proc != NULL; proc = proc->next) {
128                 f_print(fout, "\tresult_%d = ", ++i);
129                 pvname(proc->proc_name, vp->vers_num);
130                 if (proc->arg_num < 2 && !newstyle) {
131                         f_print(fout, "(");
132                         if (streq(proc->args.decls->decl.type, "void")) /* cast to void* */
133                                 f_print(fout, "(void*)");
134                         f_print(fout, "&");
135                         pvname(proc->proc_name, vp->vers_num);
136                         f_print(fout, "_arg, clnt);\n");
137                 } else if (streq(proc->args.decls->decl.type, "void")) {
138                         f_print(fout, "(clnt);\n");
139                 } else {
140                         f_print(fout, "(");
141                         for (l = proc->args.decls; l != NULL; l = l->next) {
142                                 pvname(proc->proc_name, vp->vers_num);
143                                 f_print(fout, "_%s, ", l->decl.name);
144                         }
145                         f_print(fout, "clnt);\n");
146                 }
147                 f_print(fout, "\tif (result_%d == NULL) {\n", i);
148                 f_print(fout, "\t\tclnt_perror(clnt, \"call failed:\");\n");
149                 f_print(fout, "\t}\n");
150         }
151
152         f_print(fout, "\tclnt_destroy( clnt );\n");
153         f_print(fout, "}\n");
154 }
155
156 static void
157 write_sample_server(definition * def)
158 {
159         version_list   *vp;
160         proc_list      *proc;
161
162         for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
163                 for (proc = vp->procs; proc != NULL; proc = proc->next) {
164                         f_print(fout, "\n");
165                         /*                      if( Cflag )
166                           f_print( fout, "extern \"C\"{\n");
167 */
168                         return_type(proc);
169                         f_print(fout, "* \n");
170                         if (Cflag)
171                                 pvname_svc(proc->proc_name, vp->vers_num);
172                         else
173                                 pvname(proc->proc_name, vp->vers_num);
174                         printarglist(proc, RQSTP, "struct svc_req *");
175
176                         f_print(fout, "{\n");
177                         f_print(fout, "\n\tstatic ");
178                         if (!streq(proc->res_type, "void"))
179                                 return_type(proc);
180                         else
181                                 f_print(fout, "char*"); /* cannot have void type */
182                         /* f_print(fout, " result;\n", proc->res_type); */
183                         f_print(fout, " result;\n");
184                         f_print(fout,
185                                 "\n\t/*\n\t * insert server code here\n\t */\n\n");
186                         if (!streq(proc->res_type, "void"))
187                                 f_print(fout, "\treturn(&result);\n}\n");
188                         else    /* cast back to void * */
189                                 f_print(fout, "\treturn((void*) &result);\n}\n");
190                         /*                      if( Cflag)
191                           f_print( fout, "};\n");
192 */
193
194                 }
195         }
196 }
197
198
199
200 static void
201 return_type(proc_list *plist)
202 {
203         ptype( plist->res_prefix, plist->res_type, 1 );
204 }
205
206 void
207 add_sample_msg(void)
208 {
209         f_print(fout, "/*\n");
210         f_print(fout, " * This is sample code generated by rpcgen.\n");
211         f_print(fout, " * These are only templates and you can use them\n");
212         f_print(fout, " * as a guideline for developing your own functions.\n");
213         f_print(fout, " */\n\n");
214 }
215
216 void
217 write_sample_clnt_main(void)
218 {
219   list *l;
220   definition *def;
221   version_list *vp;
222
223   f_print(fout, "\n\n" );
224   if( Cflag )
225     f_print(fout,"main( int argc, char* argv[] )\n{\n" );
226   else
227     f_print(fout, "main(argc, argv)\nint argc;\nchar *argv[];\n{\n" );
228
229   f_print(fout, "\tchar *host;");
230   f_print(fout, "\n\n\tif(argc < 2) {");
231   f_print(fout, "\n\t\tprintf(\"usage: %%s server_host\\n\", argv[0]);\n" );
232   f_print(fout, "\t\texit(1);\n\t}");
233   f_print(fout, "\n\thost = argv[1];\n");
234
235   for (l = defined; l != NULL; l = l->next) {
236                 def = l->val;
237                 if (def->def_kind != DEF_PROGRAM) {
238                         continue;
239                 }
240                 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
241                         f_print( fout, "\t" );
242                         pvname(def->def_name, vp->vers_num);
243                         f_print( fout, "( host );\n" );
244                       }
245                 }
246   f_print(fout, "}\n");
247 }