update from main archive 961016
[glibc.git] / sunrpc / rpc_cout.c
blob378a686fa7d4b11fc8139d831170f0216fff1413
1 /*
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.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
32 * From: @(#)rpc_cout.c 1.13 89/02/22 (C) 1987 SMI
34 char cout_rcsid[] =
35 "$Id$";
38 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
40 #include <ctype.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include "rpc_parse.h"
44 #include "rpc_util.h"
45 #include "proto.h"
47 static void emit_enum(definition *def);
48 static void emit_program(definition *def);
49 static void emit_union(definition *def);
50 static void emit_struct(definition *def);
51 static void emit_typedef(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, declaration *dec);
63 static void print_header(definition *def);
64 static void print_trailer(void);
65 static char *upcase(const char *str);
68 * Emit the C-routine for the given definition
70 void
71 emit(definition *def)
73 if (def->def_kind == DEF_CONST) {
74 return;
76 if (def->def_kind == DEF_PROGRAM) {
77 emit_program(def);
78 return;
80 if(def->def_kind == DEF_TYPEDEF)
82 /* now we need to handle declarations like
83 struct typedef foo foo;
84 since we dont want this to be expanded into 2 calls to xdr_foo */
86 if(strcmp(def->def.ty.old_type,def->def_name)==0)
87 return;
90 print_header(def);
91 switch (def->def_kind) {
92 case DEF_UNION:
93 emit_union(def);
94 break;
95 case DEF_ENUM:
96 emit_enum(def);
97 break;
98 case DEF_STRUCT:
99 emit_struct(def);
100 break;
101 case DEF_TYPEDEF:
102 emit_typedef(def);
103 break;
104 default:
105 /* can't happen */
107 print_trailer();
110 static int
111 findtype(const definition *def, const char *type)
114 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
115 return (0);
116 } else {
117 return (streq(def->def_name, type));
121 static int
122 undefined(const char *type)
124 definition *def;
126 def = (definition *) FINDVAL(defined, type, findtype);
129 return (def == NULL);
133 static void
134 print_generic_header(const char *procname, int pointerp)
136 f_print(fout, "\n");
137 f_print(fout, "bool_t\n");
138 if (Cflag) {
139 f_print(fout, "xdr_%s(", procname);
140 f_print(fout, "XDR *xdrs, ");
141 f_print(fout, "%s ", procname);
142 if( pointerp )
143 f_print(fout, "*");
144 f_print(fout, "objp)\n{\n\n");
145 } else {
146 f_print(fout, "xdr_%s(xdrs, objp)\n", procname);
147 f_print(fout, "\tXDR *xdrs;\n");
148 f_print(fout, "\t%s ", procname);
149 if( pointerp )
150 f_print(fout, "*");
151 f_print(fout, "objp;\n{\n\n");
155 static void
156 print_header(definition *def)
160 decl_list *dl;
161 bas_type *ptr;
162 int i;
165 print_generic_header( def->def_name,
166 def->def_kind != DEF_TYPEDEF ||
167 !isvectordef(def->def.ty.old_type, def->def.ty.rel));
169 /* Now add Inline support */
172 if(inlineflag == 0 )
173 return;
174 /*May cause lint to complain. but ... */
175 f_print(fout, "\t register long *buf;\n\n");
179 static void
180 print_prog_header(proc_list *plist)
182 print_generic_header( plist->args.argname, 1 );
185 static void
186 print_trailer(void)
188 f_print(fout, "\treturn (TRUE);\n");
189 f_print(fout, "}\n");
193 static void
194 print_ifopen(int indent, const char *name)
196 tabify(fout, indent);
197 f_print(fout, " if (!xdr_%s(xdrs", name);
200 static void
201 print_ifarg(const char *arg)
203 f_print(fout, ", %s", arg);
206 static void
207 print_ifsizeof(const char *prefix, const char *type)
209 if (streq(type, "bool")) {
210 f_print(fout, ", sizeof(bool_t), (xdrproc_t)xdr_bool");
212 else {
213 f_print(fout, ", sizeof(");
214 if (undefined(type) && prefix) {
215 f_print(fout, "%s ", prefix);
217 f_print(fout, "%s), (xdrproc_t)xdr_%s", type, type);
221 static void
222 print_ifclose(int indent)
224 f_print(fout, ")) {\n");
225 tabify(fout, indent);
226 f_print(fout, "\t return (FALSE);\n");
227 tabify(fout, indent);
228 f_print(fout, " }\n");
231 static void
232 print_ifstat(int indent, const char *prefix, const char *type, relation rel,
233 const char *amax, const char *objname, const char *name)
235 const char *alt = NULL;
237 switch (rel) {
238 case REL_POINTER:
239 print_ifopen(indent, "pointer");
240 print_ifarg("(char **)");
241 f_print(fout, "%s", objname);
242 print_ifsizeof(prefix, type);
243 break;
244 case REL_VECTOR:
245 if (streq(type, "string")) {
246 alt = "string";
247 } else if (streq(type, "opaque")) {
248 alt = "opaque";
250 if (alt) {
251 print_ifopen(indent, alt);
252 print_ifarg(objname);
253 } else {
254 print_ifopen(indent, "vector");
255 print_ifarg("(char *)");
256 f_print(fout, "%s", objname);
258 print_ifarg(amax);
259 if (!alt) {
260 print_ifsizeof(prefix, type);
262 break;
263 case REL_ARRAY:
264 if (streq(type, "string")) {
265 alt = "string";
266 } else if (streq(type, "opaque")) {
267 alt = "bytes";
269 if (streq(type, "string")) {
270 print_ifopen(indent, alt);
271 print_ifarg(objname);
272 } else {
273 if (alt) {
274 print_ifopen(indent, alt);
275 } else {
276 print_ifopen(indent, "array");
278 print_ifarg("(char **)");
279 if (*objname == '&') {
280 f_print(fout, "%s.%s_val, (u_int *)%s.%s_len",
281 objname, name, objname, name);
282 } else {
283 f_print(fout, "&%s->%s_val, (u_int *)&%s->%s_len",
284 objname, name, objname, name);
287 print_ifarg(amax);
288 if (!alt) {
289 print_ifsizeof(prefix, type);
291 break;
292 case REL_ALIAS:
293 print_ifopen(indent, type);
294 print_ifarg(objname);
295 break;
297 print_ifclose(indent);
300 static void
301 emit_enum(definition *def)
303 (void)def;
305 print_ifopen(1, "enum");
306 print_ifarg("(enum_t *)objp");
307 print_ifclose(1);
310 static void
311 emit_program(definition *def)
313 decl_list *dl;
314 version_list *vlist;
315 proc_list *plist;
317 for (vlist = def->def.pr.versions; vlist != NULL;vlist = vlist->next)
318 for(plist = vlist->procs; plist != NULL; plist = plist->next) {
319 if (!newstyle || plist->arg_num < 2)
320 continue; /* old style, or single argument */
321 print_prog_header(plist);
322 for (dl = plist->args.decls; dl != NULL;
323 dl = dl->next)
324 print_stat(1,&dl->decl);
325 print_trailer();
329 static void
330 emit_union(definition *def)
332 declaration *dflt;
333 case_list *cl;
334 declaration *cs;
335 char *object;
336 const char *vecformat = "objp->%s_u.%s";
337 const char *format = "&objp->%s_u.%s";
339 print_stat(1,&def->def.un.enum_decl);
340 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
341 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
343 f_print(fout, "\tcase %s:\n", cl->case_name);
344 if(cl->contflag == 1) /* a continued case statement */
345 continue;
346 cs = &cl->case_decl;
347 if (!streq(cs->type, "void")) {
348 object = alloc(strlen(def->def_name) + strlen(format) +
349 strlen(cs->name) + 1);
350 if (isvectordef(cs->type, cs->rel)) {
351 s_print(object, vecformat, def->def_name,
352 cs->name);
353 } else {
354 s_print(object, format, def->def_name,
355 cs->name);
357 print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max,
358 object, cs->name);
359 free(object);
361 f_print(fout, "\t\tbreak;\n");
363 dflt = def->def.un.default_decl;
364 if (dflt != NULL) {
365 if (!streq(dflt->type, "void")) {
366 f_print(fout, "\tdefault:\n");
367 object = alloc(strlen(def->def_name) + strlen(format) +
368 strlen(dflt->name) + 1);
369 if (isvectordef(dflt->type, dflt->rel)) {
370 s_print(object, vecformat, def->def_name,
371 dflt->name);
372 } else {
373 s_print(object, format, def->def_name,
374 dflt->name);
377 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
378 dflt->array_max, object, dflt->name);
379 free(object);
380 f_print(fout, "\t\tbreak;\n");
382 #ifdef __GNU_LIBRARY__
383 else {
384 f_print(fout, "\tdefault:\n");
385 f_print(fout, "\t\tbreak;\n");
387 #endif
388 } else {
389 f_print(fout, "\tdefault:\n");
390 f_print(fout, "\t\treturn (FALSE);\n");
393 f_print(fout, "\t}\n");
396 static void
397 emit_struct(definition *def)
399 decl_list *dl;
400 int i,j,size,flag;
401 decl_list *cur = NULL,*psav;
402 bas_type *ptr;
403 char *sizestr;
404 const char *plus;
405 char ptemp[256];
406 int can_inline;
409 if(inlineflag == 0) {
410 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
411 print_stat(1,&dl->decl);
414 else {
416 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
417 if(dl->decl.rel == REL_VECTOR){
418 f_print(fout,"\t int i;\n");
419 break;
422 size=0;can_inline=0;
423 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
424 if((dl->decl.prefix == NULL) && ((ptr=find_type(dl->decl.type))!= NULL) && ((dl->decl.rel == REL_ALIAS)||(dl->decl.rel == REL_VECTOR))){
426 if(dl->decl.rel == REL_ALIAS)
427 size+=ptr->length;
428 else {
429 can_inline=1;
430 break; /* can be inlined */
433 else {
434 if(size >= inlineflag){
435 can_inline=1;
436 break; /* can be inlined */
438 size=0;
440 if(size > inlineflag)
441 can_inline=1;
443 if(can_inline == 0){ /* can not inline, drop back to old mode */
444 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
445 print_stat(1,&dl->decl);
446 return;
452 flag=PUT;
453 for(j=0; j<2; j++){
455 if(flag == PUT)
456 f_print(fout,"\n\t if (xdrs->x_op == XDR_ENCODE) {\n");
457 else
458 f_print(fout,"\n \t return (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
461 i=0;
462 size=0;
463 sizestr=NULL;
464 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
466 /* now walk down the list and check for basic types */
467 if((dl->decl.prefix == NULL) && ((ptr=find_type(dl->decl.type))!= NULL) && ((dl->decl.rel == REL_ALIAS)||(dl->decl.rel == REL_VECTOR))){
468 if(i ==0 )
469 cur=dl;
470 i++;
472 if(dl->decl.rel == REL_ALIAS)
473 size+=ptr->length;
474 else {
475 /* this is required to handle arrays */
477 if(sizestr == NULL)
478 plus = " ";
479 else
480 plus = "+";
482 if(ptr->length != 1)
483 s_print(ptemp," %s %s * %d",plus,dl->decl.array_max,ptr->length);
484 else
485 s_print(ptemp," %s %s ",plus,dl->decl.array_max);
487 /*now concatenate to sizestr !!!! */
488 if (sizestr == NULL)
489 sizestr=strdup(ptemp);
490 else{
491 sizestr=realloc(sizestr,strlen(sizestr)+strlen(ptemp)+1);
492 if(sizestr == NULL){
494 f_print(stderr, "Fatal error : no memory \n");
495 crash();
497 sizestr=strcat(sizestr,ptemp); /*build up length of array */
503 else{
504 if(i > 0 )
505 if(sizestr == NULL && size < inlineflag){
506 /* don't expand into inline code if size < inlineflag */
507 while(cur != dl){
508 print_stat(1,&cur->decl);
509 cur=cur->next;
512 else{
516 /* were already looking at a xdr_inlineable structure */
517 if(sizestr == NULL)
518 f_print(fout,"\t buf = XDR_INLINE(xdrs,%d * BYTES_PER_XDR_UNIT);",
519 size);
520 else
521 if(size == 0)
522 f_print(fout,
523 "\t buf = XDR_INLINE(xdrs,%s * BYTES_PER_XDR_UNIT);",
524 sizestr);
525 else
526 f_print(fout,
527 "\t buf = XDR_INLINE(xdrs,(%d + %s)* BYTES_PER_XDR_UNIT);",
528 size,sizestr);
530 f_print(fout,"\n\t if (buf == NULL) {\n");
532 psav=cur;
533 while(cur != dl){
534 print_stat(2,&cur->decl);
535 cur=cur->next;
538 f_print(fout,"\n\t }\n\t else {\n");
540 cur=psav;
541 while(cur != dl){
542 emit_inline(&cur->decl,flag);
543 cur=cur->next;
546 f_print(fout,"\t }\n");
548 size=0;i=0;sizestr=NULL;
549 print_stat(1,&dl->decl);
553 if(i > 0 )
554 if(sizestr == NULL && size < inlineflag){
555 /* don't expand into inline code if size < inlineflag */
556 while(cur != dl){
557 print_stat(1,&cur->decl);
558 cur=cur->next;
561 else{
563 /* were already looking at a xdr_inlineable structure */
564 if(sizestr == NULL)
565 f_print(fout,"\t\tbuf = XDR_INLINE(xdrs,%d * BYTES_PER_XDR_UNIT);",
566 size);
567 else
568 if(size == 0)
569 f_print(fout,
570 "\t\tbuf = XDR_INLINE(xdrs,%s * BYTES_PER_XDR_UNIT);",
571 sizestr);
572 else
573 f_print(fout,
574 "\t\tbuf = XDR_INLINE(xdrs,(%d + %s)* BYTES_PER_XDR_UNIT);",
575 size,sizestr);
577 f_print(fout,"\n\t\tif (buf == NULL) {\n");
579 psav=cur;
580 while(cur != NULL){
581 print_stat(2,&cur->decl);
582 cur=cur->next;
584 f_print(fout,"\n\t }\n\t else {\n");
586 cur=psav;
587 while(cur != dl){
588 emit_inline(&cur->decl,flag);
589 cur=cur->next;
592 f_print(fout,"\t }\n");
595 flag=GET;
597 f_print(fout,"\t return(TRUE);\n\t}\n\n");
599 /* now take care of XDR_FREE case */
601 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
602 print_stat(1,&dl->decl);
607 static void
608 emit_typedef(definition *def)
610 const char *prefix = def->def.ty.old_prefix;
611 const char *type = def->def.ty.old_type;
612 const char *amax = def->def.ty.array_max;
613 relation rel = def->def.ty.rel;
616 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
619 static void
620 print_stat(int indent, declaration *dec)
622 const char *prefix = dec->prefix;
623 const char *type = dec->type;
624 const char *amax = dec->array_max;
625 relation rel = dec->rel;
626 char name[256];
628 if (isvectordef(type, rel)) {
629 s_print(name, "objp->%s", dec->name);
630 } else {
631 s_print(name, "&objp->%s", dec->name);
633 print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
637 void
638 emit_inline(declaration *decl, int flag)
641 /*check whether an array or not */
643 switch(decl->rel)
645 case REL_ALIAS :
646 emit_single_in_line(decl,flag,REL_ALIAS);
647 break;
648 case REL_VECTOR :
649 f_print(fout,"\t\t{ register %s *genp; \n",decl->type);
650 f_print(fout,"\t\t for ( i = 0,genp=objp->%s;\n \t\t\ti < %s; i++){\n\t\t",
651 decl->name,decl->array_max);
652 emit_single_in_line(decl,flag,REL_VECTOR);
653 f_print(fout,"\t\t }\n\t\t };\n");
655 default:
656 /* ?... do nothing I guess */
660 void
661 emit_single_in_line(declaration *decl, int flag, relation rel)
663 char *upp_case1;
664 const char *upp_case;
666 if (flag == PUT) {
667 f_print(fout,"\t\t IXDR_PUT_");
669 else {
670 if(rel== REL_ALIAS) {
671 f_print(fout, "\t\t objp->%s = IXDR_GET_", decl->name);
673 else {
674 f_print(fout,"\t\t *genp++ = IXDR_GET_");
678 upp_case1 = upcase(decl->type);
679 upp_case = upp_case1;
681 /* hack - XX */
682 if (!strcmp(upp_case, "INT")) upp_case="LONG";
683 if (!strcmp(upp_case, "U_INT")) upp_case="U_LONG";
685 if (flag == PUT) {
686 if (rel==REL_ALIAS) {
687 f_print(fout,"%s(buf,objp->%s);\n",upp_case,decl->name);
689 else {
690 f_print(fout,"%s(buf,*genp++);\n",upp_case);
693 else {
694 f_print(fout,"%s(buf);\n",upp_case);
697 free(upp_case1);
701 static char *upcase(const char *str) {
702 char *ptr, *hptr;
703 ptr = malloc(strlen(str));
704 if (ptr == NULL) {
705 f_print(stderr,"malloc failed\n");
706 exit(1);
708 hptr=ptr;
709 while (*str != 0) {
710 *ptr++ = toupper(*str++);
712 *ptr=0;
713 return hptr;