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