1 /* $NetBSD: rpc_clntout.c,v 1.14 2013/08/11 08:03:10 dholland Exp $ */ 2 /* 3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 4 * unrestricted use provided that this legend is included on all tape 5 * media and as a part of the software program in whole or part. Users 6 * may copy or modify Sun RPC without charge, but are not authorized 7 * to license or distribute it to anyone else except as part of a product or 8 * program developed by the user or with the express written consent of 9 * Sun Microsystems, Inc. 10 * 11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 14 * 15 * Sun RPC is provided with no support and without any obligation on the 16 * part of Sun Microsystems, Inc. to assist in its use, correction, 17 * modification or enhancement. 18 * 19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 21 * OR ANY PART THEREOF. 22 * 23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 24 * or profits or other special, indirect and consequential damages, even if 25 * Sun has been advised of the possibility of such damages. 26 * 27 * Sun Microsystems, Inc. 28 * 2550 Garcia Avenue 29 * Mountain View, California 94043 30 */ 31 32 #if HAVE_NBTOOL_CONFIG_H 33 #include "nbtool_config.h" 34 #endif 35 36 #include <sys/cdefs.h> 37 #if defined(__RCSID) && !defined(lint) 38 #if 0 39 static char sccsid[] = "@(#)rpc_clntout.c 1.11 89/02/22 (C) 1987 SMI"; 40 #else 41 __RCSID("$NetBSD: rpc_clntout.c,v 1.14 2013/08/11 08:03:10 dholland Exp $"); 42 #endif 43 #endif 44 45 /* 46 * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler 47 * Copyright (C) 1987, Sun Microsytsems, Inc. 48 */ 49 #include <stdio.h> 50 #include <string.h> 51 #include <rpc/types.h> 52 #include "rpc_scan.h" 53 #include "rpc_parse.h" 54 #include "rpc_util.h" 55 56 static void write_program __P((definition *)); 57 static const char *ampr __P((const char *)); 58 static const char *aster __P((const char *)); 59 static void printbody __P((proc_list *)); 60 61 #define DEFAULT_TIMEOUT 25 /* in seconds */ 62 static char RESULT[] = "clnt_res"; 63 64 65 void 66 write_stubs(void) 67 { 68 list *l; 69 definition *def; 70 71 f_print(fout, 72 "\n/* Default timeout can be changed using clnt_control() */\n"); 73 f_print(fout, "static struct timeval TIMEOUT = { %d, 0 };\n", 74 DEFAULT_TIMEOUT); 75 for (l = defined; l != NULL; l = l->next) { 76 def = (definition *) l->val; 77 if (def->def_kind == DEF_PROGRAM) { 78 write_program(def); 79 } 80 } 81 } 82 83 static void 84 write_program(definition *def) 85 { 86 version_list *vp; 87 proc_list *proc; 88 89 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 90 for (proc = vp->procs; proc != NULL; proc = proc->next) { 91 f_print(fout, "\n"); 92 if (Mflag) 93 f_print(fout, "enum clnt_stat\n"); 94 else { 95 ptype(proc->res_prefix, proc->res_type, 1); 96 f_print(fout, "*\n"); 97 } 98 pvname(proc->proc_name, vp->vers_num); 99 printarglist(proc, RESULT, "clnt", "CLIENT *"); 100 f_print(fout, "{\n"); 101 printbody(proc); 102 f_print(fout, "}\n"); 103 } 104 } 105 } 106 /* Writes out declarations of procedure's argument list. 107 In either ANSI C style, in one of old rpcgen style (pass by reference), 108 or new rpcgen style (multiple arguments, pass by value); 109 */ 110 111 /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */ 112 113 void 114 printarglist(proc_list *proc, const char *result, 115 const char *addargname, const char *addargtype) 116 { 117 118 decl_list *l; 119 120 if (!newstyle) { /* old style: always pass argument by 121 * reference */ 122 if (Cflag) { /* C++ style heading */ 123 f_print(fout, "("); 124 ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1); 125 f_print(fout, "*argp, "); 126 if (Mflag) { 127 if (streq(proc->res_type, "void")) 128 f_print(fout, "char "); 129 else 130 ptype(proc->res_prefix, proc->res_type, 0); 131 f_print(fout, "%s%s, ", aster(proc->res_type), 132 result); 133 } 134 f_print(fout, "%s%s)\n", addargtype, addargname); 135 } else { 136 f_print(fout, "(argp, "); 137 if (Mflag) 138 f_print(fout, "%s, ", result); 139 f_print(fout, "%s)\n", addargname); 140 f_print(fout, "\t"); 141 ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1); 142 f_print(fout, "*argp;\n"); 143 if (Mflag) { 144 f_print(fout, "\t"); 145 if (streq(proc->res_type, "void")) 146 f_print(fout, "char "); 147 else 148 ptype(proc->res_prefix, proc->res_type, 0); 149 f_print(fout, "%s%s;\n", aster(proc->res_type), 150 result); 151 } 152 } 153 } else { 154 f_print(fout, "("); 155 if (!streq(proc->args.decls->decl.type, "void")) { 156 /* new style, 1 or multiple arguments */ 157 if (!Cflag) { 158 for (l = proc->args.decls; l != NULL; 159 l = l->next) 160 f_print(fout, "%s, ", l->decl.name); 161 } else {/* C++ style header */ 162 for (l = proc->args.decls; l != NULL; 163 l = l->next) 164 pdeclaration(proc->args.argname, 165 &l->decl, 0, ", "); 166 } 167 } 168 if (!Cflag) { 169 if (Mflag) { 170 f_print(fout, "\t"); 171 if (streq(proc->res_type, "void")) 172 f_print(fout, "char "); 173 else 174 ptype(proc->res_prefix, proc->res_type, 0); 175 f_print(fout, "%s%s;\n", aster(proc->res_type), 176 result); 177 } 178 f_print(fout, "%s)\n", addargname); 179 if (!streq(proc->args.decls->decl.type, "void")) { 180 for (l = proc->args.decls; l != NULL; 181 l = l->next) 182 pdeclaration(proc->args.argname, 183 &l->decl, 1, ";\n"); 184 } 185 } else { 186 if (Mflag) { 187 if (streq(proc->res_type, "void")) 188 f_print(fout, "char "); 189 else 190 ptype(proc->res_prefix, proc->res_type, 0); 191 f_print(fout, "%s%s, ", aster(proc->res_type), 192 result); 193 } 194 f_print(fout, "%s%s)\n", addargtype, addargname); 195 } 196 } 197 198 if (!Cflag) 199 f_print(fout, "\t%s%s;\n", addargtype, addargname); 200 } 201 202 203 static const char * 204 ampr(const char *type) 205 { 206 if (isvectordef(type, REL_ALIAS)) { 207 return (""); 208 } else { 209 return ("&"); 210 } 211 } 212 213 static const char * 214 aster(const char *type) 215 { 216 if (isvectordef(type, REL_ALIAS)) { 217 return (""); 218 } else { 219 return ("*"); 220 } 221 } 222 223 static void 224 printbody(proc_list *proc) 225 { 226 decl_list *l; 227 bool_t args2 = (proc->arg_num > 1); 228 229 /* For new style with multiple arguments, need a structure in which to 230 * stuff the arguments. */ 231 if (newstyle && args2) { 232 f_print(fout, "\t%s", proc->args.argname); 233 f_print(fout, " arg;\n"); 234 } 235 if (!Mflag) { 236 f_print(fout, "\tstatic "); 237 if (streq(proc->res_type, "void")) 238 f_print(fout, "char "); 239 else 240 ptype(proc->res_prefix, proc->res_type, 0); 241 f_print(fout, "%s;\n", RESULT); 242 } 243 f_print(fout, "\n"); 244 if (!Mflag) 245 f_print(fout, "\tmemset((char *)%s%s, 0, sizeof(%s));\n", 246 ampr(proc->res_type), RESULT, RESULT); 247 if (newstyle && !args2 && (streq(proc->args.decls->decl.type, "void"))) { 248 /* newstyle, 0 arguments */ 249 if (Mflag) { 250 f_print(fout, "\treturn (clnt_call(clnt, %s, xdr_void", 251 proc->proc_name); 252 f_print(fout, ", NULL, xdr_%s, %s, TIMEOUT));\n", 253 stringfix(proc->res_type), RESULT); 254 } else { 255 f_print(fout, "\tif (clnt_call(clnt, %s, xdr_void, ", 256 proc->proc_name); 257 f_print(fout, 258 "NULL, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS)\n", 259 stringfix(proc->res_type), ampr(proc->res_type), 260 RESULT); 261 } 262 } else { 263 if (newstyle && args2) { 264 /* newstyle, multiple arguments: stuff arguments into 265 * structure */ 266 for (l = proc->args.decls; l != NULL; l = l->next) { 267 f_print(fout, "\targ.%s = %s;\n", 268 l->decl.name, l->decl.name); 269 } 270 if (Mflag) { 271 f_print(fout, 272 "\treturn (clnt_call(clnt, %s, xdr_%s, &arg, xdr_%s, %s, TIMEOUT));\n", 273 proc->proc_name, proc->args.argname, 274 stringfix(proc->res_type), RESULT); 275 } else { 276 f_print(fout, 277 "\tif (clnt_call(clnt, %s, xdr_%s, &arg, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS)\n", 278 proc->proc_name, proc->args.argname, 279 stringfix(proc->res_type), 280 ampr(proc->res_type), RESULT); 281 } 282 } else { /* single argument, new or old style */ 283 if (Mflag) { 284 f_print(fout, 285 "\treturn (clnt_call(clnt, %s, xdr_%s, %s%s, xdr_%s, %s, TIMEOUT));\n", 286 proc->proc_name, 287 stringfix(proc->args.decls->decl.type), 288 (newstyle ? "&" : ""), 289 (newstyle ? proc->args.decls->decl.name : "argp"), 290 stringfix(proc->res_type), RESULT); 291 } else { 292 f_print(fout, 293 "\tif (clnt_call(clnt, %s, xdr_%s, %s%s, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS)\n", 294 proc->proc_name, 295 stringfix(proc->args.decls->decl.type), 296 (newstyle ? "&" : ""), 297 (newstyle ? proc->args.decls->decl.name : "argp"), 298 stringfix(proc->res_type), 299 ampr(proc->res_type), RESULT); 300 } 301 } 302 } 303 if (!Mflag) { 304 f_print(fout, "\t\treturn (NULL);\n"); 305 if (streq(proc->res_type, "void")) 306 f_print(fout, "\treturn ((void *)%s%s);\n", 307 ampr(proc->res_type), RESULT); 308 else 309 f_print(fout, "\treturn (%s%s);\n", 310 ampr(proc->res_type), RESULT); 311 } 312 } 313