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