entered into RCS
[kopensolaris-gnu/glibc.git] / sunrpc / rpc_hout.c
1 /* @(#)rpc_hout.c       2.1 88/08/01 4.0 RPCSRC */
2 /*
3  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4  * unrestricted use provided that this legend is included on all tape
5  * media and as a part of the software program in whole or part.  Users
6  * may copy or modify Sun RPC without charge, but are not authorized
7  * to license or distribute it to anyone else except as part of a product or
8  * program developed by the user.
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 #ifndef lint
31 static char sccsid[] = "@(#)rpc_hout.c 1.6 87/07/28 (C) 1987 SMI";
32 #endif
33
34 /*
35  * rpc_hout.c, Header file outputter for the RPC protocol compiler 
36  * Copyright (C) 1987, Sun Microsystems, Inc. 
37  */
38 #include <stdio.h>
39 #include <ctype.h>
40 #include "rpc_util.h"
41 #include "rpc_parse.h"
42
43
44 /*
45  * Print the C-version of an xdr definition 
46  */
47 void
48 print_datadef(def)
49         definition *def;
50 {
51         if (def->def_kind != DEF_CONST) {
52                 f_print(fout, "\n");
53         }
54         switch (def->def_kind) {
55         case DEF_STRUCT:
56                 pstructdef(def);
57                 break;
58         case DEF_UNION:
59                 puniondef(def);
60                 break;
61         case DEF_ENUM:
62                 penumdef(def);
63                 break;
64         case DEF_TYPEDEF:
65                 ptypedef(def);
66                 break;
67         case DEF_PROGRAM:
68                 pprogramdef(def);
69                 break;
70         case DEF_CONST:
71                 pconstdef(def);
72                 break;
73         }
74         if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
75                 f_print(fout, "bool_t xdr_%s();\n", def->def_name);
76         }
77         if (def->def_kind != DEF_CONST) {
78                 f_print(fout, "\n");
79         }
80 }
81
82 static
83 pconstdef(def)
84         definition *def;
85 {
86         pdefine(def->def_name, def->def.co);
87 }
88
89 static
90 pstructdef(def)
91         definition *def;
92 {
93         decl_list *l;
94         char *name = def->def_name;
95
96         f_print(fout, "struct %s {\n", name);
97         for (l = def->def.st.decls; l != NULL; l = l->next) {
98                 pdeclaration(name, &l->decl, 1);
99         }
100         f_print(fout, "};\n");
101         f_print(fout, "typedef struct %s %s;\n", name, name);
102 }
103
104 static
105 puniondef(def)
106         definition *def;
107 {
108         case_list *l;
109         char *name = def->def_name;
110         declaration *decl;
111
112         f_print(fout, "struct %s {\n", name);
113         decl = &def->def.un.enum_decl;
114         if (streq(decl->type, "bool")) {
115                 f_print(fout, "\tbool_t %s;\n", decl->name);
116         } else {
117                 f_print(fout, "\t%s %s;\n", decl->type, decl->name);
118         }
119         f_print(fout, "\tunion {\n");
120         for (l = def->def.un.cases; l != NULL; l = l->next) {
121                 pdeclaration(name, &l->case_decl, 2);
122         }
123         decl = def->def.un.default_decl;
124         if (decl && !streq(decl->type, "void")) {
125                 pdeclaration(name, decl, 2);
126         }
127         f_print(fout, "\t} %s_u;\n", name);
128         f_print(fout, "};\n");
129         f_print(fout, "typedef struct %s %s;\n", name, name);
130 }
131
132
133
134 static
135 pdefine(name, num)
136         char *name;
137         char *num;
138 {
139         f_print(fout, "#define %s %s\n", name, num);
140 }
141
142 static
143 puldefine(name, num)
144         char *name;
145         char *num;
146 {
147         f_print(fout, "#define %s ((u_long)%s)\n", name, num);
148 }
149
150 static
151 define_printed(stop, start)
152         proc_list *stop;
153         version_list *start;
154 {
155         version_list *vers;
156         proc_list *proc;
157
158         for (vers = start; vers != NULL; vers = vers->next) {
159                 for (proc = vers->procs; proc != NULL; proc = proc->next) {
160                         if (proc == stop) {
161                                 return (0);
162                         } else if (streq(proc->proc_name, stop->proc_name)) {
163                                 return (1);
164                         }
165                 }
166         }
167         abort();
168         /* NOTREACHED */
169 }
170
171
172 static
173 pprogramdef(def)
174         definition *def;
175 {
176         version_list *vers;
177         proc_list *proc;
178
179         puldefine(def->def_name, def->def.pr.prog_num);
180         for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
181                 puldefine(vers->vers_name, vers->vers_num);
182                 for (proc = vers->procs; proc != NULL; proc = proc->next) {
183                         if (!define_printed(proc, def->def.pr.versions)) {
184                                 puldefine(proc->proc_name, proc->proc_num);
185                         }
186                         pprocdef(proc, vers);
187                 }
188         }
189 }
190
191
192 pprocdef(proc, vp)
193         proc_list *proc;
194         version_list *vp;
195 {
196         f_print(fout, "extern ");
197         if (proc->res_prefix) {
198                 if (streq(proc->res_prefix, "enum")) {
199                         f_print(fout, "enum ");
200                 } else {
201                         f_print(fout, "struct ");
202                 }
203         }
204         if (streq(proc->res_type, "bool")) {
205                 f_print(fout, "bool_t *");
206         } else if (streq(proc->res_type, "string")) {
207                 f_print(fout, "char **");
208         } else {
209                 f_print(fout, "%s *", fixtype(proc->res_type));
210         }
211         pvname(proc->proc_name, vp->vers_num);
212         f_print(fout, "();\n");
213 }
214
215 static
216 penumdef(def)
217         definition *def;
218 {
219         char *name = def->def_name;
220         enumval_list *l;
221         char *last = NULL;
222         int count = 0;
223
224         f_print(fout, "enum %s {\n", name);
225         for (l = def->def.en.vals; l != NULL; l = l->next) {
226                 f_print(fout, "\t%s", l->name);
227                 if (l->assignment) {
228                         f_print(fout, " = %s", l->assignment);
229                         last = l->assignment;
230                         count = 1;
231                 } else {
232                         if (last == NULL) {
233                                 f_print(fout, " = %d", count++);
234                         } else {
235                                 f_print(fout, " = %s + %d", last, count++);
236                         }
237                 }
238                 f_print(fout, ",\n");
239         }
240         f_print(fout, "};\n");
241         f_print(fout, "typedef enum %s %s;\n", name, name);
242 }
243
244 static
245 ptypedef(def)
246         definition *def;
247 {
248         char *name = def->def_name;
249         char *old = def->def.ty.old_type;
250         char prefix[8]; /* enough to contain "struct ", including NUL */
251         relation rel = def->def.ty.rel;
252
253
254         if (!streq(name, old)) {
255                 if (streq(old, "string")) {
256                         old = "char";
257                         rel = REL_POINTER;
258                 } else if (streq(old, "opaque")) {
259                         old = "char";
260                 } else if (streq(old, "bool")) {
261                         old = "bool_t";
262                 }
263                 if (undefined2(old, name) && def->def.ty.old_prefix) {
264                         s_print(prefix, "%s ", def->def.ty.old_prefix);
265                 } else {
266                         prefix[0] = 0;
267                 }
268                 f_print(fout, "typedef ");
269                 switch (rel) {
270                 case REL_ARRAY:
271                         f_print(fout, "struct {\n");
272                         f_print(fout, "\tu_int %s_len;\n", name);
273                         f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
274                         f_print(fout, "} %s", name);
275                         break;
276                 case REL_POINTER:
277                         f_print(fout, "%s%s *%s", prefix, old, name);
278                         break;
279                 case REL_VECTOR:
280                         f_print(fout, "%s%s %s[%s]", prefix, old, name,
281                                 def->def.ty.array_max);
282                         break;
283                 case REL_ALIAS:
284                         f_print(fout, "%s%s %s", prefix, old, name);
285                         break;
286                 }
287                 f_print(fout, ";\n");
288         }
289 }
290
291
292 static
293 pdeclaration(name, dec, tab)
294         char *name;
295         declaration *dec;
296         int tab;
297 {
298         char buf[8];    /* enough to hold "struct ", include NUL */
299         char *prefix;
300         char *type;
301
302         if (streq(dec->type, "void")) {
303                 return;
304         }
305         tabify(fout, tab);
306         if (streq(dec->type, name) && !dec->prefix) {
307                 f_print(fout, "struct ");
308         }
309         if (streq(dec->type, "string")) {
310                 f_print(fout, "char *%s", dec->name);
311         } else {
312                 prefix = "";
313                 if (streq(dec->type, "bool")) {
314                         type = "bool_t";
315                 } else if (streq(dec->type, "opaque")) {
316                         type = "char";
317                 } else {
318                         if (dec->prefix) {
319                                 s_print(buf, "%s ", dec->prefix);
320                                 prefix = buf;
321                         }
322                         type = dec->type;
323                 }
324                 switch (dec->rel) {
325                 case REL_ALIAS:
326                         f_print(fout, "%s%s %s", prefix, type, dec->name);
327                         break;
328                 case REL_VECTOR:
329                         f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
330                                 dec->array_max);
331                         break;
332                 case REL_POINTER:
333                         f_print(fout, "%s%s *%s", prefix, type, dec->name);
334                         break;
335                 case REL_ARRAY:
336                         f_print(fout, "struct {\n");
337                         tabify(fout, tab);
338                         f_print(fout, "\tu_int %s_len;\n", dec->name);
339                         tabify(fout, tab);
340                         f_print(fout, "\t%s%s *%s_val;\n", prefix, type, dec->name);
341                         tabify(fout, tab);
342                         f_print(fout, "} %s", dec->name);
343                         break;
344                 }
345         }
346         f_print(fout, ";\n");
347 }
348
349
350
351 static
352 undefined2(type, stop)
353         char *type;
354         char *stop;
355 {
356         list *l;
357         definition *def;
358
359         for (l = defined; l != NULL; l = l->next) {
360                 def = (definition *) l->val;
361                 if (def->def_kind != DEF_PROGRAM) {
362                         if (streq(def->def_name, stop)) {
363                                 return (1);
364                         } else if (streq(def->def_name, type)) {
365                                 return (0);
366                         }
367                 }
368         }
369         return (1);
370 }