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, MERCHANTIBILITY 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.13 89/02/22 (C) 1987 SMI
30 * $FreeBSD: src/usr.bin/rpcgen/rpc_cout.c,v 1.7 1999/08/28 01:05:16 peter Exp $
31 * $DragonFly: src/usr.bin/rpcgen/rpc_cout.c,v 1.4 2004/06/19 16:40:36 joerg Exp $
34 #ident "@(#)rpc_cout.c 1.14 93/07/05 SMI"
37 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
38 * Copyright (C) 1987, Sun Microsystems, Inc.
44 #include "rpc_parse.h"
47 static void print_header(definition
*);
48 static void print_trailer(void);
49 static void print_stat(int , declaration
*);
50 static void emit_enum(definition
*);
51 static void emit_program(definition
*);
52 static void emit_union(definition
*);
53 static void emit_struct(definition
*);
54 static void emit_typedef(definition
*);
55 static void emit_inline(int, declaration
*, int);
56 static void emit_single_in_line(int, declaration
*, int, relation
);
57 static char *upcase(char *str
);
60 * Emit the C-routine for the given definition
65 if (def
->def_kind
== DEF_CONST
)
67 if (def
->def_kind
== DEF_PROGRAM
) {
71 if (def
->def_kind
== DEF_TYPEDEF
) {
73 * now we need to handle declarations like
74 * struct typedef foo foo;
75 * since we dont want this to be expanded into 2 calls to xdr_foo
78 if (strcmp(def
->def
.ty
.old_type
, def
->def_name
) == 0)
82 switch (def
->def_kind
) {
95 /* DEF_CONST and DEF_PROGRAM have already been handled */
103 findtype(definition
*def
, char *type
)
106 if (def
->def_kind
== DEF_PROGRAM
|| def
->def_kind
== DEF_CONST
)
109 return(streq(def
->def_name
, type
));
113 undefined(char *type
)
117 def
= (definition
*) FINDVAL(defined
, type
, findtype
);
123 print_generic_header(char *procname
, int pointerp
)
126 f_print(fout
, "bool_t\n");
128 f_print(fout
, "xdr_%s(", procname
);
129 f_print(fout
, "register XDR *xdrs, ");
130 f_print(fout
, "%s ", procname
);
133 f_print(fout
, "objp)\n{\n\n");
135 f_print(fout
, "xdr_%s(xdrs, objp)\n", procname
);
136 f_print(fout
, "\tregister XDR *xdrs;\n");
137 f_print(fout
, "\t%s ", procname
);
140 f_print(fout
, "objp;\n{\n\n");
145 print_header(definition
*def
)
147 print_generic_header(def
->def_name
,
148 def
->def_kind
!= DEF_TYPEDEF
|| !isvectordef(def
->def
.ty
.old_type
,
150 /* Now add Inline support */
152 if (rpcgen_inline
== 0)
154 /* May cause lint to complain. but ... */
155 f_print(fout
, "\tregister long *buf;\n\n");
159 print_prog_header(proc_list
*plist
)
161 print_generic_header(plist
->args
.argname
, 1);
167 f_print(fout
, "\treturn (TRUE);\n");
168 f_print(fout
, "}\n");
173 print_ifopen(int indent
, char *name
)
175 tabify(fout
, indent
);
176 f_print(fout
, "if (!xdr_%s(xdrs", name
);
180 print_ifarg(char *arg
)
182 f_print(fout
, ", %s", arg
);
186 print_ifsizeof(int indent
, char *prefix
, char *type
)
189 f_print(fout
, ",\n");
190 tabify(fout
, indent
);
194 if (streq(type
, "bool")) {
195 f_print(fout
, "sizeof (bool_t), (xdrproc_t) xdr_bool");
197 f_print(fout
, "sizeof (");
198 if (undefined(type
) && prefix
)
199 f_print(fout
, "%s ", prefix
);
200 f_print(fout
, "%s), (xdrproc_t) xdr_%s", type
, type
);
205 print_ifclose(int indent
)
207 f_print(fout
, "))\n");
208 tabify(fout
, indent
);
209 f_print(fout
, "\treturn (FALSE);\n");
213 print_ifstat(int indent
, char *prefix
, char *type
, relation rel
, char *amax
,
214 char *objname
, char *name
)
220 print_ifopen(indent
, "pointer");
221 print_ifarg("(char **)");
222 f_print(fout
, "%s", objname
);
223 print_ifsizeof(0, prefix
, type
);
226 if (streq(type
, "string"))
228 else if (streq(type
, "opaque"))
232 print_ifopen(indent
, alt
);
233 print_ifarg(objname
);
235 print_ifopen(indent
, "vector");
236 print_ifarg("(char *)");
237 f_print(fout
, "%s", objname
);
241 print_ifsizeof(indent
+ 1, prefix
, type
);
244 if (streq(type
, "string"))
246 else if (streq(type
, "opaque"))
249 if (streq(type
, "string")) {
250 print_ifopen(indent
, alt
);
251 print_ifarg(objname
);
254 print_ifopen(indent
, alt
);
256 print_ifopen(indent
, "array");
257 print_ifarg("(char **)");
258 if (*objname
== '&') {
259 f_print(fout
, "%s.%s_val, (u_int *) %s.%s_len",
260 objname
, name
, objname
, name
);
263 "&%s->%s_val, (u_int *) &%s->%s_len",
264 objname
, name
, objname
, name
);
269 print_ifsizeof(indent
+ 1, prefix
, type
);
272 print_ifopen(indent
, type
);
273 print_ifarg(objname
);
276 print_ifclose(indent
);
281 emit_enum(definition
*def
)
283 print_ifopen(1, "enum");
284 print_ifarg("(enum_t *)objp");
289 emit_program(definition
*def
)
295 for (vlist
= def
->def
.pr
.versions
; vlist
!= NULL
; vlist
= vlist
->next
)
296 for (plist
= vlist
->procs
; plist
!= NULL
; plist
= plist
->next
) {
297 if (!newstyle
|| plist
->arg_num
< 2)
298 continue; /* old style, or single argument */
299 print_prog_header(plist
);
300 for (dl
= plist
->args
.decls
; dl
!= NULL
;
302 print_stat(1, &dl
->decl
);
309 emit_union(definition
*def
)
315 char *vecformat
= "objp->%s_u.%s";
316 char *format
= "&objp->%s_u.%s";
318 print_stat(1, &def
->def
.un
.enum_decl
);
319 f_print(fout
, "\tswitch (objp->%s) {\n", def
->def
.un
.enum_decl
.name
);
320 for (cl
= def
->def
.un
.cases
; cl
!= NULL
; cl
= cl
->next
) {
322 f_print(fout
, "\tcase %s:\n", cl
->case_name
);
323 if (cl
->contflag
== 1) /* a continued case statement */
326 if (!streq(cs
->type
, "void")) {
327 object
= alloc(strlen(def
->def_name
) + strlen(format
) +
328 strlen(cs
->name
) + 1);
329 if (isvectordef (cs
->type
, cs
->rel
)) {
330 s_print(object
, vecformat
, def
->def_name
,
333 s_print(object
, format
, def
->def_name
,
336 print_ifstat(2, cs
->prefix
, cs
->type
, cs
->rel
,
337 cs
->array_max
, object
, cs
->name
);
340 f_print(fout
, "\t\tbreak;\n");
342 dflt
= def
->def
.un
.default_decl
;
344 if (!streq(dflt
->type
, "void")) {
345 f_print(fout
, "\tdefault:\n");
346 object
= alloc(strlen(def
->def_name
) + strlen(format
) +
347 strlen(dflt
->name
) + 1);
348 if (isvectordef (dflt
->type
, dflt
->rel
)) {
349 s_print(object
, vecformat
, def
->def_name
,
352 s_print(object
, format
, def
->def_name
,
356 print_ifstat(2, dflt
->prefix
, dflt
->type
, dflt
->rel
,
357 dflt
->array_max
, object
, dflt
->name
);
359 f_print(fout
, "\t\tbreak;\n");
361 f_print(fout
, "\tdefault:\n");
362 f_print(fout
, "\t\tbreak;\n");
365 f_print(fout
, "\tdefault:\n");
366 f_print(fout
, "\t\treturn (FALSE);\n");
369 f_print(fout
, "\t}\n");
373 inline_struct(definition
*def
, int flag
)
377 decl_list
*cur
, *psav
;
379 char *sizestr
, *plus
;
384 f_print(fout
, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
386 f_print(fout
, "\t\treturn (TRUE);\n"
387 "\t} else if (xdrs->x_op == XDR_DECODE) {\n");
392 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
) { /* xxx */
393 /* now walk down the list and check for basic types */
394 if ((dl
->decl
.prefix
== NULL
) &&
395 ((ptr
= find_type(dl
->decl
.type
)) != NULL
) &&
396 ((dl
->decl
.rel
== REL_ALIAS
) ||
397 (dl
->decl
.rel
== REL_VECTOR
))){
402 if (dl
->decl
.rel
== REL_ALIAS
) {
405 /* this code is required to handle arrays */
411 if (ptr
->length
!= 1)
412 s_print(ptemp
, "%s%s * %d",
413 plus
, dl
->decl
.array_max
,
416 s_print(ptemp
, "%s%s", plus
,
419 /* now concatenate to sizestr !!!! */
420 if (sizestr
== NULL
) {
421 sizestr
= strdup(ptemp
);
423 sizestr
= realloc(sizestr
,
426 if (sizestr
== NULL
){
427 warnx("fatal error: no memory");
430 sizestr
= strcat(sizestr
, ptemp
);
431 /* build up length of array */
436 if (sizestr
== NULL
&& size
< rpcgen_inline
){
438 * don't expand into inline code
439 * if size < rpcgen_inline
442 print_stat(indent
+ 1,
447 /* were already looking at a xdr_inlineable structure */
448 tabify(fout
, indent
+ 1);
450 f_print(fout
, "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
455 "buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
459 "buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
464 tabify(fout
, indent
+ 1);
466 "if (buf == NULL) {\n");
470 print_stat(indent
+ 2, &cur
->decl
);
474 f_print(fout
, "\n\t\t} else {\n");
478 emit_inline(indent
+ 2, &cur
->decl
, flag
);
482 tabify(fout
, indent
+ 1);
483 f_print(fout
, "}\n");
489 print_stat(indent
+ 1, &dl
->decl
);
494 if (sizestr
== NULL
&& size
< rpcgen_inline
) {
495 /* don't expand into inline code if size < rpcgen_inline */
497 print_stat(indent
+ 1, &cur
->decl
);
501 /* were already looking at a xdr_inlineable structure */
503 f_print(fout
, "\t\tbuf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
508 "\t\tbuf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
512 "\t\tbuf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
516 f_print(fout
, "\n\t\tif (buf == NULL) {\n");
518 while (cur
!= NULL
) {
519 print_stat(indent
+ 2, &cur
->decl
);
522 f_print(fout
, "\t\t} else {\n");
526 emit_inline(indent
+ 2, &cur
->decl
, flag
);
529 f_print(fout
, "\t\t}\n");
542 if (rpcgen_inline
== 0) {
543 /* No xdr_inlining at all */
544 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
545 print_stat(1, &dl
->decl
);
549 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
) {
550 if (dl
->decl
.rel
== REL_VECTOR
){
551 f_print(fout
, "\tint i;\n");
559 * Make a first pass and see if inling is possible.
561 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
) {
562 if ((dl
->decl
.prefix
== NULL
) &&
563 ((ptr
= find_type(dl
->decl
.type
)) != NULL
) &&
564 ((dl
->decl
.rel
== REL_ALIAS
)||
565 (dl
->decl
.rel
== REL_VECTOR
))) {
566 if (dl
->decl
.rel
== REL_ALIAS
)
570 break; /* can be inlined */
573 if (size
>= rpcgen_inline
) {
575 break; /* can be inlined */
580 if (size
>= rpcgen_inline
)
583 if (can_inline
== 0) { /* can not inline, drop back to old mode */
584 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
585 print_stat(1, &dl
->decl
);
590 for (j
= 0; j
< 2; j
++){
591 inline_struct(def
, flag
);
596 f_print(fout
, "\t\treturn (TRUE);\n\t}\n\n");
598 /* now take care of XDR_FREE case */
600 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
601 print_stat(1, &dl
->decl
);
606 emit_typedef(definition
*def
)
608 char *prefix
= def
->def
.ty
.old_prefix
;
609 char *type
= def
->def
.ty
.old_type
;
610 char *amax
= def
->def
.ty
.array_max
;
611 relation rel
= def
->def
.ty
.rel
;
613 print_ifstat(1, prefix
, type
, rel
, amax
, "objp", def
->def_name
);
617 print_stat(int indent
, declaration
*dec
)
619 char *prefix
= dec
->prefix
;
620 char *type
= dec
->type
;
621 char *amax
= dec
->array_max
;
622 relation rel
= dec
->rel
;
625 if (isvectordef(type
, rel
))
626 s_print(name
, "objp->%s", dec
->name
);
628 s_print(name
, "&objp->%s", dec
->name
);
629 print_ifstat(indent
, prefix
, type
, rel
, amax
, name
, dec
->name
);
635 emit_inline(int indent
, declaration
*decl
, int flag
)
639 emit_single_in_line(indent
, decl
, flag
, REL_ALIAS
);
642 tabify(fout
, indent
);
643 f_print(fout
, "{\n");
644 tabify(fout
, indent
+ 1);
645 f_print(fout
, "register %s *genp;\n\n", decl
->type
);
646 tabify(fout
, indent
+ 1);
647 f_print(fout
, "for (i = 0, genp = objp->%s;\n", decl
->name
);
648 tabify(fout
, indent
+ 2);
649 f_print(fout
, "i < %s; i++) {\n", decl
->array_max
);
650 emit_single_in_line(indent
+ 2, decl
, flag
, REL_VECTOR
);
651 tabify(fout
, indent
+ 1);
652 f_print(fout
, "}\n");
653 tabify(fout
, indent
);
654 f_print(fout
, "}\n");
661 emit_single_in_line(int indent
, declaration
*decl
, int flag
, relation rel
)
666 tabify(fout
, indent
);
668 f_print(fout
, "IXDR_PUT_");
669 else if (rel
== REL_ALIAS
)
670 f_print(fout
, "objp->%s = IXDR_GET_", decl
->name
);
672 f_print(fout
, "*genp++ = IXDR_GET_");
674 upp_case
= upcase(decl
->type
);
677 if (strcmp(upp_case
, "INT") == 0)
684 if (strcmp(upp_case
, "U_INT") == 0)
691 if (rel
== REL_ALIAS
)
692 f_print(fout
, "%s(buf, objp->%s);\n", upp_case
,
695 f_print(fout
, "%s(buf, *genp++);\n", upp_case
);
697 f_print(fout
, "%s(buf);\n", upp_case
);
708 ptr
= malloc(strlen(str
)+1);
710 errx(1, "malloc failed");
714 *ptr
++ = toupper(*str
++);