1 /* $OpenBSD: rpc_hout.c,v 1.17 2007/10/03 14:35:48 weingart Exp $ */ 2 /* $NetBSD: rpc_hout.c,v 1.4 1995/06/11 21:49:55 pk Exp $ */ 3 /* 4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 5 * unrestricted use provided that this legend is included on all tape 6 * media and as a part of the software program in whole or part. Users 7 * may copy or modify Sun RPC without charge, but are not authorized 8 * to license or distribute it to anyone else except as part of a product or 9 * program developed by the user or with the express written consent of 10 * Sun Microsystems, Inc. 11 * 12 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 13 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 14 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 15 * 16 * Sun RPC is provided with no support and without any obligation on the 17 * part of Sun Microsystems, Inc. to assist in its use, correction, 18 * modification or enhancement. 19 * 20 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 21 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 22 * OR ANY PART THEREOF. 23 * 24 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 25 * or profits or other special, indirect and consequential damages, even if 26 * Sun has been advised of the possibility of such damages. 27 * 28 * Sun Microsystems, Inc. 29 * 2550 Garcia Avenue 30 * Mountain View, California 94043 31 */ 32 33 #ifndef lint 34 static const 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 */ 40 #include <sys/cdefs.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <ctype.h> 44 #include "rpc_parse.h" 45 #include "rpc_util.h" 46 47 static void pconstdef(definition *); 48 static void pargdef(definition *); 49 static void pstructdef(definition *); 50 static void puniondef(definition *); 51 static void pprogramdef(definition *); 52 static void penumdef(definition *); 53 static void ptypedef(definition *); 54 static void pdefine(char *, char *); 55 static void puldefine(char *, char *); 56 static int define_printed(proc_list *, version_list *); 57 static int undefined2(char *, char *); 58 static void parglist(proc_list *, char *); 59 void pxdrfuncdecl(char *, int); 60 void pprocdef(proc_list *, version_list *, char *, int, int); 61 void pdeclaration(char *, declaration *, int, char *); 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 fprintf(fout, "\n"); 76 switch (def->def_kind) { 77 case DEF_STRUCT: 78 pstructdef(def); 79 break; 80 case DEF_UNION: 81 puniondef(def); 82 break; 83 case DEF_ENUM: 84 penumdef(def); 85 break; 86 case DEF_TYPEDEF: 87 ptypedef(def); 88 break; 89 case DEF_PROGRAM: 90 pprogramdef(def); 91 break; 92 case DEF_CONST: 93 pconstdef(def); 94 break; 95 } 96 if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) { 97 pxdrfuncdecl(def->def_name, 98 def->def_kind != DEF_TYPEDEF || 99 !isvectordef(def->def.ty.old_type, def->def.ty.rel)); 100 } 101 } 102 103 104 void 105 print_funcdef(def) 106 definition *def; 107 { 108 switch (def->def_kind) { 109 case DEF_PROGRAM: 110 fprintf(fout, "\n"); 111 pprogramdef(def); 112 break; 113 } 114 } 115 116 void 117 pxdrfuncdecl(name, pointerp) 118 char *name; 119 int pointerp; 120 { 121 122 fprintf(fout,"#ifdef __cplusplus\n"); 123 fprintf(fout, "extern \"C\" bool_t xdr_%s(XDR *, %s %s);\n", 124 name, name, pointerp ? ("*") : ""); 125 fprintf(fout,"#elif defined(__STDC__)\n"); 126 fprintf(fout, "extern bool_t xdr_%s(XDR *, %s %s);\n", 127 name, name, pointerp ? ("*") : ""); 128 fprintf(fout,"#else /* Old Style C */\n"); 129 fprintf(fout, "bool_t xdr_%s();\n", name); 130 fprintf(fout,"#endif /* Old Style C */\n\n"); 131 } 132 133 134 static void 135 pconstdef(def) 136 definition *def; 137 { 138 pdefine(def->def_name, def->def.co); 139 } 140 141 /* 142 * print out the definitions for the arguments of functions in the 143 * header file 144 */ 145 static void 146 pargdef(def) 147 definition *def; 148 { 149 decl_list *l; 150 version_list *vers; 151 char *name; 152 proc_list *plist; 153 154 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) { 155 for (plist = vers->procs; plist != NULL; 156 plist = plist->next) { 157 if (!newstyle || plist->arg_num < 2) { 158 continue; /* old style or single args */ 159 } 160 name = plist->args.argname; 161 fprintf(fout, "struct %s {\n", name); 162 for (l = plist->args.decls; 163 l != NULL; l = l->next) { 164 pdeclaration(name, &l->decl, 1, ";\n"); 165 } 166 fprintf(fout, "};\n"); 167 fprintf(fout, "typedef struct %s %s;\n", name, name); 168 pxdrfuncdecl(name, NULL); 169 fprintf(fout, "\n"); 170 } 171 } 172 } 173 174 static void 175 pstructdef(def) 176 definition *def; 177 { 178 char *name = def->def_name; 179 decl_list *l; 180 181 fprintf(fout, "struct %s {\n", name); 182 for (l = def->def.st.decls; l != NULL; l = l->next) 183 pdeclaration(name, &l->decl, 1, ";\n"); 184 fprintf(fout, "};\n"); 185 fprintf(fout, "typedef struct %s %s;\n", name, name); 186 } 187 188 static void 189 puniondef(def) 190 definition *def; 191 { 192 case_list *l; 193 char *name = def->def_name; 194 declaration *decl; 195 196 fprintf(fout, "struct %s {\n", name); 197 decl = &def->def.un.enum_decl; 198 if (streq(decl->type, "bool")) { 199 fprintf(fout, "\tbool_t %s;\n", decl->name); 200 } else { 201 fprintf(fout, "\t%s %s;\n", decl->type, decl->name); 202 } 203 fprintf(fout, "\tunion {\n"); 204 for (l = def->def.un.cases; l != NULL; l = l->next) { 205 if (l->contflag == 0) 206 pdeclaration(name, &l->case_decl, 2, ";\n"); 207 } 208 decl = def->def.un.default_decl; 209 if (decl && !streq(decl->type, "void")) { 210 pdeclaration(name, decl, 2, ";\n"); 211 } 212 fprintf(fout, "\t} %s_u;\n", name); 213 fprintf(fout, "};\n"); 214 fprintf(fout, "typedef struct %s %s;\n", name, name); 215 } 216 217 static void 218 pdefine(name, num) 219 char *name; 220 char *num; 221 { 222 fprintf(fout, "#define %s %s\n", name, num); 223 } 224 225 static void 226 puldefine(name, num) 227 char *name; 228 char *num; 229 { 230 fprintf(fout, "#define %s ((u_long)%s)\n", name, num); 231 } 232 233 static int 234 define_printed(stop, start) 235 proc_list *stop; 236 version_list *start; 237 { 238 version_list *vers; 239 proc_list *proc; 240 241 for (vers = start; vers != NULL; vers = vers->next) { 242 for (proc = vers->procs; proc != NULL; proc = proc->next) { 243 if (proc == stop) { 244 return (0); 245 } else if (streq(proc->proc_name, stop->proc_name)) { 246 return (1); 247 } 248 } 249 } 250 abort(); 251 /* NOTREACHED */ 252 } 253 254 static void 255 pprogramdef(def) 256 definition *def; 257 { 258 version_list *vers; 259 proc_list *proc; 260 int i; 261 char *ext; 262 263 pargdef(def); 264 265 puldefine(def->def_name, def->def.pr.prog_num); 266 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) { 267 if (tblflag) { 268 fprintf(fout, "extern struct rpcgen_table %s_%s_table[];\n", 269 locase(def->def_name), vers->vers_num); 270 fprintf(fout, "extern %s_%s_nproc;\n", 271 locase(def->def_name), vers->vers_num); 272 } 273 puldefine(vers->vers_name, vers->vers_num); 274 275 /* 276 * Print out 3 definitions, one for ANSI-C, another for C++, 277 * a third for old style C 278 */ 279 for (i=0; i<3; i++) { 280 if (i==0) { 281 fprintf(fout,"\n#ifdef __cplusplus\n"); 282 ext = "extern \"C\" "; 283 } else if (i==1) { 284 fprintf(fout,"\n#elif defined(__STDC__)\n"); 285 ext = "extern "; 286 } else { 287 fprintf(fout,"\n#else /* Old Style C */\n"); 288 ext = "extern "; 289 } 290 291 for (proc = vers->procs; proc != NULL; proc = proc->next) { 292 if (!define_printed(proc, def->def.pr.versions)) 293 puldefine(proc->proc_name, proc->proc_num); 294 fprintf(fout,"%s",ext); 295 pprocdef(proc, vers, "CLIENT *", 0,i); 296 fprintf(fout,"%s",ext); 297 pprocdef(proc, vers, "struct svc_req *", 1,i); 298 } 299 } 300 fprintf(fout,"#endif /* Old Style C */\n"); 301 } 302 } 303 304 void 305 pprocdef(proc, vp, addargtype, server_p,mode) 306 proc_list *proc; 307 version_list *vp; 308 char *addargtype; 309 int server_p; 310 int mode; 311 { 312 313 ptype(proc->res_prefix, proc->res_type, 1); 314 fprintf(fout, "* "); 315 if (server_p) 316 pvname_svc(proc->proc_name, vp->vers_num); 317 else 318 pvname(proc->proc_name, vp->vers_num); 319 320 /* 321 * mode 0 == cplusplus, mode 1 = ANSI-C, mode 2 = old style C 322 */ 323 if (mode == 0 || mode == 1) 324 parglist(proc, addargtype); 325 else 326 fprintf(fout, "();\n"); 327 } 328 329 /* print out argument list of procedure */ 330 static void 331 parglist(proc, addargtype) 332 proc_list *proc; 333 char *addargtype; 334 { 335 decl_list *dl; 336 337 fprintf(fout,"("); 338 339 if (proc->arg_num < 2 && newstyle && 340 streq(proc->args.decls->decl.type, "void")) { 341 /* 0 argument in new style: do nothing */ 342 } else { 343 for (dl = proc->args.decls; dl != NULL; dl = dl->next) { 344 ptype(dl->decl.prefix, dl->decl.type, 1); 345 if (!newstyle) 346 fprintf(fout, "*"); /* old style passes by reference */ 347 fprintf(fout, ", "); 348 } 349 } 350 fprintf(fout, "%s);\n", addargtype); 351 } 352 353 static void 354 penumdef(def) 355 definition *def; 356 { 357 char *name = def->def_name; 358 enumval_list *l; 359 char *last = NULL; 360 int count = 0; 361 362 fprintf(fout, "enum %s {\n", name); 363 for (l = def->def.en.vals; l != NULL; l = l->next) { 364 fprintf(fout, "\t%s", l->name); 365 if (l->assignment) { 366 fprintf(fout, " = %s", l->assignment); 367 last = l->assignment; 368 count = 1; 369 } else { 370 if (last == NULL) { 371 fprintf(fout, " = %d", count++); 372 } else { 373 fprintf(fout, " = %s + %d", last, count++); 374 } 375 } 376 if (l->next) 377 fprintf(fout, ",\n"); 378 else 379 fprintf(fout, "\n"); 380 } 381 fprintf(fout, "};\n"); 382 fprintf(fout, "typedef enum %s %s;\n", name, name); 383 } 384 385 static void 386 ptypedef(def) 387 definition *def; 388 { 389 char *name = def->def_name; 390 char *old = def->def.ty.old_type; 391 char prefix[8]; /* enough to contain "struct ", including NUL */ 392 relation rel = def->def.ty.rel; 393 394 if (!streq(name, old)) { 395 if (streq(old, "string")) { 396 old = "char"; 397 rel = REL_POINTER; 398 } else if (streq(old, "opaque")) { 399 old = "char"; 400 } else if (streq(old, "bool")) { 401 old = "bool_t"; 402 } 403 if (undefined2(old, name) && def->def.ty.old_prefix) { 404 snprintf(prefix, sizeof prefix, "%s ", def->def.ty.old_prefix); 405 } else { 406 prefix[0] = 0; 407 } 408 fprintf(fout, "typedef "); 409 switch (rel) { 410 case REL_ARRAY: 411 fprintf(fout, "struct {\n"); 412 fprintf(fout, "\tu_int %s_len;\n", name); 413 fprintf(fout, "\t%s%s *%s_val;\n", prefix, old, name); 414 fprintf(fout, "} %s", name); 415 break; 416 case REL_POINTER: 417 fprintf(fout, "%s%s *%s", prefix, old, name); 418 break; 419 case REL_VECTOR: 420 fprintf(fout, "%s%s %s[%s]", prefix, old, name, 421 def->def.ty.array_max); 422 break; 423 case REL_ALIAS: 424 fprintf(fout, "%s%s %s", prefix, old, name); 425 break; 426 } 427 fprintf(fout, ";\n"); 428 } 429 } 430 431 void 432 pdeclaration(name, dec, tab, separator) 433 char *name; 434 declaration *dec; 435 int tab; 436 char *separator; 437 { 438 char buf[8]; /* enough to hold "struct ", include NUL */ 439 char *prefix; 440 char *type; 441 442 if (streq(dec->type, "void")) 443 return; 444 tabify(fout, tab); 445 if (streq(dec->type, name) && !dec->prefix) { 446 fprintf(fout, "struct "); 447 } 448 if (streq(dec->type, "string")) { 449 fprintf(fout, "char *%s", dec->name); 450 } else { 451 prefix = ""; 452 if (streq(dec->type, "bool")) { 453 type = "bool_t"; 454 } else if (streq(dec->type, "opaque")) { 455 type = "char"; 456 } else { 457 if (dec->prefix) { 458 snprintf(buf, sizeof buf, "%s ", dec->prefix); 459 prefix = buf; 460 } 461 type = dec->type; 462 } 463 switch (dec->rel) { 464 case REL_ALIAS: 465 fprintf(fout, "%s%s %s", prefix, type, dec->name); 466 break; 467 case REL_VECTOR: 468 fprintf(fout, "%s%s %s[%s]", prefix, type, dec->name, 469 dec->array_max); 470 break; 471 case REL_POINTER: 472 fprintf(fout, "%s%s *%s", prefix, type, dec->name); 473 break; 474 case REL_ARRAY: 475 fprintf(fout, "struct {\n"); 476 tabify(fout, tab); 477 fprintf(fout, "\tu_int %s_len;\n", dec->name); 478 tabify(fout, tab); 479 fprintf(fout, "\t%s%s *%s_val;\n", prefix, type, dec->name); 480 tabify(fout, tab); 481 fprintf(fout, "} %s", dec->name); 482 break; 483 } 484 } 485 fprintf(fout, "%s", separator); 486 } 487 488 static int 489 undefined2(type, stop) 490 char *type; 491 char *stop; 492 { 493 list *l; 494 definition *def; 495 496 for (l = defined; l != NULL; l = l->next) { 497 def = (definition *) l->val; 498 if (def->def_kind != DEF_PROGRAM) { 499 if (streq(def->def_name, stop)) { 500 return (1); 501 } else if (streq(def->def_name, type)) { 502 return (0); 503 } 504 } 505 } 506 return (1); 507 } 508