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.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)rpc_cout.c 1.14 93/07/05 SMI; 1.13 89/02/22 (C) 1987 SMI
30 * $FreeBSD: src/usr.bin/rpcgen/rpc_cout.c,v 1.17 2007/11/20 01:46:12 jb Exp $
34 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
35 * Copyright (C) 1987, Sun Microsystems, Inc.
40 #include "rpc_parse.h"
44 static void print_header(definition
*);
45 static void print_trailer(void);
46 static void print_stat(int , declaration
*);
47 static void emit_enum(definition
*);
48 static void emit_program(definition
*);
49 static void emit_union(definition
*);
50 static void emit_struct(definition
*);
51 static void emit_typedef(definition
*);
52 static void emit_inline(int, declaration
*, int);
53 static void emit_single_in_line(int, declaration
*, int, relation
);
54 static char *upcase(const char *);
57 * Emit the C-routine for the given definition
62 if (def
->def_kind
== DEF_CONST
)
64 if (def
->def_kind
== DEF_PROGRAM
) {
68 if (def
->def_kind
== DEF_TYPEDEF
) {
70 * now we need to handle declarations like
71 * struct typedef foo foo;
72 * since we dont want this to be expanded into 2 calls to xdr_foo
75 if (strcmp(def
->def
.ty
.old_type
, def
->def_name
) == 0)
79 switch (def
->def_kind
) {
92 /* DEF_CONST and DEF_PROGRAM have already been handled */
100 findtype(definition
*def
, const char *type
)
103 if (def
->def_kind
== DEF_PROGRAM
|| def
->def_kind
== DEF_CONST
)
106 return(streq(def
->def_name
, type
));
110 undefined(const char *type
)
114 def
= (definition
*) FINDVAL(defined
, type
, findtype
);
120 print_generic_header(const char *procname
, int pointerp
)
123 f_print(fout
, "bool_t\n");
124 f_print(fout
, "xdr_%s(", procname
);
125 f_print(fout
, "XDR *xdrs, ");
126 f_print(fout
, "%s ", procname
);
129 f_print(fout
, "objp)\n{\n\n");
133 print_header(definition
*def
)
135 print_generic_header(def
->def_name
,
136 def
->def_kind
!= DEF_TYPEDEF
|| !isvectordef(def
->def
.ty
.old_type
,
138 /* Now add Inline support */
140 if (inline_size
== 0)
142 /* May cause lint to complain. but ... */
143 f_print(fout
, "\tlong *buf;\n\n");
147 print_prog_header(proc_list
*plist
)
149 print_generic_header(plist
->args
.argname
, 1);
155 f_print(fout
, "\treturn (TRUE);\n");
156 f_print(fout
, "}\n");
161 print_ifopen(int indent
, const char *name
)
163 tabify(fout
, indent
);
164 f_print(fout
, "if (!xdr_%s(xdrs", name
);
168 print_ifarg(const char *arg
)
170 f_print(fout
, ", %s", arg
);
174 print_ifsizeof(int indent
, const char *prefix
, const char *type
)
177 f_print(fout
, ",\n");
178 tabify(fout
, indent
);
182 if (streq(type
, "bool")) {
183 f_print(fout
, "sizeof (bool_t), (xdrproc_t) xdr_bool");
185 f_print(fout
, "sizeof (");
186 if (undefined(type
) && prefix
)
187 f_print(fout
, "%s ", prefix
);
188 f_print(fout
, "%s), (xdrproc_t) xdr_%s", type
, type
);
193 print_ifclose(int indent
, int brace
)
195 f_print(fout
, "))\n");
196 tabify(fout
, indent
);
197 f_print(fout
, "\treturn (FALSE);\n");
199 f_print(fout
, "\t}\n");
203 print_ifstat(int indent
, const char *prefix
, const char *type
, relation rel
,
204 const char *amax
, const char *objname
, const char *name
)
206 const char *alt
= NULL
;
212 f_print(fout
, "\t{\n");
213 f_print(fout
, "\t%s **pp = %s;\n", type
, objname
);
214 print_ifopen(indent
, "pointer");
215 print_ifarg("(char **)");
217 print_ifsizeof(0, prefix
, type
);
220 if (streq(type
, "string"))
222 else if (streq(type
, "opaque"))
226 print_ifopen(indent
, alt
);
227 print_ifarg(objname
);
229 print_ifopen(indent
, "vector");
230 print_ifarg("(char *)");
231 f_print(fout
, "%s", objname
);
235 print_ifsizeof(indent
+ 1, prefix
, type
);
238 if (streq(type
, "string"))
240 else if (streq(type
, "opaque"))
243 if (streq(type
, "string")) {
244 print_ifopen(indent
, alt
);
245 print_ifarg(objname
);
248 print_ifopen(indent
, alt
);
250 print_ifopen(indent
, "array");
251 print_ifarg("(char **)");
252 if (*objname
== '&') {
253 f_print(fout
, "%s.%s_val, (u_int *) %s.%s_len",
254 objname
, name
, objname
, name
);
257 "&%s->%s_val, (u_int *) &%s->%s_len",
258 objname
, name
, objname
, name
);
263 print_ifsizeof(indent
+ 1, prefix
, type
);
266 print_ifopen(indent
, type
);
267 print_ifarg(objname
);
270 print_ifclose(indent
, brace
);
275 emit_enum(definition
*def __unused
)
277 print_ifopen(1, "enum");
278 print_ifarg("(enum_t *)objp");
283 emit_program(definition
*def
)
289 for (vlist
= def
->def
.pr
.versions
; vlist
!= NULL
; vlist
= vlist
->next
)
290 for (plist
= vlist
->procs
; plist
!= NULL
; plist
= plist
->next
) {
291 if (!newstyle
|| plist
->arg_num
< 2)
292 continue; /* old style, or single argument */
293 print_prog_header(plist
);
294 for (dl
= plist
->args
.decls
; dl
!= NULL
;
296 print_stat(1, &dl
->decl
);
303 emit_union(definition
*def
)
309 const char *vecformat
= "objp->%s_u.%s";
310 const char *format
= "&objp->%s_u.%s";
312 print_stat(1, &def
->def
.un
.enum_decl
);
313 f_print(fout
, "\tswitch (objp->%s) {\n", def
->def
.un
.enum_decl
.name
);
314 for (cl
= def
->def
.un
.cases
; cl
!= NULL
; cl
= cl
->next
) {
316 f_print(fout
, "\tcase %s:\n", cl
->case_name
);
317 if (cl
->contflag
== 1) /* a continued case statement */
320 if (!streq(cs
->type
, "void")) {
321 object
= xmalloc(strlen(def
->def_name
) +
322 strlen(format
) + strlen(cs
->name
) + 1);
323 if (isvectordef (cs
->type
, cs
->rel
)) {
324 s_print(object
, vecformat
, def
->def_name
,
327 s_print(object
, format
, def
->def_name
,
330 print_ifstat(2, cs
->prefix
, cs
->type
, cs
->rel
,
331 cs
->array_max
, object
, cs
->name
);
334 f_print(fout
, "\t\tbreak;\n");
336 dflt
= def
->def
.un
.default_decl
;
338 if (!streq(dflt
->type
, "void")) {
339 f_print(fout
, "\tdefault:\n");
340 object
= xmalloc(strlen(def
->def_name
) +
341 strlen(format
) + strlen(dflt
->name
) + 1);
342 if (isvectordef (dflt
->type
, dflt
->rel
)) {
343 s_print(object
, vecformat
, def
->def_name
,
346 s_print(object
, format
, def
->def_name
,
350 print_ifstat(2, dflt
->prefix
, dflt
->type
, dflt
->rel
,
351 dflt
->array_max
, object
, dflt
->name
);
353 f_print(fout
, "\t\tbreak;\n");
355 f_print(fout
, "\tdefault:\n");
356 f_print(fout
, "\t\tbreak;\n");
359 f_print(fout
, "\tdefault:\n");
360 f_print(fout
, "\t\treturn (FALSE);\n");
363 f_print(fout
, "\t}\n");
367 inline_struct(definition
*def
, int flag
)
371 decl_list
*cur
, *psav
;
380 f_print(fout
, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
382 f_print(fout
, "\t\treturn (TRUE);\n"
383 "\t} else if (xdrs->x_op == XDR_DECODE) {\n");
388 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
) { /* xxx */
389 /* now walk down the list and check for basic types */
390 if ((dl
->decl
.prefix
== NULL
) &&
391 ((ptr
= find_type(dl
->decl
.type
)) != NULL
) &&
392 ((dl
->decl
.rel
== REL_ALIAS
) ||
393 (dl
->decl
.rel
== REL_VECTOR
))){
398 if (dl
->decl
.rel
== REL_ALIAS
) {
401 /* this code is required to handle arrays */
407 if (ptr
->length
!= 1)
408 s_print(ptemp
, "%s%s * %d",
409 plus
, dl
->decl
.array_max
,
412 s_print(ptemp
, "%s%s", plus
,
415 /* now concatenate to sizestr !!!! */
416 if (sizestr
== NULL
) {
417 sizestr
= xstrdup(ptemp
);
419 sizestr
= xrealloc(sizestr
,
422 sizestr
= strcat(sizestr
, ptemp
);
423 /* build up length of array */
428 if (sizestr
== NULL
&& size
< inline_size
) {
430 * don't expand into inline code
431 * if size < inline_size
434 print_stat(indent
+ 1,
439 /* were already looking at a xdr_inlineable structure */
440 tabify(fout
, indent
+ 1);
442 f_print(fout
, "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
447 "buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
451 "buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
456 tabify(fout
, indent
+ 1);
458 "if (buf == NULL) {\n");
462 print_stat(indent
+ 2, &cur
->decl
);
466 f_print(fout
, "\n\t\t} else {\n");
470 emit_inline(indent
+ 2, &cur
->decl
, flag
);
474 tabify(fout
, indent
+ 1);
475 f_print(fout
, "}\n");
481 print_stat(indent
+ 1, &dl
->decl
);
486 if (sizestr
== NULL
&& size
< inline_size
) {
487 /* don't expand into inline code if size < inline_size */
489 print_stat(indent
+ 1, &cur
->decl
);
493 /* were already looking at a xdr_inlineable structure */
495 f_print(fout
, "\t\tbuf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
500 "\t\tbuf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
504 "\t\tbuf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
508 f_print(fout
, "\n\t\tif (buf == NULL) {\n");
510 while (cur
!= NULL
) {
511 print_stat(indent
+ 2, &cur
->decl
);
514 f_print(fout
, "\t\t} else {\n");
518 emit_inline(indent
+ 2, &cur
->decl
, flag
);
521 f_print(fout
, "\t\t}\n");
527 emit_struct(definition
*def
)
534 if (inline_size
== 0) {
535 /* No xdr_inlining at all */
536 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
537 print_stat(1, &dl
->decl
);
541 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
) {
542 if (dl
->decl
.rel
== REL_VECTOR
){
543 f_print(fout
, "\tint i;\n");
551 * Make a first pass and see if inling is possible.
553 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
) {
554 if ((dl
->decl
.prefix
== NULL
) &&
555 ((ptr
= find_type(dl
->decl
.type
)) != NULL
) &&
556 ((dl
->decl
.rel
== REL_ALIAS
)||
557 (dl
->decl
.rel
== REL_VECTOR
))) {
558 if (dl
->decl
.rel
== REL_ALIAS
)
562 break; /* can be inlined */
565 if (size
>= inline_size
) {
567 break; /* can be inlined */
572 if (size
>= inline_size
)
575 if (can_inline
== 0) { /* can not inline, drop back to old mode */
576 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
577 print_stat(1, &dl
->decl
);
582 for (j
= 0; j
< 2; j
++){
583 inline_struct(def
, flag
);
588 f_print(fout
, "\t\treturn (TRUE);\n\t}\n\n");
590 /* now take care of XDR_FREE case */
592 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
593 print_stat(1, &dl
->decl
);
598 emit_typedef(definition
*def
)
600 const char *prefix
= def
->def
.ty
.old_prefix
;
601 const char *type
= def
->def
.ty
.old_type
;
602 const char *amax
= def
->def
.ty
.array_max
;
603 relation rel
= def
->def
.ty
.rel
;
605 print_ifstat(1, prefix
, type
, rel
, amax
, "objp", def
->def_name
);
609 print_stat(int indent
, declaration
*dec
)
611 const char *prefix
= dec
->prefix
;
612 const char *type
= dec
->type
;
613 const char *amax
= dec
->array_max
;
614 relation rel
= dec
->rel
;
617 if (isvectordef(type
, rel
))
618 s_print(name
, "objp->%s", dec
->name
);
620 s_print(name
, "&objp->%s", dec
->name
);
621 print_ifstat(indent
, prefix
, type
, rel
, amax
, name
, dec
->name
);
626 emit_inline(int indent
, declaration
*decl
, int flag
)
630 emit_single_in_line(indent
, decl
, flag
, REL_ALIAS
);
633 tabify(fout
, indent
);
634 f_print(fout
, "{\n");
635 tabify(fout
, indent
+ 1);
636 f_print(fout
, "%s *genp;\n\n", decl
->type
);
637 tabify(fout
, indent
+ 1);
638 f_print(fout
, "for (i = 0, genp = objp->%s;\n", decl
->name
);
639 tabify(fout
, indent
+ 2);
640 f_print(fout
, "i < %s; i++) {\n", decl
->array_max
);
641 emit_single_in_line(indent
+ 2, decl
, flag
, REL_VECTOR
);
642 tabify(fout
, indent
+ 1);
643 f_print(fout
, "}\n");
644 tabify(fout
, indent
);
645 f_print(fout
, "}\n");
653 emit_single_in_line(int indent
, declaration
*decl
, int flag
, relation rel
)
657 tabify(fout
, indent
);
659 f_print(fout
, "IXDR_PUT_");
660 else if (rel
== REL_ALIAS
)
661 f_print(fout
, "objp->%s = IXDR_GET_", decl
->name
);
663 f_print(fout
, "*genp++ = IXDR_GET_");
665 upp_case
= upcase(decl
->type
);
668 if (strcmp(upp_case
, "INT") == 0)
671 upp_case
= strdup("LONG");
674 if (strcmp(upp_case
, "U_INT") == 0)
677 upp_case
= strdup("U_LONG");
680 if (rel
== REL_ALIAS
)
681 f_print(fout
, "%s(buf, objp->%s);\n", upp_case
,
684 f_print(fout
, "%s(buf, *genp++);\n", upp_case
);
686 f_print(fout
, "%s(buf);\n", upp_case
);
692 upcase(const char *str
)
696 ptr
= xmalloc(strlen(str
)+1);
700 *ptr
++ = toupper(*str
++);