xref: /freebsd/usr.bin/rpcgen/rpc_cout.c (revision e390e3af7c6aa20ef57a33a4895039ee4787509c)
14e115012SGarrett Wollman /*
24e115012SGarrett Wollman  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
34e115012SGarrett Wollman  * unrestricted use provided that this legend is included on all tape
44e115012SGarrett Wollman  * media and as a part of the software program in whole or part.  Users
54e115012SGarrett Wollman  * may copy or modify Sun RPC without charge, but are not authorized
64e115012SGarrett Wollman  * to license or distribute it to anyone else except as part of a product or
74e115012SGarrett Wollman  * program developed by the user.
84e115012SGarrett Wollman  *
94e115012SGarrett Wollman  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
104e115012SGarrett Wollman  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
114e115012SGarrett Wollman  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
124e115012SGarrett Wollman  *
134e115012SGarrett Wollman  * Sun RPC is provided with no support and without any obligation on the
144e115012SGarrett Wollman  * part of Sun Microsystems, Inc. to assist in its use, correction,
154e115012SGarrett Wollman  * modification or enhancement.
164e115012SGarrett Wollman  *
174e115012SGarrett Wollman  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
184e115012SGarrett Wollman  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
194e115012SGarrett Wollman  * OR ANY PART THEREOF.
204e115012SGarrett Wollman  *
214e115012SGarrett Wollman  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
224e115012SGarrett Wollman  * or profits or other special, indirect and consequential damages, even if
234e115012SGarrett Wollman  * Sun has been advised of the possibility of such damages.
244e115012SGarrett Wollman  *
254e115012SGarrett Wollman  * Sun Microsystems, Inc.
264e115012SGarrett Wollman  * 2550 Garcia Avenue
274e115012SGarrett Wollman  * Mountain View, California  94043
284e115012SGarrett Wollman  */
29ff49530fSBill Paul 
300e76f40dSPhilippe Charnier #if 0
3175863a6dSPhilippe Charnier #ifndef lint
3263f17371SStefan Farfeleder #ident	"@(#)rpc_cout.c	1.14	93/07/05 SMI"
33ff49530fSBill Paul static char sccsid[] = "@(#)rpc_cout.c 1.13 89/02/22 (C) 1987 SMI";
344e115012SGarrett Wollman #endif
350e76f40dSPhilippe Charnier #endif
364e115012SGarrett Wollman 
3775863a6dSPhilippe Charnier #include <sys/cdefs.h>
3875863a6dSPhilippe Charnier __FBSDID("$FreeBSD$");
3975863a6dSPhilippe Charnier 
404e115012SGarrett Wollman /*
414e115012SGarrett Wollman  * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
424e115012SGarrett Wollman  * Copyright (C) 1987, Sun Microsystems, Inc.
434e115012SGarrett Wollman  */
440e76f40dSPhilippe Charnier #include <ctype.h>
454e115012SGarrett Wollman #include <stdio.h>
46ff49530fSBill Paul #include <string.h>
474e115012SGarrett Wollman #include "rpc_parse.h"
48d0cc804bSStefan Farfeleder #include "rpc_scan.h"
49ff49530fSBill Paul #include "rpc_util.h"
504e115012SGarrett Wollman 
51d3cb5dedSWarner Losh static void print_header( definition * );
52d3cb5dedSWarner Losh static void print_trailer( void );
53d3cb5dedSWarner Losh static void print_stat( int , declaration * );
54d3cb5dedSWarner Losh static void emit_enum( definition * );
55d3cb5dedSWarner Losh static void emit_program( definition * );
56d3cb5dedSWarner Losh static void emit_union( definition * );
57d3cb5dedSWarner Losh static void emit_struct( definition * );
58d3cb5dedSWarner Losh static void emit_typedef( definition * );
59d3cb5dedSWarner Losh static void emit_inline( int, declaration *, int );
60d3cb5dedSWarner Losh static void emit_single_in_line( int, declaration *, int, relation );
614e115012SGarrett Wollman 
624e115012SGarrett Wollman /*
634e115012SGarrett Wollman  * Emit the C-routine for the given definition
644e115012SGarrett Wollman  */
654e115012SGarrett Wollman void
66e390e3afSDavid Malone emit(definition *def)
674e115012SGarrett Wollman {
68ff49530fSBill Paul 	if (def->def_kind == DEF_CONST) {
694e115012SGarrett Wollman 		return;
704e115012SGarrett Wollman 	}
71ff49530fSBill Paul 	if (def->def_kind == DEF_PROGRAM) {
72ff49530fSBill Paul 		emit_program(def);
73ff49530fSBill Paul 		return;
74ff49530fSBill Paul 	}
75ff49530fSBill Paul 	if (def->def_kind == DEF_TYPEDEF) {
76ff49530fSBill Paul 		/*
77ff49530fSBill Paul 		 * now we need to handle declarations like
78ff49530fSBill Paul 		 * struct typedef foo foo;
79ff49530fSBill Paul 		 * since we dont want this to be expanded into 2 calls to xdr_foo
80ff49530fSBill Paul 		 */
81ff49530fSBill Paul 
82ff49530fSBill Paul 		if (strcmp(def->def.ty.old_type, def->def_name) == 0)
83ff49530fSBill Paul 			return;
84ff49530fSBill Paul 	};
854e115012SGarrett Wollman 	print_header(def);
864e115012SGarrett Wollman 	switch (def->def_kind) {
874e115012SGarrett Wollman 	case DEF_UNION:
884e115012SGarrett Wollman 		emit_union(def);
894e115012SGarrett Wollman 		break;
904e115012SGarrett Wollman 	case DEF_ENUM:
914e115012SGarrett Wollman 		emit_enum(def);
924e115012SGarrett Wollman 		break;
934e115012SGarrett Wollman 	case DEF_STRUCT:
944e115012SGarrett Wollman 		emit_struct(def);
954e115012SGarrett Wollman 		break;
964e115012SGarrett Wollman 	case DEF_TYPEDEF:
974e115012SGarrett Wollman 		emit_typedef(def);
984e115012SGarrett Wollman 		break;
99526195adSJordan K. Hubbard 		/* DEF_CONST and DEF_PROGRAM have already been handled */
100526195adSJordan K. Hubbard 	default:
10140ad8885SAlfred Perlstein 		break;
1024e115012SGarrett Wollman 	}
1034e115012SGarrett Wollman 	print_trailer();
1044e115012SGarrett Wollman }
1054e115012SGarrett Wollman 
106526195adSJordan K. Hubbard static int
107e390e3afSDavid Malone findtype(definition *def, const char *type)
1084e115012SGarrett Wollman {
109ff49530fSBill Paul 
1104e115012SGarrett Wollman 	if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
1114e115012SGarrett Wollman 		return (0);
1124e115012SGarrett Wollman 	} else {
1134e115012SGarrett Wollman 		return (streq(def->def_name, type));
1144e115012SGarrett Wollman 	}
1154e115012SGarrett Wollman }
1164e115012SGarrett Wollman 
117526195adSJordan K. Hubbard static int
118e390e3afSDavid Malone undefined(const char *type)
1194e115012SGarrett Wollman {
1204e115012SGarrett Wollman 	definition *def;
1214e115012SGarrett Wollman 
1224e115012SGarrett Wollman 	def = (definition *) FINDVAL(defined, type, findtype);
1234e115012SGarrett Wollman 	return (def == NULL);
1244e115012SGarrett Wollman }
1254e115012SGarrett Wollman 
1264e115012SGarrett Wollman 
127526195adSJordan K. Hubbard static void
128e390e3afSDavid Malone print_generic_header(const char *procname, int pointerp)
129ff49530fSBill Paul {
130ff49530fSBill Paul 	f_print(fout, "\n");
131ff49530fSBill Paul 	f_print(fout, "bool_t\n");
132ff49530fSBill Paul 	f_print(fout, "xdr_%s(", procname);
133896cdc31SStefan Farfeleder 	f_print(fout, "XDR *xdrs, ");
134ff49530fSBill Paul 	f_print(fout, "%s ", procname);
135ff49530fSBill Paul 	if (pointerp)
136ff49530fSBill Paul 		f_print(fout, "*");
137ff49530fSBill Paul 	f_print(fout, "objp)\n{\n\n");
138ff49530fSBill Paul }
139ff49530fSBill Paul 
140526195adSJordan K. Hubbard static void
141e390e3afSDavid Malone print_header(definition *def)
1424e115012SGarrett Wollman {
143ff49530fSBill Paul 	print_generic_header(def->def_name,
144ff49530fSBill Paul 			    def->def_kind != DEF_TYPEDEF ||
145ff49530fSBill Paul 			    !isvectordef(def->def.ty.old_type,
146ff49530fSBill Paul 					 def->def.ty.rel));
147ff49530fSBill Paul 	/* Now add Inline support */
148ff49530fSBill Paul 
149122562cdSStefan Farfeleder 	if (inline_size == 0)
150ff49530fSBill Paul 		return;
151ff49530fSBill Paul 	/* May cause lint to complain. but  ... */
152ff49530fSBill Paul 	f_print(fout, "\tregister long *buf;\n\n");
1534e115012SGarrett Wollman }
154ff49530fSBill Paul 
155526195adSJordan K. Hubbard static void
156e390e3afSDavid Malone print_prog_header(proc_list *plist)
157ff49530fSBill Paul {
158ff49530fSBill Paul 	print_generic_header(plist->args.argname, 1);
1594e115012SGarrett Wollman }
1604e115012SGarrett Wollman 
161526195adSJordan K. Hubbard static void
162e390e3afSDavid Malone print_trailer(void)
1634e115012SGarrett Wollman {
1644e115012SGarrett Wollman 	f_print(fout, "\treturn (TRUE);\n");
1654e115012SGarrett Wollman 	f_print(fout, "}\n");
1664e115012SGarrett Wollman }
1674e115012SGarrett Wollman 
1684e115012SGarrett Wollman 
169526195adSJordan K. Hubbard static void
170e390e3afSDavid Malone print_ifopen(int indent, const char *name)
1714e115012SGarrett Wollman {
1724e115012SGarrett Wollman 	tabify(fout, indent);
1734e115012SGarrett Wollman 	f_print(fout, "if (!xdr_%s(xdrs", name);
1744e115012SGarrett Wollman }
1754e115012SGarrett Wollman 
176526195adSJordan K. Hubbard static void
177e390e3afSDavid Malone print_ifarg(const char *arg)
1784e115012SGarrett Wollman {
1794e115012SGarrett Wollman 	f_print(fout, ", %s", arg);
1804e115012SGarrett Wollman }
1814e115012SGarrett Wollman 
182526195adSJordan K. Hubbard static void
183e390e3afSDavid Malone print_ifsizeof(int indent, const char *prefix, const char *type)
1844e115012SGarrett Wollman {
185ff49530fSBill Paul 	if (indent) {
186ff49530fSBill Paul 		f_print(fout, ",\n");
187ff49530fSBill Paul 		tabify(fout, indent);
1884e115012SGarrett Wollman 	} else  {
189ff49530fSBill Paul 		f_print(fout, ", ");
190ff49530fSBill Paul 	}
191ff49530fSBill Paul 	if (streq(type, "bool")) {
192ff49530fSBill Paul 		f_print(fout, "sizeof (bool_t), (xdrproc_t) xdr_bool");
193ff49530fSBill Paul 	} else {
194ff49530fSBill Paul 		f_print(fout, "sizeof (");
1954e115012SGarrett Wollman 		if (undefined(type) && prefix) {
1964e115012SGarrett Wollman 			f_print(fout, "%s ", prefix);
1974e115012SGarrett Wollman 		}
198ff49530fSBill Paul 		f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type);
1994e115012SGarrett Wollman 	}
2004e115012SGarrett Wollman }
2014e115012SGarrett Wollman 
202526195adSJordan K. Hubbard static void
203e390e3afSDavid Malone print_ifclose(int indent)
2044e115012SGarrett Wollman {
205ff49530fSBill Paul 	f_print(fout, "))\n");
2064e115012SGarrett Wollman 	tabify(fout, indent);
2074e115012SGarrett Wollman 	f_print(fout, "\treturn (FALSE);\n");
2084e115012SGarrett Wollman }
2094e115012SGarrett Wollman 
210526195adSJordan K. Hubbard static void
211e390e3afSDavid Malone print_ifstat(int indent, const char *prefix, const char *type, relation rel,
212e390e3afSDavid Malone     const char *amax, const char *objname, const char *name)
2134e115012SGarrett Wollman {
214e390e3afSDavid Malone 	const char *alt = NULL;
2154e115012SGarrett Wollman 
2164e115012SGarrett Wollman 	switch (rel) {
2174e115012SGarrett Wollman 	case REL_POINTER:
2184e115012SGarrett Wollman 		print_ifopen(indent, "pointer");
2194e115012SGarrett Wollman 		print_ifarg("(char **)");
2204e115012SGarrett Wollman 		f_print(fout, "%s", objname);
221ff49530fSBill Paul 		print_ifsizeof(0, prefix, type);
2224e115012SGarrett Wollman 		break;
2234e115012SGarrett Wollman 	case REL_VECTOR:
2244e115012SGarrett Wollman 		if (streq(type, "string")) {
2254e115012SGarrett Wollman 			alt = "string";
2264e115012SGarrett Wollman 		} else if (streq(type, "opaque")) {
2274e115012SGarrett Wollman 			alt = "opaque";
2284e115012SGarrett Wollman 		}
2294e115012SGarrett Wollman 		if (alt) {
2304e115012SGarrett Wollman 			print_ifopen(indent, alt);
2314e115012SGarrett Wollman 			print_ifarg(objname);
2324e115012SGarrett Wollman 		} else {
2334e115012SGarrett Wollman 			print_ifopen(indent, "vector");
2344e115012SGarrett Wollman 			print_ifarg("(char *)");
2354e115012SGarrett Wollman 			f_print(fout, "%s", objname);
2364e115012SGarrett Wollman 		}
2374e115012SGarrett Wollman 		print_ifarg(amax);
2384e115012SGarrett Wollman 		if (!alt) {
239ff49530fSBill Paul 			print_ifsizeof(indent + 1, prefix, type);
2404e115012SGarrett Wollman 		}
2414e115012SGarrett Wollman 		break;
2424e115012SGarrett Wollman 	case REL_ARRAY:
2434e115012SGarrett Wollman 		if (streq(type, "string")) {
2444e115012SGarrett Wollman 			alt = "string";
2454e115012SGarrett Wollman 		} else if (streq(type, "opaque")) {
2464e115012SGarrett Wollman 			alt = "bytes";
2474e115012SGarrett Wollman 		}
2484e115012SGarrett Wollman 		if (streq(type, "string")) {
2494e115012SGarrett Wollman 			print_ifopen(indent, alt);
2504e115012SGarrett Wollman 			print_ifarg(objname);
2514e115012SGarrett Wollman 		} else {
2524e115012SGarrett Wollman 			if (alt) {
2534e115012SGarrett Wollman 				print_ifopen(indent, alt);
2544e115012SGarrett Wollman 			} else {
2554e115012SGarrett Wollman 				print_ifopen(indent, "array");
2564e115012SGarrett Wollman 			}
2574e115012SGarrett Wollman 			print_ifarg("(char **)");
2584e115012SGarrett Wollman 			if (*objname == '&') {
2594e115012SGarrett Wollman 				f_print(fout, "%s.%s_val, (u_int *) %s.%s_len",
2604e115012SGarrett Wollman 					objname, name, objname, name);
2614e115012SGarrett Wollman 			} else {
262ff49530fSBill Paul 				f_print(fout,
263ff49530fSBill Paul 					"&%s->%s_val, (u_int *) &%s->%s_len",
2644e115012SGarrett Wollman 					objname, name, objname, name);
2654e115012SGarrett Wollman 			}
2664e115012SGarrett Wollman 		}
2674e115012SGarrett Wollman 		print_ifarg(amax);
2684e115012SGarrett Wollman 		if (!alt) {
269ff49530fSBill Paul 			print_ifsizeof(indent + 1, prefix, type);
2704e115012SGarrett Wollman 		}
2714e115012SGarrett Wollman 		break;
2724e115012SGarrett Wollman 	case REL_ALIAS:
2734e115012SGarrett Wollman 		print_ifopen(indent, type);
2744e115012SGarrett Wollman 		print_ifarg(objname);
2754e115012SGarrett Wollman 		break;
2764e115012SGarrett Wollman 	}
2774e115012SGarrett Wollman 	print_ifclose(indent);
2784e115012SGarrett Wollman }
2794e115012SGarrett Wollman 
2804e115012SGarrett Wollman /* ARGSUSED */
281526195adSJordan K. Hubbard static void
282e390e3afSDavid Malone emit_enum(definition *def __unused)
2834e115012SGarrett Wollman {
2844e115012SGarrett Wollman 	print_ifopen(1, "enum");
2854e115012SGarrett Wollman 	print_ifarg("(enum_t *)objp");
2864e115012SGarrett Wollman 	print_ifclose(1);
2874e115012SGarrett Wollman }
2884e115012SGarrett Wollman 
289526195adSJordan K. Hubbard static void
290e390e3afSDavid Malone emit_program(definition *def)
291ff49530fSBill Paul {
292ff49530fSBill Paul 	decl_list *dl;
293ff49530fSBill Paul 	version_list *vlist;
294ff49530fSBill Paul 	proc_list *plist;
295ff49530fSBill Paul 
296ff49530fSBill Paul 	for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
297ff49530fSBill Paul 		for (plist = vlist->procs; plist != NULL; plist = plist->next) {
298ff49530fSBill Paul 			if (!newstyle || plist->arg_num < 2)
299ff49530fSBill Paul 				continue; /* old style, or single argument */
300ff49530fSBill Paul 			print_prog_header(plist);
301ff49530fSBill Paul 			for (dl = plist->args.decls; dl != NULL;
302ff49530fSBill Paul 			     dl = dl->next)
303ff49530fSBill Paul 				print_stat(1, &dl->decl);
304ff49530fSBill Paul 			print_trailer();
305ff49530fSBill Paul 		}
306ff49530fSBill Paul }
307ff49530fSBill Paul 
3084e115012SGarrett Wollman 
309526195adSJordan K. Hubbard static void
310e390e3afSDavid Malone emit_union(definition *def)
3114e115012SGarrett Wollman {
3124e115012SGarrett Wollman 	declaration *dflt;
3134e115012SGarrett Wollman 	case_list *cl;
3144e115012SGarrett Wollman 	declaration *cs;
3154e115012SGarrett Wollman 	char *object;
316e390e3afSDavid Malone 	const char *vecformat = "objp->%s_u.%s";
317e390e3afSDavid Malone 	const char *format = "&objp->%s_u.%s";
3184e115012SGarrett Wollman 
319ff49530fSBill Paul 	print_stat(1, &def->def.un.enum_decl);
3204e115012SGarrett Wollman 	f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
3214e115012SGarrett Wollman 	for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
322ff49530fSBill Paul 
3234e115012SGarrett Wollman 		f_print(fout, "\tcase %s:\n", cl->case_name);
324ff49530fSBill Paul 		if (cl->contflag == 1) /* a continued case statement */
325ff49530fSBill Paul 			continue;
326ff49530fSBill Paul 		cs = &cl->case_decl;
3274e115012SGarrett Wollman 		if (!streq(cs->type, "void")) {
32875863a6dSPhilippe Charnier 			object = xmalloc(strlen(def->def_name) +
32975863a6dSPhilippe Charnier 			                 strlen(format) + strlen(cs->name) + 1);
330ff49530fSBill Paul 			if (isvectordef (cs->type, cs->rel)) {
331ff49530fSBill Paul 				s_print(object, vecformat, def->def_name,
332ff49530fSBill Paul 					cs->name);
333ff49530fSBill Paul 			} else {
334ff49530fSBill Paul 				s_print(object, format, def->def_name,
335ff49530fSBill Paul 					cs->name);
336ff49530fSBill Paul 			}
337ff49530fSBill Paul 			print_ifstat(2, cs->prefix, cs->type, cs->rel,
338ff49530fSBill Paul 				     cs->array_max, object, cs->name);
3394e115012SGarrett Wollman 			free(object);
3404e115012SGarrett Wollman 		}
3414e115012SGarrett Wollman 		f_print(fout, "\t\tbreak;\n");
3424e115012SGarrett Wollman 	}
3434e115012SGarrett Wollman 	dflt = def->def.un.default_decl;
3444e115012SGarrett Wollman 	if (dflt != NULL) {
3454e115012SGarrett Wollman 		if (!streq(dflt->type, "void")) {
3464e115012SGarrett Wollman 			f_print(fout, "\tdefault:\n");
34775863a6dSPhilippe Charnier 			object = xmalloc(strlen(def->def_name) +
34875863a6dSPhilippe Charnier 			                 strlen(format) + strlen(dflt->name) + 1);
349ff49530fSBill Paul 			if (isvectordef (dflt->type, dflt->rel)) {
350ff49530fSBill Paul 				s_print(object, vecformat, def->def_name,
351ff49530fSBill Paul 					dflt->name);
352ff49530fSBill Paul 			} else {
353ff49530fSBill Paul 				s_print(object, format, def->def_name,
354ff49530fSBill Paul 					dflt->name);
355ff49530fSBill Paul 			}
356ff49530fSBill Paul 
3574e115012SGarrett Wollman 			print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
3584e115012SGarrett Wollman 				    dflt->array_max, object, dflt->name);
3594e115012SGarrett Wollman 			free(object);
3604e115012SGarrett Wollman 			f_print(fout, "\t\tbreak;\n");
361526195adSJordan K. Hubbard 		} else {
362526195adSJordan K. Hubbard 			f_print(fout, "\tdefault:\n");
363526195adSJordan K. Hubbard 			f_print(fout, "\t\tbreak;\n");
3644e115012SGarrett Wollman 		}
3654e115012SGarrett Wollman 	} else {
3664e115012SGarrett Wollman 		f_print(fout, "\tdefault:\n");
3674e115012SGarrett Wollman 		f_print(fout, "\t\treturn (FALSE);\n");
3684e115012SGarrett Wollman 	}
369ff49530fSBill Paul 
3704e115012SGarrett Wollman 	f_print(fout, "\t}\n");
3714e115012SGarrett Wollman }
3724e115012SGarrett Wollman 
373ff49530fSBill Paul static void
374e390e3afSDavid Malone inline_struct(definition *def, int flag)
375ff49530fSBill Paul {
376ff49530fSBill Paul 	decl_list *dl;
377ff49530fSBill Paul 	int i, size;
378ff49530fSBill Paul 	decl_list *cur, *psav;
379ff49530fSBill Paul 	bas_type *ptr;
380e390e3afSDavid Malone 	char *sizestr;
381e390e3afSDavid Malone 	const char *plus;
382ff49530fSBill Paul 	char ptemp[256];
383ff49530fSBill Paul 	int indent = 1;
3844e115012SGarrett Wollman 
38540ad8885SAlfred Perlstein 	cur = NULL;
386ff49530fSBill Paul 	if (flag == PUT)
387ff49530fSBill Paul 		f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
388ff49530fSBill Paul 	else
389ff49530fSBill Paul 		f_print(fout, "\t\treturn (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
390ff49530fSBill Paul 
391ff49530fSBill Paul 	i = 0;
392ff49530fSBill Paul 	size = 0;
393ff49530fSBill Paul 	sizestr = NULL;
394ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
395ff49530fSBill Paul 		/* now walk down the list and check for basic types */
396ff49530fSBill Paul 		if ((dl->decl.prefix == NULL) &&
397ff49530fSBill Paul 		    ((ptr = find_type(dl->decl.type)) != NULL) &&
398ff49530fSBill Paul 		    ((dl->decl.rel == REL_ALIAS) ||
399ff49530fSBill Paul 		     (dl->decl.rel == REL_VECTOR))){
400ff49530fSBill Paul 			if (i == 0)
401ff49530fSBill Paul 				cur = dl;
402ff49530fSBill Paul 			i++;
403ff49530fSBill Paul 
404ff49530fSBill Paul 			if (dl->decl.rel == REL_ALIAS)
405ff49530fSBill Paul 				size += ptr->length;
406ff49530fSBill Paul 			else {
407ff49530fSBill Paul 				/* this code is required to handle arrays */
408ff49530fSBill Paul 				if (sizestr == NULL)
409ff49530fSBill Paul 					plus = "";
410ff49530fSBill Paul 				else
411ff49530fSBill Paul 					plus = " + ";
412ff49530fSBill Paul 
413ff49530fSBill Paul 				if (ptr->length != 1)
414ff49530fSBill Paul 					s_print(ptemp, "%s%s * %d",
415ff49530fSBill Paul 						plus, dl->decl.array_max,
416ff49530fSBill Paul 						ptr->length);
417ff49530fSBill Paul 				else
418ff49530fSBill Paul 					s_print(ptemp, "%s%s", plus,
419ff49530fSBill Paul 						dl->decl.array_max);
420ff49530fSBill Paul 
421ff49530fSBill Paul 				/* now concatenate to sizestr !!!! */
42275863a6dSPhilippe Charnier 				if (sizestr == NULL) {
42375863a6dSPhilippe Charnier 					sizestr = xstrdup(ptemp);
42475863a6dSPhilippe Charnier 				}
425ff49530fSBill Paul 				else{
42675863a6dSPhilippe Charnier 					sizestr = xrealloc(sizestr,
427ff49530fSBill Paul 							  strlen(sizestr)
428ff49530fSBill Paul 							  +strlen(ptemp)+1);
429ff49530fSBill Paul 					sizestr = strcat(sizestr, ptemp);
430ff49530fSBill Paul 					/* build up length of array */
431ff49530fSBill Paul 				}
432ff49530fSBill Paul 			}
433ff49530fSBill Paul 		} else {
4349ef5c48bSBill Fumerola 			if (i > 0) {
435122562cdSStefan Farfeleder 				if (sizestr == NULL && size < inline_size){
436ff49530fSBill Paul 					/*
437ff49530fSBill Paul 					 * don't expand into inline code
438122562cdSStefan Farfeleder 					 * if size < inline_size
439ff49530fSBill Paul 					 */
440ff49530fSBill Paul 					while (cur != dl){
441ff49530fSBill Paul 						print_stat(indent + 1, &cur->decl);
442ff49530fSBill Paul 						cur = cur->next;
443ff49530fSBill Paul 					}
444ff49530fSBill Paul 				} else {
445ff49530fSBill Paul 					/* were already looking at a xdr_inlineable structure */
446ff49530fSBill Paul 					tabify(fout, indent + 1);
447ff49530fSBill Paul 					if (sizestr == NULL)
448ff49530fSBill Paul 						f_print(fout, "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
449ff49530fSBill Paul 							size);
4509ef5c48bSBill Fumerola 					else {
451ff49530fSBill Paul 						if (size == 0)
452ff49530fSBill Paul 							f_print(fout,
453ff49530fSBill Paul 								"buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
454ff49530fSBill Paul 								sizestr);
455ff49530fSBill Paul 						else
456ff49530fSBill Paul 							f_print(fout,
457ff49530fSBill Paul 								"buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
458ff49530fSBill Paul 								size, sizestr);
459ff49530fSBill Paul 
4609ef5c48bSBill Fumerola 					}
461ff49530fSBill Paul 					f_print(fout, "\n");
462ff49530fSBill Paul 					tabify(fout, indent + 1);
463ff49530fSBill Paul 					f_print(fout,
464ff49530fSBill Paul 						"if (buf == NULL) {\n");
465ff49530fSBill Paul 
466ff49530fSBill Paul 					psav = cur;
467ff49530fSBill Paul 					while (cur != dl){
468ff49530fSBill Paul 						print_stat(indent + 2, &cur->decl);
469ff49530fSBill Paul 						cur = cur->next;
470ff49530fSBill Paul 					}
471ff49530fSBill Paul 
472ff49530fSBill Paul 					f_print(fout, "\n\t\t} else {\n");
473ff49530fSBill Paul 
474ff49530fSBill Paul 					cur = psav;
475ff49530fSBill Paul 					while (cur != dl){
476ff49530fSBill Paul 						emit_inline(indent + 2, &cur->decl, flag);
477ff49530fSBill Paul 						cur = cur->next;
478ff49530fSBill Paul 					}
479ff49530fSBill Paul 
480ff49530fSBill Paul 					tabify(fout, indent + 1);
481ff49530fSBill Paul 					f_print(fout, "}\n");
482ff49530fSBill Paul 				}
4839ef5c48bSBill Fumerola 			}
484ff49530fSBill Paul 			size = 0;
485ff49530fSBill Paul 			i = 0;
486ff49530fSBill Paul 			sizestr = NULL;
487ff49530fSBill Paul 			print_stat(indent + 1, &dl->decl);
488ff49530fSBill Paul 		}
489ff49530fSBill Paul 	}
490ff49530fSBill Paul 
49140ad8885SAlfred Perlstein 	if (i > 0) {
492122562cdSStefan Farfeleder 		if (sizestr == NULL && size < inline_size){
493122562cdSStefan Farfeleder 			/* don't expand into inline code if size < inline_size */
494ff49530fSBill Paul 			while (cur != dl){
495ff49530fSBill Paul 				print_stat(indent + 1, &cur->decl);
496ff49530fSBill Paul 				cur = cur->next;
497ff49530fSBill Paul 			}
498ff49530fSBill Paul 		} else {
499ff49530fSBill Paul 			/* were already looking at a xdr_inlineable structure */
500ff49530fSBill Paul 			if (sizestr == NULL)
501ff49530fSBill Paul 				f_print(fout, "\t\tbuf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
502ff49530fSBill Paul 					size);
503ff49530fSBill Paul 			else
504ff49530fSBill Paul 				if (size == 0)
505ff49530fSBill Paul 					f_print(fout,
506ff49530fSBill Paul 						"\t\tbuf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
507ff49530fSBill Paul 						sizestr);
508ff49530fSBill Paul 				else
509ff49530fSBill Paul 					f_print(fout,
510ff49530fSBill Paul 						"\t\tbuf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
511ff49530fSBill Paul 						size, sizestr);
512ff49530fSBill Paul 
513ff49530fSBill Paul 			f_print(fout, "\n\t\tif (buf == NULL) {\n");
514ff49530fSBill Paul 			psav = cur;
515ff49530fSBill Paul 			while (cur != NULL){
516ff49530fSBill Paul 				print_stat(indent + 2, &cur->decl);
517ff49530fSBill Paul 				cur = cur->next;
518ff49530fSBill Paul 			}
519ff49530fSBill Paul 			f_print(fout, "\t\t} else {\n");
520ff49530fSBill Paul 
521ff49530fSBill Paul 			cur = psav;
522ff49530fSBill Paul 			while (cur != dl){
523ff49530fSBill Paul 				emit_inline(indent + 2, &cur->decl, flag);
524ff49530fSBill Paul 				cur = cur->next;
525ff49530fSBill Paul 			}
526ff49530fSBill Paul 			f_print(fout, "\t\t}\n");
527ff49530fSBill Paul 		}
528ff49530fSBill Paul 	}
52940ad8885SAlfred Perlstein }
5304e115012SGarrett Wollman 
531526195adSJordan K. Hubbard static void
532e390e3afSDavid Malone emit_struct(definition *def)
5334e115012SGarrett Wollman {
5344e115012SGarrett Wollman 	decl_list *dl;
535526195adSJordan K. Hubbard 	int j, size, flag;
536ff49530fSBill Paul 	bas_type *ptr;
537ff49530fSBill Paul 	int can_inline;
5384e115012SGarrett Wollman 
539122562cdSStefan Farfeleder 	if (inline_size == 0) {
540ff49530fSBill Paul 		/* No xdr_inlining at all */
541ff49530fSBill Paul 		for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
542ff49530fSBill Paul 			print_stat(1, &dl->decl);
543ff49530fSBill Paul 		return;
5444e115012SGarrett Wollman 	}
5454e115012SGarrett Wollman 
546ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
547ff49530fSBill Paul 		if (dl->decl.rel == REL_VECTOR){
548ff49530fSBill Paul 			f_print(fout, "\tint i;\n");
549ff49530fSBill Paul 			break;
550ff49530fSBill Paul 		}
5514e115012SGarrett Wollman 
552ff49530fSBill Paul 	size = 0;
553ff49530fSBill Paul 	can_inline = 0;
554ff49530fSBill Paul 	/*
555ff49530fSBill Paul 	 * Make a first pass and see if inling is possible.
556ff49530fSBill Paul 	 */
557ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
558ff49530fSBill Paul 		if ((dl->decl.prefix == NULL) &&
559ff49530fSBill Paul 		    ((ptr = find_type(dl->decl.type)) != NULL) &&
560ff49530fSBill Paul 		    ((dl->decl.rel == REL_ALIAS)||
561ff49530fSBill Paul 		     (dl->decl.rel == REL_VECTOR))){
562ff49530fSBill Paul 			if (dl->decl.rel == REL_ALIAS)
563ff49530fSBill Paul 				size += ptr->length;
564ff49530fSBill Paul 			else {
565ff49530fSBill Paul 				can_inline = 1;
566ff49530fSBill Paul 				break; /* can be inlined */
567ff49530fSBill Paul 			}
568ff49530fSBill Paul 		} else {
569122562cdSStefan Farfeleder 			if (size >= inline_size){
570ff49530fSBill Paul 				can_inline = 1;
571ff49530fSBill Paul 				break; /* can be inlined */
572ff49530fSBill Paul 			}
573ff49530fSBill Paul 			size = 0;
574ff49530fSBill Paul 		}
575122562cdSStefan Farfeleder 	if (size >= inline_size)
576ff49530fSBill Paul 		can_inline = 1;
5774e115012SGarrett Wollman 
578ff49530fSBill Paul 	if (can_inline == 0){	/* can not inline, drop back to old mode */
579ff49530fSBill Paul 		for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
580ff49530fSBill Paul 			print_stat(1, &dl->decl);
581ff49530fSBill Paul 		return;
582ff49530fSBill Paul 	}
583ff49530fSBill Paul 
584ff49530fSBill Paul 	flag = PUT;
585ff49530fSBill Paul 	for (j = 0; j < 2; j++){
586ff49530fSBill Paul 		inline_struct(def, flag);
587ff49530fSBill Paul 		if (flag == PUT)
588ff49530fSBill Paul 			flag = GET;
589ff49530fSBill Paul 	}
590ff49530fSBill Paul 
591ff49530fSBill Paul 	f_print(fout, "\t\treturn (TRUE);\n\t}\n\n");
592ff49530fSBill Paul 
593ff49530fSBill Paul 	/* now take care of XDR_FREE case */
594ff49530fSBill Paul 
595ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
596ff49530fSBill Paul 		print_stat(1, &dl->decl);
597ff49530fSBill Paul 
598ff49530fSBill Paul }
5994e115012SGarrett Wollman 
600526195adSJordan K. Hubbard static void
601e390e3afSDavid Malone emit_typedef(definition *def)
6024e115012SGarrett Wollman {
603e390e3afSDavid Malone 	const char *prefix = def->def.ty.old_prefix;
604e390e3afSDavid Malone 	const char *type = def->def.ty.old_type;
605e390e3afSDavid Malone 	const char *amax = def->def.ty.array_max;
6064e115012SGarrett Wollman 	relation rel = def->def.ty.rel;
6074e115012SGarrett Wollman 
6084e115012SGarrett Wollman 	print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
6094e115012SGarrett Wollman }
6104e115012SGarrett Wollman 
611526195adSJordan K. Hubbard static void
612e390e3afSDavid Malone print_stat(int indent, declaration *dec)
6134e115012SGarrett Wollman {
614e390e3afSDavid Malone 	const char *prefix = dec->prefix;
615e390e3afSDavid Malone 	const char *type = dec->type;
616e390e3afSDavid Malone 	const char *amax = dec->array_max;
6174e115012SGarrett Wollman 	relation rel = dec->rel;
6184e115012SGarrett Wollman 	char name[256];
6194e115012SGarrett Wollman 
6204e115012SGarrett Wollman 	if (isvectordef(type, rel)) {
6214e115012SGarrett Wollman 		s_print(name, "objp->%s", dec->name);
6224e115012SGarrett Wollman 	} else {
6234e115012SGarrett Wollman 		s_print(name, "&objp->%s", dec->name);
6244e115012SGarrett Wollman 	}
625ff49530fSBill Paul 	print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
626ff49530fSBill Paul }
627ff49530fSBill Paul 
628ff49530fSBill Paul 
629e390e3afSDavid Malone char *upcase(const char *);
630ff49530fSBill Paul 
631526195adSJordan K. Hubbard static void
632e390e3afSDavid Malone emit_inline(int indent, declaration *decl, int flag)
633ff49530fSBill Paul {
634ff49530fSBill Paul 	switch (decl->rel) {
635ff49530fSBill Paul 	case  REL_ALIAS :
636ff49530fSBill Paul 		emit_single_in_line(indent, decl, flag, REL_ALIAS);
637ff49530fSBill Paul 		break;
638ff49530fSBill Paul 	case REL_VECTOR :
639ff49530fSBill Paul 		tabify(fout, indent);
640ff49530fSBill Paul 		f_print(fout, "{\n");
641ff49530fSBill Paul 		tabify(fout, indent + 1);
642896cdc31SStefan Farfeleder 		f_print(fout, "%s *genp;\n\n", decl->type);
643ff49530fSBill Paul 		tabify(fout, indent + 1);
644ff49530fSBill Paul 		f_print(fout,
645ff49530fSBill Paul 			"for (i = 0, genp = objp->%s;\n", decl->name);
646ff49530fSBill Paul 		tabify(fout, indent + 2);
647ff49530fSBill Paul 		f_print(fout, "i < %s; i++) {\n", decl->array_max);
648ff49530fSBill Paul 		emit_single_in_line(indent + 2, decl, flag, REL_VECTOR);
649ff49530fSBill Paul 		tabify(fout, indent + 1);
650ff49530fSBill Paul 		f_print(fout, "}\n");
651ff49530fSBill Paul 		tabify(fout, indent);
652ff49530fSBill Paul 		f_print(fout, "}\n");
65340ad8885SAlfred Perlstein 		break;
654526195adSJordan K. Hubbard 	default:
65540ad8885SAlfred Perlstein 		break;
656ff49530fSBill Paul 	}
657ff49530fSBill Paul }
658ff49530fSBill Paul 
659526195adSJordan K. Hubbard static void
660e390e3afSDavid Malone emit_single_in_line(int indent, declaration *decl, int flag, relation rel)
661ff49530fSBill Paul {
662ff49530fSBill Paul 	char *upp_case;
663ff49530fSBill Paul 
664ff49530fSBill Paul 	tabify(fout, indent);
665ff49530fSBill Paul 	if (flag == PUT)
666ff49530fSBill Paul 		f_print(fout, "IXDR_PUT_");
667ff49530fSBill Paul 	else
668ff49530fSBill Paul 		if (rel == REL_ALIAS)
669ff49530fSBill Paul 			f_print(fout, "objp->%s = IXDR_GET_", decl->name);
670ff49530fSBill Paul 		else
671ff49530fSBill Paul 			f_print(fout, "*genp++ = IXDR_GET_");
672ff49530fSBill Paul 
673ff49530fSBill Paul 	upp_case = upcase(decl->type);
674ff49530fSBill Paul 
675ff49530fSBill Paul 	/* hack	 - XX */
676ff49530fSBill Paul 	if (strcmp(upp_case, "INT") == 0)
677ff49530fSBill Paul 	{
678ff49530fSBill Paul 		free(upp_case);
679e390e3afSDavid Malone 		upp_case = strdup("LONG");
680ff49530fSBill Paul 	}
681ff49530fSBill Paul 
682ff49530fSBill Paul 	if (strcmp(upp_case, "U_INT") == 0)
683ff49530fSBill Paul 	{
684ff49530fSBill Paul 		free(upp_case);
685e390e3afSDavid Malone 		upp_case = strdup("U_LONG");
686ff49530fSBill Paul 	}
687ff49530fSBill Paul 	if (flag == PUT)
688ff49530fSBill Paul 		if (rel == REL_ALIAS)
689ff49530fSBill Paul 			f_print(fout,
690ff49530fSBill Paul 				"%s(buf, objp->%s);\n", upp_case, decl->name);
691ff49530fSBill Paul 		else
692ff49530fSBill Paul 			f_print(fout, "%s(buf, *genp++);\n", upp_case);
693ff49530fSBill Paul 
694ff49530fSBill Paul 	else
695ff49530fSBill Paul 		f_print(fout, "%s(buf);\n", upp_case);
696ff49530fSBill Paul 	free(upp_case);
697ff49530fSBill Paul }
698ff49530fSBill Paul 
699e390e3afSDavid Malone char *
700e390e3afSDavid Malone upcase(const char *str)
701ff49530fSBill Paul {
702ff49530fSBill Paul 	char *ptr, *hptr;
703ff49530fSBill Paul 
70475863a6dSPhilippe Charnier 	ptr = (char *)xmalloc(strlen(str)+1);
705ff49530fSBill Paul 
706ff49530fSBill Paul 	hptr = ptr;
707ff49530fSBill Paul 	while (*str != '\0')
708ff49530fSBill Paul 		*ptr++ = toupper(*str++);
709ff49530fSBill Paul 
710ff49530fSBill Paul 	*ptr = '\0';
711ff49530fSBill Paul 	return (hptr);
7124e115012SGarrett Wollman }
713