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