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