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