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