1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 * 22 * Copyright 2001 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 26 /* All Rights Reserved */ 27 /* 28 * University Copyright- Copyright (c) 1982, 1986, 1988 29 * The Regents of the University of California 30 * All Rights Reserved 31 * 32 * University Acknowledgment- Portions of this document are derived from 33 * software developed by the University of California, Berkeley, and its 34 * contributors. 35 */ 36 37 #pragma ident "%Z%%M% %I% %E% SMI" 38 39 /* 40 * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler 41 */ 42 #include <stdio.h> 43 #include <string.h> 44 #include <rpc/types.h> 45 #include "rpc_parse.h" 46 #include "rpc_util.h" 47 48 extern pdeclaration(); 49 void printarglist(); 50 51 52 static char RESULT[] = "clnt_res"; 53 54 55 #define DEFAULT_TIMEOUT 25 /* in seconds */ 56 57 58 void 59 write_stubs() 60 { 61 list *l; 62 definition *def; 63 64 f_print(fout, 65 "\n/* Default timeout can be changed using clnt_control() */\n"); 66 f_print(fout, "static struct timeval TIMEOUT = { %d, 0 };\n", 67 DEFAULT_TIMEOUT); 68 for (l = defined; l != NULL; l = l->next) { 69 def = (definition *) l->val; 70 if (def->def_kind == DEF_PROGRAM) { 71 write_program(def); 72 } 73 } 74 } 75 76 static 77 write_program(def) 78 definition *def; 79 { 80 version_list *vp; 81 proc_list *proc; 82 83 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 84 for (proc = vp->procs; proc != NULL; proc = proc->next) { 85 f_print(fout, "\n"); 86 if (mtflag == 0) { 87 ptype(proc->res_prefix, proc->res_type, 1); 88 f_print(fout, "*\n"); 89 pvname(proc->proc_name, vp->vers_num); 90 printarglist(proc, RESULT, "clnt", "CLIENT *"); 91 } else { 92 f_print(fout, "enum clnt_stat \n"); 93 pvname(proc->proc_name, vp->vers_num); 94 printarglist(proc, RESULT, "clnt", "CLIENT *"); 95 96 } 97 f_print(fout, "{\n"); 98 printbody(proc); 99 100 f_print(fout, "}\n"); 101 } 102 } 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 printarglist(proc, result, addargname, addargtype) 114 proc_list *proc; 115 char *result; 116 char* addargname, * addargtype; 117 { 118 bool_t oneway = streq(proc->res_type, "oneway"); 119 decl_list *l; 120 121 if (!newstyle) { 122 /* old style: always pass argument by reference */ 123 if (Cflag) { /* C++ style heading */ 124 f_print(fout, "("); 125 ptype(proc->args.decls->decl.prefix, 126 proc->args.decls->decl.type, 1); 127 128 if (mtflag) {/* Generate result field */ 129 f_print(fout, "*argp, "); 130 if (!oneway) { 131 ptype(proc->res_prefix, 132 proc->res_type, 1); 133 f_print(fout, "*%s, ", result); 134 } 135 f_print(fout, "%s%s)\n", 136 addargtype, addargname); 137 } else 138 f_print(fout, "*argp, %s%s)\n", addargtype, addargname); 139 } else { 140 if (!mtflag) 141 f_print(fout, "(argp, %s)\n", addargname); 142 else { 143 f_print(fout, "(argp, "); 144 if (!oneway) { 145 f_print(fout, "%s, ", 146 result); 147 } 148 f_print(fout, "%s)\n", 149 addargname); 150 } 151 f_print(fout, "\t"); 152 ptype(proc->args.decls->decl.prefix, 153 proc->args.decls->decl.type, 1); 154 f_print(fout, "*argp;\n"); 155 if (mtflag && !oneway) { 156 f_print(fout, "\t"); 157 ptype(proc->res_prefix, proc->res_type, 1); 158 f_print(fout, "*%s;\n", result); 159 } 160 } 161 } else if (streq(proc->args.decls->decl.type, "void")) { 162 /* newstyle, 0 argument */ 163 if (mtflag) { 164 f_print(fout, "("); 165 166 if (Cflag) { 167 if (!oneway) { 168 ptype(proc->res_prefix, 169 proc->res_type, 1); 170 f_print(fout, "*%s, ", result); 171 } 172 f_print(fout, "%s%s)\n", 173 addargtype, addargname); 174 } else 175 f_print(fout, "(%s)\n", addargname); 176 177 } else 178 if (Cflag) 179 f_print(fout, "(%s%s)\n", addargtype, addargname); 180 else 181 f_print(fout, "(%s)\n", addargname); 182 } else { 183 /* new style, 1 or multiple arguments */ 184 if (!Cflag) { 185 f_print(fout, "("); 186 for (l = proc->args.decls; l != NULL; l = l->next) 187 f_print(fout, "%s, ", l->decl.name); 188 if (mtflag && !oneway) 189 f_print(fout, "%s, ", result); 190 191 f_print(fout, "%s)\n", addargname); 192 for (l = proc->args.decls; l != NULL; l = l->next) { 193 pdeclaration(proc->args.argname, 194 &l->decl, 1, ";\n"); 195 } 196 if (mtflag && !oneway) { 197 f_print(fout, "\t"); 198 ptype(proc->res_prefix, proc->res_type, 1); 199 f_print(fout, "*%s;\n", result); 200 } 201 202 } else { /* C++ style header */ 203 f_print(fout, "("); 204 for (l = proc->args.decls; l != NULL; l = l->next) { 205 pdeclaration(proc->args.argname, &l->decl, 0, 206 ", "); 207 } 208 if (mtflag && !oneway) { 209 ptype(proc->res_prefix, proc->res_type, 1); 210 f_print(fout, "*%s, ", result); 211 212 } 213 f_print(fout, "%s%s)\n", addargtype, addargname); 214 } 215 } 216 217 if (!Cflag) 218 f_print(fout, "\t%s%s;\n", addargtype, addargname); 219 } 220 221 222 223 static char * 224 ampr(type) 225 char *type; 226 { 227 if (isvectordef(type, REL_ALIAS)) { 228 return (""); 229 } else { 230 return ("&"); 231 } 232 } 233 234 static 235 printbody(proc) 236 proc_list *proc; 237 { 238 decl_list *l; 239 bool_t args2 = (proc->arg_num > 1); 240 int i; 241 bool_t oneway = streq(proc->res_type, "oneway"); 242 243 /* 244 * For new style with multiple arguments, need a structure in which 245 * to stuff the arguments. 246 */ 247 248 249 if (newstyle && args2) { 250 f_print(fout, "\t%s", proc->args.argname); 251 f_print(fout, " arg;\n"); 252 } 253 if (!oneway) { 254 if (!mtflag) { 255 f_print(fout, "\tstatic "); 256 if (streq(proc->res_type, "void")) { 257 f_print(fout, "char "); 258 } else { 259 ptype(proc->res_prefix, proc->res_type, 0); 260 } 261 f_print(fout, "%s;\n", RESULT); 262 f_print(fout, "\n"); 263 f_print(fout, 264 "\tmemset((char *)%s%s, 0, sizeof (%s));\n", 265 ampr(proc->res_type), RESULT, RESULT); 266 267 } 268 if (newstyle && !args2 && 269 (streq(proc->args.decls->decl.type, "void"))) { 270 /* newstyle, 0 arguments */ 271 272 if (mtflag) 273 f_print(fout, "\t return "); 274 else 275 f_print(fout, "\t if "); 276 277 f_print(fout, 278 "(clnt_call(clnt, %s,\n\t\t(xdrproc_t) xdr_void, ", 279 proc->proc_name); 280 f_print(fout, 281 "(caddr_t) NULL,\n\t\t(xdrproc_t) xdr_%s, " 282 "(caddr_t) %s%s,", 283 stringfix(proc->res_type), 284 (mtflag)?"":ampr(proc->res_type), 285 RESULT); 286 287 if (mtflag) 288 f_print(fout, "\n\t\tTIMEOUT));\n"); 289 else 290 f_print(fout, 291 "\n\t\tTIMEOUT) != RPC_SUCCESS) {\n"); 292 293 } else if (newstyle && args2) { 294 /* 295 * Newstyle, multiple arguments 296 * stuff arguments into structure 297 */ 298 for (l = proc->args.decls; l != NULL; l = l->next) { 299 f_print(fout, "\targ.%s = %s;\n", 300 l->decl.name, l->decl.name); 301 } 302 if (mtflag) 303 f_print(fout, "\treturn "); 304 else 305 f_print(fout, "\tif "); 306 f_print(fout, 307 "(clnt_call(clnt, %s,\n\t\t(xdrproc_t) xdr_%s", 308 proc->proc_name, proc->args.argname); 309 f_print(fout, 310 ", (caddr_t) &arg,\n\t\t(xdrproc_t) xdr_%s, " 311 "(caddr_t) %s%s,", 312 stringfix(proc->res_type), 313 (mtflag)?"":ampr(proc->res_type), 314 RESULT); 315 if (mtflag) 316 f_print(fout, "\n\t\tTIMEOUT));\n"); 317 else 318 f_print(fout, 319 "\n\t\tTIMEOUT) != RPC_SUCCESS) {\n"); 320 } else { /* single argument, new or old style */ 321 if (!mtflag) 322 f_print(fout, 323 "\tif (clnt_call(clnt, " 324 "%s,\n\t\t(xdrproc_t) xdr_%s, " 325 "(caddr_t) %s%s,\n\t\t(xdrproc_t) xdr_%s, " 326 "(caddr_t) %s%s,\n\t\tTIMEOUT) != " 327 "RPC_SUCCESS) {\n", 328 proc->proc_name, 329 stringfix(proc->args.decls->decl.type), 330 (newstyle ? "&" : ""), 331 (newstyle ? 332 proc->args.decls->decl.name : 333 "argp"), 334 stringfix(proc->res_type), 335 ampr(proc->res_type), 336 RESULT); 337 else 338 f_print(fout, 339 "\treturn (clnt_call(clnt, " 340 "%s,\n\t\t(xdrproc_t) xdr_%s, " 341 "(caddr_t) %s%s,\n\t\t(xdrproc_t) xdr_%s, " 342 "(caddr_t) %s%s,\n\t\tTIMEOUT));\n", 343 proc->proc_name, 344 stringfix(proc->args.decls->decl.type), 345 (newstyle ? "&" : ""), 346 (newstyle ? 347 proc->args.decls->decl.name : 348 "argp"), 349 stringfix(proc->res_type), "", 350 RESULT); 351 } 352 if (!mtflag) { 353 f_print(fout, "\t\treturn (NULL);\n"); 354 f_print(fout, "\t}\n"); 355 356 if (streq(proc->res_type, "void")) { 357 f_print(fout, "\treturn ((void *)%s%s);\n", 358 ampr(proc->res_type), RESULT); 359 } else { 360 f_print(fout, "\treturn (%s%s);\n", 361 ampr(proc->res_type), RESULT); 362 } 363 } 364 } else { 365 /* oneway call */ 366 if (!mtflag) { 367 f_print(fout, "\tstatic enum clnt_stat "); 368 f_print(fout, "%s;\n", RESULT); 369 f_print(fout, "\n"); 370 f_print(fout, 371 "\tmemset((char *)&%s, 0, sizeof (%s));\n", 372 RESULT, RESULT); 373 374 } 375 if (newstyle && !args2 && 376 (streq(proc->args.decls->decl.type, "void"))) { 377 /* newstyle, 0 arguments */ 378 379 if (mtflag) 380 f_print(fout, "\t return ("); 381 else 382 f_print(fout, "\t if ((%s = ", RESULT); 383 384 f_print(fout, 385 "clnt_send(clnt, %s,\n\t\t(xdrproc_t) xdr_void, ", 386 proc->proc_name); 387 f_print(fout, 388 "(caddr_t) NULL)"); 389 390 if (mtflag) 391 f_print(fout, ");\n"); 392 else 393 f_print(fout, ") != RPC_SUCCESS) {\n"); 394 395 } else if (newstyle && args2) { 396 /* 397 * Newstyle, multiple arguments 398 * stuff arguments into structure 399 */ 400 for (l = proc->args.decls; l != NULL; l = l->next) { 401 f_print(fout, "\targ.%s = %s;\n", 402 l->decl.name, l->decl.name); 403 } 404 if (mtflag) 405 f_print(fout, "\treturn ("); 406 else 407 f_print(fout, "\tif ((%s =", RESULT); 408 f_print(fout, 409 "clnt_send(clnt, %s,\n\t\t(xdrproc_t) xdr_%s", 410 proc->proc_name, proc->args.argname); 411 f_print(fout, 412 ", (caddr_t) &arg)"); 413 if (mtflag) 414 f_print(fout, ");\n"); 415 else 416 f_print(fout, ") != RPC_SUCCESS) {\n"); 417 } else { /* single argument, new or old style */ 418 if (!mtflag) 419 f_print(fout, 420 "\tif ((%s = clnt_send(clnt, " 421 "%s,\n\t\t(xdrproc_t) xdr_%s, " 422 "(caddr_t) %s%s)) != RPC_SUCCESS) {\n", 423 RESULT, 424 proc->proc_name, 425 stringfix(proc->args.decls->decl.type), 426 (newstyle ? "&" : ""), 427 (newstyle ? 428 proc->args.decls->decl.name : 429 "argp")); 430 else 431 432 f_print(fout, 433 "\treturn (clnt_send(clnt, " 434 "%s,\n\t\t(xdrproc_t) xdr_%s, " 435 "(caddr_t) %s%s));\n", 436 proc->proc_name, 437 stringfix(proc->args.decls->decl.type), 438 (newstyle ? "&" : ""), 439 (newstyle ? 440 proc->args.decls->decl.name : 441 "argp")); 442 } 443 if (!mtflag) { 444 f_print(fout, "\t\treturn (NULL);\n"); 445 f_print(fout, "\t}\n"); 446 447 f_print(fout, "\treturn ((void *)&%s);\n", 448 RESULT); 449 } 450 } 451 } 452