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
29 * @(#)rpc_cout.c 1.13 89/02/22 (C) 1987 SMI
30 * $FreeBSD: src/usr.bin/rpcgen/rpc_cout.c,v 1.7 1999/08/28 01:05:16 peter Exp $
31 * $DragonFly: src/usr.bin/rpcgen/rpc_cout.c,v 1.3 2003/11/03 19:31:32 eirikn Exp $
34 #ident "@(#)rpc_cout.c 1.14 93/07/05 SMI"
37 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
38 * Copyright (C) 1987, Sun Microsystems, Inc.
44 #include "rpc_parse.h"
47 static void print_header( definition * );
48 static void print_trailer( void );
49 static void print_stat( int , declaration * );
50 static void emit_enum( definition * );
51 static void emit_program( definition * );
52 static void emit_union( definition * );
53 static void emit_struct( definition * );
54 static void emit_typedef( definition * );
55 static void emit_inline( int, declaration *, int );
56 static void emit_single_in_line( int, declaration *, int, relation );
59 * Emit the C-routine for the given definition
65 if (def->def_kind == DEF_CONST) {
68 if (def->def_kind == DEF_PROGRAM) {
72 if (def->def_kind == DEF_TYPEDEF) {
74 * now we need to handle declarations like
75 * struct typedef foo foo;
76 * since we dont want this to be expanded into 2 calls to xdr_foo
79 if (strcmp(def->def.ty.old_type, def->def_name) == 0)
83 switch (def->def_kind) {
96 /* DEF_CONST and DEF_PROGRAM have already been handled */
108 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
111 return (streq(def->def_name, type));
121 def = (definition *) FINDVAL(defined, type, findtype);
122 return (def == NULL);
127 print_generic_header(procname, pointerp)
132 f_print(fout, "bool_t\n");
134 f_print(fout, "xdr_%s(", procname);
135 f_print(fout, "register XDR *xdrs, ");
136 f_print(fout, "%s ", procname);
139 f_print(fout, "objp)\n{\n\n");
141 f_print(fout, "xdr_%s(xdrs, objp)\n", procname);
142 f_print(fout, "\tregister XDR *xdrs;\n");
143 f_print(fout, "\t%s ", procname);
146 f_print(fout, "objp;\n{\n\n");
154 print_generic_header(def->def_name,
155 def->def_kind != DEF_TYPEDEF ||
156 !isvectordef(def->def.ty.old_type,
158 /* Now add Inline support */
162 /* May cause lint to complain. but ... */
163 f_print(fout, "\tregister long *buf;\n\n");
167 print_prog_header(plist)
170 print_generic_header(plist->args.argname, 1);
176 f_print(fout, "\treturn (TRUE);\n");
177 f_print(fout, "}\n");
182 print_ifopen(indent, name)
186 tabify(fout, indent);
187 f_print(fout, "if (!xdr_%s(xdrs", name);
194 f_print(fout, ", %s", arg);
198 print_ifsizeof(indent, prefix, type)
204 f_print(fout, ",\n");
205 tabify(fout, indent);
209 if (streq(type, "bool")) {
210 f_print(fout, "sizeof (bool_t), (xdrproc_t) xdr_bool");
212 f_print(fout, "sizeof (");
213 if (undefined(type) && prefix) {
214 f_print(fout, "%s ", prefix);
216 f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type);
221 print_ifclose(indent)
224 f_print(fout, "))\n");
225 tabify(fout, indent);
226 f_print(fout, "\treturn (FALSE);\n");
230 print_ifstat(indent, prefix, type, rel, amax, objname, name)
243 print_ifopen(indent, "pointer");
244 print_ifarg("(char **)");
245 f_print(fout, "%s", objname);
246 print_ifsizeof(0, prefix, type);
249 if (streq(type, "string")) {
251 } else if (streq(type, "opaque")) {
255 print_ifopen(indent, alt);
256 print_ifarg(objname);
258 print_ifopen(indent, "vector");
259 print_ifarg("(char *)");
260 f_print(fout, "%s", objname);
264 print_ifsizeof(indent + 1, prefix, type);
268 if (streq(type, "string")) {
270 } else if (streq(type, "opaque")) {
273 if (streq(type, "string")) {
274 print_ifopen(indent, alt);
275 print_ifarg(objname);
278 print_ifopen(indent, alt);
280 print_ifopen(indent, "array");
282 print_ifarg("(char **)");
283 if (*objname == '&') {
284 f_print(fout, "%s.%s_val, (u_int *) %s.%s_len",
285 objname, name, objname, name);
288 "&%s->%s_val, (u_int *) &%s->%s_len",
289 objname, name, objname, name);
294 print_ifsizeof(indent + 1, prefix, type);
298 print_ifopen(indent, type);
299 print_ifarg(objname);
302 print_ifclose(indent);
310 print_ifopen(1, "enum");
311 print_ifarg("(enum_t *)objp");
323 for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
324 for (plist = vlist->procs; plist != NULL; plist = plist->next) {
325 if (!newstyle || plist->arg_num < 2)
326 continue; /* old style, or single argument */
327 print_prog_header(plist);
328 for (dl = plist->args.decls; dl != NULL;
330 print_stat(1, &dl->decl);
344 char *vecformat = "objp->%s_u.%s";
345 char *format = "&objp->%s_u.%s";
347 print_stat(1, &def->def.un.enum_decl);
348 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
349 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
351 f_print(fout, "\tcase %s:\n", cl->case_name);
352 if (cl->contflag == 1) /* a continued case statement */
355 if (!streq(cs->type, "void")) {
356 object = alloc(strlen(def->def_name) + strlen(format) +
357 strlen(cs->name) + 1);
358 if (isvectordef (cs->type, cs->rel)) {
359 s_print(object, vecformat, def->def_name,
362 s_print(object, format, def->def_name,
365 print_ifstat(2, cs->prefix, cs->type, cs->rel,
366 cs->array_max, object, cs->name);
369 f_print(fout, "\t\tbreak;\n");
371 dflt = def->def.un.default_decl;
373 if (!streq(dflt->type, "void")) {
374 f_print(fout, "\tdefault:\n");
375 object = alloc(strlen(def->def_name) + strlen(format) +
376 strlen(dflt->name) + 1);
377 if (isvectordef (dflt->type, dflt->rel)) {
378 s_print(object, vecformat, def->def_name,
381 s_print(object, format, def->def_name,
385 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
386 dflt->array_max, object, dflt->name);
388 f_print(fout, "\t\tbreak;\n");
390 f_print(fout, "\tdefault:\n");
391 f_print(fout, "\t\tbreak;\n");
394 f_print(fout, "\tdefault:\n");
395 f_print(fout, "\t\treturn (FALSE);\n");
398 f_print(fout, "\t}\n");
402 inline_struct(def, flag)
408 decl_list *cur, *psav;
410 char *sizestr, *plus;
415 f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
417 f_print(fout, "\t\treturn (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
422 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
423 /* now walk down the list and check for basic types */
424 if ((dl->decl.prefix == NULL) &&
425 ((ptr = find_type(dl->decl.type)) != NULL) &&
426 ((dl->decl.rel == REL_ALIAS) ||
427 (dl->decl.rel == REL_VECTOR))){
432 if (dl->decl.rel == REL_ALIAS)
435 /* this code is required to handle arrays */
441 if (ptr->length != 1)
442 s_print(ptemp, "%s%s * %d",
443 plus, dl->decl.array_max,
446 s_print(ptemp, "%s%s", plus,
449 /* now concatenate to sizestr !!!! */
451 sizestr = strdup(ptemp);
453 sizestr = realloc(sizestr,
456 if (sizestr == NULL){
457 warnx("fatal error: no memory");
460 sizestr = strcat(sizestr, ptemp);
461 /* build up length of array */
466 if (sizestr == NULL && size < inline){
468 * don't expand into inline code
472 print_stat(indent + 1, &cur->decl);
476 /* were already looking at a xdr_inlineable structure */
477 tabify(fout, indent + 1);
479 f_print(fout, "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
484 "buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
488 "buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
493 tabify(fout, indent + 1);
495 "if (buf == NULL) {\n");
499 print_stat(indent + 2, &cur->decl);
503 f_print(fout, "\n\t\t} else {\n");
507 emit_inline(indent + 2, &cur->decl, flag);
511 tabify(fout, indent + 1);
512 f_print(fout, "}\n");
518 print_stat(indent + 1, &dl->decl);
523 if (sizestr == NULL && size < inline){
524 /* don't expand into inline code if size < inline */
526 print_stat(indent + 1, &cur->decl);
530 /* were already looking at a xdr_inlineable structure */
532 f_print(fout, "\t\tbuf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
537 "\t\tbuf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
541 "\t\tbuf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
544 f_print(fout, "\n\t\tif (buf == NULL) {\n");
547 print_stat(indent + 2, &cur->decl);
550 f_print(fout, "\t\t} else {\n");
554 emit_inline(indent + 2, &cur->decl, flag);
557 f_print(fout, "\t\t}\n");
571 /* No xdr_inlining at all */
572 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
573 print_stat(1, &dl->decl);
577 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
578 if (dl->decl.rel == REL_VECTOR){
579 f_print(fout, "\tint i;\n");
586 * Make a first pass and see if inling is possible.
588 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
589 if ((dl->decl.prefix == NULL) &&
590 ((ptr = find_type(dl->decl.type)) != NULL) &&
591 ((dl->decl.rel == REL_ALIAS)||
592 (dl->decl.rel == REL_VECTOR))){
593 if (dl->decl.rel == REL_ALIAS)
597 break; /* can be inlined */
602 break; /* can be inlined */
609 if (can_inline == 0){ /* can not inline, drop back to old mode */
610 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
611 print_stat(1, &dl->decl);
616 for (j = 0; j < 2; j++){
617 inline_struct(def, flag);
622 f_print(fout, "\t\treturn (TRUE);\n\t}\n\n");
624 /* now take care of XDR_FREE case */
626 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
627 print_stat(1, &dl->decl);
635 char *prefix = def->def.ty.old_prefix;
636 char *type = def->def.ty.old_type;
637 char *amax = def->def.ty.array_max;
638 relation rel = def->def.ty.rel;
640 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
644 print_stat(indent, dec)
648 char *prefix = dec->prefix;
649 char *type = dec->type;
650 char *amax = dec->array_max;
651 relation rel = dec->rel;
654 if (isvectordef(type, rel)) {
655 s_print(name, "objp->%s", dec->name);
657 s_print(name, "&objp->%s", dec->name);
659 print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
666 emit_inline(indent, decl, flag)
673 emit_single_in_line(indent, decl, flag, REL_ALIAS);
676 tabify(fout, indent);
677 f_print(fout, "{\n");
678 tabify(fout, indent + 1);
679 f_print(fout, "register %s *genp;\n\n", decl->type);
680 tabify(fout, indent + 1);
682 "for (i = 0, genp = objp->%s;\n", decl->name);
683 tabify(fout, indent + 2);
684 f_print(fout, "i < %s; i++) {\n", decl->array_max);
685 emit_single_in_line(indent + 2, decl, flag, REL_VECTOR);
686 tabify(fout, indent + 1);
687 f_print(fout, "}\n");
688 tabify(fout, indent);
689 f_print(fout, "}\n");
695 emit_single_in_line(indent, decl, flag, rel)
704 tabify(fout, indent);
706 f_print(fout, "IXDR_PUT_");
708 if (rel == REL_ALIAS)
709 f_print(fout, "objp->%s = IXDR_GET_", decl->name);
711 f_print(fout, "*genp++ = IXDR_GET_");
713 upp_case = upcase(decl->type);
716 if (strcmp(upp_case, "INT") == 0)
723 if (strcmp(upp_case, "U_INT") == 0)
730 if (rel == REL_ALIAS)
732 "%s(buf, objp->%s);\n", upp_case, decl->name);
734 f_print(fout, "%s(buf, *genp++);\n", upp_case);
737 f_print(fout, "%s(buf);\n", upp_case);
747 ptr = (char *)malloc(strlen(str)+1);
748 if (ptr == (char *) NULL)
749 errx(1, "malloc failed");
753 *ptr++ = toupper(*str++);