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