1 /* 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. 8 * 9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 12 * 13 * Sun RPC is provided with no support and without any obligation on the 14 * part of Sun Microsystems, Inc. to assist in its use, correction, 15 * modification or enhancement. 16 * 17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 19 * OR ANY PART THEREOF. 20 * 21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 22 * or profits or other special, indirect and consequential damages, even if 23 * Sun has been advised of the possibility of such damages. 24 * 25 * Sun Microsystems, Inc. 26 * 2550 Garcia Avenue 27 * Mountain View, California 94043 28 * 29 * $FreeBSD$ 30 */ 31 32 #pragma ident "@(#)rpc_sample.c 1.9 94/04/25 SMI" 33 34 /* 35 * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler 36 * Copyright (C) 1987, Sun Microsystems, Inc. 37 */ 38 39 #include <stdio.h> 40 #include <string.h> 41 #include "rpc_parse.h" 42 #include "rpc_util.h" 43 44 45 static char RQSTP[] = "rqstp"; 46 47 extern void printarglist( proc_list *, char *, char *, char *); 48 static void write_sample_client( char *, version_list * ); 49 static void write_sample_server( definition * ); 50 static void return_type( proc_list * ); 51 52 void 53 write_sample_svc(def) 54 definition *def; 55 { 56 57 if (def->def_kind != DEF_PROGRAM) 58 return; 59 write_sample_server(def); 60 } 61 62 63 int 64 write_sample_clnt(def) 65 definition *def; 66 { 67 version_list *vp; 68 int count = 0; 69 70 if (def->def_kind != DEF_PROGRAM) 71 return(0); 72 /* generate sample code for each version */ 73 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 74 write_sample_client(def->def_name, vp); 75 ++count; 76 } 77 return(count); 78 } 79 80 81 static void 82 write_sample_client(program_name, vp) 83 char *program_name; 84 version_list *vp; 85 { 86 proc_list *proc; 87 int i; 88 decl_list *l; 89 90 f_print(fout, "\n\nvoid\n"); 91 pvname(program_name, vp->vers_num); 92 if(Cflag) 93 f_print(fout,"(char *host)\n{\n"); 94 else 95 f_print(fout, "(host)\n\tchar *host;\n{\n"); 96 f_print(fout, "\tCLIENT *clnt;\n"); 97 98 i = 0; 99 for (proc = vp->procs; proc != NULL; proc = proc->next) { 100 f_print(fout, "\t"); 101 if (mtflag) { 102 f_print(fout, "enum clnt_stat retval_%d;\n\t", ++i); 103 ptype(proc->res_prefix, proc->res_type, 1); 104 f_print(fout, "result_%d;\n", i); 105 } else { 106 ptype(proc->res_prefix, proc->res_type, 1); 107 f_print(fout, " *result_%d;\n",++i); 108 } 109 /* print out declarations for arguments */ 110 if(proc->arg_num < 2 && !newstyle) { 111 f_print(fout, "\t"); 112 if(!streq(proc->args.decls->decl.type, "void")) 113 ptype(proc->args.decls->decl.prefix, 114 proc->args.decls->decl.type, 1); 115 else 116 f_print(fout, "char * "); /* cannot have "void" type */ 117 f_print(fout, " "); 118 pvname(proc->proc_name, vp->vers_num); 119 f_print(fout, "_arg;\n"); 120 } else if (!streq(proc->args.decls->decl.type, "void")) { 121 for (l = proc->args.decls; l != NULL; l = l->next) { 122 f_print(fout, "\t"); 123 ptype(l->decl.prefix, l->decl.type, 1); 124 if (strcmp(l->decl.type,"string") == 1) 125 f_print(fout, " "); 126 pvname(proc->proc_name, vp->vers_num); 127 f_print(fout, "_%s;\n", l->decl.name); 128 } 129 } 130 } 131 132 /* generate creation of client handle */ 133 f_print(fout, "\n#ifndef\tDEBUG\n"); 134 f_print(fout, "\tclnt = clnt_create(host, %s, %s, \"%s\");\n", 135 program_name, vp->vers_name, tirpcflag? "netpath" : "udp"); 136 f_print(fout, "\tif (clnt == (CLIENT *) NULL) {\n"); 137 f_print(fout, "\t\tclnt_pcreateerror(host);\n"); 138 f_print(fout, "\t\texit(1);\n\t}\n"); 139 f_print(fout, "#endif\t/* DEBUG */\n\n"); 140 141 /* generate calls to procedures */ 142 i = 0; 143 for (proc = vp->procs; proc != NULL; proc = proc->next) { 144 if (mtflag) 145 f_print(fout, "\tretval_%d = ",++i); 146 else 147 f_print(fout, "\tresult_%d = ",++i); 148 pvname(proc->proc_name, vp->vers_num); 149 if (proc->arg_num < 2 && !newstyle) { 150 f_print(fout, "("); 151 if(streq(proc->args.decls->decl.type, "void")) 152 /* cast to void * */ 153 f_print(fout, "(void *)"); 154 f_print(fout, "&"); 155 pvname(proc->proc_name, vp->vers_num); 156 if (mtflag) 157 f_print(fout, "_arg, &result_%d, clnt);\n", 158 i); 159 else 160 f_print(fout, "_arg, clnt);\n"); 161 162 } else if (streq(proc->args.decls->decl.type, "void")) { 163 if (mtflag) 164 f_print(fout, "(&result_%d, clnt);\n", i); 165 else 166 f_print(fout, "(clnt);\n"); 167 } 168 else { 169 f_print(fout, "("); 170 for (l = proc->args.decls; l != NULL; l = l->next) { 171 pvname(proc->proc_name, vp->vers_num); 172 f_print(fout, "_%s, ", l->decl.name); 173 } 174 if (mtflag) 175 f_print(fout, "&result_%d, ", i); 176 177 f_print(fout, "clnt);\n"); 178 } 179 if (mtflag) { 180 f_print(fout, "\tif (retval_%d != RPC_SUCCESS) {\n", i); 181 182 } else { 183 f_print(fout, "\tif (result_%d == (", i); 184 ptype(proc->res_prefix, proc->res_type, 1); 185 f_print(fout, "*) NULL) {\n"); 186 } 187 f_print(fout, "\t\tclnt_perror(clnt, \"call failed\");\n"); 188 f_print(fout, "\t}\n"); 189 } 190 191 f_print(fout, "#ifndef\tDEBUG\n"); 192 f_print(fout, "\tclnt_destroy(clnt);\n"); 193 f_print(fout, "#endif\t /* DEBUG */\n"); 194 f_print(fout, "}\n"); 195 } 196 197 static void 198 write_sample_server(def) 199 definition *def; 200 { 201 version_list *vp; 202 proc_list *proc; 203 204 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 205 for (proc = vp->procs; proc != NULL; proc = proc->next) { 206 f_print(fout, "\n"); 207 if (!mtflag) { 208 return_type(proc); 209 f_print(fout, "*\n"); 210 } else 211 f_print(fout, "bool_t\n"); 212 if (Cflag || mtflag) 213 pvname_svc(proc->proc_name, vp->vers_num); 214 else 215 pvname(proc->proc_name, vp->vers_num); 216 printarglist(proc, "result", RQSTP, "struct svc_req *"); 217 218 f_print(fout, "{\n"); 219 if (!mtflag) { 220 f_print(fout, "\tstatic "); 221 if(!streq(proc->res_type, "void")) 222 return_type(proc); 223 else 224 f_print(fout, "char *"); 225 /* cannot have void type */ 226 f_print(fout, " result;\n"); 227 } 228 else 229 f_print(fout, "\tbool_t retval;\n"); 230 f_print(fout, 231 "\n\t/*\n\t * insert server code here\n\t */\n\n"); 232 233 if (!mtflag) 234 if(!streq(proc->res_type, "void")) 235 f_print(fout, "\treturn (&result);\n}\n"); 236 else /* cast back to void * */ 237 f_print(fout, "\treturn((void *) &result);\n}\n"); 238 else 239 f_print(fout, "\treturn (retval);\n}\n"); 240 } 241 /* put in sample freeing routine */ 242 if (mtflag) { 243 f_print(fout, "\nint\n"); 244 pvname(def->def_name, vp->vers_num); 245 if (Cflag) 246 f_print(fout,"_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)\n"); 247 else { 248 f_print(fout,"_freeresult(transp, xdr_result, result)\n"); 249 f_print(fout,"\tSVCXPRT *transp;\n"); 250 f_print(fout,"\txdrproc_t xdr_result;\n"); 251 f_print(fout,"\tcaddr_t result;\n"); 252 } 253 f_print(fout, "{\n"); 254 f_print(fout, "\t(void) xdr_free(xdr_result, result);\n"); 255 f_print(fout, 256 "\n\t/*\n\t * Insert additional freeing code here, if needed\n\t */\n"); 257 f_print(fout, "\n}\n"); 258 259 260 } 261 } 262 } 263 264 265 266 static void 267 return_type(plist) 268 proc_list *plist; 269 { 270 ptype(plist->res_prefix, plist->res_type, 1); 271 } 272 273 void 274 add_sample_msg() 275 { 276 f_print(fout, "/*\n"); 277 f_print(fout, " * This is sample code generated by rpcgen.\n"); 278 f_print(fout, " * These are only templates and you can use them\n"); 279 f_print(fout, " * as a guideline for developing your own functions.\n"); 280 f_print(fout, " */\n\n"); 281 } 282 283 void 284 write_sample_clnt_main() 285 { 286 list *l; 287 definition *def; 288 version_list *vp; 289 290 f_print(fout, "\n\n"); 291 if(Cflag) 292 f_print(fout,"main(int argc, char *argv[])\n{\n"); 293 else 294 f_print(fout, "main(argc, argv)\n\tint argc;\n\tchar *argv[];\n{\n"); 295 296 f_print(fout, "\tchar *host;"); 297 f_print(fout, "\n\n\tif (argc < 2) {"); 298 f_print(fout, "\n\t\tprintf(\"usage: %%s server_host\\n\", argv[0]);\n"); 299 f_print(fout, "\t\texit(1);\n\t}"); 300 f_print(fout, "\n\thost = argv[1];\n"); 301 302 for (l = defined; l != NULL; l = l->next) { 303 def = l->val; 304 if (def->def_kind != DEF_PROGRAM) { 305 continue; 306 } 307 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 308 f_print(fout, "\t"); 309 pvname(def->def_name, vp->vers_num); 310 f_print(fout, "(host);\n"); 311 } 312 } 313 f_print(fout, "}\n"); 314 } 315