2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
30 #ident "@(#)rpc_cout.c 1.14 93/07/05 SMI"
34 static char sccsid[] = "@(#)rpc_cout.c 1.13 89/02/22 (C) 1987 SMI";
36 static const char rcsid[] =
37 "$FreeBSD: src/usr.bin/rpcgen/rpc_cout.c,v 1.7 1999/08/28 01:05:16 peter Exp $";
41 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
42 * Copyright (C) 1987, Sun Microsystems, Inc.
48 #include "rpc_parse.h"
51 static void print_header __P(( definition * ));
52 static void print_trailer __P(( void ));
53 static void print_stat __P(( int , declaration * ));
54 static void emit_enum __P(( definition * ));
55 static void emit_program __P(( definition * ));
56 static void emit_union __P(( definition * ));
57 static void emit_struct __P(( definition * ));
58 static void emit_typedef __P(( definition * ));
59 static void emit_inline __P(( int, declaration *, int ));
60 static void emit_single_in_line __P(( int, declaration *, int, relation ));
63 * Emit the C-routine for the given definition
69 if (def->def_kind == DEF_CONST) {
72 if (def->def_kind == DEF_PROGRAM) {
76 if (def->def_kind == DEF_TYPEDEF) {
78 * now we need to handle declarations like
79 * struct typedef foo foo;
80 * since we dont want this to be expanded into 2 calls to xdr_foo
83 if (strcmp(def->def.ty.old_type, def->def_name) == 0)
87 switch (def->def_kind) {
100 /* DEF_CONST and DEF_PROGRAM have already been handled */
112 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
115 return (streq(def->def_name, type));
125 def = (definition *) FINDVAL(defined, type, findtype);
126 return (def == NULL);
131 print_generic_header(procname, pointerp)
136 f_print(fout, "bool_t\n");
138 f_print(fout, "xdr_%s(", procname);
139 f_print(fout, "register XDR *xdrs, ");
140 f_print(fout, "%s ", procname);
143 f_print(fout, "objp)\n{\n\n");
145 f_print(fout, "xdr_%s(xdrs, objp)\n", procname);
146 f_print(fout, "\tregister XDR *xdrs;\n");
147 f_print(fout, "\t%s ", procname);
150 f_print(fout, "objp;\n{\n\n");
158 print_generic_header(def->def_name,
159 def->def_kind != DEF_TYPEDEF ||
160 !isvectordef(def->def.ty.old_type,
162 /* Now add Inline support */
166 /* May cause lint to complain. but ... */
167 f_print(fout, "\tregister long *buf;\n\n");
171 print_prog_header(plist)
174 print_generic_header(plist->args.argname, 1);
180 f_print(fout, "\treturn (TRUE);\n");
181 f_print(fout, "}\n");
186 print_ifopen(indent, name)
190 tabify(fout, indent);
191 f_print(fout, "if (!xdr_%s(xdrs", name);
198 f_print(fout, ", %s", arg);
202 print_ifsizeof(indent, prefix, type)
208 f_print(fout, ",\n");
209 tabify(fout, indent);
213 if (streq(type, "bool")) {
214 f_print(fout, "sizeof (bool_t), (xdrproc_t) xdr_bool");
216 f_print(fout, "sizeof (");
217 if (undefined(type) && prefix) {
218 f_print(fout, "%s ", prefix);
220 f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type);
225 print_ifclose(indent)
228 f_print(fout, "))\n");
229 tabify(fout, indent);
230 f_print(fout, "\treturn (FALSE);\n");
234 print_ifstat(indent, prefix, type, rel, amax, objname, name)
247 print_ifopen(indent, "pointer");
248 print_ifarg("(char **)");
249 f_print(fout, "%s", objname);
250 print_ifsizeof(0, prefix, type);
253 if (streq(type, "string")) {
255 } else if (streq(type, "opaque")) {
259 print_ifopen(indent, alt);
260 print_ifarg(objname);
262 print_ifopen(indent, "vector");
263 print_ifarg("(char *)");
264 f_print(fout, "%s", objname);
268 print_ifsizeof(indent + 1, prefix, type);
272 if (streq(type, "string")) {
274 } else if (streq(type, "opaque")) {
277 if (streq(type, "string")) {
278 print_ifopen(indent, alt);
279 print_ifarg(objname);
282 print_ifopen(indent, alt);
284 print_ifopen(indent, "array");
286 print_ifarg("(char **)");
287 if (*objname == '&') {
288 f_print(fout, "%s.%s_val, (u_int *) %s.%s_len",
289 objname, name, objname, name);
292 "&%s->%s_val, (u_int *) &%s->%s_len",
293 objname, name, objname, name);
298 print_ifsizeof(indent + 1, prefix, type);
302 print_ifopen(indent, type);
303 print_ifarg(objname);
306 print_ifclose(indent);
314 print_ifopen(1, "enum");
315 print_ifarg("(enum_t *)objp");
327 for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
328 for (plist = vlist->procs; plist != NULL; plist = plist->next) {
329 if (!newstyle || plist->arg_num < 2)
330 continue; /* old style, or single argument */
331 print_prog_header(plist);
332 for (dl = plist->args.decls; dl != NULL;
334 print_stat(1, &dl->decl);
348 char *vecformat = "objp->%s_u.%s";
349 char *format = "&objp->%s_u.%s";
351 print_stat(1, &def->def.un.enum_decl);
352 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
353 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
355 f_print(fout, "\tcase %s:\n", cl->case_name);
356 if (cl->contflag == 1) /* a continued case statement */
359 if (!streq(cs->type, "void")) {
360 object = alloc(strlen(def->def_name) + strlen(format) +
361 strlen(cs->name) + 1);
362 if (isvectordef (cs->type, cs->rel)) {
363 s_print(object, vecformat, def->def_name,
366 s_print(object, format, def->def_name,
369 print_ifstat(2, cs->prefix, cs->type, cs->rel,
370 cs->array_max, object, cs->name);
373 f_print(fout, "\t\tbreak;\n");
375 dflt = def->def.un.default_decl;
377 if (!streq(dflt->type, "void")) {
378 f_print(fout, "\tdefault:\n");
379 object = alloc(strlen(def->def_name) + strlen(format) +
380 strlen(dflt->name) + 1);
381 if (isvectordef (dflt->type, dflt->rel)) {
382 s_print(object, vecformat, def->def_name,
385 s_print(object, format, def->def_name,
389 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
390 dflt->array_max, object, dflt->name);
392 f_print(fout, "\t\tbreak;\n");
394 f_print(fout, "\tdefault:\n");
395 f_print(fout, "\t\tbreak;\n");
398 f_print(fout, "\tdefault:\n");
399 f_print(fout, "\t\treturn (FALSE);\n");
402 f_print(fout, "\t}\n");
406 inline_struct(def, flag)
412 decl_list *cur, *psav;
414 char *sizestr, *plus;
419 f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
421 f_print(fout, "\t\treturn (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
426 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
427 /* now walk down the list and check for basic types */
428 if ((dl->decl.prefix == NULL) &&
429 ((ptr = find_type(dl->decl.type)) != NULL) &&
430 ((dl->decl.rel == REL_ALIAS) ||
431 (dl->decl.rel == REL_VECTOR))){
436 if (dl->decl.rel == REL_ALIAS)
439 /* this code is required to handle arrays */
445 if (ptr->length != 1)
446 s_print(ptemp, "%s%s * %d",
447 plus, dl->decl.array_max,
450 s_print(ptemp, "%s%s", plus,
453 /* now concatenate to sizestr !!!! */
455 sizestr = strdup(ptemp);
457 sizestr = realloc(sizestr,
460 if (sizestr == NULL){
461 warnx("fatal error: no memory");
464 sizestr = strcat(sizestr, ptemp);
465 /* build up length of array */
470 if (sizestr == NULL && size < inline){
472 * don't expand into inline code
476 print_stat(indent + 1, &cur->decl);
480 /* were already looking at a xdr_inlineable structure */
481 tabify(fout, indent + 1);
483 f_print(fout, "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
488 "buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
492 "buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
497 tabify(fout, indent + 1);
499 "if (buf == NULL) {\n");
503 print_stat(indent + 2, &cur->decl);
507 f_print(fout, "\n\t\t} else {\n");
511 emit_inline(indent + 2, &cur->decl, flag);
515 tabify(fout, indent + 1);
516 f_print(fout, "}\n");
522 print_stat(indent + 1, &dl->decl);
527 if (sizestr == NULL && size < inline){
528 /* don't expand into inline code if size < inline */
530 print_stat(indent + 1, &cur->decl);
534 /* were already looking at a xdr_inlineable structure */
536 f_print(fout, "\t\tbuf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
541 "\t\tbuf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
545 "\t\tbuf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
548 f_print(fout, "\n\t\tif (buf == NULL) {\n");
551 print_stat(indent + 2, &cur->decl);
554 f_print(fout, "\t\t} else {\n");
558 emit_inline(indent + 2, &cur->decl, flag);
561 f_print(fout, "\t\t}\n");
575 /* No xdr_inlining at all */
576 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
577 print_stat(1, &dl->decl);
581 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
582 if (dl->decl.rel == REL_VECTOR){
583 f_print(fout, "\tint i;\n");
590 * Make a first pass and see if inling is possible.
592 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
593 if ((dl->decl.prefix == NULL) &&
594 ((ptr = find_type(dl->decl.type)) != NULL) &&
595 ((dl->decl.rel == REL_ALIAS)||
596 (dl->decl.rel == REL_VECTOR))){
597 if (dl->decl.rel == REL_ALIAS)
601 break; /* can be inlined */
606 break; /* can be inlined */
613 if (can_inline == 0){ /* can not inline, drop back to old mode */
614 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
615 print_stat(1, &dl->decl);
620 for (j = 0; j < 2; j++){
621 inline_struct(def, flag);
626 f_print(fout, "\t\treturn (TRUE);\n\t}\n\n");
628 /* now take care of XDR_FREE case */
630 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
631 print_stat(1, &dl->decl);
639 char *prefix = def->def.ty.old_prefix;
640 char *type = def->def.ty.old_type;
641 char *amax = def->def.ty.array_max;
642 relation rel = def->def.ty.rel;
644 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
648 print_stat(indent, dec)
652 char *prefix = dec->prefix;
653 char *type = dec->type;
654 char *amax = dec->array_max;
655 relation rel = dec->rel;
658 if (isvectordef(type, rel)) {
659 s_print(name, "objp->%s", dec->name);
661 s_print(name, "&objp->%s", dec->name);
663 print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
670 emit_inline(indent, decl, flag)
677 emit_single_in_line(indent, decl, flag, REL_ALIAS);
680 tabify(fout, indent);
681 f_print(fout, "{\n");
682 tabify(fout, indent + 1);
683 f_print(fout, "register %s *genp;\n\n", decl->type);
684 tabify(fout, indent + 1);
686 "for (i = 0, genp = objp->%s;\n", decl->name);
687 tabify(fout, indent + 2);
688 f_print(fout, "i < %s; i++) {\n", decl->array_max);
689 emit_single_in_line(indent + 2, decl, flag, REL_VECTOR);
690 tabify(fout, indent + 1);
691 f_print(fout, "}\n");
692 tabify(fout, indent);
693 f_print(fout, "}\n");
699 emit_single_in_line(indent, decl, flag, rel)
708 tabify(fout, indent);
710 f_print(fout, "IXDR_PUT_");
712 if (rel == REL_ALIAS)
713 f_print(fout, "objp->%s = IXDR_GET_", decl->name);
715 f_print(fout, "*genp++ = IXDR_GET_");
717 upp_case = upcase(decl->type);
720 if (strcmp(upp_case, "INT") == 0)
727 if (strcmp(upp_case, "U_INT") == 0)
734 if (rel == REL_ALIAS)
736 "%s(buf, objp->%s);\n", upp_case, decl->name);
738 f_print(fout, "%s(buf, *genp++);\n", upp_case);
741 f_print(fout, "%s(buf);\n", upp_case);
751 ptr = (char *)malloc(strlen(str)+1);
752 if (ptr == (char *) NULL)
753 errx(1, "malloc failed");
757 *ptr++ = toupper(*str++);