]>
git.cameronkatri.com Git - apple_cmds.git/blob - developer_cmds/rpcgen/rpc_cout.c
1 /* $NetBSD: rpc_cout.c,v 1.14 1998/08/25 20:59:40 ross 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 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)rpc_cout.c 1.13 89/02/22 (C) 1987 SMI";
37 __RCSID("$NetBSD: rpc_cout.c,v 1.14 1998/08/25 20:59:40 ross Exp $");
42 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
50 #include "rpc_parse.h"
53 static int findtype
__P((definition
*, char *));
54 static int undefined
__P((char *));
55 static void print_generic_header
__P((char *, int));
56 static void print_header
__P((definition
*));
57 static void print_prog_header
__P((proc_list
*));
58 static void print_trailer
__P((void));
59 static void print_ifopen
__P((int, char *));
60 static void print_ifarg
__P((char *));
61 static void print_ifsizeof
__P((char *, char *));
62 static void print_ifclose
__P((int));
63 static void print_ifstat
__P((int, char *, char *, relation
, char *, char *, char *));
64 static void emit_enum
__P((definition
*));
65 static void emit_program
__P((definition
*));
66 static void emit_union
__P((definition
*));
67 static void emit_struct
__P((definition
*));
68 static void emit_typedef
__P((definition
*));
69 static void print_stat
__P((int, declaration
*));
72 * Emit the C-routine for the given definition
78 if (def
->def_kind
== DEF_CONST
) {
81 if (def
->def_kind
== DEF_PROGRAM
) {
85 if (def
->def_kind
== DEF_TYPEDEF
) {
86 /* now we need to handle declarations like struct typedef foo
87 * foo; since we dont want this to be expanded into 2 calls to
90 if (strcmp(def
->def
.ty
.old_type
, def
->def_name
) == 0)
96 switch (def
->def_kind
) {
111 errx(1, "Internal error %s, %d: Case %d not handled\n",
112 __FILE__
, __LINE__
, def
->def_kind
);
124 if (def
->def_kind
== DEF_PROGRAM
|| def
->def_kind
== DEF_CONST
) {
127 return (streq(def
->def_name
, type
));
137 def
= (definition
*) FINDVAL(defined
, type
, findtype
);
140 return (def
== NULL
);
144 print_generic_header(procname
, pointerp
)
149 f_print(fout
, "bool_t\n");
151 f_print(fout
, "xdr_%s(", procname
);
152 f_print(fout
, "XDR *xdrs, ");
153 f_print(fout
, "%s ", procname
);
156 f_print(fout
, "objp)\n{\n");
158 f_print(fout
, "xdr_%s(xdrs, objp)\n", procname
);
159 f_print(fout
, "\tXDR *xdrs;\n");
160 f_print(fout
, "\t%s ", procname
);
163 f_print(fout
, "objp;\n{\n");
171 print_generic_header(def
->def_name
,
172 def
->def_kind
!= DEF_TYPEDEF
||
173 !isvectordef(def
->def
.ty
.old_type
, def
->def
.ty
.rel
));
177 print_prog_header(plist
)
180 print_generic_header(plist
->args
.argname
, 1);
186 f_print(fout
, "\treturn (TRUE);\n");
187 f_print(fout
, "}\n");
192 print_ifopen(indent
, name
)
196 tabify(fout
, indent
);
197 f_print(fout
, "if (!xdr_%s(xdrs", name
);
204 f_print(fout
, ", %s", arg
);
208 print_ifsizeof(prefix
, type
)
212 if (streq(type
, "bool")) {
213 f_print(fout
, ", sizeof(bool_t), (xdrproc_t)xdr_bool");
215 f_print(fout
, ", sizeof(");
216 if (undefined(type
) && prefix
) {
217 f_print(fout
, "%s ", prefix
);
219 f_print(fout
, "%s), (xdrproc_t)xdr_%s", type
, type
);
224 print_ifclose(indent
)
227 f_print(fout
, "))\n");
228 tabify(fout
, indent
);
229 f_print(fout
, "\treturn (FALSE);\n");
233 print_ifstat(indent
, prefix
, type
, rel
, amax
, objname
, name
)
246 print_ifopen(indent
, "pointer");
247 print_ifarg("(char **)");
248 f_print(fout
, "%s", objname
);
249 print_ifsizeof(prefix
, type
);
252 if (streq(type
, "string")) {
255 if (streq(type
, "opaque")) {
259 print_ifopen(indent
, alt
);
260 print_ifarg(objname
);
262 print_ifopen(indent
, "vector");
263 print_ifarg("(char *)");
264 f_print(fout
, "%s", objname
);
268 print_ifsizeof(prefix
, type
);
272 if (streq(type
, "string")) {
275 if (streq(type
, "opaque")) {
278 if (streq(type
, "string")) {
279 print_ifopen(indent
, alt
);
280 print_ifarg(objname
);
283 print_ifopen(indent
, alt
);
285 print_ifopen(indent
, "array");
287 print_ifarg("(char **)");
288 if (*objname
== '&') {
289 f_print(fout
, "%s.%s_val, (u_int *)%s.%s_len",
290 objname
, name
, objname
, name
);
292 f_print(fout
, "&%s->%s_val, (u_int *)&%s->%s_len",
293 objname
, name
, objname
, name
);
298 print_ifsizeof(prefix
, type
);
302 print_ifopen(indent
, type
);
303 print_ifarg(objname
);
306 print_ifclose(indent
);
314 print_ifopen(1, "enum");
315 print_ifarg("(enum_t *)objp");
327 for (vlist
= def
->def
.pr
.versions
; vlist
!= NULL
; vlist
= vlist
->next
)
328 for (plist
= vlist
->procs
; plist
!= NULL
; plist
= plist
->next
) {
329 if (!newstyle
|| plist
->arg_num
< 2)
330 continue; /* old style, or single
332 print_prog_header(plist
);
333 for (dl
= plist
->args
.decls
; dl
!= NULL
;
335 print_stat(1, &dl
->decl
);
349 char *vecformat
= "objp->%s_u.%s";
350 char *format
= "&objp->%s_u.%s";
353 print_stat(1, &def
->def
.un
.enum_decl
);
354 f_print(fout
, "\tswitch (objp->%s) {\n", def
->def
.un
.enum_decl
.name
);
355 for (cl
= def
->def
.un
.cases
; cl
!= NULL
; cl
= cl
->next
) {
356 f_print(fout
, "\tcase %s:\n", cl
->case_name
);
357 if (cl
->contflag
== 1) /* a continued case statement */
360 if (!streq(cs
->type
, "void")) {
361 object
= alloc(strlen(def
->def_name
) + strlen(format
) +
362 strlen(cs
->name
) + 1);
363 if (isvectordef(cs
->type
, cs
->rel
)) {
364 s_print(object
, vecformat
, def
->def_name
,
367 s_print(object
, format
, def
->def_name
,
370 print_ifstat(2, cs
->prefix
, cs
->type
, cs
->rel
,
371 cs
->array_max
, object
, cs
->name
);
374 f_print(fout
, "\t\tbreak;\n");
376 dflt
= def
->def
.un
.default_decl
;
377 f_print(fout
, "\tdefault:\n");
379 if (!streq(dflt
->type
, "void")) {
380 object
= alloc(strlen(def
->def_name
) + strlen(format
) +
381 strlen(dflt
->name
) + 1);
382 if (isvectordef(dflt
->type
, dflt
->rel
)) {
383 s_print(object
, vecformat
, def
->def_name
,
386 s_print(object
, format
, def
->def_name
,
389 print_ifstat(2, dflt
->prefix
, dflt
->type
, dflt
->rel
,
390 dflt
->array_max
, object
, dflt
->name
);
393 f_print(fout
, "\t\tbreak;\n");
395 f_print(fout
, "\t\treturn (FALSE);\n");
398 f_print(fout
, "\t}\n");
406 int i
, j
, size
, flag
;
407 decl_list
*cur
= NULL
, *psav
;
409 char *sizestr
, *plus
;
416 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
417 print_stat(1, &dl
->decl
);
422 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
423 if ((dl
->decl
.prefix
== NULL
) &&
424 ((ptr
= find_type(dl
->decl
.type
)) != NULL
) &&
425 ((dl
->decl
.rel
== REL_ALIAS
) || (dl
->decl
.rel
== REL_VECTOR
))) {
427 if (dl
->decl
.rel
== REL_ALIAS
)
431 break; /* can be inlined */
434 if (size
>= doinline
) {
436 break; /* can be inlined */
443 if (can_inline
== 0) { /* can not inline, drop back to old mode */
445 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
446 print_stat(1, &dl
->decl
);
450 /* May cause lint to complain. but ... */
451 f_print(fout
, "\tint32_t *buf;\n");
454 f_print(fout
, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
456 for (j
= 0; j
< 2; j
++) {
460 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
) { /* xxx */
462 /* now walk down the list and check for basic types */
463 if ((dl
->decl
.prefix
== NULL
) && ((ptr
= find_type(dl
->decl
.type
)) != NULL
) && ((dl
->decl
.rel
== REL_ALIAS
) || (dl
->decl
.rel
== REL_VECTOR
))) {
468 if (dl
->decl
.rel
== REL_ALIAS
)
471 /* this is required to handle arrays */
478 if (ptr
->length
!= 1)
479 s_print(ptemp
, "%s%s * %d", plus
, dl
->decl
.array_max
, ptr
->length
);
481 s_print(ptemp
, "%s%s", plus
, dl
->decl
.array_max
);
483 /* now concatenate to sizestr !!!! */
485 sizestr
= strdup(ptemp
);
487 sizestr
= (char *) realloc(sizestr
, strlen(sizestr
) + strlen(ptemp
) + 1);
488 if (sizestr
== NULL
) {
490 f_print(stderr
, "Fatal error : no memory\n");
493 sizestr
= strcat(sizestr
, ptemp
); /* build up length of
501 if (sizestr
== NULL
&& size
< doinline
) {
502 /* don't expand into inline
503 * code if size < doinline */
505 print_stat(2, &cur
->decl
);
512 /* were already looking at a
513 * xdr_inlineable structure */
515 f_print(fout
, "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);\n",
520 "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, %s * BYTES_PER_XDR_UNIT);\n",
524 "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, (%d + %s) * BYTES_PER_XDR_UNIT);\n",
527 f_print(fout
, "\t\tif (buf == NULL) {\n");
531 print_stat(3, &cur
->decl
);
535 f_print(fout
, "\t\t} else {\n");
539 emit_inline(&cur
->decl
, flag
);
543 f_print(fout
, "\t\t}\n");
549 print_stat(2, &dl
->decl
);
554 if (sizestr
== NULL
&& size
< doinline
) {
555 /* don't expand into inline code if size <
558 print_stat(2, &cur
->decl
);
563 /* were already looking at a xdr_inlineable
566 f_print(fout
, "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);\n",
571 "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, %s * BYTES_PER_XDR_UNIT);\n",
575 "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, (%d + %s) * BYTES_PER_XDR_UNIT);\n",
578 f_print(fout
, "\t\tif (buf == NULL) {\n");
581 while (cur
!= NULL
) {
582 print_stat(3, &cur
->decl
);
585 f_print(fout
, "\t\t} else {\n");
589 emit_inline(&cur
->decl
, flag
);
593 f_print(fout
, "\t\t}\n");
599 f_print(fout
, "\t} else if (xdrs->x_op == XDR_DECODE) {\n");
603 f_print(fout
, "\t} else {\n");
605 /* now take care of XDR_FREE case */
607 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
)
608 print_stat(2, &dl
->decl
);
610 f_print(fout
, "\t}\n");
617 char *prefix
= def
->def
.ty
.old_prefix
;
618 char *type
= def
->def
.ty
.old_type
;
619 char *amax
= def
->def
.ty
.array_max
;
620 relation rel
= def
->def
.ty
.rel
;
623 print_ifstat(1, prefix
, type
, rel
, amax
, "objp", def
->def_name
);
627 print_stat(indent
, dec
)
631 char *prefix
= dec
->prefix
;
632 char *type
= dec
->type
;
633 char *amax
= dec
->array_max
;
634 relation rel
= dec
->rel
;
637 if (isvectordef(type
, rel
)) {
638 s_print(name
, "objp->%s", dec
->name
);
640 s_print(name
, "&objp->%s", dec
->name
);
642 print_ifstat(indent
, prefix
, type
, rel
, amax
, name
, dec
->name
);
647 emit_inline(decl
, flag
)
652 /*check whether an array or not */
656 emit_single_in_line(decl
, flag
, REL_ALIAS
);
659 f_print(fout
, "\t\t\t{\n");
660 f_print(fout
, "\t\t\t\tint i;\n");
661 f_print(fout
, "\t\t\t\t%s *genp;\n", decl
->type
);
663 f_print(fout
, "\t\t\t\tfor (i = 0, genp = objp->%s;\n",
665 f_print(fout
, "\t\t\t\t i < %s; i++) {\n\t\t",
667 emit_single_in_line(decl
, flag
, REL_VECTOR
);
668 f_print(fout
, "\t\t\t\t}\n\t\t\t}\n");
672 errx(1, "Internal error %s, %d: Case %d not handled\n",
673 __FILE__
, __LINE__
, decl
->rel
);
678 emit_single_in_line(decl
, flag
, rel
)
687 f_print(fout
, "\t\t\tIXDR_PUT_");
689 if (rel
== REL_ALIAS
)
690 f_print(fout
, "\t\t\tobjp->%s = IXDR_GET_", decl
->name
);
692 f_print(fout
, "\t\t\t*genp++ = IXDR_GET_");
694 upp_case
= upcase(decl
->type
);
697 if (strcmp(upp_case
, "INT") == 0) {
702 if (strcmp(upp_case
, "U_INT") == 0) {
708 if (rel
== REL_ALIAS
)
709 f_print(fout
, "%s(buf, objp->%s);\n", upp_case
, decl
->name
);
711 f_print(fout
, "%s(buf, *genp++);\n", upp_case
);
714 f_print(fout
, "%s(buf);\n", upp_case
);
728 ptr
= (char *) malloc(strlen(str
) + 1);
729 if (ptr
== (char *) NULL
) {
730 f_print(stderr
, "malloc failed\n");
736 *ptr
++ = toupper(*str
++);