rpc_hout.c revision 12798
11592Srgrimes/* 21592Srgrimes * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 31592Srgrimes * unrestricted use provided that this legend is included on all tape 41592Srgrimes * media and as a part of the software program in whole or part. Users 51592Srgrimes * may copy or modify Sun RPC without charge, but are not authorized 61592Srgrimes * to license or distribute it to anyone else except as part of a product or 71592Srgrimes * program developed by the user. 81592Srgrimes * 91592Srgrimes * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 101592Srgrimes * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 111592Srgrimes * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 121592Srgrimes * 131592Srgrimes * Sun RPC is provided with no support and without any obligation on the 141592Srgrimes * part of Sun Microsystems, Inc. to assist in its use, correction, 151592Srgrimes * modification or enhancement. 161592Srgrimes * 171592Srgrimes * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 181592Srgrimes * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 191592Srgrimes * OR ANY PART THEREOF. 201592Srgrimes * 21262435Sbrueffer * In no event will Sun Microsystems, Inc. be liable for any lost revenue 221592Srgrimes * or profits or other special, indirect and consequential damages, even if 231592Srgrimes * Sun has been advised of the possibility of such damages. 241592Srgrimes * 251592Srgrimes * Sun Microsystems, Inc. 261592Srgrimes * 2550 Garcia Avenue 271592Srgrimes * Mountain View, California 94043 281592Srgrimes */ 291592Srgrimes 301592Srgrimes#ident "@(#)rpc_hout.c 1.16 94/04/25 SMI" 311592Srgrimes 321592Srgrimes#ifndef lint 331592Srgrimesstatic char sccsid[] = "@(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI"; 341592Srgrimes#endif 351592Srgrimes 361592Srgrimes/* 3727074Ssteve * rpc_hout.c, Header file outputter for the RPC protocol compiler 381592Srgrimes * Copyright (C) 1987, Sun Microsystems, Inc. 3927074Ssteve */ 401592Srgrimes#include <stdio.h> 4169226Skris#include <ctype.h> 4269226Skris#include "rpc_parse.h" 431592Srgrimes#include "rpc_util.h" 441592Srgrimes 451592Srgrimesvoid storexdrfuncdecl __P(( char *, int )); 461592Srgrimesstatic int pconstdef __P(( definition * )); 471592Srgrimesstatic int pstructdef __P(( definition * )); 481592Srgrimesstatic int puniondef __P(( definition * )); 491592Srgrimesstatic int pprogramdef __P(( definition * )); 501592Srgrimesstatic int pstructdef __P(( definition * )); 511592Srgrimesstatic int penumdef __P(( definition * )); 521592Srgrimesstatic int ptypedef __P(( definition * )); 531592Srgrimesstatic int pdefine __P(( char *, char * )); 541592Srgrimesstatic int undefined2 __P(( char *, char * )); 551592Srgrimesstatic int parglist __P(( proc_list *, char * )); 561592Srgrimes 5769226Skrisstatic char RESULT[] = "clnt_res"; 581592Srgrimes 591592Srgrimes 601592Srgrimes/* 611592Srgrimes * Print the C-version of an xdr definition 621592Srgrimes */ 631592Srgrimesvoid 641592Srgrimesprint_datadef(def) 651592Srgrimes definition *def; 661592Srgrimes{ 671592Srgrimes 681592Srgrimes if (def->def_kind == DEF_PROGRAM) /* handle data only */ 691592Srgrimes return; 701592Srgrimes 711592Srgrimes if (def->def_kind != DEF_CONST) { 721592Srgrimes f_print(fout, "\n"); 731592Srgrimes } 741592Srgrimes switch (def->def_kind) { 751592Srgrimes case DEF_STRUCT: 761592Srgrimes pstructdef(def); 771592Srgrimes break; 781592Srgrimes case DEF_UNION: 791592Srgrimes puniondef(def); 801592Srgrimes break; 811592Srgrimes case DEF_ENUM: 821592Srgrimes penumdef(def); 831592Srgrimes break; 841592Srgrimes case DEF_TYPEDEF: 851592Srgrimes ptypedef(def); 861592Srgrimes break; 871592Srgrimes case DEF_PROGRAM: 881592Srgrimes pprogramdef(def); 891592Srgrimes break; 901592Srgrimes case DEF_CONST: 911592Srgrimes pconstdef(def); 921592Srgrimes break; 931592Srgrimes } 941592Srgrimes if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) { 951592Srgrimes storexdrfuncdecl(def->def_name, 961592Srgrimes def->def_kind != DEF_TYPEDEF || 971592Srgrimes !isvectordef(def->def.ty.old_type, 981592Srgrimes def->def.ty.rel)); 991592Srgrimes } 1001592Srgrimes} 1011592Srgrimes 1021592Srgrimes 1031592Srgrimesvoid 1041592Srgrimesprint_funcdef(def) 1051592Srgrimes definition *def; 1061592Srgrimes{ 1071592Srgrimes switch (def->def_kind) { 10827074Ssteve case DEF_PROGRAM: 1091592Srgrimes f_print(fout, "\n"); 1101592Srgrimes pprogramdef(def); 1111592Srgrimes break; 1121592Srgrimes } 1131592Srgrimes} 1141592Srgrimes 1151592Srgrimes/* store away enough information to allow the XDR functions to be spat 1161592Srgrimes out at the end of the file */ 1171592Srgrimes 1181592Srgrimesvoid 1191592Srgrimesstorexdrfuncdecl(name, pointerp) 1201592Srgrimeschar *name; 1211592Srgrimesint pointerp; 1221592Srgrimes{ 1231592Srgrimes xdrfunc * xdrptr; 1241592Srgrimes 1251592Srgrimes xdrptr = (xdrfunc *) malloc(sizeof (struct xdrfunc)); 1261592Srgrimes 1271592Srgrimes xdrptr->name = name; 12827074Ssteve xdrptr->pointerp = pointerp; 1291592Srgrimes xdrptr->next = NULL; 1301592Srgrimes 1311592Srgrimes if (xdrfunc_tail == NULL){ 1321592Srgrimes xdrfunc_head = xdrptr; 1331592Srgrimes xdrfunc_tail = xdrptr; 1341592Srgrimes } else { 1351592Srgrimes xdrfunc_tail->next = xdrptr; 1361592Srgrimes xdrfunc_tail = xdrptr; 1371592Srgrimes } 1381592Srgrimes 1391592Srgrimes 1401592Srgrimes} 14127074Ssteve 1421592Srgrimesvoid 1431592Srgrimesprint_xdr_func_def(name, pointerp, i) 1441592Srgrimeschar* name; 1451592Srgrimesint pointerp; 1461592Srgrimesint i; 1471592Srgrimes{ 14827074Ssteve if (i == 2) { 1491592Srgrimes f_print(fout, "extern bool_t xdr_%s();\n", name); 15090377Simp return; 15190377Simp } 15290377Simp else 15390377Simp f_print(fout, "extern bool_t xdr_%s(XDR *, %s%s);\n", name, 15490377Simp name, pointerp ? "*" : ""); 15590377Simp 15690377Simp 15790377Simp} 15890377Simp 15990377Simp 16090377Simpstatic 16190377Simppconstdef(def) 16290377Simp definition *def; 16390377Simp{ 16490377Simp pdefine(def->def_name, def->def.co); 16590377Simp} 16690377Simp 16790377Simp/* print out the definitions for the arguments of functions in the 16890377Simp header file 16990377Simp*/ 17090377Simpstatic 17190377Simppargdef(def) 17290377Simp definition *def; 17390377Simp{ 17490377Simp decl_list *l; 17590377Simp version_list *vers; 17690377Simp char *name; 17790377Simp proc_list *plist; 17890377Simp 17990377Simp 18090377Simp for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) { 18190377Simp for (plist = vers->procs; plist != NULL; 18290377Simp plist = plist->next) { 183 184 if (!newstyle || plist->arg_num < 2) { 185 continue; /* old style or single args */ 186 } 187 name = plist->args.argname; 188 f_print(fout, "struct %s {\n", name); 189 for (l = plist->args.decls; 190 l != NULL; l = l->next) { 191 pdeclaration(name, &l->decl, 1, 192 ";\n"); 193 } 194 f_print(fout, "};\n"); 195 f_print(fout, "typedef struct %s %s;\n", 196 name, name); 197 storexdrfuncdecl(name, 1); 198 f_print(fout, "\n"); 199 } 200 } 201} 202 203 204static 205pstructdef(def) 206 definition *def; 207{ 208 decl_list *l; 209 char *name = def->def_name; 210 211 f_print(fout, "struct %s {\n", name); 212 for (l = def->def.st.decls; l != NULL; l = l->next) { 213 pdeclaration(name, &l->decl, 1, ";\n"); 214 } 215 f_print(fout, "};\n"); 216 f_print(fout, "typedef struct %s %s;\n", name, name); 217} 218 219static 220puniondef(def) 221 definition *def; 222{ 223 case_list *l; 224 char *name = def->def_name; 225 declaration *decl; 226 227 f_print(fout, "struct %s {\n", name); 228 decl = &def->def.un.enum_decl; 229 if (streq(decl->type, "bool")) { 230 f_print(fout, "\tbool_t %s;\n", decl->name); 231 } else { 232 f_print(fout, "\t%s %s;\n", decl->type, decl->name); 233 } 234 f_print(fout, "\tunion {\n"); 235 for (l = def->def.un.cases; l != NULL; l = l->next) { 236 if (l->contflag == 0) 237 pdeclaration(name, &l->case_decl, 2, ";\n"); 238 } 239 decl = def->def.un.default_decl; 240 if (decl && !streq(decl->type, "void")) { 241 pdeclaration(name, decl, 2, ";\n"); 242 } 243 f_print(fout, "\t} %s_u;\n", name); 244 f_print(fout, "};\n"); 245 f_print(fout, "typedef struct %s %s;\n", name, name); 246} 247 248static 249pdefine(name, num) 250 char *name; 251 char *num; 252{ 253 f_print(fout, "#define\t%s %s\n", name, num); 254} 255 256static 257puldefine(name, num) 258 char *name; 259 char *num; 260{ 261 f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num); 262} 263 264static 265define_printed(stop, start) 266 proc_list *stop; 267 version_list *start; 268{ 269 version_list *vers; 270 proc_list *proc; 271 272 for (vers = start; vers != NULL; vers = vers->next) { 273 for (proc = vers->procs; proc != NULL; proc = proc->next) { 274 if (proc == stop) { 275 return (0); 276 } else if (streq(proc->proc_name, stop->proc_name)) { 277 return (1); 278 } 279 } 280 } 281 abort(); 282 /* NOTREACHED */ 283} 284 285static 286pfreeprocdef(char * name, char *vers, int mode) 287{ 288 f_print(fout, "extern int "); 289 pvname(name, vers); 290 if (mode == 1) 291 f_print(fout,"_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n"); 292 else 293 f_print(fout,"_freeresult();\n"); 294 295 296} 297 298static 299pprogramdef(def) 300 definition *def; 301{ 302 version_list *vers; 303 proc_list *proc; 304 int i; 305 char *ext; 306 307 pargdef(def); 308 309 puldefine(def->def_name, def->def.pr.prog_num); 310 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) { 311 if (tblflag) { 312 f_print(fout, 313 "extern struct rpcgen_table %s_%s_table[];\n", 314 locase(def->def_name), vers->vers_num); 315 f_print(fout, 316 "extern %s_%s_nproc;\n", 317 locase(def->def_name), vers->vers_num); 318 } 319 puldefine(vers->vers_name, vers->vers_num); 320 321 /* 322 * Print out 2 definitions, one for ANSI-C, another for 323 * old K & R C 324 */ 325 326 if(!Cflag){ 327 ext = "extern "; 328 for (proc = vers->procs; proc != NULL; 329 proc = proc->next) { 330 if (!define_printed(proc, 331 def->def.pr.versions)) { 332 puldefine(proc->proc_name, 333 proc->proc_num); 334 } 335 f_print(fout, "%s", ext); 336 pprocdef(proc, vers, NULL, 0, 2); 337 338 if (mtflag) { 339 f_print(fout, "%s", ext); 340 pprocdef(proc, vers, NULL, 1, 2); 341 } 342 } 343 pfreeprocdef(def->def_name, vers->vers_num, 2); 344 345 } else { 346 for (i = 1; i < 3; i++){ 347 if (i == 1){ 348 f_print(fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n"); 349 ext = "extern "; 350 }else{ 351 f_print(fout, "\n#else /* K&R C */\n"); 352 ext = "extern "; 353 } 354 355 for (proc = vers->procs; proc != NULL; 356 proc = proc->next) { 357 if (!define_printed(proc, 358 def->def.pr.versions)) { 359 puldefine(proc->proc_name, 360 proc->proc_num); 361 } 362 f_print(fout, "%s", ext); 363 pprocdef(proc, vers, "CLIENT *", 0, i); 364 f_print(fout, "%s", ext); 365 pprocdef(proc, vers, "struct svc_req *", 1, i); 366 } 367 pfreeprocdef(def->def_name, vers->vers_num, i); 368 } 369 f_print(fout, "#endif /* K&R C */\n"); 370 } 371 } 372} 373 374pprocdef(proc, vp, addargtype, server_p, mode) 375 proc_list *proc; 376 version_list *vp; 377 char* addargtype; 378 int server_p; 379 int mode; 380{ 381 if (mtflag) {/* Print MT style stubs */ 382 if (server_p) 383 f_print(fout, "bool_t "); 384 else 385 f_print(fout, "enum clnt_stat "); 386 } else { 387 ptype(proc->res_prefix, proc->res_type, 1); 388 f_print(fout, "* "); 389 } 390 if (server_p) 391 pvname_svc(proc->proc_name, vp->vers_num); 392 else 393 pvname(proc->proc_name, vp->vers_num); 394 395 /* 396 * mode 1 = ANSI-C, mode 2 = K&R C 397 */ 398 if ( mode == 1) 399 parglist(proc, addargtype); 400 else 401 f_print(fout, "();\n"); 402 403 404 405} 406 407 408 409/* print out argument list of procedure */ 410static 411parglist(proc, addargtype) 412 proc_list *proc; 413 char* addargtype; 414{ 415 decl_list *dl; 416 417 f_print(fout, "("); 418 if (proc->arg_num < 2 && newstyle && 419 streq(proc->args.decls->decl.type, "void")) { 420 /* 0 argument in new style: do nothing*/ 421 } 422 else { 423 for (dl = proc->args.decls; dl != NULL; dl = dl->next) { 424 ptype(dl->decl.prefix, dl->decl.type, 1); 425 if (!newstyle) 426 f_print(fout, "*"); 427 /* old style passes by reference */ 428 f_print(fout, ", "); 429 } 430 } 431 432 if (mtflag) { 433 ptype(proc->res_prefix, proc->res_type, 1); 434 f_print(fout, "*, "); 435 } 436 437 f_print(fout, "%s);\n", addargtype); 438 439} 440 441static 442penumdef(def) 443 definition *def; 444{ 445 char *name = def->def_name; 446 enumval_list *l; 447 char *last = NULL; 448 int count = 0; 449 450 f_print(fout, "enum %s {\n", name); 451 for (l = def->def.en.vals; l != NULL; l = l->next) { 452 f_print(fout, "\t%s", l->name); 453 if (l->assignment) { 454 f_print(fout, " = %s", l->assignment); 455 last = l->assignment; 456 count = 1; 457 } else { 458 if (last == NULL) { 459 f_print(fout, " = %d", count++); 460 } else { 461 f_print(fout, " = %s + %d", last, count++); 462 } 463 } 464 if (l->next) 465 f_print(fout, ",\n"); 466 else 467 f_print(fout, "\n"); 468 } 469 f_print(fout, "};\n"); 470 f_print(fout, "typedef enum %s %s;\n", name, name); 471} 472 473static 474ptypedef(def) 475 definition *def; 476{ 477 char *name = def->def_name; 478 char *old = def->def.ty.old_type; 479 char prefix[8]; /* enough to contain "struct ", including NUL */ 480 relation rel = def->def.ty.rel; 481 482 483 if (!streq(name, old)) { 484 if (streq(old, "string")) { 485 old = "char"; 486 rel = REL_POINTER; 487 } else if (streq(old, "opaque")) { 488 old = "char"; 489 } else if (streq(old, "bool")) { 490 old = "bool_t"; 491 } 492 if (undefined2(old, name) && def->def.ty.old_prefix) { 493 s_print(prefix, "%s ", def->def.ty.old_prefix); 494 } else { 495 prefix[0] = 0; 496 } 497 f_print(fout, "typedef "); 498 switch (rel) { 499 case REL_ARRAY: 500 f_print(fout, "struct {\n"); 501 f_print(fout, "\tu_int %s_len;\n", name); 502 f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name); 503 f_print(fout, "} %s", name); 504 break; 505 case REL_POINTER: 506 f_print(fout, "%s%s *%s", prefix, old, name); 507 break; 508 case REL_VECTOR: 509 f_print(fout, "%s%s %s[%s]", prefix, old, name, 510 def->def.ty.array_max); 511 break; 512 case REL_ALIAS: 513 f_print(fout, "%s%s %s", prefix, old, name); 514 break; 515 } 516 f_print(fout, ";\n"); 517 } 518} 519 520pdeclaration(name, dec, tab, separator) 521 char *name; 522 declaration *dec; 523 int tab; 524 char *separator; 525{ 526 char buf[8]; /* enough to hold "struct ", include NUL */ 527 char *prefix; 528 char *type; 529 530 if (streq(dec->type, "void")) { 531 return; 532 } 533 tabify(fout, tab); 534 if (streq(dec->type, name) && !dec->prefix) { 535 f_print(fout, "struct "); 536 } 537 if (streq(dec->type, "string")) { 538 f_print(fout, "char *%s", dec->name); 539 } else { 540 prefix = ""; 541 if (streq(dec->type, "bool")) { 542 type = "bool_t"; 543 } else if (streq(dec->type, "opaque")) { 544 type = "char"; 545 } else { 546 if (dec->prefix) { 547 s_print(buf, "%s ", dec->prefix); 548 prefix = buf; 549 } 550 type = dec->type; 551 } 552 switch (dec->rel) { 553 case REL_ALIAS: 554 f_print(fout, "%s%s %s", prefix, type, dec->name); 555 break; 556 case REL_VECTOR: 557 f_print(fout, "%s%s %s[%s]", prefix, type, dec->name, 558 dec->array_max); 559 break; 560 case REL_POINTER: 561 f_print(fout, "%s%s *%s", prefix, type, dec->name); 562 break; 563 case REL_ARRAY: 564 f_print(fout, "struct {\n"); 565 tabify(fout, tab); 566 f_print(fout, "\tu_int %s_len;\n", dec->name); 567 tabify(fout, tab); 568 f_print(fout, 569 "\t%s%s *%s_val;\n", prefix, type, dec->name); 570 tabify(fout, tab); 571 f_print(fout, "} %s", dec->name); 572 break; 573 } 574 } 575 f_print(fout, separator); 576} 577 578static 579undefined2(type, stop) 580 char *type; 581 char *stop; 582{ 583 list *l; 584 definition *def; 585 586 for (l = defined; l != NULL; l = l->next) { 587 def = (definition *) l->val; 588 if (def->def_kind != DEF_PROGRAM) { 589 if (streq(def->def_name, stop)) { 590 return (1); 591 } else if (streq(def->def_name, type)) { 592 return (0); 593 } 594 } 595 } 596 return (1); 597} 598