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