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