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 */
167 /* May cause lint to complain. but ... */
168 f_print(fout, "\t register int32_t *buf;\n\n");
172 print_prog_header(proc_list *plist)
174 print_generic_header(plist->args.argname, 1);
180 f_print(fout, "\treturn (TRUE);\n");
181 f_print(fout, "}\n");
186 print_ifopen(int indent, char *name)
188 tabify(fout, indent);
189 f_print(fout, " if (!xdr_%s(xdrs", name);
193 print_ifarg(char *arg)
195 f_print(fout, ", %s", arg);
199 print_ifsizeof(char *prefix, char *type)
201 if (streq(type, "bool")) {
202 f_print(fout, ", sizeof(bool_t), (xdrproc_t)xdr_bool");
204 f_print(fout, ", sizeof(");
205 if (undefined(type) && prefix) {
206 f_print(fout, "%s ", prefix);
208 f_print(fout, "%s), (xdrproc_t)xdr_%s", type, type);
213 print_ifclose(int indent)
215 f_print(fout, ")) {\n");
216 tabify(fout, indent);
217 f_print(fout, "\t return (FALSE);\n");
218 tabify(fout, indent);
219 f_print(fout, " }\n");
223 print_ifstat(int indent, char *prefix, char *type, relation rel,
224 char *amax, char *objname, char *name)
230 print_ifopen(indent, "pointer");
231 print_ifarg("(char **)");
232 f_print(fout, "%s", objname);
233 print_ifsizeof(prefix, type);
236 if (streq(type, "string")) {
238 } else if (streq(type, "opaque")) {
242 print_ifopen(indent, alt);
243 print_ifarg(objname);
245 print_ifopen(indent, "vector");
246 print_ifarg("(char *)");
247 f_print(fout, "%s", objname);
251 print_ifsizeof(prefix, type);
255 if (streq(type, "string")) {
257 } else if (streq(type, "opaque")) {
260 if (streq(type, "string")) {
261 print_ifopen(indent, alt);
262 print_ifarg(objname);
265 print_ifopen(indent, alt);
267 print_ifopen(indent, "array");
269 print_ifarg("(char **)");
270 if (*objname == '&') {
271 f_print(fout, "%s.%s_val, (u_int *)%s.%s_len",
272 objname, name, objname, name);
274 f_print(fout, "&%s->%s_val, (u_int *)&%s->%s_len",
275 objname, name, objname, name);
280 print_ifsizeof(prefix, type);
284 print_ifopen(indent, type);
285 print_ifarg(objname);
288 print_ifclose(indent);
292 emit_enum(definition *def)
294 print_ifopen(1, "enum");
295 print_ifarg("(enum_t *)objp");
300 emit_program(definition *def)
306 for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
307 for (plist = vlist->procs; plist != NULL; plist = plist->next) {
308 if (!newstyle || plist->arg_num < 2)
309 continue;/* old style, or single argument */
310 print_prog_header(plist);
311 for (dl = plist->args.decls; dl != NULL; dl = dl->next)
312 print_stat(1, &dl->decl);
319 emit_union(definition *def)
325 char *vecformat = "objp->%s_u.%s";
326 char *format = "&objp->%s_u.%s";
328 print_stat(1,&def->def.un.enum_decl);
329 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
330 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
332 f_print(fout, "\tcase %s:\n", cl->case_name);
333 if(cl->contflag == 1) /* a continued case statement */
336 if (!streq(cs->type, "void")) {
337 object = alloc(strlen(def->def_name) + strlen(format) +
338 strlen(cs->name) + 1);
339 if (isvectordef (cs->type, cs->rel)) {
340 s_print(object, vecformat, def->def_name,
343 s_print(object, format, def->def_name,
346 print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max,
350 f_print(fout, "\t\tbreak;\n");
352 dflt = def->def.un.default_decl;
354 if (!streq(dflt->type, "void")) {
355 f_print(fout, "\tdefault:\n");
356 object = alloc(strlen(def->def_name) + strlen(format) +
357 strlen(dflt->name) + 1);
358 if (isvectordef (dflt->type, dflt->rel)) {
359 s_print(object, vecformat, def->def_name,
362 s_print(object, format, def->def_name,
366 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
367 dflt->array_max, object, dflt->name);
369 f_print(fout, "\t\tbreak;\n");
371 /* Avoid gcc warnings about `value not handled in switch' */
372 f_print(fout, "\tdefault:\n");
373 f_print(fout, "\t\tbreak;\n");
376 f_print(fout, "\tdefault:\n");
377 f_print(fout, "\t\treturn (FALSE);\n");
380 f_print(fout, "\t}\n");
384 emit_struct(definition *def)
387 int i, j, size, flag;
388 decl_list *cur = NULL, *psav;
390 char *sizestr, *plus;
396 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
397 print_stat(1, &dl->decl);
400 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
401 if (dl->decl.rel == REL_VECTOR) {
402 f_print(fout, "\t int i;\n");
407 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
408 if ((dl->decl.prefix == NULL) && ((ptr = find_type(dl->decl.type)) != NULL) && ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR))) {
410 if (dl->decl.rel == REL_ALIAS)
414 break; /* can be inlined */
417 if (size >= Inline) {
419 break; /* can be inlined */
426 if (can_inline == 0) { /* can not inline, drop back to old mode */
427 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
428 print_stat(1, &dl->decl);
436 for (j = 0; j < 2; j++) {
439 f_print(fout, "\n\t if (xdrs->x_op == XDR_ENCODE) {\n");
441 f_print(fout, "\n \t return (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
447 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
449 /* now walk down the list and check for basic types */
450 if ((dl->decl.prefix == NULL) && ((ptr = find_type(dl->decl.type)) != NULL) && ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR))) {
455 if (dl->decl.rel == REL_ALIAS)
458 /* this is required to handle arrays */
465 if (ptr->length != 1)
466 s_print(ptemp, " %s %s * %d", plus, dl->decl.array_max, ptr->length);
468 s_print(ptemp, " %s %s ", plus, dl->decl.array_max);
470 /*now concatenate to sizestr !!!! */
472 sizestr = strdup(ptemp);
474 sizestr = realloc(sizestr, strlen(sizestr) + strlen(ptemp) + 1);
475 if (sizestr == NULL) {
477 f_print(stderr, "Fatal error : no memory \n");
480 sizestr = strcat(sizestr, ptemp); /*build up length of array */
488 if (sizestr == NULL && size < Inline) {
489 /* don't expand into inline code if size < inline */
491 print_stat(1, &cur->decl);
498 /* were already looking at a xdr_inlineable structure */
500 f_print(fout, "\t buf = XDR_INLINE(xdrs,%d * BYTES_PER_XDR_UNIT);",
504 "\t buf = XDR_INLINE(xdrs,%s * BYTES_PER_XDR_UNIT);",
508 "\t buf = XDR_INLINE(xdrs,(%d + %s)* BYTES_PER_XDR_UNIT);",
511 f_print(fout, "\n\t if (buf == NULL) {\n");
515 print_stat(2, &cur->decl);
519 f_print(fout, "\n\t }\n\t else {\n");
523 emit_inline(&cur->decl, flag);
527 f_print(fout, "\t }\n");
533 print_stat(1, &dl->decl);
539 if (sizestr == NULL && size < Inline) {
540 /* don't expand into inline code if size < inline */
542 print_stat(1, &cur->decl);
547 /* were already looking at a xdr_inlineable structure */
549 f_print(fout, "\t\tbuf = XDR_INLINE(xdrs,%d * BYTES_PER_XDR_UNIT);",
553 "\t\tbuf = XDR_INLINE(xdrs,%s * BYTES_PER_XDR_UNIT);",
557 "\t\tbuf = XDR_INLINE(xdrs,(%d + %s)* BYTES_PER_XDR_UNIT);",
560 f_print(fout, "\n\t\tif (buf == NULL) {\n");
563 while (cur != NULL) {
564 print_stat(2, &cur->decl);
567 f_print(fout, "\n\t }\n\t else {\n");
571 emit_inline(&cur->decl, flag);
575 f_print(fout, "\t }\n");
581 f_print(fout, "\t return(TRUE);\n\t}\n\n");
583 /* now take care of XDR_FREE case */
585 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
586 print_stat(1, &dl->decl);
594 emit_typedef(definition *def)
596 char *prefix = def->def.ty.old_prefix;
597 char *type = def->def.ty.old_type;
598 char *amax = def->def.ty.array_max;
599 relation rel = def->def.ty.rel;
602 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
606 print_stat(int indent, declaration *dec)
608 char *prefix = dec->prefix;
609 char *type = dec->type;
610 char *amax = dec->array_max;
611 relation rel = dec->rel;
614 if (isvectordef(type, rel)) {
615 s_print(name, "objp->%s", dec->name);
617 s_print(name, "&objp->%s", dec->name);
619 print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
624 emit_inline(declaration *decl, int flag)
627 /*check whether an array or not */
631 emit_single_in_line(decl, flag, REL_ALIAS);
634 f_print(fout, "\t\t{ register %s *genp; \n", decl->type);
635 f_print(fout, "\t\t for ( i = 0,genp=objp->%s;\n \t\t\ti < %s; i++){\n\t\t",
636 decl->name, decl->array_max);
637 emit_single_in_line(decl, flag, REL_VECTOR);
638 f_print(fout, "\t\t }\n\t\t };\n");
646 emit_single_in_line(declaration *decl, int flag, relation rel)
652 f_print(fout,"\t\t IXDR_PUT_");
655 f_print(fout,"\t\t objp->%s = IXDR_GET_",decl->name);
657 f_print(fout,"\t\t *genp++ = IXDR_GET_");
659 upp_case=upcase(decl->type);
662 if(strcmp(upp_case,"INT") == 0)
669 if(strcmp(upp_case,"U_INT") == 0)
679 f_print(fout,"%s(buf,objp->%s);\n",upp_case,decl->name);
681 f_print(fout,"%s(buf,*genp++);\n",upp_case);
684 f_print(fout,"%s(buf);\n",upp_case);
697 ptr = (char *) malloc(strlen(str));
698 if (ptr == (char *) NULL) {
699 f_print(stderr, "malloc failed \n");
705 *ptr++ = toupper(*str++);