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 /* 31 * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler 32 * Copyright (C) 1987, Sun Microsytsems, Inc. 33 */ 34 #include <stdio.h> 35 #include <string.h> 36 #include <rpc/types.h> 37 #include "rpc_parse.h" 38 #include "rpc_scan.h" 39 #include "rpc_util.h" 40 41 static void write_program( definition * ); 42 static void printbody( proc_list * ); 43 44 static char RESULT[] = "clnt_res"; 45 46 47 #define DEFAULT_TIMEOUT 25 /* in seconds */ 48 49 50 void 51 write_stubs(void) 52 { 53 list *l; 54 definition *def; 55 56 f_print(fout, 57 "\n/* Default timeout can be changed using clnt_control() */\n"); 58 f_print(fout, "static struct timeval TIMEOUT = { %d, 0 };\n", 59 DEFAULT_TIMEOUT); 60 for (l = defined; l != NULL; l = l->next) { 61 def = (definition *) l->val; 62 if (def->def_kind == DEF_PROGRAM) { 63 write_program(def); 64 } 65 } 66 } 67 68 static void 69 write_program(definition *def) 70 { 71 version_list *vp; 72 proc_list *proc; 73 74 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 75 for (proc = vp->procs; proc != NULL; proc = proc->next) { 76 f_print(fout, "\n"); 77 if (mtflag == 0) { 78 ptype(proc->res_prefix, proc->res_type, 1); 79 f_print(fout, "*\n"); 80 pvname(proc->proc_name, vp->vers_num); 81 printarglist(proc, RESULT, "clnt", "CLIENT *"); 82 } else { 83 f_print(fout, "enum clnt_stat \n"); 84 pvname(proc->proc_name, vp->vers_num); 85 printarglist(proc, RESULT, "clnt", "CLIENT *"); 86 87 } 88 f_print(fout, "{\n"); 89 printbody(proc); 90 91 f_print(fout, "}\n"); 92 } 93 } 94 } 95 96 /* 97 * Writes out declarations of procedure's argument list. 98 * In either ANSI C style, in one of old rpcgen style (pass by reference), 99 * or new rpcgen style (multiple arguments, pass by value); 100 */ 101 102 /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */ 103 104 void 105 printarglist(proc_list *proc, const char *result, const char *addargname, 106 const char *addargtype) 107 { 108 109 decl_list *l; 110 111 if (!newstyle) { 112 /* old style: always pass argument by reference */ 113 f_print(fout, "("); 114 ptype(proc->args.decls->decl.prefix, 115 proc->args.decls->decl.type, 1); 116 117 if (mtflag) {/* Generate result field */ 118 f_print(fout, "*argp, "); 119 ptype(proc->res_prefix, proc->res_type, 1); 120 f_print(fout, "*%s, %s%s)\n", 121 result, addargtype, addargname); 122 } else 123 f_print(fout, "*argp, %s%s)\n", addargtype, addargname); 124 } else if (streq(proc->args.decls->decl.type, "void")) { 125 /* newstyle, 0 argument */ 126 if (mtflag) { 127 f_print(fout, "("); 128 ptype(proc->res_prefix, proc->res_type, 1); 129 f_print(fout, "*%s, %s%s)\n", 130 result, addargtype, addargname); 131 } else 132 f_print(fout, "(%s%s)\n", addargtype, addargname); 133 } else { 134 /* new style, 1 or multiple arguments */ 135 f_print(fout, "("); 136 for (l = proc->args.decls; l != NULL; l = l->next) { 137 pdeclaration(proc->args.argname, &l->decl, 0, ", "); 138 } 139 if (mtflag) { 140 ptype(proc->res_prefix, proc->res_type, 1); 141 f_print(fout, "*%s, ", result); 142 143 } 144 f_print(fout, "%s%s)\n", addargtype, addargname); 145 } 146 } 147 148 149 150 static const char * 151 ampr(const char *type) 152 { 153 if (isvectordef(type, REL_ALIAS)) { 154 return (""); 155 } else { 156 return ("&"); 157 } 158 } 159 160 static void 161 printbody(proc_list *proc) 162 { 163 decl_list *l; 164 bool_t args2 = (proc->arg_num > 1); 165 166 /* 167 * For new style with multiple arguments, need a structure in which 168 * to stuff the arguments. 169 */ 170 171 172 if (newstyle && args2) { 173 f_print(fout, "\t%s", proc->args.argname); 174 f_print(fout, " arg;\n"); 175 } 176 if (!mtflag) { 177 f_print(fout, "\tstatic "); 178 if (streq(proc->res_type, "void")) { 179 f_print(fout, "char "); 180 } else { 181 ptype(proc->res_prefix, proc->res_type, 0); 182 } 183 f_print(fout, "%s;\n", RESULT); 184 f_print(fout, "\n"); 185 f_print(fout, "\tmemset((char *)%s%s, 0, sizeof (%s));\n", 186 ampr(proc->res_type), RESULT, RESULT); 187 188 } 189 if (newstyle && !args2 && 190 (streq(proc->args.decls->decl.type, "void"))) { 191 /* newstyle, 0 arguments */ 192 193 if (mtflag) 194 f_print(fout, "\t return "); 195 else 196 f_print(fout, "\t if "); 197 198 f_print(fout, 199 "(clnt_call(clnt, %s,\n\t\t(xdrproc_t) xdr_void, ", 200 proc->proc_name); 201 f_print(fout, 202 "(caddr_t) NULL,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,", 203 stringfix(proc->res_type), (mtflag)?"":ampr(proc->res_type), 204 RESULT); 205 206 if (mtflag) 207 f_print(fout, "\n\t\tTIMEOUT));\n"); 208 else 209 f_print(fout, "\n\t\tTIMEOUT) != RPC_SUCCESS) {\n"); 210 211 } else if (newstyle && args2) { 212 /* 213 * Newstyle, multiple arguments 214 * stuff arguments into structure 215 */ 216 for (l = proc->args.decls; l != NULL; l = l->next) { 217 f_print(fout, "\targ.%s = %s;\n", 218 l->decl.name, l->decl.name); 219 } 220 if (mtflag) 221 f_print(fout, "\treturn "); 222 else 223 f_print(fout, "\tif "); 224 f_print(fout, 225 "(clnt_call(clnt, %s,\n\t\t(xdrproc_t) xdr_%s", 226 proc->proc_name,proc->args.argname); 227 f_print(fout, 228 ", (caddr_t) &arg,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,", 229 stringfix(proc->res_type), (mtflag)?"":ampr(proc->res_type), 230 RESULT); 231 if (mtflag) 232 f_print(fout, "\n\t\tTIMEOUT));\n"); 233 else 234 f_print(fout, "\n\t\tTIMEOUT) != RPC_SUCCESS) {\n"); 235 } else { /* single argument, new or old style */ 236 if (!mtflag) 237 f_print(fout, 238 "\tif (clnt_call(clnt, %s,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n\t\tTIMEOUT) != RPC_SUCCESS) {\n", 239 proc->proc_name, 240 stringfix(proc->args.decls->decl.type), 241 (newstyle ? "&" : ""), 242 (newstyle ? proc->args.decls->decl.name : "argp"), 243 stringfix(proc->res_type), ampr(proc->res_type), 244 RESULT); 245 else 246 247 f_print(fout, 248 "\treturn (clnt_call(clnt, %s,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n\t\tTIMEOUT));\n", 249 proc->proc_name, 250 stringfix(proc->args.decls->decl.type), 251 (newstyle ? "&" : ""), 252 (newstyle ? proc->args.decls->decl.name : "argp"), 253 stringfix(proc->res_type), "", 254 RESULT); 255 } 256 if (!mtflag) { 257 f_print(fout, "\t\treturn (NULL);\n"); 258 f_print(fout, "\t}\n"); 259 260 if (streq(proc->res_type, "void")) { 261 f_print(fout, "\treturn ((void *)%s%s);\n", 262 ampr(proc->res_type), RESULT); 263 } else { 264 f_print(fout, "\treturn (%s%s);\n", 265 ampr(proc->res_type), RESULT); 266 } 267 } 268 } 269