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