2 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 static char sccsid[] = "@(#)rpc_cout.c 1.13 89/02/22 (C) 1987 SMI";
34 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
41 #include "rpc_parse.h"
44 static int findtype(definition *def, char *type);
45 static int undefined(char *type);
46 static void print_generic_header(char *procname, int pointerp);
47 static void print_header(definition *def);
48 static void print_prog_header(proc_list *plist);
49 static void print_trailer(void);
50 static void print_ifopen(int indent, char *name);
51 static void print_ifarg(char *arg);
52 static void print_ifsizeof(char *prefix, char *type);
53 static void print_ifclose(int indent);
54 static void print_ifstat(int indent, char *prefix, char *type, relation rel,
55 char *amax, char *objname, char *name);
56 static void emit_enum(definition *def);
57 static void emit_program(definition *def);
58 static void emit_union(definition *def);
59 static void emit_struct(definition *def);
60 static void emit_typedef(definition *def);
61 static void print_stat(int indent, declaration *dec);
62 static void emit_inline(declaration *decl, int flag);
63 static void emit_single_in_line(declaration *decl, int flag, relation rel);
64 static char * upcase(char *str);
67 * Emit the C-routine for the given definition
72 if (def->def_kind == DEF_CONST) {
75 if (def->def_kind == DEF_PROGRAM) {
79 if (def->def_kind == DEF_TYPEDEF) {
80 /* now we need to handle declarations like
81 * struct typedef foo foo;
82 * since we dont want this to be expanded into 2 calls
85 if (strcmp(def->def.ty.old_type, def->def_name) == 0)
90 switch (def->def_kind) {
110 findtype(definition *def, char *type)
113 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
116 return (streq(def->def_name, type));
121 undefined(char *type)
125 def = (definition *) FINDVAL(defined, type, findtype);
127 return (def == NULL);
132 print_generic_header(char *procname, int pointerp)
135 f_print(fout, "bool_t\n");
137 f_print(fout, "xdr_%s(", procname);
138 f_print(fout, "XDR *xdrs, ");
139 f_print(fout, "%s ", procname);
142 f_print(fout, "objp)\n{\n\n");
144 f_print(fout, "xdr_%s(xdrs, objp)\n", procname);
145 f_print(fout, "\tXDR *xdrs;\n");
146 f_print(fout, "\t%s ", procname);
149 f_print(fout, "objp;\n{\n\n");
154 print_header(definition *def)
156 print_generic_header(def->def_name,
157 def->def_kind != DEF_TYPEDEF ||
158 !isvectordef(def->def.ty.old_type, def->def.ty.rel));
160 /* Now add Inline support */
168 print_prog_header(proc_list *plist)
170 print_generic_header(plist->args.argname, 1);
176 f_print(fout, "\treturn (TRUE);\n");
177 f_print(fout, "}\n");
182 print_ifopen(int indent, char *name)
184 tabify(fout, indent);
185 f_print(fout, " if (!xdr_%s(xdrs", name);
189 print_ifarg(char *arg)
191 f_print(fout, ", %s", arg);
195 print_ifsizeof(char *prefix, char *type)
197 if (streq(type, "bool")) {
198 f_print(fout, ", sizeof(bool_t), (xdrproc_t)xdr_bool");
200 f_print(fout, ", sizeof(");
201 if (undefined(type) && prefix) {
202 f_print(fout, "%s ", prefix);
204 f_print(fout, "%s), (xdrproc_t)xdr_%s", type, type);
209 print_ifclose(int indent)
211 f_print(fout, ")) {\n");
212 tabify(fout, indent);
213 f_print(fout, "\t return (FALSE);\n");
214 tabify(fout, indent);
215 f_print(fout, " }\n");
219 print_ifstat(int indent, char *prefix, char *type, relation rel,
220 char *amax, char *objname, char *name)
226 print_ifopen(indent, "pointer");
227 print_ifarg("(char **)");
228 f_print(fout, "%s", objname);
229 print_ifsizeof(prefix, type);
232 if (streq(type, "string")) {
234 } else if (streq(type, "opaque")) {
238 print_ifopen(indent, alt);
239 print_ifarg(objname);
241 print_ifopen(indent, "vector");
242 print_ifarg("(char *)");
243 f_print(fout, "%s", objname);
247 print_ifsizeof(prefix, type);
251 if (streq(type, "string")) {
253 } else if (streq(type, "opaque")) {
256 if (streq(type, "string")) {
257 print_ifopen(indent, alt);
258 print_ifarg(objname);
261 print_ifopen(indent, alt);
263 print_ifopen(indent, "array");
265 /* The (void*) avoids a gcc-4.1 warning */
266 print_ifarg("(char **)(void*)");
267 if (*objname == '&') {
268 f_print(fout, "%s.%s_val, (u_int *)%s.%s_len",
269 objname, name, objname, name);
271 f_print(fout, "&%s->%s_val, (u_int *)&%s->%s_len",
272 objname, name, objname, name);
277 print_ifsizeof(prefix, type);
281 print_ifopen(indent, type);
282 print_ifarg(objname);
285 print_ifclose(indent);
289 emit_enum(definition *def)
291 print_ifopen(1, "enum");
292 print_ifarg("(enum_t *)objp");
297 emit_program(definition *def)
303 for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
304 for (plist = vlist->procs; plist != NULL; plist = plist->next) {
305 if (!newstyle || plist->arg_num < 2)
306 continue;/* old style, or single argument */
307 print_prog_header(plist);
308 for (dl = plist->args.decls; dl != NULL; dl = dl->next)
309 print_stat(1, &dl->decl);
316 emit_union(definition *def)
322 char *vecformat = "objp->%s_u.%s";
323 char *format = "&objp->%s_u.%s";
325 print_stat(1,&def->def.un.enum_decl);
326 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
327 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
329 f_print(fout, "\tcase %s:\n", cl->case_name);
330 if(cl->contflag == 1) /* a continued case statement */
333 if (!streq(cs->type, "void")) {
334 object = alloc(strlen(def->def_name) + strlen(format) +
335 strlen(cs->name) + 1);
336 if (isvectordef (cs->type, cs->rel)) {
337 s_print(object, vecformat, def->def_name,
340 s_print(object, format, def->def_name,
343 print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max,
347 f_print(fout, "\t\tbreak;\n");
349 dflt = def->def.un.default_decl;
351 if (!streq(dflt->type, "void")) {
352 f_print(fout, "\tdefault:\n");
353 object = alloc(strlen(def->def_name) + strlen(format) +
354 strlen(dflt->name) + 1);
355 if (isvectordef (dflt->type, dflt->rel)) {
356 s_print(object, vecformat, def->def_name,
359 s_print(object, format, def->def_name,
363 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
364 dflt->array_max, object, dflt->name);
366 f_print(fout, "\t\tbreak;\n");
368 /* Avoid gcc warnings about `value not handled in switch' */
369 f_print(fout, "\tdefault:\n");
370 f_print(fout, "\t\tbreak;\n");
373 f_print(fout, "\tdefault:\n");
374 f_print(fout, "\t\treturn (FALSE);\n");
377 f_print(fout, "\t}\n");
381 emit_struct(definition *def)
384 int i, j, size, flag;
385 decl_list *cur = NULL, *psav;
387 char *sizestr, *plus;
390 const char *buf_declaration;
394 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
395 print_stat(1, &dl->decl);
399 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
400 if ((dl->decl.prefix == NULL) && ((ptr = find_type(dl->decl.type)) != NULL) && ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR))) {
402 if (dl->decl.rel == REL_ALIAS)
406 break; /* can be inlined */
409 if (size >= Inline) {
411 break; /* can be inlined */
418 if (can_inline == 0) { /* can not inline, drop back to old mode */
419 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
420 print_stat(1, &dl->decl);
428 for (j = 0; j < 2; j++) {
431 f_print(fout, "\n\t if (xdrs->x_op == XDR_ENCODE) {\n");
433 f_print(fout, "\n \t return (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
439 buf_declaration = "int32_t *";
440 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
442 /* now walk down the list and check for basic types */
443 if ((dl->decl.prefix == NULL) && ((ptr = find_type(dl->decl.type)) != NULL) && ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR))) {
448 if (dl->decl.rel == REL_ALIAS)
451 /* this is required to handle arrays */
458 if (ptr->length != 1)
459 s_print(ptemp, " %s %s * %d", plus, dl->decl.array_max, ptr->length);
461 s_print(ptemp, " %s %s ", plus, dl->decl.array_max);
463 /*now concatenate to sizestr !!!! */
465 sizestr = strdup(ptemp);
467 sizestr = realloc(sizestr, strlen(sizestr) + strlen(ptemp) + 1);
468 if (sizestr == NULL) {
470 f_print(stderr, "Fatal error : no memory \n");
473 sizestr = strcat(sizestr, ptemp); /*build up length of array */
481 if (sizestr == NULL && size < Inline) {
482 /* don't expand into inline code if size < inline */
484 print_stat(1, &cur->decl);
491 /* were already looking at a xdr_inlineable structure */
493 f_print(fout, "\t %sbuf = XDR_INLINE(xdrs,%d * BYTES_PER_XDR_UNIT);",
494 buf_declaration, size);
497 "\t %sbuf = XDR_INLINE(xdrs,%s * BYTES_PER_XDR_UNIT);",
498 buf_declaration, sizestr);
501 "\t %sbuf = XDR_INLINE(xdrs,(%d + %s)* BYTES_PER_XDR_UNIT);",
502 buf_declaration, size, sizestr);
503 buf_declaration = "";
505 f_print(fout, "\n\t if (buf == NULL) {\n");
509 print_stat(2, &cur->decl);
513 f_print(fout, "\n\t }\n\t else {\n");
517 emit_inline(&cur->decl, flag);
521 f_print(fout, "\t }\n");
527 print_stat(1, &dl->decl);
533 if (sizestr == NULL && size < Inline) {
534 /* don't expand into inline code if size < inline */
536 print_stat(1, &cur->decl);
541 /* were already looking at a xdr_inlineable structure */
543 f_print(fout, "\t\t%sbuf = XDR_INLINE(xdrs,%d * BYTES_PER_XDR_UNIT);",
544 buf_declaration, size);
547 "\t\t%sbuf = XDR_INLINE(xdrs,%s * BYTES_PER_XDR_UNIT);",
548 buf_declaration, sizestr);
551 "\t\t%sbuf = XDR_INLINE(xdrs,(%d + %s)* BYTES_PER_XDR_UNIT);",
552 buf_declaration, size, sizestr);
553 buf_declaration = "";
555 f_print(fout, "\n\t\tif (buf == NULL) {\n");
558 while (cur != NULL) {
559 print_stat(2, &cur->decl);
562 f_print(fout, "\n\t }\n\t else {\n");
566 emit_inline(&cur->decl, flag);
570 f_print(fout, "\t }\n");
576 f_print(fout, "\t return(TRUE);\n\t}\n\n");
578 /* now take care of XDR_FREE case */
580 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
581 print_stat(1, &dl->decl);
589 emit_typedef(definition *def)
591 char *prefix = def->def.ty.old_prefix;
592 char *type = def->def.ty.old_type;
593 char *amax = def->def.ty.array_max;
594 relation rel = def->def.ty.rel;
597 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
601 print_stat(int indent, declaration *dec)
603 char *prefix = dec->prefix;
604 char *type = dec->type;
605 char *amax = dec->array_max;
606 relation rel = dec->rel;
609 if (isvectordef(type, rel)) {
610 s_print(name, "objp->%s", dec->name);
612 s_print(name, "&objp->%s", dec->name);
614 print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
619 emit_inline(declaration *decl, int flag)
622 /*check whether an array or not */
626 emit_single_in_line(decl, flag, REL_ALIAS);
629 f_print(fout, "\t\t{ register %s *genp; \n", decl->type);
630 f_print(fout, "\t\t int i;\n");
631 f_print(fout, "\t\t for ( i = 0,genp=objp->%s;\n \t\t\ti < %s; i++){\n\t\t",
632 decl->name, decl->array_max);
633 emit_single_in_line(decl, flag, REL_VECTOR);
634 f_print(fout, "\t\t }\n\t\t };\n");
642 emit_single_in_line(declaration *decl, int flag, relation rel)
648 f_print(fout,"\t\t (void) IXDR_PUT_");
651 f_print(fout,"\t\t objp->%s = IXDR_GET_",decl->name);
653 f_print(fout,"\t\t *genp++ = IXDR_GET_");
655 upp_case=upcase(decl->type);
658 if(strcmp(upp_case,"INT") == 0)
665 if(strcmp(upp_case,"U_INT") == 0)
675 f_print(fout,"%s(buf,objp->%s);\n",upp_case,decl->name);
677 f_print(fout,"%s(buf,*genp++);\n",upp_case);
680 f_print(fout,"%s(buf);\n",upp_case);
693 ptr = (char *) malloc(strlen(str)+1);
694 if (ptr == (char *) NULL) {
695 f_print(stderr, "malloc failed \n");
701 *ptr++ = toupper(*str++);