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 or with the express written consent of
8 * Sun Microsystems, Inc.
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 static char sccsid[] = "@(#)rpc_cout.c 1.13 89/02/22 (C) 1987 SMI";
36 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
43 #include "rpc_parse.h"
46 static int findtype(definition *def, char *type);
47 static int undefined(char *type);
48 static void print_generic_header(char *procname, int pointerp);
49 static void print_header(definition *def);
50 static void print_prog_header(proc_list *plist);
51 static void print_trailer(void);
52 static void print_ifopen(int indent, char *name);
53 static void print_ifarg(char *arg);
54 static void print_ifsizeof(char *prefix, char *type);
55 static void print_ifclose(int indent);
56 static void print_ifstat(int indent, char *prefix, char *type, relation rel,
57 char *amax, char *objname, char *name);
58 static void emit_enum(definition *def);
59 static void emit_program(definition *def);
60 static void emit_union(definition *def);
61 static void emit_struct(definition *def);
62 static void emit_typedef(definition *def);
63 static void print_stat(int indent, declaration *dec);
64 static void emit_inline(declaration *decl, int flag);
65 static void emit_single_in_line(declaration *decl, int flag, relation rel);
66 static char * upcase(char *str);
69 * Emit the C-routine for the given definition
74 if (def->def_kind == DEF_CONST) {
77 if (def->def_kind == DEF_PROGRAM) {
81 if (def->def_kind == DEF_TYPEDEF) {
82 /* now we need to handle declarations like
83 * struct typedef foo foo;
84 * since we dont want this to be expanded into 2 calls
87 if (strcmp(def->def.ty.old_type, def->def_name) == 0)
92 switch (def->def_kind) {
112 findtype(definition *def, char *type)
115 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
118 return (streq(def->def_name, type));
123 undefined(char *type)
127 def = (definition *) FINDVAL(defined, type, findtype);
129 return (def == NULL);
134 print_generic_header(char *procname, int pointerp)
137 f_print(fout, "bool_t\n");
139 f_print(fout, "xdr_%s(", procname);
140 f_print(fout, "XDR *xdrs, ");
141 f_print(fout, "%s ", procname);
144 f_print(fout, "objp)\n{\n\n");
146 f_print(fout, "xdr_%s(xdrs, objp)\n", procname);
147 f_print(fout, "\tXDR *xdrs;\n");
148 f_print(fout, "\t%s ", procname);
151 f_print(fout, "objp;\n{\n\n");
156 print_header(definition *def)
158 print_generic_header(def->def_name,
159 def->def_kind != DEF_TYPEDEF ||
160 !isvectordef(def->def.ty.old_type, def->def.ty.rel));
162 /* Now add Inline support */
170 print_prog_header(proc_list *plist)
172 print_generic_header(plist->args.argname, 1);
178 f_print(fout, "\treturn (TRUE);\n");
179 f_print(fout, "}\n");
184 print_ifopen(int indent, char *name)
186 tabify(fout, indent);
187 f_print(fout, " if (!xdr_%s(xdrs", name);
191 print_ifarg(char *arg)
193 f_print(fout, ", %s", arg);
197 print_ifsizeof(char *prefix, char *type)
199 if (streq(type, "bool")) {
200 f_print(fout, ", sizeof(bool_t), (xdrproc_t)xdr_bool");
202 f_print(fout, ", sizeof(");
203 if (undefined(type) && prefix) {
204 f_print(fout, "%s ", prefix);
206 f_print(fout, "%s), (xdrproc_t)xdr_%s", type, type);
211 print_ifclose(int indent)
213 f_print(fout, ")) {\n");
214 tabify(fout, indent);
215 f_print(fout, "\t return (FALSE);\n");
216 tabify(fout, indent);
217 f_print(fout, " }\n");
221 print_ifstat(int indent, char *prefix, char *type, relation rel,
222 char *amax, char *objname, char *name)
228 print_ifopen(indent, "pointer");
229 print_ifarg("(char **)");
230 f_print(fout, "%s", objname);
231 print_ifsizeof(prefix, type);
234 if (streq(type, "string")) {
236 } else if (streq(type, "opaque")) {
240 print_ifopen(indent, alt);
241 print_ifarg(objname);
243 print_ifopen(indent, "vector");
244 print_ifarg("(char *)");
245 f_print(fout, "%s", objname);
249 print_ifsizeof(prefix, type);
253 if (streq(type, "string")) {
255 } else if (streq(type, "opaque")) {
258 if (streq(type, "string")) {
259 print_ifopen(indent, alt);
260 print_ifarg(objname);
263 print_ifopen(indent, alt);
265 print_ifopen(indent, "array");
267 /* The (void*) avoids a gcc-4.1 warning */
268 print_ifarg("(char **)(void*)");
269 if (*objname == '&') {
270 f_print(fout, "%s.%s_val, (u_int *)%s.%s_len",
271 objname, name, objname, name);
273 f_print(fout, "&%s->%s_val, (u_int *)&%s->%s_len",
274 objname, name, objname, name);
279 print_ifsizeof(prefix, type);
283 print_ifopen(indent, type);
284 print_ifarg(objname);
287 print_ifclose(indent);
291 emit_enum(definition *def)
293 print_ifopen(1, "enum");
294 print_ifarg("(enum_t *)objp");
299 emit_program(definition *def)
305 for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
306 for (plist = vlist->procs; plist != NULL; plist = plist->next) {
307 if (!newstyle || plist->arg_num < 2)
308 continue;/* old style, or single argument */
309 print_prog_header(plist);
310 for (dl = plist->args.decls; dl != NULL; dl = dl->next)
311 print_stat(1, &dl->decl);
318 emit_union(definition *def)
324 char *vecformat = "objp->%s_u.%s";
325 char *format = "&objp->%s_u.%s";
327 print_stat(1,&def->def.un.enum_decl);
328 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
329 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
331 f_print(fout, "\tcase %s:\n", cl->case_name);
332 if(cl->contflag == 1) /* a continued case statement */
335 if (!streq(cs->type, "void")) {
336 object = alloc(strlen(def->def_name) + strlen(format) +
337 strlen(cs->name) + 1);
338 if (isvectordef (cs->type, cs->rel)) {
339 s_print(object, vecformat, def->def_name,
342 s_print(object, format, def->def_name,
345 print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max,
349 f_print(fout, "\t\tbreak;\n");
351 dflt = def->def.un.default_decl;
353 if (!streq(dflt->type, "void")) {
354 f_print(fout, "\tdefault:\n");
355 object = alloc(strlen(def->def_name) + strlen(format) +
356 strlen(dflt->name) + 1);
357 if (isvectordef (dflt->type, dflt->rel)) {
358 s_print(object, vecformat, def->def_name,
361 s_print(object, format, def->def_name,
365 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
366 dflt->array_max, object, dflt->name);
368 f_print(fout, "\t\tbreak;\n");
370 /* Avoid gcc warnings about `value not handled in switch' */
371 f_print(fout, "\tdefault:\n");
372 f_print(fout, "\t\tbreak;\n");
375 f_print(fout, "\tdefault:\n");
376 f_print(fout, "\t\treturn (FALSE);\n");
379 f_print(fout, "\t}\n");
383 emit_struct(definition *def)
386 int i, j, size, flag;
387 decl_list *cur = NULL, *psav;
389 char *sizestr, *plus;
392 const char *buf_declaration;
396 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
397 print_stat(1, &dl->decl);
401 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
402 if ((dl->decl.prefix == NULL) && ((ptr = find_type(dl->decl.type)) != NULL) && ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR))) {
404 if (dl->decl.rel == REL_ALIAS)
408 break; /* can be inlined */
411 if (size >= Inline) {
413 break; /* can be inlined */
420 if (can_inline == 0) { /* can not inline, drop back to old mode */
421 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
422 print_stat(1, &dl->decl);
430 for (j = 0; j < 2; j++) {
433 f_print(fout, "\n\t if (xdrs->x_op == XDR_ENCODE) {\n");
435 f_print(fout, "\n \t return (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
441 buf_declaration = "int32_t *";
442 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
444 /* now walk down the list and check for basic types */
445 if ((dl->decl.prefix == NULL) && ((ptr = find_type(dl->decl.type)) != NULL) && ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR))) {
450 if (dl->decl.rel == REL_ALIAS)
453 /* this is required to handle arrays */
460 if (ptr->length != 1)
461 s_print(ptemp, " %s %s * %d", plus, dl->decl.array_max, ptr->length);
463 s_print(ptemp, " %s %s ", plus, dl->decl.array_max);
465 /*now concatenate to sizestr !!!! */
467 sizestr = strdup(ptemp);
469 sizestr = realloc(sizestr, strlen(sizestr) + strlen(ptemp) + 1);
470 if (sizestr == NULL) {
472 f_print(stderr, "Fatal error : no memory \n");
475 sizestr = strcat(sizestr, ptemp); /*build up length of array */
483 if (sizestr == NULL && size < Inline) {
484 /* don't expand into inline code if size < inline */
486 print_stat(1, &cur->decl);
493 /* were already looking at a xdr_inlineable structure */
495 f_print(fout, "\t %sbuf = XDR_INLINE(xdrs,%d * BYTES_PER_XDR_UNIT);",
496 buf_declaration, size);
499 "\t %sbuf = XDR_INLINE(xdrs,%s * BYTES_PER_XDR_UNIT);",
500 buf_declaration, sizestr);
503 "\t %sbuf = XDR_INLINE(xdrs,(%d + %s)* BYTES_PER_XDR_UNIT);",
504 buf_declaration, size, sizestr);
505 buf_declaration = "";
507 f_print(fout, "\n\t if (buf == NULL) {\n");
511 print_stat(2, &cur->decl);
515 f_print(fout, "\n\t }\n\t else {\n");
519 emit_inline(&cur->decl, flag);
523 f_print(fout, "\t }\n");
529 print_stat(1, &dl->decl);
535 if (sizestr == NULL && size < Inline) {
536 /* don't expand into inline code if size < inline */
538 print_stat(1, &cur->decl);
543 /* were already looking at a xdr_inlineable structure */
545 f_print(fout, "\t\t%sbuf = XDR_INLINE(xdrs,%d * BYTES_PER_XDR_UNIT);",
546 buf_declaration, size);
549 "\t\t%sbuf = XDR_INLINE(xdrs,%s * BYTES_PER_XDR_UNIT);",
550 buf_declaration, sizestr);
553 "\t\t%sbuf = XDR_INLINE(xdrs,(%d + %s)* BYTES_PER_XDR_UNIT);",
554 buf_declaration, size, sizestr);
555 buf_declaration = "";
557 f_print(fout, "\n\t\tif (buf == NULL) {\n");
560 while (cur != NULL) {
561 print_stat(2, &cur->decl);
564 f_print(fout, "\n\t }\n\t else {\n");
568 emit_inline(&cur->decl, flag);
572 f_print(fout, "\t }\n");
578 f_print(fout, "\t return(TRUE);\n\t}\n\n");
580 /* now take care of XDR_FREE case */
582 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
583 print_stat(1, &dl->decl);
591 emit_typedef(definition *def)
593 char *prefix = def->def.ty.old_prefix;
594 char *type = def->def.ty.old_type;
595 char *amax = def->def.ty.array_max;
596 relation rel = def->def.ty.rel;
599 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
603 print_stat(int indent, declaration *dec)
605 char *prefix = dec->prefix;
606 char *type = dec->type;
607 char *amax = dec->array_max;
608 relation rel = dec->rel;
611 if (isvectordef(type, rel)) {
612 s_print(name, "objp->%s", dec->name);
614 s_print(name, "&objp->%s", dec->name);
616 print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
621 emit_inline(declaration *decl, int flag)
624 /*check whether an array or not */
628 emit_single_in_line(decl, flag, REL_ALIAS);
631 f_print(fout, "\t\t{ register %s *genp; \n", decl->type);
632 f_print(fout, "\t\t int i;\n");
633 f_print(fout, "\t\t for ( i = 0,genp=objp->%s;\n \t\t\ti < %s; i++){\n\t\t",
634 decl->name, decl->array_max);
635 emit_single_in_line(decl, flag, REL_VECTOR);
636 f_print(fout, "\t\t }\n\t\t };\n");
644 emit_single_in_line(declaration *decl, int flag, relation rel)
650 f_print(fout,"\t\t (void) IXDR_PUT_");
653 f_print(fout,"\t\t objp->%s = IXDR_GET_",decl->name);
655 f_print(fout,"\t\t *genp++ = IXDR_GET_");
657 upp_case=upcase(decl->type);
660 if(strcmp(upp_case,"INT") == 0)
667 if(strcmp(upp_case,"U_INT") == 0)
677 f_print(fout,"%s(buf,objp->%s);\n",upp_case,decl->name);
679 f_print(fout,"%s(buf,*genp++);\n",upp_case);
682 f_print(fout,"%s(buf);\n",upp_case);
695 ptr = (char *) malloc(strlen(str)+1);
696 if (ptr == (char *) NULL) {
697 f_print(stderr, "malloc failed \n");
703 *ptr++ = toupper(*str++);