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