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)
160 print_generic_header(def->def_name,
161 def->def_kind != DEF_TYPEDEF ||
162 !isvectordef(def->def.ty.old_type, def->def.ty.rel));
164 /* Now add Inline support */
169 /* May cause lint to complain. but ... */
170 f_print(fout, "\t register int32_t *buf;\n\n");
174 print_prog_header(proc_list *plist)
176 print_generic_header(plist->args.argname, 1);
182 f_print(fout, "\treturn (TRUE);\n");
183 f_print(fout, "}\n");
188 print_ifopen(int indent, char *name)
190 tabify(fout, indent);
191 f_print(fout, " if (!xdr_%s(xdrs", name);
195 print_ifarg(char *arg)
197 f_print(fout, ", %s", arg);
201 print_ifsizeof(char *prefix, char *type)
203 if (streq(type, "bool")) {
204 f_print(fout, ", sizeof(bool_t), (xdrproc_t)xdr_bool");
206 f_print(fout, ", sizeof(");
207 if (undefined(type) && prefix) {
208 f_print(fout, "%s ", prefix);
210 f_print(fout, "%s), (xdrproc_t)xdr_%s", type, type);
215 print_ifclose(int indent)
217 f_print(fout, ")) {\n");
218 tabify(fout, indent);
219 f_print(fout, "\t return (FALSE);\n");
220 tabify(fout, indent);
221 f_print(fout, " }\n");
225 print_ifstat(int indent, char *prefix, char *type, relation rel,
226 char *amax, char *objname, char *name)
232 print_ifopen(indent, "pointer");
233 print_ifarg("(char **)");
234 f_print(fout, "%s", objname);
235 print_ifsizeof(prefix, type);
238 if (streq(type, "string")) {
240 } else if (streq(type, "opaque")) {
244 print_ifopen(indent, alt);
245 print_ifarg(objname);
247 print_ifopen(indent, "vector");
248 print_ifarg("(char *)");
249 f_print(fout, "%s", objname);
253 print_ifsizeof(prefix, type);
257 if (streq(type, "string")) {
259 } else if (streq(type, "opaque")) {
262 if (streq(type, "string")) {
263 print_ifopen(indent, alt);
264 print_ifarg(objname);
267 print_ifopen(indent, alt);
269 print_ifopen(indent, "array");
271 print_ifarg("(char **)");
272 if (*objname == '&') {
273 f_print(fout, "%s.%s_val, (u_int *)%s.%s_len",
274 objname, name, objname, name);
276 f_print(fout, "&%s->%s_val, (u_int *)&%s->%s_len",
277 objname, name, objname, name);
282 print_ifsizeof(prefix, type);
286 print_ifopen(indent, type);
287 print_ifarg(objname);
290 print_ifclose(indent);
294 emit_enum(definition *def)
296 print_ifopen(1, "enum");
297 print_ifarg("(enum_t *)objp");
302 emit_program(definition *def)
308 for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
309 for (plist = vlist->procs; plist != NULL; plist = plist->next) {
310 if (!newstyle || plist->arg_num < 2)
311 continue;/* old style, or single argument */
312 print_prog_header(plist);
313 for (dl = plist->args.decls; dl != NULL; dl = dl->next)
314 print_stat(1, &dl->decl);
321 emit_union(definition *def)
327 char *vecformat = "objp->%s_u.%s";
328 char *format = "&objp->%s_u.%s";
330 print_stat(1,&def->def.un.enum_decl);
331 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
332 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
334 f_print(fout, "\tcase %s:\n", cl->case_name);
335 if(cl->contflag == 1) /* a continued case statement */
338 if (!streq(cs->type, "void")) {
339 object = alloc(strlen(def->def_name) + strlen(format) +
340 strlen(cs->name) + 1);
341 if (isvectordef (cs->type, cs->rel)) {
342 s_print(object, vecformat, def->def_name,
345 s_print(object, format, def->def_name,
348 print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max,
352 f_print(fout, "\t\tbreak;\n");
354 dflt = def->def.un.default_decl;
356 if (!streq(dflt->type, "void")) {
357 f_print(fout, "\tdefault:\n");
358 object = alloc(strlen(def->def_name) + strlen(format) +
359 strlen(dflt->name) + 1);
360 if (isvectordef (dflt->type, dflt->rel)) {
361 s_print(object, vecformat, def->def_name,
364 s_print(object, format, def->def_name,
368 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
369 dflt->array_max, object, dflt->name);
371 f_print(fout, "\t\tbreak;\n");
373 /* Avoid gcc warnings about `value not handled in switch' */
374 f_print(fout, "\tdefault:\n");
375 f_print(fout, "\t\tbreak;\n");
378 f_print(fout, "\tdefault:\n");
379 f_print(fout, "\t\treturn (FALSE);\n");
382 f_print(fout, "\t}\n");
386 emit_struct(definition *def)
389 int i, j, size, flag;
390 decl_list *cur = NULL, *psav;
392 char *sizestr, *plus;
398 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
399 print_stat(1, &dl->decl);
402 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
403 if (dl->decl.rel == REL_VECTOR) {
404 f_print(fout, "\t int i;\n");
409 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
410 if ((dl->decl.prefix == NULL) && ((ptr = find_type(dl->decl.type)) != NULL) && ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR))) {
412 if (dl->decl.rel == REL_ALIAS)
416 break; /* can be inlined */
419 if (size >= Inline) {
421 break; /* can be inlined */
428 if (can_inline == 0) { /* can not inline, drop back to old mode */
429 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
430 print_stat(1, &dl->decl);
438 for (j = 0; j < 2; j++) {
441 f_print(fout, "\n\t if (xdrs->x_op == XDR_ENCODE) {\n");
443 f_print(fout, "\n \t return (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
449 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
451 /* now walk down the list and check for basic types */
452 if ((dl->decl.prefix == NULL) && ((ptr = find_type(dl->decl.type)) != NULL) && ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR))) {
457 if (dl->decl.rel == REL_ALIAS)
460 /* this is required to handle arrays */
467 if (ptr->length != 1)
468 s_print(ptemp, " %s %s * %d", plus, dl->decl.array_max, ptr->length);
470 s_print(ptemp, " %s %s ", plus, dl->decl.array_max);
472 /*now concatenate to sizestr !!!! */
474 sizestr = strdup(ptemp);
476 sizestr = realloc(sizestr, strlen(sizestr) + strlen(ptemp) + 1);
477 if (sizestr == NULL) {
479 f_print(stderr, "Fatal error : no memory \n");
482 sizestr = strcat(sizestr, ptemp); /*build up length of array */
490 if (sizestr == NULL && size < Inline) {
491 /* don't expand into inline code if size < inline */
493 print_stat(1, &cur->decl);
500 /* were already looking at a xdr_inlineable structure */
502 f_print(fout, "\t buf = XDR_INLINE(xdrs,%d * BYTES_PER_XDR_UNIT);",
506 "\t buf = XDR_INLINE(xdrs,%s * BYTES_PER_XDR_UNIT);",
510 "\t buf = XDR_INLINE(xdrs,(%d + %s)* BYTES_PER_XDR_UNIT);",
513 f_print(fout, "\n\t if (buf == NULL) {\n");
517 print_stat(2, &cur->decl);
521 f_print(fout, "\n\t }\n\t else {\n");
525 emit_inline(&cur->decl, flag);
529 f_print(fout, "\t }\n");
535 print_stat(1, &dl->decl);
541 if (sizestr == NULL && size < Inline) {
542 /* don't expand into inline code if size < inline */
544 print_stat(1, &cur->decl);
549 /* were already looking at a xdr_inlineable structure */
551 f_print(fout, "\t\tbuf = XDR_INLINE(xdrs,%d * BYTES_PER_XDR_UNIT);",
555 "\t\tbuf = XDR_INLINE(xdrs,%s * BYTES_PER_XDR_UNIT);",
559 "\t\tbuf = XDR_INLINE(xdrs,(%d + %s)* BYTES_PER_XDR_UNIT);",
562 f_print(fout, "\n\t\tif (buf == NULL) {\n");
565 while (cur != NULL) {
566 print_stat(2, &cur->decl);
569 f_print(fout, "\n\t }\n\t else {\n");
573 emit_inline(&cur->decl, flag);
577 f_print(fout, "\t }\n");
583 f_print(fout, "\t return(TRUE);\n\t}\n\n");
585 /* now take care of XDR_FREE case */
587 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
588 print_stat(1, &dl->decl);
596 emit_typedef(definition *def)
598 char *prefix = def->def.ty.old_prefix;
599 char *type = def->def.ty.old_type;
600 char *amax = def->def.ty.array_max;
601 relation rel = def->def.ty.rel;
604 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
608 print_stat(int indent, declaration *dec)
610 char *prefix = dec->prefix;
611 char *type = dec->type;
612 char *amax = dec->array_max;
613 relation rel = dec->rel;
616 if (isvectordef(type, rel)) {
617 s_print(name, "objp->%s", dec->name);
619 s_print(name, "&objp->%s", dec->name);
621 print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
626 emit_inline(declaration *decl, int flag)
629 /*check whether an array or not */
633 emit_single_in_line(decl, flag, REL_ALIAS);
636 f_print(fout, "\t\t{ register %s *genp; \n", decl->type);
637 f_print(fout, "\t\t for ( i = 0,genp=objp->%s;\n \t\t\ti < %s; i++){\n\t\t",
638 decl->name, decl->array_max);
639 emit_single_in_line(decl, flag, REL_VECTOR);
640 f_print(fout, "\t\t }\n\t\t };\n");
648 emit_single_in_line(declaration *decl, int flag, relation rel)
654 f_print(fout,"\t\t IXDR_PUT_");
657 f_print(fout,"\t\t objp->%s = IXDR_GET_",decl->name);
659 f_print(fout,"\t\t *genp++ = IXDR_GET_");
661 upp_case=upcase(decl->type);
664 if(strcmp(upp_case,"INT") == 0)
671 if(strcmp(upp_case,"U_INT") == 0)
681 f_print(fout,"%s(buf,objp->%s);\n",upp_case,decl->name);
683 f_print(fout,"%s(buf,*genp++);\n",upp_case);
686 f_print(fout,"%s(buf);\n",upp_case);
699 ptr = (char *) malloc(strlen(str));
700 if (ptr == (char *) NULL) {
701 f_print(stderr, "malloc failed \n");
707 *ptr++ = toupper(*str++);