1/*	$NetBSD: rpc_tblout.c,v 1.15 2016/01/23 02:33:09 dholland 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.15 2016/01/23 02:33:09 dholland 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    "unsigned int %s_nproc =\n\t(unsigned int)(sizeof(%s_table)/sizeof(%s_table[0]));\n\n";
70
71static void write_table(definition *);
72static void printit(const char *, const char *);
73
74void
75write_tables(void)
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(definition *def)
91{
92	version_list *vp;
93	proc_list *proc;
94	int     current;
95	int     expected;
96	char    progvers[100];
97	int     warning;
98
99	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
100		warning = 0;
101		s_print(progvers, "%s_%s",
102		    locase(def->def_name), vp->vers_num);
103		/* print the table header */
104		f_print(fout, tbl_hdr, progvers);
105
106		if (nullproc(vp->procs)) {
107			expected = 0;
108		} else {
109			expected = 1;
110			f_print(fout, null_entry);
111		}
112		for (proc = vp->procs; proc != NULL; proc = proc->next) {
113			if (expected != 0)
114				f_print(fout, "\n");
115			current = atoi(proc->proc_num);
116			if (current != expected++) {
117				f_print(fout,
118				    "/*\n * WARNING: table out of order\n */\n\n");
119				if (warning == 0) {
120					f_print(stderr,
121					    "WARNING %s table is out of order\n",
122					    progvers);
123					warning = 1;
124					nonfatalerrors = 1;
125				}
126				expected = current + 1;
127			}
128			f_print(fout, "\t(char *(*)())RPCGEN_ACTION(");
129
130			/* routine to invoke */
131			if (!newstyle)
132				pvname_svc(proc->proc_name, vp->vers_num);
133			else {
134				if (newstyle)
135					f_print(fout, "_");	/* calls internal func */
136				pvname(proc->proc_name, vp->vers_num);
137			}
138			f_print(fout, "),\n");
139
140			/* argument info */
141			if (proc->arg_num > 1)
142				printit(NULL, proc->args.argname);
143			else
144				/* do we have to do something special for
145				 * newstyle */
146				printit(proc->args.decls->decl.prefix,
147				    proc->args.decls->decl.type);
148			/* result info */
149			printit(proc->res_prefix, proc->res_type);
150		}
151
152		/* print the table trailer */
153		f_print(fout, tbl_end);
154		f_print(fout, tbl_nproc, progvers, progvers, progvers);
155	}
156}
157
158static void
159printit(const char *prefix, const char *type)
160{
161	int     len;
162	int     tabs;
163
164
165	len = fprintf(fout, "\txdr_%s,", stringfix(type));
166	/* account for leading tab expansion */
167	len += TABSIZE - 1;
168	/* round up to tabs required */
169	tabs = (TABSTOP - len + TABSIZE - 1) / TABSIZE;
170	f_print(fout, "%s", &tabstr[TABCOUNT - tabs]);
171
172	if (streq(type, "void")) {
173		f_print(fout, "0");
174	} else {
175		f_print(fout, "(unsigned int)sizeof(");
176		/* XXX: should "follow" be 1 ??? */
177		ptype(prefix, type, 0);
178		f_print(fout, ")");
179	}
180	f_print(fout, ",\n");
181}
182