xref: /freebsd/usr.bin/rpcgen/rpc_cout.c (revision 2a4a1db342263067035ce69a4017c645da63455d)
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 	}
103 	print_trailer();
104 }
105 
106 static int
107 findtype(def, type)
108 	definition *def;
109 	char *type;
110 {
111 
112 	if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
113 		return (0);
114 	} else {
115 		return (streq(def->def_name, type));
116 	}
117 }
118 
119 static int
120 undefined(type)
121 	char *type;
122 {
123 	definition *def;
124 
125 	def = (definition *) FINDVAL(defined, type, findtype);
126 	return (def == NULL);
127 }
128 
129 
130 static void
131 print_generic_header(procname, pointerp)
132     char* procname;
133     int pointerp;
134 {
135 	f_print(fout, "\n");
136 	f_print(fout, "bool_t\n");
137 	if (Cflag) {
138 	    f_print(fout, "xdr_%s(", procname);
139 	    f_print(fout, "register XDR *xdrs, ");
140 	    f_print(fout, "%s ", procname);
141 	    if (pointerp)
142 		    f_print(fout, "*");
143 	    f_print(fout, "objp)\n{\n\n");
144 	} else {
145 	    f_print(fout, "xdr_%s(xdrs, objp)\n", procname);
146 	    f_print(fout, "\tregister XDR *xdrs;\n");
147 	    f_print(fout, "\t%s ", procname);
148 	    if (pointerp)
149 		    f_print(fout, "*");
150 	    f_print(fout, "objp;\n{\n\n");
151 	}
152 }
153 
154 static void
155 print_header(def)
156 	definition *def;
157 {
158 	print_generic_header(def->def_name,
159 			    def->def_kind != DEF_TYPEDEF ||
160 			    !isvectordef(def->def.ty.old_type,
161 					 def->def.ty.rel));
162 	/* Now add Inline support */
163 
164 	if (inline == 0)
165 		return;
166 	/* May cause lint to complain. but  ... */
167 	f_print(fout, "\tregister long *buf;\n\n");
168 }
169 
170 static void
171 print_prog_header(plist)
172 	proc_list *plist;
173 {
174 	print_generic_header(plist->args.argname, 1);
175 }
176 
177 static void
178 print_trailer()
179 {
180 	f_print(fout, "\treturn (TRUE);\n");
181 	f_print(fout, "}\n");
182 }
183 
184 
185 static void
186 print_ifopen(indent, name)
187 	int indent;
188 	char *name;
189 {
190 	tabify(fout, indent);
191 	f_print(fout, "if (!xdr_%s(xdrs", name);
192 }
193 
194 static void
195 print_ifarg(arg)
196 	char *arg;
197 {
198 	f_print(fout, ", %s", arg);
199 }
200 
201 static void
202 print_ifsizeof(indent, prefix, type)
203 	int indent;
204 	char *prefix;
205 	char *type;
206 {
207 	if (indent) {
208 		f_print(fout, ",\n");
209 		tabify(fout, indent);
210 	} else  {
211 		f_print(fout, ", ");
212 	}
213 	if (streq(type, "bool")) {
214 		f_print(fout, "sizeof (bool_t), (xdrproc_t) xdr_bool");
215 	} else {
216 		f_print(fout, "sizeof (");
217 		if (undefined(type) && prefix) {
218 			f_print(fout, "%s ", prefix);
219 		}
220 		f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type);
221 	}
222 }
223 
224 static void
225 print_ifclose(indent)
226 	int indent;
227 {
228 	f_print(fout, "))\n");
229 	tabify(fout, indent);
230 	f_print(fout, "\treturn (FALSE);\n");
231 }
232 
233 static void
234 print_ifstat(indent, prefix, type, rel, amax, objname, name)
235 	int indent;
236 	char *prefix;
237 	char *type;
238 	relation rel;
239 	char *amax;
240 	char *objname;
241 	char *name;
242 {
243 	char *alt = NULL;
244 
245 	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")) {
254 			alt = "string";
255 		} else 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(indent + 1, prefix, type);
269 		}
270 		break;
271 	case REL_ARRAY:
272 		if (streq(type, "string")) {
273 			alt = "string";
274 		} else if (streq(type, "opaque")) {
275 			alt = "bytes";
276 		}
277 		if (streq(type, "string")) {
278 			print_ifopen(indent, alt);
279 			print_ifarg(objname);
280 		} else {
281 			if (alt) {
282 				print_ifopen(indent, alt);
283 			} else {
284 				print_ifopen(indent, "array");
285 			}
286 			print_ifarg("(char **)");
287 			if (*objname == '&') {
288 				f_print(fout, "%s.%s_val, (u_int *) %s.%s_len",
289 					objname, name, objname, name);
290 			} else {
291 				f_print(fout,
292 					"&%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(indent + 1, 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 	print_ifopen(1, "enum");
315 	print_ifarg("(enum_t *)objp");
316 	print_ifclose(1);
317 }
318 
319 static void
320 emit_program(def)
321 	definition *def;
322 {
323 	decl_list *dl;
324 	version_list *vlist;
325 	proc_list *plist;
326 
327 	for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
328 		for (plist = vlist->procs; plist != NULL; plist = plist->next) {
329 			if (!newstyle || plist->arg_num < 2)
330 				continue; /* old style, or single argument */
331 			print_prog_header(plist);
332 			for (dl = plist->args.decls; dl != NULL;
333 			     dl = dl->next)
334 				print_stat(1, &dl->decl);
335 			print_trailer();
336 		}
337 }
338 
339 
340 static void
341 emit_union(def)
342 	definition *def;
343 {
344 	declaration *dflt;
345 	case_list *cl;
346 	declaration *cs;
347 	char *object;
348 	char *vecformat = "objp->%s_u.%s";
349 	char *format = "&objp->%s_u.%s";
350 
351 	print_stat(1, &def->def.un.enum_decl);
352 	f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
353 	for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
354 
355 		f_print(fout, "\tcase %s:\n", cl->case_name);
356 		if (cl->contflag == 1) /* a continued case statement */
357 			continue;
358 		cs = &cl->case_decl;
359 		if (!streq(cs->type, "void")) {
360 			object = alloc(strlen(def->def_name) + strlen(format) +
361 				       strlen(cs->name) + 1);
362 			if (isvectordef (cs->type, cs->rel)) {
363 				s_print(object, vecformat, def->def_name,
364 					cs->name);
365 			} else {
366 				s_print(object, format, def->def_name,
367 					cs->name);
368 			}
369 			print_ifstat(2, cs->prefix, cs->type, cs->rel,
370 				     cs->array_max, object, cs->name);
371 			free(object);
372 		}
373 		f_print(fout, "\t\tbreak;\n");
374 	}
375 	dflt = def->def.un.default_decl;
376 	if (dflt != NULL) {
377 		if (!streq(dflt->type, "void")) {
378 			f_print(fout, "\tdefault:\n");
379 			object = alloc(strlen(def->def_name) + strlen(format) +
380 strlen(dflt->name) + 1);
381 			if (isvectordef (dflt->type, dflt->rel)) {
382 				s_print(object, vecformat, def->def_name,
383 					dflt->name);
384 			} else {
385 				s_print(object, format, def->def_name,
386 					dflt->name);
387 			}
388 
389 			print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
390 				    dflt->array_max, object, dflt->name);
391 			free(object);
392 			f_print(fout, "\t\tbreak;\n");
393 		} else {
394 			f_print(fout, "\tdefault:\n");
395 			f_print(fout, "\t\tbreak;\n");
396 		}
397 	} else {
398 		f_print(fout, "\tdefault:\n");
399 		f_print(fout, "\t\treturn (FALSE);\n");
400 	}
401 
402 	f_print(fout, "\t}\n");
403 }
404 
405 static void
406 inline_struct(def, flag)
407 definition *def;
408 int flag;
409 {
410 	decl_list *dl;
411 	int i, size;
412 	decl_list *cur, *psav;
413 	bas_type *ptr;
414 	char *sizestr, *plus;
415 	char ptemp[256];
416 	int indent = 1;
417 
418 	if (flag == PUT)
419 		f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
420 	else
421 		f_print(fout, "\t\treturn (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
422 
423 	i = 0;
424 	size = 0;
425 	sizestr = NULL;
426 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
427 		/* now walk down the list and check for basic types */
428 		if ((dl->decl.prefix == NULL) &&
429 		    ((ptr = find_type(dl->decl.type)) != NULL) &&
430 		    ((dl->decl.rel == REL_ALIAS) ||
431 		     (dl->decl.rel == REL_VECTOR))){
432 			if (i == 0)
433 				cur = dl;
434 			i++;
435 
436 			if (dl->decl.rel == REL_ALIAS)
437 				size += ptr->length;
438 			else {
439 				/* this code is required to handle arrays */
440 				if (sizestr == NULL)
441 					plus = "";
442 				else
443 					plus = " + ";
444 
445 				if (ptr->length != 1)
446 					s_print(ptemp, "%s%s * %d",
447 						plus, dl->decl.array_max,
448 						ptr->length);
449 				else
450 					s_print(ptemp, "%s%s", plus,
451 						dl->decl.array_max);
452 
453 				/* now concatenate to sizestr !!!! */
454 				if (sizestr == NULL)
455 					sizestr = strdup(ptemp);
456 				else{
457 					sizestr = realloc(sizestr,
458 							  strlen(sizestr)
459 							  +strlen(ptemp)+1);
460 					if (sizestr == NULL){
461 						warnx("fatal error: no memory");
462 						crash();
463 					};
464 					sizestr = strcat(sizestr, ptemp);
465 					/* build up length of array */
466 				}
467 			}
468 		} else {
469 			if (i > 0) {
470 				if (sizestr == NULL && size < inline){
471 					/*
472 					 * don't expand into inline code
473 					 * if size < inline
474 					 */
475 					while (cur != dl){
476 						print_stat(indent + 1, &cur->decl);
477 						cur = cur->next;
478 					}
479 				} else {
480 					/* were already looking at a xdr_inlineable structure */
481 					tabify(fout, indent + 1);
482 					if (sizestr == NULL)
483 						f_print(fout, "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
484 							size);
485 					else {
486 						if (size == 0)
487 							f_print(fout,
488 								"buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
489 								sizestr);
490 						else
491 							f_print(fout,
492 								"buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
493 								size, sizestr);
494 
495 					}
496 					f_print(fout, "\n");
497 					tabify(fout, indent + 1);
498 					f_print(fout,
499 						"if (buf == NULL) {\n");
500 
501 					psav = cur;
502 					while (cur != dl){
503 						print_stat(indent + 2, &cur->decl);
504 						cur = cur->next;
505 					}
506 
507 					f_print(fout, "\n\t\t} else {\n");
508 
509 					cur = psav;
510 					while (cur != dl){
511 						emit_inline(indent + 2, &cur->decl, flag);
512 						cur = cur->next;
513 					}
514 
515 					tabify(fout, indent + 1);
516 					f_print(fout, "}\n");
517 				}
518 			}
519 			size = 0;
520 			i = 0;
521 			sizestr = NULL;
522 			print_stat(indent + 1, &dl->decl);
523 		}
524 	}
525 
526 	if (i > 0)
527 		if (sizestr == NULL && size < inline){
528 			/* don't expand into inline code if size < inline */
529 			while (cur != dl){
530 				print_stat(indent + 1, &cur->decl);
531 				cur = cur->next;
532 			}
533 		} else {
534 			/* were already looking at a xdr_inlineable structure */
535 			if (sizestr == NULL)
536 				f_print(fout, "\t\tbuf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
537 					size);
538 			else
539 				if (size == 0)
540 					f_print(fout,
541 						"\t\tbuf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
542 						sizestr);
543 				else
544 					f_print(fout,
545 						"\t\tbuf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
546 						size, sizestr);
547 
548 			f_print(fout, "\n\t\tif (buf == NULL) {\n");
549 			psav = cur;
550 			while (cur != NULL){
551 				print_stat(indent + 2, &cur->decl);
552 				cur = cur->next;
553 			}
554 			f_print(fout, "\t\t} else {\n");
555 
556 			cur = psav;
557 			while (cur != dl){
558 				emit_inline(indent + 2, &cur->decl, flag);
559 				cur = cur->next;
560 			}
561 			f_print(fout, "\t\t}\n");
562 		}
563 }
564 
565 static void
566 emit_struct(def)
567 	definition *def;
568 {
569 	decl_list *dl;
570 	int j, size, flag;
571 	bas_type *ptr;
572 	int can_inline;
573 
574 	if (inline == 0) {
575 		/* No xdr_inlining at all */
576 		for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
577 			print_stat(1, &dl->decl);
578 		return;
579 	}
580 
581 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
582 		if (dl->decl.rel == REL_VECTOR){
583 			f_print(fout, "\tint i;\n");
584 			break;
585 		}
586 
587 	size = 0;
588 	can_inline = 0;
589 	/*
590 	 * Make a first pass and see if inling is possible.
591 	 */
592 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
593 		if ((dl->decl.prefix == NULL) &&
594 		    ((ptr = find_type(dl->decl.type)) != NULL) &&
595 		    ((dl->decl.rel == REL_ALIAS)||
596 		     (dl->decl.rel == REL_VECTOR))){
597 			if (dl->decl.rel == REL_ALIAS)
598 				size += ptr->length;
599 			else {
600 				can_inline = 1;
601 				break; /* can be inlined */
602 			}
603 		} else {
604 			if (size >= inline){
605 				can_inline = 1;
606 				break; /* can be inlined */
607 			}
608 			size = 0;
609 		}
610 	if (size >= inline)
611 		can_inline = 1;
612 
613 	if (can_inline == 0){	/* can not inline, drop back to old mode */
614 		for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
615 			print_stat(1, &dl->decl);
616 		return;
617 	}
618 
619 	flag = PUT;
620 	for (j = 0; j < 2; j++){
621 		inline_struct(def, flag);
622 		if (flag == PUT)
623 			flag = GET;
624 	}
625 
626 	f_print(fout, "\t\treturn (TRUE);\n\t}\n\n");
627 
628 	/* now take care of XDR_FREE case */
629 
630 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
631 		print_stat(1, &dl->decl);
632 
633 }
634 
635 static void
636 emit_typedef(def)
637 	definition *def;
638 {
639 	char *prefix = def->def.ty.old_prefix;
640 	char *type = def->def.ty.old_type;
641 	char *amax = def->def.ty.array_max;
642 	relation rel = def->def.ty.rel;
643 
644 	print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
645 }
646 
647 static void
648 print_stat(indent, dec)
649 	int indent;
650 	declaration *dec;
651 {
652 	char *prefix = dec->prefix;
653 	char *type = dec->type;
654 	char *amax = dec->array_max;
655 	relation rel = dec->rel;
656 	char name[256];
657 
658 	if (isvectordef(type, rel)) {
659 		s_print(name, "objp->%s", dec->name);
660 	} else {
661 		s_print(name, "&objp->%s", dec->name);
662 	}
663 	print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
664 }
665 
666 
667 char *upcase ();
668 
669 static void
670 emit_inline(indent, decl, flag)
671 int indent;
672 declaration *decl;
673 int flag;
674 {
675 	switch (decl->rel) {
676 	case  REL_ALIAS :
677 		emit_single_in_line(indent, decl, flag, REL_ALIAS);
678 		break;
679 	case REL_VECTOR :
680 		tabify(fout, indent);
681 		f_print(fout, "{\n");
682 		tabify(fout, indent + 1);
683 		f_print(fout, "register %s *genp;\n\n", decl->type);
684 		tabify(fout, indent + 1);
685 		f_print(fout,
686 			"for (i = 0, genp = objp->%s;\n", decl->name);
687 		tabify(fout, indent + 2);
688 		f_print(fout, "i < %s; i++) {\n", decl->array_max);
689 		emit_single_in_line(indent + 2, decl, flag, REL_VECTOR);
690 		tabify(fout, indent + 1);
691 		f_print(fout, "}\n");
692 		tabify(fout, indent);
693 		f_print(fout, "}\n");
694 	default:
695 	}
696 }
697 
698 static void
699 emit_single_in_line(indent, decl, flag, rel)
700 int indent;
701 declaration *decl;
702 int flag;
703 relation rel;
704 {
705 	char *upp_case;
706 	int freed = 0;
707 
708 	tabify(fout, indent);
709 	if (flag == PUT)
710 		f_print(fout, "IXDR_PUT_");
711 	else
712 		if (rel == REL_ALIAS)
713 			f_print(fout, "objp->%s = IXDR_GET_", decl->name);
714 		else
715 			f_print(fout, "*genp++ = IXDR_GET_");
716 
717 	upp_case = upcase(decl->type);
718 
719 	/* hack	 - XX */
720 	if (strcmp(upp_case, "INT") == 0)
721 	{
722 		free(upp_case);
723 		freed = 1;
724 		upp_case = "LONG";
725 	}
726 
727 	if (strcmp(upp_case, "U_INT") == 0)
728 	{
729 		free(upp_case);
730 		freed = 1;
731 		upp_case = "U_LONG";
732 	}
733 	if (flag == PUT)
734 		if (rel == REL_ALIAS)
735 			f_print(fout,
736 				"%s(buf, objp->%s);\n", upp_case, decl->name);
737 		else
738 			f_print(fout, "%s(buf, *genp++);\n", upp_case);
739 
740 	else
741 		f_print(fout, "%s(buf);\n", upp_case);
742 	if (!freed)
743 		free(upp_case);
744 }
745 
746 char *upcase(str)
747 char *str;
748 {
749 	char *ptr, *hptr;
750 
751 	ptr =  (char *)malloc(strlen(str)+1);
752 	if (ptr == (char *) NULL)
753 		errx(1, "malloc failed");
754 
755 	hptr = ptr;
756 	while (*str != '\0')
757 		*ptr++ = toupper(*str++);
758 
759 	*ptr = '\0';
760 	return (hptr);
761 }
762