Deleted Added
sdiff udiff text old ( 12798 ) new ( 17142 )
full compact
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.
8 *
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12 *
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
16 *
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
20 *
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
24 *
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
28 */
29
30#ident "@(#)rpc_cout.c 1.14 93/07/05 SMI"
31
32#ifndef lint
33static char sccsid[] = "@(#)rpc_cout.c 1.13 89/02/22 (C) 1987 SMI";
34#endif
35
36/*
37 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
38 * Copyright (C) 1987, Sun Microsystems, Inc.
39 */
40#include <stdio.h>
41#include <string.h>
42#include "rpc_parse.h"
43#include "rpc_util.h"
44
45static int print_header __P(( definition * ));
46static int print_trailer __P(( void ));
47static int print_stat __P(( int , declaration * ));
48static int emit_enum __P(( definition * ));
49static int emit_program __P(( definition * ));
50static int emit_union __P(( definition * ));
51static int emit_struct __P(( definition * ));
52static int emit_typedef __P(( definition * ));
53
54/*
55 * Emit the C-routine for the given definition
56 */
57void
58emit(def)
59 definition *def;
60{
61 if (def->def_kind == DEF_CONST) {
62 return;
63 }
64 if (def->def_kind == DEF_PROGRAM) {
65 emit_program(def);
66 return;
67 }
68 if (def->def_kind == DEF_TYPEDEF) {
69 /*
70 * now we need to handle declarations like
71 * struct typedef foo foo;
72 * since we dont want this to be expanded into 2 calls to xdr_foo
73 */
74
75 if (strcmp(def->def.ty.old_type, def->def_name) == 0)
76 return;
77 };
78 print_header(def);
79 switch (def->def_kind) {
80 case DEF_UNION:
81 emit_union(def);
82 break;
83 case DEF_ENUM:
84 emit_enum(def);
85 break;
86 case DEF_STRUCT:
87 emit_struct(def);
88 break;
89 case DEF_TYPEDEF:
90 emit_typedef(def);
91 break;
92 }
93 print_trailer();
94}
95
96static
97findtype(def, type)
98 definition *def;
99 char *type;
100{
101
102 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
103 return (0);
104 } else {
105 return (streq(def->def_name, type));
106 }
107}
108
109static
110undefined(type)
111 char *type;
112{
113 definition *def;
114
115 def = (definition *) FINDVAL(defined, type, findtype);
116 return (def == NULL);
117}
118
119
120static
121print_generic_header(procname, pointerp)
122 char* procname;
123 int pointerp;
124{
125 f_print(fout, "\n");
126 f_print(fout, "bool_t\n");
127 if (Cflag) {
128 f_print(fout, "xdr_%s(", procname);
129 f_print(fout, "register XDR *xdrs, ");
130 f_print(fout, "%s ", procname);
131 if (pointerp)
132 f_print(fout, "*");
133 f_print(fout, "objp)\n{\n\n");
134 } else {
135 f_print(fout, "xdr_%s(xdrs, objp)\n", procname);
136 f_print(fout, "\tregister XDR *xdrs;\n");
137 f_print(fout, "\t%s ", procname);
138 if (pointerp)
139 f_print(fout, "*");
140 f_print(fout, "objp;\n{\n\n");
141 }
142}
143
144static
145print_header(def)
146 definition *def;
147{
148
149 decl_list *dl;
150 bas_type *ptr;
151 int i;
152
153 print_generic_header(def->def_name,
154 def->def_kind != DEF_TYPEDEF ||
155 !isvectordef(def->def.ty.old_type,
156 def->def.ty.rel));
157 /* Now add Inline support */
158
159 if (inline == 0)
160 return;
161 /* May cause lint to complain. but ... */
162 f_print(fout, "\tregister long *buf;\n\n");
163}
164
165static
166print_prog_header(plist)
167 proc_list *plist;
168{
169 print_generic_header(plist->args.argname, 1);
170}
171
172static
173print_trailer()
174{
175 f_print(fout, "\treturn (TRUE);\n");
176 f_print(fout, "}\n");
177}
178
179
180static
181print_ifopen(indent, name)
182 int indent;
183 char *name;
184{
185 tabify(fout, indent);
186 f_print(fout, "if (!xdr_%s(xdrs", name);
187}
188
189static
190print_ifarg(arg)
191 char *arg;
192{
193 f_print(fout, ", %s", arg);
194}
195
196static
197print_ifsizeof(indent, prefix, type)
198 int indent;
199 char *prefix;
200 char *type;
201{
202 if (indent) {
203 f_print(fout, ",\n");
204 tabify(fout, indent);
205 } else {
206 f_print(fout, ", ");
207 }
208 if (streq(type, "bool")) {
209 f_print(fout, "sizeof (bool_t), (xdrproc_t) xdr_bool");
210 } else {
211 f_print(fout, "sizeof (");
212 if (undefined(type) && prefix) {
213 f_print(fout, "%s ", prefix);
214 }
215 f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type);
216 }
217}
218
219static
220print_ifclose(indent)
221 int indent;
222{
223 f_print(fout, "))\n");
224 tabify(fout, indent);
225 f_print(fout, "\treturn (FALSE);\n");
226}
227
228static
229print_ifstat(indent, prefix, type, rel, amax, objname, name)
230 int indent;
231 char *prefix;
232 char *type;
233 relation rel;
234 char *amax;
235 char *objname;
236 char *name;
237{
238 char *alt = NULL;
239
240 switch (rel) {
241 case REL_POINTER:
242 print_ifopen(indent, "pointer");
243 print_ifarg("(char **)");
244 f_print(fout, "%s", objname);
245 print_ifsizeof(0, prefix, type);
246 break;
247 case REL_VECTOR:
248 if (streq(type, "string")) {
249 alt = "string";
250 } else if (streq(type, "opaque")) {
251 alt = "opaque";
252 }
253 if (alt) {
254 print_ifopen(indent, alt);
255 print_ifarg(objname);
256 } else {
257 print_ifopen(indent, "vector");
258 print_ifarg("(char *)");
259 f_print(fout, "%s", objname);
260 }
261 print_ifarg(amax);
262 if (!alt) {
263 print_ifsizeof(indent + 1, prefix, type);
264 }
265 break;
266 case REL_ARRAY:
267 if (streq(type, "string")) {
268 alt = "string";
269 } else if (streq(type, "opaque")) {
270 alt = "bytes";
271 }
272 if (streq(type, "string")) {
273 print_ifopen(indent, alt);
274 print_ifarg(objname);
275 } else {
276 if (alt) {
277 print_ifopen(indent, alt);
278 } else {
279 print_ifopen(indent, "array");
280 }
281 print_ifarg("(char **)");
282 if (*objname == '&') {
283 f_print(fout, "%s.%s_val, (u_int *) %s.%s_len",
284 objname, name, objname, name);
285 } else {
286 f_print(fout,
287 "&%s->%s_val, (u_int *) &%s->%s_len",
288 objname, name, objname, name);
289 }
290 }
291 print_ifarg(amax);
292 if (!alt) {
293 print_ifsizeof(indent + 1, prefix, type);
294 }
295 break;
296 case REL_ALIAS:
297 print_ifopen(indent, type);
298 print_ifarg(objname);
299 break;
300 }
301 print_ifclose(indent);
302}
303
304/* ARGSUSED */
305static
306emit_enum(def)
307 definition *def;
308{
309 print_ifopen(1, "enum");
310 print_ifarg("(enum_t *)objp");
311 print_ifclose(1);
312}
313
314static
315emit_program(def)
316 definition *def;
317{
318 decl_list *dl;
319 version_list *vlist;
320 proc_list *plist;
321
322 for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
323 for (plist = vlist->procs; plist != NULL; plist = plist->next) {
324 if (!newstyle || plist->arg_num < 2)
325 continue; /* old style, or single argument */
326 print_prog_header(plist);
327 for (dl = plist->args.decls; dl != NULL;
328 dl = dl->next)
329 print_stat(1, &dl->decl);
330 print_trailer();
331 }
332}
333
334
335static
336emit_union(def)
337 definition *def;
338{
339 declaration *dflt;
340 case_list *cl;
341 declaration *cs;
342 char *object;
343 char *vecformat = "objp->%s_u.%s";
344 char *format = "&objp->%s_u.%s";
345
346 print_stat(1, &def->def.un.enum_decl);
347 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
348 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
349
350 f_print(fout, "\tcase %s:\n", cl->case_name);
351 if (cl->contflag == 1) /* a continued case statement */
352 continue;
353 cs = &cl->case_decl;
354 if (!streq(cs->type, "void")) {
355 object = alloc(strlen(def->def_name) + strlen(format) +
356 strlen(cs->name) + 1);
357 if (isvectordef (cs->type, cs->rel)) {
358 s_print(object, vecformat, def->def_name,
359 cs->name);
360 } else {
361 s_print(object, format, def->def_name,
362 cs->name);
363 }
364 print_ifstat(2, cs->prefix, cs->type, cs->rel,
365 cs->array_max, object, cs->name);
366 free(object);
367 }
368 f_print(fout, "\t\tbreak;\n");
369 }
370 dflt = def->def.un.default_decl;
371 if (dflt != NULL) {
372 if (!streq(dflt->type, "void")) {
373 f_print(fout, "\tdefault:\n");
374 object = alloc(strlen(def->def_name) + strlen(format) +
375strlen(dflt->name) + 1);
376 if (isvectordef (dflt->type, dflt->rel)) {
377 s_print(object, vecformat, def->def_name,
378 dflt->name);
379 } else {
380 s_print(object, format, def->def_name,
381 dflt->name);
382 }
383
384 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
385 dflt->array_max, object, dflt->name);
386 free(object);
387 f_print(fout, "\t\tbreak;\n");
388 }
389 } else {
390 f_print(fout, "\tdefault:\n");
391 f_print(fout, "\t\treturn (FALSE);\n");
392 }
393
394 f_print(fout, "\t}\n");
395}
396
397static void
398inline_struct(def, flag)
399definition *def;
400int flag;
401{
402 decl_list *dl;
403 int i, size;
404 decl_list *cur, *psav;
405 bas_type *ptr;
406 char *sizestr, *plus;
407 char ptemp[256];
408 int indent = 1;
409
410 if (flag == PUT)
411 f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
412 else
413 f_print(fout, "\t\treturn (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
414
415 i = 0;
416 size = 0;
417 sizestr = NULL;
418 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
419 /* now walk down the list and check for basic types */
420 if ((dl->decl.prefix == NULL) &&
421 ((ptr = find_type(dl->decl.type)) != NULL) &&
422 ((dl->decl.rel == REL_ALIAS) ||
423 (dl->decl.rel == REL_VECTOR))){
424 if (i == 0)
425 cur = dl;
426 i++;
427
428 if (dl->decl.rel == REL_ALIAS)
429 size += ptr->length;
430 else {
431 /* this code is required to handle arrays */
432 if (sizestr == NULL)
433 plus = "";
434 else
435 plus = " + ";
436
437 if (ptr->length != 1)
438 s_print(ptemp, "%s%s * %d",
439 plus, dl->decl.array_max,
440 ptr->length);
441 else
442 s_print(ptemp, "%s%s", plus,
443 dl->decl.array_max);
444
445 /* now concatenate to sizestr !!!! */
446 if (sizestr == NULL)
447 sizestr = strdup(ptemp);
448 else{
449 sizestr = realloc(sizestr,
450 strlen(sizestr)
451 +strlen(ptemp)+1);
452 if (sizestr == NULL){
453 f_print(stderr,
454 "Fatal error : no memory\n");
455 crash();
456 };
457 sizestr = strcat(sizestr, ptemp);
458 /* build up length of array */
459 }
460 }
461 } else {
462 if (i > 0)
463 if (sizestr == NULL && size < inline){
464 /*
465 * don't expand into inline code
466 * if size < inline
467 */
468 while (cur != dl){
469 print_stat(indent + 1, &cur->decl);
470 cur = cur->next;
471 }
472 } else {
473 /* were already looking at a xdr_inlineable structure */
474 tabify(fout, indent + 1);
475 if (sizestr == NULL)
476 f_print(fout, "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
477 size);
478 else
479 if (size == 0)
480 f_print(fout,
481 "buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
482 sizestr);
483 else
484 f_print(fout,
485 "buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
486 size, sizestr);
487
488 f_print(fout, "\n");
489 tabify(fout, indent + 1);
490 f_print(fout,
491 "if (buf == NULL) {\n");
492
493 psav = cur;
494 while (cur != dl){
495 print_stat(indent + 2, &cur->decl);
496 cur = cur->next;
497 }
498
499 f_print(fout, "\n\t\t} else {\n");
500
501 cur = psav;
502 while (cur != dl){
503 emit_inline(indent + 2, &cur->decl, flag);
504 cur = cur->next;
505 }
506
507 tabify(fout, indent + 1);
508 f_print(fout, "}\n");
509 }
510 size = 0;
511 i = 0;
512 sizestr = NULL;
513 print_stat(indent + 1, &dl->decl);
514 }
515 }
516
517 if (i > 0)
518 if (sizestr == NULL && size < inline){
519 /* don't expand into inline code if size < inline */
520 while (cur != dl){
521 print_stat(indent + 1, &cur->decl);
522 cur = cur->next;
523 }
524 } else {
525 /* were already looking at a xdr_inlineable structure */
526 if (sizestr == NULL)
527 f_print(fout, "\t\tbuf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
528 size);
529 else
530 if (size == 0)
531 f_print(fout,
532 "\t\tbuf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
533 sizestr);
534 else
535 f_print(fout,
536 "\t\tbuf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
537 size, sizestr);
538
539 f_print(fout, "\n\t\tif (buf == NULL) {\n");
540 psav = cur;
541 while (cur != NULL){
542 print_stat(indent + 2, &cur->decl);
543 cur = cur->next;
544 }
545 f_print(fout, "\t\t} else {\n");
546
547 cur = psav;
548 while (cur != dl){
549 emit_inline(indent + 2, &cur->decl, flag);
550 cur = cur->next;
551 }
552 f_print(fout, "\t\t}\n");
553 }
554}
555
556static
557emit_struct(def)
558 definition *def;
559{
560 decl_list *dl;
561 int i, j, size, flag;
562 bas_type *ptr;
563 int can_inline;
564
565 if (inline == 0) {
566 /* No xdr_inlining at all */
567 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
568 print_stat(1, &dl->decl);
569 return;
570 }
571
572 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
573 if (dl->decl.rel == REL_VECTOR){
574 f_print(fout, "\tint i;\n");
575 break;
576 }
577
578 size = 0;
579 can_inline = 0;
580 /*
581 * Make a first pass and see if inling is possible.
582 */
583 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
584 if ((dl->decl.prefix == NULL) &&
585 ((ptr = find_type(dl->decl.type)) != NULL) &&
586 ((dl->decl.rel == REL_ALIAS)||
587 (dl->decl.rel == REL_VECTOR))){
588 if (dl->decl.rel == REL_ALIAS)
589 size += ptr->length;
590 else {
591 can_inline = 1;
592 break; /* can be inlined */
593 }
594 } else {
595 if (size >= inline){
596 can_inline = 1;
597 break; /* can be inlined */
598 }
599 size = 0;
600 }
601 if (size >= inline)
602 can_inline = 1;
603
604 if (can_inline == 0){ /* can not inline, drop back to old mode */
605 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
606 print_stat(1, &dl->decl);
607 return;
608 }
609
610 flag = PUT;
611 for (j = 0; j < 2; j++){
612 inline_struct(def, flag);
613 if (flag == PUT)
614 flag = GET;
615 }
616
617 f_print(fout, "\t\treturn (TRUE);\n\t}\n\n");
618
619 /* now take care of XDR_FREE case */
620
621 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
622 print_stat(1, &dl->decl);
623
624}
625
626static
627emit_typedef(def)
628 definition *def;
629{
630 char *prefix = def->def.ty.old_prefix;
631 char *type = def->def.ty.old_type;
632 char *amax = def->def.ty.array_max;
633 relation rel = def->def.ty.rel;
634
635 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
636}
637
638static
639print_stat(indent, dec)
640 int indent;
641 declaration *dec;
642{
643 char *prefix = dec->prefix;
644 char *type = dec->type;
645 char *amax = dec->array_max;
646 relation rel = dec->rel;
647 char name[256];
648
649 if (isvectordef(type, rel)) {
650 s_print(name, "objp->%s", dec->name);
651 } else {
652 s_print(name, "&objp->%s", dec->name);
653 }
654 print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
655}
656
657
658char *upcase ();
659
660emit_inline(indent, decl, flag)
661int indent;
662declaration *decl;
663int flag;
664{
665 switch (decl->rel) {
666 case REL_ALIAS :
667 emit_single_in_line(indent, decl, flag, REL_ALIAS);
668 break;
669 case REL_VECTOR :
670 tabify(fout, indent);
671 f_print(fout, "{\n");
672 tabify(fout, indent + 1);
673 f_print(fout, "register %s *genp;\n\n", decl->type);
674 tabify(fout, indent + 1);
675 f_print(fout,
676 "for (i = 0, genp = objp->%s;\n", decl->name);
677 tabify(fout, indent + 2);
678 f_print(fout, "i < %s; i++) {\n", decl->array_max);
679 emit_single_in_line(indent + 2, decl, flag, REL_VECTOR);
680 tabify(fout, indent + 1);
681 f_print(fout, "}\n");
682 tabify(fout, indent);
683 f_print(fout, "}\n");
684 }
685}
686
687emit_single_in_line(indent, decl, flag, rel)
688int indent;
689declaration *decl;
690int flag;
691relation rel;
692{
693 char *upp_case;
694 int freed = 0;
695
696 tabify(fout, indent);
697 if (flag == PUT)
698 f_print(fout, "IXDR_PUT_");
699 else
700 if (rel == REL_ALIAS)
701 f_print(fout, "objp->%s = IXDR_GET_", decl->name);
702 else
703 f_print(fout, "*genp++ = IXDR_GET_");
704
705 upp_case = upcase(decl->type);
706
707 /* hack - XX */
708 if (strcmp(upp_case, "INT") == 0)
709 {
710 free(upp_case);
711 freed = 1;
712 upp_case = "LONG";
713 }
714
715 if (strcmp(upp_case, "U_INT") == 0)
716 {
717 free(upp_case);
718 freed = 1;
719 upp_case = "U_LONG";
720 }
721 if (flag == PUT)
722 if (rel == REL_ALIAS)
723 f_print(fout,
724 "%s(buf, objp->%s);\n", upp_case, decl->name);
725 else
726 f_print(fout, "%s(buf, *genp++);\n", upp_case);
727
728 else
729 f_print(fout, "%s(buf);\n", upp_case);
730 if (!freed)
731 free(upp_case);
732}
733
734char *upcase(str)
735char *str;
736{
737 char *ptr, *hptr;
738
739 ptr = (char *)malloc(strlen(str)+1);
740 if (ptr == (char *) NULL)
741 {
742 f_print(stderr, "malloc failed\n");
743 exit(1);
744 };
745
746 hptr = ptr;
747 while (*str != '\0')
748 *ptr++ = toupper(*str++);
749
750 *ptr = '\0';
751 return (hptr);
752}