1 /* $NetBSD: rpc_cout.c,v 1.28 2006/04/04 21:29:42 christos Exp $ */
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user or with the express written consent of
9 * Sun Microsystems, Inc.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
39 static char sccsid
[] = "@(#)rpc_cout.c 1.13 89/02/22 (C) 1987 SMI";
41 __RCSID("$NetBSD: rpc_cout.c,v 1.28 2006/04/04 21:29:42 christos Exp $");
46 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
54 #include "rpc_parse.h"
57 static int findtype
__P((definition
*, char *));
58 static int undefined
__P((char *));
59 static void print_generic_header
__P((char *, int));
60 static void print_header
__P((definition
*));
61 static void print_prog_header
__P((proc_list
*));
62 static void print_trailer
__P((void));
63 static void print_ifopen
__P((int, char *));
64 static void print_ifarg
__P((char *));
65 static void print_ifsizeof
__P((char *, char *));
66 static void print_ifclose
__P((int));
67 static void print_ifstat
__P((int, char *, char *, relation
, char *, char *, char *));
68 static void emit_enum
__P((definition
*));
69 static void emit_program
__P((definition
*));
70 static void emit_union
__P((definition
*));
71 static void emit_struct
__P((definition
*));
72 static void emit_typedef
__P((definition
*));
73 static void print_stat
__P((int, declaration
*));
76 * Emit the C-routine for the given definition
82 if (def
->def_kind
== DEF_CONST
) {
85 if (def
->def_kind
== DEF_PROGRAM
) {
89 if (def
->def_kind
== DEF_TYPEDEF
) {
90 /* now we need to handle declarations like struct typedef foo
91 * foo; since we dont want this to be expanded into 2 calls to
94 if (strcmp(def
->def
.ty
.old_type
, def
->def_name
) == 0)
100 switch (def
->def_kind
) {
115 errx(1, "Internal error %s, %d: Case %d not handled",
116 __FILE__
, __LINE__
, def
->def_kind
);
128 if (def
->def_kind
== DEF_PROGRAM
|| def
->def_kind
== DEF_CONST
) {
131 return (streq(def
->def_name
, type
));
141 def
= (definition
*) FINDVAL(defined
, type
, findtype
);
144 return (def
== NULL
);
148 print_generic_header(procname
, pointerp
)
153 f_print(fout
, "bool_t\n");
155 f_print(fout
, "xdr_%s(", procname
);
156 f_print(fout
, "XDR *xdrs, ");
157 f_print(fout
, "%s ", procname
);
160 f_print(fout
, "objp)\n{\n");
162 f_print(fout
, "xdr_%s(xdrs, objp)\n", procname
);
163 f_print(fout
, "\tXDR *xdrs;\n");
164 f_print(fout
, "\t%s ", procname
);
167 f_print(fout
, "objp;\n{\n");
175 print_generic_header(def
->def_name
,
176 def
->def_kind
!= DEF_TYPEDEF
||
177 !isvectordef(def
->def
.ty
.old_type
, def
->def
.ty
.rel
));
181 print_prog_header(plist
)
184 print_generic_header(plist
->args
.argname
, 1);
190 f_print(fout
, "\treturn (TRUE);\n");
191 f_print(fout
, "}\n");
196 print_ifopen(indent
, name
)
202 * XXX Solaris seems to strip the _t. No idea why.
204 if (!strcmp(name
, "rpcprog_t") || !strcmp(name
, "rpcvers_t") ||
205 !strcmp(name
, "rpcproc_t") || !strcmp(name
, "rpcprot_t") ||
206 !strcmp(name
, "rpcport_t") || !strcmp(name
, "rpcpinline_t")) {
207 strncpy(_t_kludge
, name
, strlen(name
) - 2);
210 tabify(fout
, indent
);
211 f_print(fout
, "if (!xdr_%s(xdrs", name
);
218 f_print(fout
, ", %s", arg
);
222 print_ifsizeof(prefix
, type
)
226 if (streq(type
, "bool")) {
227 f_print(fout
, ", (u_int)sizeof(bool_t), (xdrproc_t)xdr_bool");
229 f_print(fout
, ", (u_int)sizeof(");
230 if (undefined(type
) && prefix
) {
231 f_print(fout
, "%s ", prefix
);
233 f_print(fout
, "%s), (xdrproc_t)xdr_%s", type
, type
);
238 print_ifclose(indent
)
241 f_print(fout
, "))\n");
242 tabify(fout
, indent
);
243 f_print(fout
, "\treturn (FALSE);\n");
247 print_ifstat(indent
, prefix
, type
, rel
, amax
, objname
, name
)
260 print_ifopen(indent
, "pointer");
261 print_ifarg("(char **)(void *)");
262 f_print(fout
, "%s", objname
);
263 print_ifsizeof(prefix
, type
);
266 if (streq(type
, "string")) {
269 if (streq(type
, "opaque")) {
273 print_ifopen(indent
, alt
);
274 print_ifarg(objname
);
276 print_ifopen(indent
, "vector");
277 print_ifarg("(char *)(void *)");
278 f_print(fout
, "%s", objname
);
282 print_ifsizeof(prefix
, type
);
286 if (streq(type
, "string")) {
289 if (streq(type
, "opaque")) {
292 if (streq(type
, "string")) {
293 print_ifopen(indent
, alt
);
294 print_ifarg(objname
);
297 print_ifopen(indent
, alt
);
299 print_ifopen(indent
, "array");
301 print_ifarg("(char **)(void *)");
302 if (*objname
== '&') {
303 f_print(fout
, "%s.%s_val, (u_int *)%s.%s_len",
304 objname
, name
, objname
, name
);
306 f_print(fout
, "&%s->%s_val, (u_int *)&%s->%s_len",
307 objname
, name
, objname
, name
);
312 print_ifsizeof(prefix
, type
);
316 print_ifopen(indent
, type
);
317 print_ifarg(objname
);
320 print_ifclose(indent
);
328 f_print(fout
, "{\n");
330 f_print(fout
, "enum_t et = (enum_t)*objp;\n");
331 print_ifopen(2, "enum");
335 f_print(fout
, "*objp = (%s)et;\n", def
->def_name
);
337 f_print(fout
, "}\n");
348 for (vlist
= def
->def
.pr
.versions
; vlist
!= NULL
; vlist
= vlist
->next
)
349 for (plist
= vlist
->procs
; plist
!= NULL
; plist
= plist
->next
) {
350 if (!newstyle
|| plist
->arg_num
< 2)
351 continue; /* old style, or single
353 print_prog_header(plist
);
354 for (dl
= plist
->args
.decls
; dl
!= NULL
;
356 print_stat(1, &dl
->decl
);
370 static const char vecformat
[] = "objp->%s_u.%s";
371 static const char format
[] = "&objp->%s_u.%s";
374 print_stat(1, &def
->def
.un
.enum_decl
);
375 f_print(fout
, "\tswitch (objp->%s) {\n", def
->def
.un
.enum_decl
.name
);
376 for (cl
= def
->def
.un
.cases
; cl
!= NULL
; cl
= cl
->next
) {
377 f_print(fout
, "\tcase %s:\n", cl
->case_name
);
378 if (cl
->contflag
== 1) /* a continued case statement */
381 if (!streq(cs
->type
, "void")) {
382 object
= alloc(strlen(def
->def_name
) + strlen(format
) +
383 strlen(cs
->name
) + 1);
384 if (isvectordef(cs
->type
, cs
->rel
)) {
385 s_print(object
, vecformat
, def
->def_name
,
388 s_print(object
, format
, def
->def_name
,
391 print_ifstat(2, cs
->prefix
, cs
->type
, cs
->rel
,
392 cs
->array_max
, object
, cs
->name
);
395 f_print(fout
, "\t\tbreak;\n");
397 dflt
= def
->def
.un
.default_decl
;
398 f_print(fout
, "\tdefault:\n");
400 if (!streq(dflt
->type
, "void")) {
401 object
= alloc(strlen(def
->def_name
) + strlen(format
) +
402 strlen(dflt
->name
) + 1);
403 if (isvectordef(dflt
->type
, dflt
->rel
)) {
404 s_print(object
, vecformat
, def
->def_name
,
407 s_print(object
, format
, def
->def_name
,
410 print_ifstat(2, dflt
->prefix
, dflt
->type
, dflt
->rel
,
411 dflt
->array_max
, object
, dflt
->name
);
414 f_print(fout
, "\t\tbreak;\n");
416 f_print(fout
, "\t\treturn (FALSE);\n");
419 f_print(fout
, "\t}\n");
427 int i
, j
, size
, flag
;
428 decl_list
*cur
= NULL
, *psav
;
430 char *sizestr
, *plus
;
437 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
438 print_stat(1, &dl
->decl
);
443 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
444 if ((dl
->decl
.prefix
== NULL
) &&
445 ((ptr
= find_type(dl
->decl
.type
)) != NULL
) &&
446 ((dl
->decl
.rel
== REL_ALIAS
) || (dl
->decl
.rel
== REL_VECTOR
))) {
448 if (dl
->decl
.rel
== REL_ALIAS
)
452 break; /* can be inlined */
455 if (size
>= doinline
) {
457 break; /* can be inlined */
464 if (can_inline
== 0) { /* can not inline, drop back to old mode */
466 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
467 print_stat(1, &dl
->decl
);
471 /* May cause lint to complain. but ... */
472 f_print(fout
, "\tint32_t *buf;\n");
475 f_print(fout
, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
477 for (j
= 0; j
< 2; j
++) {
481 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
) { /* xxx */
483 /* now walk down the list and check for basic types */
484 if ((dl
->decl
.prefix
== NULL
) && ((ptr
= find_type(dl
->decl
.type
)) != NULL
) && ((dl
->decl
.rel
== REL_ALIAS
) || (dl
->decl
.rel
== REL_VECTOR
))) {
489 if (dl
->decl
.rel
== REL_ALIAS
)
492 /* this is required to handle arrays */
499 if (ptr
->length
!= 1)
500 s_print(ptemp
, "%s%s * %d", plus
, dl
->decl
.array_max
, ptr
->length
);
502 s_print(ptemp
, "%s%s", plus
, dl
->decl
.array_max
);
504 /* now concatenate to sizestr !!!! */
506 sizestr
= strdup(ptemp
);
510 nsizestr
= (char *) realloc(sizestr
, strlen(sizestr
) + strlen(ptemp
) + 1);
511 if (nsizestr
== NULL
) {
513 f_print(stderr
, "Fatal error : no memory\n");
517 sizestr
= strcat(sizestr
, ptemp
); /* build up length of
525 if (sizestr
== NULL
&& size
< doinline
) {
526 /* don't expand into inline
527 * code if size < doinline */
529 print_stat(2, &cur
->decl
);
536 /* were already looking at a
537 * xdr_inlineable structure */
539 f_print(fout
, "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);\n",
544 "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, %s * BYTES_PER_XDR_UNIT);\n",
548 "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, (%d + %s) * BYTES_PER_XDR_UNIT);\n",
551 f_print(fout
, "\t\tif (buf == NULL) {\n");
555 print_stat(3, &cur
->decl
);
559 f_print(fout
, "\t\t} else {\n");
563 emit_inline(&cur
->decl
, flag
);
567 f_print(fout
, "\t\t}\n");
576 print_stat(2, &dl
->decl
);
581 if (sizestr
== NULL
&& size
< doinline
) {
582 /* don't expand into inline code if size <
585 print_stat(2, &cur
->decl
);
590 /* were already looking at a xdr_inlineable
593 f_print(fout
, "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);\n",
598 "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, %s * BYTES_PER_XDR_UNIT);\n",
602 "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, (%d + %s) * BYTES_PER_XDR_UNIT);\n",
605 f_print(fout
, "\t\tif (buf == NULL) {\n");
608 while (cur
!= NULL
) {
609 print_stat(3, &cur
->decl
);
612 f_print(fout
, "\t\t} else {\n");
616 emit_inline(&cur
->decl
, flag
);
620 f_print(fout
, "\t\t}\n");
626 f_print(fout
, "\t} else if (xdrs->x_op == XDR_DECODE) {\n");
630 f_print(fout
, "\t} else {\n");
632 /* now take care of XDR_FREE case */
634 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
635 print_stat(2, &dl
->decl
);
637 f_print(fout
, "\t}\n");
644 char *prefix
= def
->def
.ty
.old_prefix
;
645 char *type
= def
->def
.ty
.old_type
;
646 char *amax
= def
->def
.ty
.array_max
;
647 relation rel
= def
->def
.ty
.rel
;
650 print_ifstat(1, prefix
, type
, rel
, amax
, "objp", def
->def_name
);
654 print_stat(indent
, dec
)
658 char *prefix
= dec
->prefix
;
659 char *type
= dec
->type
;
660 char *amax
= dec
->array_max
;
661 relation rel
= dec
->rel
;
664 if (isvectordef(type
, rel
)) {
665 s_print(name
, "objp->%s", dec
->name
);
667 s_print(name
, "&objp->%s", dec
->name
);
669 print_ifstat(indent
, prefix
, type
, rel
, amax
, name
, dec
->name
);
674 emit_inline(decl
, flag
)
679 /*check whether an array or not */
683 emit_single_in_line(decl
, flag
, REL_ALIAS
);
686 f_print(fout
, "\t\t\t{\n");
687 f_print(fout
, "\t\t\t\tint i;\n");
688 f_print(fout
, "\t\t\t\t%s *genp;\n", decl
->type
);
690 f_print(fout
, "\t\t\t\tfor (i = 0, genp = objp->%s;\n",
692 f_print(fout
, "\t\t\t\t i < %s; i++) {\n\t\t",
694 emit_single_in_line(decl
, flag
, REL_VECTOR
);
695 f_print(fout
, "\t\t\t\t}\n\t\t\t}\n");
699 errx(1, "Internal error %s, %d: Case %d not handled",
700 __FILE__
, __LINE__
, decl
->rel
);
705 emit_single_in_line(decl
, flag
, rel
)
714 f_print(fout
, "\t\t\tIXDR_PUT_");
716 if (rel
== REL_ALIAS
)
717 f_print(fout
, "\t\t\tobjp->%s = IXDR_GET_", decl
->name
);
719 f_print(fout
, "\t\t\t*genp++ = IXDR_GET_");
721 upp_case
= upcase(decl
->type
);
724 if (strcmp(upp_case
, "INT") == 0) {
729 if (strcmp(upp_case
, "U_INT") == 0) {
732 upp_case
= "U_INT32";
735 if (rel
== REL_ALIAS
)
736 f_print(fout
, "%s(buf, objp->%s);\n", upp_case
, decl
->name
);
738 f_print(fout
, "%s(buf, *genp++);\n", upp_case
);
741 f_print(fout
, "%s(buf);\n", upp_case
);
755 ptr
= (char *) malloc(strlen(str
) + 1);
756 if (ptr
== (char *) NULL
) {
757 f_print(stderr
, "malloc failed\n");
763 *ptr
++ = toupper((unsigned char)*str
++);