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