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