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