Fix rxgen-generated warnings
[openafs.git] / src / rxgen / rpc_cout.c
1 /* @(#)rpc_cout.c       1.1 87/11/04 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_cout.c, XDR routine 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
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include "rpc_scan.h"
43 #include "rpc_parse.h"
44 #include "rpc_util.h"
45
46 /* Static prototypes */
47 static int findtype(definition * def, char *type);
48 static int undefined(char *type);
49 static void print_header(definition * def);
50 static void print_trailer(void);
51 static void print_ifopen(int indent, char *name);
52 static void print_ifarg(char *arg);
53 static void print_ifarg_with_cast(int ptr_to, char *type, char *arg);
54 static void print_ifsizeof(char *prefix, char *type);
55 static void print_ifclose(int indent);
56 static void space(void);
57 static void print_ifstat(int indent, char *prefix, char *type, relation rel,
58                          char *amax, char *objname, char *name);
59 static void emit_enum(definition * def);
60 static void emit_union(definition * def);
61 static void emit_struct(definition * def);
62 static void emit_typedef(definition * def);
63 static void print_stat(declaration * dec);
64 static void print_hout(declaration * dec);
65 static void print_cout(declaration * dec);
66 static void print_rxifopen(char *typename);
67 static void print_rxifarg(char *amp, char *arg, int costant);
68 static void print_rxifsizeof(char *prefix, char *type);
69
70
71 /*
72  * Emit the C-routine for the given definition 
73  */
74 void
75 emit(definition * def)
76 {
77     if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
78         return;
79     }
80     print_header(def);
81     switch (def->def_kind) {
82     case DEF_UNION:
83         emit_union(def);
84         break;
85     case DEF_ENUM:
86         emit_enum(def);
87         break;
88     case DEF_STRUCT:
89         emit_struct(def);
90         break;
91     case DEF_TYPEDEF:
92         emit_typedef(def);
93         break;
94     default:
95         break;
96     }
97     print_trailer();
98 }
99
100 static int
101 findtype(definition * def, char *type)
102 {
103     if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
104         return (0);
105     } else {
106         return (streq(def->def_name, type));
107     }
108 }
109
110 static int
111 undefined(char *type)
112 {
113     definition *def;
114
115     def = (definition *) FINDVAL(defined, type, findtype);
116     return (def == NULL);
117 }
118
119
120 static void
121 print_header(definition * def)
122 {
123     space();
124     f_print(fout, "bool_t\n");
125     f_print(fout, "xdr_%s(XDR *xdrs, ", def->def_name);
126     f_print(fout, "%s ", def->def_name);
127 #if 0
128     if (def->def_kind != DEF_TYPEDEF
129         || !isvectordef(def->def.ty.old_type, def->def.ty.rel)) {
130         f_print(fout, "*");
131     }
132 #else
133     f_print(fout, "*");
134 #endif
135     f_print(fout, "objp)\n");
136     f_print(fout, "{\n");
137 }
138
139 static void
140 print_trailer(void)
141 {
142     f_print(fout, "\treturn (TRUE);\n");
143     f_print(fout, "}\n");
144     space();
145 }
146
147
148 static void
149 print_ifopen(int indent, char *name)
150 {
151     tabify(fout, indent);
152     f_print(fout, "if (!xdr_%s(xdrs", name);
153 }
154
155
156 static void
157 print_ifarg(char *arg)
158 {
159     f_print(fout, ", %s", arg);
160 }
161
162
163 static void
164 print_ifarg_with_cast(int ptr_to, char *type, char *arg)
165 {
166     f_print(fout, ptr_to ? ", (%s *) %s" : ", (%s) %s", type, arg);
167 }
168
169 static void
170 print_ifsizeof(char *prefix, char *type)
171 {
172     if (streq(type, "bool")) {
173         f_print(fout, ", sizeof(bool_t), (xdrproc_t) xdr_bool");
174     } else {
175         f_print(fout, ", sizeof(");
176         if (undefined(type) && prefix) {
177             f_print(fout, "%s ", prefix);
178         }
179         f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type);
180     }
181 }
182
183 static void
184 print_ifclose(int indent)
185 {
186     f_print(fout, ")) {\n");
187     tabify(fout, indent);
188     f_print(fout, "\treturn (FALSE);\n");
189     tabify(fout, indent);
190     f_print(fout, "}\n");
191 }
192
193 static void
194 space(void)
195 {
196     f_print(fout, "\n\n");
197 }
198
199 static void
200 print_ifstat(int indent, char *prefix, char *type, relation rel, char *amax,
201              char *objname, char *name)
202 {
203     char *alt = NULL;
204     char *altcast = NULL;
205
206     switch (rel) {
207     case REL_POINTER:
208         print_ifopen(indent, "pointer");
209         print_ifarg_with_cast(1, "char *", objname);
210         print_ifsizeof(prefix, type);
211         break;
212     case REL_VECTOR:
213         if (streq(type, "string")) {
214             alt = "string";
215         } else if (streq(type, "opaque")) {
216             alt = "opaque";
217             altcast = "caddr_t";
218         }
219         if (alt) {
220             print_ifopen(indent, alt);
221             if (altcast) {
222                 print_ifarg_with_cast(0, altcast, objname);
223             } else {
224                 print_ifarg(objname);
225             }
226         } else {
227             print_ifopen(indent, "vector");
228             print_ifarg_with_cast(1, "char", objname);
229         }
230         print_ifarg(amax);
231         if (!alt) {
232             print_ifsizeof(prefix, type);
233         }
234         break;
235     case REL_ARRAY:
236         if (streq(type, "string")) {
237             alt = "string";
238         } else if (streq(type, "opaque")) {
239             alt = "bytes";
240         }
241         if (streq(type, "string")) {
242             print_ifopen(indent, alt);
243             print_ifarg(objname);
244         } else {
245             if (alt) {
246                 print_ifopen(indent, alt);
247                 print_ifarg("(char **)");
248             } else {
249                 print_ifopen(indent, "array");
250                 print_ifarg("(caddr_t *)");
251             }
252             if (*objname == '&') {
253                 f_print(fout, "%s.%s_val, (u_int *)%s.%s_len", objname, name,
254                         objname, name);
255             } else {
256                 f_print(fout, "&%s->%s_val, (u_int *)&%s->%s_len", objname,
257                         name, objname, name);
258             }
259         }
260         print_ifarg(amax);
261         if (!alt) {
262             print_ifsizeof(prefix, type);
263         }
264         break;
265     case REL_ALIAS:
266         print_ifopen(indent, type);
267         print_ifarg_with_cast(1, type, objname);
268         break;
269     }
270     print_ifclose(indent);
271 }
272
273
274 static void
275 emit_enum(definition * def)
276 {
277     print_ifopen(1, "enum");
278     print_ifarg("(enum_t *)objp");
279     print_ifclose(1);
280 }
281
282
283 static void
284 emit_union(definition * def)
285 {
286     declaration *dflt;
287     case_list *cl;
288     declaration *cs;
289     char *object;
290     char *format = "&objp->%s_u.%s";
291
292     print_stat(&def->def.un.enum_decl);
293     f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
294     for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
295         cs = &cl->case_decl;
296         f_print(fout, "\tcase %s:\n", cl->case_name);
297         if (!streq(cs->type, "void")) {
298             object =
299                 alloc(strlen(def->def_name) + strlen(format) +
300                       strlen(cs->name) + 1);
301             s_print(object, format, def->def_name, cs->name);
302             print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max,
303                          object, cs->name);
304             free(object);
305         }
306         f_print(fout, "\t\tbreak;\n");
307     }
308     dflt = def->def.un.default_decl;
309     if (dflt != NULL) {
310         if (!streq(dflt->type, "void")) {
311             f_print(fout, "\tdefault:\n");
312             object =
313                 alloc(strlen(def->def_name) + strlen(format) +
314                       strlen(dflt->name) + 1);
315             s_print(object, format, def->def_name, dflt->name);
316             print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
317                          dflt->array_max, object, dflt->name);
318             free(object);
319             f_print(fout, "\t\tbreak;\n");
320         }
321     } else {
322         f_print(fout, "\tdefault:\n");
323         f_print(fout, "\t\treturn (FALSE);\n");
324     }
325     f_print(fout, "\t}\n");
326 }
327
328
329
330 static void
331 emit_struct(definition * def)
332 {
333     decl_list *dl;
334
335     for (dl = def->def.st.decls; dl != NULL; dl = dl->next) {
336         print_stat(&dl->decl);
337     }
338 }
339
340
341
342
343 static void
344 emit_typedef(definition * def)
345 {
346     char *prefix = def->def.ty.old_prefix;
347     char *type = def->def.ty.old_type;
348     char *amax = def->def.ty.array_max;
349     relation rel = def->def.ty.rel;
350
351     print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
352 }
353
354
355
356
357
358 static void
359 print_stat(declaration * dec)
360 {
361     char *prefix = dec->prefix;
362     char *type = dec->type;
363     char *amax = dec->array_max;
364     relation rel = dec->rel;
365     char name[256];
366
367     if (isvectordef(type, rel)) {
368         s_print(name, "objp->%s", dec->name);
369     } else {
370         s_print(name, "&objp->%s", dec->name);
371     }
372     print_ifstat(1, prefix, type, rel, amax, name, dec->name);
373 }
374
375 static void
376 print_hout(declaration * dec)
377 {
378     char prefix[8];
379
380     if (hflag) {
381         if (dec->prefix)
382             s_print(prefix, "%s ", dec->prefix);
383         else
384             prefix[0] = 0;
385         f_print(fout, "\ntypedef ");
386         switch (dec->rel) {
387         case REL_ARRAY:
388             f_print(fout, "struct %s {\n", dec->name);
389             f_print(fout, "\tu_int %s_len;\n", dec->name);
390             f_print(fout, "\t%s%s *%s_val;\n", prefix, dec->type, dec->name);
391             f_print(fout, "} %s", dec->name);
392             break;
393         default:
394             break;
395         }
396         f_print(fout, ";\n");
397         f_print(fout, "bool_t xdr_%s(XDR *xdrs, %s *objp);\n", dec->name,
398                 dec->name);
399     }
400 }
401
402
403 static void
404 print_cout(declaration * dec)
405 {
406     if (cflag) {
407         space();
408         f_print(fout, "bool_t\n");
409         f_print(fout, "xdr_%s(XDR *xdrs, %s *objp)\n", dec->name, dec->name);
410         f_print(fout, "{\n");
411         print_ifstat(1, dec->prefix, dec->type, dec->rel, dec->array_max,
412                      "objp", dec->name);
413         print_trailer();
414     }
415 }
416
417
418 static void
419 print_rxifopen(char *typename)
420 {
421     sprintf(Proc_list->code, "xdr_%s(&z_xdrs", typename);
422     sprintf(Proc_list->scode, "xdr_%s(z_xdrs", typename);
423 }
424
425
426 static void
427 print_rxifarg(char *amp, char *arg, int costant)
428 {
429     char code[100], scode[100];
430
431     sprintf(code, ", %s%s", amp, arg);
432     if (costant)
433         sprintf(scode, ", %s", arg);
434     else
435         sprintf(scode, ", &%s", arg);
436     strcat(Proc_list->code, code);
437     strcat(Proc_list->scode, scode);
438 }
439
440
441 static void
442 print_rxifsizeof(char *prefix, char *type)
443 {
444     char name[256];
445
446     if (streq(type, "bool")) {
447         strcat(Proc_list->code, ", sizeof(bool_t), xdr_bool");
448         strcat(Proc_list->scode, ", sizeof(bool_t), xdr_bool");
449     } else {
450         strcat(Proc_list->code, ", sizeof(");
451         strcat(Proc_list->scode, ", sizeof(");
452         if (undefined(type) && prefix) {
453             sprintf(name, "%s ", prefix);
454             strcat(Proc_list->code, name);
455             strcat(Proc_list->scode, name);
456         }
457         sprintf(name, "%s), xdr_%s", type, type);
458         strcat(Proc_list->code, name);
459         strcat(Proc_list->scode, name);
460     }
461 }
462
463
464 void
465 print_param(declaration * dec)
466 {
467     char *prefix = dec->prefix;
468     char *type = dec->type;
469     char *amax = dec->array_max;
470     relation rel = dec->rel;
471     char *name = dec->name;
472     char *alt = NULL;
473     char temp[256];
474     char *objname, *amp = "";
475
476     if (rel == REL_POINTER)
477         Proc_list->pl.param_flag |= INDIRECT_PARAM;
478     else {
479         amp = "&";
480         Proc_list->pl.param_flag &= ~INDIRECT_PARAM;
481     }
482     objname = Proc_list->pl.param_name;
483     switch (rel) {
484     case REL_POINTER:
485         print_rxifopen(type);
486         print_rxifarg(amp, objname, 0);
487 /*
488                 print_rxifopen("pointer");
489                 print_rxifarg(amp, "(char **)", 1);
490                 sprintf(temp, "%s", objname);
491                 strcat(Proc_list->code, temp);
492                 strcat(Proc_list->scode, temp);
493                 print_rxifsizeof(prefix, type);
494 */
495         break;
496     case REL_VECTOR:
497         if (streq(type, "string")) {
498             alt = "string";
499         } else if (streq(type, "opaque")) {
500             alt = "opaque";
501         }
502         if (alt) {
503             print_rxifopen(alt);
504             print_rxifarg(amp, objname, 0);
505         } else {
506             print_rxifopen("vector");
507             print_rxifarg(amp, "(char *)", 0);
508             sprintf(temp, "%s", objname);
509             strcat(Proc_list->code, temp);
510             strcat(Proc_list->scode, temp);
511         }
512         print_rxifarg("", amax, 1);
513         if (!alt) {
514             print_rxifsizeof(prefix, type);
515         }
516         break;
517     case REL_ARRAY:
518         if (streq(type, "string")) {
519             alt = "string";
520         } else if (streq(type, "opaque")) {
521             alt = "bytes";
522         }
523         if (streq(type, "string")) {
524             print_rxifopen(alt);
525             if ((Proc_list->pl.param_kind == DEF_OUTPARAM)
526                 || (Proc_list->pl.param_kind == DEF_INOUTPARAM)) {
527                 Proc_list->pl.param_flag |= OUT_STRING;
528                 print_rxifarg("", objname, 0);
529             } else
530                 print_rxifarg("&", objname, 0);
531 /*                      print_rxifarg(amp, objname, 0); */
532             print_rxifarg("", amax, 1);
533             if (!alt) {
534                 print_rxifsizeof(prefix, type);
535             }
536         } else {
537             char typecontents[100];
538
539             print_hout(dec);
540             print_cout(dec);
541             strcpy(temp, dec->name);
542             strcpy(typecontents, dec->name);
543             strcat(typecontents, " *");
544             strcpy(Proc_list->pl.param_type, typecontents);
545             sprintf(typecontents, "%s_%d", Proc_list->pl.param_name,
546                     ++PerProcCounter);
547             strcpy(Proc_list->pl.param_name, typecontents);
548             Proc_list->pl.param_flag |= FREETHIS_PARAM;
549             print_rxifopen(temp);
550             print_rxifarg(amp, name, 0);
551         }
552 /*
553                         if (alt) {
554                                 print_rxifopen(alt);
555                         } else {
556                                 print_rxifopen("array");
557                         }
558                         print_rxifarg(amp, "(char **)", 1);
559                         if (*objname == '&') {
560                                 sprintf(temp, "%s.%s_val, (u_int *)%s.%s_len",
561                                         objname, name, objname, name);
562                         } else {
563                                 sprintf(temp, "&%s->%s_val, (u_int *)&%s->%s_len",
564                                         objname, name, objname, name);
565                         }
566                         strcat(Proc_list->code, temp);
567                         strcat(Proc_list->scode, temp);
568                 }
569                 print_rxifarg("", amax, 1);
570                 if (!alt) {
571                         print_rxifsizeof(prefix, type);
572                 }
573 */
574         break;
575     case REL_ALIAS:
576         print_rxifopen(type);
577         print_rxifarg(amp, objname, 0);
578         break;
579     }
580     strcat(Proc_list->code, ")");
581     strcat(Proc_list->scode, ")");
582 }