rpc_tblout.c revision 12796
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#ident	"@(#)rpc_tblout.c	1.11	93/07/05 SMI"
31
32#ifndef lint
33static char sccsid[] = "@(#)rpc_tblout.c 1.4 89/02/22 (C) 1988 SMI";
34#endif
35
36/*
37 * rpc_tblout.c, Dispatch table outputter for the RPC protocol compiler
38 * Copyright (C) 1989, Sun Microsystems, Inc.
39 */
40#include <stdio.h>
41#include <string.h>
42#include "rpc_parse.h"
43#include "rpc_util.h"
44
45#define TABSIZE		8
46#define TABCOUNT	5
47#define TABSTOP		(TABSIZE*TABCOUNT)
48
49static char tabstr[TABCOUNT+1] = "\t\t\t\t\t";
50
51static char tbl_hdr[] = "struct rpcgen_table %s_table[] = {\n";
52static char tbl_end[] = "};\n";
53
54static char null_entry[] = "\n\t(char *(*)())0,\n\
55 \t(xdrproc_t) xdr_void,\t\t\t0,\n\
56 \t(xdrproc_t) xdr_void,\t\t\t0,\n";
57
58
59static char tbl_nproc[] = "int %s_nproc =\n\tsizeof(%s_table)/sizeof(%s_table[0]);\n\n";
60
61static int write_table __P(( definition * ));
62static int printit __P(( char *, char * ));
63
64void
65write_tables()
66{
67	list *l;
68	definition *def;
69
70	f_print(fout, "\n");
71	for (l = defined; l != NULL; l = l->next) {
72		def = (definition *) l->val;
73		if (def->def_kind == DEF_PROGRAM) {
74			write_table(def);
75		}
76	}
77}
78
79static
80write_table(def)
81	definition *def;
82{
83	version_list *vp;
84	proc_list *proc;
85	int current;
86	int expected;
87	char progvers[100];
88	int warning;
89
90	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
91		warning = 0;
92		s_print(progvers, "%s_%s",
93		    locase(def->def_name), vp->vers_num);
94		/* print the table header */
95		f_print(fout, tbl_hdr, progvers);
96
97		if (nullproc(vp->procs)) {
98			expected = 0;
99		} else {
100			expected = 1;
101			f_print(fout, null_entry);
102		}
103		for (proc = vp->procs; proc != NULL; proc = proc->next) {
104			current = atoi(proc->proc_num);
105			if (current != expected++) {
106				f_print(fout,
107			"\n/*\n * WARNING: table out of order\n */\n");
108				if (warning == 0) {
109					f_print(stderr,
110				    "WARNING %s table is out of order\n",
111					    progvers);
112					warning = 1;
113					nonfatalerrors = 1;
114				}
115				expected = current + 1;
116			}
117			f_print(fout, "\n\t(char *(*)())RPCGEN_ACTION(");
118
119			/* routine to invoke */
120			if( Cflag && !newstyle )
121			  pvname_svc(proc->proc_name, vp->vers_num);
122			else {
123			  if( newstyle )
124			    f_print( fout, "_");   /* calls internal func */
125			  pvname(proc->proc_name, vp->vers_num);
126			}
127			f_print(fout, "),\n");
128
129			/* argument info */
130			if( proc->arg_num > 1 )
131			  printit((char*) NULL, proc->args.argname );
132			else
133			  /* do we have to do something special for newstyle */
134			  printit( proc->args.decls->decl.prefix,
135				  proc->args.decls->decl.type );
136			/* result info */
137			printit(proc->res_prefix, proc->res_type);
138		}
139
140		/* print the table trailer */
141		f_print(fout, tbl_end);
142		f_print(fout, tbl_nproc, progvers, progvers, progvers);
143	}
144}
145
146static
147printit(prefix, type)
148	char *prefix;
149	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