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