xref: /netbsd-src/usr.bin/rpcgen/rpc_tblout.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: rpc_tblout.c,v 1.3 1995/06/24 15:00:15 pk 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 #ifndef lint
33 static char sccsid[] = "@(#)rpc_tblout.c 1.4 89/02/22 (C) 1988 SMI";
34 #endif
35 
36 /*
37  * rpc_tblout.c, Dispatch table outputter for the RPC protocol compiler
38  */
39 #include <sys/cdefs.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include "rpc_parse.h"
43 #include "rpc_util.h"
44 
45 #define TABSIZE		8
46 #define TABCOUNT	5
47 #define TABSTOP		(TABSIZE*TABCOUNT)
48 
49 static char tabstr[TABCOUNT+1] = "\t\t\t\t\t";
50 
51 static char tbl_hdr[] = "struct rpcgen_table %s_table[] = {\n";
52 static char tbl_end[] = "};\n";
53 
54 static char null_entry[] = "\n\t(char *(*)())0,\n\
55  \t(xdrproc_t) xdr_void,\t\t\t0,\n\
56  \t(xdrproc_t) xdr_void,\t\t\t0,\n";
57 
58 static char tbl_nproc[] = "int %s_nproc =\n\tsizeof(%s_table)/sizeof(%s_table[0]);\n\n";
59 
60 static write_table __P((definition *));
61 static printit __P((char *, char *));
62 
63 void
64 write_tables()
65 {
66 	list *l;
67 	definition *def;
68 
69 	f_print(fout, "\n");
70 	for (l = defined; l != NULL; l = l->next) {
71 		def = (definition *) l->val;
72 		if (def->def_kind == DEF_PROGRAM) {
73 			write_table(def);
74 		}
75 	}
76 }
77 
78 static
79 write_table(def)
80 	definition *def;
81 {
82 	version_list *vp;
83 	proc_list *proc;
84 	int current;
85 	int expected;
86 	char progvers[100];
87 	int warning;
88 
89 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
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 			expected = 0;
98 		} else {
99 			expected = 1;
100 			f_print(fout, null_entry);
101 		}
102 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
103 			current = atoi(proc->proc_num);
104 			if (current != expected++) {
105 				f_print(fout,
106 			"\n/*\n * WARNING: table out of order\n */\n");
107 				if (warning == 0) {
108 					f_print(stderr,
109 				    "WARNING %s table is out of order\n",
110 					    progvers);
111 					warning = 1;
112 					nonfatalerrors = 1;
113 				}
114 				expected = current + 1;
115 			}
116 			f_print(fout, "\n\t(char *(*)())RPCGEN_ACTION(");
117 
118 			/* routine to invoke */
119 			if ( !newstyle)
120 			  pvname_svc(proc->proc_name, vp->vers_num);
121 			else {
122 			  if( newstyle )
123 			    f_print( fout, "_");   /* calls internal func */
124 			  pvname(proc->proc_name, vp->vers_num);
125 			}
126 			f_print(fout, "),\n");
127 
128 			/* argument info */
129 			if( proc->arg_num > 1 )
130 			  printit((char*) NULL, proc->args.argname );
131 			else
132 			  /* do we have to do something special for newstyle */
133 			  printit( proc->args.decls->decl.prefix,
134 				  proc->args.decls->decl.type );
135 			/* result info */
136 			printit(proc->res_prefix, proc->res_type);
137 		}
138 
139 		/* print the table trailer */
140 		f_print(fout, tbl_end);
141 		f_print(fout, tbl_nproc, progvers, progvers, progvers);
142 	}
143 }
144 
145 static
146 printit(prefix, type)
147 	char *prefix;
148 	char *type;
149 {
150 	int len;
151 	int tabs;
152 
153 
154  	len = fprintf(fout, "\txdr_%s,", stringfix(type));
155 	/* account for leading tab expansion */
156 	len += TABSIZE - 1;
157 	/* round up to tabs required */
158 	tabs = (TABSTOP - len + TABSIZE - 1)/TABSIZE;
159 	f_print(fout, "%s", &tabstr[TABCOUNT-tabs]);
160 
161 	if (streq(type, "void")) {
162 		f_print(fout, "0");
163 	} else {
164 		f_print(fout, "sizeof ( ");
165 		/* XXX: should "follow" be 1 ??? */
166 		ptype(prefix, type, 0);
167 		f_print(fout, ")");
168 	}
169 	f_print(fout, ",\n");
170 }
171