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