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_hout.c 1.16 94/04/25 SMI" 33 static char sccsid[] = "@(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI"; 34 #endif 35 #endif 36 37 #include <sys/cdefs.h> 38 /* 39 * rpc_hout.c, Header file outputter for the RPC protocol compiler 40 * Copyright (C) 1987, Sun Microsystems, Inc. 41 */ 42 #include <stdio.h> 43 #include <ctype.h> 44 #include "rpc_parse.h" 45 #include "rpc_scan.h" 46 #include "rpc_util.h" 47 48 void storexdrfuncdecl(const char *, int ); 49 static void pconstdef( definition * ); 50 static void pstructdef( definition * ); 51 static void puniondef( definition * ); 52 static void pprogramdef( definition *, int ); 53 static void penumdef( definition * ); 54 static void ptypedef( definition * ); 55 static void pdefine(const char *, const char *); 56 static int undefined2(const char *, const char *); 57 static void parglist(proc_list *, const char *); 58 static void pprocdef(proc_list *, version_list *, const char *, int); 59 60 /* 61 * Print the C-version of an xdr definition 62 */ 63 void 64 print_datadef(definition *def, int headeronly) 65 { 66 67 if (def->def_kind == DEF_PROGRAM) /* handle data only */ 68 return; 69 70 if (def->def_kind != DEF_CONST) { 71 f_print(fout, "\n"); 72 } 73 switch (def->def_kind) { 74 case DEF_STRUCT: 75 pstructdef(def); 76 break; 77 case DEF_UNION: 78 puniondef(def); 79 break; 80 case DEF_ENUM: 81 penumdef(def); 82 break; 83 case DEF_TYPEDEF: 84 ptypedef(def); 85 break; 86 case DEF_PROGRAM: 87 pprogramdef(def, headeronly); 88 break; 89 case DEF_CONST: 90 pconstdef(def); 91 break; 92 } 93 if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) { 94 storexdrfuncdecl(def->def_name, 95 def->def_kind != DEF_TYPEDEF || 96 !isvectordef(def->def.ty.old_type, 97 def->def.ty.rel)); 98 } 99 } 100 101 102 void 103 print_funcdef(definition *def, int headeronly) 104 { 105 switch (def->def_kind) { 106 case DEF_PROGRAM: 107 f_print(fout, "\n"); 108 pprogramdef(def, headeronly); 109 break; 110 default: 111 break; 112 } 113 } 114 115 /* store away enough information to allow the XDR functions to be spat 116 out at the end of the file */ 117 118 void 119 storexdrfuncdecl(const char *name, int pointerp) 120 { 121 xdrfunc * xdrptr; 122 123 xdrptr = XALLOC(struct xdrfunc); 124 125 xdrptr->name = name; 126 xdrptr->pointerp = pointerp; 127 xdrptr->next = NULL; 128 129 if (xdrfunc_tail == NULL){ 130 xdrfunc_head = xdrptr; 131 xdrfunc_tail = xdrptr; 132 } else { 133 xdrfunc_tail->next = xdrptr; 134 xdrfunc_tail = xdrptr; 135 } 136 137 138 } 139 140 void 141 print_xdr_func_def(const char *name, int pointerp) 142 { 143 f_print(fout, "extern bool_t xdr_%s(XDR *, %s%s);\n", name, 144 name, pointerp ? "*" : ""); 145 } 146 147 148 static void 149 pconstdef(definition *def) 150 { 151 pdefine(def->def_name, def->def.co); 152 } 153 154 /* print out the definitions for the arguments of functions in the 155 header file 156 */ 157 static void 158 pargdef(definition *def) 159 { 160 decl_list *l; 161 version_list *vers; 162 char *name; 163 proc_list *plist; 164 165 166 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) { 167 for (plist = vers->procs; plist != NULL; 168 plist = plist->next) { 169 170 if (!newstyle || plist->arg_num < 2) { 171 continue; /* old style or single args */ 172 } 173 name = plist->args.argname; 174 f_print(fout, "struct %s {\n", name); 175 for (l = plist->args.decls; 176 l != NULL; l = l->next) { 177 pdeclaration(name, &l->decl, 1, 178 ";\n"); 179 } 180 f_print(fout, "};\n"); 181 f_print(fout, "typedef struct %s %s;\n", 182 name, name); 183 storexdrfuncdecl(name, 1); 184 f_print(fout, "\n"); 185 } 186 } 187 } 188 189 190 static void 191 pstructdef(definition *def) 192 { 193 decl_list *l; 194 const char *name = def->def_name; 195 196 f_print(fout, "struct %s {\n", name); 197 for (l = def->def.st.decls; l != NULL; l = l->next) { 198 pdeclaration(name, &l->decl, 1, ";\n"); 199 } 200 f_print(fout, "};\n"); 201 f_print(fout, "typedef struct %s %s;\n", name, name); 202 } 203 204 static void 205 puniondef(definition *def) 206 { 207 case_list *l; 208 const char *name = def->def_name; 209 declaration *decl; 210 211 f_print(fout, "struct %s {\n", name); 212 decl = &def->def.un.enum_decl; 213 if (streq(decl->type, "bool")) { 214 f_print(fout, "\tbool_t %s;\n", decl->name); 215 } else { 216 f_print(fout, "\t%s %s;\n", decl->type, decl->name); 217 } 218 f_print(fout, "\tunion {\n"); 219 for (l = def->def.un.cases; l != NULL; l = l->next) { 220 if (l->contflag == 0) 221 pdeclaration(name, &l->case_decl, 2, ";\n"); 222 } 223 decl = def->def.un.default_decl; 224 if (decl && !streq(decl->type, "void")) { 225 pdeclaration(name, decl, 2, ";\n"); 226 } 227 f_print(fout, "\t} %s_u;\n", name); 228 f_print(fout, "};\n"); 229 f_print(fout, "typedef struct %s %s;\n", name, name); 230 } 231 232 static void 233 pdefine(const char *name, const char *num) 234 { 235 f_print(fout, "#define\t%s %s\n", name, num); 236 } 237 238 static void 239 puldefine(const char *name, const char *num) 240 { 241 f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num); 242 } 243 244 static int 245 define_printed(proc_list *stop, version_list *start) 246 { 247 version_list *vers; 248 proc_list *proc; 249 250 for (vers = start; vers != NULL; vers = vers->next) { 251 for (proc = vers->procs; proc != NULL; proc = proc->next) { 252 if (proc == stop) { 253 return (0); 254 } else if (streq(proc->proc_name, stop->proc_name)) { 255 return (1); 256 } 257 } 258 } 259 abort(); 260 /* NOTREACHED */ 261 } 262 263 static void 264 pfreeprocdef(const char * name, const char *vers) 265 { 266 f_print(fout, "extern int "); 267 pvname(name, vers); 268 f_print(fout, "_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n"); 269 } 270 271 static void 272 pdispatch(const char * name, const char *vers) 273 { 274 275 f_print(fout, "void "); 276 pvname(name, vers); 277 f_print(fout, "(struct svc_req *rqstp, SVCXPRT *transp);\n"); 278 } 279 280 static void 281 pprogramdef(definition *def, int headeronly) 282 { 283 version_list *vers; 284 proc_list *proc; 285 const char *ext; 286 287 pargdef(def); 288 289 puldefine(def->def_name, def->def.pr.prog_num); 290 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) { 291 if (tblflag) { 292 f_print(fout, 293 "extern struct rpcgen_table %s_%s_table[];\n", 294 locase(def->def_name), vers->vers_num); 295 f_print(fout, 296 "extern %s_%s_nproc;\n", 297 locase(def->def_name), vers->vers_num); 298 } 299 puldefine(vers->vers_name, vers->vers_num); 300 301 f_print(fout, "\n"); 302 ext = "extern "; 303 if (headeronly) { 304 f_print(fout, "%s", ext); 305 pdispatch(def->def_name, vers->vers_num); 306 } 307 for (proc = vers->procs; proc != NULL; proc = proc->next) { 308 if (!define_printed(proc, def->def.pr.versions)) { 309 puldefine(proc->proc_name, proc->proc_num); 310 } 311 f_print(fout, "%s", ext); 312 pprocdef(proc, vers, "CLIENT *", 0); 313 f_print(fout, "%s", ext); 314 pprocdef(proc, vers, "struct svc_req *", 1); 315 } 316 pfreeprocdef(def->def_name, vers->vers_num); 317 } 318 } 319 320 static void 321 pprocdef(proc_list *proc, version_list *vp, const char *addargtype, int server_p) 322 { 323 if (mtflag) {/* Print MT style stubs */ 324 if (server_p) 325 f_print(fout, "bool_t "); 326 else 327 f_print(fout, "enum clnt_stat "); 328 } else { 329 ptype(proc->res_prefix, proc->res_type, 1); 330 f_print(fout, "* "); 331 } 332 if (server_p) 333 pvname_svc(proc->proc_name, vp->vers_num); 334 else 335 pvname(proc->proc_name, vp->vers_num); 336 337 parglist(proc, addargtype); 338 } 339 340 341 342 /* print out argument list of procedure */ 343 static void 344 parglist(proc_list *proc, const char *addargtype) 345 { 346 decl_list *dl; 347 348 f_print(fout, "("); 349 if (proc->arg_num < 2 && newstyle && 350 streq(proc->args.decls->decl.type, "void")) { 351 /* 0 argument in new style: do nothing*/ 352 } 353 else { 354 for (dl = proc->args.decls; dl != NULL; dl = dl->next) { 355 ptype(dl->decl.prefix, dl->decl.type, 1); 356 if (!newstyle) 357 f_print(fout, "*"); 358 /* old style passes by reference */ 359 f_print(fout, ", "); 360 } 361 } 362 363 if (mtflag) { 364 ptype(proc->res_prefix, proc->res_type, 1); 365 f_print(fout, "*, "); 366 } 367 368 f_print(fout, "%s);\n", addargtype); 369 370 } 371 372 static void 373 penumdef(definition *def) 374 { 375 const char *name = def->def_name; 376 enumval_list *l; 377 const char *last = NULL; 378 int count = 0; 379 380 f_print(fout, "enum %s {\n", name); 381 for (l = def->def.en.vals; l != NULL; l = l->next) { 382 f_print(fout, "\t%s", l->name); 383 if (l->assignment) { 384 f_print(fout, " = %s", l->assignment); 385 last = l->assignment; 386 count = 1; 387 } else { 388 if (last == NULL) { 389 f_print(fout, " = %d", count++); 390 } else { 391 f_print(fout, " = %s + %d", last, count++); 392 } 393 } 394 if (l->next) 395 f_print(fout, ",\n"); 396 else 397 f_print(fout, "\n"); 398 } 399 f_print(fout, "};\n"); 400 f_print(fout, "typedef enum %s %s;\n", name, name); 401 } 402 403 static void 404 ptypedef(definition *def) 405 { 406 const char *name = def->def_name; 407 const char *old = def->def.ty.old_type; 408 char prefix[8]; /* enough to contain "struct ", including NUL */ 409 relation rel = def->def.ty.rel; 410 411 412 if (!streq(name, old)) { 413 if (streq(old, "string")) { 414 old = "char"; 415 rel = REL_POINTER; 416 } else if (streq(old, "opaque")) { 417 old = "char"; 418 } else if (streq(old, "bool")) { 419 old = "bool_t"; 420 } 421 if (undefined2(old, name) && def->def.ty.old_prefix) { 422 s_print(prefix, "%s ", def->def.ty.old_prefix); 423 } else { 424 prefix[0] = 0; 425 } 426 f_print(fout, "typedef "); 427 switch (rel) { 428 case REL_ARRAY: 429 f_print(fout, "struct {\n"); 430 f_print(fout, "\tu_int %s_len;\n", name); 431 f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name); 432 f_print(fout, "} %s", name); 433 break; 434 case REL_POINTER: 435 f_print(fout, "%s%s *%s", prefix, old, name); 436 break; 437 case REL_VECTOR: 438 f_print(fout, "%s%s %s[%s]", prefix, old, name, 439 def->def.ty.array_max); 440 break; 441 case REL_ALIAS: 442 f_print(fout, "%s%s %s", prefix, old, name); 443 break; 444 } 445 f_print(fout, ";\n"); 446 } 447 } 448 449 void 450 pdeclaration(const char *name, declaration *dec, int tab, const char *separator) 451 { 452 char buf[8]; /* enough to hold "struct ", include NUL */ 453 const char *prefix; 454 const char *type; 455 456 if (streq(dec->type, "void")) { 457 return; 458 } 459 tabify(fout, tab); 460 if (streq(dec->type, name) && !dec->prefix) { 461 f_print(fout, "struct "); 462 } 463 if (streq(dec->type, "string")) { 464 f_print(fout, "char *%s", dec->name); 465 } else { 466 prefix = ""; 467 if (streq(dec->type, "bool")) { 468 type = "bool_t"; 469 } else if (streq(dec->type, "opaque")) { 470 type = "char"; 471 } else { 472 if (dec->prefix) { 473 s_print(buf, "%s ", dec->prefix); 474 prefix = buf; 475 } 476 type = dec->type; 477 } 478 switch (dec->rel) { 479 case REL_ALIAS: 480 f_print(fout, "%s%s %s", prefix, type, dec->name); 481 break; 482 case REL_VECTOR: 483 f_print(fout, "%s%s %s[%s]", prefix, type, dec->name, 484 dec->array_max); 485 break; 486 case REL_POINTER: 487 f_print(fout, "%s%s *%s", prefix, type, dec->name); 488 break; 489 case REL_ARRAY: 490 f_print(fout, "struct {\n"); 491 tabify(fout, tab); 492 f_print(fout, "\tu_int %s_len;\n", dec->name); 493 tabify(fout, tab); 494 f_print(fout, 495 "\t%s%s *%s_val;\n", prefix, type, dec->name); 496 tabify(fout, tab); 497 f_print(fout, "} %s", dec->name); 498 break; 499 } 500 } 501 fputs(separator, fout); 502 } 503 504 static int 505 undefined2(const char *type, const char *stop) 506 { 507 list *l; 508 definition *def; 509 510 for (l = defined; l != NULL; l = l->next) { 511 def = (definition *) l->val; 512 if (def->def_kind != DEF_PROGRAM) { 513 if (streq(def->def_name, stop)) { 514 return (1); 515 } else if (streq(def->def_name, type)) { 516 return (0); 517 } 518 } 519 } 520 return (1); 521 } 522