xref: /freebsd/usr.bin/rpcgen/rpc_cout.c (revision 63f17371)
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 #if 0
31 #ifndef lint
32 #ident	"@(#)rpc_cout.c	1.14	93/07/05 SMI"
33 static char sccsid[] = "@(#)rpc_cout.c 1.13 89/02/22 (C) 1987 SMI";
34 #endif
35 #endif
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 /*
41  * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
42  * Copyright (C) 1987, Sun Microsystems, Inc.
43  */
44 #include <ctype.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include "rpc_parse.h"
48 #include "rpc_util.h"
49 
50 static void print_header( definition * );
51 static void print_trailer( void );
52 static void print_stat( int , declaration * );
53 static void emit_enum( definition * );
54 static void emit_program( definition * );
55 static void emit_union( definition * );
56 static void emit_struct( definition * );
57 static void emit_typedef( definition * );
58 static void emit_inline( int, declaration *, int );
59 static void emit_single_in_line( int, declaration *, int, relation );
60 
61 /*
62  * Emit the C-routine for the given definition
63  */
64 void
65 emit(def)
66 	definition *def;
67 {
68 	if (def->def_kind == DEF_CONST) {
69 		return;
70 	}
71 	if (def->def_kind == DEF_PROGRAM) {
72 		emit_program(def);
73 		return;
74 	}
75 	if (def->def_kind == DEF_TYPEDEF) {
76 		/*
77 		 * now we need to handle declarations like
78 		 * struct typedef foo foo;
79 		 * since we dont want this to be expanded into 2 calls to xdr_foo
80 		 */
81 
82 		if (strcmp(def->def.ty.old_type, def->def_name) == 0)
83 			return;
84 	};
85 	print_header(def);
86 	switch (def->def_kind) {
87 	case DEF_UNION:
88 		emit_union(def);
89 		break;
90 	case DEF_ENUM:
91 		emit_enum(def);
92 		break;
93 	case DEF_STRUCT:
94 		emit_struct(def);
95 		break;
96 	case DEF_TYPEDEF:
97 		emit_typedef(def);
98 		break;
99 		/* DEF_CONST and DEF_PROGRAM have already been handled */
100 	default:
101 		break;
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 = xmalloc(strlen(def->def_name) +
361 			                 strlen(format) + 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 = xmalloc(strlen(def->def_name) +
380 			                 strlen(format) + 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 	cur = NULL;
419 	if (flag == PUT)
420 		f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
421 	else
422 		f_print(fout, "\t\treturn (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
423 
424 	i = 0;
425 	size = 0;
426 	sizestr = NULL;
427 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
428 		/* now walk down the list and check for basic types */
429 		if ((dl->decl.prefix == NULL) &&
430 		    ((ptr = find_type(dl->decl.type)) != NULL) &&
431 		    ((dl->decl.rel == REL_ALIAS) ||
432 		     (dl->decl.rel == REL_VECTOR))){
433 			if (i == 0)
434 				cur = dl;
435 			i++;
436 
437 			if (dl->decl.rel == REL_ALIAS)
438 				size += ptr->length;
439 			else {
440 				/* this code is required to handle arrays */
441 				if (sizestr == NULL)
442 					plus = "";
443 				else
444 					plus = " + ";
445 
446 				if (ptr->length != 1)
447 					s_print(ptemp, "%s%s * %d",
448 						plus, dl->decl.array_max,
449 						ptr->length);
450 				else
451 					s_print(ptemp, "%s%s", plus,
452 						dl->decl.array_max);
453 
454 				/* now concatenate to sizestr !!!! */
455 				if (sizestr == NULL) {
456 					sizestr = xstrdup(ptemp);
457 				}
458 				else{
459 					sizestr = xrealloc(sizestr,
460 							  strlen(sizestr)
461 							  +strlen(ptemp)+1);
462 					sizestr = strcat(sizestr, ptemp);
463 					/* build up length of array */
464 				}
465 			}
466 		} else {
467 			if (i > 0) {
468 				if (sizestr == NULL && size < inline){
469 					/*
470 					 * don't expand into inline code
471 					 * if size < inline
472 					 */
473 					while (cur != dl){
474 						print_stat(indent + 1, &cur->decl);
475 						cur = cur->next;
476 					}
477 				} else {
478 					/* were already looking at a xdr_inlineable structure */
479 					tabify(fout, indent + 1);
480 					if (sizestr == NULL)
481 						f_print(fout, "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
482 							size);
483 					else {
484 						if (size == 0)
485 							f_print(fout,
486 								"buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
487 								sizestr);
488 						else
489 							f_print(fout,
490 								"buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
491 								size, sizestr);
492 
493 					}
494 					f_print(fout, "\n");
495 					tabify(fout, indent + 1);
496 					f_print(fout,
497 						"if (buf == NULL) {\n");
498 
499 					psav = cur;
500 					while (cur != dl){
501 						print_stat(indent + 2, &cur->decl);
502 						cur = cur->next;
503 					}
504 
505 					f_print(fout, "\n\t\t} else {\n");
506 
507 					cur = psav;
508 					while (cur != dl){
509 						emit_inline(indent + 2, &cur->decl, flag);
510 						cur = cur->next;
511 					}
512 
513 					tabify(fout, indent + 1);
514 					f_print(fout, "}\n");
515 				}
516 			}
517 			size = 0;
518 			i = 0;
519 			sizestr = NULL;
520 			print_stat(indent + 1, &dl->decl);
521 		}
522 	}
523 
524 	if (i > 0) {
525 		if (sizestr == NULL && size < inline){
526 			/* don't expand into inline code if size < inline */
527 			while (cur != dl){
528 				print_stat(indent + 1, &cur->decl);
529 				cur = cur->next;
530 			}
531 		} else {
532 			/* were already looking at a xdr_inlineable structure */
533 			if (sizestr == NULL)
534 				f_print(fout, "\t\tbuf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
535 					size);
536 			else
537 				if (size == 0)
538 					f_print(fout,
539 						"\t\tbuf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
540 						sizestr);
541 				else
542 					f_print(fout,
543 						"\t\tbuf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
544 						size, sizestr);
545 
546 			f_print(fout, "\n\t\tif (buf == NULL) {\n");
547 			psav = cur;
548 			while (cur != NULL){
549 				print_stat(indent + 2, &cur->decl);
550 				cur = cur->next;
551 			}
552 			f_print(fout, "\t\t} else {\n");
553 
554 			cur = psav;
555 			while (cur != dl){
556 				emit_inline(indent + 2, &cur->decl, flag);
557 				cur = cur->next;
558 			}
559 			f_print(fout, "\t\t}\n");
560 		}
561 	}
562 }
563 
564 static void
565 emit_struct(def)
566 	definition *def;
567 {
568 	decl_list *dl;
569 	int j, size, flag;
570 	bas_type *ptr;
571 	int can_inline;
572 
573 	if (inline == 0) {
574 		/* No xdr_inlining at all */
575 		for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
576 			print_stat(1, &dl->decl);
577 		return;
578 	}
579 
580 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
581 		if (dl->decl.rel == REL_VECTOR){
582 			f_print(fout, "\tint i;\n");
583 			break;
584 		}
585 
586 	size = 0;
587 	can_inline = 0;
588 	/*
589 	 * Make a first pass and see if inling is possible.
590 	 */
591 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
592 		if ((dl->decl.prefix == NULL) &&
593 		    ((ptr = find_type(dl->decl.type)) != NULL) &&
594 		    ((dl->decl.rel == REL_ALIAS)||
595 		     (dl->decl.rel == REL_VECTOR))){
596 			if (dl->decl.rel == REL_ALIAS)
597 				size += ptr->length;
598 			else {
599 				can_inline = 1;
600 				break; /* can be inlined */
601 			}
602 		} else {
603 			if (size >= inline){
604 				can_inline = 1;
605 				break; /* can be inlined */
606 			}
607 			size = 0;
608 		}
609 	if (size >= inline)
610 		can_inline = 1;
611 
612 	if (can_inline == 0){	/* can not inline, drop back to old mode */
613 		for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
614 			print_stat(1, &dl->decl);
615 		return;
616 	}
617 
618 	flag = PUT;
619 	for (j = 0; j < 2; j++){
620 		inline_struct(def, flag);
621 		if (flag == PUT)
622 			flag = GET;
623 	}
624 
625 	f_print(fout, "\t\treturn (TRUE);\n\t}\n\n");
626 
627 	/* now take care of XDR_FREE case */
628 
629 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
630 		print_stat(1, &dl->decl);
631 
632 }
633 
634 static void
635 emit_typedef(def)
636 	definition *def;
637 {
638 	char *prefix = def->def.ty.old_prefix;
639 	char *type = def->def.ty.old_type;
640 	char *amax = def->def.ty.array_max;
641 	relation rel = def->def.ty.rel;
642 
643 	print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
644 }
645 
646 static void
647 print_stat(indent, dec)
648 	int indent;
649 	declaration *dec;
650 {
651 	char *prefix = dec->prefix;
652 	char *type = dec->type;
653 	char *amax = dec->array_max;
654 	relation rel = dec->rel;
655 	char name[256];
656 
657 	if (isvectordef(type, rel)) {
658 		s_print(name, "objp->%s", dec->name);
659 	} else {
660 		s_print(name, "&objp->%s", dec->name);
661 	}
662 	print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
663 }
664 
665 
666 char *upcase ();
667 
668 static void
669 emit_inline(indent, decl, flag)
670 int indent;
671 declaration *decl;
672 int flag;
673 {
674 	switch (decl->rel) {
675 	case  REL_ALIAS :
676 		emit_single_in_line(indent, decl, flag, REL_ALIAS);
677 		break;
678 	case REL_VECTOR :
679 		tabify(fout, indent);
680 		f_print(fout, "{\n");
681 		tabify(fout, indent + 1);
682 		f_print(fout, "register %s *genp;\n\n", decl->type);
683 		tabify(fout, indent + 1);
684 		f_print(fout,
685 			"for (i = 0, genp = objp->%s;\n", decl->name);
686 		tabify(fout, indent + 2);
687 		f_print(fout, "i < %s; i++) {\n", decl->array_max);
688 		emit_single_in_line(indent + 2, decl, flag, REL_VECTOR);
689 		tabify(fout, indent + 1);
690 		f_print(fout, "}\n");
691 		tabify(fout, indent);
692 		f_print(fout, "}\n");
693 		break;
694 	default:
695 		break;
696 	}
697 }
698 
699 static void
700 emit_single_in_line(indent, decl, flag, rel)
701 int indent;
702 declaration *decl;
703 int flag;
704 relation rel;
705 {
706 	char *upp_case;
707 	int freed = 0;
708 
709 	tabify(fout, indent);
710 	if (flag == PUT)
711 		f_print(fout, "IXDR_PUT_");
712 	else
713 		if (rel == REL_ALIAS)
714 			f_print(fout, "objp->%s = IXDR_GET_", decl->name);
715 		else
716 			f_print(fout, "*genp++ = IXDR_GET_");
717 
718 	upp_case = upcase(decl->type);
719 
720 	/* hack	 - XX */
721 	if (strcmp(upp_case, "INT") == 0)
722 	{
723 		free(upp_case);
724 		freed = 1;
725 		upp_case = "LONG";
726 	}
727 
728 	if (strcmp(upp_case, "U_INT") == 0)
729 	{
730 		free(upp_case);
731 		freed = 1;
732 		upp_case = "U_LONG";
733 	}
734 	if (flag == PUT)
735 		if (rel == REL_ALIAS)
736 			f_print(fout,
737 				"%s(buf, objp->%s);\n", upp_case, decl->name);
738 		else
739 			f_print(fout, "%s(buf, *genp++);\n", upp_case);
740 
741 	else
742 		f_print(fout, "%s(buf);\n", upp_case);
743 	if (!freed)
744 		free(upp_case);
745 }
746 
747 char *upcase(str)
748 char *str;
749 {
750 	char *ptr, *hptr;
751 
752 	ptr = (char *)xmalloc(strlen(str)+1);
753 
754 	hptr = ptr;
755 	while (*str != '\0')
756 		*ptr++ = toupper(*str++);
757 
758 	*ptr = '\0';
759 	return (hptr);
760 }
761