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