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 f_print(fout, "objp)\n");
127 f_print(fout, "{\n");
133 f_print(fout, "\treturn (TRUE);\n");
134 f_print(fout, "}\n");
140 print_ifopen(int indent, char *name)
142 tabify(fout, indent);
143 f_print(fout, "if (!xdr_%s(xdrs", name);
148 print_ifarg(char *arg)
150 f_print(fout, ", %s", arg);
155 print_ifarg_with_cast(int ptr_to, char *type, char *arg)
157 if (streq(type, "bool")) {
158 f_print(fout, ptr_to ? ", (bool_t *) %s" : ", (bool_t) %s", arg);
160 f_print(fout, ptr_to ? ", (%s *) %s" : ", (%s) %s", type, arg);
165 print_ifsizeof(char *prefix, char *type)
167 if (streq(type, "bool")) {
168 f_print(fout, ", sizeof(bool_t), (xdrproc_t) xdr_bool");
170 f_print(fout, ", sizeof(");
171 if (undefined(type) && prefix) {
172 f_print(fout, "%s ", prefix);
174 f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type);
179 print_ifclose(int indent)
181 f_print(fout, ")) {\n");
182 tabify(fout, indent);
183 f_print(fout, "\treturn (FALSE);\n");
184 tabify(fout, indent);
185 f_print(fout, "}\n");
191 f_print(fout, "\n\n");
195 print_ifarg_val(char *objname, char *name)
197 if (*objname == '&') {
199 f_print(fout, "%s.val", objname);
201 f_print(fout, "%s.%s_val", objname, name);
205 f_print(fout, "&%s->val", objname);
207 f_print(fout, "&%s->%s_val", objname, name);
213 print_ifarg_len(char *objname, char *name)
215 if (*objname == '&') {
217 f_print(fout, "%s.len", objname);
219 f_print(fout, "%s.%s_len", objname, name);
223 f_print(fout, "&%s->len", objname);
225 f_print(fout, "&%s->%s_len", objname, name);
231 print_ifstat(int indent, char *prefix, char *type, relation rel, char *amax,
232 char *objname, char *name)
235 char *altcast = NULL;
239 print_ifopen(indent, "pointer");
240 print_ifarg_with_cast(1, "char *", objname);
241 print_ifsizeof(prefix, type);
244 if (streq(type, "string")) {
246 } else if (streq(type, "opaque")) {
251 print_ifopen(indent, alt);
253 print_ifarg_with_cast(0, altcast, objname);
255 print_ifarg(objname);
258 print_ifopen(indent, "vector");
259 print_ifarg_with_cast(1, "char", objname);
263 print_ifsizeof(prefix, type);
267 if (streq(type, "string")) {
269 } else if (streq(type, "opaque")) {
271 tabify(fout, indent);
272 f_print(fout, "{\n");
274 tabify(fout, indent);
275 f_print(fout, "u_int __len = (u_int) ");
277 print_ifarg_len(objname, name);
278 f_print(fout, ");\n");
280 if (streq(type, "string")) {
281 print_ifopen(indent, alt);
282 print_ifarg(objname);
285 print_ifopen(indent, alt);
286 print_ifarg("(char **)");
288 print_ifopen(indent, "array");
289 print_ifarg("(caddr_t *)");
291 print_ifarg_val(objname, name);
293 if (streq(type, "opaque")) {
294 f_print(fout, "&__len");
296 print_ifarg_len(objname, name);
301 print_ifsizeof(prefix, type);
305 print_ifopen(indent, type);
306 print_ifarg(objname);
309 print_ifclose(indent);
310 if (rel == REL_ARRAY && streq(type, "opaque")) {
311 tabify(fout, indent);
313 print_ifarg_len(objname, name);
315 f_print(fout, " = __len;\n");
317 tabify(fout, indent);
318 f_print(fout, "}\n");
325 emit_enum(definition * def)
327 print_ifopen(1, "enum");
328 print_ifarg("(enum_t *)objp");
334 emit_union(definition * def)
340 char *format = "&objp->%s_u.%s";
341 char *briefformat = "&objp->u.%s";
343 print_stat(&def->def.un.enum_decl);
344 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
345 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
347 f_print(fout, "\tcase %s:\n", cl->case_name);
348 if (!streq(cs->type, "void")) {
350 alloc(strlen(def->def_name) + strlen(format) +
351 strlen(cs->name) + 1);
354 s_print(object, briefformat, cs->name);
356 s_print(object, format, def->def_name, cs->name);
358 print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max,
362 f_print(fout, "\t\tbreak;\n");
364 dflt = def->def.un.default_decl;
366 if (!streq(dflt->type, "void")) {
367 f_print(fout, "\tdefault:\n");
369 alloc(strlen(def->def_name) + strlen(format) +
370 strlen(dflt->name) + 1);
372 s_print(object, briefformat, dflt->name);
374 s_print(object, format, def->def_name, dflt->name);
375 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
376 dflt->array_max, object, dflt->name);
378 f_print(fout, "\t\tbreak;\n");
381 f_print(fout, "\tdefault:\n");
382 f_print(fout, "\t\treturn (FALSE);\n");
384 f_print(fout, "\t}\n");
390 emit_struct(definition * def)
394 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) {
395 print_stat(&dl->decl);
403 emit_typedef(definition * def)
405 char *prefix = def->def.ty.old_prefix;
406 char *type = def->def.ty.old_type;
407 char *amax = def->def.ty.array_max;
408 relation rel = def->def.ty.rel;
410 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
418 print_stat(declaration * dec)
420 char *prefix = dec->prefix;
421 char *type = dec->type;
422 char *amax = dec->array_max;
423 relation rel = dec->rel;
426 if (isvectordef(type, rel)) {
427 s_print(name, "objp->%s", dec->name);
429 s_print(name, "&objp->%s", dec->name);
431 print_ifstat(1, prefix, type, rel, amax, name, dec->name);
435 print_hout(declaration * dec)
441 s_print(prefix, "%s ", dec->prefix);
444 f_print(fout, "\ntypedef ");
447 f_print(fout, "struct %s {\n", dec->name);
449 f_print(fout, "\tu_int %s_len;\n", dec->name);
450 f_print(fout, "\t%s%s *%s_val;\n", prefix,
451 dec->type, dec->name);
453 f_print(fout, "\tu_int %s_len;\n", dec->name);
454 f_print(fout, "\t%s%s *%s_val;\n", prefix,
455 dec->type, dec->name);
457 f_print(fout, "} %s", dec->name);
462 f_print(fout, ";\n");
463 f_print(fout, "bool_t xdr_%s(XDR *xdrs, %s *objp);\n", dec->name,
470 print_cout(declaration * dec)
474 f_print(fout, "bool_t\n");
475 f_print(fout, "xdr_%s(XDR *xdrs, %s *objp)\n", dec->name, dec->name);
476 f_print(fout, "{\n");
477 print_ifstat(1, dec->prefix, dec->type, dec->rel, dec->array_max,
485 print_rxifopen(char *typename)
487 sprintf(Proc_list->code, "xdr_%s(&z_xdrs", typename);
488 sprintf(Proc_list->scode, "xdr_%s(z_xdrs", typename);
493 print_rxifarg(char *amp, char *arg, int costant)
495 char code[100], scode[100];
497 sprintf(code, ", %s%s", amp, arg);
499 sprintf(scode, ", %s", arg);
501 sprintf(scode, ", &%s", arg);
502 strcat(Proc_list->code, code);
503 strcat(Proc_list->scode, scode);
508 print_rxifsizeof(char *prefix, char *type)
512 if (streq(type, "bool")) {
513 strcat(Proc_list->code, ", sizeof(bool_t), xdr_bool");
514 strcat(Proc_list->scode, ", sizeof(bool_t), xdr_bool");
516 strcat(Proc_list->code, ", sizeof(");
517 strcat(Proc_list->scode, ", sizeof(");
518 if (undefined(type) && prefix) {
519 sprintf(name, "%s ", prefix);
520 strcat(Proc_list->code, name);
521 strcat(Proc_list->scode, name);
523 sprintf(name, "%s), xdr_%s", type, type);
524 strcat(Proc_list->code, name);
525 strcat(Proc_list->scode, name);
531 print_param(declaration * dec)
533 char *prefix = dec->prefix;
534 char *type = dec->type;
535 char *amax = dec->array_max;
536 relation rel = dec->rel;
537 char *name = dec->name;
540 char *objname, *amp = "";
542 if (rel == REL_POINTER)
543 Proc_list->pl.param_flag |= INDIRECT_PARAM;
546 Proc_list->pl.param_flag &= ~INDIRECT_PARAM;
548 objname = Proc_list->pl.param_name;
551 print_rxifopen(type);
552 print_rxifarg(amp, objname, 0);
554 print_rxifopen("pointer");
555 print_rxifarg(amp, "(char **)", 1);
556 sprintf(temp, "%s", objname);
557 strcat(Proc_list->code, temp);
558 strcat(Proc_list->scode, temp);
559 print_rxifsizeof(prefix, type);
563 if (streq(type, "string")) {
565 } else if (streq(type, "opaque")) {
571 print_rxifopen("vector");
573 print_rxifarg(amp, "(char *)", 0);
574 sprintf(temp, "%s", objname);
575 strcat(Proc_list->code, temp);
576 strcat(Proc_list->scode, temp);
577 print_rxifarg("", amax, 1);
579 print_rxifsizeof(prefix, type);
583 if (streq(type, "string")) {
585 } else if (streq(type, "opaque")) {
588 if (streq(type, "string")) {
590 if ((Proc_list->pl.param_kind == DEF_OUTPARAM)
591 || (Proc_list->pl.param_kind == DEF_INOUTPARAM)) {
592 Proc_list->pl.param_flag |= OUT_STRING;
593 print_rxifarg("", objname, 0);
595 print_rxifarg("(char **) &", objname, 0);
596 /* print_rxifarg(amp, objname, 0); */
597 print_rxifarg("", amax, 1);
599 print_rxifsizeof(prefix, type);
602 char typecontents[100];
606 strcpy(temp, dec->name);
607 strcpy(typecontents, dec->name);
608 strcat(typecontents, " *");
609 strcpy(Proc_list->pl.param_type, typecontents);
610 sprintf(typecontents, "%s_%d", Proc_list->pl.param_name,
612 strcpy(Proc_list->pl.param_name, typecontents);
613 Proc_list->pl.param_flag |= FREETHIS_PARAM;
614 print_rxifopen(temp);
615 print_rxifarg(amp, name, 0);
621 print_rxifopen("array");
623 print_rxifarg(amp, "(char **)", 1);
624 if (*objname == '&') {
625 sprintf(temp, "%s.%s_val, (u_int *)%s.%s_len",
626 objname, name, objname, name);
628 sprintf(temp, "&%s->%s_val, (u_int *)&%s->%s_len",
629 objname, name, objname, name);
631 strcat(Proc_list->code, temp);
632 strcat(Proc_list->scode, temp);
634 print_rxifarg("", amax, 1);
636 print_rxifsizeof(prefix, type);
641 print_rxifopen(type);
642 print_rxifarg(amp, objname, 0);
645 strcat(Proc_list->code, ")");
646 strcat(Proc_list->scode, ")");