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