2 * Copyright (c) 2009, Sun Microsystems, Inc.
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.
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.
30 static char sccsid[] = "@(#)rpc_sample.c 1.1 90/08/30 (C) 1987 SMI";
35 * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
40 #include "rpc_parse.h"
44 static char RQSTP[] = "rqstp";
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);
51 write_sample_svc(definition *def)
53 if (def->def_kind != DEF_PROGRAM)
55 write_sample_server(def);
60 write_sample_clnt(definition *def)
65 if (def->def_kind != DEF_PROGRAM)
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);
77 write_sample_client(char *program_name, version_list *vp)
83 f_print(fout, "\n\nvoid\n");
84 pvname(program_name, vp->vers_num);
86 f_print(fout, "( char* host )\n{\n");
88 f_print(fout, "(host)\nchar *host;\n{\n");
89 f_print(fout, "\tCLIENT *clnt;\n");
92 for (proc = vp->procs; proc != NULL; proc = proc->next) {
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) {
99 if (!streq(proc->args.decls->decl.type, "void"))
100 ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
102 f_print(fout, "char* "); /* cannot have "void" type */
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) {
109 ptype(l->decl.prefix, l->decl.type, 1);
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" );*/
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");
125 /* generate calls to procedures */
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) {
132 if (streq(proc->args.decls->decl.type, "void")) /* cast to void* */
133 f_print(fout, "(void*)");
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");
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);
145 f_print(fout, "clnt);\n");
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");
152 f_print(fout, "\tclnt_destroy( clnt );\n");
153 f_print(fout, "}\n");
157 write_sample_server(definition * def)
162 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
163 for (proc = vp->procs; proc != NULL; proc = proc->next) {
166 f_print( fout, "extern \"C\"{\n");
169 f_print(fout, "* \n");
171 pvname_svc(proc->proc_name, vp->vers_num);
173 pvname(proc->proc_name, vp->vers_num);
174 printarglist(proc, RQSTP, "struct svc_req *");
176 f_print(fout, "{\n");
177 f_print(fout, "\n\tstatic ");
178 if (!streq(proc->res_type, "void"))
181 f_print(fout, "char*"); /* cannot have void type */
182 /* f_print(fout, " result;\n", proc->res_type); */
183 f_print(fout, " result;\n");
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");
191 f_print( fout, "};\n");
201 return_type(proc_list *plist)
203 ptype( plist->res_prefix, plist->res_type, 1 );
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");
217 write_sample_clnt_main(void)
223 f_print(fout, "\n\n" );
225 f_print(fout,"main( int argc, char* argv[] )\n{\n" );
227 f_print(fout, "main(argc, argv)\nint argc;\nchar *argv[];\n{\n" );
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");
235 for (l = defined; l != NULL; l = l->next) {
237 if (def->def_kind != DEF_PROGRAM) {
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" );
246 f_print(fout, "}\n");