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 #if 0 31 #ifndef lint 32 #ident "@(#)rpc_cout.c 1.14 93/07/05 SMI" 33 static char sccsid[] = "@(#)rpc_cout.c 1.13 89/02/22 (C) 1987 SMI"; 34 #endif 35 #endif 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 /* 41 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler 42 * Copyright (C) 1987, Sun Microsystems, Inc. 43 */ 44 #include <ctype.h> 45 #include <stdio.h> 46 #include <string.h> 47 #include "rpc_parse.h" 48 #include "rpc_scan.h" 49 #include "rpc_util.h" 50 51 static void print_header( definition * ); 52 static void print_trailer( void ); 53 static void print_stat( int , declaration * ); 54 static void emit_enum( definition * ); 55 static void emit_program( definition * ); 56 static void emit_union( definition * ); 57 static void emit_struct( definition * ); 58 static void emit_typedef( definition * ); 59 static void emit_inline( int, declaration *, int ); 60 static void emit_single_in_line( int, declaration *, int, relation ); 61 62 /* 63 * Emit the C-routine for the given definition 64 */ 65 void 66 emit(definition *def) 67 { 68 if (def->def_kind == DEF_CONST) { 69 return; 70 } 71 if (def->def_kind == DEF_PROGRAM) { 72 emit_program(def); 73 return; 74 } 75 if (def->def_kind == DEF_TYPEDEF) { 76 /* 77 * now we need to handle declarations like 78 * struct typedef foo foo; 79 * since we dont want this to be expanded into 2 calls to xdr_foo 80 */ 81 82 if (strcmp(def->def.ty.old_type, def->def_name) == 0) 83 return; 84 } 85 print_header(def); 86 switch (def->def_kind) { 87 case DEF_UNION: 88 emit_union(def); 89 break; 90 case DEF_ENUM: 91 emit_enum(def); 92 break; 93 case DEF_STRUCT: 94 emit_struct(def); 95 break; 96 case DEF_TYPEDEF: 97 emit_typedef(def); 98 break; 99 /* DEF_CONST and DEF_PROGRAM have already been handled */ 100 default: 101 break; 102 } 103 print_trailer(); 104 } 105 106 static int 107 findtype(definition *def, const char *type) 108 { 109 110 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) { 111 return (0); 112 } else { 113 return (streq(def->def_name, type)); 114 } 115 } 116 117 static int 118 undefined(const char *type) 119 { 120 definition *def; 121 122 def = (definition *) FINDVAL(defined, type, findtype); 123 return (def == NULL); 124 } 125 126 127 static void 128 print_generic_header(const char *procname, int pointerp) 129 { 130 f_print(fout, "\n"); 131 f_print(fout, "bool_t\n"); 132 f_print(fout, "xdr_%s(", procname); 133 f_print(fout, "XDR *xdrs, "); 134 f_print(fout, "%s ", procname); 135 if (pointerp) 136 f_print(fout, "*"); 137 f_print(fout, "objp)\n{\n\n"); 138 } 139 140 static void 141 print_header(definition *def) 142 { 143 print_generic_header(def->def_name, 144 def->def_kind != DEF_TYPEDEF || 145 !isvectordef(def->def.ty.old_type, 146 def->def.ty.rel)); 147 /* Now add Inline support */ 148 149 if (inline_size == 0) 150 return; 151 /* May cause lint to complain. but ... */ 152 f_print(fout, "\tregister long *buf;\n\n"); 153 } 154 155 static void 156 print_prog_header(proc_list *plist) 157 { 158 print_generic_header(plist->args.argname, 1); 159 } 160 161 static void 162 print_trailer(void) 163 { 164 f_print(fout, "\treturn (TRUE);\n"); 165 f_print(fout, "}\n"); 166 } 167 168 169 static void 170 print_ifopen(int indent, const char *name) 171 { 172 tabify(fout, indent); 173 f_print(fout, "if (!xdr_%s(xdrs", name); 174 } 175 176 static void 177 print_ifarg(const char *arg) 178 { 179 f_print(fout, ", %s", arg); 180 } 181 182 static void 183 print_ifsizeof(int indent, const char *prefix, const char *type) 184 { 185 if (indent) { 186 f_print(fout, ",\n"); 187 tabify(fout, indent); 188 } else { 189 f_print(fout, ", "); 190 } 191 if (streq(type, "bool")) { 192 f_print(fout, "sizeof (bool_t), (xdrproc_t) xdr_bool"); 193 } else { 194 f_print(fout, "sizeof ("); 195 if (undefined(type) && prefix) { 196 f_print(fout, "%s ", prefix); 197 } 198 f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type); 199 } 200 } 201 202 static void 203 print_ifclose(int indent, int brace) 204 { 205 f_print(fout, "))\n"); 206 tabify(fout, indent); 207 f_print(fout, "\treturn (FALSE);\n"); 208 if (brace) 209 f_print(fout, "\t}\n"); 210 } 211 212 static void 213 print_ifstat(int indent, const char *prefix, const char *type, relation rel, 214 const char *amax, const char *objname, const char *name) 215 { 216 const char *alt = NULL; 217 int brace = 0; 218 219 switch (rel) { 220 case REL_POINTER: 221 brace = 1; 222 f_print(fout, "\t{\n"); 223 f_print(fout, "\t%s **pp = %s;\n", type, objname); 224 print_ifopen(indent, "pointer"); 225 print_ifarg("(char **)"); 226 f_print(fout, "pp"); 227 print_ifsizeof(0, prefix, type); 228 break; 229 case REL_VECTOR: 230 if (streq(type, "string")) { 231 alt = "string"; 232 } else if (streq(type, "opaque")) { 233 alt = "opaque"; 234 } 235 if (alt) { 236 print_ifopen(indent, alt); 237 print_ifarg(objname); 238 } else { 239 print_ifopen(indent, "vector"); 240 print_ifarg("(char *)"); 241 f_print(fout, "%s", objname); 242 } 243 print_ifarg(amax); 244 if (!alt) { 245 print_ifsizeof(indent + 1, prefix, type); 246 } 247 break; 248 case REL_ARRAY: 249 if (streq(type, "string")) { 250 alt = "string"; 251 } else if (streq(type, "opaque")) { 252 alt = "bytes"; 253 } 254 if (streq(type, "string")) { 255 print_ifopen(indent, alt); 256 print_ifarg(objname); 257 } else { 258 if (alt) { 259 print_ifopen(indent, alt); 260 } else { 261 print_ifopen(indent, "array"); 262 } 263 print_ifarg("(char **)"); 264 if (*objname == '&') { 265 f_print(fout, "%s.%s_val, (u_int *) %s.%s_len", 266 objname, name, objname, name); 267 } else { 268 f_print(fout, 269 "&%s->%s_val, (u_int *) &%s->%s_len", 270 objname, name, objname, name); 271 } 272 } 273 print_ifarg(amax); 274 if (!alt) { 275 print_ifsizeof(indent + 1, prefix, type); 276 } 277 break; 278 case REL_ALIAS: 279 print_ifopen(indent, type); 280 print_ifarg(objname); 281 break; 282 } 283 print_ifclose(indent, brace); 284 } 285 286 /* ARGSUSED */ 287 static void 288 emit_enum(definition *def __unused) 289 { 290 print_ifopen(1, "enum"); 291 print_ifarg("(enum_t *)objp"); 292 print_ifclose(1, 0); 293 } 294 295 static void 296 emit_program(definition *def) 297 { 298 decl_list *dl; 299 version_list *vlist; 300 proc_list *plist; 301 302 for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next) 303 for (plist = vlist->procs; plist != NULL; plist = plist->next) { 304 if (!newstyle || plist->arg_num < 2) 305 continue; /* old style, or single argument */ 306 print_prog_header(plist); 307 for (dl = plist->args.decls; dl != NULL; 308 dl = dl->next) 309 print_stat(1, &dl->decl); 310 print_trailer(); 311 } 312 } 313 314 315 static void 316 emit_union(definition *def) 317 { 318 declaration *dflt; 319 case_list *cl; 320 declaration *cs; 321 char *object; 322 const char *vecformat = "objp->%s_u.%s"; 323 const char *format = "&objp->%s_u.%s"; 324 325 print_stat(1, &def->def.un.enum_decl); 326 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name); 327 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) { 328 329 f_print(fout, "\tcase %s:\n", cl->case_name); 330 if (cl->contflag == 1) /* a continued case statement */ 331 continue; 332 cs = &cl->case_decl; 333 if (!streq(cs->type, "void")) { 334 object = xmalloc(strlen(def->def_name) + 335 strlen(format) + strlen(cs->name) + 1); 336 if (isvectordef (cs->type, cs->rel)) { 337 s_print(object, vecformat, def->def_name, 338 cs->name); 339 } else { 340 s_print(object, format, def->def_name, 341 cs->name); 342 } 343 print_ifstat(2, cs->prefix, cs->type, cs->rel, 344 cs->array_max, object, cs->name); 345 free(object); 346 } 347 f_print(fout, "\t\tbreak;\n"); 348 } 349 dflt = def->def.un.default_decl; 350 if (dflt != NULL) { 351 if (!streq(dflt->type, "void")) { 352 f_print(fout, "\tdefault:\n"); 353 object = xmalloc(strlen(def->def_name) + 354 strlen(format) + strlen(dflt->name) + 1); 355 if (isvectordef (dflt->type, dflt->rel)) { 356 s_print(object, vecformat, def->def_name, 357 dflt->name); 358 } else { 359 s_print(object, format, def->def_name, 360 dflt->name); 361 } 362 363 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel, 364 dflt->array_max, object, dflt->name); 365 free(object); 366 f_print(fout, "\t\tbreak;\n"); 367 } else { 368 f_print(fout, "\tdefault:\n"); 369 f_print(fout, "\t\tbreak;\n"); 370 } 371 } else { 372 f_print(fout, "\tdefault:\n"); 373 f_print(fout, "\t\treturn (FALSE);\n"); 374 } 375 376 f_print(fout, "\t}\n"); 377 } 378 379 static void 380 inline_struct(definition *def, int flag) 381 { 382 decl_list *dl; 383 int i, size; 384 decl_list *cur, *psav; 385 bas_type *ptr; 386 char *sizestr; 387 const char *plus; 388 char ptemp[256]; 389 int indent = 1; 390 391 cur = NULL; 392 if (flag == PUT) 393 f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n"); 394 else 395 f_print(fout, "\t\treturn (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n"); 396 397 i = 0; 398 size = 0; 399 sizestr = NULL; 400 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */ 401 /* now walk down the list and check for basic types */ 402 if ((dl->decl.prefix == NULL) && 403 ((ptr = find_type(dl->decl.type)) != NULL) && 404 ((dl->decl.rel == REL_ALIAS) || 405 (dl->decl.rel == REL_VECTOR))){ 406 if (i == 0) 407 cur = dl; 408 i++; 409 410 if (dl->decl.rel == REL_ALIAS) 411 size += ptr->length; 412 else { 413 /* this code is required to handle arrays */ 414 if (sizestr == NULL) 415 plus = ""; 416 else 417 plus = " + "; 418 419 if (ptr->length != 1) 420 s_print(ptemp, "%s%s * %d", 421 plus, dl->decl.array_max, 422 ptr->length); 423 else 424 s_print(ptemp, "%s%s", plus, 425 dl->decl.array_max); 426 427 /* now concatenate to sizestr !!!! */ 428 if (sizestr == NULL) { 429 sizestr = xstrdup(ptemp); 430 } 431 else{ 432 sizestr = xrealloc(sizestr, 433 strlen(sizestr) 434 +strlen(ptemp)+1); 435 sizestr = strcat(sizestr, ptemp); 436 /* build up length of array */ 437 } 438 } 439 } else { 440 if (i > 0) { 441 if (sizestr == NULL && size < inline_size){ 442 /* 443 * don't expand into inline code 444 * if size < inline_size 445 */ 446 while (cur != dl){ 447 print_stat(indent + 1, &cur->decl); 448 cur = cur->next; 449 } 450 } else { 451 /* were already looking at a xdr_inlineable structure */ 452 tabify(fout, indent + 1); 453 if (sizestr == NULL) 454 f_print(fout, "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);", 455 size); 456 else { 457 if (size == 0) 458 f_print(fout, 459 "buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);", 460 sizestr); 461 else 462 f_print(fout, 463 "buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);", 464 size, sizestr); 465 466 } 467 f_print(fout, "\n"); 468 tabify(fout, indent + 1); 469 f_print(fout, 470 "if (buf == NULL) {\n"); 471 472 psav = cur; 473 while (cur != dl){ 474 print_stat(indent + 2, &cur->decl); 475 cur = cur->next; 476 } 477 478 f_print(fout, "\n\t\t} else {\n"); 479 480 cur = psav; 481 while (cur != dl){ 482 emit_inline(indent + 2, &cur->decl, flag); 483 cur = cur->next; 484 } 485 486 tabify(fout, indent + 1); 487 f_print(fout, "}\n"); 488 } 489 } 490 size = 0; 491 i = 0; 492 free(sizestr); 493 sizestr = NULL; 494 print_stat(indent + 1, &dl->decl); 495 } 496 } 497 498 if (i > 0) { 499 if (sizestr == NULL && size < inline_size){ 500 /* don't expand into inline code if size < inline_size */ 501 while (cur != dl){ 502 print_stat(indent + 1, &cur->decl); 503 cur = cur->next; 504 } 505 } else { 506 /* were already looking at a xdr_inlineable structure */ 507 if (sizestr == NULL) 508 f_print(fout, "\t\tbuf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);", 509 size); 510 else 511 if (size == 0) 512 f_print(fout, 513 "\t\tbuf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);", 514 sizestr); 515 else 516 f_print(fout, 517 "\t\tbuf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);", 518 size, sizestr); 519 520 f_print(fout, "\n\t\tif (buf == NULL) {\n"); 521 psav = cur; 522 while (cur != NULL){ 523 print_stat(indent + 2, &cur->decl); 524 cur = cur->next; 525 } 526 f_print(fout, "\t\t} else {\n"); 527 528 cur = psav; 529 while (cur != dl){ 530 emit_inline(indent + 2, &cur->decl, flag); 531 cur = cur->next; 532 } 533 f_print(fout, "\t\t}\n"); 534 } 535 } 536 } 537 538 static void 539 emit_struct(definition *def) 540 { 541 decl_list *dl; 542 int j, size, flag; 543 bas_type *ptr; 544 int can_inline; 545 546 if (inline_size == 0) { 547 /* No xdr_inlining at all */ 548 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) 549 print_stat(1, &dl->decl); 550 return; 551 } 552 553 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) 554 if (dl->decl.rel == REL_VECTOR && 555 strcmp(dl->decl.type, "opaque") != 0){ 556 f_print(fout, "\tint i;\n"); 557 break; 558 } 559 560 size = 0; 561 can_inline = 0; 562 /* 563 * Make a first pass and see if inling is possible. 564 */ 565 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) 566 if ((dl->decl.prefix == NULL) && 567 ((ptr = find_type(dl->decl.type)) != NULL) && 568 ((dl->decl.rel == REL_ALIAS)|| 569 (dl->decl.rel == REL_VECTOR))){ 570 if (dl->decl.rel == REL_ALIAS) 571 size += ptr->length; 572 else { 573 can_inline = 1; 574 break; /* can be inlined */ 575 } 576 } else { 577 if (size >= inline_size){ 578 can_inline = 1; 579 break; /* can be inlined */ 580 } 581 size = 0; 582 } 583 if (size >= inline_size) 584 can_inline = 1; 585 586 if (can_inline == 0){ /* can not inline, drop back to old mode */ 587 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) 588 print_stat(1, &dl->decl); 589 return; 590 } 591 592 flag = PUT; 593 for (j = 0; j < 2; j++){ 594 inline_struct(def, flag); 595 if (flag == PUT) 596 flag = GET; 597 } 598 599 f_print(fout, "\t\treturn (TRUE);\n\t}\n\n"); 600 601 /* now take care of XDR_FREE case */ 602 603 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) 604 print_stat(1, &dl->decl); 605 606 } 607 608 static void 609 emit_typedef(definition *def) 610 { 611 const char *prefix = def->def.ty.old_prefix; 612 const char *type = def->def.ty.old_type; 613 const char *amax = def->def.ty.array_max; 614 relation rel = def->def.ty.rel; 615 616 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name); 617 } 618 619 static void 620 print_stat(int indent, declaration *dec) 621 { 622 const char *prefix = dec->prefix; 623 const char *type = dec->type; 624 const char *amax = dec->array_max; 625 relation rel = dec->rel; 626 char name[256]; 627 628 if (isvectordef(type, rel)) { 629 s_print(name, "objp->%s", dec->name); 630 } else { 631 s_print(name, "&objp->%s", dec->name); 632 } 633 print_ifstat(indent, prefix, type, rel, amax, name, dec->name); 634 } 635 636 637 char *upcase(const char *); 638 639 static void 640 emit_inline(int indent, declaration *decl, int flag) 641 { 642 switch (decl->rel) { 643 case REL_ALIAS : 644 emit_single_in_line(indent, decl, flag, REL_ALIAS); 645 break; 646 case REL_VECTOR : 647 tabify(fout, indent); 648 f_print(fout, "{\n"); 649 tabify(fout, indent + 1); 650 f_print(fout, "%s *genp;\n\n", decl->type); 651 tabify(fout, indent + 1); 652 f_print(fout, 653 "for (i = 0, genp = objp->%s;\n", decl->name); 654 tabify(fout, indent + 2); 655 f_print(fout, "i < %s; i++) {\n", decl->array_max); 656 emit_single_in_line(indent + 2, decl, flag, REL_VECTOR); 657 tabify(fout, indent + 1); 658 f_print(fout, "}\n"); 659 tabify(fout, indent); 660 f_print(fout, "}\n"); 661 break; 662 default: 663 break; 664 } 665 } 666 667 static void 668 emit_single_in_line(int indent, declaration *decl, int flag, relation rel) 669 { 670 char *upp_case; 671 672 tabify(fout, indent); 673 if (flag == PUT) 674 f_print(fout, "IXDR_PUT_"); 675 else 676 if (rel == REL_ALIAS) 677 f_print(fout, "objp->%s = IXDR_GET_", decl->name); 678 else 679 f_print(fout, "*genp++ = IXDR_GET_"); 680 681 upp_case = upcase(decl->type); 682 683 /* hack - XX */ 684 if (strcmp(upp_case, "INT") == 0) 685 { 686 free(upp_case); 687 upp_case = strdup("LONG"); 688 } 689 690 if (strcmp(upp_case, "U_INT") == 0) 691 { 692 free(upp_case); 693 upp_case = strdup("U_LONG"); 694 } 695 if (flag == PUT) 696 if (rel == REL_ALIAS) 697 f_print(fout, 698 "%s(buf, objp->%s);\n", upp_case, decl->name); 699 else 700 f_print(fout, "%s(buf, *genp++);\n", upp_case); 701 702 else 703 f_print(fout, "%s(buf);\n", upp_case); 704 free(upp_case); 705 } 706 707 char * 708 upcase(const char *str) 709 { 710 char *ptr, *hptr; 711 712 ptr = (char *)xmalloc(strlen(str)+1); 713 714 hptr = ptr; 715 while (*str != '\0') 716 *ptr++ = toupper(*str++); 717 718 *ptr = '\0'; 719 return (hptr); 720 } 721