Initial revision
[nfs-utils.git] / tools / 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 or with the express written consent of
8  * Sun Microsystems, Inc.
9  *
10  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13  *
14  * Sun RPC is provided with no support and without any obligation on the
15  * part of Sun Microsystems, Inc. to assist in its use, correction,
16  * modification or enhancement.
17  *
18  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20  * OR ANY PART THEREOF.
21  *
22  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23  * or profits or other special, indirect and consequential damages, even if
24  * Sun has been advised of the possibility of such damages.
25  *
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  */
30
31 #ifndef lint
32 static char sccsid[] = "@(#)rpc_tblout.c 1.4 89/02/22 (C) 1988 SMI";
33 #endif
34
35 /*
36  * rpc_tblout.c, Dispatch table outputter for the RPC protocol compiler
37  */
38 #include <stdio.h>
39 #include <string.h>
40 #include "rpc_parse.h"
41 #include "rpc_util.h"
42 #include "rpc_output.h"
43
44 static void     write_table(definition *def);
45 static void     printit(char *prefix, char *type);
46
47 #define TABSIZE         8
48 #define TABCOUNT        5
49 #define TABSTOP         (TABSIZE*TABCOUNT)
50
51 static char tabstr[TABCOUNT+1] = "\t\t\t\t\t";
52
53 static char tbl_hdr[] = "struct rpcgen_table %s_table[] = {\n";
54 static char tbl_end[] = "};\n";
55
56 static char null_entry[] = "\n\t(char *(*)())0,\n\
57  \t(xdrproc_t) xdr_void,\t\t\t0,\n\
58  \t(xdrproc_t) xdr_void,\t\t\t0,\n";
59
60
61 static char tbl_nproc[] = "int %s_nproc =\n\tsizeof(%s_table)/sizeof(%s_table[0]);\n\n";
62
63 void
64 write_tables(void)
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 void
79 write_table(definition *def)
80 {
81         version_list *vp;
82         proc_list *proc;
83         int current;
84         int expected;
85         char progvers[100];
86         int warning;
87
88         for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
89                 warning = 0;
90                 s_print(progvers, "%s_%s",
91                     locase(def->def_name), vp->vers_num);
92                 /* print the table header */
93                 f_print(fout, tbl_hdr, progvers);
94
95                 if (nullproc(vp->procs)) {
96                         expected = 0;
97                 } else {
98                         expected = 1;
99                         f_print(fout, null_entry);
100                 }
101                 for (proc = vp->procs; proc != NULL; proc = proc->next) {
102                         current = atoi(proc->proc_num);
103                         if (current != expected++) {
104                                 f_print(fout,
105                         "\n/*\n * WARNING: table out of order\n */\n");
106                                 if (warning == 0) {
107                                         f_print(stderr,
108                                     "WARNING %s table is out of order\n",
109                                             progvers);
110                                         warning = 1;
111                                         nonfatalerrors = 1;
112                                 }
113                                 expected = current + 1;
114                         }
115                         f_print(fout, "\n\t(char *(*)())RPCGEN_ACTION(");
116
117                         /* routine to invoke */
118                         if( Cflag && !newstyle )
119                           pvname_svc(proc->proc_name, vp->vers_num);
120                         else {
121                           if( newstyle )
122                             f_print( fout, "_");   /* calls internal func */
123                           pvname(proc->proc_name, vp->vers_num);
124                         }
125                         f_print(fout, "),\n");
126
127                         /* argument info */
128                         if( proc->arg_num > 1 )
129                           printit((char*) NULL, proc->args.argname );
130                         else  
131                           /* do we have to do something special for newstyle */
132                           printit( proc->args.decls->decl.prefix,
133                                   proc->args.decls->decl.type );
134                         /* result info */
135                         printit(proc->res_prefix, proc->res_type);
136                 }
137
138                 /* print the table trailer */
139                 f_print(fout, tbl_end);
140                 f_print(fout, tbl_nproc, progvers, progvers, progvers);
141         }
142 }
143
144 static void
145 printit(char *prefix, char *type)
146 {
147         int len;
148         int tabs;
149
150
151         len = fprintf(fout, "\txdr_%s,", stringfix(type));
152         /* account for leading tab expansion */
153         len += TABSIZE - 1;
154         /* round up to tabs required */
155         tabs = (TABSTOP - len + TABSIZE - 1)/TABSIZE;
156         f_print(fout, "%s", &tabstr[TABCOUNT-tabs]);
157
158         if (streq(type, "void")) {
159                 f_print(fout, "0");
160         } else {
161                 f_print(fout, "sizeof ( ");
162                 /* XXX: should "follow" be 1 ??? */
163                 ptype(prefix, type, 0);
164                 f_print(fout, ")");
165         }
166         f_print(fout, ",\n");
167 }