]> git.decadent.org.uk Git - nfs-utils.git/blob - tools/rpcgen/rpc_tblout.c
rpc.statd: Fix socket binding loop.
[nfs-utils.git] / tools / rpcgen / rpc_tblout.c
1 /*
2  * Copyright (c) 2009, Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * - Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  *   this list of conditions and the following disclaimer in the documentation
11  *   and/or other materials provided with the distribution.
12  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13  *   contributors may be used to endorse or promote products derived
14  *   from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #if 0
30 static char sccsid[] = "@(#)rpc_tblout.c 1.4 89/02/22 (C) 1988 SMI";
31 #endif
32
33 /*
34  * rpc_tblout.c, Dispatch table outputter for the RPC protocol compiler
35  */
36 #include <stdio.h>
37 #include <string.h>
38 #include "rpc_parse.h"
39 #include "rpc_util.h"
40 #include "rpc_output.h"
41
42 static void     write_table(definition *def);
43 static void     printit(char *prefix, char *type);
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
59 static char tbl_nproc[] = "int %s_nproc =\n\tsizeof(%s_table)/sizeof(%s_table[0]);\n\n";
60
61 void
62 write_tables(void)
63 {
64         list *l;
65         definition *def;
66
67         f_print(fout, "\n");
68         for (l = defined; l != NULL; l = l->next) {
69                 def = (definition *) l->val;
70                 if (def->def_kind == DEF_PROGRAM) {
71                         write_table(def);
72                 }
73         }
74 }
75
76 static void
77 write_table(definition *def)
78 {
79         version_list *vp;
80         proc_list *proc;
81         int current;
82         int expected;
83         char progvers[100];
84         int warning;
85
86         for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
87                 warning = 0;
88                 s_print(progvers, "%s_%s",
89                     locase(def->def_name), vp->vers_num);
90                 /* print the table header */
91                 f_print(fout, tbl_hdr, progvers);
92
93                 if (nullproc(vp->procs)) {
94                         expected = 0;
95                 } else {
96                         expected = 1;
97                         f_print(fout, null_entry);
98                 }
99                 for (proc = vp->procs; proc != NULL; proc = proc->next) {
100                         current = atoi(proc->proc_num);
101                         if (current != expected++) {
102                                 f_print(fout,
103                         "\n/*\n * WARNING: table out of order\n */\n");
104                                 if (warning == 0) {
105                                         f_print(stderr,
106                                     "WARNING %s table is out of order\n",
107                                             progvers);
108                                         warning = 1;
109                                         nonfatalerrors = 1;
110                                 }
111                                 expected = current + 1;
112                         }
113                         f_print(fout, "\n\t(char *(*)())RPCGEN_ACTION(");
114
115                         /* routine to invoke */
116                         if( Cflag && !newstyle )
117                           pvname_svc(proc->proc_name, vp->vers_num);
118                         else {
119                           if( newstyle )
120                             f_print( fout, "_");   /* calls internal func */
121                           pvname(proc->proc_name, vp->vers_num);
122                         }
123                         f_print(fout, "),\n");
124
125                         /* argument info */
126                         if( proc->arg_num > 1 )
127                           printit((char*) NULL, proc->args.argname );
128                         else  
129                           /* do we have to do something special for newstyle */
130                           printit( proc->args.decls->decl.prefix,
131                                   proc->args.decls->decl.type );
132                         /* result info */
133                         printit(proc->res_prefix, proc->res_type);
134                 }
135
136                 /* print the table trailer */
137                 f_print(fout, tbl_end);
138                 f_print(fout, tbl_nproc, progvers, progvers, progvers);
139         }
140 }
141
142 static void
143 printit(char *prefix, char *type)
144 {
145         int len;
146         int tabs;
147
148
149         len = fprintf(fout, "\txdr_%s,", stringfix(type));
150         /* account for leading tab expansion */
151         len += TABSIZE - 1;
152         /* round up to tabs required */
153         tabs = (TABSTOP - len + TABSIZE - 1)/TABSIZE;
154         f_print(fout, "%s", &tabstr[TABCOUNT-tabs]);
155
156         if (streq(type, "void")) {
157                 f_print(fout, "0");
158         } else {
159                 f_print(fout, "sizeof ( ");
160                 /* XXX: should "follow" be 1 ??? */
161                 ptype(prefix, type, 0);
162                 f_print(fout, ")");
163         }
164         f_print(fout, ",\n");
165 }