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