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