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 * From: @(#)rpc_sample.c 1.1 90/08/30 (C) 1987 SMI
38 * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
43 #include "rpc_parse.h"
48 static char RQSTP
[] = "rqstp";
50 static void write_sample_client(const char *program_name
, version_list
*vp
);
51 static void write_sample_server(definition
*def
);
52 static void return_type(proc_list
*plist
);
56 write_sample_svc(definition
*def
)
59 if (def
->def_kind
!= DEF_PROGRAM
)
61 write_sample_server(def
);
66 write_sample_clnt(definition
*def
)
71 if (def
->def_kind
!= DEF_PROGRAM
)
73 /* generate sample code for each version */
74 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
75 write_sample_client(def
->def_name
, vp
);
83 write_sample_client(const char *program_name
, version_list
*vp
)
89 f_print(fout
, "\n\nvoid\n" );
90 pvname( program_name
, vp
->vers_num
);
92 f_print(fout
,"( char* host )\n{\n" );
94 f_print(fout
, "(host)\nchar *host;\n{\n" );
95 f_print(fout
, "\tCLIENT *clnt;\n");
98 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
100 ptype(proc
->res_prefix
, proc
->res_type
, 1);
101 f_print(fout
, " *result_%d;\n",++i
);
102 /* print out declarations for arguments */
103 if( proc
->arg_num
< 2 && !newstyle
) {
104 f_print( fout
, "\t" );
105 if( !streq( proc
->args
.decls
->decl
.type
, "void") )
106 ptype(proc
->args
.decls
->decl
.prefix
, proc
->args
.decls
->decl
.type
, 1);
108 f_print(fout
, "char* "); /* cannot have "void" type */
110 pvname( proc
->proc_name
, vp
->vers_num
);
111 f_print(fout
, "_arg;\n");
112 } else if (!streq( proc
->args
.decls
->decl
.type
, "void")) {
113 for (l
= proc
->args
.decls
; l
!= NULL
; l
= l
->next
) {
114 f_print( fout
, "\t" );
115 ptype(l
->decl
.prefix
, l
->decl
.type
, 1);
117 pvname( proc
->proc_name
, vp
->vers_num
);
118 f_print(fout
, "_%s;\n", l
->decl
.name
);
119 /* pdeclaration(proc->args.argname, &l->decl, 1, ";\n" );*/
124 /* generate creation of client handle */
125 f_print(fout
, "\tclnt = clnt_create(host, %s, %s, \"%s\");\n",
126 program_name
, vp
->vers_name
, tirpcflag
? "netpath" : "udp");
127 f_print(fout
, "\tif (clnt == NULL) {\n");
128 f_print(fout
, "\t\tclnt_pcreateerror(host);\n");
129 f_print(fout
, "\t\texit(1);\n\t}\n");
131 /* generate calls to procedures */
133 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
134 f_print(fout
, "\tresult_%d = ",++i
);
135 pvname(proc
->proc_name
, vp
->vers_num
);
136 if (proc
->arg_num
< 2 && !newstyle
) {
138 if( streq( proc
->args
.decls
->decl
.type
, "void") ) /* cast to void* */
139 f_print(fout
, "(void*)");
141 pvname(proc
->proc_name
, vp
->vers_num
);
142 f_print(fout
, "_arg, clnt);\n");
143 } else if (streq( proc
->args
.decls
->decl
.type
, "void")) {
144 f_print(fout
, "(clnt);\n");
148 for (l
= proc
->args
.decls
; l
!= NULL
; l
= l
->next
) {
149 pvname( proc
->proc_name
, vp
->vers_num
);
150 f_print(fout
, "_%s, ", l
->decl
.name
);
152 f_print(fout
, "clnt);\n");
154 f_print(fout
, "\tif (result_%d == NULL) {\n", i
);
155 f_print(fout
, "\t\tclnt_perror(clnt, \"call failed:\");\n");
156 f_print(fout
, "\t}\n");
159 f_print(fout
, "\tclnt_destroy( clnt );\n" );
160 f_print(fout
, "}\n");
164 write_sample_server(definition
*def
)
169 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
170 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
173 f_print( fout, "extern \"C\"{\n");
176 f_print(fout
, "* \n");
178 pvname_svc(proc
->proc_name
, vp
->vers_num
);
180 pvname(proc
->proc_name
, vp
->vers_num
);
181 printarglist( proc
, RQSTP
, "struct svc_req *" );
183 f_print(fout
, "{\n");
184 f_print(fout
, "\n\tstatic ");
185 if( !streq( proc
->res_type
, "void") )
188 f_print(fout
, "char*" ); /* cannot have void type */
189 f_print(fout
, " result;\n" /*, proc->res_type*/ /*?*/);
191 "\n\t/*\n\t * insert server code here\n\t */\n\n");
192 if( !streq( proc
->res_type
, "void") )
193 f_print(fout
, "\treturn(&result);\n}\n");
194 else /* cast back to void * */
195 f_print(fout
, "\treturn((void*) &result);\n}\n");
197 f_print( fout, "};\n");
207 return_type(proc_list
*plist
)
209 ptype( plist
->res_prefix
, plist
->res_type
, 1 );
215 f_print(fout
, "/*\n");
216 f_print(fout
, " * This is sample code generated by rpcgen.\n");
217 f_print(fout
, " * These are only templates and you can use them\n");
218 f_print(fout
, " * as a guideline for developing your own functions.\n");
219 f_print(fout
, " */\n\n");
223 write_sample_clnt_main(void)
229 f_print(fout
, "\n\n" );
231 f_print(fout
,"main( int argc, char* argv[] )\n{\n" );
233 f_print(fout
, "main(argc, argv)\nint argc;\nchar *argv[];\n{\n" );
235 f_print(fout
, "\tchar *host;");
236 f_print(fout
, "\n\n\tif(argc < 2) {");
237 f_print(fout
, "\n\t\tprintf(\"usage: %%s server_host\\n\", argv[0]);\n" );
238 f_print(fout
, "\t\texit(1);\n\t}");
239 f_print(fout
, "\n\thost = argv[1];\n");
241 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
243 if (def
->def_kind
!= DEF_PROGRAM
) {
246 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
247 f_print( fout
, "\t" );
248 pvname(def
->def_name
, vp
->vers_num
);
249 f_print( fout
, "( host );\n" );
252 f_print(fout
, "}\n");