rpc_hout.c revision 200462
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: head/usr.bin/rpcgen/rpc_hout.c 200462 2009-12-13 03:14:06Z delphij $"); 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 1511897Swollmanpconstdef(def) 1521897Swollman definition *def; 1531897Swollman{ 1541897Swollman pdefine(def->def_name, def->def.co); 1551897Swollman} 1561897Swollman 15712798Swpaul/* print out the definitions for the arguments of functions in the 15812798Swpaul header file 15912798Swpaul*/ 16017142Sjkhstatic void 161152398Sdwmalonepargdef(definition *def) 16212798Swpaul{ 16312798Swpaul decl_list *l; 16412798Swpaul version_list *vers; 16512798Swpaul char *name; 16612798Swpaul proc_list *plist; 16712798Swpaul 16812798Swpaul 16912798Swpaul for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) { 17012798Swpaul for (plist = vers->procs; plist != NULL; 17112798Swpaul plist = plist->next) { 17212798Swpaul 17312798Swpaul if (!newstyle || plist->arg_num < 2) { 17412798Swpaul continue; /* old style or single args */ 17512798Swpaul } 17612798Swpaul name = plist->args.argname; 17712798Swpaul f_print(fout, "struct %s {\n", name); 17812798Swpaul for (l = plist->args.decls; 17912798Swpaul l != NULL; l = l->next) { 18012798Swpaul pdeclaration(name, &l->decl, 1, 18112798Swpaul ";\n"); 18212798Swpaul } 18312798Swpaul f_print(fout, "};\n"); 18412798Swpaul f_print(fout, "typedef struct %s %s;\n", 18512798Swpaul name, name); 18612798Swpaul storexdrfuncdecl(name, 1); 18712798Swpaul f_print(fout, "\n"); 18812798Swpaul } 18912798Swpaul } 19012798Swpaul} 19112798Swpaul 19212798Swpaul 19317142Sjkhstatic void 194152398Sdwmalonepstructdef(definition *def) 1951897Swollman{ 1961897Swollman decl_list *l; 197152398Sdwmalone const char *name = def->def_name; 1981897Swollman 1991897Swollman f_print(fout, "struct %s {\n", name); 2001897Swollman for (l = def->def.st.decls; l != NULL; l = l->next) { 20112798Swpaul pdeclaration(name, &l->decl, 1, ";\n"); 2021897Swollman } 2031897Swollman f_print(fout, "};\n"); 2041897Swollman f_print(fout, "typedef struct %s %s;\n", name, name); 2051897Swollman} 2061897Swollman 20717142Sjkhstatic void 2081897Swollmanpuniondef(def) 2091897Swollman definition *def; 2101897Swollman{ 2111897Swollman case_list *l; 212152398Sdwmalone const char *name = def->def_name; 2131897Swollman declaration *decl; 2141897Swollman 2151897Swollman f_print(fout, "struct %s {\n", name); 2161897Swollman decl = &def->def.un.enum_decl; 2171897Swollman if (streq(decl->type, "bool")) { 2181897Swollman f_print(fout, "\tbool_t %s;\n", decl->name); 2191897Swollman } else { 2201897Swollman f_print(fout, "\t%s %s;\n", decl->type, decl->name); 2211897Swollman } 2221897Swollman f_print(fout, "\tunion {\n"); 2231897Swollman for (l = def->def.un.cases; l != NULL; l = l->next) { 22412798Swpaul if (l->contflag == 0) 22512798Swpaul pdeclaration(name, &l->case_decl, 2, ";\n"); 2261897Swollman } 2271897Swollman decl = def->def.un.default_decl; 2281897Swollman if (decl && !streq(decl->type, "void")) { 22912798Swpaul pdeclaration(name, decl, 2, ";\n"); 2301897Swollman } 2311897Swollman f_print(fout, "\t} %s_u;\n", name); 2321897Swollman f_print(fout, "};\n"); 2331897Swollman f_print(fout, "typedef struct %s %s;\n", name, name); 2341897Swollman} 2351897Swollman 23617142Sjkhstatic void 237152398Sdwmalonepdefine(const char *name, const char *num) 2381897Swollman{ 23912798Swpaul f_print(fout, "#define\t%s %s\n", name, num); 2401897Swollman} 2411897Swollman 24217142Sjkhstatic void 243152398Sdwmalonepuldefine(const char *name, const char *num) 2441897Swollman{ 24512798Swpaul f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num); 2461897Swollman} 2471897Swollman 24817142Sjkhstatic int 249152398Sdwmalonedefine_printed(proc_list *stop, version_list *start) 2501897Swollman{ 2511897Swollman version_list *vers; 2521897Swollman proc_list *proc; 2531897Swollman 2541897Swollman for (vers = start; vers != NULL; vers = vers->next) { 2551897Swollman for (proc = vers->procs; proc != NULL; proc = proc->next) { 2561897Swollman if (proc == stop) { 2571897Swollman return (0); 2581897Swollman } else if (streq(proc->proc_name, stop->proc_name)) { 2591897Swollman return (1); 2601897Swollman } 2611897Swollman } 2621897Swollman } 2631897Swollman abort(); 2641897Swollman /* NOTREACHED */ 2651897Swollman} 2661897Swollman 26717142Sjkhstatic void 268152398Sdwmalonepfreeprocdef(const char * name, const char *vers) 26912798Swpaul{ 27012798Swpaul f_print(fout, "extern int "); 27112798Swpaul pvname(name, vers); 272149709Sstefanf f_print(fout, "_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n"); 27312798Swpaul} 27412798Swpaul 27517142Sjkhstatic void 276152398Sdwmalonepdispatch(const char * name, const char *vers) 27786318Salfred{ 27886318Salfred 27986318Salfred f_print(fout, "void "); 28086318Salfred pvname(name, vers); 281149710Sstefanf f_print(fout, "(struct svc_req *rqstp, SVCXPRT *transp);\n"); 28286318Salfred} 28386318Salfred 28486318Salfredstatic void 285149695Sstefanfpprogramdef(definition *def, int headeronly) 2861897Swollman{ 2871897Swollman version_list *vers; 2881897Swollman proc_list *proc; 289152398Sdwmalone const char *ext; 2901897Swollman 29112798Swpaul pargdef(def); 29212798Swpaul 2931897Swollman puldefine(def->def_name, def->def.pr.prog_num); 2941897Swollman for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) { 29512798Swpaul if (tblflag) { 29612798Swpaul f_print(fout, 29712798Swpaul "extern struct rpcgen_table %s_%s_table[];\n", 29812798Swpaul locase(def->def_name), vers->vers_num); 29912798Swpaul f_print(fout, 30012798Swpaul "extern %s_%s_nproc;\n", 30112798Swpaul locase(def->def_name), vers->vers_num); 30212798Swpaul } 3031897Swollman puldefine(vers->vers_name, vers->vers_num); 30412798Swpaul 305149709Sstefanf f_print(fout, "\n"); 306149709Sstefanf ext = "extern "; 307149709Sstefanf if (headeronly) { 308149709Sstefanf f_print(fout, "%s", ext); 309149709Sstefanf pdispatch(def->def_name, vers->vers_num); 310149709Sstefanf } 311149709Sstefanf for (proc = vers->procs; proc != NULL; proc = proc->next) { 312149709Sstefanf if (!define_printed(proc, def->def.pr.versions)) { 313149709Sstefanf puldefine(proc->proc_name, proc->proc_num); 314149695Sstefanf } 315149709Sstefanf f_print(fout, "%s", ext); 316149709Sstefanf pprocdef(proc, vers, "CLIENT *", 0); 317149709Sstefanf f_print(fout, "%s", ext); 318149709Sstefanf pprocdef(proc, vers, "struct svc_req *", 1); 3191897Swollman } 320149709Sstefanf pfreeprocdef(def->def_name, vers->vers_num); 3211897Swollman } 3221897Swollman} 3231897Swollman 32417142Sjkhstatic void 325152398Sdwmalonepprocdef(proc_list *proc, version_list *vp, const char *addargtype, int server_p) 3261897Swollman{ 32712798Swpaul if (mtflag) {/* Print MT style stubs */ 32812798Swpaul if (server_p) 32912798Swpaul f_print(fout, "bool_t "); 33012798Swpaul else 33112798Swpaul f_print(fout, "enum clnt_stat "); 33212798Swpaul } else { 33312798Swpaul ptype(proc->res_prefix, proc->res_type, 1); 33412798Swpaul f_print(fout, "* "); 33512798Swpaul } 33612798Swpaul if (server_p) 33712798Swpaul pvname_svc(proc->proc_name, vp->vers_num); 33812798Swpaul else 33912798Swpaul pvname(proc->proc_name, vp->vers_num); 34012798Swpaul 341149709Sstefanf parglist(proc, addargtype); 34212798Swpaul} 34312798Swpaul 34412798Swpaul 34512798Swpaul 34612798Swpaul/* print out argument list of procedure */ 34717142Sjkhstatic void 348152398Sdwmaloneparglist(proc_list *proc, const char *addargtype) 34912798Swpaul{ 35012798Swpaul decl_list *dl; 35112798Swpaul 35212798Swpaul f_print(fout, "("); 35312798Swpaul if (proc->arg_num < 2 && newstyle && 35412798Swpaul streq(proc->args.decls->decl.type, "void")) { 35512798Swpaul /* 0 argument in new style: do nothing*/ 35612798Swpaul } 35712798Swpaul else { 35812798Swpaul for (dl = proc->args.decls; dl != NULL; dl = dl->next) { 35912798Swpaul ptype(dl->decl.prefix, dl->decl.type, 1); 36012798Swpaul if (!newstyle) 36112798Swpaul f_print(fout, "*"); 36212798Swpaul /* old style passes by reference */ 36312798Swpaul f_print(fout, ", "); 3641897Swollman } 3651897Swollman } 36612798Swpaul 36712798Swpaul if (mtflag) { 36812798Swpaul ptype(proc->res_prefix, proc->res_type, 1); 36912798Swpaul f_print(fout, "*, "); 3701897Swollman } 37112798Swpaul 37212798Swpaul f_print(fout, "%s);\n", addargtype); 37312798Swpaul 3741897Swollman} 3751897Swollman 37617142Sjkhstatic void 3771897Swollmanpenumdef(def) 3781897Swollman definition *def; 3791897Swollman{ 380152398Sdwmalone const char *name = def->def_name; 3811897Swollman enumval_list *l; 382152398Sdwmalone const char *last = NULL; 3831897Swollman int count = 0; 3841897Swollman 3851897Swollman f_print(fout, "enum %s {\n", name); 3861897Swollman for (l = def->def.en.vals; l != NULL; l = l->next) { 3871897Swollman f_print(fout, "\t%s", l->name); 3881897Swollman if (l->assignment) { 3891897Swollman f_print(fout, " = %s", l->assignment); 3901897Swollman last = l->assignment; 3911897Swollman count = 1; 3921897Swollman } else { 3931897Swollman if (last == NULL) { 3941897Swollman f_print(fout, " = %d", count++); 3951897Swollman } else { 3961897Swollman f_print(fout, " = %s + %d", last, count++); 3971897Swollman } 3981897Swollman } 39912798Swpaul if (l->next) 40012798Swpaul f_print(fout, ",\n"); 40112798Swpaul else 40212798Swpaul f_print(fout, "\n"); 4031897Swollman } 4041897Swollman f_print(fout, "};\n"); 4051897Swollman f_print(fout, "typedef enum %s %s;\n", name, name); 4061897Swollman} 4071897Swollman 40817142Sjkhstatic void 4091897Swollmanptypedef(def) 4101897Swollman definition *def; 4111897Swollman{ 412152398Sdwmalone const char *name = def->def_name; 413152398Sdwmalone const char *old = def->def.ty.old_type; 4141897Swollman char prefix[8]; /* enough to contain "struct ", including NUL */ 4151897Swollman relation rel = def->def.ty.rel; 4161897Swollman 4171897Swollman 4181897Swollman if (!streq(name, old)) { 4191897Swollman if (streq(old, "string")) { 4201897Swollman old = "char"; 4211897Swollman rel = REL_POINTER; 4221897Swollman } else if (streq(old, "opaque")) { 4231897Swollman old = "char"; 4241897Swollman } else if (streq(old, "bool")) { 4251897Swollman old = "bool_t"; 4261897Swollman } 4271897Swollman if (undefined2(old, name) && def->def.ty.old_prefix) { 4281897Swollman s_print(prefix, "%s ", def->def.ty.old_prefix); 4291897Swollman } else { 4301897Swollman prefix[0] = 0; 4311897Swollman } 4321897Swollman f_print(fout, "typedef "); 4331897Swollman switch (rel) { 4341897Swollman case REL_ARRAY: 4351897Swollman f_print(fout, "struct {\n"); 4361897Swollman f_print(fout, "\tu_int %s_len;\n", name); 4371897Swollman f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name); 4381897Swollman f_print(fout, "} %s", name); 4391897Swollman break; 4401897Swollman case REL_POINTER: 4411897Swollman f_print(fout, "%s%s *%s", prefix, old, name); 4421897Swollman break; 4431897Swollman case REL_VECTOR: 4441897Swollman f_print(fout, "%s%s %s[%s]", prefix, old, name, 4451897Swollman def->def.ty.array_max); 4461897Swollman break; 4471897Swollman case REL_ALIAS: 44812798Swpaul f_print(fout, "%s%s %s", prefix, old, name); 4491897Swollman break; 4501897Swollman } 4511897Swollman f_print(fout, ";\n"); 4521897Swollman } 4531897Swollman} 4541897Swollman 45517142Sjkhvoid 456152398Sdwmalonepdeclaration(const char *name, declaration *dec, int tab, const char *separator) 4571897Swollman{ 4581897Swollman char buf[8]; /* enough to hold "struct ", include NUL */ 459152398Sdwmalone const char *prefix; 460152398Sdwmalone const char *type; 4611897Swollman 4621897Swollman if (streq(dec->type, "void")) { 4631897Swollman return; 4641897Swollman } 4651897Swollman tabify(fout, tab); 4661897Swollman if (streq(dec->type, name) && !dec->prefix) { 4671897Swollman f_print(fout, "struct "); 4681897Swollman } 4691897Swollman if (streq(dec->type, "string")) { 4701897Swollman f_print(fout, "char *%s", dec->name); 4711897Swollman } else { 4721897Swollman prefix = ""; 4731897Swollman if (streq(dec->type, "bool")) { 4741897Swollman type = "bool_t"; 4751897Swollman } else if (streq(dec->type, "opaque")) { 4761897Swollman type = "char"; 4771897Swollman } else { 4781897Swollman if (dec->prefix) { 4791897Swollman s_print(buf, "%s ", dec->prefix); 4801897Swollman prefix = buf; 4811897Swollman } 4821897Swollman type = dec->type; 4831897Swollman } 4841897Swollman switch (dec->rel) { 4851897Swollman case REL_ALIAS: 4861897Swollman f_print(fout, "%s%s %s", prefix, type, dec->name); 4871897Swollman break; 4881897Swollman case REL_VECTOR: 4891897Swollman f_print(fout, "%s%s %s[%s]", prefix, type, dec->name, 4901897Swollman dec->array_max); 4911897Swollman break; 4921897Swollman case REL_POINTER: 4931897Swollman f_print(fout, "%s%s *%s", prefix, type, dec->name); 4941897Swollman break; 4951897Swollman case REL_ARRAY: 4961897Swollman f_print(fout, "struct {\n"); 4971897Swollman tabify(fout, tab); 4981897Swollman f_print(fout, "\tu_int %s_len;\n", dec->name); 4991897Swollman tabify(fout, tab); 50012798Swpaul f_print(fout, 50112798Swpaul "\t%s%s *%s_val;\n", prefix, type, dec->name); 5021897Swollman tabify(fout, tab); 5031897Swollman f_print(fout, "} %s", dec->name); 5041897Swollman break; 5051897Swollman } 5061897Swollman } 50712798Swpaul f_print(fout, separator); 5081897Swollman} 5091897Swollman 51017142Sjkhstatic int 511152398Sdwmaloneundefined2(const char *type, const char *stop) 5121897Swollman{ 5131897Swollman list *l; 5141897Swollman definition *def; 5151897Swollman 5161897Swollman for (l = defined; l != NULL; l = l->next) { 5171897Swollman def = (definition *) l->val; 5181897Swollman if (def->def_kind != DEF_PROGRAM) { 5191897Swollman if (streq(def->def_name, stop)) { 5201897Swollman return (1); 5211897Swollman } else if (streq(def->def_name, type)) { 5221897Swollman return (0); 5231897Swollman } 5241897Swollman } 5251897Swollman } 5261897Swollman return (1); 5271897Swollman} 528