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