rpc_tblout.c revision 141152
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#if 0
33#ifndef lint
34static char sccsid[] = "@(#)rpc_tblout.c 1.4 89/02/22 (C) 1988 SMI";
35#endif
36#endif
37
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/usr.bin/rpcgen/rpc_tblout.c 141152 2005-02-02 22:14:10Z alfred $");
40
41/*
42 * rpc_tblout.c, Dispatch table outputter for the RPC protocol compiler
43 * Copyright (C) 1989, Sun Microsystems, Inc.
44 */
45#include <err.h>
46#include <stdio.h>
47#include <string.h>
48#include "rpc_parse.h"
49#include "rpc_util.h"
50
51#define TABSIZE		8
52#define TABCOUNT	5
53#define TABSTOP		(TABSIZE*TABCOUNT)
54
55static char tabstr[TABCOUNT+1] = "\t\t\t\t\t";
56
57static char tbl_hdr[] = "struct rpcgen_table %s_table[] = {\n";
58static char tbl_end[] = "\n};\n";
59
60static char null_entry[] = "\n\t{\n\
61 \tNULL,\n\
62 \t(xdrproc_t) xdr_void,\t\t\t0,\n\
63 \t(xdrproc_t) xdr_void,\t\t\t0,\n\
64 \t}";
65
66
67static char tbl_nproc[] = "int %s_nproc =\n\tsizeof(%s_table)/sizeof(%s_table[0]);\n\n";
68
69extern int nullproc( proc_list * );
70static void write_table( definition * );
71static void printit( char *, char * );
72
73void
74write_tables()
75{
76	list *l;
77	definition *def;
78
79	f_print(fout, "\n");
80	for (l = defined; l != NULL; l = l->next) {
81		def = (definition *) l->val;
82		if (def->def_kind == DEF_PROGRAM) {
83			write_table(def);
84		}
85	}
86}
87
88static void
89write_table(def)
90	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			current = atoi(proc->proc_num);
114			if (current != expected++) {
115				f_print(fout,
116			"\n/*\n * WARNING: table out of order\n */\n");
117				if (warning == 0) {
118					warnx("WARNING %s table is out of order", progvers);
119					warning = 1;
120					nonfatalerrors = 1;
121				}
122				expected = current + 1;
123			}
124			f_print(fout, ",\n\n\t{\n\tRPCGEN_ACTION(");
125
126			/* routine to invoke */
127			if( Cflag && !newstyle )
128			  pvname_svc(proc->proc_name, vp->vers_num);
129			else {
130			  if( newstyle )
131			    f_print( fout, "_");   /* calls internal func */
132			  pvname(proc->proc_name, vp->vers_num);
133			}
134			f_print(fout, "),\n");
135
136			/* argument info */
137			if( proc->arg_num > 1 )
138			  printit((char*) NULL, proc->args.argname );
139			else
140			  /* do we have to do something special for newstyle */
141			  printit( proc->args.decls->decl.prefix,
142				  proc->args.decls->decl.type );
143			/* result info */
144			printit(proc->res_prefix, proc->res_type);
145			f_print(fout, "\t}");
146		}
147
148		/* print the table trailer */
149		f_print(fout, tbl_end);
150		f_print(fout, tbl_nproc, progvers, progvers, progvers);
151	}
152}
153
154static void
155printit(prefix, type)
156	char *prefix;
157	char *type;
158{
159	int len;
160	int tabs;
161
162
163 	len = fprintf(fout, "\t(xdrproc_t) xdr_%s,", stringfix(type));
164	/* account for leading tab expansion */
165	len += TABSIZE - 1;
166	/* round up to tabs required */
167	tabs = (TABSTOP - len + TABSIZE - 1)/TABSIZE;
168	f_print(fout, "%s", &tabstr[TABCOUNT-tabs]);
169
170	if (streq(type, "void")) {
171		f_print(fout, "0");
172	} else {
173		f_print(fout, " sizeof ( ");
174		/* XXX: should "follow" be 1 ??? */
175		ptype(prefix, type, 0);
176		f_print(fout, ")");
177	}
178	f_print(fout, ",\n");
179}
180