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