xref: /freebsd/usr.bin/rpcgen/rpc_cout.c (revision e9ed334f)
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;
8480c7cc1cSPedro F. Giffuni 	}
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
203844b7833SJohn Birrell print_ifclose(int indent, int brace)
2044e115012SGarrett Wollman {
205ff49530fSBill Paul 	f_print(fout, "))\n");
2064e115012SGarrett Wollman 	tabify(fout, indent);
2074e115012SGarrett Wollman 	f_print(fout, "\treturn (FALSE);\n");
208844b7833SJohn Birrell 	if (brace)
209844b7833SJohn Birrell 		f_print(fout, "\t}\n");
2104e115012SGarrett Wollman }
2114e115012SGarrett Wollman 
212526195adSJordan K. Hubbard static void
213e390e3afSDavid Malone print_ifstat(int indent, const char *prefix, const char *type, relation rel,
214e390e3afSDavid Malone     const char *amax, const char *objname, const char *name)
2154e115012SGarrett Wollman {
216e390e3afSDavid Malone 	const char *alt = NULL;
217844b7833SJohn Birrell 	int brace = 0;
2184e115012SGarrett Wollman 
2194e115012SGarrett Wollman 	switch (rel) {
2204e115012SGarrett Wollman 	case REL_POINTER:
221844b7833SJohn Birrell 		brace = 1;
222844b7833SJohn Birrell 		f_print(fout, "\t{\n");
223844b7833SJohn Birrell 		f_print(fout, "\t%s **pp = %s;\n", type, objname);
2244e115012SGarrett Wollman 		print_ifopen(indent, "pointer");
2254e115012SGarrett Wollman 		print_ifarg("(char **)");
226844b7833SJohn Birrell 		f_print(fout, "pp");
227ff49530fSBill Paul 		print_ifsizeof(0, prefix, type);
2284e115012SGarrett Wollman 		break;
2294e115012SGarrett Wollman 	case REL_VECTOR:
2304e115012SGarrett Wollman 		if (streq(type, "string")) {
2314e115012SGarrett Wollman 			alt = "string";
2324e115012SGarrett Wollman 		} else if (streq(type, "opaque")) {
2334e115012SGarrett Wollman 			alt = "opaque";
2344e115012SGarrett Wollman 		}
2354e115012SGarrett Wollman 		if (alt) {
2364e115012SGarrett Wollman 			print_ifopen(indent, alt);
2374e115012SGarrett Wollman 			print_ifarg(objname);
2384e115012SGarrett Wollman 		} else {
2394e115012SGarrett Wollman 			print_ifopen(indent, "vector");
2404e115012SGarrett Wollman 			print_ifarg("(char *)");
2414e115012SGarrett Wollman 			f_print(fout, "%s", objname);
2424e115012SGarrett Wollman 		}
2434e115012SGarrett Wollman 		print_ifarg(amax);
2444e115012SGarrett Wollman 		if (!alt) {
245ff49530fSBill Paul 			print_ifsizeof(indent + 1, prefix, type);
2464e115012SGarrett Wollman 		}
2474e115012SGarrett Wollman 		break;
2484e115012SGarrett Wollman 	case REL_ARRAY:
2494e115012SGarrett Wollman 		if (streq(type, "string")) {
2504e115012SGarrett Wollman 			alt = "string";
2514e115012SGarrett Wollman 		} else if (streq(type, "opaque")) {
2524e115012SGarrett Wollman 			alt = "bytes";
2534e115012SGarrett Wollman 		}
2544e115012SGarrett Wollman 		if (streq(type, "string")) {
2554e115012SGarrett Wollman 			print_ifopen(indent, alt);
2564e115012SGarrett Wollman 			print_ifarg(objname);
2574e115012SGarrett Wollman 		} else {
2584e115012SGarrett Wollman 			if (alt) {
2594e115012SGarrett Wollman 				print_ifopen(indent, alt);
2604e115012SGarrett Wollman 			} else {
2614e115012SGarrett Wollman 				print_ifopen(indent, "array");
2624e115012SGarrett Wollman 			}
2634e115012SGarrett Wollman 			print_ifarg("(char **)");
2644e115012SGarrett Wollman 			if (*objname == '&') {
2654e115012SGarrett Wollman 				f_print(fout, "%s.%s_val, (u_int *) %s.%s_len",
2664e115012SGarrett Wollman 					objname, name, objname, name);
2674e115012SGarrett Wollman 			} else {
268ff49530fSBill Paul 				f_print(fout,
269ff49530fSBill Paul 					"&%s->%s_val, (u_int *) &%s->%s_len",
2704e115012SGarrett Wollman 					objname, name, objname, name);
2714e115012SGarrett Wollman 			}
2724e115012SGarrett Wollman 		}
2734e115012SGarrett Wollman 		print_ifarg(amax);
2744e115012SGarrett Wollman 		if (!alt) {
275ff49530fSBill Paul 			print_ifsizeof(indent + 1, prefix, type);
2764e115012SGarrett Wollman 		}
2774e115012SGarrett Wollman 		break;
2784e115012SGarrett Wollman 	case REL_ALIAS:
2794e115012SGarrett Wollman 		print_ifopen(indent, type);
2804e115012SGarrett Wollman 		print_ifarg(objname);
2814e115012SGarrett Wollman 		break;
2824e115012SGarrett Wollman 	}
283844b7833SJohn Birrell 	print_ifclose(indent, brace);
2844e115012SGarrett Wollman }
2854e115012SGarrett Wollman 
2864e115012SGarrett Wollman /* ARGSUSED */
287526195adSJordan K. Hubbard static void
288e390e3afSDavid Malone emit_enum(definition *def __unused)
2894e115012SGarrett Wollman {
2904e115012SGarrett Wollman 	print_ifopen(1, "enum");
2914e115012SGarrett Wollman 	print_ifarg("(enum_t *)objp");
292844b7833SJohn Birrell 	print_ifclose(1, 0);
2934e115012SGarrett Wollman }
2944e115012SGarrett Wollman 
295526195adSJordan K. Hubbard static void
296e390e3afSDavid Malone emit_program(definition *def)
297ff49530fSBill Paul {
298ff49530fSBill Paul 	decl_list *dl;
299ff49530fSBill Paul 	version_list *vlist;
300ff49530fSBill Paul 	proc_list *plist;
301ff49530fSBill Paul 
302ff49530fSBill Paul 	for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
303ff49530fSBill Paul 		for (plist = vlist->procs; plist != NULL; plist = plist->next) {
304ff49530fSBill Paul 			if (!newstyle || plist->arg_num < 2)
305ff49530fSBill Paul 				continue; /* old style, or single argument */
306ff49530fSBill Paul 			print_prog_header(plist);
307ff49530fSBill Paul 			for (dl = plist->args.decls; dl != NULL;
308ff49530fSBill Paul 			     dl = dl->next)
309ff49530fSBill Paul 				print_stat(1, &dl->decl);
310ff49530fSBill Paul 			print_trailer();
311ff49530fSBill Paul 		}
312ff49530fSBill Paul }
313ff49530fSBill Paul 
3144e115012SGarrett Wollman 
315526195adSJordan K. Hubbard static void
316e390e3afSDavid Malone emit_union(definition *def)
3174e115012SGarrett Wollman {
3184e115012SGarrett Wollman 	declaration *dflt;
3194e115012SGarrett Wollman 	case_list *cl;
3204e115012SGarrett Wollman 	declaration *cs;
3214e115012SGarrett Wollman 	char *object;
322e390e3afSDavid Malone 	const char *vecformat = "objp->%s_u.%s";
323e390e3afSDavid Malone 	const char *format = "&objp->%s_u.%s";
3244e115012SGarrett Wollman 
325ff49530fSBill Paul 	print_stat(1, &def->def.un.enum_decl);
3264e115012SGarrett Wollman 	f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
3274e115012SGarrett Wollman 	for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
328ff49530fSBill Paul 
3294e115012SGarrett Wollman 		f_print(fout, "\tcase %s:\n", cl->case_name);
330ff49530fSBill Paul 		if (cl->contflag == 1) /* a continued case statement */
331ff49530fSBill Paul 			continue;
332ff49530fSBill Paul 		cs = &cl->case_decl;
3334e115012SGarrett Wollman 		if (!streq(cs->type, "void")) {
33475863a6dSPhilippe Charnier 			object = xmalloc(strlen(def->def_name) +
33575863a6dSPhilippe Charnier 			                 strlen(format) + strlen(cs->name) + 1);
336ff49530fSBill Paul 			if (isvectordef (cs->type, cs->rel)) {
337ff49530fSBill Paul 				s_print(object, vecformat, def->def_name,
338ff49530fSBill Paul 					cs->name);
339ff49530fSBill Paul 			} else {
340ff49530fSBill Paul 				s_print(object, format, def->def_name,
341ff49530fSBill Paul 					cs->name);
342ff49530fSBill Paul 			}
343ff49530fSBill Paul 			print_ifstat(2, cs->prefix, cs->type, cs->rel,
344ff49530fSBill Paul 				     cs->array_max, object, cs->name);
3454e115012SGarrett Wollman 			free(object);
3464e115012SGarrett Wollman 		}
3474e115012SGarrett Wollman 		f_print(fout, "\t\tbreak;\n");
3484e115012SGarrett Wollman 	}
3494e115012SGarrett Wollman 	dflt = def->def.un.default_decl;
3504e115012SGarrett Wollman 	if (dflt != NULL) {
3514e115012SGarrett Wollman 		if (!streq(dflt->type, "void")) {
3524e115012SGarrett Wollman 			f_print(fout, "\tdefault:\n");
35375863a6dSPhilippe Charnier 			object = xmalloc(strlen(def->def_name) +
35475863a6dSPhilippe Charnier 			                 strlen(format) + strlen(dflt->name) + 1);
355ff49530fSBill Paul 			if (isvectordef (dflt->type, dflt->rel)) {
356ff49530fSBill Paul 				s_print(object, vecformat, def->def_name,
357ff49530fSBill Paul 					dflt->name);
358ff49530fSBill Paul 			} else {
359ff49530fSBill Paul 				s_print(object, format, def->def_name,
360ff49530fSBill Paul 					dflt->name);
361ff49530fSBill Paul 			}
362ff49530fSBill Paul 
3634e115012SGarrett Wollman 			print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
3644e115012SGarrett Wollman 				    dflt->array_max, object, dflt->name);
3654e115012SGarrett Wollman 			free(object);
3664e115012SGarrett Wollman 			f_print(fout, "\t\tbreak;\n");
367526195adSJordan K. Hubbard 		} else {
368526195adSJordan K. Hubbard 			f_print(fout, "\tdefault:\n");
369526195adSJordan K. Hubbard 			f_print(fout, "\t\tbreak;\n");
3704e115012SGarrett Wollman 		}
3714e115012SGarrett Wollman 	} else {
3724e115012SGarrett Wollman 		f_print(fout, "\tdefault:\n");
3734e115012SGarrett Wollman 		f_print(fout, "\t\treturn (FALSE);\n");
3744e115012SGarrett Wollman 	}
375ff49530fSBill Paul 
3764e115012SGarrett Wollman 	f_print(fout, "\t}\n");
3774e115012SGarrett Wollman }
3784e115012SGarrett Wollman 
379ff49530fSBill Paul static void
380e390e3afSDavid Malone inline_struct(definition *def, int flag)
381ff49530fSBill Paul {
382ff49530fSBill Paul 	decl_list *dl;
383ff49530fSBill Paul 	int i, size;
384ff49530fSBill Paul 	decl_list *cur, *psav;
385ff49530fSBill Paul 	bas_type *ptr;
386e390e3afSDavid Malone 	char *sizestr;
387e390e3afSDavid Malone 	const char *plus;
388ff49530fSBill Paul 	char ptemp[256];
389ff49530fSBill Paul 	int indent = 1;
3904e115012SGarrett Wollman 
39140ad8885SAlfred Perlstein 	cur = NULL;
392ff49530fSBill Paul 	if (flag == PUT)
393ff49530fSBill Paul 		f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
394ff49530fSBill Paul 	else
395ff49530fSBill Paul 		f_print(fout, "\t\treturn (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
396ff49530fSBill Paul 
397ff49530fSBill Paul 	i = 0;
398ff49530fSBill Paul 	size = 0;
399ff49530fSBill Paul 	sizestr = NULL;
400ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
401ff49530fSBill Paul 		/* now walk down the list and check for basic types */
402ff49530fSBill Paul 		if ((dl->decl.prefix == NULL) &&
403ff49530fSBill Paul 		    ((ptr = find_type(dl->decl.type)) != NULL) &&
404ff49530fSBill Paul 		    ((dl->decl.rel == REL_ALIAS) ||
405ff49530fSBill Paul 		     (dl->decl.rel == REL_VECTOR))){
406ff49530fSBill Paul 			if (i == 0)
407ff49530fSBill Paul 				cur = dl;
408ff49530fSBill Paul 			i++;
409ff49530fSBill Paul 
410ff49530fSBill Paul 			if (dl->decl.rel == REL_ALIAS)
411ff49530fSBill Paul 				size += ptr->length;
412ff49530fSBill Paul 			else {
413ff49530fSBill Paul 				/* this code is required to handle arrays */
414ff49530fSBill Paul 				if (sizestr == NULL)
415ff49530fSBill Paul 					plus = "";
416ff49530fSBill Paul 				else
417ff49530fSBill Paul 					plus = " + ";
418ff49530fSBill Paul 
419ff49530fSBill Paul 				if (ptr->length != 1)
420ff49530fSBill Paul 					s_print(ptemp, "%s%s * %d",
421ff49530fSBill Paul 						plus, dl->decl.array_max,
422ff49530fSBill Paul 						ptr->length);
423ff49530fSBill Paul 				else
424ff49530fSBill Paul 					s_print(ptemp, "%s%s", plus,
425ff49530fSBill Paul 						dl->decl.array_max);
426ff49530fSBill Paul 
427ff49530fSBill Paul 				/* now concatenate to sizestr !!!! */
42875863a6dSPhilippe Charnier 				if (sizestr == NULL) {
42975863a6dSPhilippe Charnier 					sizestr = xstrdup(ptemp);
43075863a6dSPhilippe Charnier 				}
431ff49530fSBill Paul 				else{
43275863a6dSPhilippe Charnier 					sizestr = xrealloc(sizestr,
433ff49530fSBill Paul 							  strlen(sizestr)
434ff49530fSBill Paul 							  +strlen(ptemp)+1);
435ff49530fSBill Paul 					sizestr = strcat(sizestr, ptemp);
436ff49530fSBill Paul 					/* build up length of array */
437ff49530fSBill Paul 				}
438ff49530fSBill Paul 			}
439ff49530fSBill Paul 		} else {
4409ef5c48bSBill Fumerola 			if (i > 0) {
441122562cdSStefan Farfeleder 				if (sizestr == NULL && size < inline_size){
442ff49530fSBill Paul 					/*
443ff49530fSBill Paul 					 * don't expand into inline code
444122562cdSStefan Farfeleder 					 * if size < inline_size
445ff49530fSBill Paul 					 */
446ff49530fSBill Paul 					while (cur != dl){
447ff49530fSBill Paul 						print_stat(indent + 1, &cur->decl);
448ff49530fSBill Paul 						cur = cur->next;
449ff49530fSBill Paul 					}
450ff49530fSBill Paul 				} else {
451ff49530fSBill Paul 					/* were already looking at a xdr_inlineable structure */
452ff49530fSBill Paul 					tabify(fout, indent + 1);
453ff49530fSBill Paul 					if (sizestr == NULL)
454ff49530fSBill Paul 						f_print(fout, "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
455ff49530fSBill Paul 							size);
4569ef5c48bSBill Fumerola 					else {
457ff49530fSBill Paul 						if (size == 0)
458ff49530fSBill Paul 							f_print(fout,
459ff49530fSBill Paul 								"buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
460ff49530fSBill Paul 								sizestr);
461ff49530fSBill Paul 						else
462ff49530fSBill Paul 							f_print(fout,
463ff49530fSBill Paul 								"buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
464ff49530fSBill Paul 								size, sizestr);
465ff49530fSBill Paul 
4669ef5c48bSBill Fumerola 					}
467ff49530fSBill Paul 					f_print(fout, "\n");
468ff49530fSBill Paul 					tabify(fout, indent + 1);
469ff49530fSBill Paul 					f_print(fout,
470ff49530fSBill Paul 						"if (buf == NULL) {\n");
471ff49530fSBill Paul 
472ff49530fSBill Paul 					psav = cur;
473ff49530fSBill Paul 					while (cur != dl){
474ff49530fSBill Paul 						print_stat(indent + 2, &cur->decl);
475ff49530fSBill Paul 						cur = cur->next;
476ff49530fSBill Paul 					}
477ff49530fSBill Paul 
478ff49530fSBill Paul 					f_print(fout, "\n\t\t} else {\n");
479ff49530fSBill Paul 
480ff49530fSBill Paul 					cur = psav;
481ff49530fSBill Paul 					while (cur != dl){
482ff49530fSBill Paul 						emit_inline(indent + 2, &cur->decl, flag);
483ff49530fSBill Paul 						cur = cur->next;
484ff49530fSBill Paul 					}
485ff49530fSBill Paul 
486ff49530fSBill Paul 					tabify(fout, indent + 1);
487ff49530fSBill Paul 					f_print(fout, "}\n");
488ff49530fSBill Paul 				}
4899ef5c48bSBill Fumerola 			}
490ff49530fSBill Paul 			size = 0;
491ff49530fSBill Paul 			i = 0;
4923f65dafdSXin LI 			free(sizestr);
493ff49530fSBill Paul 			sizestr = NULL;
494ff49530fSBill Paul 			print_stat(indent + 1, &dl->decl);
495ff49530fSBill Paul 		}
496ff49530fSBill Paul 	}
497ff49530fSBill Paul 
49840ad8885SAlfred Perlstein 	if (i > 0) {
499122562cdSStefan Farfeleder 		if (sizestr == NULL && size < inline_size){
500122562cdSStefan Farfeleder 			/* don't expand into inline code if size < inline_size */
501ff49530fSBill Paul 			while (cur != dl){
502ff49530fSBill Paul 				print_stat(indent + 1, &cur->decl);
503ff49530fSBill Paul 				cur = cur->next;
504ff49530fSBill Paul 			}
505ff49530fSBill Paul 		} else {
506ff49530fSBill Paul 			/* were already looking at a xdr_inlineable structure */
507ff49530fSBill Paul 			if (sizestr == NULL)
508ff49530fSBill Paul 				f_print(fout, "\t\tbuf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
509ff49530fSBill Paul 					size);
510ff49530fSBill Paul 			else
511ff49530fSBill Paul 				if (size == 0)
512ff49530fSBill Paul 					f_print(fout,
513ff49530fSBill Paul 						"\t\tbuf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
514ff49530fSBill Paul 						sizestr);
515ff49530fSBill Paul 				else
516ff49530fSBill Paul 					f_print(fout,
517ff49530fSBill Paul 						"\t\tbuf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
518ff49530fSBill Paul 						size, sizestr);
519ff49530fSBill Paul 
520ff49530fSBill Paul 			f_print(fout, "\n\t\tif (buf == NULL) {\n");
521ff49530fSBill Paul 			psav = cur;
522ff49530fSBill Paul 			while (cur != NULL){
523ff49530fSBill Paul 				print_stat(indent + 2, &cur->decl);
524ff49530fSBill Paul 				cur = cur->next;
525ff49530fSBill Paul 			}
526ff49530fSBill Paul 			f_print(fout, "\t\t} else {\n");
527ff49530fSBill Paul 
528ff49530fSBill Paul 			cur = psav;
529ff49530fSBill Paul 			while (cur != dl){
530ff49530fSBill Paul 				emit_inline(indent + 2, &cur->decl, flag);
531ff49530fSBill Paul 				cur = cur->next;
532ff49530fSBill Paul 			}
533ff49530fSBill Paul 			f_print(fout, "\t\t}\n");
534ff49530fSBill Paul 		}
535ff49530fSBill Paul 	}
53640ad8885SAlfred Perlstein }
5374e115012SGarrett Wollman 
538526195adSJordan K. Hubbard static void
539e390e3afSDavid Malone emit_struct(definition *def)
5404e115012SGarrett Wollman {
5414e115012SGarrett Wollman 	decl_list *dl;
542526195adSJordan K. Hubbard 	int j, size, flag;
543ff49530fSBill Paul 	bas_type *ptr;
544ff49530fSBill Paul 	int can_inline;
5454e115012SGarrett Wollman 
546122562cdSStefan Farfeleder 	if (inline_size == 0) {
547ff49530fSBill Paul 		/* No xdr_inlining at all */
548ff49530fSBill Paul 		for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
549ff49530fSBill Paul 			print_stat(1, &dl->decl);
550ff49530fSBill Paul 		return;
5514e115012SGarrett Wollman 	}
5524e115012SGarrett Wollman 
553ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
554e9ed334fSPedro F. Giffuni 		if (dl->decl.rel == REL_VECTOR &&
555e9ed334fSPedro F. Giffuni 		    strcmp(dl->decl.type, "opaque") != 0){
556ff49530fSBill Paul 			f_print(fout, "\tint i;\n");
557ff49530fSBill Paul 			break;
558ff49530fSBill Paul 		}
5594e115012SGarrett Wollman 
560ff49530fSBill Paul 	size = 0;
561ff49530fSBill Paul 	can_inline = 0;
562ff49530fSBill Paul 	/*
563ff49530fSBill Paul 	 * Make a first pass and see if inling is possible.
564ff49530fSBill Paul 	 */
565ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
566ff49530fSBill Paul 		if ((dl->decl.prefix == NULL) &&
567ff49530fSBill Paul 		    ((ptr = find_type(dl->decl.type)) != NULL) &&
568ff49530fSBill Paul 		    ((dl->decl.rel == REL_ALIAS)||
569ff49530fSBill Paul 		     (dl->decl.rel == REL_VECTOR))){
570ff49530fSBill Paul 			if (dl->decl.rel == REL_ALIAS)
571ff49530fSBill Paul 				size += ptr->length;
572ff49530fSBill Paul 			else {
573ff49530fSBill Paul 				can_inline = 1;
574ff49530fSBill Paul 				break; /* can be inlined */
575ff49530fSBill Paul 			}
576ff49530fSBill Paul 		} else {
577122562cdSStefan Farfeleder 			if (size >= inline_size){
578ff49530fSBill Paul 				can_inline = 1;
579ff49530fSBill Paul 				break; /* can be inlined */
580ff49530fSBill Paul 			}
581ff49530fSBill Paul 			size = 0;
582ff49530fSBill Paul 		}
583122562cdSStefan Farfeleder 	if (size >= inline_size)
584ff49530fSBill Paul 		can_inline = 1;
5854e115012SGarrett Wollman 
586ff49530fSBill Paul 	if (can_inline == 0){	/* can not inline, drop back to old mode */
587ff49530fSBill Paul 		for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
588ff49530fSBill Paul 			print_stat(1, &dl->decl);
589ff49530fSBill Paul 		return;
590ff49530fSBill Paul 	}
591ff49530fSBill Paul 
592ff49530fSBill Paul 	flag = PUT;
593ff49530fSBill Paul 	for (j = 0; j < 2; j++){
594ff49530fSBill Paul 		inline_struct(def, flag);
595ff49530fSBill Paul 		if (flag == PUT)
596ff49530fSBill Paul 			flag = GET;
597ff49530fSBill Paul 	}
598ff49530fSBill Paul 
599ff49530fSBill Paul 	f_print(fout, "\t\treturn (TRUE);\n\t}\n\n");
600ff49530fSBill Paul 
601ff49530fSBill Paul 	/* now take care of XDR_FREE case */
602ff49530fSBill Paul 
603ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
604ff49530fSBill Paul 		print_stat(1, &dl->decl);
605ff49530fSBill Paul 
606ff49530fSBill Paul }
6074e115012SGarrett Wollman 
608526195adSJordan K. Hubbard static void
609e390e3afSDavid Malone emit_typedef(definition *def)
6104e115012SGarrett Wollman {
611e390e3afSDavid Malone 	const char *prefix = def->def.ty.old_prefix;
612e390e3afSDavid Malone 	const char *type = def->def.ty.old_type;
613e390e3afSDavid Malone 	const char *amax = def->def.ty.array_max;
6144e115012SGarrett Wollman 	relation rel = def->def.ty.rel;
6154e115012SGarrett Wollman 
6164e115012SGarrett Wollman 	print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
6174e115012SGarrett Wollman }
6184e115012SGarrett Wollman 
619526195adSJordan K. Hubbard static void
620e390e3afSDavid Malone print_stat(int indent, declaration *dec)
6214e115012SGarrett Wollman {
622e390e3afSDavid Malone 	const char *prefix = dec->prefix;
623e390e3afSDavid Malone 	const char *type = dec->type;
624e390e3afSDavid Malone 	const char *amax = dec->array_max;
6254e115012SGarrett Wollman 	relation rel = dec->rel;
6264e115012SGarrett Wollman 	char name[256];
6274e115012SGarrett Wollman 
6284e115012SGarrett Wollman 	if (isvectordef(type, rel)) {
6294e115012SGarrett Wollman 		s_print(name, "objp->%s", dec->name);
6304e115012SGarrett Wollman 	} else {
6314e115012SGarrett Wollman 		s_print(name, "&objp->%s", dec->name);
6324e115012SGarrett Wollman 	}
633ff49530fSBill Paul 	print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
634ff49530fSBill Paul }
635ff49530fSBill Paul 
636ff49530fSBill Paul 
637e390e3afSDavid Malone char *upcase(const char *);
638ff49530fSBill Paul 
639526195adSJordan K. Hubbard static void
640e390e3afSDavid Malone emit_inline(int indent, declaration *decl, int flag)
641ff49530fSBill Paul {
642ff49530fSBill Paul 	switch (decl->rel) {
643ff49530fSBill Paul 	case  REL_ALIAS :
644ff49530fSBill Paul 		emit_single_in_line(indent, decl, flag, REL_ALIAS);
645ff49530fSBill Paul 		break;
646ff49530fSBill Paul 	case REL_VECTOR :
647ff49530fSBill Paul 		tabify(fout, indent);
648ff49530fSBill Paul 		f_print(fout, "{\n");
649ff49530fSBill Paul 		tabify(fout, indent + 1);
650896cdc31SStefan Farfeleder 		f_print(fout, "%s *genp;\n\n", decl->type);
651ff49530fSBill Paul 		tabify(fout, indent + 1);
652ff49530fSBill Paul 		f_print(fout,
653ff49530fSBill Paul 			"for (i = 0, genp = objp->%s;\n", decl->name);
654ff49530fSBill Paul 		tabify(fout, indent + 2);
655ff49530fSBill Paul 		f_print(fout, "i < %s; i++) {\n", decl->array_max);
656ff49530fSBill Paul 		emit_single_in_line(indent + 2, decl, flag, REL_VECTOR);
657ff49530fSBill Paul 		tabify(fout, indent + 1);
658ff49530fSBill Paul 		f_print(fout, "}\n");
659ff49530fSBill Paul 		tabify(fout, indent);
660ff49530fSBill Paul 		f_print(fout, "}\n");
66140ad8885SAlfred Perlstein 		break;
662526195adSJordan K. Hubbard 	default:
66340ad8885SAlfred Perlstein 		break;
664ff49530fSBill Paul 	}
665ff49530fSBill Paul }
666ff49530fSBill Paul 
667526195adSJordan K. Hubbard static void
668e390e3afSDavid Malone emit_single_in_line(int indent, declaration *decl, int flag, relation rel)
669ff49530fSBill Paul {
670ff49530fSBill Paul 	char *upp_case;
671ff49530fSBill Paul 
672ff49530fSBill Paul 	tabify(fout, indent);
673ff49530fSBill Paul 	if (flag == PUT)
674ff49530fSBill Paul 		f_print(fout, "IXDR_PUT_");
675ff49530fSBill Paul 	else
676ff49530fSBill Paul 		if (rel == REL_ALIAS)
677ff49530fSBill Paul 			f_print(fout, "objp->%s = IXDR_GET_", decl->name);
678ff49530fSBill Paul 		else
679ff49530fSBill Paul 			f_print(fout, "*genp++ = IXDR_GET_");
680ff49530fSBill Paul 
681ff49530fSBill Paul 	upp_case = upcase(decl->type);
682ff49530fSBill Paul 
683ff49530fSBill Paul 	/* hack	 - XX */
684ff49530fSBill Paul 	if (strcmp(upp_case, "INT") == 0)
685ff49530fSBill Paul 	{
686ff49530fSBill Paul 		free(upp_case);
687e390e3afSDavid Malone 		upp_case = strdup("LONG");
688ff49530fSBill Paul 	}
689ff49530fSBill Paul 
690ff49530fSBill Paul 	if (strcmp(upp_case, "U_INT") == 0)
691ff49530fSBill Paul 	{
692ff49530fSBill Paul 		free(upp_case);
693e390e3afSDavid Malone 		upp_case = strdup("U_LONG");
694ff49530fSBill Paul 	}
695ff49530fSBill Paul 	if (flag == PUT)
696ff49530fSBill Paul 		if (rel == REL_ALIAS)
697ff49530fSBill Paul 			f_print(fout,
698ff49530fSBill Paul 				"%s(buf, objp->%s);\n", upp_case, decl->name);
699ff49530fSBill Paul 		else
700ff49530fSBill Paul 			f_print(fout, "%s(buf, *genp++);\n", upp_case);
701ff49530fSBill Paul 
702ff49530fSBill Paul 	else
703ff49530fSBill Paul 		f_print(fout, "%s(buf);\n", upp_case);
704ff49530fSBill Paul 	free(upp_case);
705ff49530fSBill Paul }
706ff49530fSBill Paul 
707e390e3afSDavid Malone char *
708e390e3afSDavid Malone upcase(const char *str)
709ff49530fSBill Paul {
710ff49530fSBill Paul 	char *ptr, *hptr;
711ff49530fSBill Paul 
71275863a6dSPhilippe Charnier 	ptr = (char *)xmalloc(strlen(str)+1);
713ff49530fSBill Paul 
714ff49530fSBill Paul 	hptr = ptr;
715ff49530fSBill Paul 	while (*str != '\0')
716ff49530fSBill Paul 		*ptr++ = toupper(*str++);
717ff49530fSBill Paul 
718ff49530fSBill Paul 	*ptr = '\0';
719ff49530fSBill Paul 	return (hptr);
7204e115012SGarrett Wollman }
721