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