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