xref: /illumos-gate/usr/src/cmd/rpcgen/rpc_cout.c (revision 9f3a0b43)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a2f144d1SJordan Brown  * Common Development and Distribution License (the "License").
6a2f144d1SJordan Brown  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2161961e0fSrobinson 
227c478bd9Sstevel@tonic-gate /*
23a2f144d1SJordan Brown  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
307c478bd9Sstevel@tonic-gate  * The Regents of the University of California
317c478bd9Sstevel@tonic-gate  * All Rights Reserved
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
347c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
357c478bd9Sstevel@tonic-gate  * contributors.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate #include <stdio.h>
4261961e0fSrobinson #include <stdlib.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
4461961e0fSrobinson #include <ctype.h>
457c478bd9Sstevel@tonic-gate #include "rpc_parse.h"
467c478bd9Sstevel@tonic-gate #include "rpc_util.h"
477c478bd9Sstevel@tonic-gate 
4861961e0fSrobinson extern void crash(void);
4961961e0fSrobinson 
5061961e0fSrobinson static void print_header(definition *);
5161961e0fSrobinson static void print_trailer(void);
5261961e0fSrobinson static void emit_enum(definition *);
5361961e0fSrobinson static void emit_program(definition *);
5461961e0fSrobinson static void emit_union(definition *);
5561961e0fSrobinson static void emit_struct(definition *);
5661961e0fSrobinson static void emit_typedef(definition *);
5761961e0fSrobinson static void print_stat(int, declaration *);
5861961e0fSrobinson static void emit_inline(int, declaration *, int);
5961961e0fSrobinson static void emit_inline64(int, declaration *, int);
6061961e0fSrobinson static void emit_single_in_line(int, declaration *, int, relation);
6161961e0fSrobinson static void emit_single_in_line64(int, declaration *, int, relation);
6261961e0fSrobinson static char *upcase(char *);
6361961e0fSrobinson 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * Emit the C-routine for the given definition
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate void
emit(definition * def)6861961e0fSrobinson emit(definition *def)
697c478bd9Sstevel@tonic-gate {
7061961e0fSrobinson 	if (def->def_kind == DEF_CONST)
717c478bd9Sstevel@tonic-gate 		return;
727c478bd9Sstevel@tonic-gate 	if (def->def_kind == DEF_PROGRAM) {
737c478bd9Sstevel@tonic-gate 		emit_program(def);
747c478bd9Sstevel@tonic-gate 		return;
757c478bd9Sstevel@tonic-gate 	}
767c478bd9Sstevel@tonic-gate 	if (def->def_kind == DEF_TYPEDEF) {
777c478bd9Sstevel@tonic-gate 		/*
787c478bd9Sstevel@tonic-gate 		 * now we need to handle declarations like
797c478bd9Sstevel@tonic-gate 		 * struct typedef foo foo;
807c478bd9Sstevel@tonic-gate 		 * since we dont want this to be expanded into 2 calls
817c478bd9Sstevel@tonic-gate 		 * to xdr_foo
827c478bd9Sstevel@tonic-gate 		 */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 		if (strcmp(def->def.ty.old_type, def->def_name) == 0)
857c478bd9Sstevel@tonic-gate 			return;
867c478bd9Sstevel@tonic-gate 	};
877c478bd9Sstevel@tonic-gate 	print_header(def);
887c478bd9Sstevel@tonic-gate 	switch (def->def_kind) {
897c478bd9Sstevel@tonic-gate 	case DEF_UNION:
907c478bd9Sstevel@tonic-gate 		emit_union(def);
917c478bd9Sstevel@tonic-gate 		break;
927c478bd9Sstevel@tonic-gate 	case DEF_ENUM:
937c478bd9Sstevel@tonic-gate 		emit_enum(def);
947c478bd9Sstevel@tonic-gate 		break;
957c478bd9Sstevel@tonic-gate 	case DEF_STRUCT:
967c478bd9Sstevel@tonic-gate 		emit_struct(def);
977c478bd9Sstevel@tonic-gate 		break;
987c478bd9Sstevel@tonic-gate 	case DEF_TYPEDEF:
997c478bd9Sstevel@tonic-gate 		emit_typedef(def);
1007c478bd9Sstevel@tonic-gate 		break;
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 	print_trailer();
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
10561961e0fSrobinson static int
findtype(definition * def,char * type)10661961e0fSrobinson findtype(definition *def, char *type)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 
10961961e0fSrobinson 	if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST)
1107c478bd9Sstevel@tonic-gate 		return (0);
11161961e0fSrobinson 	return (streq(def->def_name, type));
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate 
11461961e0fSrobinson static int
undefined(char * type)11561961e0fSrobinson undefined(char *type)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	definition *def;
1187c478bd9Sstevel@tonic-gate 
11961961e0fSrobinson 	def = (definition *)FINDVAL(defined, type, findtype);
1207c478bd9Sstevel@tonic-gate 	return (def == NULL);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 
12461961e0fSrobinson static void
print_generic_header(char * procname,int pointerp)12561961e0fSrobinson print_generic_header(char *procname, int pointerp)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
1287c478bd9Sstevel@tonic-gate 	f_print(fout, "bool_t\n");
1297c478bd9Sstevel@tonic-gate 	if (Cflag) {
13061961e0fSrobinson 		f_print(fout, "xdr_%s(", procname);
13161961e0fSrobinson 		f_print(fout, "XDR *xdrs, ");
13261961e0fSrobinson 		f_print(fout, "%s ", procname);
13361961e0fSrobinson 		if (pointerp)
13461961e0fSrobinson 			f_print(fout, "*");
13561961e0fSrobinson 		f_print(fout, "objp)\n{\n\n");
1367c478bd9Sstevel@tonic-gate 	} else {
13761961e0fSrobinson 		f_print(fout, "xdr_%s(xdrs, objp)\n", procname);
13861961e0fSrobinson 		f_print(fout, "\tXDR *xdrs;\n");
13961961e0fSrobinson 		f_print(fout, "\t%s ", procname);
14061961e0fSrobinson 		if (pointerp)
14161961e0fSrobinson 			f_print(fout, "*");
14261961e0fSrobinson 		f_print(fout, "objp;\n{\n\n");
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
14661961e0fSrobinson static void
print_header(definition * def)14761961e0fSrobinson print_header(definition *def)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	print_generic_header(def->def_name,
150a2f144d1SJordan Brown 	    def->def_kind != DEF_TYPEDEF ||
151a2f144d1SJordan Brown 	    !isvectordef(def->def.ty.old_type, def->def.ty.rel));
1527c478bd9Sstevel@tonic-gate 	/* Now add Inline support */
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	if (inlinelen == 0)
1557c478bd9Sstevel@tonic-gate 		return;
156*9f3a0b43SToomas Soome 	f_print(fout, "\trpc_inline_t *buf __unused;\n\n");
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
15961961e0fSrobinson static void
print_prog_header(proc_list * plist)16061961e0fSrobinson print_prog_header(proc_list *plist)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	print_generic_header(plist->args.argname, 1);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
16561961e0fSrobinson static void
print_trailer(void)16661961e0fSrobinson print_trailer(void)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	f_print(fout, "\treturn (TRUE);\n");
1697c478bd9Sstevel@tonic-gate 	f_print(fout, "}\n");
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 
17361961e0fSrobinson static void
print_ifopen(int indent,char * name)17461961e0fSrobinson print_ifopen(int indent, char *name)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	tabify(fout, indent);
1777c478bd9Sstevel@tonic-gate 	if (streq(name, "rpcprog_t") ||
178a2f144d1SJordan Brown 	    streq(name, "rpcvers_t") ||
179a2f144d1SJordan Brown 	    streq(name, "rpcproc_t") ||
180a2f144d1SJordan Brown 	    streq(name, "rpcprot_t") ||
181a2f144d1SJordan Brown 	    streq(name, "rpcport_t"))
18261961e0fSrobinson 		(void) strtok(name, "_");
1837c478bd9Sstevel@tonic-gate 	f_print(fout, "if (!xdr_%s(xdrs", name);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
18661961e0fSrobinson static void
print_ifarg(char * arg)18761961e0fSrobinson print_ifarg(char *arg)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	f_print(fout, ", %s", arg);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
19261961e0fSrobinson static void
print_ifsizeof(int indent,char * prefix,char * type)19361961e0fSrobinson print_ifsizeof(int indent, char *prefix, char *type)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	if (indent) {
1967c478bd9Sstevel@tonic-gate 		f_print(fout, ",\n");
1977c478bd9Sstevel@tonic-gate 		tabify(fout, indent);
19861961e0fSrobinson 	} else {
1997c478bd9Sstevel@tonic-gate 		f_print(fout, ", ");
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 	if (streq(type, "bool")) {
20261961e0fSrobinson 		f_print(fout, "sizeof (bool_t), (xdrproc_t)xdr_bool");
2037c478bd9Sstevel@tonic-gate 	} else {
2047c478bd9Sstevel@tonic-gate 		f_print(fout, "sizeof (");
2057c478bd9Sstevel@tonic-gate 		if (undefined(type) && prefix) {
2067c478bd9Sstevel@tonic-gate 			f_print(fout, "%s ", prefix);
2077c478bd9Sstevel@tonic-gate 		}
20861961e0fSrobinson 		f_print(fout, "%s), (xdrproc_t)xdr_%s", type, type);
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate 
21261961e0fSrobinson static void
print_ifclose(int indent)21361961e0fSrobinson print_ifclose(int indent)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	f_print(fout, "))\n");
2167c478bd9Sstevel@tonic-gate 	tabify(fout, indent);
2177c478bd9Sstevel@tonic-gate 	f_print(fout, "\treturn (FALSE);\n");
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate 
22061961e0fSrobinson static void
print_ifstat(int indent,char * prefix,char * type,relation rel,char * amax,char * objname,char * name)22161961e0fSrobinson print_ifstat(int indent, char *prefix, char *type, relation rel,
222*9f3a0b43SToomas Soome     char *amax, char *objname, char *name)
2237c478bd9Sstevel@tonic-gate {
2247c478bd9Sstevel@tonic-gate 	char *alt = NULL;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	switch (rel) {
2277c478bd9Sstevel@tonic-gate 	case REL_POINTER:
2287c478bd9Sstevel@tonic-gate 		print_ifopen(indent, "pointer");
2297c478bd9Sstevel@tonic-gate 		print_ifarg("(char **)");
2307c478bd9Sstevel@tonic-gate 		f_print(fout, "%s", objname);
2317c478bd9Sstevel@tonic-gate 		print_ifsizeof(0, prefix, type);
2327c478bd9Sstevel@tonic-gate 		break;
2337c478bd9Sstevel@tonic-gate 	case REL_VECTOR:
23461961e0fSrobinson 		if (streq(type, "string"))
2357c478bd9Sstevel@tonic-gate 			alt = "string";
23661961e0fSrobinson 		else if (streq(type, "opaque"))
2377c478bd9Sstevel@tonic-gate 			alt = "opaque";
2387c478bd9Sstevel@tonic-gate 		if (alt) {
2397c478bd9Sstevel@tonic-gate 			print_ifopen(indent, alt);
2407c478bd9Sstevel@tonic-gate 			print_ifarg(objname);
2417c478bd9Sstevel@tonic-gate 		} else {
2427c478bd9Sstevel@tonic-gate 			print_ifopen(indent, "vector");
2437c478bd9Sstevel@tonic-gate 			print_ifarg("(char *)");
2447c478bd9Sstevel@tonic-gate 			f_print(fout, "%s", objname);
2457c478bd9Sstevel@tonic-gate 		}
2467c478bd9Sstevel@tonic-gate 		print_ifarg(amax);
24761961e0fSrobinson 		if (!alt)
2487c478bd9Sstevel@tonic-gate 			print_ifsizeof(indent + 1, prefix, type);
2497c478bd9Sstevel@tonic-gate 		break;
2507c478bd9Sstevel@tonic-gate 	case REL_ARRAY:
25161961e0fSrobinson 		if (streq(type, "string"))
2527c478bd9Sstevel@tonic-gate 			alt = "string";
25361961e0fSrobinson 		else if (streq(type, "opaque"))
2547c478bd9Sstevel@tonic-gate 			alt = "bytes";
2557c478bd9Sstevel@tonic-gate 		if (streq(type, "string")) {
2567c478bd9Sstevel@tonic-gate 			print_ifopen(indent, alt);
2577c478bd9Sstevel@tonic-gate 			print_ifarg(objname);
2587c478bd9Sstevel@tonic-gate 		} else {
25961961e0fSrobinson 			if (alt)
2607c478bd9Sstevel@tonic-gate 				print_ifopen(indent, alt);
26161961e0fSrobinson 			else
2627c478bd9Sstevel@tonic-gate 				print_ifopen(indent, "array");
2637c478bd9Sstevel@tonic-gate 			print_ifarg("(char **)");
26461961e0fSrobinson 			if (*objname == '&')
2657c478bd9Sstevel@tonic-gate 				f_print(fout, "%s.%s_val, (u_int *) %s.%s_len",
266a2f144d1SJordan Brown 				    objname, name, objname, name);
26761961e0fSrobinson 			else
2687c478bd9Sstevel@tonic-gate 				f_print(fout,
269a2f144d1SJordan Brown 				    "&%s->%s_val, (u_int *) &%s->%s_len",
270a2f144d1SJordan Brown 				    objname, name, objname, name);
2717c478bd9Sstevel@tonic-gate 		}
2727c478bd9Sstevel@tonic-gate 		print_ifarg(amax);
27361961e0fSrobinson 		if (!alt)
2747c478bd9Sstevel@tonic-gate 			print_ifsizeof(indent + 1, prefix, type);
2757c478bd9Sstevel@tonic-gate 		break;
2767c478bd9Sstevel@tonic-gate 	case REL_ALIAS:
2777c478bd9Sstevel@tonic-gate 		print_ifopen(indent, type);
2787c478bd9Sstevel@tonic-gate 		print_ifarg(objname);
2797c478bd9Sstevel@tonic-gate 		break;
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 	print_ifclose(indent);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate /* ARGSUSED */
28561961e0fSrobinson static void
emit_enum(definition * def)28661961e0fSrobinson emit_enum(definition *def)
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate 	print_ifopen(1, "enum");
2897c478bd9Sstevel@tonic-gate 	print_ifarg("(enum_t *)objp");
2907c478bd9Sstevel@tonic-gate 	print_ifclose(1);
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate 
29361961e0fSrobinson static void
emit_program(definition * def)29461961e0fSrobinson emit_program(definition *def)
2957c478bd9Sstevel@tonic-gate {
2967c478bd9Sstevel@tonic-gate 	decl_list *dl;
2977c478bd9Sstevel@tonic-gate 	version_list *vlist;
2987c478bd9Sstevel@tonic-gate 	proc_list *plist;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
3017c478bd9Sstevel@tonic-gate 		for (plist = vlist->procs; plist != NULL; plist = plist->next) {
3027c478bd9Sstevel@tonic-gate 			if (!newstyle || plist->arg_num < 2)
3037c478bd9Sstevel@tonic-gate 				continue; /* old style, or single argument */
3047c478bd9Sstevel@tonic-gate 			print_prog_header(plist);
3057c478bd9Sstevel@tonic-gate 			for (dl = plist->args.decls; dl != NULL;
306a2f144d1SJordan Brown 			    dl = dl->next)
3077c478bd9Sstevel@tonic-gate 				print_stat(1, &dl->decl);
3087c478bd9Sstevel@tonic-gate 			print_trailer();
3097c478bd9Sstevel@tonic-gate 		}
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 
31361961e0fSrobinson static void
emit_union(definition * def)31461961e0fSrobinson emit_union(definition *def)
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate 	declaration *dflt;
3177c478bd9Sstevel@tonic-gate 	case_list *cl;
3187c478bd9Sstevel@tonic-gate 	declaration *cs;
3197c478bd9Sstevel@tonic-gate 	char *object;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	print_stat(1, &def->def.un.enum_decl);
3227c478bd9Sstevel@tonic-gate 	f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
3237c478bd9Sstevel@tonic-gate 	for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 		f_print(fout, "\tcase %s:\n", cl->case_name);
3267c478bd9Sstevel@tonic-gate 		if (cl->contflag == 1) /* a continued case statement */
3277c478bd9Sstevel@tonic-gate 			continue;
3287c478bd9Sstevel@tonic-gate 		cs = &cl->case_decl;
3297c478bd9Sstevel@tonic-gate 		if (!streq(cs->type, "void")) {
33061961e0fSrobinson 			size_t len = strlen(def->def_name) +
331a2f144d1SJordan Brown 			    strlen("&objp->%s_u.%s") +
332a2f144d1SJordan Brown 			    strlen(cs->name) + 1;
33361961e0fSrobinson 			object = malloc(len);
33461961e0fSrobinson 			if (isvectordef(cs->type, cs->rel))
33561961e0fSrobinson 				(void) snprintf(object, len, "objp->%s_u.%s",
336a2f144d1SJordan Brown 				    def->def_name, cs->name);
33761961e0fSrobinson 			else
33861961e0fSrobinson 				(void) snprintf(object, len, "&objp->%s_u.%s",
339a2f144d1SJordan Brown 				    def->def_name, cs->name);
3407c478bd9Sstevel@tonic-gate 			print_ifstat(2, cs->prefix, cs->type, cs->rel,
341a2f144d1SJordan Brown 			    cs->array_max, object, cs->name);
3427c478bd9Sstevel@tonic-gate 			free(object);
3437c478bd9Sstevel@tonic-gate 		}
3447c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tbreak;\n");
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate 	dflt = def->def.un.default_decl;
3477c478bd9Sstevel@tonic-gate 	if (dflt != NULL) {
3487c478bd9Sstevel@tonic-gate 		if (!streq(dflt->type, "void")) {
34961961e0fSrobinson 			size_t len = strlen(def->def_name) +
350a2f144d1SJordan Brown 			    strlen("&objp->%s_u.%s") +
351a2f144d1SJordan Brown 			    strlen(dflt->name) + 1;
3527c478bd9Sstevel@tonic-gate 			f_print(fout, "\tdefault:\n");
35361961e0fSrobinson 			object = malloc(len);
35461961e0fSrobinson 			if (isvectordef(dflt->type, dflt->rel))
35561961e0fSrobinson 				(void) snprintf(object, len, "objp->%s_u.%s",
356a2f144d1SJordan Brown 				    def->def_name, dflt->name);
35761961e0fSrobinson 			else
35861961e0fSrobinson 				(void) snprintf(object, len, "&objp->%s_u.%s",
359a2f144d1SJordan Brown 				    def->def_name, dflt->name);
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 			print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
362a2f144d1SJordan Brown 			    dflt->array_max, object, dflt->name);
3637c478bd9Sstevel@tonic-gate 			free(object);
3647c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\tbreak;\n");
365*9f3a0b43SToomas Soome 		} else {
366*9f3a0b43SToomas Soome 			f_print(fout, "\tdefault:\n");
367*9f3a0b43SToomas Soome 			f_print(fout, "\t\tbreak;\n");
3687c478bd9Sstevel@tonic-gate 		}
3697c478bd9Sstevel@tonic-gate 	} else {
3707c478bd9Sstevel@tonic-gate 		f_print(fout, "\tdefault:\n");
3717c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\treturn (FALSE);\n");
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	f_print(fout, "\t}\n");
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate static void
expand_inline(int indent,const char * sizestr,int size,int flag,decl_list * dl,decl_list * cur)3787c478bd9Sstevel@tonic-gate expand_inline(int indent, const char *sizestr,
379a2f144d1SJordan Brown     int size, int flag, decl_list *dl, decl_list *cur)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	decl_list *psav;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	/*
3847c478bd9Sstevel@tonic-gate 	 * were already looking at a xdr_inlineable structure
3857c478bd9Sstevel@tonic-gate 	 */
3867c478bd9Sstevel@tonic-gate 	tabify(fout, indent + 1);
3877c478bd9Sstevel@tonic-gate 	if (sizestr == NULL)
3887c478bd9Sstevel@tonic-gate 		f_print(fout,
389a2f144d1SJordan Brown 		    "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
390a2f144d1SJordan Brown 		    size);
3917c478bd9Sstevel@tonic-gate 	else if (size == 0)
3927c478bd9Sstevel@tonic-gate 		f_print(fout,
393a2f144d1SJordan Brown 		    "buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
394a2f144d1SJordan Brown 		    sizestr);
3957c478bd9Sstevel@tonic-gate 	else
3967c478bd9Sstevel@tonic-gate 		f_print(fout,
397a2f144d1SJordan Brown 		    "buf = XDR_INLINE(xdrs, (%d + (%s)) "
398a2f144d1SJordan Brown 		    "* BYTES_PER_XDR_UNIT);", size, sizestr);
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
4017c478bd9Sstevel@tonic-gate 	tabify(fout, indent + 1);
4027c478bd9Sstevel@tonic-gate 	f_print(fout, "if (buf == NULL) {\n");
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	psav = cur;
4057c478bd9Sstevel@tonic-gate 	while (cur != dl) {
4067c478bd9Sstevel@tonic-gate 		print_stat(indent + 2,
407a2f144d1SJordan Brown 		    &cur->decl);
4087c478bd9Sstevel@tonic-gate 		cur = cur->next;
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	tabify(fout, indent+1);
4127c478bd9Sstevel@tonic-gate 	f_print(fout, "} else {\n");
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	f_print(fout, "#if defined(_LP64) || defined(_KERNEL)\n");
4157c478bd9Sstevel@tonic-gate 	cur = psav;
4167c478bd9Sstevel@tonic-gate 	while (cur != dl) {
4177c478bd9Sstevel@tonic-gate 		emit_inline64(indent + 2, &cur->decl, flag);
4187c478bd9Sstevel@tonic-gate 		cur = cur->next;
4197c478bd9Sstevel@tonic-gate 	}
4207c478bd9Sstevel@tonic-gate 	f_print(fout, "#else\n");
4217c478bd9Sstevel@tonic-gate 	cur = psav;
4227c478bd9Sstevel@tonic-gate 	while (cur != dl) {
4237c478bd9Sstevel@tonic-gate 		emit_inline(indent + 2, &cur->decl, flag);
4247c478bd9Sstevel@tonic-gate 		cur = cur->next;
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 	f_print(fout, "#endif\n");
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	tabify(fout, indent + 1);
4297c478bd9Sstevel@tonic-gate 	f_print(fout, "}\n");
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate /*
4337c478bd9Sstevel@tonic-gate  * An inline type is a base type (interger type) or a vector of base types.
4347c478bd9Sstevel@tonic-gate  */
4357c478bd9Sstevel@tonic-gate static int
inline_type(declaration * dc,int * size)4367c478bd9Sstevel@tonic-gate inline_type(declaration *dc, int *size)
4377c478bd9Sstevel@tonic-gate {
4387c478bd9Sstevel@tonic-gate 	bas_type *ptr;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	*size = 0;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	if (dc->prefix == NULL &&
4437c478bd9Sstevel@tonic-gate 	    (dc->rel == REL_ALIAS || dc->rel == REL_VECTOR)) {
4447c478bd9Sstevel@tonic-gate 		ptr = find_type(dc->type);
4457c478bd9Sstevel@tonic-gate 		if (ptr != NULL) {
4467c478bd9Sstevel@tonic-gate 			*size = ptr->length;
4477c478bd9Sstevel@tonic-gate 			return (1);
4487c478bd9Sstevel@tonic-gate 		}
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	return (0);
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate static char *
arraysize(char * sz,declaration * dc,int elsize)4557c478bd9Sstevel@tonic-gate arraysize(char *sz, declaration *dc, int elsize)
4567c478bd9Sstevel@tonic-gate {
4577c478bd9Sstevel@tonic-gate 	int len;
4587c478bd9Sstevel@tonic-gate 	int elsz = elsize;
4597c478bd9Sstevel@tonic-gate 	int digits;
4607c478bd9Sstevel@tonic-gate 	int slen = 0;
4617c478bd9Sstevel@tonic-gate 	char *plus = "";
4627c478bd9Sstevel@tonic-gate 	char *tmp;
46361961e0fSrobinson 	size_t tlen;
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	/*
4667c478bd9Sstevel@tonic-gate 	 * Calculate the size of a string to hold the size of all arrays
4677c478bd9Sstevel@tonic-gate 	 * to be inlined.
4687c478bd9Sstevel@tonic-gate 	 *
4697c478bd9Sstevel@tonic-gate 	 * We have the string representation of the total size that has already
4707c478bd9Sstevel@tonic-gate 	 * been seen. (Null if this is the first array).
4717c478bd9Sstevel@tonic-gate 	 * We have the string representation of array max from the declaration,
4727c478bd9Sstevel@tonic-gate 	 * optionally the plus string, " + ", if this is not the first array,
4737c478bd9Sstevel@tonic-gate 	 * and the number of digits for the element size for this declaration.
4747c478bd9Sstevel@tonic-gate 	 */
4757c478bd9Sstevel@tonic-gate 	if (sz != NULL) {
4767c478bd9Sstevel@tonic-gate 		plus = " + ";
4777c478bd9Sstevel@tonic-gate 		slen = strlen(sz);
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	/* Calculate the number of digits to hold the element size */
4817c478bd9Sstevel@tonic-gate 	for (digits = 1; elsz >= 10; digits++)
4827c478bd9Sstevel@tonic-gate 		elsz /= 10;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	/*
4857c478bd9Sstevel@tonic-gate 	 * If elsize != 1 the allocate 3 extra bytes for the times
4867c478bd9Sstevel@tonic-gate 	 * string, " * ", the "()" below,  and the digits. One extra
4877c478bd9Sstevel@tonic-gate 	 * for the trailing NULL
4887c478bd9Sstevel@tonic-gate 	 */
4897c478bd9Sstevel@tonic-gate 	len = strlen(dc->array_max) +  (elsize == 1 ? 0 : digits + 5) + 1;
49061961e0fSrobinson 	tlen = slen + len + strlen(plus);
49161961e0fSrobinson 	tmp = realloc(sz, tlen);
4927c478bd9Sstevel@tonic-gate 	if (tmp == NULL) {
4937c478bd9Sstevel@tonic-gate 		f_print(stderr, "Fatal error : no memory\n");
4947c478bd9Sstevel@tonic-gate 		crash();
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	if (elsize == 1)
49861961e0fSrobinson 		(void) snprintf(tmp + slen, tlen - slen, "%s%s",
499a2f144d1SJordan Brown 		    plus, dc->array_max);
5007c478bd9Sstevel@tonic-gate 	else
50161961e0fSrobinson 		(void) snprintf(tmp + slen, tlen - slen, "%s(%s) * %d",
502a2f144d1SJordan Brown 		    plus, dc->array_max, elsize);
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	return (tmp);
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate static void
inline_struct(decl_list * dl,decl_list * last,int flag,int indent)5087c478bd9Sstevel@tonic-gate inline_struct(decl_list *dl, decl_list *last, int flag, int indent)
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate 	int size, tsize;
51161961e0fSrobinson 	decl_list *cur;
51261961e0fSrobinson 	char *sizestr;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	cur = NULL;
5157c478bd9Sstevel@tonic-gate 	tsize = 0;
5167c478bd9Sstevel@tonic-gate 	sizestr = NULL;
5177c478bd9Sstevel@tonic-gate 	for (; dl != last; dl = dl->next) {
5187c478bd9Sstevel@tonic-gate 		if (inline_type(&dl->decl, &size)) {
5197c478bd9Sstevel@tonic-gate 			if (cur == NULL)
5207c478bd9Sstevel@tonic-gate 				cur = dl;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 			if (dl->decl.rel == REL_ALIAS)
5237c478bd9Sstevel@tonic-gate 				tsize += size;
5247c478bd9Sstevel@tonic-gate 			else {
5257c478bd9Sstevel@tonic-gate 				/* this code is required to handle arrays */
5267c478bd9Sstevel@tonic-gate 				sizestr = arraysize(sizestr, &dl->decl, size);
5277c478bd9Sstevel@tonic-gate 			}
5287c478bd9Sstevel@tonic-gate 		} else {
5297c478bd9Sstevel@tonic-gate 			if (cur != NULL)
5307c478bd9Sstevel@tonic-gate 				if (sizestr == NULL && tsize < inlinelen) {
5317c478bd9Sstevel@tonic-gate 					/*
5327c478bd9Sstevel@tonic-gate 					 * don't expand into inline code
5337c478bd9Sstevel@tonic-gate 					 * if tsize < inlinelen
5347c478bd9Sstevel@tonic-gate 					 */
5357c478bd9Sstevel@tonic-gate 					while (cur != dl) {
5367c478bd9Sstevel@tonic-gate 						print_stat(indent + 1,
537a2f144d1SJordan Brown 						    &cur->decl);
5387c478bd9Sstevel@tonic-gate 						cur = cur->next;
5397c478bd9Sstevel@tonic-gate 					}
5407c478bd9Sstevel@tonic-gate 				} else {
5417c478bd9Sstevel@tonic-gate 					expand_inline(indent, sizestr,
542a2f144d1SJordan Brown 					    tsize, flag, dl, cur);
5437c478bd9Sstevel@tonic-gate 				}
5447c478bd9Sstevel@tonic-gate 			tsize = 0;
5457c478bd9Sstevel@tonic-gate 			cur = NULL;
5467c478bd9Sstevel@tonic-gate 			sizestr = NULL;
5477c478bd9Sstevel@tonic-gate 			print_stat(indent + 1, &dl->decl);
5487c478bd9Sstevel@tonic-gate 		}
5497c478bd9Sstevel@tonic-gate 	}
5507c478bd9Sstevel@tonic-gate 
55161961e0fSrobinson 	if (cur == NULL)
55261961e0fSrobinson 		return;
55361961e0fSrobinson 	if (sizestr == NULL && tsize < inlinelen) {
55461961e0fSrobinson 		/* don't expand into inline code if tsize < inlinelen */
55561961e0fSrobinson 		while (cur != dl) {
55661961e0fSrobinson 			print_stat(indent + 1, &cur->decl);
55761961e0fSrobinson 			cur = cur->next;
5587c478bd9Sstevel@tonic-gate 		}
55961961e0fSrobinson 	} else {
56061961e0fSrobinson 		expand_inline(indent, sizestr, tsize, flag, dl, cur);
56161961e0fSrobinson 	}
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate /*
5657c478bd9Sstevel@tonic-gate  * Check if we can inline this structure. While we are at it check if the
5667c478bd9Sstevel@tonic-gate  * declaration list has any vectors defined of "basic" types.
5677c478bd9Sstevel@tonic-gate  */
5687c478bd9Sstevel@tonic-gate static int
check_inline(decl_list * dl,int inlinelen,int * have_vector)5697c478bd9Sstevel@tonic-gate check_inline(decl_list *dl, int inlinelen, int *have_vector)
5707c478bd9Sstevel@tonic-gate {
5717c478bd9Sstevel@tonic-gate 	int tsize = 0;
5727c478bd9Sstevel@tonic-gate 	int size;
5737c478bd9Sstevel@tonic-gate 	int doinline = 0;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	*have_vector = 0;
5767c478bd9Sstevel@tonic-gate 	if (inlinelen == 0)
5777c478bd9Sstevel@tonic-gate 		return (0);
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	for (; dl != NULL; dl = dl->next) {
58061961e0fSrobinson 		if (!inline_type(&dl->decl, &size)) {
5817c478bd9Sstevel@tonic-gate 			tsize = 0;
58261961e0fSrobinson 			continue;
58361961e0fSrobinson 		}
58461961e0fSrobinson 		if (dl->decl.rel == REL_VECTOR) {
58561961e0fSrobinson 			*have_vector = 1;
58661961e0fSrobinson 			return (1);
5877c478bd9Sstevel@tonic-gate 		}
58861961e0fSrobinson 		tsize += size;
58961961e0fSrobinson 		if (tsize >= inlinelen)
59061961e0fSrobinson 			doinline = 1;
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	return (doinline);
5947c478bd9Sstevel@tonic-gate }
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate static void
emit_struct_tail_recursion(definition * defp,int can_inline)5987c478bd9Sstevel@tonic-gate emit_struct_tail_recursion(definition *defp, int can_inline)
5997c478bd9Sstevel@tonic-gate {
6007c478bd9Sstevel@tonic-gate 	int indent = 3;
6017c478bd9Sstevel@tonic-gate 	struct_def *sp = &defp->def.st;
6027c478bd9Sstevel@tonic-gate 	decl_list *dl;
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	f_print(fout, "\t%s *tmp_%s;\n",
606a2f144d1SJordan Brown 	    defp->def_name, defp->def_name);
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	f_print(fout, "\tbool_t more_data = TRUE;\n");
6097c478bd9Sstevel@tonic-gate 	f_print(fout, "\tbool_t first_objp = TRUE;\n\n");
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	f_print(fout, "\n\tif (xdrs->x_op == XDR_DECODE) {\n");
6127c478bd9Sstevel@tonic-gate 	f_print(fout, "\n\t\twhile (more_data) {\n");
6137c478bd9Sstevel@tonic-gate 	f_print(fout, "\n\t\t\tvoid bzero();\n\n");
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	if (can_inline)
6167c478bd9Sstevel@tonic-gate 		inline_struct(sp->decls, sp->tail, GET, indent);
6177c478bd9Sstevel@tonic-gate 	else
6187c478bd9Sstevel@tonic-gate 		for (dl = sp->decls; dl != NULL && dl != sp->tail;
6197c478bd9Sstevel@tonic-gate 		    dl = dl->next)
6207c478bd9Sstevel@tonic-gate 			print_stat(indent, &dl->decl);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\tif (!xdr_bool(xdrs, "
623a2f144d1SJordan Brown 	    "&more_data))\n\t\t\t\treturn (FALSE);\n");
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	f_print(fout, "\n\t\t\tif (!more_data) {\n");
6267c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\t\tobjp->%s = NULL;\n", sp->tail->decl.name);
6277c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\t\tbreak;\n");
6287c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\t}\n\n");
6297c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\tif (objp->%s == NULL) {\n", sp->tail->decl.name);
6307c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\t\tobjp->%s = "
631a2f144d1SJordan Brown 	    "(%s *)\n\t\t\t\t\tmem_alloc(sizeof (%s));\n",
632a2f144d1SJordan Brown 	    sp->tail->decl.name, defp->def_name, defp->def_name);
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\t\tif (objp->%s == NULL)\n"
635a2f144d1SJordan Brown 	    "\t\t\t\t\treturn (FALSE);\n", sp->tail->decl.name);
6367c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\t\tbzero(objp->%s, sizeof (%s));\n",
637a2f144d1SJordan Brown 	    sp->tail->decl.name, defp->def_name);
6387c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\t}\n");
6397c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\tobjp = objp->%s;\n", sp->tail->decl.name);
6407c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t}\n");
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	f_print(fout, "\n\t} else if (xdrs->x_op == XDR_ENCODE) {\n");
6437c478bd9Sstevel@tonic-gate 	f_print(fout, "\n\t\twhile (more_data) {\n");
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	if (can_inline)
6467c478bd9Sstevel@tonic-gate 		inline_struct(sp->decls, sp->tail, PUT, indent);
6477c478bd9Sstevel@tonic-gate 	else
6487c478bd9Sstevel@tonic-gate 		for (dl = sp->decls; dl != NULL && dl != sp->tail;
6497c478bd9Sstevel@tonic-gate 		    dl = dl->next)
6507c478bd9Sstevel@tonic-gate 			print_stat(indent, &dl->decl);
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\tobjp = objp->%s;\n", sp->tail->decl.name);
6537c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\tif (objp == NULL)\n");
6547c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\t\tmore_data = FALSE;\n");
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\tif (!xdr_bool(xdrs, &more_data))\n"
657a2f144d1SJordan Brown 	    "\t\t\t\treturn (FALSE);\n");
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t}\n");
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	f_print(fout, "\n\t} else {\n");
6627c478bd9Sstevel@tonic-gate 	f_print(fout, "\n\t\twhile (more_data) {\n");
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	for (dl = sp->decls; dl != NULL && dl != sp->tail; dl = dl->next)
6657c478bd9Sstevel@tonic-gate 		print_stat(indent, &dl->decl);
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\ttmp_%s = objp;\n", defp->def_name);
6687c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\tobjp = objp->%s;\n", sp->tail->decl.name);
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\tif (objp == NULL)\n");
6717c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\t\tmore_data = FALSE;\n");
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\tif (!first_objp)\n");
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\t\tmem_free(tmp_%s, sizeof (%s));\n",
676a2f144d1SJordan Brown 	    defp->def_name, defp->def_name);
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\telse\n\t\t\t\tfirst_objp = FALSE;\n\t\t}\n");
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	f_print(fout, "\n\t}\n");
6817c478bd9Sstevel@tonic-gate }
6827c478bd9Sstevel@tonic-gate 
68361961e0fSrobinson static void
emit_struct(definition * def)68461961e0fSrobinson emit_struct(definition *def)
6857c478bd9Sstevel@tonic-gate {
6867c478bd9Sstevel@tonic-gate 	decl_list *dl = def->def.st.decls;
6877c478bd9Sstevel@tonic-gate 	int can_inline, have_vector;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	can_inline = check_inline(dl, inlinelen, &have_vector);
6917c478bd9Sstevel@tonic-gate 	if (have_vector)
6927c478bd9Sstevel@tonic-gate 		f_print(fout, "\tint i;\n");
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	if (rflag && def->def.st.self_pointer) {
6967c478bd9Sstevel@tonic-gate 		/* Handle tail recursion elimination */
6977c478bd9Sstevel@tonic-gate 		emit_struct_tail_recursion(def, can_inline);
6987c478bd9Sstevel@tonic-gate 		return;
6997c478bd9Sstevel@tonic-gate 	}
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	if (can_inline) {
7037c478bd9Sstevel@tonic-gate 		f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
7047c478bd9Sstevel@tonic-gate 		inline_struct(dl, NULL, PUT, 1);
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\treturn (TRUE);\n\t}"
707a2f144d1SJordan Brown 		    " else if (xdrs->x_op == XDR_DECODE) {\n");
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 		inline_struct(dl, NULL, GET, 1);
7107c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\treturn (TRUE);\n\t}\n\n");
7117c478bd9Sstevel@tonic-gate 	}
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	/* now take care of XDR_FREE inline  case or the non-inline cases */
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
7167c478bd9Sstevel@tonic-gate 		print_stat(1, &dl->decl);
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate }
7197c478bd9Sstevel@tonic-gate 
72061961e0fSrobinson static void
emit_typedef(definition * def)72161961e0fSrobinson emit_typedef(definition *def)
7227c478bd9Sstevel@tonic-gate {
7237c478bd9Sstevel@tonic-gate 	char *prefix = def->def.ty.old_prefix;
7247c478bd9Sstevel@tonic-gate 	char *type = def->def.ty.old_type;
7257c478bd9Sstevel@tonic-gate 	char *amax = def->def.ty.array_max;
7267c478bd9Sstevel@tonic-gate 	relation rel = def->def.ty.rel;
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
7297c478bd9Sstevel@tonic-gate }
7307c478bd9Sstevel@tonic-gate 
73161961e0fSrobinson static void
print_stat(int indent,declaration * dec)73261961e0fSrobinson print_stat(int indent, declaration *dec)
7337c478bd9Sstevel@tonic-gate {
7347c478bd9Sstevel@tonic-gate 	char *prefix = dec->prefix;
7357c478bd9Sstevel@tonic-gate 	char *type = dec->type;
7367c478bd9Sstevel@tonic-gate 	char *amax = dec->array_max;
7377c478bd9Sstevel@tonic-gate 	relation rel = dec->rel;
7387c478bd9Sstevel@tonic-gate 	char name[256];
7397c478bd9Sstevel@tonic-gate 
74061961e0fSrobinson 	if (isvectordef(type, rel))
74161961e0fSrobinson 		(void) snprintf(name, sizeof (name), "objp->%s", dec->name);
74261961e0fSrobinson 	else
74361961e0fSrobinson 		(void) snprintf(name, sizeof (name), "&objp->%s", dec->name);
7447c478bd9Sstevel@tonic-gate 	print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
7457c478bd9Sstevel@tonic-gate }
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 
74861961e0fSrobinson static void
emit_inline(int indent,declaration * decl,int flag)74961961e0fSrobinson emit_inline(int indent, declaration *decl, int flag)
7507c478bd9Sstevel@tonic-gate {
7517c478bd9Sstevel@tonic-gate 	switch (decl->rel) {
7527c478bd9Sstevel@tonic-gate 	case  REL_ALIAS :
7537c478bd9Sstevel@tonic-gate 		emit_single_in_line(indent, decl, flag, REL_ALIAS);
7547c478bd9Sstevel@tonic-gate 		break;
7557c478bd9Sstevel@tonic-gate 	case REL_VECTOR :
7567c478bd9Sstevel@tonic-gate 		tabify(fout, indent);
7577c478bd9Sstevel@tonic-gate 		f_print(fout, "{\n");
7587c478bd9Sstevel@tonic-gate 		tabify(fout, indent + 1);
75961961e0fSrobinson 		f_print(fout, "%s *genp;\n\n", decl->type);
7607c478bd9Sstevel@tonic-gate 		tabify(fout, indent + 1);
7617c478bd9Sstevel@tonic-gate 		f_print(fout,
762a2f144d1SJordan Brown 		    "for (i = 0, genp = objp->%s;\n", decl->name);
7637c478bd9Sstevel@tonic-gate 		tabify(fout, indent + 2);
7647c478bd9Sstevel@tonic-gate 		f_print(fout, "i < %s; i++) {\n", decl->array_max);
7657c478bd9Sstevel@tonic-gate 		emit_single_in_line(indent + 2, decl, flag, REL_VECTOR);
7667c478bd9Sstevel@tonic-gate 		tabify(fout, indent + 1);
7677c478bd9Sstevel@tonic-gate 		f_print(fout, "}\n");
7687c478bd9Sstevel@tonic-gate 		tabify(fout, indent);
7697c478bd9Sstevel@tonic-gate 		f_print(fout, "}\n");
7707c478bd9Sstevel@tonic-gate 	}
7717c478bd9Sstevel@tonic-gate }
7727c478bd9Sstevel@tonic-gate 
77361961e0fSrobinson static void
emit_inline64(int indent,declaration * decl,int flag)77461961e0fSrobinson emit_inline64(int indent, declaration *decl, int flag)
7757c478bd9Sstevel@tonic-gate {
7767c478bd9Sstevel@tonic-gate 	switch (decl->rel) {
7777c478bd9Sstevel@tonic-gate 	case  REL_ALIAS :
7787c478bd9Sstevel@tonic-gate 		emit_single_in_line64(indent, decl, flag, REL_ALIAS);
7797c478bd9Sstevel@tonic-gate 		break;
7807c478bd9Sstevel@tonic-gate 	case REL_VECTOR :
7817c478bd9Sstevel@tonic-gate 		tabify(fout, indent);
7827c478bd9Sstevel@tonic-gate 		f_print(fout, "{\n");
7837c478bd9Sstevel@tonic-gate 		tabify(fout, indent + 1);
78461961e0fSrobinson 		f_print(fout, "%s *genp;\n\n", decl->type);
7857c478bd9Sstevel@tonic-gate 		tabify(fout, indent + 1);
7867c478bd9Sstevel@tonic-gate 		f_print(fout,
787a2f144d1SJordan Brown 		    "for (i = 0, genp = objp->%s;\n", decl->name);
7887c478bd9Sstevel@tonic-gate 		tabify(fout, indent + 2);
7897c478bd9Sstevel@tonic-gate 		f_print(fout, "i < %s; i++) {\n", decl->array_max);
7907c478bd9Sstevel@tonic-gate 		emit_single_in_line64(indent + 2, decl, flag, REL_VECTOR);
7917c478bd9Sstevel@tonic-gate 		tabify(fout, indent + 1);
7927c478bd9Sstevel@tonic-gate 		f_print(fout, "}\n");
7937c478bd9Sstevel@tonic-gate 		tabify(fout, indent);
7947c478bd9Sstevel@tonic-gate 		f_print(fout, "}\n");
7957c478bd9Sstevel@tonic-gate 	}
7967c478bd9Sstevel@tonic-gate }
7977c478bd9Sstevel@tonic-gate 
79861961e0fSrobinson static void
emit_single_in_line(int indent,declaration * decl,int flag,relation rel)79961961e0fSrobinson emit_single_in_line(int indent, declaration *decl, int flag, relation rel)
8007c478bd9Sstevel@tonic-gate {
8017c478bd9Sstevel@tonic-gate 	char *upp_case;
8027c478bd9Sstevel@tonic-gate 	int freed = 0;
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	tabify(fout, indent);
8057c478bd9Sstevel@tonic-gate 	if (flag == PUT)
8067c478bd9Sstevel@tonic-gate 		f_print(fout, "IXDR_PUT_");
8077c478bd9Sstevel@tonic-gate 	else
8087c478bd9Sstevel@tonic-gate 		if (rel == REL_ALIAS)
8097c478bd9Sstevel@tonic-gate 			f_print(fout, "objp->%s = IXDR_GET_", decl->name);
8107c478bd9Sstevel@tonic-gate 		else
8117c478bd9Sstevel@tonic-gate 			f_print(fout, "*genp++ = IXDR_GET_");
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	upp_case = upcase(decl->type);
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	/* hack	 - XX */
81661961e0fSrobinson 	if (strcmp(upp_case, "INT") == 0) {
8177c478bd9Sstevel@tonic-gate 		free(upp_case);
8187c478bd9Sstevel@tonic-gate 		freed = 1;
8197c478bd9Sstevel@tonic-gate 		upp_case = "LONG";
8207c478bd9Sstevel@tonic-gate 	}
8217c478bd9Sstevel@tonic-gate 	if ((strcmp(upp_case, "U_INT") == 0) ||
822a2f144d1SJordan Brown 	    (strcmp(upp_case, "RPCPROG") == 0) ||
823a2f144d1SJordan Brown 	    (strcmp(upp_case, "RPCVERS") == 0) ||
824a2f144d1SJordan Brown 	    (strcmp(upp_case, "RPCPROC") == 0) ||
825a2f144d1SJordan Brown 	    (strcmp(upp_case, "RPCPROT") == 0) ||
826a2f144d1SJordan Brown 	    (strcmp(upp_case, "RPCPORT") == 0)) {
8277c478bd9Sstevel@tonic-gate 		free(upp_case);
8287c478bd9Sstevel@tonic-gate 		freed = 1;
8297c478bd9Sstevel@tonic-gate 		upp_case = "U_LONG";
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	if (flag == PUT)
8337c478bd9Sstevel@tonic-gate 		if (rel == REL_ALIAS)
8347c478bd9Sstevel@tonic-gate 			f_print(fout,
835a2f144d1SJordan Brown 			    "%s(buf, objp->%s);\n", upp_case, decl->name);
8367c478bd9Sstevel@tonic-gate 		else
8377c478bd9Sstevel@tonic-gate 			f_print(fout, "%s(buf, *genp++);\n", upp_case);
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	else
8407c478bd9Sstevel@tonic-gate 		f_print(fout, "%s(buf);\n", upp_case);
8417c478bd9Sstevel@tonic-gate 	if (!freed)
8427c478bd9Sstevel@tonic-gate 		free(upp_case);
8437c478bd9Sstevel@tonic-gate }
8447c478bd9Sstevel@tonic-gate 
84561961e0fSrobinson static void
emit_single_in_line64(int indent,declaration * decl,int flag,relation rel)84661961e0fSrobinson emit_single_in_line64(int indent, declaration *decl, int flag, relation rel)
8477c478bd9Sstevel@tonic-gate {
8487c478bd9Sstevel@tonic-gate 	char *upp_case;
8497c478bd9Sstevel@tonic-gate 	int freed = 0;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	tabify(fout, indent);
8527c478bd9Sstevel@tonic-gate 	if (flag == PUT)
8537c478bd9Sstevel@tonic-gate 		f_print(fout, "IXDR_PUT_");
8547c478bd9Sstevel@tonic-gate 	else
8557c478bd9Sstevel@tonic-gate 		if (rel == REL_ALIAS)
8567c478bd9Sstevel@tonic-gate 			f_print(fout, "objp->%s = IXDR_GET_", decl->name);
8577c478bd9Sstevel@tonic-gate 		else
8587c478bd9Sstevel@tonic-gate 			f_print(fout, "*genp++ = IXDR_GET_");
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	upp_case = upcase(decl->type);
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	/* hack	 - XX */
86361961e0fSrobinson 	if ((strcmp(upp_case, "INT") == 0)||(strcmp(upp_case, "LONG") == 0)) {
8647c478bd9Sstevel@tonic-gate 		free(upp_case);
8657c478bd9Sstevel@tonic-gate 		freed = 1;
8667c478bd9Sstevel@tonic-gate 		upp_case = "INT32";
8677c478bd9Sstevel@tonic-gate 	}
8687c478bd9Sstevel@tonic-gate 	if ((strcmp(upp_case, "U_INT") == 0) ||
869a2f144d1SJordan Brown 	    (strcmp(upp_case, "U_LONG") == 0) ||
870a2f144d1SJordan Brown 	    (strcmp(upp_case, "RPCPROG") == 0) ||
871a2f144d1SJordan Brown 	    (strcmp(upp_case, "RPCVERS") == 0) ||
872a2f144d1SJordan Brown 	    (strcmp(upp_case, "RPCPROC") == 0) ||
873a2f144d1SJordan Brown 	    (strcmp(upp_case, "RPCPROT") == 0) ||
874a2f144d1SJordan Brown 	    (strcmp(upp_case, "RPCPORT") == 0)) {
8757c478bd9Sstevel@tonic-gate 		free(upp_case);
8767c478bd9Sstevel@tonic-gate 		freed = 1;
8777c478bd9Sstevel@tonic-gate 		upp_case = "U_INT32";
8787c478bd9Sstevel@tonic-gate 	}
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	if (flag == PUT)
8817c478bd9Sstevel@tonic-gate 		if (rel == REL_ALIAS)
8827c478bd9Sstevel@tonic-gate 			f_print(fout,
883a2f144d1SJordan Brown 			    "%s(buf, objp->%s);\n", upp_case, decl->name);
8847c478bd9Sstevel@tonic-gate 		else
8857c478bd9Sstevel@tonic-gate 			f_print(fout, "%s(buf, *genp++);\n", upp_case);
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	else
8887c478bd9Sstevel@tonic-gate 		f_print(fout, "%s(buf);\n", upp_case);
8897c478bd9Sstevel@tonic-gate 	if (!freed)
8907c478bd9Sstevel@tonic-gate 		free(upp_case);
8917c478bd9Sstevel@tonic-gate }
8927c478bd9Sstevel@tonic-gate 
89361961e0fSrobinson static char *
upcase(char * str)89461961e0fSrobinson upcase(char *str)
8957c478bd9Sstevel@tonic-gate {
8967c478bd9Sstevel@tonic-gate 	char *ptr, *hptr;
8977c478bd9Sstevel@tonic-gate 
89861961e0fSrobinson 	ptr = malloc(strlen(str)+1);
89961961e0fSrobinson 	if (ptr == NULL) {
9007c478bd9Sstevel@tonic-gate 		f_print(stderr, "malloc failed\n");
9017c478bd9Sstevel@tonic-gate 		exit(1);
9027c478bd9Sstevel@tonic-gate 	};
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	hptr = ptr;
9057c478bd9Sstevel@tonic-gate 	while (*str != '\0')
9067c478bd9Sstevel@tonic-gate 		*ptr++ = toupper(*str++);
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	*ptr = '\0';
9097c478bd9Sstevel@tonic-gate 	return (hptr);
9107c478bd9Sstevel@tonic-gate }
911