xref: /titanic_44/usr/src/cmd/rpcgen/rpc_cout.c (revision f2b7ce3eb6db75966c27b08fa312c3158f8dfabf)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 /*
30  * University Copyright- Copyright (c) 1982, 1986, 1988
31  * The Regents of the University of California
32  * All Rights Reserved
33  *
34  * University Acknowledgment- Portions of this document are derived from
35  * software developed by the University of California, Berkeley, and its
36  * contributors.
37  */
38 
39 #pragma ident	"%Z%%M%	%I%	%E% SMI"
40 
41 /*
42  * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
43  */
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <ctype.h>
48 #include "rpc_parse.h"
49 #include "rpc_util.h"
50 
51 extern void crash(void);
52 
53 static void print_header(definition *);
54 static void print_trailer(void);
55 static void emit_enum(definition *);
56 static void emit_program(definition *);
57 static void emit_union(definition *);
58 static void emit_struct(definition *);
59 static void emit_typedef(definition *);
60 static void print_stat(int, declaration *);
61 static void emit_inline(int, declaration *, int);
62 static void emit_inline64(int, declaration *, int);
63 static void emit_single_in_line(int, declaration *, int, relation);
64 static void emit_single_in_line64(int, declaration *, int, relation);
65 static char *upcase(char *);
66 
67 /*
68  * Emit the C-routine for the given definition
69  */
70 void
71 emit(definition *def)
72 {
73 	if (def->def_kind == DEF_CONST)
74 		return;
75 	if (def->def_kind == DEF_PROGRAM) {
76 		emit_program(def);
77 		return;
78 	}
79 	if (def->def_kind == DEF_TYPEDEF) {
80 		/*
81 		 * now we need to handle declarations like
82 		 * struct typedef foo foo;
83 		 * since we dont want this to be expanded into 2 calls
84 		 * to xdr_foo
85 		 */
86 
87 		if (strcmp(def->def.ty.old_type, def->def_name) == 0)
88 			return;
89 	};
90 	print_header(def);
91 	switch (def->def_kind) {
92 	case DEF_UNION:
93 		emit_union(def);
94 		break;
95 	case DEF_ENUM:
96 		emit_enum(def);
97 		break;
98 	case DEF_STRUCT:
99 		emit_struct(def);
100 		break;
101 	case DEF_TYPEDEF:
102 		emit_typedef(def);
103 		break;
104 	}
105 	print_trailer();
106 }
107 
108 static int
109 findtype(definition *def, char *type)
110 {
111 
112 	if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST)
113 		return (0);
114 	return (streq(def->def_name, type));
115 }
116 
117 static int
118 undefined(char *type)
119 {
120 	definition *def;
121 
122 	def = (definition *)FINDVAL(defined, type, findtype);
123 	return (def == NULL);
124 }
125 
126 
127 static void
128 print_generic_header(char *procname, int pointerp)
129 {
130 	f_print(fout, "\n");
131 	f_print(fout, "bool_t\n");
132 	if (Cflag) {
133 		f_print(fout, "xdr_%s(", procname);
134 		f_print(fout, "XDR *xdrs, ");
135 		f_print(fout, "%s ", procname);
136 		if (pointerp)
137 			f_print(fout, "*");
138 		f_print(fout, "objp)\n{\n\n");
139 	} else {
140 		f_print(fout, "xdr_%s(xdrs, objp)\n", procname);
141 		f_print(fout, "\tXDR *xdrs;\n");
142 		f_print(fout, "\t%s ", procname);
143 		if (pointerp)
144 			f_print(fout, "*");
145 		f_print(fout, "objp;\n{\n\n");
146 	}
147 }
148 
149 static void
150 print_header(definition *def)
151 {
152 	print_generic_header(def->def_name,
153 			    def->def_kind != DEF_TYPEDEF ||
154 			    !isvectordef(def->def.ty.old_type,
155 					def->def.ty.rel));
156 	/* Now add Inline support */
157 
158 	if (inlinelen == 0)
159 		return;
160 	/* May cause lint to complain. but  ... */
161 	f_print(fout, "\trpc_inline_t *buf;\n\n");
162 }
163 
164 static void
165 print_prog_header(proc_list *plist)
166 {
167 	print_generic_header(plist->args.argname, 1);
168 }
169 
170 static void
171 print_trailer(void)
172 {
173 	f_print(fout, "\treturn (TRUE);\n");
174 	f_print(fout, "}\n");
175 }
176 
177 
178 static void
179 print_ifopen(int indent, char *name)
180 {
181 	tabify(fout, indent);
182 	if (streq(name, "rpcprog_t") ||
183 			streq(name, "rpcvers_t") ||
184 			streq(name, "rpcproc_t") ||
185 			streq(name, "rpcprot_t") ||
186 			streq(name, "rpcport_t"))
187 		(void) strtok(name, "_");
188 	f_print(fout, "if (!xdr_%s(xdrs", name);
189 }
190 
191 static void
192 print_ifarg(char *arg)
193 {
194 	f_print(fout, ", %s", arg);
195 }
196 
197 static void
198 print_ifsizeof(int indent, char *prefix, char *type)
199 {
200 	if (indent) {
201 		f_print(fout, ",\n");
202 		tabify(fout, indent);
203 	} else {
204 		f_print(fout, ", ");
205 	}
206 	if (streq(type, "bool")) {
207 		f_print(fout, "sizeof (bool_t), (xdrproc_t)xdr_bool");
208 	} else {
209 		f_print(fout, "sizeof (");
210 		if (undefined(type) && prefix) {
211 			f_print(fout, "%s ", prefix);
212 		}
213 		f_print(fout, "%s), (xdrproc_t)xdr_%s", type, type);
214 	}
215 }
216 
217 static void
218 print_ifclose(int indent)
219 {
220 	f_print(fout, "))\n");
221 	tabify(fout, indent);
222 	f_print(fout, "\treturn (FALSE);\n");
223 }
224 
225 static void
226 print_ifstat(int indent, char *prefix, char *type, relation rel,
227 					char *amax, char *objname, char *name)
228 {
229 	char *alt = NULL;
230 
231 	switch (rel) {
232 	case REL_POINTER:
233 		print_ifopen(indent, "pointer");
234 		print_ifarg("(char **)");
235 		f_print(fout, "%s", objname);
236 		print_ifsizeof(0, prefix, type);
237 		break;
238 	case REL_VECTOR:
239 		if (streq(type, "string"))
240 			alt = "string";
241 		else if (streq(type, "opaque"))
242 			alt = "opaque";
243 		if (alt) {
244 			print_ifopen(indent, alt);
245 			print_ifarg(objname);
246 		} else {
247 			print_ifopen(indent, "vector");
248 			print_ifarg("(char *)");
249 			f_print(fout, "%s", objname);
250 		}
251 		print_ifarg(amax);
252 		if (!alt)
253 			print_ifsizeof(indent + 1, prefix, type);
254 		break;
255 	case REL_ARRAY:
256 		if (streq(type, "string"))
257 			alt = "string";
258 		else if (streq(type, "opaque"))
259 			alt = "bytes";
260 		if (streq(type, "string")) {
261 			print_ifopen(indent, alt);
262 			print_ifarg(objname);
263 		} else {
264 			if (alt)
265 				print_ifopen(indent, alt);
266 			else
267 				print_ifopen(indent, "array");
268 			print_ifarg("(char **)");
269 			if (*objname == '&')
270 				f_print(fout, "%s.%s_val, (u_int *) %s.%s_len",
271 					objname, name, objname, name);
272 			else
273 				f_print(fout,
274 					"&%s->%s_val, (u_int *) &%s->%s_len",
275 					objname, name, objname, name);
276 		}
277 		print_ifarg(amax);
278 		if (!alt)
279 			print_ifsizeof(indent + 1, prefix, type);
280 		break;
281 	case REL_ALIAS:
282 		print_ifopen(indent, type);
283 		print_ifarg(objname);
284 		break;
285 	}
286 	print_ifclose(indent);
287 }
288 
289 /* ARGSUSED */
290 static void
291 emit_enum(definition *def)
292 {
293 	print_ifopen(1, "enum");
294 	print_ifarg("(enum_t *)objp");
295 	print_ifclose(1);
296 }
297 
298 static void
299 emit_program(definition *def)
300 {
301 	decl_list *dl;
302 	version_list *vlist;
303 	proc_list *plist;
304 
305 	for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
306 		for (plist = vlist->procs; plist != NULL; plist = plist->next) {
307 			if (!newstyle || plist->arg_num < 2)
308 				continue; /* old style, or single argument */
309 			print_prog_header(plist);
310 			for (dl = plist->args.decls; dl != NULL;
311 				dl = dl->next)
312 				print_stat(1, &dl->decl);
313 			print_trailer();
314 		}
315 }
316 
317 
318 static void
319 emit_union(definition *def)
320 {
321 	declaration *dflt;
322 	case_list *cl;
323 	declaration *cs;
324 	char *object;
325 
326 	print_stat(1, &def->def.un.enum_decl);
327 	f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
328 	for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
329 
330 		f_print(fout, "\tcase %s:\n", cl->case_name);
331 		if (cl->contflag == 1) /* a continued case statement */
332 			continue;
333 		cs = &cl->case_decl;
334 		if (!streq(cs->type, "void")) {
335 			size_t len = strlen(def->def_name) +
336 					strlen("&objp->%s_u.%s") +
337 					strlen(cs->name) + 1;
338 			object = malloc(len);
339 			if (isvectordef(cs->type, cs->rel))
340 				(void) snprintf(object, len, "objp->%s_u.%s",
341 					def->def_name, cs->name);
342 			else
343 				(void) snprintf(object, len, "&objp->%s_u.%s",
344 					def->def_name, cs->name);
345 			print_ifstat(2, cs->prefix, cs->type, cs->rel,
346 				cs->array_max, object, cs->name);
347 			free(object);
348 		}
349 		f_print(fout, "\t\tbreak;\n");
350 	}
351 	dflt = def->def.un.default_decl;
352 	if (dflt != NULL) {
353 		if (!streq(dflt->type, "void")) {
354 			size_t len = strlen(def->def_name) +
355 						strlen("&objp->%s_u.%s") +
356 						strlen(dflt->name) + 1;
357 			f_print(fout, "\tdefault:\n");
358 			object = malloc(len);
359 			if (isvectordef(dflt->type, dflt->rel))
360 				(void) snprintf(object, len, "objp->%s_u.%s",
361 					def->def_name, dflt->name);
362 			else
363 				(void) snprintf(object, len, "&objp->%s_u.%s",
364 					def->def_name, dflt->name);
365 
366 			print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
367 				    dflt->array_max, object, dflt->name);
368 			free(object);
369 			f_print(fout, "\t\tbreak;\n");
370 		}
371 	} else {
372 		f_print(fout, "\tdefault:\n");
373 		f_print(fout, "\t\treturn (FALSE);\n");
374 	}
375 
376 	f_print(fout, "\t}\n");
377 }
378 
379 static void
380 expand_inline(int indent, const char *sizestr,
381 	    int size, int flag, decl_list *dl, decl_list *cur)
382 {
383 	decl_list *psav;
384 
385 	/*
386 	 * were already looking at a xdr_inlineable structure
387 	 */
388 	tabify(fout, indent + 1);
389 	if (sizestr == NULL)
390 		f_print(fout,
391 			"buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
392 			size);
393 	else if (size == 0)
394 		f_print(fout,
395 			"buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
396 			sizestr);
397 	else
398 		f_print(fout,
399 			"buf = XDR_INLINE(xdrs, (%d + (%s)) "
400 			"* BYTES_PER_XDR_UNIT);", size, sizestr);
401 
402 	f_print(fout, "\n");
403 	tabify(fout, indent + 1);
404 	f_print(fout, "if (buf == NULL) {\n");
405 
406 	psav = cur;
407 	while (cur != dl) {
408 		print_stat(indent + 2,
409 			&cur->decl);
410 		cur = cur->next;
411 	}
412 
413 	tabify(fout, indent+1);
414 	f_print(fout, "} else {\n");
415 
416 	f_print(fout, "#if defined(_LP64) || defined(_KERNEL)\n");
417 	cur = psav;
418 	while (cur != dl) {
419 		emit_inline64(indent + 2, &cur->decl, flag);
420 		cur = cur->next;
421 	}
422 	f_print(fout, "#else\n");
423 	cur = psav;
424 	while (cur != dl) {
425 		emit_inline(indent + 2, &cur->decl, flag);
426 		cur = cur->next;
427 	}
428 	f_print(fout, "#endif\n");
429 
430 	tabify(fout, indent + 1);
431 	f_print(fout, "}\n");
432 }
433 
434 /*
435  * An inline type is a base type (interger type) or a vector of base types.
436  */
437 static int
438 inline_type(declaration *dc, int *size)
439 {
440 	bas_type *ptr;
441 
442 	*size = 0;
443 
444 	if (dc->prefix == NULL &&
445 	    (dc->rel == REL_ALIAS || dc->rel == REL_VECTOR)) {
446 		ptr = find_type(dc->type);
447 		if (ptr != NULL) {
448 			*size = ptr->length;
449 			return (1);
450 		}
451 	}
452 
453 	return (0);
454 }
455 
456 static char *
457 arraysize(char *sz, declaration *dc, int elsize)
458 {
459 	int len;
460 	int elsz = elsize;
461 	int digits;
462 	int slen = 0;
463 	char *plus = "";
464 	char *tmp;
465 	size_t tlen;
466 
467 	/*
468 	 * Calculate the size of a string to hold the size of all arrays
469 	 * to be inlined.
470 	 *
471 	 * We have the string representation of the total size that has already
472 	 * been seen. (Null if this is the first array).
473 	 * We have the string representation of array max from the declaration,
474 	 * optionally the plus string, " + ", if this is not the first array,
475 	 * and the number of digits for the element size for this declaration.
476 	 */
477 	if (sz != NULL) {
478 		plus = " + ";
479 		slen = strlen(sz);
480 	}
481 
482 	/* Calculate the number of digits to hold the element size */
483 	for (digits = 1; elsz >= 10; digits++)
484 		elsz /= 10;
485 
486 	/*
487 	 * If elsize != 1 the allocate 3 extra bytes for the times
488 	 * string, " * ", the "()" below,  and the digits. One extra
489 	 * for the trailing NULL
490 	 */
491 	len = strlen(dc->array_max) +  (elsize == 1 ? 0 : digits + 5) + 1;
492 	tlen = slen + len + strlen(plus);
493 	tmp = realloc(sz, tlen);
494 	if (tmp == NULL) {
495 		f_print(stderr, "Fatal error : no memory\n");
496 		crash();
497 	}
498 
499 	if (elsize == 1)
500 		(void) snprintf(tmp + slen, tlen - slen, "%s%s",
501 						plus, dc->array_max);
502 	else
503 		(void) snprintf(tmp + slen, tlen - slen, "%s(%s) * %d",
504 						plus, dc->array_max, elsize);
505 
506 	return (tmp);
507 }
508 
509 static void
510 inline_struct(decl_list *dl, decl_list *last, int flag, int indent)
511 {
512 	int size, tsize;
513 	decl_list *cur;
514 	char *sizestr;
515 
516 	cur = NULL;
517 	tsize = 0;
518 	sizestr = NULL;
519 	for (; dl != last; dl = dl->next) {
520 		if (inline_type(&dl->decl, &size)) {
521 			if (cur == NULL)
522 				cur = dl;
523 
524 			if (dl->decl.rel == REL_ALIAS)
525 				tsize += size;
526 			else {
527 				/* this code is required to handle arrays */
528 				sizestr = arraysize(sizestr, &dl->decl, size);
529 			}
530 		} else {
531 			if (cur != NULL)
532 				if (sizestr == NULL && tsize < inlinelen) {
533 					/*
534 					 * don't expand into inline code
535 					 * if tsize < inlinelen
536 					 */
537 					while (cur != dl) {
538 						print_stat(indent + 1,
539 							&cur->decl);
540 						cur = cur->next;
541 					}
542 				} else {
543 					expand_inline(indent, sizestr,
544 						    tsize, flag, dl, cur);
545 				}
546 			tsize = 0;
547 			cur = NULL;
548 			sizestr = NULL;
549 			print_stat(indent + 1, &dl->decl);
550 		}
551 	}
552 
553 	if (cur == NULL)
554 		return;
555 	if (sizestr == NULL && tsize < inlinelen) {
556 		/* don't expand into inline code if tsize < inlinelen */
557 		while (cur != dl) {
558 			print_stat(indent + 1, &cur->decl);
559 			cur = cur->next;
560 		}
561 	} else {
562 		expand_inline(indent, sizestr, tsize, flag, dl, cur);
563 	}
564 }
565 
566 /*
567  * Check if we can inline this structure. While we are at it check if the
568  * declaration list has any vectors defined of "basic" types.
569  */
570 static int
571 check_inline(decl_list *dl, int inlinelen, int *have_vector)
572 {
573 	int tsize = 0;
574 	int size;
575 	int doinline = 0;
576 
577 	*have_vector = 0;
578 	if (inlinelen == 0)
579 		return (0);
580 
581 	for (; dl != NULL; dl = dl->next) {
582 		if (!inline_type(&dl->decl, &size)) {
583 			tsize = 0;
584 			continue;
585 		}
586 		if (dl->decl.rel == REL_VECTOR) {
587 			*have_vector = 1;
588 			return (1);
589 		}
590 		tsize += size;
591 		if (tsize >= inlinelen)
592 			doinline = 1;
593 	}
594 
595 	return (doinline);
596 }
597 
598 
599 static void
600 emit_struct_tail_recursion(definition *defp, int can_inline)
601 {
602 	int indent = 3;
603 	struct_def *sp = &defp->def.st;
604 	decl_list *dl;
605 
606 
607 	f_print(fout, "\t%s *tmp_%s;\n",
608 		defp->def_name, defp->def_name);
609 
610 	f_print(fout, "\tbool_t more_data = TRUE;\n");
611 	f_print(fout, "\tbool_t first_objp = TRUE;\n\n");
612 
613 	f_print(fout, "\n\tif (xdrs->x_op == XDR_DECODE) {\n");
614 	f_print(fout, "\n\t\twhile (more_data) {\n");
615 	f_print(fout, "\n\t\t\tvoid bzero();\n\n");
616 
617 	if (can_inline)
618 		inline_struct(sp->decls, sp->tail, GET, indent);
619 	else
620 		for (dl = sp->decls; dl != NULL && dl != sp->tail;
621 		    dl = dl->next)
622 			print_stat(indent, &dl->decl);
623 
624 	f_print(fout, "\t\t\tif (!xdr_bool(xdrs, "
625 		"&more_data))\n\t\t\t\treturn (FALSE);\n");
626 
627 	f_print(fout, "\n\t\t\tif (!more_data) {\n");
628 	f_print(fout, "\t\t\t\tobjp->%s = NULL;\n", sp->tail->decl.name);
629 	f_print(fout, "\t\t\t\tbreak;\n");
630 	f_print(fout, "\t\t\t}\n\n");
631 	f_print(fout, "\t\t\tif (objp->%s == NULL) {\n", sp->tail->decl.name);
632 	f_print(fout, "\t\t\t\tobjp->%s = "
633 		"(%s *)\n\t\t\t\t\tmem_alloc(sizeof (%s));\n",
634 		sp->tail->decl.name, defp->def_name, defp->def_name);
635 
636 	f_print(fout, "\t\t\t\tif (objp->%s == NULL)\n"
637 		"\t\t\t\t\treturn (FALSE);\n", sp->tail->decl.name);
638 	f_print(fout, "\t\t\t\tbzero(objp->%s, sizeof (%s));\n",
639 		sp->tail->decl.name, defp->def_name);
640 	f_print(fout, "\t\t\t}\n");
641 	f_print(fout, "\t\t\tobjp = objp->%s;\n", sp->tail->decl.name);
642 	f_print(fout, "\t\t}\n");
643 
644 	f_print(fout, "\n\t} else if (xdrs->x_op == XDR_ENCODE) {\n");
645 	f_print(fout, "\n\t\twhile (more_data) {\n");
646 
647 	if (can_inline)
648 		inline_struct(sp->decls, sp->tail, PUT, indent);
649 	else
650 		for (dl = sp->decls; dl != NULL && dl != sp->tail;
651 		    dl = dl->next)
652 			print_stat(indent, &dl->decl);
653 
654 	f_print(fout, "\t\t\tobjp = objp->%s;\n", sp->tail->decl.name);
655 	f_print(fout, "\t\t\tif (objp == NULL)\n");
656 	f_print(fout, "\t\t\t\tmore_data = FALSE;\n");
657 
658 	f_print(fout, "\t\t\tif (!xdr_bool(xdrs, &more_data))\n"
659 		"\t\t\t\treturn (FALSE);\n");
660 
661 	f_print(fout, "\t\t}\n");
662 
663 	f_print(fout, "\n\t} else {\n");
664 	f_print(fout, "\n\t\twhile (more_data) {\n");
665 
666 	for (dl = sp->decls; dl != NULL && dl != sp->tail; dl = dl->next)
667 		print_stat(indent, &dl->decl);
668 
669 	f_print(fout, "\t\t\ttmp_%s = objp;\n", defp->def_name);
670 	f_print(fout, "\t\t\tobjp = objp->%s;\n", sp->tail->decl.name);
671 
672 	f_print(fout, "\t\t\tif (objp == NULL)\n");
673 	f_print(fout, "\t\t\t\tmore_data = FALSE;\n");
674 
675 	f_print(fout, "\t\t\tif (!first_objp)\n");
676 
677 	f_print(fout, "\t\t\t\tmem_free(tmp_%s, sizeof (%s));\n",
678 		defp->def_name, defp->def_name);
679 
680 	f_print(fout, "\t\t\telse\n\t\t\t\tfirst_objp = FALSE;\n\t\t}\n");
681 
682 	f_print(fout, "\n\t}\n");
683 }
684 
685 static void
686 emit_struct(definition *def)
687 {
688 	decl_list *dl = def->def.st.decls;
689 	int can_inline, have_vector;
690 
691 
692 	can_inline = check_inline(dl, inlinelen, &have_vector);
693 	if (have_vector)
694 		f_print(fout, "\tint i;\n");
695 
696 
697 	if (rflag && def->def.st.self_pointer) {
698 		/* Handle tail recursion elimination */
699 		emit_struct_tail_recursion(def, can_inline);
700 		return;
701 	}
702 
703 
704 	if (can_inline) {
705 		f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
706 		inline_struct(dl, NULL, PUT, 1);
707 
708 		f_print(fout, "\t\treturn (TRUE);\n\t}"
709 			" else if (xdrs->x_op == XDR_DECODE) {\n");
710 
711 		inline_struct(dl, NULL, GET, 1);
712 		f_print(fout, "\t\treturn (TRUE);\n\t}\n\n");
713 	}
714 
715 	/* now take care of XDR_FREE inline  case or the non-inline cases */
716 
717 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
718 		print_stat(1, &dl->decl);
719 
720 }
721 
722 static void
723 emit_typedef(definition *def)
724 {
725 	char *prefix = def->def.ty.old_prefix;
726 	char *type = def->def.ty.old_type;
727 	char *amax = def->def.ty.array_max;
728 	relation rel = def->def.ty.rel;
729 
730 	print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
731 }
732 
733 static void
734 print_stat(int indent, declaration *dec)
735 {
736 	char *prefix = dec->prefix;
737 	char *type = dec->type;
738 	char *amax = dec->array_max;
739 	relation rel = dec->rel;
740 	char name[256];
741 
742 	if (isvectordef(type, rel))
743 		(void) snprintf(name, sizeof (name), "objp->%s", dec->name);
744 	else
745 		(void) snprintf(name, sizeof (name), "&objp->%s", dec->name);
746 	print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
747 }
748 
749 
750 static void
751 emit_inline(int indent, declaration *decl, int flag)
752 {
753 	switch (decl->rel) {
754 	case  REL_ALIAS :
755 		emit_single_in_line(indent, decl, flag, REL_ALIAS);
756 		break;
757 	case REL_VECTOR :
758 		tabify(fout, indent);
759 		f_print(fout, "{\n");
760 		tabify(fout, indent + 1);
761 		f_print(fout, "%s *genp;\n\n", decl->type);
762 		tabify(fout, indent + 1);
763 		f_print(fout,
764 			"for (i = 0, genp = objp->%s;\n", decl->name);
765 		tabify(fout, indent + 2);
766 		f_print(fout, "i < %s; i++) {\n", decl->array_max);
767 		emit_single_in_line(indent + 2, decl, flag, REL_VECTOR);
768 		tabify(fout, indent + 1);
769 		f_print(fout, "}\n");
770 		tabify(fout, indent);
771 		f_print(fout, "}\n");
772 	}
773 }
774 
775 static void
776 emit_inline64(int indent, declaration *decl, int flag)
777 {
778 	switch (decl->rel) {
779 	case  REL_ALIAS :
780 		emit_single_in_line64(indent, decl, flag, REL_ALIAS);
781 		break;
782 	case REL_VECTOR :
783 		tabify(fout, indent);
784 		f_print(fout, "{\n");
785 		tabify(fout, indent + 1);
786 		f_print(fout, "%s *genp;\n\n", decl->type);
787 		tabify(fout, indent + 1);
788 		f_print(fout,
789 			"for (i = 0, genp = objp->%s;\n", decl->name);
790 		tabify(fout, indent + 2);
791 		f_print(fout, "i < %s; i++) {\n", decl->array_max);
792 		emit_single_in_line64(indent + 2, decl, flag, REL_VECTOR);
793 		tabify(fout, indent + 1);
794 		f_print(fout, "}\n");
795 		tabify(fout, indent);
796 		f_print(fout, "}\n");
797 	}
798 }
799 
800 static void
801 emit_single_in_line(int indent, declaration *decl, int flag, relation rel)
802 {
803 	char *upp_case;
804 	int freed = 0;
805 
806 	tabify(fout, indent);
807 	if (flag == PUT)
808 		f_print(fout, "IXDR_PUT_");
809 	else
810 		if (rel == REL_ALIAS)
811 			f_print(fout, "objp->%s = IXDR_GET_", decl->name);
812 		else
813 			f_print(fout, "*genp++ = IXDR_GET_");
814 
815 	upp_case = upcase(decl->type);
816 
817 	/* hack	 - XX */
818 	if (strcmp(upp_case, "INT") == 0) {
819 		free(upp_case);
820 		freed = 1;
821 		upp_case = "LONG";
822 	}
823 	if ((strcmp(upp_case, "U_INT") == 0) ||
824 			(strcmp(upp_case, "RPCPROG") == 0) ||
825 			(strcmp(upp_case, "RPCVERS") == 0) ||
826 			(strcmp(upp_case, "RPCPROC") == 0) ||
827 			(strcmp(upp_case, "RPCPROT") == 0) ||
828 			(strcmp(upp_case, "RPCPORT") == 0)) {
829 		free(upp_case);
830 		freed = 1;
831 		upp_case = "U_LONG";
832 	}
833 
834 	if (flag == PUT)
835 		if (rel == REL_ALIAS)
836 			f_print(fout,
837 				"%s(buf, objp->%s);\n", upp_case, decl->name);
838 		else
839 			f_print(fout, "%s(buf, *genp++);\n", upp_case);
840 
841 	else
842 		f_print(fout, "%s(buf);\n", upp_case);
843 	if (!freed)
844 		free(upp_case);
845 }
846 
847 static void
848 emit_single_in_line64(int indent, declaration *decl, int flag, relation rel)
849 {
850 	char *upp_case;
851 	int freed = 0;
852 
853 	tabify(fout, indent);
854 	if (flag == PUT)
855 		f_print(fout, "IXDR_PUT_");
856 	else
857 		if (rel == REL_ALIAS)
858 			f_print(fout, "objp->%s = IXDR_GET_", decl->name);
859 		else
860 			f_print(fout, "*genp++ = IXDR_GET_");
861 
862 	upp_case = upcase(decl->type);
863 
864 	/* hack	 - XX */
865 	if ((strcmp(upp_case, "INT") == 0)||(strcmp(upp_case, "LONG") == 0)) {
866 		free(upp_case);
867 		freed = 1;
868 		upp_case = "INT32";
869 	}
870 	if ((strcmp(upp_case, "U_INT") == 0) ||
871 			(strcmp(upp_case, "U_LONG") == 0) ||
872 			(strcmp(upp_case, "RPCPROG") == 0) ||
873 			(strcmp(upp_case, "RPCVERS") == 0) ||
874 			(strcmp(upp_case, "RPCPROC") == 0) ||
875 			(strcmp(upp_case, "RPCPROT") == 0) ||
876 			(strcmp(upp_case, "RPCPORT") == 0)) {
877 		free(upp_case);
878 		freed = 1;
879 		upp_case = "U_INT32";
880 	}
881 
882 	if (flag == PUT)
883 		if (rel == REL_ALIAS)
884 			f_print(fout,
885 				"%s(buf, objp->%s);\n", upp_case, decl->name);
886 		else
887 			f_print(fout, "%s(buf, *genp++);\n", upp_case);
888 
889 	else
890 		f_print(fout, "%s(buf);\n", upp_case);
891 	if (!freed)
892 		free(upp_case);
893 }
894 
895 static char *
896 upcase(char *str)
897 {
898 	char *ptr, *hptr;
899 
900 	ptr = malloc(strlen(str)+1);
901 	if (ptr == NULL) {
902 		f_print(stderr, "malloc failed\n");
903 		exit(1);
904 	};
905 
906 	hptr = ptr;
907 	while (*str != '\0')
908 		*ptr++ = toupper(*str++);
909 
910 	*ptr = '\0';
911 	return (hptr);
912 }
913