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