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.14 93/07/05 SMI; 1.13 89/02/22 (C) 1987 SMI
30 * $FreeBSD: src/usr.bin/rpcgen/rpc_cout.c,v 1.17 2007/11/20 01:46:12 jb Exp $
31 * $DragonFly: src/usr.bin/rpcgen/rpc_cout.c,v 1.5 2008/10/16 01:52:33 swildner Exp $
35 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
36 * Copyright (C) 1987, Sun Microsystems, Inc.
41 #include "rpc_parse.h"
45 static void print_header(definition *);
46 static void print_trailer(void);
47 static void print_stat(int , declaration *);
48 static void emit_enum(definition *);
49 static void emit_program(definition *);
50 static void emit_union(definition *);
51 static void emit_struct(definition *);
52 static void emit_typedef(definition *);
53 static void emit_inline(int, declaration *, int);
54 static void emit_single_in_line(int, declaration *, int, relation);
55 static char *upcase(const char *);
58 * Emit the C-routine for the given definition
63 if (def->def_kind == DEF_CONST)
65 if (def->def_kind == DEF_PROGRAM) {
69 if (def->def_kind == DEF_TYPEDEF) {
71 * now we need to handle declarations like
72 * struct typedef foo foo;
73 * since we dont want this to be expanded into 2 calls to xdr_foo
76 if (strcmp(def->def.ty.old_type, def->def_name) == 0)
80 switch (def->def_kind) {
93 /* DEF_CONST and DEF_PROGRAM have already been handled */
101 findtype(definition *def, const char *type)
104 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST)
107 return(streq(def->def_name, type));
111 undefined(const char *type)
115 def = (definition *) FINDVAL(defined, type, findtype);
121 print_generic_header(const char *procname, int pointerp)
124 f_print(fout, "bool_t\n");
125 f_print(fout, "xdr_%s(", procname);
126 f_print(fout, "XDR *xdrs, ");
127 f_print(fout, "%s ", procname);
130 f_print(fout, "objp)\n{\n\n");
134 print_header(definition *def)
136 print_generic_header(def->def_name,
137 def->def_kind != DEF_TYPEDEF || !isvectordef(def->def.ty.old_type,
139 /* Now add Inline support */
141 if (inline_size == 0)
143 /* May cause lint to complain. but ... */
144 f_print(fout, "\tlong *buf;\n\n");
148 print_prog_header(proc_list *plist)
150 print_generic_header(plist->args.argname, 1);
156 f_print(fout, "\treturn (TRUE);\n");
157 f_print(fout, "}\n");
162 print_ifopen(int indent, const char *name)
164 tabify(fout, indent);
165 f_print(fout, "if (!xdr_%s(xdrs", name);
169 print_ifarg(const char *arg)
171 f_print(fout, ", %s", arg);
175 print_ifsizeof(int indent, const char *prefix, const char *type)
178 f_print(fout, ",\n");
179 tabify(fout, indent);
183 if (streq(type, "bool")) {
184 f_print(fout, "sizeof (bool_t), (xdrproc_t) xdr_bool");
186 f_print(fout, "sizeof (");
187 if (undefined(type) && prefix)
188 f_print(fout, "%s ", prefix);
189 f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type);
194 print_ifclose(int indent, int brace)
196 f_print(fout, "))\n");
197 tabify(fout, indent);
198 f_print(fout, "\treturn (FALSE);\n");
200 f_print(fout, "\t}\n");
204 print_ifstat(int indent, const char *prefix, const char *type, relation rel,
205 const char *amax, const char *objname, const char *name)
207 const char *alt = NULL;
213 f_print(fout, "\t{\n");
214 f_print(fout, "\t%s **pp = %s;\n", type, objname);
215 print_ifopen(indent, "pointer");
216 print_ifarg("(char **)");
218 print_ifsizeof(0, prefix, type);
221 if (streq(type, "string"))
223 else if (streq(type, "opaque"))
227 print_ifopen(indent, alt);
228 print_ifarg(objname);
230 print_ifopen(indent, "vector");
231 print_ifarg("(char *)");
232 f_print(fout, "%s", objname);
236 print_ifsizeof(indent + 1, prefix, type);
239 if (streq(type, "string"))
241 else if (streq(type, "opaque"))
244 if (streq(type, "string")) {
245 print_ifopen(indent, alt);
246 print_ifarg(objname);
249 print_ifopen(indent, alt);
251 print_ifopen(indent, "array");
252 print_ifarg("(char **)");
253 if (*objname == '&') {
254 f_print(fout, "%s.%s_val, (u_int *) %s.%s_len",
255 objname, name, objname, name);
258 "&%s->%s_val, (u_int *) &%s->%s_len",
259 objname, name, objname, name);
264 print_ifsizeof(indent + 1, prefix, type);
267 print_ifopen(indent, type);
268 print_ifarg(objname);
271 print_ifclose(indent, brace);
276 emit_enum(definition *def __unused)
278 print_ifopen(1, "enum");
279 print_ifarg("(enum_t *)objp");
284 emit_program(definition *def)
290 for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
291 for (plist = vlist->procs; plist != NULL; plist = plist->next) {
292 if (!newstyle || plist->arg_num < 2)
293 continue; /* old style, or single argument */
294 print_prog_header(plist);
295 for (dl = plist->args.decls; dl != NULL;
297 print_stat(1, &dl->decl);
304 emit_union(definition *def)
310 const char *vecformat = "objp->%s_u.%s";
311 const char *format = "&objp->%s_u.%s";
313 print_stat(1, &def->def.un.enum_decl);
314 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
315 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
317 f_print(fout, "\tcase %s:\n", cl->case_name);
318 if (cl->contflag == 1) /* a continued case statement */
321 if (!streq(cs->type, "void")) {
322 object = xmalloc(strlen(def->def_name) +
323 strlen(format) + strlen(cs->name) + 1);
324 if (isvectordef (cs->type, cs->rel)) {
325 s_print(object, vecformat, def->def_name,
328 s_print(object, format, def->def_name,
331 print_ifstat(2, cs->prefix, cs->type, cs->rel,
332 cs->array_max, object, cs->name);
335 f_print(fout, "\t\tbreak;\n");
337 dflt = def->def.un.default_decl;
339 if (!streq(dflt->type, "void")) {
340 f_print(fout, "\tdefault:\n");
341 object = xmalloc(strlen(def->def_name) +
342 strlen(format) + strlen(dflt->name) + 1);
343 if (isvectordef (dflt->type, dflt->rel)) {
344 s_print(object, vecformat, def->def_name,
347 s_print(object, format, def->def_name,
351 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
352 dflt->array_max, object, dflt->name);
354 f_print(fout, "\t\tbreak;\n");
356 f_print(fout, "\tdefault:\n");
357 f_print(fout, "\t\tbreak;\n");
360 f_print(fout, "\tdefault:\n");
361 f_print(fout, "\t\treturn (FALSE);\n");
364 f_print(fout, "\t}\n");
368 inline_struct(definition *def, int flag)
372 decl_list *cur, *psav;
381 f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
383 f_print(fout, "\t\treturn (TRUE);\n"
384 "\t} else if (xdrs->x_op == XDR_DECODE) {\n");
389 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
390 /* now walk down the list and check for basic types */
391 if ((dl->decl.prefix == NULL) &&
392 ((ptr = find_type(dl->decl.type)) != NULL) &&
393 ((dl->decl.rel == REL_ALIAS) ||
394 (dl->decl.rel == REL_VECTOR))){
399 if (dl->decl.rel == REL_ALIAS) {
402 /* this code is required to handle arrays */
408 if (ptr->length != 1)
409 s_print(ptemp, "%s%s * %d",
410 plus, dl->decl.array_max,
413 s_print(ptemp, "%s%s", plus,
416 /* now concatenate to sizestr !!!! */
417 if (sizestr == NULL) {
418 sizestr = xstrdup(ptemp);
420 sizestr = xrealloc(sizestr,
423 sizestr = strcat(sizestr, ptemp);
424 /* build up length of array */
429 if (sizestr == NULL && size < inline_size) {
431 * don't expand into inline code
432 * if size < inline_size
435 print_stat(indent + 1,
440 /* were already looking at a xdr_inlineable structure */
441 tabify(fout, indent + 1);
443 f_print(fout, "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
448 "buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
452 "buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
457 tabify(fout, indent + 1);
459 "if (buf == NULL) {\n");
463 print_stat(indent + 2, &cur->decl);
467 f_print(fout, "\n\t\t} else {\n");
471 emit_inline(indent + 2, &cur->decl, flag);
475 tabify(fout, indent + 1);
476 f_print(fout, "}\n");
482 print_stat(indent + 1, &dl->decl);
487 if (sizestr == NULL && size < inline_size) {
488 /* don't expand into inline code if size < inline_size */
490 print_stat(indent + 1, &cur->decl);
494 /* were already looking at a xdr_inlineable structure */
496 f_print(fout, "\t\tbuf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
501 "\t\tbuf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
505 "\t\tbuf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
509 f_print(fout, "\n\t\tif (buf == NULL) {\n");
511 while (cur != NULL) {
512 print_stat(indent + 2, &cur->decl);
515 f_print(fout, "\t\t} else {\n");
519 emit_inline(indent + 2, &cur->decl, flag);
522 f_print(fout, "\t\t}\n");
528 emit_struct(definition *def)
535 if (inline_size == 0) {
536 /* No xdr_inlining at all */
537 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
538 print_stat(1, &dl->decl);
542 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) {
543 if (dl->decl.rel == REL_VECTOR){
544 f_print(fout, "\tint i;\n");
552 * Make a first pass and see if inling is possible.
554 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) {
555 if ((dl->decl.prefix == NULL) &&
556 ((ptr = find_type(dl->decl.type)) != NULL) &&
557 ((dl->decl.rel == REL_ALIAS)||
558 (dl->decl.rel == REL_VECTOR))) {
559 if (dl->decl.rel == REL_ALIAS)
563 break; /* can be inlined */
566 if (size >= inline_size) {
568 break; /* can be inlined */
573 if (size >= inline_size)
576 if (can_inline == 0) { /* can not inline, drop back to old mode */
577 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
578 print_stat(1, &dl->decl);
583 for (j = 0; j < 2; j++){
584 inline_struct(def, flag);
589 f_print(fout, "\t\treturn (TRUE);\n\t}\n\n");
591 /* now take care of XDR_FREE case */
593 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
594 print_stat(1, &dl->decl);
599 emit_typedef(definition *def)
601 const char *prefix = def->def.ty.old_prefix;
602 const char *type = def->def.ty.old_type;
603 const char *amax = def->def.ty.array_max;
604 relation rel = def->def.ty.rel;
606 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
610 print_stat(int indent, declaration *dec)
612 const char *prefix = dec->prefix;
613 const char *type = dec->type;
614 const char *amax = dec->array_max;
615 relation rel = dec->rel;
618 if (isvectordef(type, rel))
619 s_print(name, "objp->%s", dec->name);
621 s_print(name, "&objp->%s", dec->name);
622 print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
627 emit_inline(int indent, declaration *decl, int flag)
631 emit_single_in_line(indent, decl, flag, REL_ALIAS);
634 tabify(fout, indent);
635 f_print(fout, "{\n");
636 tabify(fout, indent + 1);
637 f_print(fout, "%s *genp;\n\n", decl->type);
638 tabify(fout, indent + 1);
639 f_print(fout, "for (i = 0, genp = objp->%s;\n", decl->name);
640 tabify(fout, indent + 2);
641 f_print(fout, "i < %s; i++) {\n", decl->array_max);
642 emit_single_in_line(indent + 2, decl, flag, REL_VECTOR);
643 tabify(fout, indent + 1);
644 f_print(fout, "}\n");
645 tabify(fout, indent);
646 f_print(fout, "}\n");
654 emit_single_in_line(int indent, declaration *decl, int flag, relation rel)
658 tabify(fout, indent);
660 f_print(fout, "IXDR_PUT_");
661 else if (rel == REL_ALIAS)
662 f_print(fout, "objp->%s = IXDR_GET_", decl->name);
664 f_print(fout, "*genp++ = IXDR_GET_");
666 upp_case = upcase(decl->type);
669 if (strcmp(upp_case, "INT") == 0)
672 upp_case = strdup("LONG");
675 if (strcmp(upp_case, "U_INT") == 0)
678 upp_case = strdup("U_LONG");
681 if (rel == REL_ALIAS)
682 f_print(fout, "%s(buf, objp->%s);\n", upp_case,
685 f_print(fout, "%s(buf, *genp++);\n", upp_case);
687 f_print(fout, "%s(buf);\n", upp_case);
693 upcase(const char *str)
697 ptr = xmalloc(strlen(str)+1);
701 *ptr++ = toupper(*str++);