1 /* @(#)rpc_cout.c 1.1 87/11/04 3.9 RPCSRC */
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.
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.
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.
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.
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.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
32 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
33 * Copyright (C) 1987, Sun Microsystems, Inc.
35 #include <afsconfig.h>
36 #include <afs/param.h>
41 #include "rpc_parse.h"
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);
70 * Emit the C-routine for the given definition
73 emit(definition * def)
75 if (def->def_kind == DEF_CONST) {
79 switch (def->def_kind) {
99 findtype(definition * def, char *type)
101 if (def->def_kind == DEF_CONST) {
104 return (streq(def->def_name, type));
109 undefined(char *type)
113 def = (definition *) FINDVAL(defined, type, findtype);
114 return (def == NULL);
119 print_header(definition * def)
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);
126 if (def->def_kind != DEF_TYPEDEF
127 || !isvectordef(def->def.ty.old_type, def->def.ty.rel)) {
133 f_print(fout, "objp)\n");
134 f_print(fout, "{\n");
140 f_print(fout, "\treturn (TRUE);\n");
141 f_print(fout, "}\n");
147 print_ifopen(int indent, char *name)
149 tabify(fout, indent);
150 f_print(fout, "if (!xdr_%s(xdrs", name);
155 print_ifarg(char *arg)
157 f_print(fout, ", %s", arg);
162 print_ifarg_with_cast(int ptr_to, char *type, char *arg)
164 if (streq(type, "bool")) {
165 f_print(fout, ptr_to ? ", (bool_t *) %s" : ", (bool_t) %s", arg);
167 f_print(fout, ptr_to ? ", (%s *) %s" : ", (%s) %s", type, arg);
172 print_ifsizeof(char *prefix, char *type)
174 if (streq(type, "bool")) {
175 f_print(fout, ", sizeof(bool_t), (xdrproc_t) xdr_bool");
177 f_print(fout, ", sizeof(");
178 if (undefined(type) && prefix) {
179 f_print(fout, "%s ", prefix);
181 f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type);
186 print_ifclose(int indent)
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");
198 f_print(fout, "\n\n");
202 print_ifstat(int indent, char *prefix, char *type, relation rel, char *amax,
203 char *objname, char *name)
206 char *altcast = NULL;
210 print_ifopen(indent, "pointer");
211 print_ifarg_with_cast(1, "char *", objname);
212 print_ifsizeof(prefix, type);
215 if (streq(type, "string")) {
217 } else if (streq(type, "opaque")) {
222 print_ifopen(indent, alt);
224 print_ifarg_with_cast(0, altcast, objname);
226 print_ifarg(objname);
229 print_ifopen(indent, "vector");
230 print_ifarg_with_cast(1, "char", objname);
234 print_ifsizeof(prefix, type);
238 if (streq(type, "string")) {
240 } else if (streq(type, "opaque")) {
243 if (streq(type, "string")) {
244 print_ifopen(indent, alt);
245 print_ifarg(objname);
248 print_ifopen(indent, alt);
249 print_ifarg("(char **)");
251 print_ifopen(indent, "array");
252 print_ifarg("(caddr_t *)");
254 if (*objname == '&') {
256 f_print(fout, "%s.val, (u_int *)%s.len", objname, objname);
258 f_print(fout, "%s.%s_val, (u_int *)%s.%s_len",
259 objname, name, objname, name);
263 f_print(fout, "&%s->val, (u_int *)&%s->len",
266 f_print(fout, "&%s->%s_val, (u_int *)&%s->%s_len",
267 objname, name, objname, name);
273 print_ifsizeof(prefix, type);
277 print_ifopen(indent, type);
278 print_ifarg_with_cast(1, type, objname);
281 print_ifclose(indent);
286 emit_enum(definition * def)
288 print_ifopen(1, "enum");
289 print_ifarg("(enum_t *)objp");
295 emit_union(definition * def)
301 char *format = "&objp->%s_u.%s";
302 char *briefformat = "&objp->u.%s";
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) {
308 f_print(fout, "\tcase %s:\n", cl->case_name);
309 if (!streq(cs->type, "void")) {
311 alloc(strlen(def->def_name) + strlen(format) +
312 strlen(cs->name) + 1);
315 s_print(object, briefformat, cs->name);
317 s_print(object, format, def->def_name, cs->name);
319 print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max,
323 f_print(fout, "\t\tbreak;\n");
325 dflt = def->def.un.default_decl;
327 if (!streq(dflt->type, "void")) {
328 f_print(fout, "\tdefault:\n");
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);
336 f_print(fout, "\t\tbreak;\n");
339 f_print(fout, "\tdefault:\n");
340 f_print(fout, "\t\treturn (FALSE);\n");
342 f_print(fout, "\t}\n");
348 emit_struct(definition * def)
352 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) {
353 print_stat(&dl->decl);
361 emit_typedef(definition * def)
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;
368 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
376 print_stat(declaration * dec)
378 char *prefix = dec->prefix;
379 char *type = dec->type;
380 char *amax = dec->array_max;
381 relation rel = dec->rel;
384 if (isvectordef(type, rel)) {
385 s_print(name, "objp->%s", dec->name);
387 s_print(name, "&objp->%s", dec->name);
389 print_ifstat(1, prefix, type, rel, amax, name, dec->name);
393 print_hout(declaration * dec)
399 s_print(prefix, "%s ", dec->prefix);
402 f_print(fout, "\ntypedef ");
405 f_print(fout, "struct %s {\n", dec->name);
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);
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);
415 f_print(fout, "} %s", dec->name);
420 f_print(fout, ";\n");
421 f_print(fout, "bool_t xdr_%s(XDR *xdrs, %s *objp);\n", dec->name,
428 print_cout(declaration * dec)
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,
443 print_rxifopen(char *typename)
445 sprintf(Proc_list->code, "xdr_%s(&z_xdrs", typename);
446 sprintf(Proc_list->scode, "xdr_%s(z_xdrs", typename);
451 print_rxifarg(char *amp, char *arg, int costant)
453 char code[100], scode[100];
455 sprintf(code, ", %s%s", amp, arg);
457 sprintf(scode, ", %s", arg);
459 sprintf(scode, ", &%s", arg);
460 strcat(Proc_list->code, code);
461 strcat(Proc_list->scode, scode);
466 print_rxifsizeof(char *prefix, char *type)
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");
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);
481 sprintf(name, "%s), xdr_%s", type, type);
482 strcat(Proc_list->code, name);
483 strcat(Proc_list->scode, name);
489 print_param(declaration * dec)
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;
498 char *objname, *amp = "";
500 if (rel == REL_POINTER)
501 Proc_list->pl.param_flag |= INDIRECT_PARAM;
504 Proc_list->pl.param_flag &= ~INDIRECT_PARAM;
506 objname = Proc_list->pl.param_name;
509 print_rxifopen(type);
510 print_rxifarg(amp, objname, 0);
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);
521 if (streq(type, "string")) {
523 } else if (streq(type, "opaque")) {
529 print_rxifopen("vector");
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);
537 print_rxifsizeof(prefix, type);
541 if (streq(type, "string")) {
543 } else if (streq(type, "opaque")) {
546 if (streq(type, "string")) {
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);
553 print_rxifarg("(char **) &", objname, 0);
554 /* print_rxifarg(amp, objname, 0); */
555 print_rxifarg("", amax, 1);
557 print_rxifsizeof(prefix, type);
560 char typecontents[100];
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,
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);
579 print_rxifopen("array");
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);
586 sprintf(temp, "&%s->%s_val, (u_int *)&%s->%s_len",
587 objname, name, objname, name);
589 strcat(Proc_list->code, temp);
590 strcat(Proc_list->scode, temp);
592 print_rxifarg("", amax, 1);
594 print_rxifsizeof(prefix, type);
599 print_rxifopen(type);
600 print_rxifarg(amp, objname, 0);
603 strcat(Proc_list->code, ")");
604 strcat(Proc_list->scode, ")");