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