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