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.
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.
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.
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.
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.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
32 static char sccsid[] = "@(#)rpc_sample.c 1.1 90/08/30 (C) 1987 SMI";
37 * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
42 #include "rpc_parse.h"
46 static char RQSTP[] = "rqstp";
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);
53 write_sample_svc(definition *def)
55 if (def->def_kind != DEF_PROGRAM)
57 write_sample_server(def);
62 write_sample_clnt(definition *def)
67 if (def->def_kind != DEF_PROGRAM)
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);
79 write_sample_client(char *program_name, version_list *vp)
85 f_print(fout, "\n\nvoid\n");
86 pvname(program_name, vp->vers_num);
88 f_print(fout, "( char* host )\n{\n");
90 f_print(fout, "(host)\nchar *host;\n{\n");
91 f_print(fout, "\tCLIENT *clnt;\n");
94 for (proc = vp->procs; proc != NULL; proc = proc->next) {
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) {
101 if (!streq(proc->args.decls->decl.type, "void"))
102 ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
104 f_print(fout, "char* "); /* cannot have "void" type */
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) {
111 ptype(l->decl.prefix, l->decl.type, 1);
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" );*/
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");
127 /* generate calls to procedures */
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) {
134 if (streq(proc->args.decls->decl.type, "void")) /* cast to void* */
135 f_print(fout, "(void*)");
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");
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);
147 f_print(fout, "clnt);\n");
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");
154 f_print(fout, "\tclnt_destroy( clnt );\n");
155 f_print(fout, "}\n");
159 write_sample_server(definition * def)
164 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
165 for (proc = vp->procs; proc != NULL; proc = proc->next) {
168 f_print( fout, "extern \"C\"{\n");
171 f_print(fout, "* \n");
173 pvname_svc(proc->proc_name, vp->vers_num);
175 pvname(proc->proc_name, vp->vers_num);
176 printarglist(proc, RQSTP, "struct svc_req *");
178 f_print(fout, "{\n");
179 f_print(fout, "\n\tstatic ");
180 if (!streq(proc->res_type, "void"))
183 f_print(fout, "char*"); /* cannot have void type */
184 /* f_print(fout, " result;\n", proc->res_type); */
185 f_print(fout, " result;\n");
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");
193 f_print( fout, "};\n");
203 return_type(proc_list *plist)
205 ptype( plist->res_prefix, plist->res_type, 1 );
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");
219 write_sample_clnt_main(void)
225 f_print(fout, "\n\n" );
227 f_print(fout,"main( int argc, char* argv[] )\n{\n" );
229 f_print(fout, "main(argc, argv)\nint argc;\nchar *argv[];\n{\n" );
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");
237 for (l = defined; l != NULL; l = l->next) {
239 if (def->def_kind != DEF_PROGRAM) {
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" );
248 f_print(fout, "}\n");