Revert:
[glibc.git] / sunrpc / rpc_cout.c
blobdfc57fd277b8b61de4e55df466e3de190a6a7aae
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
36 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
38 #include <ctype.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include "rpc_parse.h"
42 #include "rpc_util.h"
43 #include "proto.h"
45 static void emit_enum (definition * def);
46 static void emit_program (const definition * def);
47 static void emit_union (const definition * def);
48 static void emit_struct (definition * def);
49 static void emit_typedef (const definition * def);
50 static void emit_inline (int indent, declaration * decl, int flag);
51 static void emit_single_in_line (int indent, declaration *decl, int flag,
52 relation rel);
53 static int findtype (const definition * def, const char *type);
54 static int undefined (const char *type);
55 static void print_generic_header (const char *procname, int pointerp);
56 static void print_ifopen (int indent, const char *name);
57 static void print_ifarg (const char *arg);
58 static void print_ifsizeof (int indent, const char *prefix, const char *type);
59 static void print_ifclose (int indent);
60 static void print_ifstat (int indent, const char *prefix, const char *type,
61 relation rel, const char *amax,
62 const char *objname, const char *name);
63 static void print_stat (int indent, const declaration * dec);
64 static void print_header (const definition * def);
65 static void print_trailer (void);
66 static char *upcase (const char *str);
69 * Emit the C-routine for the given definition
71 void
72 emit (definition * def)
74 if (def->def_kind == DEF_CONST)
76 return;
78 if (def->def_kind == DEF_PROGRAM)
80 emit_program (def);
81 return;
83 if (def->def_kind == DEF_TYPEDEF)
85 /* now we need to handle declarations like
86 struct typedef foo foo;
87 since we don't want this to be expanded
88 into 2 calls to xdr_foo */
90 if (strcmp (def->def.ty.old_type, def->def_name) == 0)
91 return;
94 print_header (def);
95 switch (def->def_kind)
97 case DEF_UNION:
98 emit_union (def);
99 break;
100 case DEF_ENUM:
101 emit_enum (def);
102 break;
103 case DEF_STRUCT:
104 emit_struct (def);
105 break;
106 case DEF_TYPEDEF:
107 emit_typedef (def);
108 break;
109 default:
110 /* can't happen */
111 break;
113 print_trailer ();
116 static int
117 findtype (const definition * def, const char *type)
119 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST)
121 return 0;
123 else
125 return (streq (def->def_name, type));
129 static int
130 undefined (const char *type)
132 definition *def;
133 def = (definition *) FINDVAL (defined, type, findtype);
134 return (def == NULL);
138 static void
139 print_generic_header (const char *procname, int pointerp)
141 f_print (fout, "\n");
142 f_print (fout, "bool_t\n");
143 if (Cflag)
145 f_print (fout, "xdr_%s (", procname);
146 f_print (fout, "XDR *xdrs, ");
147 f_print (fout, "%s ", procname);
148 if (pointerp)
149 f_print (fout, "*");
150 f_print (fout, "objp)\n{\n");
152 else
154 f_print (fout, "xdr_%s (xdrs, objp)\n", procname);
155 f_print (fout, "\tXDR *xdrs;\n");
156 f_print (fout, "\t%s ", procname);
157 if (pointerp)
158 f_print (fout, "*");
159 f_print (fout, "objp;\n{\n");
163 static void
164 print_header (const definition * def)
166 print_generic_header (def->def_name,
167 def->def_kind != DEF_TYPEDEF ||
168 !isvectordef (def->def.ty.old_type,
169 def->def.ty.rel));
171 /* Now add Inline support */
173 if (inlineflag == 0)
174 return;
175 /*May cause lint to complain. but ... */
176 f_print (fout, "\tregister int32_t *buf;\n\n");
179 static void
180 print_prog_header (const 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 (int indent, const char *prefix, const char *type)
209 if (indent)
211 fprintf (fout, ",\n");
212 tabify (fout, indent);
214 else
215 fprintf (fout, ", ");
217 if (streq (type, "bool"))
218 fprintf (fout, "sizeof (bool_t), (xdrproc_t) xdr_bool");
219 else
221 fprintf (fout, "sizeof (");
222 if (undefined (type) && prefix)
224 f_print (fout, "%s ", prefix);
226 fprintf (fout, "%s), (xdrproc_t) xdr_%s", type, type);
230 static void
231 print_ifclose (int indent)
233 f_print (fout, "))\n");
234 tabify (fout, indent);
235 f_print (fout, "\t return FALSE;\n");
238 static void
239 print_ifstat (int indent, const char *prefix, const char *type, relation rel,
240 const char *amax, const char *objname, const char *name)
242 const char *alt = NULL;
244 switch (rel)
246 case REL_POINTER:
247 print_ifopen (indent, "pointer");
248 print_ifarg ("(char **)");
249 f_print (fout, "%s", objname);
250 print_ifsizeof (0, prefix, type);
251 break;
252 case REL_VECTOR:
253 if (streq (type, "string"))
255 alt = "string";
257 else if (streq (type, "opaque"))
259 alt = "opaque";
261 if (alt)
263 print_ifopen (indent, alt);
264 print_ifarg (objname);
266 else
268 print_ifopen (indent, "vector");
269 print_ifarg ("(char *)");
270 f_print (fout, "%s", objname);
272 print_ifarg (amax);
273 if (!alt)
275 print_ifsizeof (indent + 1, prefix, type);
277 break;
278 case REL_ARRAY:
279 if (streq (type, "string"))
281 alt = "string";
283 else if (streq (type, "opaque"))
285 alt = "bytes";
287 if (streq (type, "string"))
289 print_ifopen (indent, alt);
290 print_ifarg (objname);
292 else
294 if (alt)
296 print_ifopen (indent, alt);
298 else
300 print_ifopen (indent, "array");
302 print_ifarg ("(char **)");
303 if (*objname == '&')
305 f_print (fout, "%s.%s_val, (u_int *) %s.%s_len",
306 objname, name, objname, name);
308 else
310 f_print (fout, "&%s->%s_val, (u_int *) &%s->%s_len",
311 objname, name, objname, name);
314 print_ifarg (amax);
315 if (!alt)
317 print_ifsizeof (indent + 1, prefix, type);
319 break;
320 case REL_ALIAS:
321 print_ifopen (indent, type);
322 print_ifarg (objname);
323 break;
325 print_ifclose (indent);
328 static void
329 emit_enum (definition * def)
331 (void) def;
333 print_ifopen (1, "enum");
334 print_ifarg ("(enum_t *) objp");
335 print_ifclose (1);
338 static void
339 emit_program (const definition * def)
341 decl_list *dl;
342 version_list *vlist;
343 proc_list *plist;
345 for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
346 for (plist = vlist->procs; plist != NULL; plist = plist->next)
348 if (!newstyle || plist->arg_num < 2)
349 continue; /* old style, or single argument */
350 print_prog_header (plist);
351 for (dl = plist->args.decls; dl != NULL;
352 dl = dl->next)
353 print_stat (1, &dl->decl);
354 print_trailer ();
358 static void
359 emit_union (const definition * def)
361 declaration *dflt;
362 case_list *cl;
363 declaration *cs;
364 char *object;
365 const char *vecformat = "objp->%s_u.%s";
366 const char *format = "&objp->%s_u.%s";
368 print_stat (1, &def->def.un.enum_decl);
369 f_print (fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
370 for (cl = def->def.un.cases; cl != NULL; cl = cl->next)
373 f_print (fout, "\tcase %s:\n", cl->case_name);
374 if (cl->contflag == 1) /* a continued case statement */
375 continue;
376 cs = &cl->case_decl;
377 if (!streq (cs->type, "void"))
379 object = alloc (strlen (def->def_name) + strlen (format) +
380 strlen (cs->name) + 1);
381 if (isvectordef (cs->type, cs->rel))
383 s_print (object, vecformat, def->def_name,
384 cs->name);
386 else
388 s_print (object, format, def->def_name,
389 cs->name);
391 print_ifstat (2, cs->prefix, cs->type, cs->rel, cs->array_max,
392 object, cs->name);
393 free (object);
395 f_print (fout, "\t\tbreak;\n");
397 dflt = def->def.un.default_decl;
398 if (dflt != NULL)
400 if (!streq (dflt->type, "void"))
402 f_print (fout, "\tdefault:\n");
403 object = alloc (strlen (def->def_name) + strlen (format) +
404 strlen (dflt->name) + 1);
405 if (isvectordef (dflt->type, dflt->rel))
407 s_print (object, vecformat, def->def_name,
408 dflt->name);
410 else
412 s_print (object, format, def->def_name,
413 dflt->name);
416 print_ifstat (2, dflt->prefix, dflt->type, dflt->rel,
417 dflt->array_max, object, dflt->name);
418 free (object);
419 f_print (fout, "\t\tbreak;\n");
421 #ifdef __GNU_LIBRARY__
422 else
424 f_print (fout, "\tdefault:\n");
425 f_print (fout, "\t\tbreak;\n");
427 #endif
429 else
431 f_print (fout, "\tdefault:\n");
432 f_print (fout, "\t\treturn FALSE;\n");
435 f_print (fout, "\t}\n");
438 static void
439 inline_struct (definition *def, int flag)
441 decl_list *dl;
442 int i, size;
443 decl_list *cur = NULL;
444 decl_list *psav;
445 bas_type *ptr;
446 char *sizestr;
447 const char *plus;
448 char ptemp[256];
449 int indent = 1;
451 if (flag == PUT)
452 f_print (fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
453 else
454 f_print (fout,
455 "\t\treturn TRUE;\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
457 i = 0;
458 size = 0;
459 sizestr = NULL;
460 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
461 { /* xxx */
462 /* now walk down the list and check for basic types */
463 if ((dl->decl.prefix == NULL) &&
464 ((ptr = find_type (dl->decl.type)) != NULL) &&
465 ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR)))
467 if (i == 0)
468 cur = dl;
469 ++i;
471 if (dl->decl.rel == REL_ALIAS)
472 size += ptr->length;
473 else
475 /* this is required to handle arrays */
476 if (sizestr == NULL)
477 plus = "";
478 else
479 plus = "+ ";
481 if (ptr->length != 1)
482 s_print (ptemp, " %s %s * %d", plus, dl->decl.array_max,
483 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
492 sizestr = realloc (sizestr, strlen (sizestr) +
493 strlen (ptemp) + 1);
494 if (sizestr == NULL)
496 f_print (stderr, "Fatal error : no memory \n");
497 crash ();
499 sizestr = strcat (sizestr, ptemp);
500 /*build up length of array */
504 else
506 if (i > 0)
508 if (sizestr == NULL && size < inlineflag)
510 /* don't expand into inline code if size < inlineflag */
511 while (cur != dl)
513 print_stat (indent + 1, &cur->decl);
514 cur = cur->next;
517 else
519 /* were already looking at a xdr_inlineable structure */
520 tabify (fout, indent + 1);
521 if (sizestr == NULL)
522 f_print (fout, "buf = XDR_INLINE (xdrs, %d * BYTES_PER_XDR_UNIT);", size);
523 else if (size == 0)
524 f_print (fout,
525 "buf = XDR_INLINE (xdrs, (%s) * BYTES_PER_XDR_UNIT);",
526 sizestr);
527 else
528 f_print (fout,
529 "buf = XDR_INLINE (xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
530 size, sizestr);
531 f_print (fout, "\n");
532 tabify (fout, indent + 1);
533 fprintf (fout, "if (buf == NULL) {\n");
534 psav = cur;
535 while (cur != dl)
537 print_stat (indent + 2, &cur->decl);
538 cur = cur->next;
541 f_print (fout, "\n\t\t} else {\n");
542 cur = psav;
543 while (cur != dl)
545 emit_inline (indent + 1, &cur->decl, flag);
546 cur = cur->next;
548 tabify (fout, indent + 1);
549 f_print (fout, "}\n");
552 size = 0;
553 i = 0;
554 sizestr = NULL;
555 print_stat (indent + 1, &dl->decl);
558 if (i > 0)
560 if (sizestr == NULL && size < inlineflag)
562 /* don't expand into inline code if size < inlineflag */
563 while (cur != dl)
565 print_stat (indent + 1, &cur->decl);
566 cur = cur->next;
569 else
571 /* were already looking at a xdr_inlineable structure */
572 if (sizestr == NULL)
573 f_print (fout,
574 "\t\tbuf = XDR_INLINE (xdrs, %d * BYTES_PER_XDR_UNIT);",
575 size);
576 else if (size == 0)
577 f_print (fout,
578 "\t\tbuf = XDR_INLINE (xdrs, (%s) * BYTES_PER_XDR_UNIT);",
579 sizestr);
580 else
581 f_print (fout,
582 "\t\tbuf = XDR_INLINE (xdrs, (%d + %s)* BYTES_PER_XDR_UNIT);",
583 size, sizestr);
584 f_print (fout, "\n\t\tif (buf == NULL) {\n");
585 psav = cur;
586 while (cur != NULL)
588 print_stat (indent + 2, &cur->decl);
589 cur = cur->next;
591 f_print (fout, "\t\t} else {\n");
593 cur = psav;
594 while (cur != dl)
596 emit_inline (indent + 2, &cur->decl, flag);
597 cur = cur->next;
599 f_print (fout, "\t\t}\n");
604 /* this may be const. i haven't traced this one through yet. */
606 static void
607 emit_struct (definition * def)
609 decl_list *dl;
610 int j, size, flag;
611 bas_type *ptr;
612 int can_inline;
615 if (inlineflag == 0)
617 /* No xdr_inlining at all */
618 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
619 print_stat (1, &dl->decl);
620 return;
623 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
624 if (dl->decl.rel == REL_VECTOR)
626 f_print (fout, "\tint i;\n");
627 break;
630 size = 0;
631 can_inline = 0;
633 * Make a first pass and see if inling is possible.
635 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
636 if ((dl->decl.prefix == NULL) &&
637 ((ptr = find_type (dl->decl.type)) != NULL) &&
638 ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR)))
640 if (dl->decl.rel == REL_ALIAS)
641 size += ptr->length;
642 else
644 can_inline = 1;
645 break; /* can be inlined */
648 else
650 if (size >= inlineflag)
652 can_inline = 1;
653 break; /* can be inlined */
655 size = 0;
657 if (size > inlineflag)
658 can_inline = 1;
660 if (can_inline == 0)
661 { /* can not inline, drop back to old mode */
662 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
663 print_stat (1, &dl->decl);
664 return;
667 flag = PUT;
668 for (j = 0; j < 2; j++)
670 inline_struct (def, flag);
671 if (flag == PUT)
672 flag = GET;
675 f_print (fout, "\t return TRUE;\n\t}\n\n");
677 /* now take care of XDR_FREE case */
679 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
680 print_stat (1, &dl->decl);
683 static void
684 emit_typedef (const definition * def)
686 const char *prefix = def->def.ty.old_prefix;
687 const char *type = def->def.ty.old_type;
688 const char *amax = def->def.ty.array_max;
689 relation rel = def->def.ty.rel;
691 print_ifstat (1, prefix, type, rel, amax, "objp", def->def_name);
694 static void
695 print_stat (int indent, const declaration * dec)
697 const char *prefix = dec->prefix;
698 const char *type = dec->type;
699 const char *amax = dec->array_max;
700 relation rel = dec->rel;
701 char name[256];
703 if (isvectordef (type, rel))
705 s_print (name, "objp->%s", dec->name);
707 else
709 s_print (name, "&objp->%s", dec->name);
711 print_ifstat (indent, prefix, type, rel, amax, name, dec->name);
715 static void
716 emit_inline (int indent, declaration * decl, int flag)
718 switch (decl->rel)
720 case REL_ALIAS:
721 emit_single_in_line (indent, decl, flag, REL_ALIAS);
722 break;
723 case REL_VECTOR:
724 tabify (fout, indent);
725 f_print (fout, "{\n");
726 tabify (fout, indent + 1);
727 f_print (fout, "register %s *genp;\n\n", decl->type);
728 tabify (fout, indent + 1);
729 f_print (fout,
730 "for (i = 0, genp = objp->%s;\n", decl->name);
731 tabify (fout, indent + 2);
732 f_print (fout, "i < %s; ++i) {\n", decl->array_max);
733 emit_single_in_line (indent + 2, decl, flag, REL_VECTOR);
734 tabify (fout, indent + 1);
735 f_print (fout, "}\n");
736 tabify (fout, indent);
737 f_print (fout, "}\n");
738 break;
739 default:
740 break;
741 /* ?... do nothing I guess */
745 static void
746 emit_single_in_line (int indent, declaration *decl, int flag, relation rel)
748 char *upp_case;
749 int freed = 0;
751 tabify (fout, indent);
752 if (flag == PUT)
753 f_print (fout, "IXDR_PUT_");
754 else
756 if (rel == REL_ALIAS)
757 f_print (fout, "objp->%s = IXDR_GET_", decl->name);
758 else
759 f_print (fout, "*genp++ = IXDR_GET_");
762 upp_case = upcase (decl->type);
764 /* hack - XX */
765 if (!strcmp (upp_case, "INT"))
767 free (upp_case);
768 freed = 1;
769 /* Casting is safe since the `freed' flag is set. */
770 upp_case = (char *) "LONG";
773 if (!strcmp (upp_case, "U_INT"))
775 free (upp_case);
776 freed = 1;
777 /* Casting is safe since the `freed' flag is set. */
778 upp_case = (char *) "U_LONG";
781 if (flag == PUT)
783 if (rel == REL_ALIAS)
784 f_print (fout, "%s(buf, objp->%s);\n", upp_case, decl->name);
785 else
786 f_print (fout, "%s(buf, *genp++);\n", upp_case);
788 else
790 f_print (fout, "%s(buf);\n", upp_case);
793 if (!freed)
794 free (upp_case);
798 static char *
799 upcase (const char *str)
801 char *ptr, *hptr;
802 ptr = malloc (strlen (str) + 1);
803 if (ptr == NULL)
805 f_print (stderr, "malloc failed\n");
806 exit (1);
808 hptr = ptr;
809 while (*str != '\0')
810 *ptr++ = toupper (*str++);
812 *ptr = '\0';
813 return hptr;