xref: /netbsd-src/usr.bin/rpcgen/rpc_tblout.c (revision 7944f94c110fc487c61b5c41bbd803b04eba69b0)
1*7944f94cSdholland /*	$NetBSD: rpc_tblout.c,v 1.15 2016/01/23 02:33:09 dholland Exp $	*/
2ee826b8cSpk /*
3ee826b8cSpk  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4ee826b8cSpk  * unrestricted use provided that this legend is included on all tape
5ee826b8cSpk  * media and as a part of the software program in whole or part.  Users
6ee826b8cSpk  * may copy or modify Sun RPC without charge, but are not authorized
7ee826b8cSpk  * to license or distribute it to anyone else except as part of a product or
8ee826b8cSpk  * program developed by the user or with the express written consent of
9ee826b8cSpk  * Sun Microsystems, Inc.
10ee826b8cSpk  *
11ee826b8cSpk  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12ee826b8cSpk  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13ee826b8cSpk  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14ee826b8cSpk  *
15ee826b8cSpk  * Sun RPC is provided with no support and without any obligation on the
16ee826b8cSpk  * part of Sun Microsystems, Inc. to assist in its use, correction,
17ee826b8cSpk  * modification or enhancement.
18ee826b8cSpk  *
19ee826b8cSpk  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20ee826b8cSpk  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21ee826b8cSpk  * OR ANY PART THEREOF.
22ee826b8cSpk  *
23ee826b8cSpk  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24ee826b8cSpk  * or profits or other special, indirect and consequential damages, even if
25ee826b8cSpk  * Sun has been advised of the possibility of such damages.
26ee826b8cSpk  *
27ee826b8cSpk  * Sun Microsystems, Inc.
28ee826b8cSpk  * 2550 Garcia Avenue
29ee826b8cSpk  * Mountain View, California  94043
30ee826b8cSpk  */
31ee826b8cSpk 
32b2f78261Sjmc #if HAVE_NBTOOL_CONFIG_H
33b2f78261Sjmc #include "nbtool_config.h"
34b2f78261Sjmc #endif
35b2f78261Sjmc 
3693579481Schristos #include <sys/cdefs.h>
3776834aefStv #if defined(__RCSID) && !defined(lint)
3893579481Schristos #if 0
39ee826b8cSpk static char sccsid[] = "@(#)rpc_tblout.c 1.4 89/02/22 (C) 1988 SMI";
4093579481Schristos #else
41*7944f94cSdholland __RCSID("$NetBSD: rpc_tblout.c,v 1.15 2016/01/23 02:33:09 dholland Exp $");
4293579481Schristos #endif
43ee826b8cSpk #endif
44ee826b8cSpk 
45ee826b8cSpk /*
46ee826b8cSpk  * rpc_tblout.c, Dispatch table outputter for the RPC protocol compiler
47ee826b8cSpk  */
48ee826b8cSpk #include <stdio.h>
49ee826b8cSpk #include <string.h>
5093579481Schristos #include <stdlib.h>
5193579481Schristos #include "rpc_scan.h"
52ee826b8cSpk #include "rpc_parse.h"
53ee826b8cSpk #include "rpc_util.h"
54ee826b8cSpk 
55ee826b8cSpk #define TABSIZE		8
56ee826b8cSpk #define TABCOUNT	5
57ee826b8cSpk #define TABSTOP		(TABSIZE*TABCOUNT)
58ee826b8cSpk 
59ee826b8cSpk static char tabstr[TABCOUNT + 1] = "\t\t\t\t\t";
60ee826b8cSpk 
61135600f9Sis static const char tbl_hdr[] = "struct rpcgen_table %s_table[] = {\n";
62135600f9Sis static const char tbl_end[] = "};\n";
63ee826b8cSpk 
64135600f9Sis static const char null_entry[] = "\t(char *(*)())0,\n\
65bfa2e15aSmycroft  \t(xdrproc_t)xdr_void,\t\t0,\n\
66bfa2e15aSmycroft  \t(xdrproc_t)xdr_void,\t\t0,\n";
67ee826b8cSpk 
6822a84276Schristos static const char tbl_nproc[] =
69*7944f94cSdholland     "unsigned int %s_nproc =\n\t(unsigned int)(sizeof(%s_table)/sizeof(%s_table[0]));\n\n";
70ee826b8cSpk 
710213edf0Schristos static void write_table(definition *);
720213edf0Schristos static void printit(const char *, const char *);
7371a0fb45Spk 
74ee826b8cSpk void
write_tables(void)75e9067f11Sdholland write_tables(void)
76ee826b8cSpk {
77ee826b8cSpk 	list   *l;
78ee826b8cSpk 	definition *def;
79ee826b8cSpk 
80ee826b8cSpk 	f_print(fout, "\n");
81ee826b8cSpk 	for (l = defined; l != NULL; l = l->next) {
82ee826b8cSpk 		def = (definition *) l->val;
83ee826b8cSpk 		if (def->def_kind == DEF_PROGRAM) {
84ee826b8cSpk 			write_table(def);
85ee826b8cSpk 		}
86ee826b8cSpk 	}
87ee826b8cSpk }
88ee826b8cSpk 
8993579481Schristos static void
write_table(definition * def)90e9067f11Sdholland write_table(definition *def)
91ee826b8cSpk {
92ee826b8cSpk 	version_list *vp;
93ee826b8cSpk 	proc_list *proc;
94ee826b8cSpk 	int     current;
95ee826b8cSpk 	int     expected;
96ee826b8cSpk 	char    progvers[100];
97ee826b8cSpk 	int     warning;
98ee826b8cSpk 
99ee826b8cSpk 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
100ee826b8cSpk 		warning = 0;
101ee826b8cSpk 		s_print(progvers, "%s_%s",
102ee826b8cSpk 		    locase(def->def_name), vp->vers_num);
103ee826b8cSpk 		/* print the table header */
104ee826b8cSpk 		f_print(fout, tbl_hdr, progvers);
105ee826b8cSpk 
106ee826b8cSpk 		if (nullproc(vp->procs)) {
107ee826b8cSpk 			expected = 0;
108ee826b8cSpk 		} else {
109ee826b8cSpk 			expected = 1;
110ee826b8cSpk 			f_print(fout, null_entry);
111ee826b8cSpk 		}
112ee826b8cSpk 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
113bfa2e15aSmycroft 			if (expected != 0)
114bfa2e15aSmycroft 				f_print(fout, "\n");
115ee826b8cSpk 			current = atoi(proc->proc_num);
116ee826b8cSpk 			if (current != expected++) {
117ee826b8cSpk 				f_print(fout,
118bfa2e15aSmycroft 				    "/*\n * WARNING: table out of order\n */\n\n");
119ee826b8cSpk 				if (warning == 0) {
120ee826b8cSpk 					f_print(stderr,
121ee826b8cSpk 					    "WARNING %s table is out of order\n",
122ee826b8cSpk 					    progvers);
123ee826b8cSpk 					warning = 1;
124ee826b8cSpk 					nonfatalerrors = 1;
125ee826b8cSpk 				}
126ee826b8cSpk 				expected = current + 1;
127ee826b8cSpk 			}
128bfa2e15aSmycroft 			f_print(fout, "\t(char *(*)())RPCGEN_ACTION(");
129ee826b8cSpk 
130ee826b8cSpk 			/* routine to invoke */
131ee0767daSpk 			if (!newstyle)
132ee826b8cSpk 				pvname_svc(proc->proc_name, vp->vers_num);
133ee826b8cSpk 			else {
134ee826b8cSpk 				if (newstyle)
135ee826b8cSpk 					f_print(fout, "_");	/* calls internal func */
136ee826b8cSpk 				pvname(proc->proc_name, vp->vers_num);
137ee826b8cSpk 			}
138ee826b8cSpk 			f_print(fout, "),\n");
139ee826b8cSpk 
140ee826b8cSpk 			/* argument info */
141ee826b8cSpk 			if (proc->arg_num > 1)
1429f61b804Splunky 				printit(NULL, proc->args.argname);
143ee826b8cSpk 			else
14499071d1bSlukem 				/* do we have to do something special for
14599071d1bSlukem 				 * newstyle */
146ee826b8cSpk 				printit(proc->args.decls->decl.prefix,
147ee826b8cSpk 				    proc->args.decls->decl.type);
148ee826b8cSpk 			/* result info */
149ee826b8cSpk 			printit(proc->res_prefix, proc->res_type);
150ee826b8cSpk 		}
151ee826b8cSpk 
152ee826b8cSpk 		/* print the table trailer */
153ee826b8cSpk 		f_print(fout, tbl_end);
154ee826b8cSpk 		f_print(fout, tbl_nproc, progvers, progvers, progvers);
155ee826b8cSpk 	}
156ee826b8cSpk }
157ee826b8cSpk 
15893579481Schristos static void
printit(const char * prefix,const char * type)159e9067f11Sdholland printit(const char *prefix, const char *type)
160ee826b8cSpk {
161ee826b8cSpk 	int     len;
162ee826b8cSpk 	int     tabs;
163ee826b8cSpk 
164ee826b8cSpk 
165ee826b8cSpk 	len = fprintf(fout, "\txdr_%s,", stringfix(type));
166ee826b8cSpk 	/* account for leading tab expansion */
167ee826b8cSpk 	len += TABSIZE - 1;
168ee826b8cSpk 	/* round up to tabs required */
169ee826b8cSpk 	tabs = (TABSTOP - len + TABSIZE - 1) / TABSIZE;
170ee826b8cSpk 	f_print(fout, "%s", &tabstr[TABCOUNT - tabs]);
171ee826b8cSpk 
172ee826b8cSpk 	if (streq(type, "void")) {
173ee826b8cSpk 		f_print(fout, "0");
174ee826b8cSpk 	} else {
175*7944f94cSdholland 		f_print(fout, "(unsigned int)sizeof(");
176ee826b8cSpk 		/* XXX: should "follow" be 1 ??? */
177ee826b8cSpk 		ptype(prefix, type, 0);
178ee826b8cSpk 		f_print(fout, ")");
179ee826b8cSpk 	}
180ee826b8cSpk 	f_print(fout, ",\n");
181ee826b8cSpk }
182