xref: /freebsd/usr.bin/rpcgen/rpc_cout.c (revision 40ad88851bea3bb7fce12e7d23e5a0c1159e9080)
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 
30ff49530fSBill Paul #ident	"@(#)rpc_cout.c	1.14	93/07/05 SMI"
31ff49530fSBill Paul 
324e115012SGarrett Wollman #ifndef lint
330e76f40dSPhilippe Charnier #if 0
34ff49530fSBill Paul static char sccsid[] = "@(#)rpc_cout.c 1.13 89/02/22 (C) 1987 SMI";
354e115012SGarrett Wollman #endif
360e76f40dSPhilippe Charnier static const char rcsid[] =
37c3aac50fSPeter Wemm   "$FreeBSD$";
380e76f40dSPhilippe Charnier #endif
394e115012SGarrett Wollman 
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 <err.h>
450e76f40dSPhilippe Charnier #include <ctype.h>
464e115012SGarrett Wollman #include <stdio.h>
47ff49530fSBill Paul #include <string.h>
484e115012SGarrett Wollman #include "rpc_parse.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
664e115012SGarrett Wollman emit(def)
674e115012SGarrett Wollman 	definition *def;
684e115012SGarrett Wollman {
69ff49530fSBill Paul 	if (def->def_kind == DEF_CONST) {
704e115012SGarrett Wollman 		return;
714e115012SGarrett Wollman 	}
72ff49530fSBill Paul 	if (def->def_kind == DEF_PROGRAM) {
73ff49530fSBill Paul 		emit_program(def);
74ff49530fSBill Paul 		return;
75ff49530fSBill Paul 	}
76ff49530fSBill Paul 	if (def->def_kind == DEF_TYPEDEF) {
77ff49530fSBill Paul 		/*
78ff49530fSBill Paul 		 * now we need to handle declarations like
79ff49530fSBill Paul 		 * struct typedef foo foo;
80ff49530fSBill Paul 		 * since we dont want this to be expanded into 2 calls to xdr_foo
81ff49530fSBill Paul 		 */
82ff49530fSBill Paul 
83ff49530fSBill Paul 		if (strcmp(def->def.ty.old_type, def->def_name) == 0)
84ff49530fSBill Paul 			return;
85ff49530fSBill Paul 	};
864e115012SGarrett Wollman 	print_header(def);
874e115012SGarrett Wollman 	switch (def->def_kind) {
884e115012SGarrett Wollman 	case DEF_UNION:
894e115012SGarrett Wollman 		emit_union(def);
904e115012SGarrett Wollman 		break;
914e115012SGarrett Wollman 	case DEF_ENUM:
924e115012SGarrett Wollman 		emit_enum(def);
934e115012SGarrett Wollman 		break;
944e115012SGarrett Wollman 	case DEF_STRUCT:
954e115012SGarrett Wollman 		emit_struct(def);
964e115012SGarrett Wollman 		break;
974e115012SGarrett Wollman 	case DEF_TYPEDEF:
984e115012SGarrett Wollman 		emit_typedef(def);
994e115012SGarrett Wollman 		break;
100526195adSJordan K. Hubbard 		/* DEF_CONST and DEF_PROGRAM have already been handled */
101526195adSJordan K. Hubbard 	default:
10240ad8885SAlfred Perlstein 		break;
1034e115012SGarrett Wollman 	}
1044e115012SGarrett Wollman 	print_trailer();
1054e115012SGarrett Wollman }
1064e115012SGarrett Wollman 
107526195adSJordan K. Hubbard static int
1084e115012SGarrett Wollman findtype(def, type)
1094e115012SGarrett Wollman 	definition *def;
1104e115012SGarrett Wollman 	char *type;
1114e115012SGarrett Wollman {
112ff49530fSBill Paul 
1134e115012SGarrett Wollman 	if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
1144e115012SGarrett Wollman 		return (0);
1154e115012SGarrett Wollman 	} else {
1164e115012SGarrett Wollman 		return (streq(def->def_name, type));
1174e115012SGarrett Wollman 	}
1184e115012SGarrett Wollman }
1194e115012SGarrett Wollman 
120526195adSJordan K. Hubbard static int
1214e115012SGarrett Wollman undefined(type)
1224e115012SGarrett Wollman 	char *type;
1234e115012SGarrett Wollman {
1244e115012SGarrett Wollman 	definition *def;
1254e115012SGarrett Wollman 
1264e115012SGarrett Wollman 	def = (definition *) FINDVAL(defined, type, findtype);
1274e115012SGarrett Wollman 	return (def == NULL);
1284e115012SGarrett Wollman }
1294e115012SGarrett Wollman 
1304e115012SGarrett Wollman 
131526195adSJordan K. Hubbard static void
132ff49530fSBill Paul print_generic_header(procname, pointerp)
133ff49530fSBill Paul     char* procname;
134ff49530fSBill Paul     int pointerp;
135ff49530fSBill Paul {
136ff49530fSBill Paul 	f_print(fout, "\n");
137ff49530fSBill Paul 	f_print(fout, "bool_t\n");
138ff49530fSBill Paul 	if (Cflag) {
139ff49530fSBill Paul 	    f_print(fout, "xdr_%s(", procname);
140ff49530fSBill Paul 	    f_print(fout, "register XDR *xdrs, ");
141ff49530fSBill Paul 	    f_print(fout, "%s ", procname);
142ff49530fSBill Paul 	    if (pointerp)
143ff49530fSBill Paul 		    f_print(fout, "*");
144ff49530fSBill Paul 	    f_print(fout, "objp)\n{\n\n");
145ff49530fSBill Paul 	} else {
146ff49530fSBill Paul 	    f_print(fout, "xdr_%s(xdrs, objp)\n", procname);
147ff49530fSBill Paul 	    f_print(fout, "\tregister XDR *xdrs;\n");
148ff49530fSBill Paul 	    f_print(fout, "\t%s ", procname);
149ff49530fSBill Paul 	    if (pointerp)
150ff49530fSBill Paul 		    f_print(fout, "*");
151ff49530fSBill Paul 	    f_print(fout, "objp;\n{\n\n");
152ff49530fSBill Paul 	}
153ff49530fSBill Paul }
154ff49530fSBill Paul 
155526195adSJordan K. Hubbard static void
1564e115012SGarrett Wollman print_header(def)
1574e115012SGarrett Wollman 	definition *def;
1584e115012SGarrett Wollman {
159ff49530fSBill Paul 	print_generic_header(def->def_name,
160ff49530fSBill Paul 			    def->def_kind != DEF_TYPEDEF ||
161ff49530fSBill Paul 			    !isvectordef(def->def.ty.old_type,
162ff49530fSBill Paul 					 def->def.ty.rel));
163ff49530fSBill Paul 	/* Now add Inline support */
164ff49530fSBill Paul 
165ff49530fSBill Paul 	if (inline == 0)
166ff49530fSBill Paul 		return;
167ff49530fSBill Paul 	/* May cause lint to complain. but  ... */
168ff49530fSBill Paul 	f_print(fout, "\tregister long *buf;\n\n");
1694e115012SGarrett Wollman }
170ff49530fSBill Paul 
171526195adSJordan K. Hubbard static void
172ff49530fSBill Paul print_prog_header(plist)
173ff49530fSBill Paul 	proc_list *plist;
174ff49530fSBill Paul {
175ff49530fSBill Paul 	print_generic_header(plist->args.argname, 1);
1764e115012SGarrett Wollman }
1774e115012SGarrett Wollman 
178526195adSJordan K. Hubbard static void
1794e115012SGarrett Wollman print_trailer()
1804e115012SGarrett Wollman {
1814e115012SGarrett Wollman 	f_print(fout, "\treturn (TRUE);\n");
1824e115012SGarrett Wollman 	f_print(fout, "}\n");
1834e115012SGarrett Wollman }
1844e115012SGarrett Wollman 
1854e115012SGarrett Wollman 
186526195adSJordan K. Hubbard static void
1874e115012SGarrett Wollman print_ifopen(indent, name)
1884e115012SGarrett Wollman 	int indent;
1894e115012SGarrett Wollman 	char *name;
1904e115012SGarrett Wollman {
1914e115012SGarrett Wollman 	tabify(fout, indent);
1924e115012SGarrett Wollman 	f_print(fout, "if (!xdr_%s(xdrs", name);
1934e115012SGarrett Wollman }
1944e115012SGarrett Wollman 
195526195adSJordan K. Hubbard static void
1964e115012SGarrett Wollman print_ifarg(arg)
1974e115012SGarrett Wollman 	char *arg;
1984e115012SGarrett Wollman {
1994e115012SGarrett Wollman 	f_print(fout, ", %s", arg);
2004e115012SGarrett Wollman }
2014e115012SGarrett Wollman 
202526195adSJordan K. Hubbard static void
203ff49530fSBill Paul print_ifsizeof(indent, prefix, type)
204ff49530fSBill Paul 	int indent;
2054e115012SGarrett Wollman 	char *prefix;
2064e115012SGarrett Wollman 	char *type;
2074e115012SGarrett Wollman {
208ff49530fSBill Paul 	if (indent) {
209ff49530fSBill Paul 		f_print(fout, ",\n");
210ff49530fSBill Paul 		tabify(fout, indent);
2114e115012SGarrett Wollman 	} else  {
212ff49530fSBill Paul 		f_print(fout, ", ");
213ff49530fSBill Paul 	}
214ff49530fSBill Paul 	if (streq(type, "bool")) {
215ff49530fSBill Paul 		f_print(fout, "sizeof (bool_t), (xdrproc_t) xdr_bool");
216ff49530fSBill Paul 	} else {
217ff49530fSBill Paul 		f_print(fout, "sizeof (");
2184e115012SGarrett Wollman 		if (undefined(type) && prefix) {
2194e115012SGarrett Wollman 			f_print(fout, "%s ", prefix);
2204e115012SGarrett Wollman 		}
221ff49530fSBill Paul 		f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type);
2224e115012SGarrett Wollman 	}
2234e115012SGarrett Wollman }
2244e115012SGarrett Wollman 
225526195adSJordan K. Hubbard static void
2264e115012SGarrett Wollman print_ifclose(indent)
2274e115012SGarrett Wollman 	int indent;
2284e115012SGarrett Wollman {
229ff49530fSBill Paul 	f_print(fout, "))\n");
2304e115012SGarrett Wollman 	tabify(fout, indent);
2314e115012SGarrett Wollman 	f_print(fout, "\treturn (FALSE);\n");
2324e115012SGarrett Wollman }
2334e115012SGarrett Wollman 
234526195adSJordan K. Hubbard static void
2354e115012SGarrett Wollman print_ifstat(indent, prefix, type, rel, amax, objname, name)
2364e115012SGarrett Wollman 	int indent;
2374e115012SGarrett Wollman 	char *prefix;
2384e115012SGarrett Wollman 	char *type;
2394e115012SGarrett Wollman 	relation rel;
2404e115012SGarrett Wollman 	char *amax;
2414e115012SGarrett Wollman 	char *objname;
2424e115012SGarrett Wollman 	char *name;
2434e115012SGarrett Wollman {
2444e115012SGarrett Wollman 	char *alt = NULL;
2454e115012SGarrett Wollman 
2464e115012SGarrett Wollman 	switch (rel) {
2474e115012SGarrett Wollman 	case REL_POINTER:
2484e115012SGarrett Wollman 		print_ifopen(indent, "pointer");
2494e115012SGarrett Wollman 		print_ifarg("(char **)");
2504e115012SGarrett Wollman 		f_print(fout, "%s", objname);
251ff49530fSBill Paul 		print_ifsizeof(0, prefix, type);
2524e115012SGarrett Wollman 		break;
2534e115012SGarrett Wollman 	case REL_VECTOR:
2544e115012SGarrett Wollman 		if (streq(type, "string")) {
2554e115012SGarrett Wollman 			alt = "string";
2564e115012SGarrett Wollman 		} else if (streq(type, "opaque")) {
2574e115012SGarrett Wollman 			alt = "opaque";
2584e115012SGarrett Wollman 		}
2594e115012SGarrett Wollman 		if (alt) {
2604e115012SGarrett Wollman 			print_ifopen(indent, alt);
2614e115012SGarrett Wollman 			print_ifarg(objname);
2624e115012SGarrett Wollman 		} else {
2634e115012SGarrett Wollman 			print_ifopen(indent, "vector");
2644e115012SGarrett Wollman 			print_ifarg("(char *)");
2654e115012SGarrett Wollman 			f_print(fout, "%s", objname);
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_ARRAY:
2734e115012SGarrett Wollman 		if (streq(type, "string")) {
2744e115012SGarrett Wollman 			alt = "string";
2754e115012SGarrett Wollman 		} else if (streq(type, "opaque")) {
2764e115012SGarrett Wollman 			alt = "bytes";
2774e115012SGarrett Wollman 		}
2784e115012SGarrett Wollman 		if (streq(type, "string")) {
2794e115012SGarrett Wollman 			print_ifopen(indent, alt);
2804e115012SGarrett Wollman 			print_ifarg(objname);
2814e115012SGarrett Wollman 		} else {
2824e115012SGarrett Wollman 			if (alt) {
2834e115012SGarrett Wollman 				print_ifopen(indent, alt);
2844e115012SGarrett Wollman 			} else {
2854e115012SGarrett Wollman 				print_ifopen(indent, "array");
2864e115012SGarrett Wollman 			}
2874e115012SGarrett Wollman 			print_ifarg("(char **)");
2884e115012SGarrett Wollman 			if (*objname == '&') {
2894e115012SGarrett Wollman 				f_print(fout, "%s.%s_val, (u_int *) %s.%s_len",
2904e115012SGarrett Wollman 					objname, name, objname, name);
2914e115012SGarrett Wollman 			} else {
292ff49530fSBill Paul 				f_print(fout,
293ff49530fSBill Paul 					"&%s->%s_val, (u_int *) &%s->%s_len",
2944e115012SGarrett Wollman 					objname, name, objname, name);
2954e115012SGarrett Wollman 			}
2964e115012SGarrett Wollman 		}
2974e115012SGarrett Wollman 		print_ifarg(amax);
2984e115012SGarrett Wollman 		if (!alt) {
299ff49530fSBill Paul 			print_ifsizeof(indent + 1, prefix, type);
3004e115012SGarrett Wollman 		}
3014e115012SGarrett Wollman 		break;
3024e115012SGarrett Wollman 	case REL_ALIAS:
3034e115012SGarrett Wollman 		print_ifopen(indent, type);
3044e115012SGarrett Wollman 		print_ifarg(objname);
3054e115012SGarrett Wollman 		break;
3064e115012SGarrett Wollman 	}
3074e115012SGarrett Wollman 	print_ifclose(indent);
3084e115012SGarrett Wollman }
3094e115012SGarrett Wollman 
3104e115012SGarrett Wollman /* ARGSUSED */
311526195adSJordan K. Hubbard static void
3124e115012SGarrett Wollman emit_enum(def)
3134e115012SGarrett Wollman 	definition *def;
3144e115012SGarrett Wollman {
3154e115012SGarrett Wollman 	print_ifopen(1, "enum");
3164e115012SGarrett Wollman 	print_ifarg("(enum_t *)objp");
3174e115012SGarrett Wollman 	print_ifclose(1);
3184e115012SGarrett Wollman }
3194e115012SGarrett Wollman 
320526195adSJordan K. Hubbard static void
321ff49530fSBill Paul emit_program(def)
322ff49530fSBill Paul 	definition *def;
323ff49530fSBill Paul {
324ff49530fSBill Paul 	decl_list *dl;
325ff49530fSBill Paul 	version_list *vlist;
326ff49530fSBill Paul 	proc_list *plist;
327ff49530fSBill Paul 
328ff49530fSBill Paul 	for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
329ff49530fSBill Paul 		for (plist = vlist->procs; plist != NULL; plist = plist->next) {
330ff49530fSBill Paul 			if (!newstyle || plist->arg_num < 2)
331ff49530fSBill Paul 				continue; /* old style, or single argument */
332ff49530fSBill Paul 			print_prog_header(plist);
333ff49530fSBill Paul 			for (dl = plist->args.decls; dl != NULL;
334ff49530fSBill Paul 			     dl = dl->next)
335ff49530fSBill Paul 				print_stat(1, &dl->decl);
336ff49530fSBill Paul 			print_trailer();
337ff49530fSBill Paul 		}
338ff49530fSBill Paul }
339ff49530fSBill Paul 
3404e115012SGarrett Wollman 
341526195adSJordan K. Hubbard static void
3424e115012SGarrett Wollman emit_union(def)
3434e115012SGarrett Wollman 	definition *def;
3444e115012SGarrett Wollman {
3454e115012SGarrett Wollman 	declaration *dflt;
3464e115012SGarrett Wollman 	case_list *cl;
3474e115012SGarrett Wollman 	declaration *cs;
3484e115012SGarrett Wollman 	char *object;
349ff49530fSBill Paul 	char *vecformat = "objp->%s_u.%s";
3504e115012SGarrett Wollman 	char *format = "&objp->%s_u.%s";
3514e115012SGarrett Wollman 
352ff49530fSBill Paul 	print_stat(1, &def->def.un.enum_decl);
3534e115012SGarrett Wollman 	f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
3544e115012SGarrett Wollman 	for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
355ff49530fSBill Paul 
3564e115012SGarrett Wollman 		f_print(fout, "\tcase %s:\n", cl->case_name);
357ff49530fSBill Paul 		if (cl->contflag == 1) /* a continued case statement */
358ff49530fSBill Paul 			continue;
359ff49530fSBill Paul 		cs = &cl->case_decl;
3604e115012SGarrett Wollman 		if (!streq(cs->type, "void")) {
3614e115012SGarrett Wollman 			object = alloc(strlen(def->def_name) + strlen(format) +
3624e115012SGarrett Wollman 				       strlen(cs->name) + 1);
363ff49530fSBill Paul 			if (isvectordef (cs->type, cs->rel)) {
364ff49530fSBill Paul 				s_print(object, vecformat, def->def_name,
365ff49530fSBill Paul 					cs->name);
366ff49530fSBill Paul 			} else {
367ff49530fSBill Paul 				s_print(object, format, def->def_name,
368ff49530fSBill Paul 					cs->name);
369ff49530fSBill Paul 			}
370ff49530fSBill Paul 			print_ifstat(2, cs->prefix, cs->type, cs->rel,
371ff49530fSBill Paul 				     cs->array_max, object, cs->name);
3724e115012SGarrett Wollman 			free(object);
3734e115012SGarrett Wollman 		}
3744e115012SGarrett Wollman 		f_print(fout, "\t\tbreak;\n");
3754e115012SGarrett Wollman 	}
3764e115012SGarrett Wollman 	dflt = def->def.un.default_decl;
3774e115012SGarrett Wollman 	if (dflt != NULL) {
3784e115012SGarrett Wollman 		if (!streq(dflt->type, "void")) {
3794e115012SGarrett Wollman 			f_print(fout, "\tdefault:\n");
3804e115012SGarrett Wollman 			object = alloc(strlen(def->def_name) + strlen(format) +
3814e115012SGarrett Wollman strlen(dflt->name) + 1);
382ff49530fSBill Paul 			if (isvectordef (dflt->type, dflt->rel)) {
383ff49530fSBill Paul 				s_print(object, vecformat, def->def_name,
384ff49530fSBill Paul 					dflt->name);
385ff49530fSBill Paul 			} else {
386ff49530fSBill Paul 				s_print(object, format, def->def_name,
387ff49530fSBill Paul 					dflt->name);
388ff49530fSBill Paul 			}
389ff49530fSBill Paul 
3904e115012SGarrett Wollman 			print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
3914e115012SGarrett Wollman 				    dflt->array_max, object, dflt->name);
3924e115012SGarrett Wollman 			free(object);
3934e115012SGarrett Wollman 			f_print(fout, "\t\tbreak;\n");
394526195adSJordan K. Hubbard 		} else {
395526195adSJordan K. Hubbard 			f_print(fout, "\tdefault:\n");
396526195adSJordan K. Hubbard 			f_print(fout, "\t\tbreak;\n");
3974e115012SGarrett Wollman 		}
3984e115012SGarrett Wollman 	} else {
3994e115012SGarrett Wollman 		f_print(fout, "\tdefault:\n");
4004e115012SGarrett Wollman 		f_print(fout, "\t\treturn (FALSE);\n");
4014e115012SGarrett Wollman 	}
402ff49530fSBill Paul 
4034e115012SGarrett Wollman 	f_print(fout, "\t}\n");
4044e115012SGarrett Wollman }
4054e115012SGarrett Wollman 
406ff49530fSBill Paul static void
407ff49530fSBill Paul inline_struct(def, flag)
408ff49530fSBill Paul definition *def;
409ff49530fSBill Paul int flag;
410ff49530fSBill Paul {
411ff49530fSBill Paul 	decl_list *dl;
412ff49530fSBill Paul 	int i, size;
413ff49530fSBill Paul 	decl_list *cur, *psav;
414ff49530fSBill Paul 	bas_type *ptr;
415ff49530fSBill Paul 	char *sizestr, *plus;
416ff49530fSBill Paul 	char ptemp[256];
417ff49530fSBill Paul 	int indent = 1;
4184e115012SGarrett Wollman 
41940ad8885SAlfred Perlstein 	cur = NULL;
420ff49530fSBill Paul 	if (flag == PUT)
421ff49530fSBill Paul 		f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
422ff49530fSBill Paul 	else
423ff49530fSBill Paul 		f_print(fout, "\t\treturn (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
424ff49530fSBill Paul 
425ff49530fSBill Paul 	i = 0;
426ff49530fSBill Paul 	size = 0;
427ff49530fSBill Paul 	sizestr = NULL;
428ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
429ff49530fSBill Paul 		/* now walk down the list and check for basic types */
430ff49530fSBill Paul 		if ((dl->decl.prefix == NULL) &&
431ff49530fSBill Paul 		    ((ptr = find_type(dl->decl.type)) != NULL) &&
432ff49530fSBill Paul 		    ((dl->decl.rel == REL_ALIAS) ||
433ff49530fSBill Paul 		     (dl->decl.rel == REL_VECTOR))){
434ff49530fSBill Paul 			if (i == 0)
435ff49530fSBill Paul 				cur = dl;
436ff49530fSBill Paul 			i++;
437ff49530fSBill Paul 
438ff49530fSBill Paul 			if (dl->decl.rel == REL_ALIAS)
439ff49530fSBill Paul 				size += ptr->length;
440ff49530fSBill Paul 			else {
441ff49530fSBill Paul 				/* this code is required to handle arrays */
442ff49530fSBill Paul 				if (sizestr == NULL)
443ff49530fSBill Paul 					plus = "";
444ff49530fSBill Paul 				else
445ff49530fSBill Paul 					plus = " + ";
446ff49530fSBill Paul 
447ff49530fSBill Paul 				if (ptr->length != 1)
448ff49530fSBill Paul 					s_print(ptemp, "%s%s * %d",
449ff49530fSBill Paul 						plus, dl->decl.array_max,
450ff49530fSBill Paul 						ptr->length);
451ff49530fSBill Paul 				else
452ff49530fSBill Paul 					s_print(ptemp, "%s%s", plus,
453ff49530fSBill Paul 						dl->decl.array_max);
454ff49530fSBill Paul 
455ff49530fSBill Paul 				/* now concatenate to sizestr !!!! */
456ff49530fSBill Paul 				if (sizestr == NULL)
457ff49530fSBill Paul 					sizestr = strdup(ptemp);
458ff49530fSBill Paul 				else{
459ff49530fSBill Paul 					sizestr = realloc(sizestr,
460ff49530fSBill Paul 							  strlen(sizestr)
461ff49530fSBill Paul 							  +strlen(ptemp)+1);
462ff49530fSBill Paul 					if (sizestr == NULL){
4630e76f40dSPhilippe Charnier 						warnx("fatal error: no memory");
464ff49530fSBill Paul 						crash();
465ff49530fSBill Paul 					};
466ff49530fSBill Paul 					sizestr = strcat(sizestr, ptemp);
467ff49530fSBill Paul 					/* build up length of array */
468ff49530fSBill Paul 				}
469ff49530fSBill Paul 			}
470ff49530fSBill Paul 		} else {
4719ef5c48bSBill Fumerola 			if (i > 0) {
472ff49530fSBill Paul 				if (sizestr == NULL && size < inline){
473ff49530fSBill Paul 					/*
474ff49530fSBill Paul 					 * don't expand into inline code
475ff49530fSBill Paul 					 * if size < inline
476ff49530fSBill Paul 					 */
477ff49530fSBill Paul 					while (cur != dl){
478ff49530fSBill Paul 						print_stat(indent + 1, &cur->decl);
479ff49530fSBill Paul 						cur = cur->next;
480ff49530fSBill Paul 					}
481ff49530fSBill Paul 				} else {
482ff49530fSBill Paul 					/* were already looking at a xdr_inlineable structure */
483ff49530fSBill Paul 					tabify(fout, indent + 1);
484ff49530fSBill Paul 					if (sizestr == NULL)
485ff49530fSBill Paul 						f_print(fout, "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
486ff49530fSBill Paul 							size);
4879ef5c48bSBill Fumerola 					else {
488ff49530fSBill Paul 						if (size == 0)
489ff49530fSBill Paul 							f_print(fout,
490ff49530fSBill Paul 								"buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
491ff49530fSBill Paul 								sizestr);
492ff49530fSBill Paul 						else
493ff49530fSBill Paul 							f_print(fout,
494ff49530fSBill Paul 								"buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
495ff49530fSBill Paul 								size, sizestr);
496ff49530fSBill Paul 
4979ef5c48bSBill Fumerola 					}
498ff49530fSBill Paul 					f_print(fout, "\n");
499ff49530fSBill Paul 					tabify(fout, indent + 1);
500ff49530fSBill Paul 					f_print(fout,
501ff49530fSBill Paul 						"if (buf == NULL) {\n");
502ff49530fSBill Paul 
503ff49530fSBill Paul 					psav = cur;
504ff49530fSBill Paul 					while (cur != dl){
505ff49530fSBill Paul 						print_stat(indent + 2, &cur->decl);
506ff49530fSBill Paul 						cur = cur->next;
507ff49530fSBill Paul 					}
508ff49530fSBill Paul 
509ff49530fSBill Paul 					f_print(fout, "\n\t\t} else {\n");
510ff49530fSBill Paul 
511ff49530fSBill Paul 					cur = psav;
512ff49530fSBill Paul 					while (cur != dl){
513ff49530fSBill Paul 						emit_inline(indent + 2, &cur->decl, flag);
514ff49530fSBill Paul 						cur = cur->next;
515ff49530fSBill Paul 					}
516ff49530fSBill Paul 
517ff49530fSBill Paul 					tabify(fout, indent + 1);
518ff49530fSBill Paul 					f_print(fout, "}\n");
519ff49530fSBill Paul 				}
5209ef5c48bSBill Fumerola 			}
521ff49530fSBill Paul 			size = 0;
522ff49530fSBill Paul 			i = 0;
523ff49530fSBill Paul 			sizestr = NULL;
524ff49530fSBill Paul 			print_stat(indent + 1, &dl->decl);
525ff49530fSBill Paul 		}
526ff49530fSBill Paul 	}
527ff49530fSBill Paul 
52840ad8885SAlfred Perlstein 	if (i > 0) {
529ff49530fSBill Paul 		if (sizestr == NULL && size < inline){
530ff49530fSBill Paul 			/* don't expand into inline code if size < inline */
531ff49530fSBill Paul 			while (cur != dl){
532ff49530fSBill Paul 				print_stat(indent + 1, &cur->decl);
533ff49530fSBill Paul 				cur = cur->next;
534ff49530fSBill Paul 			}
535ff49530fSBill Paul 		} else {
536ff49530fSBill Paul 			/* were already looking at a xdr_inlineable structure */
537ff49530fSBill Paul 			if (sizestr == NULL)
538ff49530fSBill Paul 				f_print(fout, "\t\tbuf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
539ff49530fSBill Paul 					size);
540ff49530fSBill Paul 			else
541ff49530fSBill Paul 				if (size == 0)
542ff49530fSBill Paul 					f_print(fout,
543ff49530fSBill Paul 						"\t\tbuf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
544ff49530fSBill Paul 						sizestr);
545ff49530fSBill Paul 				else
546ff49530fSBill Paul 					f_print(fout,
547ff49530fSBill Paul 						"\t\tbuf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
548ff49530fSBill Paul 						size, sizestr);
549ff49530fSBill Paul 
550ff49530fSBill Paul 			f_print(fout, "\n\t\tif (buf == NULL) {\n");
551ff49530fSBill Paul 			psav = cur;
552ff49530fSBill Paul 			while (cur != NULL){
553ff49530fSBill Paul 				print_stat(indent + 2, &cur->decl);
554ff49530fSBill Paul 				cur = cur->next;
555ff49530fSBill Paul 			}
556ff49530fSBill Paul 			f_print(fout, "\t\t} else {\n");
557ff49530fSBill Paul 
558ff49530fSBill Paul 			cur = psav;
559ff49530fSBill Paul 			while (cur != dl){
560ff49530fSBill Paul 				emit_inline(indent + 2, &cur->decl, flag);
561ff49530fSBill Paul 				cur = cur->next;
562ff49530fSBill Paul 			}
563ff49530fSBill Paul 			f_print(fout, "\t\t}\n");
564ff49530fSBill Paul 		}
565ff49530fSBill Paul 	}
56640ad8885SAlfred Perlstein }
5674e115012SGarrett Wollman 
568526195adSJordan K. Hubbard static void
5694e115012SGarrett Wollman emit_struct(def)
5704e115012SGarrett Wollman 	definition *def;
5714e115012SGarrett Wollman {
5724e115012SGarrett Wollman 	decl_list *dl;
573526195adSJordan K. Hubbard 	int j, size, flag;
574ff49530fSBill Paul 	bas_type *ptr;
575ff49530fSBill Paul 	int can_inline;
5764e115012SGarrett Wollman 
577ff49530fSBill Paul 	if (inline == 0) {
578ff49530fSBill Paul 		/* No xdr_inlining at all */
579ff49530fSBill Paul 		for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
580ff49530fSBill Paul 			print_stat(1, &dl->decl);
581ff49530fSBill Paul 		return;
5824e115012SGarrett Wollman 	}
5834e115012SGarrett Wollman 
584ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
585ff49530fSBill Paul 		if (dl->decl.rel == REL_VECTOR){
586ff49530fSBill Paul 			f_print(fout, "\tint i;\n");
587ff49530fSBill Paul 			break;
588ff49530fSBill Paul 		}
5894e115012SGarrett Wollman 
590ff49530fSBill Paul 	size = 0;
591ff49530fSBill Paul 	can_inline = 0;
592ff49530fSBill Paul 	/*
593ff49530fSBill Paul 	 * Make a first pass and see if inling is possible.
594ff49530fSBill Paul 	 */
595ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
596ff49530fSBill Paul 		if ((dl->decl.prefix == NULL) &&
597ff49530fSBill Paul 		    ((ptr = find_type(dl->decl.type)) != NULL) &&
598ff49530fSBill Paul 		    ((dl->decl.rel == REL_ALIAS)||
599ff49530fSBill Paul 		     (dl->decl.rel == REL_VECTOR))){
600ff49530fSBill Paul 			if (dl->decl.rel == REL_ALIAS)
601ff49530fSBill Paul 				size += ptr->length;
602ff49530fSBill Paul 			else {
603ff49530fSBill Paul 				can_inline = 1;
604ff49530fSBill Paul 				break; /* can be inlined */
605ff49530fSBill Paul 			}
606ff49530fSBill Paul 		} else {
607ff49530fSBill Paul 			if (size >= inline){
608ff49530fSBill Paul 				can_inline = 1;
609ff49530fSBill Paul 				break; /* can be inlined */
610ff49530fSBill Paul 			}
611ff49530fSBill Paul 			size = 0;
612ff49530fSBill Paul 		}
613ff49530fSBill Paul 	if (size >= inline)
614ff49530fSBill Paul 		can_inline = 1;
6154e115012SGarrett Wollman 
616ff49530fSBill Paul 	if (can_inline == 0){	/* can not inline, drop back to old mode */
617ff49530fSBill Paul 		for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
618ff49530fSBill Paul 			print_stat(1, &dl->decl);
619ff49530fSBill Paul 		return;
620ff49530fSBill Paul 	}
621ff49530fSBill Paul 
622ff49530fSBill Paul 	flag = PUT;
623ff49530fSBill Paul 	for (j = 0; j < 2; j++){
624ff49530fSBill Paul 		inline_struct(def, flag);
625ff49530fSBill Paul 		if (flag == PUT)
626ff49530fSBill Paul 			flag = GET;
627ff49530fSBill Paul 	}
628ff49530fSBill Paul 
629ff49530fSBill Paul 	f_print(fout, "\t\treturn (TRUE);\n\t}\n\n");
630ff49530fSBill Paul 
631ff49530fSBill Paul 	/* now take care of XDR_FREE case */
632ff49530fSBill Paul 
633ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
634ff49530fSBill Paul 		print_stat(1, &dl->decl);
635ff49530fSBill Paul 
636ff49530fSBill Paul }
6374e115012SGarrett Wollman 
638526195adSJordan K. Hubbard static void
6394e115012SGarrett Wollman emit_typedef(def)
6404e115012SGarrett Wollman 	definition *def;
6414e115012SGarrett Wollman {
6424e115012SGarrett Wollman 	char *prefix = def->def.ty.old_prefix;
6434e115012SGarrett Wollman 	char *type = def->def.ty.old_type;
6444e115012SGarrett Wollman 	char *amax = def->def.ty.array_max;
6454e115012SGarrett Wollman 	relation rel = def->def.ty.rel;
6464e115012SGarrett Wollman 
6474e115012SGarrett Wollman 	print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
6484e115012SGarrett Wollman }
6494e115012SGarrett Wollman 
650526195adSJordan K. Hubbard static void
651ff49530fSBill Paul print_stat(indent, dec)
652ff49530fSBill Paul 	int indent;
6534e115012SGarrett Wollman 	declaration *dec;
6544e115012SGarrett Wollman {
6554e115012SGarrett Wollman 	char *prefix = dec->prefix;
6564e115012SGarrett Wollman 	char *type = dec->type;
6574e115012SGarrett Wollman 	char *amax = dec->array_max;
6584e115012SGarrett Wollman 	relation rel = dec->rel;
6594e115012SGarrett Wollman 	char name[256];
6604e115012SGarrett Wollman 
6614e115012SGarrett Wollman 	if (isvectordef(type, rel)) {
6624e115012SGarrett Wollman 		s_print(name, "objp->%s", dec->name);
6634e115012SGarrett Wollman 	} else {
6644e115012SGarrett Wollman 		s_print(name, "&objp->%s", dec->name);
6654e115012SGarrett Wollman 	}
666ff49530fSBill Paul 	print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
667ff49530fSBill Paul }
668ff49530fSBill Paul 
669ff49530fSBill Paul 
670ff49530fSBill Paul char *upcase ();
671ff49530fSBill Paul 
672526195adSJordan K. Hubbard static void
673ff49530fSBill Paul emit_inline(indent, decl, flag)
674ff49530fSBill Paul int indent;
675ff49530fSBill Paul declaration *decl;
676ff49530fSBill Paul int flag;
677ff49530fSBill Paul {
678ff49530fSBill Paul 	switch (decl->rel) {
679ff49530fSBill Paul 	case  REL_ALIAS :
680ff49530fSBill Paul 		emit_single_in_line(indent, decl, flag, REL_ALIAS);
681ff49530fSBill Paul 		break;
682ff49530fSBill Paul 	case REL_VECTOR :
683ff49530fSBill Paul 		tabify(fout, indent);
684ff49530fSBill Paul 		f_print(fout, "{\n");
685ff49530fSBill Paul 		tabify(fout, indent + 1);
686ff49530fSBill Paul 		f_print(fout, "register %s *genp;\n\n", decl->type);
687ff49530fSBill Paul 		tabify(fout, indent + 1);
688ff49530fSBill Paul 		f_print(fout,
689ff49530fSBill Paul 			"for (i = 0, genp = objp->%s;\n", decl->name);
690ff49530fSBill Paul 		tabify(fout, indent + 2);
691ff49530fSBill Paul 		f_print(fout, "i < %s; i++) {\n", decl->array_max);
692ff49530fSBill Paul 		emit_single_in_line(indent + 2, decl, flag, REL_VECTOR);
693ff49530fSBill Paul 		tabify(fout, indent + 1);
694ff49530fSBill Paul 		f_print(fout, "}\n");
695ff49530fSBill Paul 		tabify(fout, indent);
696ff49530fSBill Paul 		f_print(fout, "}\n");
69740ad8885SAlfred Perlstein 		break;
698526195adSJordan K. Hubbard 	default:
69940ad8885SAlfred Perlstein 		break;
700ff49530fSBill Paul 	}
701ff49530fSBill Paul }
702ff49530fSBill Paul 
703526195adSJordan K. Hubbard static void
704ff49530fSBill Paul emit_single_in_line(indent, decl, flag, rel)
705ff49530fSBill Paul int indent;
706ff49530fSBill Paul declaration *decl;
707ff49530fSBill Paul int flag;
708ff49530fSBill Paul relation rel;
709ff49530fSBill Paul {
710ff49530fSBill Paul 	char *upp_case;
711ff49530fSBill Paul 	int freed = 0;
712ff49530fSBill Paul 
713ff49530fSBill Paul 	tabify(fout, indent);
714ff49530fSBill Paul 	if (flag == PUT)
715ff49530fSBill Paul 		f_print(fout, "IXDR_PUT_");
716ff49530fSBill Paul 	else
717ff49530fSBill Paul 		if (rel == REL_ALIAS)
718ff49530fSBill Paul 			f_print(fout, "objp->%s = IXDR_GET_", decl->name);
719ff49530fSBill Paul 		else
720ff49530fSBill Paul 			f_print(fout, "*genp++ = IXDR_GET_");
721ff49530fSBill Paul 
722ff49530fSBill Paul 	upp_case = upcase(decl->type);
723ff49530fSBill Paul 
724ff49530fSBill Paul 	/* hack	 - XX */
725ff49530fSBill Paul 	if (strcmp(upp_case, "INT") == 0)
726ff49530fSBill Paul 	{
727ff49530fSBill Paul 		free(upp_case);
728ff49530fSBill Paul 		freed = 1;
729ff49530fSBill Paul 		upp_case = "LONG";
730ff49530fSBill Paul 	}
731ff49530fSBill Paul 
732ff49530fSBill Paul 	if (strcmp(upp_case, "U_INT") == 0)
733ff49530fSBill Paul 	{
734ff49530fSBill Paul 		free(upp_case);
735ff49530fSBill Paul 		freed = 1;
736ff49530fSBill Paul 		upp_case = "U_LONG";
737ff49530fSBill Paul 	}
738ff49530fSBill Paul 	if (flag == PUT)
739ff49530fSBill Paul 		if (rel == REL_ALIAS)
740ff49530fSBill Paul 			f_print(fout,
741ff49530fSBill Paul 				"%s(buf, objp->%s);\n", upp_case, decl->name);
742ff49530fSBill Paul 		else
743ff49530fSBill Paul 			f_print(fout, "%s(buf, *genp++);\n", upp_case);
744ff49530fSBill Paul 
745ff49530fSBill Paul 	else
746ff49530fSBill Paul 		f_print(fout, "%s(buf);\n", upp_case);
747ff49530fSBill Paul 	if (!freed)
748ff49530fSBill Paul 		free(upp_case);
749ff49530fSBill Paul }
750ff49530fSBill Paul 
751ff49530fSBill Paul char *upcase(str)
752ff49530fSBill Paul char *str;
753ff49530fSBill Paul {
754ff49530fSBill Paul 	char *ptr, *hptr;
755ff49530fSBill Paul 
756ff49530fSBill Paul 	ptr =  (char *)malloc(strlen(str)+1);
757ff49530fSBill Paul 	if (ptr == (char *) NULL)
7580e76f40dSPhilippe Charnier 		errx(1, "malloc failed");
759ff49530fSBill Paul 
760ff49530fSBill Paul 	hptr = ptr;
761ff49530fSBill Paul 	while (*str != '\0')
762ff49530fSBill Paul 		*ptr++ = toupper(*str++);
763ff49530fSBill Paul 
764ff49530fSBill Paul 	*ptr = '\0';
765ff49530fSBill Paul 	return (hptr);
7664e115012SGarrett Wollman }
767