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