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