rpc_tblout.c revision 200462
1249259Sdim/*
2249259Sdim * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3249259Sdim * unrestricted use provided that this legend is included on all tape
4249259Sdim * media and as a part of the software program in whole or part.  Users
5249259Sdim * may copy or modify Sun RPC without charge, but are not authorized
6249259Sdim * to license or distribute it to anyone else except as part of a product or
7249259Sdim * program developed by the user.
8249259Sdim *
9249259Sdim * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10249259Sdim * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11249259Sdim * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12249259Sdim *
13249259Sdim * Sun RPC is provided with no support and without any obligation on the
14249259Sdim * part of Sun Microsystems, Inc. to assist in its use, correction,
15249259Sdim * modification or enhancement.
16249259Sdim *
17249259Sdim * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18249259Sdim * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19249259Sdim * OR ANY PART THEREOF.
20249259Sdim *
21249259Sdim * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22249259Sdim * or profits or other special, indirect and consequential damages, even if
23249259Sdim * Sun has been advised of the possibility of such damages.
24249259Sdim *
25249259Sdim * Sun Microsystems, Inc.
26249259Sdim * 2550 Garcia Avenue
27249259Sdim * Mountain View, California  94043
28249259Sdim */
29249259Sdim
30249259Sdim#if 0
31249259Sdim#ifndef lint
32249259Sdim#ident	"@(#)rpc_tblout.c	1.11	93/07/05 SMI"
33249259Sdimstatic char sccsid[] = "@(#)rpc_tblout.c 1.4 89/02/22 (C) 1988 SMI";
34249259Sdim#endif
35249259Sdim#endif
36249259Sdim
37249259Sdim#include <sys/cdefs.h>
38249259Sdim__FBSDID("$FreeBSD: head/usr.bin/rpcgen/rpc_tblout.c 200462 2009-12-13 03:14:06Z delphij $");
39249259Sdim
40249259Sdim/*
41249259Sdim * rpc_tblout.c, Dispatch table outputter for the RPC protocol compiler
42263509Sdim * Copyright (C) 1989, Sun Microsystems, Inc.
43249259Sdim */
44249259Sdim#include <err.h>
45249259Sdim#include <stdio.h>
46249259Sdim#include <string.h>
47249259Sdim#include "rpc_parse.h"
48249259Sdim#include "rpc_scan.h"
49249259Sdim#include "rpc_util.h"
50249259Sdim
51249259Sdim#define TABSIZE		8
52249259Sdim#define TABCOUNT	5
53249259Sdim#define TABSTOP		(TABSIZE*TABCOUNT)
54249259Sdim
55249259Sdimstatic char tabstr[TABCOUNT+1] = "\t\t\t\t\t";
56249259Sdim
57249259Sdimstatic char tbl_hdr[] = "struct rpcgen_table %s_table[] = {\n";
58249259Sdimstatic char tbl_end[] = "};\n";
59249259Sdim
60249259Sdimstatic char null_entry[] = "\n\t(char *(*)())0,\n\
61263509Sdim \t(xdrproc_t) xdr_void,\t\t\t0,\n\
62249259Sdim \t(xdrproc_t) xdr_void,\t\t\t0,\n";
63249259Sdim
64249259Sdim
65249259Sdimstatic char tbl_nproc[] = "int %s_nproc =\n\tsizeof(%s_table)/sizeof(%s_table[0]);\n\n";
66249259Sdim
67249259Sdimstatic void write_table( definition * );
68263509Sdimstatic void printit(const  char *, const char *);
69249259Sdim
70249259Sdimvoid
71249259Sdimwrite_tables(void)
72249259Sdim{
73263509Sdim	list *l;
74249259Sdim	definition *def;
75249259Sdim
76249259Sdim	f_print(fout, "\n");
77249259Sdim	for (l = defined; l != NULL; l = l->next) {
78249259Sdim		def = (definition *) l->val;
79249259Sdim		if (def->def_kind == DEF_PROGRAM) {
80249259Sdim			write_table(def);
81249259Sdim		}
82249259Sdim	}
83249259Sdim}
84249259Sdim
85263509Sdimstatic void
86263509Sdimwrite_table(definition *def)
87263509Sdim{
88263509Sdim	version_list *vp;
89249259Sdim	proc_list *proc;
90249259Sdim	int current;
91249259Sdim	int expected;
92249259Sdim	char progvers[100];
93249259Sdim	int warning;
94249259Sdim
95249259Sdim	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
96249259Sdim		warning = 0;
97249259Sdim		s_print(progvers, "%s_%s",
98249259Sdim		    locase(def->def_name), vp->vers_num);
99249259Sdim		/* print the table header */
100249259Sdim		f_print(fout, tbl_hdr, progvers);
101249259Sdim
102249259Sdim		if (nullproc(vp->procs)) {
103249259Sdim			expected = 0;
104249259Sdim		} else {
105249259Sdim			expected = 1;
106249259Sdim			f_print(fout, null_entry);
107249259Sdim		}
108249259Sdim		for (proc = vp->procs; proc != NULL; proc = proc->next) {
109249259Sdim			current = atoi(proc->proc_num);
110249259Sdim			if (current != expected++) {
111249259Sdim				f_print(fout,
112249259Sdim			"\n/*\n * WARNING: table out of order\n */\n");
113249259Sdim				if (warning == 0) {
114249259Sdim					warnx("WARNING %s table is out of order", progvers);
115249259Sdim					warning = 1;
116249259Sdim					nonfatalerrors = 1;
117249259Sdim				}
118249259Sdim				expected = current + 1;
119249259Sdim			}
120249259Sdim			f_print(fout, "\n\t(char *(*)())RPCGEN_ACTION(");
121249259Sdim
122249259Sdim			/* routine to invoke */
123249259Sdim			if( !newstyle )
124249259Sdim			  pvname_svc(proc->proc_name, vp->vers_num);
125249259Sdim			else {
126249259Sdim			  if( newstyle )
127249259Sdim			    f_print( fout, "_");   /* calls internal func */
128249259Sdim			  pvname(proc->proc_name, vp->vers_num);
129249259Sdim			}
130249259Sdim			f_print(fout, "),\n");
131249259Sdim
132249259Sdim			/* argument info */
133249259Sdim			if( proc->arg_num > 1 )
134249259Sdim			  printit((char*) NULL, proc->args.argname );
135249259Sdim			else
136249259Sdim			  /* do we have to do something special for newstyle */
137249259Sdim			  printit( proc->args.decls->decl.prefix,
138249259Sdim				  proc->args.decls->decl.type );
139249259Sdim			/* result info */
140249259Sdim			printit(proc->res_prefix, proc->res_type);
141249259Sdim		}
142249259Sdim
143249259Sdim		/* print the table trailer */
144249259Sdim		f_print(fout, tbl_end);
145249259Sdim		f_print(fout, tbl_nproc, progvers, progvers, progvers);
146249259Sdim	}
147249259Sdim}
148249259Sdim
149249259Sdimstatic void
150249259Sdimprintit(const char *prefix, const char *type)
151249259Sdim{
152249259Sdim	int len;
153249259Sdim	int tabs;
154249259Sdim
155249259Sdim
156249259Sdim 	len = fprintf(fout, "\txdr_%s,", stringfix(type));
157249259Sdim	/* account for leading tab expansion */
158263509Sdim	len += TABSIZE - 1;
159263509Sdim	/* round up to tabs required */
160263509Sdim	tabs = (TABSTOP - len + TABSIZE - 1)/TABSIZE;
161263509Sdim	f_print(fout, "%s", &tabstr[TABCOUNT-tabs]);
162263509Sdim
163263509Sdim	if (streq(type, "void")) {
164263509Sdim		f_print(fout, "0");
165263509Sdim	} else {
166263509Sdim		f_print(fout, "sizeof ( ");
167263509Sdim		/* XXX: should "follow" be 1 ??? */
168263509Sdim		ptype(prefix, type, 0);
169263509Sdim		f_print(fout, ")");
170263509Sdim	}
171249259Sdim	f_print(fout, ",\n");
172249259Sdim}
173249259Sdim