Update.
[glibc.git] / sunrpc / rpc_cout.c
blob44027ea39ef733d57b3d49267086f478563531f7
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 (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 void emit_inline (int indent, declaration * decl, int flag);
53 static void emit_single_in_line (int indent, declaration *decl, int flag,
54 relation rel);
55 static int findtype (const definition * def, const char *type);
56 static int undefined (const char *type);
57 static void print_generic_header (const char *procname, int pointerp);
58 static void print_ifopen (int indent, const char *name);
59 static void print_ifarg (const char *arg);
60 static void print_ifsizeof (int indent, const char *prefix, const char *type);
61 static void print_ifclose (int indent);
62 static void print_ifstat (int indent, const char *prefix, const char *type,
63 relation rel, const char *amax,
64 const char *objname, const char *name);
65 static void print_stat (int indent, const declaration * dec);
66 static void print_header (const definition * def);
67 static void print_trailer (void);
68 static char *upcase (const char *str);
71 * Emit the C-routine for the given definition
73 void
74 emit (definition * def)
76 if (def->def_kind == DEF_CONST)
78 return;
80 if (def->def_kind == DEF_PROGRAM)
82 emit_program (def);
83 return;
85 if (def->def_kind == DEF_TYPEDEF)
87 /* now we need to handle declarations like
88 struct typedef foo foo;
89 since we don't want this to be expanded
90 into 2 calls to xdr_foo */
92 if (strcmp (def->def.ty.old_type, def->def_name) == 0)
93 return;
96 print_header (def);
97 switch (def->def_kind)
99 case DEF_UNION:
100 emit_union (def);
101 break;
102 case DEF_ENUM:
103 emit_enum (def);
104 break;
105 case DEF_STRUCT:
106 emit_struct (def);
107 break;
108 case DEF_TYPEDEF:
109 emit_typedef (def);
110 break;
111 default:
112 /* can't happen */
113 break;
115 print_trailer ();
118 static int
119 findtype (const definition * def, const char *type)
121 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST)
123 return 0;
125 else
127 return (streq (def->def_name, type));
131 static int
132 undefined (const char *type)
134 definition *def;
135 def = (definition *) FINDVAL (defined, type, findtype);
136 return (def == NULL);
140 static void
141 print_generic_header (const char *procname, int pointerp)
143 f_print (fout, "\n");
144 f_print (fout, "bool_t\n");
145 if (Cflag)
147 f_print (fout, "xdr_%s (", procname);
148 f_print (fout, "XDR *xdrs, ");
149 f_print (fout, "%s ", procname);
150 if (pointerp)
151 f_print (fout, "*");
152 f_print (fout, "objp)\n{\n");
154 else
156 f_print (fout, "xdr_%s (xdrs, objp)\n", procname);
157 f_print (fout, "\tXDR *xdrs;\n");
158 f_print (fout, "\t%s ", procname);
159 if (pointerp)
160 f_print (fout, "*");
161 f_print (fout, "objp;\n{\n");
165 static void
166 print_header (const definition * def)
168 print_generic_header (def->def_name,
169 def->def_kind != DEF_TYPEDEF ||
170 !isvectordef (def->def.ty.old_type,
171 def->def.ty.rel));
173 /* Now add Inline support */
175 if (inlineflag == 0)
176 return;
177 /*May cause lint to complain. but ... */
178 f_print (fout, "\tregister int32_t *buf;\n\n");
181 static void
182 print_prog_header (const proc_list * plist)
184 print_generic_header (plist->args.argname, 1);
187 static void
188 print_trailer (void)
190 f_print (fout, "\treturn TRUE;\n");
191 f_print (fout, "}\n");
195 static void
196 print_ifopen (int indent, const char *name)
198 tabify (fout, indent);
199 f_print (fout, " if (!xdr_%s (xdrs", name);
202 static void
203 print_ifarg (const char *arg)
205 f_print (fout, ", %s", arg);
208 static void
209 print_ifsizeof (int indent, const char *prefix, const char *type)
211 if (indent)
213 fprintf (fout, ",\n");
214 tabify (fout, indent);
216 else
217 fprintf (fout, ", ");
219 if (streq (type, "bool"))
220 fprintf (fout, "sizeof (bool_t), (xdrproc_t) xdr_bool");
221 else
223 fprintf (fout, "sizeof (");
224 if (undefined (type) && prefix)
226 f_print (fout, "%s ", prefix);
228 fprintf (fout, "%s), (xdrproc_t) xdr_%s", type, type);
232 static void
233 print_ifclose (int indent)
235 f_print (fout, "))\n");
236 tabify (fout, indent);
237 f_print (fout, "\t return FALSE;\n");
240 static void
241 print_ifstat (int indent, const char *prefix, const char *type, relation rel,
242 const char *amax, const char *objname, const char *name)
244 const char *alt = NULL;
246 switch (rel)
248 case REL_POINTER:
249 print_ifopen (indent, "pointer");
250 print_ifarg ("(char **)");
251 f_print (fout, "%s", objname);
252 print_ifsizeof (0, prefix, type);
253 break;
254 case REL_VECTOR:
255 if (streq (type, "string"))
257 alt = "string";
259 else if (streq (type, "opaque"))
261 alt = "opaque";
263 if (alt)
265 print_ifopen (indent, alt);
266 print_ifarg (objname);
268 else
270 print_ifopen (indent, "vector");
271 print_ifarg ("(char *)");
272 f_print (fout, "%s", objname);
274 print_ifarg (amax);
275 if (!alt)
277 print_ifsizeof (indent + 1, prefix, type);
279 break;
280 case REL_ARRAY:
281 if (streq (type, "string"))
283 alt = "string";
285 else if (streq (type, "opaque"))
287 alt = "bytes";
289 if (streq (type, "string"))
291 print_ifopen (indent, alt);
292 print_ifarg (objname);
294 else
296 if (alt)
298 print_ifopen (indent, alt);
300 else
302 print_ifopen (indent, "array");
304 print_ifarg ("(char **)");
305 if (*objname == '&')
307 f_print (fout, "%s.%s_val, (u_int *) %s.%s_len",
308 objname, name, objname, name);
310 else
312 f_print (fout, "&%s->%s_val, (u_int *) &%s->%s_len",
313 objname, name, objname, name);
316 print_ifarg (amax);
317 if (!alt)
319 print_ifsizeof (indent + 1, prefix, type);
321 break;
322 case REL_ALIAS:
323 print_ifopen (indent, type);
324 print_ifarg (objname);
325 break;
327 print_ifclose (indent);
330 static void
331 emit_enum (definition * def)
333 (void) def;
335 print_ifopen (1, "enum");
336 print_ifarg ("(enum_t *) objp");
337 print_ifclose (1);
340 static void
341 emit_program (const definition * def)
343 decl_list *dl;
344 version_list *vlist;
345 proc_list *plist;
347 for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
348 for (plist = vlist->procs; plist != NULL; plist = plist->next)
350 if (!newstyle || plist->arg_num < 2)
351 continue; /* old style, or single argument */
352 print_prog_header (plist);
353 for (dl = plist->args.decls; dl != NULL;
354 dl = dl->next)
355 print_stat (1, &dl->decl);
356 print_trailer ();
360 static void
361 emit_union (const definition * def)
363 declaration *dflt;
364 case_list *cl;
365 declaration *cs;
366 char *object;
367 const char *vecformat = "objp->%s_u.%s";
368 const char *format = "&objp->%s_u.%s";
370 print_stat (1, &def->def.un.enum_decl);
371 f_print (fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
372 for (cl = def->def.un.cases; cl != NULL; cl = cl->next)
375 f_print (fout, "\tcase %s:\n", cl->case_name);
376 if (cl->contflag == 1) /* a continued case statement */
377 continue;
378 cs = &cl->case_decl;
379 if (!streq (cs->type, "void"))
381 object = alloc (strlen (def->def_name) + strlen (format) +
382 strlen (cs->name) + 1);
383 if (isvectordef (cs->type, cs->rel))
385 s_print (object, vecformat, def->def_name,
386 cs->name);
388 else
390 s_print (object, format, def->def_name,
391 cs->name);
393 print_ifstat (2, cs->prefix, cs->type, cs->rel, cs->array_max,
394 object, cs->name);
395 free (object);
397 f_print (fout, "\t\tbreak;\n");
399 dflt = def->def.un.default_decl;
400 if (dflt != NULL)
402 if (!streq (dflt->type, "void"))
404 f_print (fout, "\tdefault:\n");
405 object = alloc (strlen (def->def_name) + strlen (format) +
406 strlen (dflt->name) + 1);
407 if (isvectordef (dflt->type, dflt->rel))
409 s_print (object, vecformat, def->def_name,
410 dflt->name);
412 else
414 s_print (object, format, def->def_name,
415 dflt->name);
418 print_ifstat (2, dflt->prefix, dflt->type, dflt->rel,
419 dflt->array_max, object, dflt->name);
420 free (object);
421 f_print (fout, "\t\tbreak;\n");
423 #ifdef __GNU_LIBRARY__
424 else
426 f_print (fout, "\tdefault:\n");
427 f_print (fout, "\t\tbreak;\n");
429 #endif
431 else
433 f_print (fout, "\tdefault:\n");
434 f_print (fout, "\t\treturn FALSE;\n");
437 f_print (fout, "\t}\n");
440 static void
441 inline_struct (definition *def, int flag)
443 decl_list *dl;
444 int i, size;
445 decl_list *cur = NULL;
446 decl_list *psav;
447 bas_type *ptr;
448 char *sizestr;
449 const char *plus;
450 char ptemp[256];
451 int indent = 1;
453 if (flag == PUT)
454 f_print (fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
455 else
456 f_print (fout,
457 "\t\treturn TRUE;\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
459 i = 0;
460 size = 0;
461 sizestr = NULL;
462 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
463 { /* xxx */
464 /* now walk down the list and check for basic types */
465 if ((dl->decl.prefix == NULL) &&
466 ((ptr = find_type (dl->decl.type)) != NULL) &&
467 ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR)))
469 if (i == 0)
470 cur = dl;
471 ++i;
473 if (dl->decl.rel == REL_ALIAS)
474 size += ptr->length;
475 else
477 /* this is required to handle arrays */
478 if (sizestr == NULL)
479 plus = "";
480 else
481 plus = "+ ";
483 if (ptr->length != 1)
484 s_print (ptemp, " %s %s * %d", plus, dl->decl.array_max,
485 ptr->length);
486 else
487 s_print (ptemp, " %s%s ", plus, dl->decl.array_max);
489 /*now concatenate to sizestr !!!! */
490 if (sizestr == NULL)
491 sizestr = strdup (ptemp);
492 else
494 sizestr = realloc (sizestr, strlen (sizestr) +
495 strlen (ptemp) + 1);
496 if (sizestr == NULL)
498 f_print (stderr, "Fatal error : no memory \n");
499 crash ();
501 sizestr = strcat (sizestr, ptemp);
502 /*build up length of array */
506 else
508 if (i > 0)
510 if (sizestr == NULL && size < inlineflag)
512 /* don't expand into inline code if size < inlineflag */
513 while (cur != dl)
515 print_stat (indent + 1, &cur->decl);
516 cur = cur->next;
519 else
521 /* were already looking at a xdr_inlineable structure */
522 tabify (fout, indent + 1);
523 if (sizestr == NULL)
524 f_print (fout, "buf = XDR_INLINE (xdrs, %d * BYTES_PER_XDR_UNIT);", size);
525 else if (size == 0)
526 f_print (fout,
527 "buf = XDR_INLINE (xdrs, (%s) * BYTES_PER_XDR_UNIT);",
528 sizestr);
529 else
530 f_print (fout,
531 "buf = XDR_INLINE (xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
532 size, sizestr);
533 f_print (fout, "\n");
534 tabify (fout, indent + 1);
535 fprintf (fout, "if (buf == NULL) {\n");
536 psav = cur;
537 while (cur != dl)
539 print_stat (indent + 2, &cur->decl);
540 cur = cur->next;
543 f_print (fout, "\n\t\t} else {\n");
544 cur = psav;
545 while (cur != dl)
547 emit_inline (indent + 1, &cur->decl, flag);
548 cur = cur->next;
550 tabify (fout, indent + 1);
551 f_print (fout, "}\n");
554 size = 0;
555 i = 0;
556 sizestr = NULL;
557 print_stat (indent + 1, &dl->decl);
560 if (i > 0)
562 if (sizestr == NULL && size < inlineflag)
564 /* don't expand into inline code if size < inlineflag */
565 while (cur != dl)
567 print_stat (indent + 1, &cur->decl);
568 cur = cur->next;
571 else
573 /* were already looking at a xdr_inlineable structure */
574 if (sizestr == NULL)
575 f_print (fout,
576 "\t\tbuf = XDR_INLINE (xdrs, %d * BYTES_PER_XDR_UNIT);",
577 size);
578 else if (size == 0)
579 f_print (fout,
580 "\t\tbuf = XDR_INLINE (xdrs, (%s) * BYTES_PER_XDR_UNIT);",
581 sizestr);
582 else
583 f_print (fout,
584 "\t\tbuf = XDR_INLINE (xdrs, (%d + %s)* BYTES_PER_XDR_UNIT);",
585 size, sizestr);
586 f_print (fout, "\n\t\tif (buf == NULL) {\n");
587 psav = cur;
588 while (cur != NULL)
590 print_stat (indent + 2, &cur->decl);
591 cur = cur->next;
593 f_print (fout, "\t\t} else {\n");
595 cur = psav;
596 while (cur != dl)
598 emit_inline (indent + 2, &cur->decl, flag);
599 cur = cur->next;
601 f_print (fout, "\t\t}\n");
606 /* this may be const. i haven't traced this one through yet. */
608 static void
609 emit_struct (definition * def)
611 decl_list *dl;
612 int j, size, flag;
613 bas_type *ptr;
614 int can_inline;
617 if (inlineflag == 0)
619 /* No xdr_inlining at all */
620 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
621 print_stat (1, &dl->decl);
622 return;
625 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
626 if (dl->decl.rel == REL_VECTOR)
628 f_print (fout, "\tint i;\n");
629 break;
632 size = 0;
633 can_inline = 0;
635 * Make a first pass and see if inling is possible.
637 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
638 if ((dl->decl.prefix == NULL) &&
639 ((ptr = find_type (dl->decl.type)) != NULL) &&
640 ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR)))
642 if (dl->decl.rel == REL_ALIAS)
643 size += ptr->length;
644 else
646 can_inline = 1;
647 break; /* can be inlined */
650 else
652 if (size >= inlineflag)
654 can_inline = 1;
655 break; /* can be inlined */
657 size = 0;
659 if (size > inlineflag)
660 can_inline = 1;
662 if (can_inline == 0)
663 { /* can not inline, drop back to old mode */
664 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
665 print_stat (1, &dl->decl);
666 return;
669 flag = PUT;
670 for (j = 0; j < 2; j++)
672 inline_struct (def, flag);
673 if (flag == PUT)
674 flag = GET;
677 f_print (fout, "\t return TRUE;\n\t}\n\n");
679 /* now take care of XDR_FREE case */
681 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
682 print_stat (1, &dl->decl);
685 static void
686 emit_typedef (const definition * def)
688 const char *prefix = def->def.ty.old_prefix;
689 const char *type = def->def.ty.old_type;
690 const char *amax = def->def.ty.array_max;
691 relation rel = def->def.ty.rel;
693 print_ifstat (1, prefix, type, rel, amax, "objp", def->def_name);
696 static void
697 print_stat (int indent, const declaration * dec)
699 const char *prefix = dec->prefix;
700 const char *type = dec->type;
701 const char *amax = dec->array_max;
702 relation rel = dec->rel;
703 char name[256];
705 if (isvectordef (type, rel))
707 s_print (name, "objp->%s", dec->name);
709 else
711 s_print (name, "&objp->%s", dec->name);
713 print_ifstat (indent, prefix, type, rel, amax, name, dec->name);
717 static void
718 emit_inline (int indent, declaration * decl, int flag)
720 switch (decl->rel)
722 case REL_ALIAS:
723 emit_single_in_line (indent, decl, flag, REL_ALIAS);
724 break;
725 case REL_VECTOR:
726 tabify (fout, indent);
727 f_print (fout, "{\n");
728 tabify (fout, indent + 1);
729 f_print (fout, "register %s *genp;\n\n", decl->type);
730 tabify (fout, indent + 1);
731 f_print (fout,
732 "for (i = 0, genp = objp->%s;\n", decl->name);
733 tabify (fout, indent + 2);
734 f_print (fout, "i < %s; ++i) {\n", decl->array_max);
735 emit_single_in_line (indent + 2, decl, flag, REL_VECTOR);
736 tabify (fout, indent + 1);
737 f_print (fout, "}\n");
738 tabify (fout, indent);
739 f_print (fout, "}\n");
740 break;
741 default:
742 break;
743 /* ?... do nothing I guess */
747 static void
748 emit_single_in_line (int indent, declaration *decl, int flag, relation rel)
750 char *upp_case;
751 int freed = 0;
753 tabify (fout, indent);
754 if (flag == PUT)
755 f_print (fout, "IXDR_PUT_");
756 else
758 if (rel == REL_ALIAS)
759 f_print (fout, "objp->%s = IXDR_GET_", decl->name);
760 else
761 f_print (fout, "*genp++ = IXDR_GET_");
764 upp_case = upcase (decl->type);
766 /* hack - XX */
767 if (!strcmp (upp_case, "INT"))
769 free (upp_case);
770 freed = 1;
771 /* Casting is safe since the `freed' flag is set. */
772 upp_case = (char *) "LONG";
775 if (!strcmp (upp_case, "U_INT"))
777 free (upp_case);
778 freed = 1;
779 /* Casting is safe since the `freed' flag is set. */
780 upp_case = (char *) "U_LONG";
783 if (flag == PUT)
785 if (rel == REL_ALIAS)
786 f_print (fout, "%s(buf, objp->%s);\n", upp_case, decl->name);
787 else
788 f_print (fout, "%s(buf, *genp++);\n", upp_case);
790 else
792 f_print (fout, "%s(buf);\n", upp_case);
795 if (!freed)
796 free (upp_case);
800 static char *
801 upcase (const char *str)
803 char *ptr, *hptr;
804 ptr = malloc (strlen (str));
805 if (ptr == NULL)
807 f_print (stderr, "malloc failed\n");
808 exit (1);
810 hptr = ptr;
811 while (*str != '\0')
812 *ptr++ = toupper (*str++);
814 *ptr = '\0';
815 return hptr;