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 * From: @(#)rpc_cout.c 1.13 89/02/22 (C) 1987 SMI
38 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
43 #include "rpc_parse.h"
47 static void emit_enum(definition
*def
);
48 static void emit_program(const definition
*def
);
49 static void emit_union(const definition
*def
);
50 static void emit_struct(definition
*def
);
51 static void emit_typedef(const definition
*def
);
52 static int findtype(const definition
*def
, const char *type
);
53 static int undefined(const char *type
);
54 static void print_generic_header(const char *procname
, int pointerp
);
55 static void print_ifopen(int indent
, const char *name
);
56 static void print_ifarg(const char *arg
);
57 static void print_ifsizeof(const char *prefix
, const char *type
);
58 static void print_ifclose(int indent
);
59 static void print_ifstat(int indent
, const char *prefix
, const char *type
,
60 relation rel
, const char *amax
,
61 const char *objname
, const char *name
);
62 static void print_stat(int indent
, const declaration
*dec
);
63 static void print_header(const definition
*def
);
64 static void print_trailer(void);
65 static char *upcase(const char *str
);
68 * Emit the C-routine for the given definition
73 if (def
->def_kind
== DEF_CONST
) {
76 if (def
->def_kind
== DEF_PROGRAM
) {
80 if (def
->def_kind
== DEF_TYPEDEF
)
82 /* now we need to handle declarations like
83 struct typedef foo foo;
84 since we don't want this to be expanded
85 into 2 calls to xdr_foo */
87 if (strcmp(def
->def
.ty
.old_type
,def
->def_name
)==0)
92 switch (def
->def_kind
) {
113 findtype(const definition
*def
, const char *type
)
115 if (def
->def_kind
== DEF_PROGRAM
|| def
->def_kind
== DEF_CONST
) {
118 return (streq(def
->def_name
, type
));
123 undefined(const char *type
)
126 def
= (definition
*) FINDVAL(defined
, type
, findtype
);
127 return (def
== NULL
);
132 print_generic_header(const 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(const definition
*def
)
156 print_generic_header(def
->def_name
,
157 def
->def_kind
!= DEF_TYPEDEF
||
158 !isvectordef(def
->def
.ty
.old_type
,
161 /* Now add Inline support */
165 /*May cause lint to complain. but ... */
166 f_print(fout
, "\t register long *buf;\n\n");
170 print_prog_header(const proc_list
*plist
)
172 print_generic_header(plist
->args
.argname
, 1 );
178 f_print(fout
, "\treturn (TRUE);\n");
179 f_print(fout
, "}\n");
184 print_ifopen(int indent
, const char *name
)
186 tabify(fout
, indent
);
187 f_print(fout
, " if (!xdr_%s(xdrs", name
);
191 print_ifarg(const char *arg
)
193 f_print(fout
, ", %s", arg
);
197 print_ifsizeof(const char *prefix
, const char *type
)
199 if (streq(type
, "bool")) {
200 f_print(fout
, ", sizeof(bool_t), (xdrproc_t)xdr_bool");
203 f_print(fout
, ", sizeof(");
204 if (undefined(type
) && prefix
) {
205 f_print(fout
, "%s ", prefix
);
207 f_print(fout
, "%s), (xdrproc_t)xdr_%s", type
, type
);
212 print_ifclose(int indent
)
214 f_print(fout
, ")) {\n");
215 tabify(fout
, indent
);
216 f_print(fout
, "\t return (FALSE);\n");
217 tabify(fout
, indent
);
218 f_print(fout
, " }\n");
222 print_ifstat(int indent
, const char *prefix
, const char *type
, relation rel
,
223 const char *amax
, const char *objname
, const char *name
)
225 const char *alt
= NULL
;
229 print_ifopen(indent
, "pointer");
230 print_ifarg("(char **)");
231 f_print(fout
, "%s", objname
);
232 print_ifsizeof(prefix
, type
);
235 if (streq(type
, "string")) {
237 } else if (streq(type
, "opaque")) {
241 print_ifopen(indent
, alt
);
242 print_ifarg(objname
);
244 print_ifopen(indent
, "vector");
245 print_ifarg("(char *)");
246 f_print(fout
, "%s", objname
);
250 print_ifsizeof(prefix
, type
);
254 if (streq(type
, "string")) {
256 } else if (streq(type
, "opaque")) {
259 if (streq(type
, "string")) {
260 print_ifopen(indent
, alt
);
261 print_ifarg(objname
);
264 print_ifopen(indent
, alt
);
266 print_ifopen(indent
, "array");
268 print_ifarg("(char **)");
269 if (*objname
== '&') {
270 f_print(fout
, "%s.%s_val, (u_int *)%s.%s_len",
271 objname
, name
, objname
, name
);
273 f_print(fout
, "&%s->%s_val, (u_int *)&%s->%s_len",
274 objname
, name
, objname
, name
);
279 print_ifsizeof(prefix
, type
);
283 print_ifopen(indent
, type
);
284 print_ifarg(objname
);
287 print_ifclose(indent
);
291 emit_enum(definition
*def
)
295 print_ifopen(1, "enum");
296 print_ifarg("(enum_t *)objp");
301 emit_program(const definition
*def
)
307 for (vlist
= def
->def
.pr
.versions
; vlist
!= NULL
;vlist
= vlist
->next
)
308 for(plist
= vlist
->procs
; plist
!= NULL
; plist
= plist
->next
) {
309 if (!newstyle
|| plist
->arg_num
< 2)
310 continue; /* old style, or single argument */
311 print_prog_header(plist
);
312 for (dl
= plist
->args
.decls
; dl
!= NULL
;
314 print_stat(1,&dl
->decl
);
320 emit_union(const definition
*def
)
326 const char *vecformat
= "objp->%s_u.%s";
327 const char *format
= "&objp->%s_u.%s";
329 print_stat(1,&def
->def
.un
.enum_decl
);
330 f_print(fout
, "\tswitch (objp->%s) {\n", def
->def
.un
.enum_decl
.name
);
331 for (cl
= def
->def
.un
.cases
; cl
!= NULL
; cl
= cl
->next
) {
333 f_print(fout
, "\tcase %s:\n", cl
->case_name
);
334 if(cl
->contflag
== 1) /* a continued case statement */
337 if (!streq(cs
->type
, "void")) {
338 object
= alloc(strlen(def
->def_name
) + strlen(format
) +
339 strlen(cs
->name
) + 1);
340 if (isvectordef(cs
->type
, cs
->rel
)) {
341 s_print(object
, vecformat
, def
->def_name
,
344 s_print(object
, format
, def
->def_name
,
347 print_ifstat(2, cs
->prefix
, cs
->type
, cs
->rel
, cs
->array_max
,
351 f_print(fout
, "\t\tbreak;\n");
353 dflt
= def
->def
.un
.default_decl
;
355 if (!streq(dflt
->type
, "void")) {
356 f_print(fout
, "\tdefault:\n");
357 object
= alloc(strlen(def
->def_name
) + strlen(format
) +
358 strlen(dflt
->name
) + 1);
359 if (isvectordef(dflt
->type
, dflt
->rel
)) {
360 s_print(object
, vecformat
, def
->def_name
,
363 s_print(object
, format
, def
->def_name
,
367 print_ifstat(2, dflt
->prefix
, dflt
->type
, dflt
->rel
,
368 dflt
->array_max
, object
, dflt
->name
);
370 f_print(fout
, "\t\tbreak;\n");
372 #ifdef __GNU_LIBRARY__
374 f_print(fout
, "\tdefault:\n");
375 f_print(fout
, "\t\tbreak;\n");
379 f_print(fout
, "\tdefault:\n");
380 f_print(fout
, "\t\treturn (FALSE);\n");
383 f_print(fout
, "\t}\n");
386 /* this may be const. i haven't traced this one through yet. */
389 emit_struct(definition
*def
)
393 decl_list
*cur
= NULL
,*psav
;
401 if(inlineflag
== 0) {
402 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
403 print_stat(1,&dl
->decl
);
408 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
409 if(dl
->decl
.rel
== REL_VECTOR
){
410 f_print(fout
,"\t int i;\n");
415 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
416 if((dl
->decl
.prefix
== NULL
) && ((ptr
=find_type(dl
->decl
.type
))!= NULL
) && ((dl
->decl
.rel
== REL_ALIAS
)||(dl
->decl
.rel
== REL_VECTOR
))){
418 if(dl
->decl
.rel
== REL_ALIAS
)
422 break; /* can be inlined */
426 if(size
>= inlineflag
){
428 break; /* can be inlined */
432 if(size
> inlineflag
)
435 if(can_inline
== 0){ /* can not inline, drop back to old mode */
436 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
437 print_stat(1,&dl
->decl
);
445 f_print(fout
,"\n\t if (xdrs->x_op == XDR_ENCODE) {\n");
447 f_print(fout
,"\n \t return (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
453 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
) { /* xxx */
455 /* now walk down the list and check for basic types */
456 if((dl
->decl
.prefix
== NULL
) && ((ptr
=find_type(dl
->decl
.type
))!= NULL
) && ((dl
->decl
.rel
== REL_ALIAS
)||(dl
->decl
.rel
== REL_VECTOR
))){
461 if(dl
->decl
.rel
== REL_ALIAS
)
464 /* this is required to handle arrays */
472 s_print(ptemp
," %s %s * %d",plus
,dl
->decl
.array_max
,ptr
->length
);
474 s_print(ptemp
," %s %s ",plus
,dl
->decl
.array_max
);
476 /*now concatenate to sizestr !!!! */
478 sizestr
=strdup(ptemp
);
480 sizestr
=realloc(sizestr
,strlen(sizestr
)+strlen(ptemp
)+1);
483 f_print(stderr
, "Fatal error : no memory \n");
486 sizestr
=strcat(sizestr
,ptemp
); /*build up length of array */
494 if(sizestr
== NULL
&& size
< inlineflag
){
495 /* don't expand into inline code if size < inlineflag */
497 print_stat(1,&cur
->decl
);
505 /* were already looking at a xdr_inlineable structure */
507 f_print(fout
,"\t buf = XDR_INLINE(xdrs,%d * BYTES_PER_XDR_UNIT);",
512 "\t buf = XDR_INLINE(xdrs,%s * BYTES_PER_XDR_UNIT);",
516 "\t buf = XDR_INLINE(xdrs,(%d + %s)* BYTES_PER_XDR_UNIT);",
519 f_print(fout
,"\n\t if (buf == NULL) {\n");
523 print_stat(2,&cur
->decl
);
527 f_print(fout
,"\n\t }\n\t else {\n");
531 emit_inline(&cur
->decl
,flag
);
535 f_print(fout
,"\t }\n");
537 size
=0;i
=0;sizestr
=NULL
;
538 print_stat(1,&dl
->decl
);
543 if(sizestr
== NULL
&& size
< inlineflag
){
544 /* don't expand into inline code if size < inlineflag */
546 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);",
559 "\t\tbuf = XDR_INLINE(xdrs,%s * BYTES_PER_XDR_UNIT);",
563 "\t\tbuf = XDR_INLINE(xdrs,(%d + %s)* BYTES_PER_XDR_UNIT);",
566 f_print(fout
,"\n\t\tif (buf == NULL) {\n");
570 print_stat(2,&cur
->decl
);
573 f_print(fout
,"\n\t }\n\t else {\n");
577 emit_inline(&cur
->decl
,flag
);
581 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
);
597 emit_typedef(const definition
*def
)
599 const char *prefix
= def
->def
.ty
.old_prefix
;
600 const char *type
= def
->def
.ty
.old_type
;
601 const char *amax
= def
->def
.ty
.array_max
;
602 relation rel
= def
->def
.ty
.rel
;
605 print_ifstat(1, prefix
, type
, rel
, amax
, "objp", def
->def_name
);
609 print_stat(int indent
, const 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
);
622 print_ifstat(indent
, prefix
, type
, rel
, amax
, name
, dec
->name
);
627 emit_inline(declaration
*decl
, int flag
)
630 /*check whether an array or not */
635 emit_single_in_line(decl
,flag
,REL_ALIAS
);
638 f_print(fout
,"\t\t{ register %s *genp; \n",decl
->type
);
639 f_print(fout
,"\t\t for ( i = 0,genp=objp->%s;\n \t\t\ti < %s; i++){\n\t\t",
640 decl
->name
,decl
->array_max
);
641 emit_single_in_line(decl
,flag
,REL_VECTOR
);
642 f_print(fout
,"\t\t }\n\t\t };\n");
645 /* ?... do nothing I guess */
650 emit_single_in_line(declaration
*decl
, int flag
, relation rel
)
653 const char *upp_case
;
656 f_print(fout
,"\t\t IXDR_PUT_");
659 if(rel
== REL_ALIAS
) {
660 f_print(fout
, "\t\t objp->%s = IXDR_GET_", decl
->name
);
663 f_print(fout
,"\t\t *genp++ = IXDR_GET_");
667 upp_case1
= upcase(decl
->type
);
668 upp_case
= upp_case1
;
671 if (!strcmp(upp_case
, "INT")) upp_case
="LONG";
672 if (!strcmp(upp_case
, "U_INT")) upp_case
="U_LONG";
675 if (rel
==REL_ALIAS
) {
676 f_print(fout
,"%s(buf,objp->%s);\n",upp_case
,decl
->name
);
679 f_print(fout
,"%s(buf,*genp++);\n",upp_case
);
683 f_print(fout
,"%s(buf);\n",upp_case
);
690 static char *upcase(const char *str
) {
692 ptr
= malloc(strlen(str
));
694 f_print(stderr
,"malloc failed\n");
699 *ptr
++ = toupper(*str
++);