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