Sun agreed to a change of the license for the RPC code to a BSD-like license.
[platform/upstream/glibc.git] / sunrpc / rpc_tblout.c
1 /*
2  * From: @(#)rpc_tblout.c 1.4 89/02/22 (C) 1988 SMI
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  *       notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  *       copyright notice, this list of conditions and the following
12  *       disclaimer in the documentation and/or other materials
13  *       provided with the distribution.
14  *     * Neither the name of Sun Microsystems, Inc. nor the names of its
15  *       contributors may be used to endorse or promote products derived
16  *       from this software without specific prior written permission.
17  *
18  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 /*
33  * rpc_tblout.c, Dispatch table outputter for the RPC protocol compiler
34  */
35 #include <stdio.h>
36 #include <string.h>
37 #include "rpc_parse.h"
38 #include "rpc_util.h"
39 #include "proto.h"
40
41 #define TABSIZE         8
42 #define TABCOUNT        5
43 #define TABSTOP         (TABSIZE*TABCOUNT)
44
45 static const char tabstr[TABCOUNT + 1] = "\t\t\t\t\t";
46
47 static const char tbl_hdr[] = "struct rpcgen_table %s_table[] = {\n";
48 static const char tbl_end[] = "};\n";
49
50 static const 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 const char tbl_nproc[] = "int %s_nproc =\n\tsizeof(%s_table)/sizeof(%s_table[0]);\n\n";
56
57 static void write_table (const definition * def);
58 static void printit (const char *prefix, const char *type);
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     {
69       def = (definition *) l->val;
70       if (def->def_kind == DEF_PROGRAM)
71         {
72           write_table (def);
73         }
74     }
75 }
76
77 static void
78 write_table (const definition * def)
79 {
80   version_list *vp;
81   proc_list *proc;
82   int current;
83   int expected;
84   char progvers[100];
85   int warning;
86
87   for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
88     {
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         {
97           expected = 0;
98         }
99       else
100         {
101           expected = 1;
102           f_print (fout, null_entry);
103         }
104       for (proc = vp->procs; proc != NULL; proc = proc->next)
105         {
106           current = atoi (proc->proc_num);
107           if (current != expected++)
108             {
109               f_print (fout,
110                        "\n/*\n * WARNING: table out of order\n */\n");
111               if (warning == 0)
112                 {
113                   f_print (stderr,
114                            "WARNING %s table is out of order\n",
115                            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             {
128               if (newstyle)
129                 f_print (fout, "_");    /* calls internal func */
130               pvname (proc->proc_name, vp->vers_num);
131             }
132           f_print (fout, "),\n");
133
134           /* argument info */
135           if (proc->arg_num > 1)
136             printit ((char *) NULL, proc->args.argname);
137           else
138             /* do we have to do something special for newstyle */
139             printit (proc->args.decls->decl.prefix,
140                      proc->args.decls->decl.type);
141           /* result info */
142           printit (proc->res_prefix, proc->res_type);
143         }
144
145       /* print the table trailer */
146       f_print (fout, tbl_end);
147       f_print (fout, tbl_nproc, progvers, progvers, progvers);
148     }
149 }
150
151 static void
152 printit (const char *prefix, const char *type)
153 {
154   int len;
155   int tabs;
156
157
158   len = fprintf (fout, "\txdr_%s,", stringfix (type));
159   /* account for leading tab expansion */
160   len += TABSIZE - 1;
161   /* round up to tabs required */
162   tabs = (TABSTOP - len + TABSIZE - 1) / TABSIZE;
163   f_print (fout, "%s", &tabstr[TABCOUNT - tabs]);
164
165   if (streq (type, "void"))
166     {
167       f_print (fout, "0");
168     }
169   else
170     {
171       f_print (fout, "sizeof ( ");
172       /* XXX: should "follow" be 1 ??? */
173       ptype (prefix, type, 0);
174       f_print (fout, ")");
175     }
176   f_print (fout, ",\n");
177 }