1 /* $FreeBSD$ */ 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. 9 * 10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 13 * 14 * Sun RPC is provided with no support and without any obligation on the 15 * part of Sun Microsystems, Inc. to assist in its use, correction, 16 * modification or enhancement. 17 * 18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 20 * OR ANY PART THEREOF. 21 * 22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 23 * or profits or other special, indirect and consequential damages, even if 24 * Sun has been advised of the possibility of such damages. 25 * 26 * Sun Microsystems, Inc. 27 * 2550 Garcia Avenue 28 * Mountain View, California 94043 29 */ 30 31 #ident "@(#)rpc_hout.c 1.16 94/04/25 SMI" 32 33 #ifndef lint 34 static char sccsid[] = "@(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI"; 35 #endif 36 37 /* 38 * rpc_hout.c, Header file outputter for the RPC protocol compiler 39 * Copyright (C) 1987, Sun Microsystems, Inc. 40 */ 41 #include <stdio.h> 42 #include <ctype.h> 43 #include "rpc_parse.h" 44 #include "rpc_util.h" 45 46 void storexdrfuncdecl __P(( char *, int )); 47 static void pconstdef __P(( definition * )); 48 static void pstructdef __P(( definition * )); 49 static void puniondef __P(( definition * )); 50 static void pprogramdef __P(( definition * )); 51 static void pstructdef __P(( definition * )); 52 static void penumdef __P(( definition * )); 53 static void ptypedef __P(( definition * )); 54 static void pdefine __P(( char *, char * )); 55 static int undefined2 __P(( char *, char * )); 56 static void parglist __P(( proc_list *, char * )); 57 static void pprocdef __P(( proc_list *, version_list *, char *, int, int )); 58 void pdeclaration __P(( char *, declaration *, int, char * )); 59 60 static char RESULT[] = "clnt_res"; 61 62 63 /* 64 * Print the C-version of an xdr definition 65 */ 66 void 67 print_datadef(def) 68 definition *def; 69 { 70 71 if (def->def_kind == DEF_PROGRAM) /* handle data only */ 72 return; 73 74 if (def->def_kind != DEF_CONST) { 75 f_print(fout, "\n"); 76 } 77 switch (def->def_kind) { 78 case DEF_STRUCT: 79 pstructdef(def); 80 break; 81 case DEF_UNION: 82 puniondef(def); 83 break; 84 case DEF_ENUM: 85 penumdef(def); 86 break; 87 case DEF_TYPEDEF: 88 ptypedef(def); 89 break; 90 case DEF_PROGRAM: 91 pprogramdef(def); 92 break; 93 case DEF_CONST: 94 pconstdef(def); 95 break; 96 } 97 if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) { 98 storexdrfuncdecl(def->def_name, 99 def->def_kind != DEF_TYPEDEF || 100 !isvectordef(def->def.ty.old_type, 101 def->def.ty.rel)); 102 } 103 } 104 105 106 void 107 print_funcdef(def) 108 definition *def; 109 { 110 switch (def->def_kind) { 111 case DEF_PROGRAM: 112 f_print(fout, "\n"); 113 pprogramdef(def); 114 break; 115 default: 116 } 117 } 118 119 /* store away enough information to allow the XDR functions to be spat 120 out at the end of the file */ 121 122 void 123 storexdrfuncdecl(name, pointerp) 124 char *name; 125 int pointerp; 126 { 127 xdrfunc * xdrptr; 128 129 xdrptr = (xdrfunc *) malloc(sizeof (struct xdrfunc)); 130 131 xdrptr->name = name; 132 xdrptr->pointerp = pointerp; 133 xdrptr->next = NULL; 134 135 if (xdrfunc_tail == NULL){ 136 xdrfunc_head = xdrptr; 137 xdrfunc_tail = xdrptr; 138 } else { 139 xdrfunc_tail->next = xdrptr; 140 xdrfunc_tail = xdrptr; 141 } 142 143 144 } 145 146 void 147 print_xdr_func_def(name, pointerp, i) 148 char* name; 149 int pointerp; 150 int i; 151 { 152 if (i == 2) { 153 f_print(fout, "extern bool_t xdr_%s();\n", name); 154 return; 155 } 156 else 157 f_print(fout, "extern bool_t xdr_%s(XDR *, %s%s);\n", name, 158 name, pointerp ? "*" : ""); 159 160 161 } 162 163 164 static void 165 pconstdef(def) 166 definition *def; 167 { 168 pdefine(def->def_name, def->def.co); 169 } 170 171 /* print out the definitions for the arguments of functions in the 172 header file 173 */ 174 static void 175 pargdef(def) 176 definition *def; 177 { 178 decl_list *l; 179 version_list *vers; 180 char *name; 181 proc_list *plist; 182 183 184 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) { 185 for (plist = vers->procs; plist != NULL; 186 plist = plist->next) { 187 188 if (!newstyle || plist->arg_num < 2) { 189 continue; /* old style or single args */ 190 } 191 name = plist->args.argname; 192 f_print(fout, "struct %s {\n", name); 193 for (l = plist->args.decls; 194 l != NULL; l = l->next) { 195 pdeclaration(name, &l->decl, 1, 196 ";\n"); 197 } 198 f_print(fout, "};\n"); 199 f_print(fout, "typedef struct %s %s;\n", 200 name, name); 201 storexdrfuncdecl(name, 1); 202 f_print(fout, "\n"); 203 } 204 } 205 } 206 207 208 static void 209 pstructdef(def) 210 definition *def; 211 { 212 decl_list *l; 213 char *name = def->def_name; 214 215 f_print(fout, "struct %s {\n", name); 216 for (l = def->def.st.decls; l != NULL; l = l->next) { 217 pdeclaration(name, &l->decl, 1, ";\n"); 218 } 219 f_print(fout, "};\n"); 220 f_print(fout, "typedef struct %s %s;\n", name, name); 221 } 222 223 static void 224 puniondef(def) 225 definition *def; 226 { 227 case_list *l; 228 char *name = def->def_name; 229 declaration *decl; 230 231 f_print(fout, "struct %s {\n", name); 232 decl = &def->def.un.enum_decl; 233 if (streq(decl->type, "bool")) { 234 f_print(fout, "\tbool_t %s;\n", decl->name); 235 } else { 236 f_print(fout, "\t%s %s;\n", decl->type, decl->name); 237 } 238 f_print(fout, "\tunion {\n"); 239 for (l = def->def.un.cases; l != NULL; l = l->next) { 240 if (l->contflag == 0) 241 pdeclaration(name, &l->case_decl, 2, ";\n"); 242 } 243 decl = def->def.un.default_decl; 244 if (decl && !streq(decl->type, "void")) { 245 pdeclaration(name, decl, 2, ";\n"); 246 } 247 f_print(fout, "\t} %s_u;\n", name); 248 f_print(fout, "};\n"); 249 f_print(fout, "typedef struct %s %s;\n", name, name); 250 } 251 252 static void 253 pdefine(name, num) 254 char *name; 255 char *num; 256 { 257 f_print(fout, "#define\t%s %s\n", name, num); 258 } 259 260 static void 261 puldefine(name, num) 262 char *name; 263 char *num; 264 { 265 f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num); 266 } 267 268 static int 269 define_printed(stop, start) 270 proc_list *stop; 271 version_list *start; 272 { 273 version_list *vers; 274 proc_list *proc; 275 276 for (vers = start; vers != NULL; vers = vers->next) { 277 for (proc = vers->procs; proc != NULL; proc = proc->next) { 278 if (proc == stop) { 279 return (0); 280 } else if (streq(proc->proc_name, stop->proc_name)) { 281 return (1); 282 } 283 } 284 } 285 abort(); 286 /* NOTREACHED */ 287 } 288 289 static void 290 pfreeprocdef(char * name, char *vers, int mode) 291 { 292 f_print(fout, "extern int "); 293 pvname(name, vers); 294 if (mode == 1) 295 f_print(fout,"_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n"); 296 else 297 f_print(fout,"_freeresult();\n"); 298 299 300 } 301 302 static void 303 pdispatch(char * name, char *vers, int mode) 304 { 305 306 f_print(fout, "void "); 307 pvname(name, vers); 308 if (mode == 1) 309 f_print(fout, 310 "(struct svc_req *rqstp, register SVCXPRT *transp);\n"); 311 else 312 f_print(fout,"();\n"); 313 314 } 315 316 static void 317 pprogramdef(def) 318 definition *def; 319 { 320 version_list *vers; 321 proc_list *proc; 322 int i; 323 char *ext; 324 325 pargdef(def); 326 327 puldefine(def->def_name, def->def.pr.prog_num); 328 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) { 329 if (tblflag) { 330 f_print(fout, 331 "extern struct rpcgen_table %s_%s_table[];\n", 332 locase(def->def_name), vers->vers_num); 333 f_print(fout, 334 "extern %s_%s_nproc;\n", 335 locase(def->def_name), vers->vers_num); 336 } 337 puldefine(vers->vers_name, vers->vers_num); 338 339 /* 340 * Print out 2 definitions, one for ANSI-C, another for 341 * old K & R C 342 */ 343 344 if(!Cflag){ 345 ext = "extern "; 346 f_print(fout, "%s", ext); 347 pdispatch(def->def_name, vers->vers_num, 2); 348 for (proc = vers->procs; proc != NULL; 349 proc = proc->next) { 350 if (!define_printed(proc, 351 def->def.pr.versions)) { 352 puldefine(proc->proc_name, 353 proc->proc_num); 354 } 355 f_print(fout, "%s", ext); 356 pprocdef(proc, vers, NULL, 0, 2); 357 358 if (mtflag) { 359 f_print(fout, "%s", ext); 360 pprocdef(proc, vers, NULL, 1, 2); 361 } 362 } 363 pfreeprocdef(def->def_name, vers->vers_num, 2); 364 365 } else { 366 for (i = 1; i < 3; i++){ 367 if (i == 1){ 368 f_print(fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n"); 369 ext = "extern "; 370 }else{ 371 f_print(fout, "\n#else /* K&R C */\n"); 372 ext = "extern "; 373 } 374 375 f_print(fout, "%s", ext); 376 pdispatch(def->def_name, vers->vers_num, i); 377 for (proc = vers->procs; proc != NULL; 378 proc = proc->next) { 379 if (!define_printed(proc, 380 def->def.pr.versions)) { 381 puldefine(proc->proc_name, 382 proc->proc_num); 383 } 384 f_print(fout, "%s", ext); 385 pprocdef(proc, vers, "CLIENT *", 0, i); 386 f_print(fout, "%s", ext); 387 pprocdef(proc, vers, "struct svc_req *", 1, i); 388 } 389 pfreeprocdef(def->def_name, vers->vers_num, i); 390 } 391 f_print(fout, "#endif /* K&R C */\n"); 392 } 393 } 394 } 395 396 static void 397 pprocdef(proc, vp, addargtype, server_p, mode) 398 proc_list *proc; 399 version_list *vp; 400 char* addargtype; 401 int server_p; 402 int mode; 403 { 404 if (mtflag) {/* Print MT style stubs */ 405 if (server_p) 406 f_print(fout, "bool_t "); 407 else 408 f_print(fout, "enum clnt_stat "); 409 } else { 410 ptype(proc->res_prefix, proc->res_type, 1); 411 f_print(fout, "* "); 412 } 413 if (server_p) 414 pvname_svc(proc->proc_name, vp->vers_num); 415 else 416 pvname(proc->proc_name, vp->vers_num); 417 418 /* 419 * mode 1 = ANSI-C, mode 2 = K&R C 420 */ 421 if ( mode == 1) 422 parglist(proc, addargtype); 423 else 424 f_print(fout, "();\n"); 425 } 426 427 428 429 /* print out argument list of procedure */ 430 static void 431 parglist(proc, addargtype) 432 proc_list *proc; 433 char* addargtype; 434 { 435 decl_list *dl; 436 437 f_print(fout, "("); 438 if (proc->arg_num < 2 && newstyle && 439 streq(proc->args.decls->decl.type, "void")) { 440 /* 0 argument in new style: do nothing*/ 441 } 442 else { 443 for (dl = proc->args.decls; dl != NULL; dl = dl->next) { 444 ptype(dl->decl.prefix, dl->decl.type, 1); 445 if (!newstyle) 446 f_print(fout, "*"); 447 /* old style passes by reference */ 448 f_print(fout, ", "); 449 } 450 } 451 452 if (mtflag) { 453 ptype(proc->res_prefix, proc->res_type, 1); 454 f_print(fout, "*, "); 455 } 456 457 f_print(fout, "%s);\n", addargtype); 458 459 } 460 461 static void 462 penumdef(def) 463 definition *def; 464 { 465 char *name = def->def_name; 466 enumval_list *l; 467 char *last = NULL; 468 int count = 0; 469 470 f_print(fout, "enum %s {\n", name); 471 for (l = def->def.en.vals; l != NULL; l = l->next) { 472 f_print(fout, "\t%s", l->name); 473 if (l->assignment) { 474 f_print(fout, " = %s", l->assignment); 475 last = l->assignment; 476 count = 1; 477 } else { 478 if (last == NULL) { 479 f_print(fout, " = %d", count++); 480 } else { 481 f_print(fout, " = %s + %d", last, count++); 482 } 483 } 484 if (l->next) 485 f_print(fout, ",\n"); 486 else 487 f_print(fout, "\n"); 488 } 489 f_print(fout, "};\n"); 490 f_print(fout, "typedef enum %s %s;\n", name, name); 491 } 492 493 static void 494 ptypedef(def) 495 definition *def; 496 { 497 char *name = def->def_name; 498 char *old = def->def.ty.old_type; 499 char prefix[8]; /* enough to contain "struct ", including NUL */ 500 relation rel = def->def.ty.rel; 501 502 503 if (!streq(name, old)) { 504 if (streq(old, "string")) { 505 old = "char"; 506 rel = REL_POINTER; 507 } else if (streq(old, "opaque")) { 508 old = "char"; 509 } else if (streq(old, "bool")) { 510 old = "bool_t"; 511 } 512 if (undefined2(old, name) && def->def.ty.old_prefix) { 513 s_print(prefix, "%s ", def->def.ty.old_prefix); 514 } else { 515 prefix[0] = 0; 516 } 517 f_print(fout, "typedef "); 518 switch (rel) { 519 case REL_ARRAY: 520 f_print(fout, "struct {\n"); 521 f_print(fout, "\tu_int %s_len;\n", name); 522 f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name); 523 f_print(fout, "} %s", name); 524 break; 525 case REL_POINTER: 526 f_print(fout, "%s%s *%s", prefix, old, name); 527 break; 528 case REL_VECTOR: 529 f_print(fout, "%s%s %s[%s]", prefix, old, name, 530 def->def.ty.array_max); 531 break; 532 case REL_ALIAS: 533 f_print(fout, "%s%s %s", prefix, old, name); 534 break; 535 } 536 f_print(fout, ";\n"); 537 } 538 } 539 540 void 541 pdeclaration(name, dec, tab, separator) 542 char *name; 543 declaration *dec; 544 int tab; 545 char *separator; 546 { 547 char buf[8]; /* enough to hold "struct ", include NUL */ 548 char *prefix; 549 char *type; 550 551 if (streq(dec->type, "void")) { 552 return; 553 } 554 tabify(fout, tab); 555 if (streq(dec->type, name) && !dec->prefix) { 556 f_print(fout, "struct "); 557 } 558 if (streq(dec->type, "string")) { 559 f_print(fout, "char *%s", dec->name); 560 } else { 561 prefix = ""; 562 if (streq(dec->type, "bool")) { 563 type = "bool_t"; 564 } else if (streq(dec->type, "opaque")) { 565 type = "char"; 566 } else { 567 if (dec->prefix) { 568 s_print(buf, "%s ", dec->prefix); 569 prefix = buf; 570 } 571 type = dec->type; 572 } 573 switch (dec->rel) { 574 case REL_ALIAS: 575 f_print(fout, "%s%s %s", prefix, type, dec->name); 576 break; 577 case REL_VECTOR: 578 f_print(fout, "%s%s %s[%s]", prefix, type, dec->name, 579 dec->array_max); 580 break; 581 case REL_POINTER: 582 f_print(fout, "%s%s *%s", prefix, type, dec->name); 583 break; 584 case REL_ARRAY: 585 f_print(fout, "struct {\n"); 586 tabify(fout, tab); 587 f_print(fout, "\tu_int %s_len;\n", dec->name); 588 tabify(fout, tab); 589 f_print(fout, 590 "\t%s%s *%s_val;\n", prefix, type, dec->name); 591 tabify(fout, tab); 592 f_print(fout, "} %s", dec->name); 593 break; 594 } 595 } 596 f_print(fout, separator); 597 } 598 599 static int 600 undefined2(type, stop) 601 char *type; 602 char *stop; 603 { 604 list *l; 605 definition *def; 606 607 for (l = defined; l != NULL; l = l->next) { 608 def = (definition *) l->val; 609 if (def->def_kind != DEF_PROGRAM) { 610 if (streq(def->def_name, stop)) { 611 return (1); 612 } else if (streq(def->def_name, type)) { 613 return (0); 614 } 615 } 616 } 617 return (1); 618 } 619