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)
163 print_generic_header(def->def_name,
164 def->def_kind != DEF_TYPEDEF ||
165 !isvectordef(def->def.ty.old_type, def->def.ty.rel));
167 /* Now add Inline support */
172 /* May cause lint to complain. but ... */
173 f_print(fout, "\t register int32_t *buf;\n\n");
177 print_prog_header(proc_list *plist)
179 print_generic_header(plist->args.argname, 1);
185 f_print(fout, "\treturn (TRUE);\n");
186 f_print(fout, "}\n");
191 print_ifopen(int indent, char *name)
193 tabify(fout, indent);
194 f_print(fout, " if (!xdr_%s(xdrs", name);
198 print_ifarg(char *arg)
200 f_print(fout, ", %s", arg);
204 print_ifsizeof(char *prefix, char *type)
206 if (streq(type, "bool")) {
207 f_print(fout, ", sizeof(bool_t), (xdrproc_t)xdr_bool");
209 f_print(fout, ", sizeof(");
210 if (undefined(type) && prefix) {
211 f_print(fout, "%s ", prefix);
213 f_print(fout, "%s), (xdrproc_t)xdr_%s", type, type);
218 print_ifclose(int indent)
220 f_print(fout, ")) {\n");
221 tabify(fout, indent);
222 f_print(fout, "\t return (FALSE);\n");
223 tabify(fout, indent);
224 f_print(fout, " }\n");
228 print_ifstat(int indent, char *prefix, char *type, relation rel,
229 char *amax, char *objname, char *name)
235 print_ifopen(indent, "pointer");
236 print_ifarg("(char **)");
237 f_print(fout, "%s", objname);
238 print_ifsizeof(prefix, type);
241 if (streq(type, "string")) {
243 } else if (streq(type, "opaque")) {
247 print_ifopen(indent, alt);
248 print_ifarg(objname);
250 print_ifopen(indent, "vector");
251 print_ifarg("(char *)");
252 f_print(fout, "%s", objname);
256 print_ifsizeof(prefix, type);
260 if (streq(type, "string")) {
262 } else if (streq(type, "opaque")) {
265 if (streq(type, "string")) {
266 print_ifopen(indent, alt);
267 print_ifarg(objname);
270 print_ifopen(indent, alt);
272 print_ifopen(indent, "array");
274 print_ifarg("(char **)");
275 if (*objname == '&') {
276 f_print(fout, "%s.%s_val, (u_int *)%s.%s_len",
277 objname, name, objname, name);
279 f_print(fout, "&%s->%s_val, (u_int *)&%s->%s_len",
280 objname, name, objname, name);
285 print_ifsizeof(prefix, type);
289 print_ifopen(indent, type);
290 print_ifarg(objname);
293 print_ifclose(indent);
297 emit_enum(definition *def)
299 print_ifopen(1, "enum");
300 print_ifarg("(enum_t *)objp");
305 emit_program(definition *def)
311 for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
312 for (plist = vlist->procs; plist != NULL; plist = plist->next) {
313 if (!newstyle || plist->arg_num < 2)
314 continue;/* old style, or single argument */
315 print_prog_header(plist);
316 for (dl = plist->args.decls; dl != NULL; dl = dl->next)
317 print_stat(1, &dl->decl);
324 emit_union(definition *def)
330 char *vecformat = "objp->%s_u.%s";
331 char *format = "&objp->%s_u.%s";
333 print_stat(1,&def->def.un.enum_decl);
334 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
335 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
337 f_print(fout, "\tcase %s:\n", cl->case_name);
338 if(cl->contflag == 1) /* a continued case statement */
341 if (!streq(cs->type, "void")) {
342 object = alloc(strlen(def->def_name) + strlen(format) +
343 strlen(cs->name) + 1);
344 if (isvectordef (cs->type, cs->rel)) {
345 s_print(object, vecformat, def->def_name,
348 s_print(object, format, def->def_name,
351 print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max,
355 f_print(fout, "\t\tbreak;\n");
357 dflt = def->def.un.default_decl;
359 if (!streq(dflt->type, "void")) {
360 f_print(fout, "\tdefault:\n");
361 object = alloc(strlen(def->def_name) + strlen(format) +
362 strlen(dflt->name) + 1);
363 if (isvectordef (dflt->type, dflt->rel)) {
364 s_print(object, vecformat, def->def_name,
367 s_print(object, format, def->def_name,
371 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
372 dflt->array_max, object, dflt->name);
374 f_print(fout, "\t\tbreak;\n");
376 /* Avoid gcc warnings about `value not handled in switch' */
377 f_print(fout, "\tdefault:\n");
378 f_print(fout, "\t\tbreak;\n");
381 f_print(fout, "\tdefault:\n");
382 f_print(fout, "\t\treturn (FALSE);\n");
385 f_print(fout, "\t}\n");
389 emit_struct(definition *def)
392 int i, j, size, flag;
393 decl_list *cur = NULL, *psav;
395 char *sizestr, *plus;
401 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
402 print_stat(1, &dl->decl);
405 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
406 if (dl->decl.rel == REL_VECTOR) {
407 f_print(fout, "\t int i;\n");
412 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
413 if ((dl->decl.prefix == NULL) && ((ptr = find_type(dl->decl.type)) != NULL) && ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR))) {
415 if (dl->decl.rel == REL_ALIAS)
419 break; /* can be inlined */
422 if (size >= Inline) {
424 break; /* can be inlined */
431 if (can_inline == 0) { /* can not inline, drop back to old mode */
432 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
433 print_stat(1, &dl->decl);
441 for (j = 0; j < 2; j++) {
444 f_print(fout, "\n\t if (xdrs->x_op == XDR_ENCODE) {\n");
446 f_print(fout, "\n \t return (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
452 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
454 /* now walk down the list and check for basic types */
455 if ((dl->decl.prefix == NULL) && ((ptr = find_type(dl->decl.type)) != NULL) && ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR))) {
460 if (dl->decl.rel == REL_ALIAS)
463 /* this is required to handle arrays */
470 if (ptr->length != 1)
471 s_print(ptemp, " %s %s * %d", plus, dl->decl.array_max, ptr->length);
473 s_print(ptemp, " %s %s ", plus, dl->decl.array_max);
475 /*now concatenate to sizestr !!!! */
477 sizestr = strdup(ptemp);
479 sizestr = realloc(sizestr, strlen(sizestr) + strlen(ptemp) + 1);
480 if (sizestr == NULL) {
482 f_print(stderr, "Fatal error : no memory \n");
485 sizestr = strcat(sizestr, ptemp); /*build up length of array */
493 if (sizestr == NULL && size < Inline) {
494 /* don't expand into inline code if size < inline */
496 print_stat(1, &cur->decl);
503 /* were already looking at a xdr_inlineable structure */
505 f_print(fout, "\t buf = XDR_INLINE(xdrs,%d * BYTES_PER_XDR_UNIT);",
509 "\t buf = XDR_INLINE(xdrs,%s * BYTES_PER_XDR_UNIT);",
513 "\t buf = XDR_INLINE(xdrs,(%d + %s)* BYTES_PER_XDR_UNIT);",
516 f_print(fout, "\n\t if (buf == NULL) {\n");
520 print_stat(2, &cur->decl);
524 f_print(fout, "\n\t }\n\t else {\n");
528 emit_inline(&cur->decl, flag);
532 f_print(fout, "\t }\n");
538 print_stat(1, &dl->decl);
544 if (sizestr == NULL && size < Inline) {
545 /* don't expand into inline code if size < inline */
547 print_stat(1, &cur->decl);
552 /* were already looking at a xdr_inlineable structure */
554 f_print(fout, "\t\tbuf = XDR_INLINE(xdrs,%d * BYTES_PER_XDR_UNIT);",
558 "\t\tbuf = XDR_INLINE(xdrs,%s * BYTES_PER_XDR_UNIT);",
562 "\t\tbuf = XDR_INLINE(xdrs,(%d + %s)* BYTES_PER_XDR_UNIT);",
565 f_print(fout, "\n\t\tif (buf == NULL) {\n");
568 while (cur != NULL) {
569 print_stat(2, &cur->decl);
572 f_print(fout, "\n\t }\n\t else {\n");
576 emit_inline(&cur->decl, flag);
580 f_print(fout, "\t }\n");
586 f_print(fout, "\t return(TRUE);\n\t}\n\n");
588 /* now take care of XDR_FREE case */
590 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
591 print_stat(1, &dl->decl);
599 emit_typedef(definition *def)
601 char *prefix = def->def.ty.old_prefix;
602 char *type = def->def.ty.old_type;
603 char *amax = def->def.ty.array_max;
604 relation rel = def->def.ty.rel;
607 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
611 print_stat(int indent, declaration *dec)
613 char *prefix = dec->prefix;
614 char *type = dec->type;
615 char *amax = dec->array_max;
616 relation rel = dec->rel;
619 if (isvectordef(type, rel)) {
620 s_print(name, "objp->%s", dec->name);
622 s_print(name, "&objp->%s", dec->name);
624 print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
629 emit_inline(declaration *decl, int flag)
632 /*check whether an array or not */
636 emit_single_in_line(decl, flag, REL_ALIAS);
639 f_print(fout, "\t\t{ register %s *genp; \n", decl->type);
640 f_print(fout, "\t\t for ( i = 0,genp=objp->%s;\n \t\t\ti < %s; i++){\n\t\t",
641 decl->name, decl->array_max);
642 emit_single_in_line(decl, flag, REL_VECTOR);
643 f_print(fout, "\t\t }\n\t\t };\n");
651 emit_single_in_line(declaration *decl, int flag, relation rel)
657 f_print(fout,"\t\t IXDR_PUT_");
660 f_print(fout,"\t\t objp->%s = IXDR_GET_",decl->name);
662 f_print(fout,"\t\t *genp++ = IXDR_GET_");
664 upp_case=upcase(decl->type);
667 if(strcmp(upp_case,"INT") == 0)
674 if(strcmp(upp_case,"U_INT") == 0)
684 f_print(fout,"%s(buf,objp->%s);\n",upp_case,decl->name);
686 f_print(fout,"%s(buf,*genp++);\n",upp_case);
689 f_print(fout,"%s(buf);\n",upp_case);
702 ptr = (char *) malloc(strlen(str));
703 if (ptr == (char *) NULL) {
704 f_print(stderr, "malloc failed \n");
710 *ptr++ = toupper(*str++);