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