rpc_hout.c revision 149710
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 149710 2005-09-02 10:32:05Z stefanf $"); 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> 451897Swollman#include <ctype.h> 4612798Swpaul#include "rpc_parse.h" 47149682Sstefanf#include "rpc_scan.h" 481897Swollman#include "rpc_util.h" 491897Swollman 5092921Simpvoid storexdrfuncdecl( char *, int ); 5192921Simpstatic void pconstdef( definition * ); 5292921Simpstatic void pstructdef( definition * ); 5392921Simpstatic void puniondef( definition * ); 54149695Sstefanfstatic void pprogramdef( definition *, int ); 5592921Simpstatic void pstructdef( definition * ); 5692921Simpstatic void penumdef( definition * ); 5792921Simpstatic void ptypedef( definition * ); 5892921Simpstatic void pdefine( char *, char * ); 5992921Simpstatic int undefined2( char *, char * ); 6092921Simpstatic void parglist( proc_list *, char * ); 61149709Sstefanfstatic void pprocdef( proc_list *, version_list *, char *, int ); 6292921Simpvoid pdeclaration( char *, declaration *, int, char * ); 631897Swollman 641897Swollman/* 658874Srgrimes * Print the C-version of an xdr definition 661897Swollman */ 671897Swollmanvoid 68149695Sstefanfprint_datadef(definition *def, int headeronly) 691897Swollman{ 7012798Swpaul 7112798Swpaul if (def->def_kind == DEF_PROGRAM) /* handle data only */ 7212798Swpaul return; 7312798Swpaul 741897Swollman if (def->def_kind != DEF_CONST) { 751897Swollman f_print(fout, "\n"); 761897Swollman } 771897Swollman switch (def->def_kind) { 781897Swollman case DEF_STRUCT: 791897Swollman pstructdef(def); 801897Swollman break; 811897Swollman case DEF_UNION: 821897Swollman puniondef(def); 831897Swollman break; 841897Swollman case DEF_ENUM: 851897Swollman penumdef(def); 861897Swollman break; 871897Swollman case DEF_TYPEDEF: 881897Swollman ptypedef(def); 891897Swollman break; 901897Swollman case DEF_PROGRAM: 91149695Sstefanf pprogramdef(def, headeronly); 921897Swollman break; 931897Swollman case DEF_CONST: 941897Swollman pconstdef(def); 951897Swollman break; 961897Swollman } 971897Swollman if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) { 9812798Swpaul storexdrfuncdecl(def->def_name, 9912798Swpaul def->def_kind != DEF_TYPEDEF || 10012798Swpaul !isvectordef(def->def.ty.old_type, 10112798Swpaul def->def.ty.rel)); 1021897Swollman } 10312798Swpaul} 10412798Swpaul 10512798Swpaul 10612798Swpaulvoid 107149695Sstefanfprint_funcdef(definition *def, int headeronly) 10812798Swpaul{ 10912798Swpaul switch (def->def_kind) { 11012798Swpaul case DEF_PROGRAM: 1111897Swollman f_print(fout, "\n"); 112149695Sstefanf pprogramdef(def, headeronly); 11312798Swpaul break; 11417142Sjkh default: 11599979Salfred break; 11617142Sjkh } 11712798Swpaul} 11812798Swpaul 11912798Swpaul/* store away enough information to allow the XDR functions to be spat 12012798Swpaul out at the end of the file */ 12112798Swpaul 12212798Swpaulvoid 12312798Swpaulstorexdrfuncdecl(name, pointerp) 12412798Swpaulchar *name; 12512798Swpaulint pointerp; 12612798Swpaul{ 12712798Swpaul xdrfunc * xdrptr; 12812798Swpaul 129100441Scharnier xdrptr = XALLOC(struct xdrfunc); 13012798Swpaul 13112798Swpaul xdrptr->name = name; 13212798Swpaul xdrptr->pointerp = pointerp; 13312798Swpaul xdrptr->next = NULL; 13412798Swpaul 13512798Swpaul if (xdrfunc_tail == NULL){ 13612798Swpaul xdrfunc_head = xdrptr; 13712798Swpaul xdrfunc_tail = xdrptr; 13812798Swpaul } else { 13912798Swpaul xdrfunc_tail->next = xdrptr; 14012798Swpaul xdrfunc_tail = xdrptr; 1411897Swollman } 14212798Swpaul 14312798Swpaul 1441897Swollman} 1451897Swollman 14612798Swpaulvoid 147149709Sstefanfprint_xdr_func_def(char *name, int pointerp) 14812798Swpaul{ 149149709Sstefanf f_print(fout, "extern bool_t xdr_%s(XDR *, %s%s);\n", name, 150149709Sstefanf name, pointerp ? "*" : ""); 15112798Swpaul} 15212798Swpaul 15312798Swpaul 15417142Sjkhstatic void 1551897Swollmanpconstdef(def) 1561897Swollman definition *def; 1571897Swollman{ 1581897Swollman pdefine(def->def_name, def->def.co); 1591897Swollman} 1601897Swollman 16112798Swpaul/* print out the definitions for the arguments of functions in the 16212798Swpaul header file 16312798Swpaul*/ 16417142Sjkhstatic void 16512798Swpaulpargdef(def) 16612798Swpaul definition *def; 16712798Swpaul{ 16812798Swpaul decl_list *l; 16912798Swpaul version_list *vers; 17012798Swpaul char *name; 17112798Swpaul proc_list *plist; 17212798Swpaul 17312798Swpaul 17412798Swpaul for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) { 17512798Swpaul for (plist = vers->procs; plist != NULL; 17612798Swpaul plist = plist->next) { 17712798Swpaul 17812798Swpaul if (!newstyle || plist->arg_num < 2) { 17912798Swpaul continue; /* old style or single args */ 18012798Swpaul } 18112798Swpaul name = plist->args.argname; 18212798Swpaul f_print(fout, "struct %s {\n", name); 18312798Swpaul for (l = plist->args.decls; 18412798Swpaul l != NULL; l = l->next) { 18512798Swpaul pdeclaration(name, &l->decl, 1, 18612798Swpaul ";\n"); 18712798Swpaul } 18812798Swpaul f_print(fout, "};\n"); 18912798Swpaul f_print(fout, "typedef struct %s %s;\n", 19012798Swpaul name, name); 19112798Swpaul storexdrfuncdecl(name, 1); 19212798Swpaul f_print(fout, "\n"); 19312798Swpaul } 19412798Swpaul } 19512798Swpaul} 19612798Swpaul 19712798Swpaul 19817142Sjkhstatic void 1991897Swollmanpstructdef(def) 2001897Swollman definition *def; 2011897Swollman{ 2021897Swollman decl_list *l; 2031897Swollman char *name = def->def_name; 2041897Swollman 2051897Swollman f_print(fout, "struct %s {\n", name); 2061897Swollman for (l = def->def.st.decls; l != NULL; l = l->next) { 20712798Swpaul pdeclaration(name, &l->decl, 1, ";\n"); 2081897Swollman } 2091897Swollman f_print(fout, "};\n"); 2101897Swollman f_print(fout, "typedef struct %s %s;\n", name, name); 2111897Swollman} 2121897Swollman 21317142Sjkhstatic void 2141897Swollmanpuniondef(def) 2151897Swollman definition *def; 2161897Swollman{ 2171897Swollman case_list *l; 2181897Swollman char *name = def->def_name; 2191897Swollman declaration *decl; 2201897Swollman 2211897Swollman f_print(fout, "struct %s {\n", name); 2221897Swollman decl = &def->def.un.enum_decl; 2231897Swollman if (streq(decl->type, "bool")) { 2241897Swollman f_print(fout, "\tbool_t %s;\n", decl->name); 2251897Swollman } else { 2261897Swollman f_print(fout, "\t%s %s;\n", decl->type, decl->name); 2271897Swollman } 2281897Swollman f_print(fout, "\tunion {\n"); 2291897Swollman for (l = def->def.un.cases; l != NULL; l = l->next) { 23012798Swpaul if (l->contflag == 0) 23112798Swpaul pdeclaration(name, &l->case_decl, 2, ";\n"); 2321897Swollman } 2331897Swollman decl = def->def.un.default_decl; 2341897Swollman if (decl && !streq(decl->type, "void")) { 23512798Swpaul pdeclaration(name, decl, 2, ";\n"); 2361897Swollman } 2371897Swollman f_print(fout, "\t} %s_u;\n", name); 2381897Swollman f_print(fout, "};\n"); 2391897Swollman f_print(fout, "typedef struct %s %s;\n", name, name); 2401897Swollman} 2411897Swollman 24217142Sjkhstatic void 2431897Swollmanpdefine(name, num) 2441897Swollman char *name; 2451897Swollman char *num; 2461897Swollman{ 24712798Swpaul f_print(fout, "#define\t%s %s\n", name, num); 2481897Swollman} 2491897Swollman 25017142Sjkhstatic void 2511897Swollmanpuldefine(name, num) 2521897Swollman char *name; 2531897Swollman char *num; 2541897Swollman{ 25512798Swpaul f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num); 2561897Swollman} 2571897Swollman 25817142Sjkhstatic int 2591897Swollmandefine_printed(stop, start) 2601897Swollman proc_list *stop; 2611897Swollman version_list *start; 2621897Swollman{ 2631897Swollman version_list *vers; 2641897Swollman proc_list *proc; 2651897Swollman 2661897Swollman for (vers = start; vers != NULL; vers = vers->next) { 2671897Swollman for (proc = vers->procs; proc != NULL; proc = proc->next) { 2681897Swollman if (proc == stop) { 2691897Swollman return (0); 2701897Swollman } else if (streq(proc->proc_name, stop->proc_name)) { 2711897Swollman return (1); 2721897Swollman } 2731897Swollman } 2741897Swollman } 2751897Swollman abort(); 2761897Swollman /* NOTREACHED */ 2771897Swollman} 2781897Swollman 27917142Sjkhstatic void 280149709Sstefanfpfreeprocdef(char * name, char *vers) 28112798Swpaul{ 28212798Swpaul f_print(fout, "extern int "); 28312798Swpaul pvname(name, vers); 284149709Sstefanf f_print(fout, "_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n"); 28512798Swpaul} 28612798Swpaul 28717142Sjkhstatic void 288149709Sstefanfpdispatch(char * name, char *vers) 28986318Salfred{ 29086318Salfred 29186318Salfred f_print(fout, "void "); 29286318Salfred pvname(name, vers); 293149710Sstefanf f_print(fout, "(struct svc_req *rqstp, SVCXPRT *transp);\n"); 29486318Salfred} 29586318Salfred 29686318Salfredstatic void 297149695Sstefanfpprogramdef(definition *def, int headeronly) 2981897Swollman{ 2991897Swollman version_list *vers; 3001897Swollman proc_list *proc; 30112798Swpaul char *ext; 3021897Swollman 30312798Swpaul pargdef(def); 30412798Swpaul 3051897Swollman puldefine(def->def_name, def->def.pr.prog_num); 3061897Swollman for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) { 30712798Swpaul if (tblflag) { 30812798Swpaul f_print(fout, 30912798Swpaul "extern struct rpcgen_table %s_%s_table[];\n", 31012798Swpaul locase(def->def_name), vers->vers_num); 31112798Swpaul f_print(fout, 31212798Swpaul "extern %s_%s_nproc;\n", 31312798Swpaul locase(def->def_name), vers->vers_num); 31412798Swpaul } 3151897Swollman puldefine(vers->vers_name, vers->vers_num); 31612798Swpaul 317149709Sstefanf f_print(fout, "\n"); 318149709Sstefanf ext = "extern "; 319149709Sstefanf if (headeronly) { 320149709Sstefanf f_print(fout, "%s", ext); 321149709Sstefanf pdispatch(def->def_name, vers->vers_num); 322149709Sstefanf } 323149709Sstefanf for (proc = vers->procs; proc != NULL; proc = proc->next) { 324149709Sstefanf if (!define_printed(proc, def->def.pr.versions)) { 325149709Sstefanf puldefine(proc->proc_name, proc->proc_num); 326149695Sstefanf } 327149709Sstefanf f_print(fout, "%s", ext); 328149709Sstefanf pprocdef(proc, vers, "CLIENT *", 0); 329149709Sstefanf f_print(fout, "%s", ext); 330149709Sstefanf pprocdef(proc, vers, "struct svc_req *", 1); 3311897Swollman } 332149709Sstefanf pfreeprocdef(def->def_name, vers->vers_num); 3331897Swollman } 3341897Swollman} 3351897Swollman 33617142Sjkhstatic void 337149709Sstefanfpprocdef(proc_list *proc, version_list *vp, char *addargtype, int server_p) 3381897Swollman{ 33912798Swpaul if (mtflag) {/* Print MT style stubs */ 34012798Swpaul if (server_p) 34112798Swpaul f_print(fout, "bool_t "); 34212798Swpaul else 34312798Swpaul f_print(fout, "enum clnt_stat "); 34412798Swpaul } else { 34512798Swpaul ptype(proc->res_prefix, proc->res_type, 1); 34612798Swpaul f_print(fout, "* "); 34712798Swpaul } 34812798Swpaul if (server_p) 34912798Swpaul pvname_svc(proc->proc_name, vp->vers_num); 35012798Swpaul else 35112798Swpaul pvname(proc->proc_name, vp->vers_num); 35212798Swpaul 353149709Sstefanf parglist(proc, addargtype); 35412798Swpaul} 35512798Swpaul 35612798Swpaul 35712798Swpaul 35812798Swpaul/* print out argument list of procedure */ 35917142Sjkhstatic void 36012798Swpaulparglist(proc, addargtype) 36112798Swpaul proc_list *proc; 36212798Swpaul char* addargtype; 36312798Swpaul{ 36412798Swpaul decl_list *dl; 36512798Swpaul 36612798Swpaul f_print(fout, "("); 36712798Swpaul if (proc->arg_num < 2 && newstyle && 36812798Swpaul streq(proc->args.decls->decl.type, "void")) { 36912798Swpaul /* 0 argument in new style: do nothing*/ 37012798Swpaul } 37112798Swpaul else { 37212798Swpaul for (dl = proc->args.decls; dl != NULL; dl = dl->next) { 37312798Swpaul ptype(dl->decl.prefix, dl->decl.type, 1); 37412798Swpaul if (!newstyle) 37512798Swpaul f_print(fout, "*"); 37612798Swpaul /* old style passes by reference */ 37712798Swpaul f_print(fout, ", "); 3781897Swollman } 3791897Swollman } 38012798Swpaul 38112798Swpaul if (mtflag) { 38212798Swpaul ptype(proc->res_prefix, proc->res_type, 1); 38312798Swpaul f_print(fout, "*, "); 3841897Swollman } 38512798Swpaul 38612798Swpaul f_print(fout, "%s);\n", addargtype); 38712798Swpaul 3881897Swollman} 3891897Swollman 39017142Sjkhstatic void 3911897Swollmanpenumdef(def) 3921897Swollman definition *def; 3931897Swollman{ 3941897Swollman char *name = def->def_name; 3951897Swollman enumval_list *l; 3961897Swollman char *last = NULL; 3971897Swollman int count = 0; 3981897Swollman 3991897Swollman f_print(fout, "enum %s {\n", name); 4001897Swollman for (l = def->def.en.vals; l != NULL; l = l->next) { 4011897Swollman f_print(fout, "\t%s", l->name); 4021897Swollman if (l->assignment) { 4031897Swollman f_print(fout, " = %s", l->assignment); 4041897Swollman last = l->assignment; 4051897Swollman count = 1; 4061897Swollman } else { 4071897Swollman if (last == NULL) { 4081897Swollman f_print(fout, " = %d", count++); 4091897Swollman } else { 4101897Swollman f_print(fout, " = %s + %d", last, count++); 4111897Swollman } 4121897Swollman } 41312798Swpaul if (l->next) 41412798Swpaul f_print(fout, ",\n"); 41512798Swpaul else 41612798Swpaul f_print(fout, "\n"); 4171897Swollman } 4181897Swollman f_print(fout, "};\n"); 4191897Swollman f_print(fout, "typedef enum %s %s;\n", name, name); 4201897Swollman} 4211897Swollman 42217142Sjkhstatic void 4231897Swollmanptypedef(def) 4241897Swollman definition *def; 4251897Swollman{ 4261897Swollman char *name = def->def_name; 4271897Swollman char *old = def->def.ty.old_type; 4281897Swollman char prefix[8]; /* enough to contain "struct ", including NUL */ 4291897Swollman relation rel = def->def.ty.rel; 4301897Swollman 4311897Swollman 4321897Swollman if (!streq(name, old)) { 4331897Swollman if (streq(old, "string")) { 4341897Swollman old = "char"; 4351897Swollman rel = REL_POINTER; 4361897Swollman } else if (streq(old, "opaque")) { 4371897Swollman old = "char"; 4381897Swollman } else if (streq(old, "bool")) { 4391897Swollman old = "bool_t"; 4401897Swollman } 4411897Swollman if (undefined2(old, name) && def->def.ty.old_prefix) { 4421897Swollman s_print(prefix, "%s ", def->def.ty.old_prefix); 4431897Swollman } else { 4441897Swollman prefix[0] = 0; 4451897Swollman } 4461897Swollman f_print(fout, "typedef "); 4471897Swollman switch (rel) { 4481897Swollman case REL_ARRAY: 4491897Swollman f_print(fout, "struct {\n"); 4501897Swollman f_print(fout, "\tu_int %s_len;\n", name); 4511897Swollman f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name); 4521897Swollman f_print(fout, "} %s", name); 4531897Swollman break; 4541897Swollman case REL_POINTER: 4551897Swollman f_print(fout, "%s%s *%s", prefix, old, name); 4561897Swollman break; 4571897Swollman case REL_VECTOR: 4581897Swollman f_print(fout, "%s%s %s[%s]", prefix, old, name, 4591897Swollman def->def.ty.array_max); 4601897Swollman break; 4611897Swollman case REL_ALIAS: 46212798Swpaul f_print(fout, "%s%s %s", prefix, old, name); 4631897Swollman break; 4641897Swollman } 4651897Swollman f_print(fout, ";\n"); 4661897Swollman } 4671897Swollman} 4681897Swollman 46917142Sjkhvoid 47012798Swpaulpdeclaration(name, dec, tab, separator) 4711897Swollman char *name; 4721897Swollman declaration *dec; 4731897Swollman int tab; 47412798Swpaul char *separator; 4751897Swollman{ 4761897Swollman char buf[8]; /* enough to hold "struct ", include NUL */ 4771897Swollman char *prefix; 4781897Swollman char *type; 4791897Swollman 4801897Swollman if (streq(dec->type, "void")) { 4811897Swollman return; 4821897Swollman } 4831897Swollman tabify(fout, tab); 4841897Swollman if (streq(dec->type, name) && !dec->prefix) { 4851897Swollman f_print(fout, "struct "); 4861897Swollman } 4871897Swollman if (streq(dec->type, "string")) { 4881897Swollman f_print(fout, "char *%s", dec->name); 4891897Swollman } else { 4901897Swollman prefix = ""; 4911897Swollman if (streq(dec->type, "bool")) { 4921897Swollman type = "bool_t"; 4931897Swollman } else if (streq(dec->type, "opaque")) { 4941897Swollman type = "char"; 4951897Swollman } else { 4961897Swollman if (dec->prefix) { 4971897Swollman s_print(buf, "%s ", dec->prefix); 4981897Swollman prefix = buf; 4991897Swollman } 5001897Swollman type = dec->type; 5011897Swollman } 5021897Swollman switch (dec->rel) { 5031897Swollman case REL_ALIAS: 5041897Swollman f_print(fout, "%s%s %s", prefix, type, dec->name); 5051897Swollman break; 5061897Swollman case REL_VECTOR: 5071897Swollman f_print(fout, "%s%s %s[%s]", prefix, type, dec->name, 5081897Swollman dec->array_max); 5091897Swollman break; 5101897Swollman case REL_POINTER: 5111897Swollman f_print(fout, "%s%s *%s", prefix, type, dec->name); 5121897Swollman break; 5131897Swollman case REL_ARRAY: 5141897Swollman f_print(fout, "struct {\n"); 5151897Swollman tabify(fout, tab); 5161897Swollman f_print(fout, "\tu_int %s_len;\n", dec->name); 5171897Swollman tabify(fout, tab); 51812798Swpaul f_print(fout, 51912798Swpaul "\t%s%s *%s_val;\n", prefix, type, dec->name); 5201897Swollman tabify(fout, tab); 5211897Swollman f_print(fout, "} %s", dec->name); 5221897Swollman break; 5231897Swollman } 5241897Swollman } 52512798Swpaul f_print(fout, separator); 5261897Swollman} 5271897Swollman 52817142Sjkhstatic int 5291897Swollmanundefined2(type, stop) 5301897Swollman char *type; 5311897Swollman char *stop; 5321897Swollman{ 5331897Swollman list *l; 5341897Swollman definition *def; 5351897Swollman 5361897Swollman for (l = defined; l != NULL; l = l->next) { 5371897Swollman def = (definition *) l->val; 5381897Swollman if (def->def_kind != DEF_PROGRAM) { 5391897Swollman if (streq(def->def_name, stop)) { 5401897Swollman return (1); 5411897Swollman } else if (streq(def->def_name, type)) { 5421897Swollman return (0); 5431897Swollman } 5441897Swollman } 5451897Swollman } 5461897Swollman return (1); 5471897Swollman} 548