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