xref: /titanic_51/usr/src/cmd/rpcgen/rpc_tblout.c (revision 06e46062ef4f5f4b687cbafb4518fb123fe23920)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 /*
30  * University Copyright- Copyright (c) 1982, 1986, 1988
31  * The Regents of the University of California
32  * All Rights Reserved
33  *
34  * University Acknowledgment- Portions of this document are derived from
35  * software developed by the University of California, Berkeley, and its
36  * contributors.
37  */
38 
39 #pragma ident	"%Z%%M%	%I%	%E% SMI"
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 "rpc_parse.h"
47 #include "rpc_util.h"
48 
49 extern int nullproc(proc_list *);
50 
51 static void write_table(definition *);
52 static void printit(char *, char *);
53 
54 #define	TABSIZE		8
55 #define	TABCOUNT	5
56 #define	TABSTOP		(TABSIZE*TABCOUNT)
57 
58 static char tabstr[TABCOUNT+1] = "\t\t\t\t\t";
59 
60 static char tbl_hdr[] = "struct rpcgen_table %s_table[] = {\n";
61 static char tbl_end[] = "};\n";
62 
63 static char null_entry_b[] = "\n\t(char *(*)())0,\n"
64 			" \t(xdrproc_t)xdr_void,\t\t\t0,\n"
65 			" \t(xdrproc_t)xdr_void,\t\t\t0,\n";
66 
67 static char null_entry[] = "\n\t(void *(*)())0,\n"
68 			" \t(xdrproc_t)xdr_void,\t\t\t0,\n"
69 			" \t(xdrproc_t)xdr_void,\t\t\t0,\n";
70 
71 
72 static char tbl_nproc[] = "int %s_nproc =\n\tsizeof(%s_table)"
73 				"/sizeof(%s_table[0]);\n\n";
74 
75 void
76 write_tables(void)
77 {
78 	list *l;
79 	definition *def;
80 
81 	f_print(fout, "\n");
82 	for (l = defined; l != NULL; l = l->next) {
83 		def = (definition *)l->val;
84 		if (def->def_kind == DEF_PROGRAM) {
85 			write_table(def);
86 		}
87 	}
88 }
89 
90 static void
91 write_table(definition *def)
92 {
93 	version_list *vp;
94 	proc_list *proc;
95 	int current;
96 	int expected;
97 	char progvers[100];
98 	int warning;
99 
100 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
101 		warning = 0;
102 		(void) snprintf(progvers, sizeof (progvers), "%s_%s",
103 		    locase(def->def_name), vp->vers_num);
104 		/* print the table header */
105 		f_print(fout, tbl_hdr, progvers);
106 
107 		if (nullproc(vp->procs)) {
108 			expected = 0;
109 		} else {
110 			expected = 1;
111 			if (tirpcflag)
112 				f_print(fout, null_entry);
113 			else
114 				f_print(fout, null_entry_b);
115 		}
116 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
117 			current = atoi(proc->proc_num);
118 			if (current != expected++) {
119 				f_print(fout,
120 			"\n/*\n * WARNING: table out of order\n */\n");
121 				if (warning == 0) {
122 					f_print(stderr,
123 				    "WARNING %s table is out of order\n",
124 					    progvers);
125 					warning = 1;
126 					nonfatalerrors = 1;
127 				}
128 				expected = current + 1;
129 			}
130 			if (tirpcflag)
131 				f_print(fout,
132 					"\n\t(void *(*)())RPCGEN_ACTION(");
133 			else
134 				f_print(fout,
135 					"\n\t(char *(*)())RPCGEN_ACTION(");
136 
137 			/* routine to invoke */
138 			if (Cflag && !newstyle)
139 				pvname_svc(proc->proc_name, vp->vers_num);
140 			else {
141 				if (newstyle) /* calls internal func */
142 					f_print(fout, "_");
143 				pvname(proc->proc_name, vp->vers_num);
144 			}
145 			f_print(fout, "),\n");
146 
147 			/* argument info */
148 			if (proc->arg_num > 1)
149 				printit(NULL, proc->args.argname);
150 			else
151 			/* do we have to do something special for newstyle */
152 				printit(proc->args.decls->decl.prefix,
153 					proc->args.decls->decl.type);
154 			/* result info */
155 			printit(proc->res_prefix, proc->res_type);
156 		}
157 
158 		/* print the table trailer */
159 		f_print(fout, tbl_end);
160 		f_print(fout, tbl_nproc, progvers, progvers, progvers);
161 	}
162 }
163 
164 static void
165 printit(char *prefix, char *type)
166 {
167 	int len;
168 	int tabs;
169 
170 
171 	if (streq(type, "oneway"))
172 		len = fprintf(fout, "\t(xdrproc_t)xdr_void,");
173 	else
174 		len = fprintf(fout, "\t(xdrproc_t)xdr_%s,", stringfix(type));
175 	/* account for leading tab expansion */
176 	len += TABSIZE - 1;
177 	if (len >= TABSTOP) {
178 		f_print(fout, "\n");
179 		len = 0;
180 	}
181 	/* round up to tabs required */
182 	tabs = (TABSTOP - len + TABSIZE - 1)/TABSIZE;
183 	f_print(fout, "%s", &tabstr[TABCOUNT-tabs]);
184 
185 	if (streq(type, "void") || streq(type, "oneway")) {
186 		f_print(fout, "0");
187 	} else {
188 		f_print(fout, "sizeof ( ");
189 		/* XXX: should "follow" be 1 ??? */
190 		ptype(prefix, type, 0);
191 		f_print(fout, ")");
192 	}
193 	f_print(fout, ",\n");
194 }
195