1/*
2 * From: @(#)rpc_tblout.c 1.4 89/02/22
3 *
4 * Copyright (c) 2010, Oracle America, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 * * Neither the name of the "Oracle America, Inc." nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * rpc_tblout.c, Dispatch table outputter for the RPC protocol compiler
35 */
36#include <stdio.h>
37#include <string.h>
38#include "rpc_parse.h"
39#include "rpc_util.h"
40#include "proto.h"
41
42#define TABSIZE 8
43#define TABCOUNT 5
44#define TABSTOP (TABSIZE*TABCOUNT)
45
46static const char tabstr[TABCOUNT + 1] = "\t\t\t\t\t";
47
48static const char tbl_hdr[] = "struct rpcgen_table %s_table[] = {\n";
49static const char tbl_end[] = "};\n";
50
51static const char null_entry[] = "\n\t(char *(*)())0,\n\
52 \t(xdrproc_t) xdr_void,\t\t\t0,\n\
53 \t(xdrproc_t) xdr_void,\t\t\t0,\n";
54
55
56static const char tbl_nproc[] = "int %s_nproc =\n\tsizeof(%s_table)/sizeof(%s_table[0]);\n\n";
57
58static void write_table (const definition * def);
59static void printit (const char *prefix, const char *type);
60
61void
62write_tables (void)
63{
64 list *l;
65 definition *def;
66
67 f_print (fout, "\n");
68 for (l = defined; l != NULL; l = l->next)
69 {
70 def = (definition *) l->val;
71 if (def->def_kind == DEF_PROGRAM)
72 {
73 write_table (def);
74 }
75 }
76}
77
78static void
79write_table (const definition * def)
80{
81 version_list *vp;
82 proc_list *proc;
83 int current;
84 int expected;
85 char progvers[100];
86 int warning;
87
88 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
89 {
90 warning = 0;
91 s_print (progvers, "%s_%s",
92 locase (def->def_name), vp->vers_num);
93 /* print the table header */
94 f_print (fout, tbl_hdr, progvers);
95
96 if (nullproc (vp->procs))
97 {
98 expected = 0;
99 }
100 else
101 {
102 expected = 1;
103 f_print (fout, null_entry);
104 }
105 for (proc = vp->procs; proc != NULL; proc = proc->next)
106 {
107 current = atoi (proc->proc_num);
108 if (current != expected++)
109 {
110 f_print (fout,
111 "\n/*\n * WARNING: table out of order\n */\n");
112 if (warning == 0)
113 {
114 f_print (stderr,
115 "WARNING %s table is out of order\n",
116 progvers);
117 warning = 1;
118 nonfatalerrors = 1;
119 }
120 expected = current + 1;
121 }
122 f_print (fout, "\n\t(char *(*)())RPCGEN_ACTION(");
123
124 /* routine to invoke */
125 if (Cflag && !newstyle)
126 pvname_svc (proc->proc_name, vp->vers_num);
127 else
128 {
129 if (newstyle)
130 f_print (fout, "_"); /* calls internal func */
131 pvname (proc->proc_name, vp->vers_num);
132 }
133 f_print (fout, "),\n");
134
135 /* argument info */
136 if (proc->arg_num > 1)
137 printit ((char *) NULL, proc->args.argname);
138 else
139 /* do we have to do something special for newstyle */
140 printit (proc->args.decls->decl.prefix,
141 proc->args.decls->decl.type);
142 /* result info */
143 printit (proc->res_prefix, proc->res_type);
144 }
145
146 /* print the table trailer */
147 f_print (fout, tbl_end);
148 f_print (fout, tbl_nproc, progvers, progvers, progvers);
149 }
150}
151
152static void
153printit (const char *prefix, const char *type)
154{
155 int len;
156 int tabs;
157
158
159 len = fprintf (fout, "\txdr_%s,", stringfix (type));
160 /* account for leading tab expansion */
161 len += TABSIZE - 1;
162 /* round up to tabs required */
163 tabs = (TABSTOP - len + TABSIZE - 1) / TABSIZE;
164 f_print (fout, "%s", &tabstr[TABCOUNT - tabs]);
165
166 if (streq (type, "void"))
167 {
168 f_print (fout, "0");
169 }
170 else
171 {
172 f_print (fout, "sizeof ( ");
173 /* XXX: should "follow" be 1 ??? */
174 ptype (prefix, type, 0);
175 f_print (fout, ")");
176 }
177 f_print (fout, ",\n");
178}
179