11897Swollman/*
21897Swollman * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
31897Swollman * unrestricted use provided that this legend is included on all tape
41897Swollman * media and as a part of the software program in whole or part.  Users
51897Swollman * may copy or modify Sun RPC without charge, but are not authorized
61897Swollman * to license or distribute it to anyone else except as part of a product or
71897Swollman * program developed by the user.
8100441Scharnier *
91897Swollman * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
101897Swollman * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
111897Swollman * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12100441Scharnier *
131897Swollman * Sun RPC is provided with no support and without any obligation on the
141897Swollman * part of Sun Microsystems, Inc. to assist in its use, correction,
151897Swollman * modification or enhancement.
16100441Scharnier *
171897Swollman * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
181897Swollman * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
191897Swollman * OR ANY PART THEREOF.
20100441Scharnier *
211897Swollman * In no event will Sun Microsystems, Inc. be liable for any lost revenue
221897Swollman * or profits or other special, indirect and consequential damages, even if
231897Swollman * Sun has been advised of the possibility of such damages.
24100441Scharnier *
251897Swollman * Sun Microsystems, Inc.
261897Swollman * 2550 Garcia Avenue
271897Swollman * Mountain View, California  94043
281897Swollman */
2912798Swpaul
30100441Scharnier#if 0
311897Swollman#ifndef lint
32146833Sstefanf#ident	"@(#)rpc_cout.c	1.14	93/07/05 SMI"
3312798Swpaulstatic char sccsid[] = "@(#)rpc_cout.c 1.13 89/02/22 (C) 1987 SMI";
341897Swollman#endif
3527935Scharnier#endif
361897Swollman
37100441Scharnier#include <sys/cdefs.h>
38100441Scharnier__FBSDID("$FreeBSD$");
39100441Scharnier
401897Swollman/*
418874Srgrimes * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
428874Srgrimes * Copyright (C) 1987, Sun Microsystems, Inc.
431897Swollman */
4427935Scharnier#include <ctype.h>
451897Swollman#include <stdio.h>
4612798Swpaul#include <string.h>
4712798Swpaul#include "rpc_parse.h"
48149682Sstefanf#include "rpc_scan.h"
491897Swollman#include "rpc_util.h"
501897Swollman
5192921Simpstatic void print_header( definition * );
5292921Simpstatic void print_trailer( void );
5392921Simpstatic void print_stat( int , declaration * );
5492921Simpstatic void emit_enum( definition * );
5592921Simpstatic void emit_program( definition * );
5692921Simpstatic void emit_union( definition * );
5792921Simpstatic void emit_struct( definition * );
5892921Simpstatic void emit_typedef( definition * );
5992921Simpstatic void emit_inline( int, declaration *, int );
6092921Simpstatic void emit_single_in_line( int, declaration *, int, relation );
611897Swollman
621897Swollman/*
638874Srgrimes * Emit the C-routine for the given definition
641897Swollman */
651897Swollmanvoid
66152398Sdwmaloneemit(definition *def)
671897Swollman{
6812798Swpaul	if (def->def_kind == DEF_CONST) {
691897Swollman		return;
701897Swollman	}
7112798Swpaul	if (def->def_kind == DEF_PROGRAM) {
7212798Swpaul		emit_program(def);
7312798Swpaul		return;
7412798Swpaul	}
7512798Swpaul	if (def->def_kind == DEF_TYPEDEF) {
7612798Swpaul		/*
7712798Swpaul		 * now we need to handle declarations like
7812798Swpaul		 * struct typedef foo foo;
7912798Swpaul		 * since we dont want this to be expanded into 2 calls to xdr_foo
8012798Swpaul		 */
8112798Swpaul
8212798Swpaul		if (strcmp(def->def.ty.old_type, def->def_name) == 0)
8312798Swpaul			return;
8412798Swpaul	};
851897Swollman	print_header(def);
861897Swollman	switch (def->def_kind) {
871897Swollman	case DEF_UNION:
881897Swollman		emit_union(def);
891897Swollman		break;
901897Swollman	case DEF_ENUM:
911897Swollman		emit_enum(def);
921897Swollman		break;
931897Swollman	case DEF_STRUCT:
941897Swollman		emit_struct(def);
951897Swollman		break;
961897Swollman	case DEF_TYPEDEF:
971897Swollman		emit_typedef(def);
981897Swollman		break;
9917142Sjkh		/* DEF_CONST and DEF_PROGRAM have already been handled */
100100441Scharnier	default:
10199979Salfred		break;
1021897Swollman	}
1031897Swollman	print_trailer();
1041897Swollman}
1051897Swollman
10617142Sjkhstatic int
107152398Sdwmalonefindtype(definition *def, const char *type)
1081897Swollman{
10912798Swpaul
1101897Swollman	if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
1111897Swollman		return (0);
1121897Swollman	} else {
1131897Swollman		return (streq(def->def_name, type));
1141897Swollman	}
1151897Swollman}
1161897Swollman
11717142Sjkhstatic int
118152398Sdwmaloneundefined(const char *type)
1191897Swollman{
1201897Swollman	definition *def;
1211897Swollman
1221897Swollman	def = (definition *) FINDVAL(defined, type, findtype);
1231897Swollman	return (def == NULL);
1241897Swollman}
1251897Swollman
1261897Swollman
12717142Sjkhstatic void
128152398Sdwmaloneprint_generic_header(const char *procname, int pointerp)
1291897Swollman{
13012798Swpaul	f_print(fout, "\n");
1311897Swollman	f_print(fout, "bool_t\n");
132149709Sstefanf	f_print(fout, "xdr_%s(", procname);
133149710Sstefanf	f_print(fout, "XDR *xdrs, ");
134149709Sstefanf	f_print(fout, "%s ", procname);
135149709Sstefanf	if (pointerp)
136149709Sstefanf		f_print(fout, "*");
137149709Sstefanf	f_print(fout, "objp)\n{\n\n");
1381897Swollman}
1391897Swollman
14017142Sjkhstatic void
141152398Sdwmaloneprint_header(definition *def)
14212798Swpaul{
14312798Swpaul	print_generic_header(def->def_name,
14412798Swpaul			    def->def_kind != DEF_TYPEDEF ||
14512798Swpaul			    !isvectordef(def->def.ty.old_type,
14612798Swpaul					 def->def.ty.rel));
14712798Swpaul	/* Now add Inline support */
14812798Swpaul
149149680Sstefanf	if (inline_size == 0)
15012798Swpaul		return;
15112798Swpaul	/* May cause lint to complain. but  ... */
15212798Swpaul	f_print(fout, "\tregister long *buf;\n\n");
15312798Swpaul}
15412798Swpaul
15517142Sjkhstatic void
156152398Sdwmaloneprint_prog_header(proc_list *plist)
15712798Swpaul{
15812798Swpaul	print_generic_header(plist->args.argname, 1);
15912798Swpaul}
16012798Swpaul
16117142Sjkhstatic void
162152398Sdwmaloneprint_trailer(void)
1631897Swollman{
1641897Swollman	f_print(fout, "\treturn (TRUE);\n");
1651897Swollman	f_print(fout, "}\n");
1661897Swollman}
1671897Swollman
1681897Swollman
16917142Sjkhstatic void
170152398Sdwmaloneprint_ifopen(int indent, const char *name)
1711897Swollman{
1721897Swollman	tabify(fout, indent);
1731897Swollman	f_print(fout, "if (!xdr_%s(xdrs", name);
1741897Swollman}
1751897Swollman
17617142Sjkhstatic void
177152398Sdwmaloneprint_ifarg(const char *arg)
1781897Swollman{
1791897Swollman	f_print(fout, ", %s", arg);
1801897Swollman}
1811897Swollman
18217142Sjkhstatic void
183152398Sdwmaloneprint_ifsizeof(int indent, const char *prefix, const char *type)
1841897Swollman{
18512798Swpaul	if (indent) {
18612798Swpaul		f_print(fout, ",\n");
18712798Swpaul		tabify(fout, indent);
18812798Swpaul	} else  {
18912798Swpaul		f_print(fout, ", ");
19012798Swpaul	}
1911897Swollman	if (streq(type, "bool")) {
19212798Swpaul		f_print(fout, "sizeof (bool_t), (xdrproc_t) xdr_bool");
1931897Swollman	} else {
19412798Swpaul		f_print(fout, "sizeof (");
1951897Swollman		if (undefined(type) && prefix) {
1961897Swollman			f_print(fout, "%s ", prefix);
1971897Swollman		}
19812798Swpaul		f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type);
1991897Swollman	}
2001897Swollman}
2011897Swollman
20217142Sjkhstatic void
203173761Sjbprint_ifclose(int indent, int brace)
2041897Swollman{
20512798Swpaul	f_print(fout, "))\n");
2061897Swollman	tabify(fout, indent);
2071897Swollman	f_print(fout, "\treturn (FALSE);\n");
208173761Sjb	if (brace)
209173761Sjb		f_print(fout, "\t}\n");
2101897Swollman}
2111897Swollman
21217142Sjkhstatic void
213152398Sdwmaloneprint_ifstat(int indent, const char *prefix, const char *type, relation rel,
214152398Sdwmalone    const char *amax, const char *objname, const char *name)
2151897Swollman{
216152398Sdwmalone	const char *alt = NULL;
217173761Sjb	int brace = 0;
2181897Swollman
2191897Swollman	switch (rel) {
2201897Swollman	case REL_POINTER:
221173761Sjb		brace = 1;
222173761Sjb		f_print(fout, "\t{\n");
223173761Sjb		f_print(fout, "\t%s **pp = %s;\n", type, objname);
2241897Swollman		print_ifopen(indent, "pointer");
2251897Swollman		print_ifarg("(char **)");
226173761Sjb		f_print(fout, "pp");
22712798Swpaul		print_ifsizeof(0, prefix, type);
2281897Swollman		break;
2291897Swollman	case REL_VECTOR:
2301897Swollman		if (streq(type, "string")) {
2311897Swollman			alt = "string";
2321897Swollman		} else if (streq(type, "opaque")) {
2331897Swollman			alt = "opaque";
2341897Swollman		}
2351897Swollman		if (alt) {
2361897Swollman			print_ifopen(indent, alt);
2371897Swollman			print_ifarg(objname);
2381897Swollman		} else {
2391897Swollman			print_ifopen(indent, "vector");
2401897Swollman			print_ifarg("(char *)");
2411897Swollman			f_print(fout, "%s", objname);
2421897Swollman		}
2431897Swollman		print_ifarg(amax);
2441897Swollman		if (!alt) {
24512798Swpaul			print_ifsizeof(indent + 1, prefix, type);
2461897Swollman		}
2471897Swollman		break;
2481897Swollman	case REL_ARRAY:
2491897Swollman		if (streq(type, "string")) {
2501897Swollman			alt = "string";
2511897Swollman		} else if (streq(type, "opaque")) {
2521897Swollman			alt = "bytes";
2531897Swollman		}
2541897Swollman		if (streq(type, "string")) {
2551897Swollman			print_ifopen(indent, alt);
2561897Swollman			print_ifarg(objname);
2571897Swollman		} else {
2581897Swollman			if (alt) {
2591897Swollman				print_ifopen(indent, alt);
2601897Swollman			} else {
2611897Swollman				print_ifopen(indent, "array");
2621897Swollman			}
2631897Swollman			print_ifarg("(char **)");
2641897Swollman			if (*objname == '&') {
26512798Swpaul				f_print(fout, "%s.%s_val, (u_int *) %s.%s_len",
2661897Swollman					objname, name, objname, name);
2671897Swollman			} else {
26812798Swpaul				f_print(fout,
26912798Swpaul					"&%s->%s_val, (u_int *) &%s->%s_len",
2701897Swollman					objname, name, objname, name);
2711897Swollman			}
2721897Swollman		}
2731897Swollman		print_ifarg(amax);
2741897Swollman		if (!alt) {
27512798Swpaul			print_ifsizeof(indent + 1, prefix, type);
2761897Swollman		}
2771897Swollman		break;
2781897Swollman	case REL_ALIAS:
2791897Swollman		print_ifopen(indent, type);
2801897Swollman		print_ifarg(objname);
2811897Swollman		break;
2821897Swollman	}
283173761Sjb	print_ifclose(indent, brace);
2841897Swollman}
2851897Swollman
2861897Swollman/* ARGSUSED */
28717142Sjkhstatic void
288152398Sdwmaloneemit_enum(definition *def __unused)
2891897Swollman{
2901897Swollman	print_ifopen(1, "enum");
2911897Swollman	print_ifarg("(enum_t *)objp");
292173761Sjb	print_ifclose(1, 0);
2931897Swollman}
2941897Swollman
29517142Sjkhstatic void
296152398Sdwmaloneemit_program(definition *def)
29712798Swpaul{
29812798Swpaul	decl_list *dl;
29912798Swpaul	version_list *vlist;
30012798Swpaul	proc_list *plist;
3011897Swollman
30212798Swpaul	for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
30312798Swpaul		for (plist = vlist->procs; plist != NULL; plist = plist->next) {
30412798Swpaul			if (!newstyle || plist->arg_num < 2)
30512798Swpaul				continue; /* old style, or single argument */
30612798Swpaul			print_prog_header(plist);
30712798Swpaul			for (dl = plist->args.decls; dl != NULL;
30812798Swpaul			     dl = dl->next)
30912798Swpaul				print_stat(1, &dl->decl);
31012798Swpaul			print_trailer();
31112798Swpaul		}
31212798Swpaul}
31312798Swpaul
31412798Swpaul
31517142Sjkhstatic void
316152398Sdwmaloneemit_union(definition *def)
3171897Swollman{
3181897Swollman	declaration *dflt;
3191897Swollman	case_list *cl;
3201897Swollman	declaration *cs;
3211897Swollman	char *object;
322152398Sdwmalone	const char *vecformat = "objp->%s_u.%s";
323152398Sdwmalone	const char *format = "&objp->%s_u.%s";
3241897Swollman
32512798Swpaul	print_stat(1, &def->def.un.enum_decl);
3261897Swollman	f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
3271897Swollman	for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
32812798Swpaul
32912798Swpaul		f_print(fout, "\tcase %s:\n", cl->case_name);
33012798Swpaul		if (cl->contflag == 1) /* a continued case statement */
33112798Swpaul			continue;
3321897Swollman		cs = &cl->case_decl;
3331897Swollman		if (!streq(cs->type, "void")) {
334100441Scharnier			object = xmalloc(strlen(def->def_name) +
335100441Scharnier			                 strlen(format) + strlen(cs->name) + 1);
33612798Swpaul			if (isvectordef (cs->type, cs->rel)) {
33712798Swpaul				s_print(object, vecformat, def->def_name,
33812798Swpaul					cs->name);
33912798Swpaul			} else {
34012798Swpaul				s_print(object, format, def->def_name,
34112798Swpaul					cs->name);
34212798Swpaul			}
34312798Swpaul			print_ifstat(2, cs->prefix, cs->type, cs->rel,
34412798Swpaul				     cs->array_max, object, cs->name);
3451897Swollman			free(object);
3461897Swollman		}
3471897Swollman		f_print(fout, "\t\tbreak;\n");
3481897Swollman	}
3491897Swollman	dflt = def->def.un.default_decl;
3501897Swollman	if (dflt != NULL) {
3511897Swollman		if (!streq(dflt->type, "void")) {
3521897Swollman			f_print(fout, "\tdefault:\n");
353100441Scharnier			object = xmalloc(strlen(def->def_name) +
354100441Scharnier			                 strlen(format) + strlen(dflt->name) + 1);
35512798Swpaul			if (isvectordef (dflt->type, dflt->rel)) {
35612798Swpaul				s_print(object, vecformat, def->def_name,
35712798Swpaul					dflt->name);
35812798Swpaul			} else {
35912798Swpaul				s_print(object, format, def->def_name,
36012798Swpaul					dflt->name);
36112798Swpaul			}
36212798Swpaul
3631897Swollman			print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
36412798Swpaul				    dflt->array_max, object, dflt->name);
3651897Swollman			free(object);
3661897Swollman			f_print(fout, "\t\tbreak;\n");
36717142Sjkh		} else {
36817142Sjkh			f_print(fout, "\tdefault:\n");
36917142Sjkh			f_print(fout, "\t\tbreak;\n");
3701897Swollman		}
3711897Swollman	} else {
3721897Swollman		f_print(fout, "\tdefault:\n");
3731897Swollman		f_print(fout, "\t\treturn (FALSE);\n");
3741897Swollman	}
37512798Swpaul
3761897Swollman	f_print(fout, "\t}\n");
3771897Swollman}
3781897Swollman
37912798Swpaulstatic void
380152398Sdwmaloneinline_struct(definition *def, int flag)
38112798Swpaul{
38212798Swpaul	decl_list *dl;
38312798Swpaul	int i, size;
38412798Swpaul	decl_list *cur, *psav;
38512798Swpaul	bas_type *ptr;
386152398Sdwmalone	char *sizestr;
387152398Sdwmalone	const char *plus;
38812798Swpaul	char ptemp[256];
38912798Swpaul	int indent = 1;
3901897Swollman
39199979Salfred	cur = NULL;
39212798Swpaul	if (flag == PUT)
39312798Swpaul		f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
39412798Swpaul	else
39512798Swpaul		f_print(fout, "\t\treturn (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
3961897Swollman
39712798Swpaul	i = 0;
39812798Swpaul	size = 0;
39912798Swpaul	sizestr = NULL;
40012798Swpaul	for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
40112798Swpaul		/* now walk down the list and check for basic types */
40212798Swpaul		if ((dl->decl.prefix == NULL) &&
40312798Swpaul		    ((ptr = find_type(dl->decl.type)) != NULL) &&
40412798Swpaul		    ((dl->decl.rel == REL_ALIAS) ||
40512798Swpaul		     (dl->decl.rel == REL_VECTOR))){
40612798Swpaul			if (i == 0)
40712798Swpaul				cur = dl;
40812798Swpaul			i++;
40912798Swpaul
41012798Swpaul			if (dl->decl.rel == REL_ALIAS)
41112798Swpaul				size += ptr->length;
41212798Swpaul			else {
41312798Swpaul				/* this code is required to handle arrays */
41412798Swpaul				if (sizestr == NULL)
41512798Swpaul					plus = "";
41612798Swpaul				else
41712798Swpaul					plus = " + ";
41812798Swpaul
41912798Swpaul				if (ptr->length != 1)
42012798Swpaul					s_print(ptemp, "%s%s * %d",
42112798Swpaul						plus, dl->decl.array_max,
42212798Swpaul						ptr->length);
42312798Swpaul				else
42412798Swpaul					s_print(ptemp, "%s%s", plus,
42512798Swpaul						dl->decl.array_max);
42612798Swpaul
42712798Swpaul				/* now concatenate to sizestr !!!! */
428100441Scharnier				if (sizestr == NULL) {
429100441Scharnier					sizestr = xstrdup(ptemp);
430100441Scharnier				}
43112798Swpaul				else{
432100441Scharnier					sizestr = xrealloc(sizestr,
43312798Swpaul							  strlen(sizestr)
43412798Swpaul							  +strlen(ptemp)+1);
43512798Swpaul					sizestr = strcat(sizestr, ptemp);
43612798Swpaul					/* build up length of array */
43712798Swpaul				}
43812798Swpaul			}
43912798Swpaul		} else {
44048566Sbillf			if (i > 0) {
441149680Sstefanf				if (sizestr == NULL && size < inline_size){
44212798Swpaul					/*
44312798Swpaul					 * don't expand into inline code
444149680Sstefanf					 * if size < inline_size
44512798Swpaul					 */
44612798Swpaul					while (cur != dl){
44712798Swpaul						print_stat(indent + 1, &cur->decl);
44812798Swpaul						cur = cur->next;
44912798Swpaul					}
45012798Swpaul				} else {
45112798Swpaul					/* were already looking at a xdr_inlineable structure */
45212798Swpaul					tabify(fout, indent + 1);
45312798Swpaul					if (sizestr == NULL)
45412798Swpaul						f_print(fout, "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
45512798Swpaul							size);
45648566Sbillf					else {
45712798Swpaul						if (size == 0)
45812798Swpaul							f_print(fout,
45912798Swpaul								"buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
46012798Swpaul								sizestr);
46112798Swpaul						else
46212798Swpaul							f_print(fout,
46312798Swpaul								"buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
46412798Swpaul								size, sizestr);
46512798Swpaul
46648566Sbillf					}
46712798Swpaul					f_print(fout, "\n");
46812798Swpaul					tabify(fout, indent + 1);
46912798Swpaul					f_print(fout,
47012798Swpaul						"if (buf == NULL) {\n");
47112798Swpaul
47212798Swpaul					psav = cur;
47312798Swpaul					while (cur != dl){
47412798Swpaul						print_stat(indent + 2, &cur->decl);
47512798Swpaul						cur = cur->next;
47612798Swpaul					}
47712798Swpaul
47812798Swpaul					f_print(fout, "\n\t\t} else {\n");
47912798Swpaul
48012798Swpaul					cur = psav;
48112798Swpaul					while (cur != dl){
48212798Swpaul						emit_inline(indent + 2, &cur->decl, flag);
48312798Swpaul						cur = cur->next;
48412798Swpaul					}
48512798Swpaul
48612798Swpaul					tabify(fout, indent + 1);
48712798Swpaul					f_print(fout, "}\n");
48812798Swpaul				}
48948566Sbillf			}
49012798Swpaul			size = 0;
49112798Swpaul			i = 0;
492207733Sdelphij			free(sizestr);
49312798Swpaul			sizestr = NULL;
49412798Swpaul			print_stat(indent + 1, &dl->decl);
49512798Swpaul		}
49612798Swpaul	}
49712798Swpaul
49899979Salfred	if (i > 0) {
499149680Sstefanf		if (sizestr == NULL && size < inline_size){
500149680Sstefanf			/* don't expand into inline code if size < inline_size */
50112798Swpaul			while (cur != dl){
50212798Swpaul				print_stat(indent + 1, &cur->decl);
50312798Swpaul				cur = cur->next;
50412798Swpaul			}
50512798Swpaul		} else {
50612798Swpaul			/* were already looking at a xdr_inlineable structure */
50712798Swpaul			if (sizestr == NULL)
50812798Swpaul				f_print(fout, "\t\tbuf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
50912798Swpaul					size);
51012798Swpaul			else
51112798Swpaul				if (size == 0)
51212798Swpaul					f_print(fout,
51312798Swpaul						"\t\tbuf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
51412798Swpaul						sizestr);
51512798Swpaul				else
51612798Swpaul					f_print(fout,
51712798Swpaul						"\t\tbuf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
51812798Swpaul						size, sizestr);
51912798Swpaul
52012798Swpaul			f_print(fout, "\n\t\tif (buf == NULL) {\n");
52112798Swpaul			psav = cur;
52212798Swpaul			while (cur != NULL){
52312798Swpaul				print_stat(indent + 2, &cur->decl);
52412798Swpaul				cur = cur->next;
52512798Swpaul			}
52612798Swpaul			f_print(fout, "\t\t} else {\n");
52712798Swpaul
52812798Swpaul			cur = psav;
52912798Swpaul			while (cur != dl){
53012798Swpaul				emit_inline(indent + 2, &cur->decl, flag);
53112798Swpaul				cur = cur->next;
53212798Swpaul			}
53312798Swpaul			f_print(fout, "\t\t}\n");
53412798Swpaul		}
53599979Salfred	}
53612798Swpaul}
53712798Swpaul
53817142Sjkhstatic void
539152398Sdwmaloneemit_struct(definition *def)
5401897Swollman{
5411897Swollman	decl_list *dl;
54217142Sjkh	int j, size, flag;
54312798Swpaul	bas_type *ptr;
54412798Swpaul	int can_inline;
5451897Swollman
546149680Sstefanf	if (inline_size == 0) {
54712798Swpaul		/* No xdr_inlining at all */
54812798Swpaul		for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
54912798Swpaul			print_stat(1, &dl->decl);
55012798Swpaul		return;
5511897Swollman	}
5521897Swollman
55312798Swpaul	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
55412798Swpaul		if (dl->decl.rel == REL_VECTOR){
55512798Swpaul			f_print(fout, "\tint i;\n");
55612798Swpaul			break;
55712798Swpaul		}
5581897Swollman
55912798Swpaul	size = 0;
56012798Swpaul	can_inline = 0;
56112798Swpaul	/*
56212798Swpaul	 * Make a first pass and see if inling is possible.
56312798Swpaul	 */
56412798Swpaul	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
56512798Swpaul		if ((dl->decl.prefix == NULL) &&
56612798Swpaul		    ((ptr = find_type(dl->decl.type)) != NULL) &&
56712798Swpaul		    ((dl->decl.rel == REL_ALIAS)||
56812798Swpaul		     (dl->decl.rel == REL_VECTOR))){
56912798Swpaul			if (dl->decl.rel == REL_ALIAS)
57012798Swpaul				size += ptr->length;
57112798Swpaul			else {
57212798Swpaul				can_inline = 1;
57312798Swpaul				break; /* can be inlined */
57412798Swpaul			}
57512798Swpaul		} else {
576149680Sstefanf			if (size >= inline_size){
57712798Swpaul				can_inline = 1;
57812798Swpaul				break; /* can be inlined */
57912798Swpaul			}
58012798Swpaul			size = 0;
58112798Swpaul		}
582149680Sstefanf	if (size >= inline_size)
58312798Swpaul		can_inline = 1;
5841897Swollman
58512798Swpaul	if (can_inline == 0){	/* can not inline, drop back to old mode */
58612798Swpaul		for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
58712798Swpaul			print_stat(1, &dl->decl);
58812798Swpaul		return;
58912798Swpaul	}
5901897Swollman
59112798Swpaul	flag = PUT;
59212798Swpaul	for (j = 0; j < 2; j++){
59312798Swpaul		inline_struct(def, flag);
59412798Swpaul		if (flag == PUT)
59512798Swpaul			flag = GET;
59612798Swpaul	}
59712798Swpaul
59812798Swpaul	f_print(fout, "\t\treturn (TRUE);\n\t}\n\n");
59912798Swpaul
60012798Swpaul	/* now take care of XDR_FREE case */
60112798Swpaul
60212798Swpaul	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
60312798Swpaul		print_stat(1, &dl->decl);
60412798Swpaul
60512798Swpaul}
60612798Swpaul
60717142Sjkhstatic void
608152398Sdwmaloneemit_typedef(definition *def)
6091897Swollman{
610152398Sdwmalone	const char *prefix = def->def.ty.old_prefix;
611152398Sdwmalone	const char *type = def->def.ty.old_type;
612152398Sdwmalone	const char *amax = def->def.ty.array_max;
6131897Swollman	relation rel = def->def.ty.rel;
6141897Swollman
6151897Swollman	print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
6161897Swollman}
6171897Swollman
61817142Sjkhstatic void
619152398Sdwmaloneprint_stat(int indent, declaration *dec)
6201897Swollman{
621152398Sdwmalone	const char *prefix = dec->prefix;
622152398Sdwmalone	const char *type = dec->type;
623152398Sdwmalone	const char *amax = dec->array_max;
6241897Swollman	relation rel = dec->rel;
6251897Swollman	char name[256];
6261897Swollman
6271897Swollman	if (isvectordef(type, rel)) {
6281897Swollman		s_print(name, "objp->%s", dec->name);
6291897Swollman	} else {
6301897Swollman		s_print(name, "&objp->%s", dec->name);
6311897Swollman	}
63212798Swpaul	print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
6331897Swollman}
63412798Swpaul
63512798Swpaul
636152398Sdwmalonechar *upcase(const char *);
63712798Swpaul
63817142Sjkhstatic void
639152398Sdwmaloneemit_inline(int indent, declaration *decl, int flag)
64012798Swpaul{
64112798Swpaul	switch (decl->rel) {
64212798Swpaul	case  REL_ALIAS :
64312798Swpaul		emit_single_in_line(indent, decl, flag, REL_ALIAS);
64412798Swpaul		break;
64512798Swpaul	case REL_VECTOR :
64612798Swpaul		tabify(fout, indent);
64712798Swpaul		f_print(fout, "{\n");
64812798Swpaul		tabify(fout, indent + 1);
649149710Sstefanf		f_print(fout, "%s *genp;\n\n", decl->type);
65012798Swpaul		tabify(fout, indent + 1);
65112798Swpaul		f_print(fout,
65212798Swpaul			"for (i = 0, genp = objp->%s;\n", decl->name);
65312798Swpaul		tabify(fout, indent + 2);
65412798Swpaul		f_print(fout, "i < %s; i++) {\n", decl->array_max);
65512798Swpaul		emit_single_in_line(indent + 2, decl, flag, REL_VECTOR);
65612798Swpaul		tabify(fout, indent + 1);
65712798Swpaul		f_print(fout, "}\n");
65812798Swpaul		tabify(fout, indent);
65912798Swpaul		f_print(fout, "}\n");
66099979Salfred		break;
66117142Sjkh	default:
66299979Salfred		break;
66312798Swpaul	}
66412798Swpaul}
66512798Swpaul
66617142Sjkhstatic void
667152398Sdwmaloneemit_single_in_line(int indent, declaration *decl, int flag, relation rel)
66812798Swpaul{
66912798Swpaul	char *upp_case;
67012798Swpaul
67112798Swpaul	tabify(fout, indent);
67212798Swpaul	if (flag == PUT)
67312798Swpaul		f_print(fout, "IXDR_PUT_");
67412798Swpaul	else
67512798Swpaul		if (rel == REL_ALIAS)
67612798Swpaul			f_print(fout, "objp->%s = IXDR_GET_", decl->name);
67712798Swpaul		else
67812798Swpaul			f_print(fout, "*genp++ = IXDR_GET_");
67912798Swpaul
68012798Swpaul	upp_case = upcase(decl->type);
68112798Swpaul
68212798Swpaul	/* hack	 - XX */
68312798Swpaul	if (strcmp(upp_case, "INT") == 0)
68412798Swpaul	{
68512798Swpaul		free(upp_case);
686152398Sdwmalone		upp_case = strdup("LONG");
68712798Swpaul	}
68812798Swpaul
68912798Swpaul	if (strcmp(upp_case, "U_INT") == 0)
69012798Swpaul	{
69112798Swpaul		free(upp_case);
692152398Sdwmalone		upp_case = strdup("U_LONG");
69312798Swpaul	}
69412798Swpaul	if (flag == PUT)
69512798Swpaul		if (rel == REL_ALIAS)
69612798Swpaul			f_print(fout,
69712798Swpaul				"%s(buf, objp->%s);\n", upp_case, decl->name);
69812798Swpaul		else
69912798Swpaul			f_print(fout, "%s(buf, *genp++);\n", upp_case);
70012798Swpaul
70112798Swpaul	else
70212798Swpaul		f_print(fout, "%s(buf);\n", upp_case);
703152398Sdwmalone	free(upp_case);
70412798Swpaul}
70512798Swpaul
706152398Sdwmalonechar *
707152398Sdwmaloneupcase(const char *str)
70812798Swpaul{
70912798Swpaul	char *ptr, *hptr;
71012798Swpaul
711100441Scharnier	ptr = (char *)xmalloc(strlen(str)+1);
71212798Swpaul
71312798Swpaul	hptr = ptr;
71412798Swpaul	while (*str != '\0')
71512798Swpaul		*ptr++ = toupper(*str++);
71612798Swpaul
71712798Swpaul	*ptr = '\0';
71812798Swpaul	return (hptr);
71912798Swpaul}
720