11897Swollman/* 21897Swollman * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 31897Swollman * unrestricted use provided that this legend is included on all tape 41897Swollman * media and as a part of the software program in whole or part. Users 51897Swollman * may copy or modify Sun RPC without charge, but are not authorized 61897Swollman * to license or distribute it to anyone else except as part of a product or 71897Swollman * program developed by the user. 8100441Scharnier * 91897Swollman * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 101897Swollman * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 111897Swollman * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 12100441Scharnier * 131897Swollman * Sun RPC is provided with no support and without any obligation on the 141897Swollman * part of Sun Microsystems, Inc. to assist in its use, correction, 151897Swollman * modification or enhancement. 16100441Scharnier * 171897Swollman * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 181897Swollman * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 191897Swollman * OR ANY PART THEREOF. 20100441Scharnier * 211897Swollman * In no event will Sun Microsystems, Inc. be liable for any lost revenue 221897Swollman * or profits or other special, indirect and consequential damages, even if 231897Swollman * Sun has been advised of the possibility of such damages. 24100441Scharnier * 251897Swollman * Sun Microsystems, Inc. 261897Swollman * 2550 Garcia Avenue 271897Swollman * Mountain View, California 94043 281897Swollman */ 2912798Swpaul 30100441Scharnier#if 0 311897Swollman#ifndef lint 32146833Sstefanf#ident "@(#)rpc_hout.c 1.16 94/04/25 SMI" 3312798Swpaulstatic char sccsid[] = "@(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI"; 341897Swollman#endif 3599979Salfred#endif 361897Swollman 37100441Scharnier#include <sys/cdefs.h> 38100441Scharnier__FBSDID("$FreeBSD$"); 39100441Scharnier 401897Swollman/* 418874Srgrimes * rpc_hout.c, Header file outputter for the RPC protocol compiler 428874Srgrimes * Copyright (C) 1987, Sun Microsystems, Inc. 431897Swollman */ 441897Swollman#include <stdio.h> 45200462Sdelphij#include <ctype.h> 4612798Swpaul#include "rpc_parse.h" 47149682Sstefanf#include "rpc_scan.h" 481897Swollman#include "rpc_util.h" 491897Swollman 50152398Sdwmalonevoid storexdrfuncdecl(const char *, int ); 5192921Simpstatic void pconstdef( definition * ); 5292921Simpstatic void pstructdef( definition * ); 5392921Simpstatic void puniondef( definition * ); 54149695Sstefanfstatic void pprogramdef( definition *, int ); 5592921Simpstatic void penumdef( definition * ); 5692921Simpstatic void ptypedef( definition * ); 57152398Sdwmalonestatic void pdefine(const char *, const char *); 58152398Sdwmalonestatic int undefined2(const char *, const char *); 59152398Sdwmalonestatic void parglist(proc_list *, const char *); 60152398Sdwmalonestatic void pprocdef(proc_list *, version_list *, const char *, int); 611897Swollman 621897Swollman/* 638874Srgrimes * Print the C-version of an xdr definition 641897Swollman */ 651897Swollmanvoid 66149695Sstefanfprint_datadef(definition *def, int headeronly) 671897Swollman{ 6812798Swpaul 6912798Swpaul if (def->def_kind == DEF_PROGRAM) /* handle data only */ 7012798Swpaul return; 7112798Swpaul 721897Swollman if (def->def_kind != DEF_CONST) { 731897Swollman f_print(fout, "\n"); 741897Swollman } 751897Swollman switch (def->def_kind) { 761897Swollman case DEF_STRUCT: 771897Swollman pstructdef(def); 781897Swollman break; 791897Swollman case DEF_UNION: 801897Swollman puniondef(def); 811897Swollman break; 821897Swollman case DEF_ENUM: 831897Swollman penumdef(def); 841897Swollman break; 851897Swollman case DEF_TYPEDEF: 861897Swollman ptypedef(def); 871897Swollman break; 881897Swollman case DEF_PROGRAM: 89149695Sstefanf pprogramdef(def, headeronly); 901897Swollman break; 911897Swollman case DEF_CONST: 921897Swollman pconstdef(def); 931897Swollman break; 941897Swollman } 951897Swollman if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) { 9612798Swpaul storexdrfuncdecl(def->def_name, 9712798Swpaul def->def_kind != DEF_TYPEDEF || 9812798Swpaul !isvectordef(def->def.ty.old_type, 9912798Swpaul def->def.ty.rel)); 1001897Swollman } 10112798Swpaul} 10212798Swpaul 10312798Swpaul 10412798Swpaulvoid 105149695Sstefanfprint_funcdef(definition *def, int headeronly) 10612798Swpaul{ 10712798Swpaul switch (def->def_kind) { 10812798Swpaul case DEF_PROGRAM: 1091897Swollman f_print(fout, "\n"); 110149695Sstefanf pprogramdef(def, headeronly); 11112798Swpaul break; 11217142Sjkh default: 11399979Salfred break; 11417142Sjkh } 11512798Swpaul} 11612798Swpaul 11712798Swpaul/* store away enough information to allow the XDR functions to be spat 11812798Swpaul out at the end of the file */ 11912798Swpaul 12012798Swpaulvoid 121152398Sdwmalonestorexdrfuncdecl(const char *name, int pointerp) 12212798Swpaul{ 12312798Swpaul xdrfunc * xdrptr; 12412798Swpaul 125100441Scharnier xdrptr = XALLOC(struct xdrfunc); 12612798Swpaul 12712798Swpaul xdrptr->name = name; 12812798Swpaul xdrptr->pointerp = pointerp; 12912798Swpaul xdrptr->next = NULL; 13012798Swpaul 13112798Swpaul if (xdrfunc_tail == NULL){ 13212798Swpaul xdrfunc_head = xdrptr; 13312798Swpaul xdrfunc_tail = xdrptr; 13412798Swpaul } else { 13512798Swpaul xdrfunc_tail->next = xdrptr; 13612798Swpaul xdrfunc_tail = xdrptr; 1371897Swollman } 13812798Swpaul 13912798Swpaul 1401897Swollman} 1411897Swollman 14212798Swpaulvoid 143152398Sdwmaloneprint_xdr_func_def(const char *name, int pointerp) 14412798Swpaul{ 145149709Sstefanf f_print(fout, "extern bool_t xdr_%s(XDR *, %s%s);\n", name, 146149709Sstefanf name, pointerp ? "*" : ""); 14712798Swpaul} 14812798Swpaul 14912798Swpaul 15017142Sjkhstatic void 151201382Sedpconstdef(definition *def) 1521897Swollman{ 1531897Swollman pdefine(def->def_name, def->def.co); 1541897Swollman} 1551897Swollman 15612798Swpaul/* print out the definitions for the arguments of functions in the 15712798Swpaul header file 15812798Swpaul*/ 15917142Sjkhstatic void 160152398Sdwmalonepargdef(definition *def) 16112798Swpaul{ 16212798Swpaul decl_list *l; 16312798Swpaul version_list *vers; 16412798Swpaul char *name; 16512798Swpaul proc_list *plist; 16612798Swpaul 16712798Swpaul 16812798Swpaul for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) { 16912798Swpaul for (plist = vers->procs; plist != NULL; 17012798Swpaul plist = plist->next) { 17112798Swpaul 17212798Swpaul if (!newstyle || plist->arg_num < 2) { 17312798Swpaul continue; /* old style or single args */ 17412798Swpaul } 17512798Swpaul name = plist->args.argname; 17612798Swpaul f_print(fout, "struct %s {\n", name); 17712798Swpaul for (l = plist->args.decls; 17812798Swpaul l != NULL; l = l->next) { 17912798Swpaul pdeclaration(name, &l->decl, 1, 18012798Swpaul ";\n"); 18112798Swpaul } 18212798Swpaul f_print(fout, "};\n"); 18312798Swpaul f_print(fout, "typedef struct %s %s;\n", 18412798Swpaul name, name); 18512798Swpaul storexdrfuncdecl(name, 1); 18612798Swpaul f_print(fout, "\n"); 18712798Swpaul } 18812798Swpaul } 18912798Swpaul} 19012798Swpaul 19112798Swpaul 19217142Sjkhstatic void 193152398Sdwmalonepstructdef(definition *def) 1941897Swollman{ 1951897Swollman decl_list *l; 196152398Sdwmalone const char *name = def->def_name; 1971897Swollman 1981897Swollman f_print(fout, "struct %s {\n", name); 1991897Swollman for (l = def->def.st.decls; l != NULL; l = l->next) { 20012798Swpaul pdeclaration(name, &l->decl, 1, ";\n"); 2011897Swollman } 2021897Swollman f_print(fout, "};\n"); 2031897Swollman f_print(fout, "typedef struct %s %s;\n", name, name); 2041897Swollman} 2051897Swollman 20617142Sjkhstatic void 207201382Sedpuniondef(definition *def) 2081897Swollman{ 2091897Swollman case_list *l; 210152398Sdwmalone const char *name = def->def_name; 2111897Swollman declaration *decl; 2121897Swollman 2131897Swollman f_print(fout, "struct %s {\n", name); 2141897Swollman decl = &def->def.un.enum_decl; 2151897Swollman if (streq(decl->type, "bool")) { 2161897Swollman f_print(fout, "\tbool_t %s;\n", decl->name); 2171897Swollman } else { 2181897Swollman f_print(fout, "\t%s %s;\n", decl->type, decl->name); 2191897Swollman } 2201897Swollman f_print(fout, "\tunion {\n"); 2211897Swollman for (l = def->def.un.cases; l != NULL; l = l->next) { 22212798Swpaul if (l->contflag == 0) 22312798Swpaul pdeclaration(name, &l->case_decl, 2, ";\n"); 2241897Swollman } 2251897Swollman decl = def->def.un.default_decl; 2261897Swollman if (decl && !streq(decl->type, "void")) { 22712798Swpaul pdeclaration(name, decl, 2, ";\n"); 2281897Swollman } 2291897Swollman f_print(fout, "\t} %s_u;\n", name); 2301897Swollman f_print(fout, "};\n"); 2311897Swollman f_print(fout, "typedef struct %s %s;\n", name, name); 2321897Swollman} 2331897Swollman 23417142Sjkhstatic void 235152398Sdwmalonepdefine(const char *name, const char *num) 2361897Swollman{ 23712798Swpaul f_print(fout, "#define\t%s %s\n", name, num); 2381897Swollman} 2391897Swollman 24017142Sjkhstatic void 241152398Sdwmalonepuldefine(const char *name, const char *num) 2421897Swollman{ 24312798Swpaul f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num); 2441897Swollman} 2451897Swollman 24617142Sjkhstatic int 247152398Sdwmalonedefine_printed(proc_list *stop, version_list *start) 2481897Swollman{ 2491897Swollman version_list *vers; 2501897Swollman proc_list *proc; 2511897Swollman 2521897Swollman for (vers = start; vers != NULL; vers = vers->next) { 2531897Swollman for (proc = vers->procs; proc != NULL; proc = proc->next) { 2541897Swollman if (proc == stop) { 2551897Swollman return (0); 2561897Swollman } else if (streq(proc->proc_name, stop->proc_name)) { 2571897Swollman return (1); 2581897Swollman } 2591897Swollman } 2601897Swollman } 2611897Swollman abort(); 2621897Swollman /* NOTREACHED */ 2631897Swollman} 2641897Swollman 26517142Sjkhstatic void 266152398Sdwmalonepfreeprocdef(const char * name, const char *vers) 26712798Swpaul{ 26812798Swpaul f_print(fout, "extern int "); 26912798Swpaul pvname(name, vers); 270149709Sstefanf f_print(fout, "_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n"); 27112798Swpaul} 27212798Swpaul 27317142Sjkhstatic void 274152398Sdwmalonepdispatch(const char * name, const char *vers) 27586318Salfred{ 27686318Salfred 27786318Salfred f_print(fout, "void "); 27886318Salfred pvname(name, vers); 279149710Sstefanf f_print(fout, "(struct svc_req *rqstp, SVCXPRT *transp);\n"); 28086318Salfred} 28186318Salfred 28286318Salfredstatic void 283149695Sstefanfpprogramdef(definition *def, int headeronly) 2841897Swollman{ 2851897Swollman version_list *vers; 2861897Swollman proc_list *proc; 287152398Sdwmalone const char *ext; 2881897Swollman 28912798Swpaul pargdef(def); 29012798Swpaul 2911897Swollman puldefine(def->def_name, def->def.pr.prog_num); 2921897Swollman for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) { 29312798Swpaul if (tblflag) { 29412798Swpaul f_print(fout, 29512798Swpaul "extern struct rpcgen_table %s_%s_table[];\n", 29612798Swpaul locase(def->def_name), vers->vers_num); 29712798Swpaul f_print(fout, 29812798Swpaul "extern %s_%s_nproc;\n", 29912798Swpaul locase(def->def_name), vers->vers_num); 30012798Swpaul } 3011897Swollman puldefine(vers->vers_name, vers->vers_num); 30212798Swpaul 303149709Sstefanf f_print(fout, "\n"); 304149709Sstefanf ext = "extern "; 305149709Sstefanf if (headeronly) { 306149709Sstefanf f_print(fout, "%s", ext); 307149709Sstefanf pdispatch(def->def_name, vers->vers_num); 308149709Sstefanf } 309149709Sstefanf for (proc = vers->procs; proc != NULL; proc = proc->next) { 310149709Sstefanf if (!define_printed(proc, def->def.pr.versions)) { 311149709Sstefanf puldefine(proc->proc_name, proc->proc_num); 312149695Sstefanf } 313149709Sstefanf f_print(fout, "%s", ext); 314149709Sstefanf pprocdef(proc, vers, "CLIENT *", 0); 315149709Sstefanf f_print(fout, "%s", ext); 316149709Sstefanf pprocdef(proc, vers, "struct svc_req *", 1); 3171897Swollman } 318149709Sstefanf pfreeprocdef(def->def_name, vers->vers_num); 3191897Swollman } 3201897Swollman} 3211897Swollman 32217142Sjkhstatic void 323152398Sdwmalonepprocdef(proc_list *proc, version_list *vp, const char *addargtype, int server_p) 3241897Swollman{ 32512798Swpaul if (mtflag) {/* Print MT style stubs */ 32612798Swpaul if (server_p) 32712798Swpaul f_print(fout, "bool_t "); 32812798Swpaul else 32912798Swpaul f_print(fout, "enum clnt_stat "); 33012798Swpaul } else { 33112798Swpaul ptype(proc->res_prefix, proc->res_type, 1); 33212798Swpaul f_print(fout, "* "); 33312798Swpaul } 33412798Swpaul if (server_p) 33512798Swpaul pvname_svc(proc->proc_name, vp->vers_num); 33612798Swpaul else 33712798Swpaul pvname(proc->proc_name, vp->vers_num); 33812798Swpaul 339149709Sstefanf parglist(proc, addargtype); 34012798Swpaul} 34112798Swpaul 34212798Swpaul 34312798Swpaul 34412798Swpaul/* print out argument list of procedure */ 34517142Sjkhstatic void 346152398Sdwmaloneparglist(proc_list *proc, const char *addargtype) 34712798Swpaul{ 34812798Swpaul decl_list *dl; 34912798Swpaul 35012798Swpaul f_print(fout, "("); 35112798Swpaul if (proc->arg_num < 2 && newstyle && 35212798Swpaul streq(proc->args.decls->decl.type, "void")) { 35312798Swpaul /* 0 argument in new style: do nothing*/ 35412798Swpaul } 35512798Swpaul else { 35612798Swpaul for (dl = proc->args.decls; dl != NULL; dl = dl->next) { 35712798Swpaul ptype(dl->decl.prefix, dl->decl.type, 1); 35812798Swpaul if (!newstyle) 35912798Swpaul f_print(fout, "*"); 36012798Swpaul /* old style passes by reference */ 36112798Swpaul f_print(fout, ", "); 3621897Swollman } 3631897Swollman } 36412798Swpaul 36512798Swpaul if (mtflag) { 36612798Swpaul ptype(proc->res_prefix, proc->res_type, 1); 36712798Swpaul f_print(fout, "*, "); 3681897Swollman } 36912798Swpaul 37012798Swpaul f_print(fout, "%s);\n", addargtype); 37112798Swpaul 3721897Swollman} 3731897Swollman 37417142Sjkhstatic void 375201382Sedpenumdef(definition *def) 3761897Swollman{ 377152398Sdwmalone const char *name = def->def_name; 3781897Swollman enumval_list *l; 379152398Sdwmalone const char *last = NULL; 3801897Swollman int count = 0; 3811897Swollman 3821897Swollman f_print(fout, "enum %s {\n", name); 3831897Swollman for (l = def->def.en.vals; l != NULL; l = l->next) { 3841897Swollman f_print(fout, "\t%s", l->name); 3851897Swollman if (l->assignment) { 3861897Swollman f_print(fout, " = %s", l->assignment); 3871897Swollman last = l->assignment; 3881897Swollman count = 1; 3891897Swollman } else { 3901897Swollman if (last == NULL) { 3911897Swollman f_print(fout, " = %d", count++); 3921897Swollman } else { 3931897Swollman f_print(fout, " = %s + %d", last, count++); 3941897Swollman } 3951897Swollman } 39612798Swpaul if (l->next) 39712798Swpaul f_print(fout, ",\n"); 39812798Swpaul else 39912798Swpaul f_print(fout, "\n"); 4001897Swollman } 4011897Swollman f_print(fout, "};\n"); 4021897Swollman f_print(fout, "typedef enum %s %s;\n", name, name); 4031897Swollman} 4041897Swollman 40517142Sjkhstatic void 406201382Sedptypedef(definition *def) 4071897Swollman{ 408152398Sdwmalone const char *name = def->def_name; 409152398Sdwmalone const char *old = def->def.ty.old_type; 4101897Swollman char prefix[8]; /* enough to contain "struct ", including NUL */ 4111897Swollman relation rel = def->def.ty.rel; 4121897Swollman 4131897Swollman 4141897Swollman if (!streq(name, old)) { 4151897Swollman if (streq(old, "string")) { 4161897Swollman old = "char"; 4171897Swollman rel = REL_POINTER; 4181897Swollman } else if (streq(old, "opaque")) { 4191897Swollman old = "char"; 4201897Swollman } else if (streq(old, "bool")) { 4211897Swollman old = "bool_t"; 4221897Swollman } 4231897Swollman if (undefined2(old, name) && def->def.ty.old_prefix) { 4241897Swollman s_print(prefix, "%s ", def->def.ty.old_prefix); 4251897Swollman } else { 4261897Swollman prefix[0] = 0; 4271897Swollman } 4281897Swollman f_print(fout, "typedef "); 4291897Swollman switch (rel) { 4301897Swollman case REL_ARRAY: 4311897Swollman f_print(fout, "struct {\n"); 4321897Swollman f_print(fout, "\tu_int %s_len;\n", name); 4331897Swollman f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name); 4341897Swollman f_print(fout, "} %s", name); 4351897Swollman break; 4361897Swollman case REL_POINTER: 4371897Swollman f_print(fout, "%s%s *%s", prefix, old, name); 4381897Swollman break; 4391897Swollman case REL_VECTOR: 4401897Swollman f_print(fout, "%s%s %s[%s]", prefix, old, name, 4411897Swollman def->def.ty.array_max); 4421897Swollman break; 4431897Swollman case REL_ALIAS: 44412798Swpaul f_print(fout, "%s%s %s", prefix, old, name); 4451897Swollman break; 4461897Swollman } 4471897Swollman f_print(fout, ";\n"); 4481897Swollman } 4491897Swollman} 4501897Swollman 45117142Sjkhvoid 452152398Sdwmalonepdeclaration(const char *name, declaration *dec, int tab, const char *separator) 4531897Swollman{ 4541897Swollman char buf[8]; /* enough to hold "struct ", include NUL */ 455152398Sdwmalone const char *prefix; 456152398Sdwmalone const char *type; 4571897Swollman 4581897Swollman if (streq(dec->type, "void")) { 4591897Swollman return; 4601897Swollman } 4611897Swollman tabify(fout, tab); 4621897Swollman if (streq(dec->type, name) && !dec->prefix) { 4631897Swollman f_print(fout, "struct "); 4641897Swollman } 4651897Swollman if (streq(dec->type, "string")) { 4661897Swollman f_print(fout, "char *%s", dec->name); 4671897Swollman } else { 4681897Swollman prefix = ""; 4691897Swollman if (streq(dec->type, "bool")) { 4701897Swollman type = "bool_t"; 4711897Swollman } else if (streq(dec->type, "opaque")) { 4721897Swollman type = "char"; 4731897Swollman } else { 4741897Swollman if (dec->prefix) { 4751897Swollman s_print(buf, "%s ", dec->prefix); 4761897Swollman prefix = buf; 4771897Swollman } 4781897Swollman type = dec->type; 4791897Swollman } 4801897Swollman switch (dec->rel) { 4811897Swollman case REL_ALIAS: 4821897Swollman f_print(fout, "%s%s %s", prefix, type, dec->name); 4831897Swollman break; 4841897Swollman case REL_VECTOR: 4851897Swollman f_print(fout, "%s%s %s[%s]", prefix, type, dec->name, 4861897Swollman dec->array_max); 4871897Swollman break; 4881897Swollman case REL_POINTER: 4891897Swollman f_print(fout, "%s%s *%s", prefix, type, dec->name); 4901897Swollman break; 4911897Swollman case REL_ARRAY: 4921897Swollman f_print(fout, "struct {\n"); 4931897Swollman tabify(fout, tab); 4941897Swollman f_print(fout, "\tu_int %s_len;\n", dec->name); 4951897Swollman tabify(fout, tab); 49612798Swpaul f_print(fout, 49712798Swpaul "\t%s%s *%s_val;\n", prefix, type, dec->name); 4981897Swollman tabify(fout, tab); 4991897Swollman f_print(fout, "} %s", dec->name); 5001897Swollman break; 5011897Swollman } 5021897Swollman } 503222080Sbenl fputs(separator, fout); 5041897Swollman} 5051897Swollman 50617142Sjkhstatic int 507152398Sdwmaloneundefined2(const char *type, const char *stop) 5081897Swollman{ 5091897Swollman list *l; 5101897Swollman definition *def; 5111897Swollman 5121897Swollman for (l = defined; l != NULL; l = l->next) { 5131897Swollman def = (definition *) l->val; 5141897Swollman if (def->def_kind != DEF_PROGRAM) { 5151897Swollman if (streq(def->def_name, stop)) { 5161897Swollman return (1); 5171897Swollman } else if (streq(def->def_name, type)) { 5181897Swollman return (0); 5191897Swollman } 5201897Swollman } 5211897Swollman } 5221897Swollman return (1); 5231897Swollman} 524