xref: /freebsd/usr.bin/rpcgen/rpc_cout.c (revision ff49530f45dc674ce45ec75d7d33218f97c79b42)
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
33ff49530fSBill Paul static char sccsid[] = "@(#)rpc_cout.c 1.13 89/02/22 (C) 1987 SMI";
344e115012SGarrett Wollman #endif
354e115012SGarrett Wollman 
364e115012SGarrett Wollman /*
374e115012SGarrett Wollman  * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
384e115012SGarrett Wollman  * Copyright (C) 1987, Sun Microsystems, Inc.
394e115012SGarrett Wollman  */
404e115012SGarrett Wollman #include <stdio.h>
41ff49530fSBill Paul #include <string.h>
424e115012SGarrett Wollman #include "rpc_parse.h"
43ff49530fSBill Paul #include "rpc_util.h"
444e115012SGarrett Wollman 
45ff49530fSBill Paul static int print_header __P(( definition * ));
46ff49530fSBill Paul static int print_trailer __P(( void ));
47ff49530fSBill Paul static int print_stat __P(( int , declaration * ));
48ff49530fSBill Paul static int emit_enum __P(( definition * ));
49ff49530fSBill Paul static int emit_program __P(( definition * ));
50ff49530fSBill Paul static int emit_union __P(( definition * ));
51ff49530fSBill Paul static int emit_struct __P(( definition * ));
52ff49530fSBill Paul static int emit_typedef __P(( definition * ));
534e115012SGarrett Wollman 
544e115012SGarrett Wollman /*
554e115012SGarrett Wollman  * Emit the C-routine for the given definition
564e115012SGarrett Wollman  */
574e115012SGarrett Wollman void
584e115012SGarrett Wollman emit(def)
594e115012SGarrett Wollman 	definition *def;
604e115012SGarrett Wollman {
61ff49530fSBill Paul 	if (def->def_kind == DEF_CONST) {
624e115012SGarrett Wollman 		return;
634e115012SGarrett Wollman 	}
64ff49530fSBill Paul 	if (def->def_kind == DEF_PROGRAM) {
65ff49530fSBill Paul 		emit_program(def);
66ff49530fSBill Paul 		return;
67ff49530fSBill Paul 	}
68ff49530fSBill Paul 	if (def->def_kind == DEF_TYPEDEF) {
69ff49530fSBill Paul 		/*
70ff49530fSBill Paul 		 * now we need to handle declarations like
71ff49530fSBill Paul 		 * struct typedef foo foo;
72ff49530fSBill Paul 		 * since we dont want this to be expanded into 2 calls to xdr_foo
73ff49530fSBill Paul 		 */
74ff49530fSBill Paul 
75ff49530fSBill Paul 		if (strcmp(def->def.ty.old_type, def->def_name) == 0)
76ff49530fSBill Paul 			return;
77ff49530fSBill Paul 	};
784e115012SGarrett Wollman 	print_header(def);
794e115012SGarrett Wollman 	switch (def->def_kind) {
804e115012SGarrett Wollman 	case DEF_UNION:
814e115012SGarrett Wollman 		emit_union(def);
824e115012SGarrett Wollman 		break;
834e115012SGarrett Wollman 	case DEF_ENUM:
844e115012SGarrett Wollman 		emit_enum(def);
854e115012SGarrett Wollman 		break;
864e115012SGarrett Wollman 	case DEF_STRUCT:
874e115012SGarrett Wollman 		emit_struct(def);
884e115012SGarrett Wollman 		break;
894e115012SGarrett Wollman 	case DEF_TYPEDEF:
904e115012SGarrett Wollman 		emit_typedef(def);
914e115012SGarrett Wollman 		break;
924e115012SGarrett Wollman 	}
934e115012SGarrett Wollman 	print_trailer();
944e115012SGarrett Wollman }
954e115012SGarrett Wollman 
964e115012SGarrett Wollman static
974e115012SGarrett Wollman findtype(def, type)
984e115012SGarrett Wollman 	definition *def;
994e115012SGarrett Wollman 	char *type;
1004e115012SGarrett Wollman {
101ff49530fSBill Paul 
1024e115012SGarrett Wollman 	if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
1034e115012SGarrett Wollman 		return (0);
1044e115012SGarrett Wollman 	} else {
1054e115012SGarrett Wollman 		return (streq(def->def_name, type));
1064e115012SGarrett Wollman 	}
1074e115012SGarrett Wollman }
1084e115012SGarrett Wollman 
1094e115012SGarrett Wollman static
1104e115012SGarrett Wollman undefined(type)
1114e115012SGarrett Wollman 	char *type;
1124e115012SGarrett Wollman {
1134e115012SGarrett Wollman 	definition *def;
1144e115012SGarrett Wollman 
1154e115012SGarrett Wollman 	def = (definition *) FINDVAL(defined, type, findtype);
1164e115012SGarrett Wollman 	return (def == NULL);
1174e115012SGarrett Wollman }
1184e115012SGarrett Wollman 
1194e115012SGarrett Wollman 
1204e115012SGarrett Wollman static
121ff49530fSBill Paul print_generic_header(procname, pointerp)
122ff49530fSBill Paul     char* procname;
123ff49530fSBill Paul     int pointerp;
124ff49530fSBill Paul {
125ff49530fSBill Paul 	f_print(fout, "\n");
126ff49530fSBill Paul 	f_print(fout, "bool_t\n");
127ff49530fSBill Paul 	if (Cflag) {
128ff49530fSBill Paul 	    f_print(fout, "xdr_%s(", procname);
129ff49530fSBill Paul 	    f_print(fout, "register XDR *xdrs, ");
130ff49530fSBill Paul 	    f_print(fout, "%s ", procname);
131ff49530fSBill Paul 	    if (pointerp)
132ff49530fSBill Paul 		    f_print(fout, "*");
133ff49530fSBill Paul 	    f_print(fout, "objp)\n{\n\n");
134ff49530fSBill Paul 	} else {
135ff49530fSBill Paul 	    f_print(fout, "xdr_%s(xdrs, objp)\n", procname);
136ff49530fSBill Paul 	    f_print(fout, "\tregister XDR *xdrs;\n");
137ff49530fSBill Paul 	    f_print(fout, "\t%s ", procname);
138ff49530fSBill Paul 	    if (pointerp)
139ff49530fSBill Paul 		    f_print(fout, "*");
140ff49530fSBill Paul 	    f_print(fout, "objp;\n{\n\n");
141ff49530fSBill Paul 	}
142ff49530fSBill Paul }
143ff49530fSBill Paul 
144ff49530fSBill Paul static
1454e115012SGarrett Wollman print_header(def)
1464e115012SGarrett Wollman 	definition *def;
1474e115012SGarrett Wollman {
148ff49530fSBill Paul 
149ff49530fSBill Paul 	decl_list *dl;
150ff49530fSBill Paul 	bas_type *ptr;
151ff49530fSBill Paul 	int i;
152ff49530fSBill Paul 
153ff49530fSBill Paul 	print_generic_header(def->def_name,
154ff49530fSBill Paul 			    def->def_kind != DEF_TYPEDEF ||
155ff49530fSBill Paul 			    !isvectordef(def->def.ty.old_type,
156ff49530fSBill Paul 					 def->def.ty.rel));
157ff49530fSBill Paul 	/* Now add Inline support */
158ff49530fSBill Paul 
159ff49530fSBill Paul 	if (inline == 0)
160ff49530fSBill Paul 		return;
161ff49530fSBill Paul 	/* May cause lint to complain. but  ... */
162ff49530fSBill Paul 	f_print(fout, "\tregister long *buf;\n\n");
1634e115012SGarrett Wollman }
164ff49530fSBill Paul 
165ff49530fSBill Paul static
166ff49530fSBill Paul print_prog_header(plist)
167ff49530fSBill Paul 	proc_list *plist;
168ff49530fSBill Paul {
169ff49530fSBill Paul 	print_generic_header(plist->args.argname, 1);
1704e115012SGarrett Wollman }
1714e115012SGarrett Wollman 
1724e115012SGarrett Wollman static
1734e115012SGarrett Wollman print_trailer()
1744e115012SGarrett Wollman {
1754e115012SGarrett Wollman 	f_print(fout, "\treturn (TRUE);\n");
1764e115012SGarrett Wollman 	f_print(fout, "}\n");
1774e115012SGarrett Wollman }
1784e115012SGarrett Wollman 
1794e115012SGarrett Wollman 
1804e115012SGarrett Wollman static
1814e115012SGarrett Wollman print_ifopen(indent, name)
1824e115012SGarrett Wollman 	int indent;
1834e115012SGarrett Wollman 	char *name;
1844e115012SGarrett Wollman {
1854e115012SGarrett Wollman 	tabify(fout, indent);
1864e115012SGarrett Wollman 	f_print(fout, "if (!xdr_%s(xdrs", name);
1874e115012SGarrett Wollman }
1884e115012SGarrett Wollman 
1894e115012SGarrett Wollman static
1904e115012SGarrett Wollman print_ifarg(arg)
1914e115012SGarrett Wollman 	char *arg;
1924e115012SGarrett Wollman {
1934e115012SGarrett Wollman 	f_print(fout, ", %s", arg);
1944e115012SGarrett Wollman }
1954e115012SGarrett Wollman 
1964e115012SGarrett Wollman static
197ff49530fSBill Paul print_ifsizeof(indent, prefix, type)
198ff49530fSBill Paul 	int indent;
1994e115012SGarrett Wollman 	char *prefix;
2004e115012SGarrett Wollman 	char *type;
2014e115012SGarrett Wollman {
202ff49530fSBill Paul 	if (indent) {
203ff49530fSBill Paul 		f_print(fout, ",\n");
204ff49530fSBill Paul 		tabify(fout, indent);
2054e115012SGarrett Wollman 	} else  {
206ff49530fSBill Paul 		f_print(fout, ", ");
207ff49530fSBill Paul 	}
208ff49530fSBill Paul 	if (streq(type, "bool")) {
209ff49530fSBill Paul 		f_print(fout, "sizeof (bool_t), (xdrproc_t) xdr_bool");
210ff49530fSBill Paul 	} else {
211ff49530fSBill Paul 		f_print(fout, "sizeof (");
2124e115012SGarrett Wollman 		if (undefined(type) && prefix) {
2134e115012SGarrett Wollman 			f_print(fout, "%s ", prefix);
2144e115012SGarrett Wollman 		}
215ff49530fSBill Paul 		f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type);
2164e115012SGarrett Wollman 	}
2174e115012SGarrett Wollman }
2184e115012SGarrett Wollman 
2194e115012SGarrett Wollman static
2204e115012SGarrett Wollman print_ifclose(indent)
2214e115012SGarrett Wollman 	int indent;
2224e115012SGarrett Wollman {
223ff49530fSBill Paul 	f_print(fout, "))\n");
2244e115012SGarrett Wollman 	tabify(fout, indent);
2254e115012SGarrett Wollman 	f_print(fout, "\treturn (FALSE);\n");
2264e115012SGarrett Wollman }
2274e115012SGarrett Wollman 
2284e115012SGarrett Wollman static
2294e115012SGarrett Wollman print_ifstat(indent, prefix, type, rel, amax, objname, name)
2304e115012SGarrett Wollman 	int indent;
2314e115012SGarrett Wollman 	char *prefix;
2324e115012SGarrett Wollman 	char *type;
2334e115012SGarrett Wollman 	relation rel;
2344e115012SGarrett Wollman 	char *amax;
2354e115012SGarrett Wollman 	char *objname;
2364e115012SGarrett Wollman 	char *name;
2374e115012SGarrett Wollman {
2384e115012SGarrett Wollman 	char *alt = NULL;
2394e115012SGarrett Wollman 
2404e115012SGarrett Wollman 	switch (rel) {
2414e115012SGarrett Wollman 	case REL_POINTER:
2424e115012SGarrett Wollman 		print_ifopen(indent, "pointer");
2434e115012SGarrett Wollman 		print_ifarg("(char **)");
2444e115012SGarrett Wollman 		f_print(fout, "%s", objname);
245ff49530fSBill Paul 		print_ifsizeof(0, prefix, type);
2464e115012SGarrett Wollman 		break;
2474e115012SGarrett Wollman 	case REL_VECTOR:
2484e115012SGarrett Wollman 		if (streq(type, "string")) {
2494e115012SGarrett Wollman 			alt = "string";
2504e115012SGarrett Wollman 		} else if (streq(type, "opaque")) {
2514e115012SGarrett Wollman 			alt = "opaque";
2524e115012SGarrett Wollman 		}
2534e115012SGarrett Wollman 		if (alt) {
2544e115012SGarrett Wollman 			print_ifopen(indent, alt);
2554e115012SGarrett Wollman 			print_ifarg(objname);
2564e115012SGarrett Wollman 		} else {
2574e115012SGarrett Wollman 			print_ifopen(indent, "vector");
2584e115012SGarrett Wollman 			print_ifarg("(char *)");
2594e115012SGarrett Wollman 			f_print(fout, "%s", objname);
2604e115012SGarrett Wollman 		}
2614e115012SGarrett Wollman 		print_ifarg(amax);
2624e115012SGarrett Wollman 		if (!alt) {
263ff49530fSBill Paul 			print_ifsizeof(indent + 1, prefix, type);
2644e115012SGarrett Wollman 		}
2654e115012SGarrett Wollman 		break;
2664e115012SGarrett Wollman 	case REL_ARRAY:
2674e115012SGarrett Wollman 		if (streq(type, "string")) {
2684e115012SGarrett Wollman 			alt = "string";
2694e115012SGarrett Wollman 		} else if (streq(type, "opaque")) {
2704e115012SGarrett Wollman 			alt = "bytes";
2714e115012SGarrett Wollman 		}
2724e115012SGarrett Wollman 		if (streq(type, "string")) {
2734e115012SGarrett Wollman 			print_ifopen(indent, alt);
2744e115012SGarrett Wollman 			print_ifarg(objname);
2754e115012SGarrett Wollman 		} else {
2764e115012SGarrett Wollman 			if (alt) {
2774e115012SGarrett Wollman 				print_ifopen(indent, alt);
2784e115012SGarrett Wollman 			} else {
2794e115012SGarrett Wollman 				print_ifopen(indent, "array");
2804e115012SGarrett Wollman 			}
2814e115012SGarrett Wollman 			print_ifarg("(char **)");
2824e115012SGarrett Wollman 			if (*objname == '&') {
2834e115012SGarrett Wollman 				f_print(fout, "%s.%s_val, (u_int *) %s.%s_len",
2844e115012SGarrett Wollman 					objname, name, objname, name);
2854e115012SGarrett Wollman 			} else {
286ff49530fSBill Paul 				f_print(fout,
287ff49530fSBill Paul 					"&%s->%s_val, (u_int *) &%s->%s_len",
2884e115012SGarrett Wollman 					objname, name, objname, name);
2894e115012SGarrett Wollman 			}
2904e115012SGarrett Wollman 		}
2914e115012SGarrett Wollman 		print_ifarg(amax);
2924e115012SGarrett Wollman 		if (!alt) {
293ff49530fSBill Paul 			print_ifsizeof(indent + 1, prefix, type);
2944e115012SGarrett Wollman 		}
2954e115012SGarrett Wollman 		break;
2964e115012SGarrett Wollman 	case REL_ALIAS:
2974e115012SGarrett Wollman 		print_ifopen(indent, type);
2984e115012SGarrett Wollman 		print_ifarg(objname);
2994e115012SGarrett Wollman 		break;
3004e115012SGarrett Wollman 	}
3014e115012SGarrett Wollman 	print_ifclose(indent);
3024e115012SGarrett Wollman }
3034e115012SGarrett Wollman 
3044e115012SGarrett Wollman /* ARGSUSED */
3054e115012SGarrett Wollman static
3064e115012SGarrett Wollman emit_enum(def)
3074e115012SGarrett Wollman 	definition *def;
3084e115012SGarrett Wollman {
3094e115012SGarrett Wollman 	print_ifopen(1, "enum");
3104e115012SGarrett Wollman 	print_ifarg("(enum_t *)objp");
3114e115012SGarrett Wollman 	print_ifclose(1);
3124e115012SGarrett Wollman }
3134e115012SGarrett Wollman 
314ff49530fSBill Paul static
315ff49530fSBill Paul emit_program(def)
316ff49530fSBill Paul 	definition *def;
317ff49530fSBill Paul {
318ff49530fSBill Paul 	decl_list *dl;
319ff49530fSBill Paul 	version_list *vlist;
320ff49530fSBill Paul 	proc_list *plist;
321ff49530fSBill Paul 
322ff49530fSBill Paul 	for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
323ff49530fSBill Paul 		for (plist = vlist->procs; plist != NULL; plist = plist->next) {
324ff49530fSBill Paul 			if (!newstyle || plist->arg_num < 2)
325ff49530fSBill Paul 				continue; /* old style, or single argument */
326ff49530fSBill Paul 			print_prog_header(plist);
327ff49530fSBill Paul 			for (dl = plist->args.decls; dl != NULL;
328ff49530fSBill Paul 			     dl = dl->next)
329ff49530fSBill Paul 				print_stat(1, &dl->decl);
330ff49530fSBill Paul 			print_trailer();
331ff49530fSBill Paul 		}
332ff49530fSBill Paul }
333ff49530fSBill Paul 
3344e115012SGarrett Wollman 
3354e115012SGarrett Wollman static
3364e115012SGarrett Wollman emit_union(def)
3374e115012SGarrett Wollman 	definition *def;
3384e115012SGarrett Wollman {
3394e115012SGarrett Wollman 	declaration *dflt;
3404e115012SGarrett Wollman 	case_list *cl;
3414e115012SGarrett Wollman 	declaration *cs;
3424e115012SGarrett Wollman 	char *object;
343ff49530fSBill Paul 	char *vecformat = "objp->%s_u.%s";
3444e115012SGarrett Wollman 	char *format = "&objp->%s_u.%s";
3454e115012SGarrett Wollman 
346ff49530fSBill Paul 	print_stat(1, &def->def.un.enum_decl);
3474e115012SGarrett Wollman 	f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
3484e115012SGarrett Wollman 	for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
349ff49530fSBill Paul 
3504e115012SGarrett Wollman 		f_print(fout, "\tcase %s:\n", cl->case_name);
351ff49530fSBill Paul 		if (cl->contflag == 1) /* a continued case statement */
352ff49530fSBill Paul 			continue;
353ff49530fSBill Paul 		cs = &cl->case_decl;
3544e115012SGarrett Wollman 		if (!streq(cs->type, "void")) {
3554e115012SGarrett Wollman 			object = alloc(strlen(def->def_name) + strlen(format) +
3564e115012SGarrett Wollman 				       strlen(cs->name) + 1);
357ff49530fSBill Paul 			if (isvectordef (cs->type, cs->rel)) {
358ff49530fSBill Paul 				s_print(object, vecformat, def->def_name,
359ff49530fSBill Paul 					cs->name);
360ff49530fSBill Paul 			} else {
361ff49530fSBill Paul 				s_print(object, format, def->def_name,
362ff49530fSBill Paul 					cs->name);
363ff49530fSBill Paul 			}
364ff49530fSBill Paul 			print_ifstat(2, cs->prefix, cs->type, cs->rel,
365ff49530fSBill Paul 				     cs->array_max, object, cs->name);
3664e115012SGarrett Wollman 			free(object);
3674e115012SGarrett Wollman 		}
3684e115012SGarrett Wollman 		f_print(fout, "\t\tbreak;\n");
3694e115012SGarrett Wollman 	}
3704e115012SGarrett Wollman 	dflt = def->def.un.default_decl;
3714e115012SGarrett Wollman 	if (dflt != NULL) {
3724e115012SGarrett Wollman 		if (!streq(dflt->type, "void")) {
3734e115012SGarrett Wollman 			f_print(fout, "\tdefault:\n");
3744e115012SGarrett Wollman 			object = alloc(strlen(def->def_name) + strlen(format) +
3754e115012SGarrett Wollman strlen(dflt->name) + 1);
376ff49530fSBill Paul 			if (isvectordef (dflt->type, dflt->rel)) {
377ff49530fSBill Paul 				s_print(object, vecformat, def->def_name,
378ff49530fSBill Paul 					dflt->name);
379ff49530fSBill Paul 			} else {
380ff49530fSBill Paul 				s_print(object, format, def->def_name,
381ff49530fSBill Paul 					dflt->name);
382ff49530fSBill Paul 			}
383ff49530fSBill Paul 
3844e115012SGarrett Wollman 			print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
3854e115012SGarrett Wollman 				    dflt->array_max, object, dflt->name);
3864e115012SGarrett Wollman 			free(object);
3874e115012SGarrett Wollman 			f_print(fout, "\t\tbreak;\n");
3884e115012SGarrett Wollman 		}
3894e115012SGarrett Wollman 	} else {
3904e115012SGarrett Wollman 		f_print(fout, "\tdefault:\n");
3914e115012SGarrett Wollman 		f_print(fout, "\t\treturn (FALSE);\n");
3924e115012SGarrett Wollman 	}
393ff49530fSBill Paul 
3944e115012SGarrett Wollman 	f_print(fout, "\t}\n");
3954e115012SGarrett Wollman }
3964e115012SGarrett Wollman 
397ff49530fSBill Paul static void
398ff49530fSBill Paul inline_struct(def, flag)
399ff49530fSBill Paul definition *def;
400ff49530fSBill Paul int flag;
401ff49530fSBill Paul {
402ff49530fSBill Paul 	decl_list *dl;
403ff49530fSBill Paul 	int i, size;
404ff49530fSBill Paul 	decl_list *cur, *psav;
405ff49530fSBill Paul 	bas_type *ptr;
406ff49530fSBill Paul 	char *sizestr, *plus;
407ff49530fSBill Paul 	char ptemp[256];
408ff49530fSBill Paul 	int indent = 1;
4094e115012SGarrett Wollman 
410ff49530fSBill Paul 	if (flag == PUT)
411ff49530fSBill Paul 		f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
412ff49530fSBill Paul 	else
413ff49530fSBill Paul 		f_print(fout, "\t\treturn (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
414ff49530fSBill Paul 
415ff49530fSBill Paul 	i = 0;
416ff49530fSBill Paul 	size = 0;
417ff49530fSBill Paul 	sizestr = NULL;
418ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
419ff49530fSBill Paul 		/* now walk down the list and check for basic types */
420ff49530fSBill Paul 		if ((dl->decl.prefix == NULL) &&
421ff49530fSBill Paul 		    ((ptr = find_type(dl->decl.type)) != NULL) &&
422ff49530fSBill Paul 		    ((dl->decl.rel == REL_ALIAS) ||
423ff49530fSBill Paul 		     (dl->decl.rel == REL_VECTOR))){
424ff49530fSBill Paul 			if (i == 0)
425ff49530fSBill Paul 				cur = dl;
426ff49530fSBill Paul 			i++;
427ff49530fSBill Paul 
428ff49530fSBill Paul 			if (dl->decl.rel == REL_ALIAS)
429ff49530fSBill Paul 				size += ptr->length;
430ff49530fSBill Paul 			else {
431ff49530fSBill Paul 				/* this code is required to handle arrays */
432ff49530fSBill Paul 				if (sizestr == NULL)
433ff49530fSBill Paul 					plus = "";
434ff49530fSBill Paul 				else
435ff49530fSBill Paul 					plus = " + ";
436ff49530fSBill Paul 
437ff49530fSBill Paul 				if (ptr->length != 1)
438ff49530fSBill Paul 					s_print(ptemp, "%s%s * %d",
439ff49530fSBill Paul 						plus, dl->decl.array_max,
440ff49530fSBill Paul 						ptr->length);
441ff49530fSBill Paul 				else
442ff49530fSBill Paul 					s_print(ptemp, "%s%s", plus,
443ff49530fSBill Paul 						dl->decl.array_max);
444ff49530fSBill Paul 
445ff49530fSBill Paul 				/* now concatenate to sizestr !!!! */
446ff49530fSBill Paul 				if (sizestr == NULL)
447ff49530fSBill Paul 					sizestr = strdup(ptemp);
448ff49530fSBill Paul 				else{
449ff49530fSBill Paul 					sizestr = realloc(sizestr,
450ff49530fSBill Paul 							  strlen(sizestr)
451ff49530fSBill Paul 							  +strlen(ptemp)+1);
452ff49530fSBill Paul 					if (sizestr == NULL){
453ff49530fSBill Paul 						f_print(stderr,
454ff49530fSBill Paul 							"Fatal error : no memory\n");
455ff49530fSBill Paul 						crash();
456ff49530fSBill Paul 					};
457ff49530fSBill Paul 					sizestr = strcat(sizestr, ptemp);
458ff49530fSBill Paul 					/* build up length of array */
459ff49530fSBill Paul 				}
460ff49530fSBill Paul 			}
461ff49530fSBill Paul 		} else {
462ff49530fSBill Paul 			if (i > 0)
463ff49530fSBill Paul 				if (sizestr == NULL && size < inline){
464ff49530fSBill Paul 					/*
465ff49530fSBill Paul 					 * don't expand into inline code
466ff49530fSBill Paul 					 * if size < inline
467ff49530fSBill Paul 					 */
468ff49530fSBill Paul 					while (cur != dl){
469ff49530fSBill Paul 						print_stat(indent + 1, &cur->decl);
470ff49530fSBill Paul 						cur = cur->next;
471ff49530fSBill Paul 					}
472ff49530fSBill Paul 				} else {
473ff49530fSBill Paul 					/* were already looking at a xdr_inlineable structure */
474ff49530fSBill Paul 					tabify(fout, indent + 1);
475ff49530fSBill Paul 					if (sizestr == NULL)
476ff49530fSBill Paul 						f_print(fout, "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
477ff49530fSBill Paul 							size);
478ff49530fSBill Paul 					else
479ff49530fSBill Paul 						if (size == 0)
480ff49530fSBill Paul 							f_print(fout,
481ff49530fSBill Paul 								"buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
482ff49530fSBill Paul 								sizestr);
483ff49530fSBill Paul 						else
484ff49530fSBill Paul 							f_print(fout,
485ff49530fSBill Paul 								"buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
486ff49530fSBill Paul 								size, sizestr);
487ff49530fSBill Paul 
488ff49530fSBill Paul 					f_print(fout, "\n");
489ff49530fSBill Paul 					tabify(fout, indent + 1);
490ff49530fSBill Paul 					f_print(fout,
491ff49530fSBill Paul 						"if (buf == NULL) {\n");
492ff49530fSBill Paul 
493ff49530fSBill Paul 					psav = cur;
494ff49530fSBill Paul 					while (cur != dl){
495ff49530fSBill Paul 						print_stat(indent + 2, &cur->decl);
496ff49530fSBill Paul 						cur = cur->next;
497ff49530fSBill Paul 					}
498ff49530fSBill Paul 
499ff49530fSBill Paul 					f_print(fout, "\n\t\t} else {\n");
500ff49530fSBill Paul 
501ff49530fSBill Paul 					cur = psav;
502ff49530fSBill Paul 					while (cur != dl){
503ff49530fSBill Paul 						emit_inline(indent + 2, &cur->decl, flag);
504ff49530fSBill Paul 						cur = cur->next;
505ff49530fSBill Paul 					}
506ff49530fSBill Paul 
507ff49530fSBill Paul 					tabify(fout, indent + 1);
508ff49530fSBill Paul 					f_print(fout, "}\n");
509ff49530fSBill Paul 				}
510ff49530fSBill Paul 			size = 0;
511ff49530fSBill Paul 			i = 0;
512ff49530fSBill Paul 			sizestr = NULL;
513ff49530fSBill Paul 			print_stat(indent + 1, &dl->decl);
514ff49530fSBill Paul 		}
515ff49530fSBill Paul 	}
516ff49530fSBill Paul 
517ff49530fSBill Paul 	if (i > 0)
518ff49530fSBill Paul 		if (sizestr == NULL && size < inline){
519ff49530fSBill Paul 			/* don't expand into inline code if size < inline */
520ff49530fSBill Paul 			while (cur != dl){
521ff49530fSBill Paul 				print_stat(indent + 1, &cur->decl);
522ff49530fSBill Paul 				cur = cur->next;
523ff49530fSBill Paul 			}
524ff49530fSBill Paul 		} else {
525ff49530fSBill Paul 			/* were already looking at a xdr_inlineable structure */
526ff49530fSBill Paul 			if (sizestr == NULL)
527ff49530fSBill Paul 				f_print(fout, "\t\tbuf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
528ff49530fSBill Paul 					size);
529ff49530fSBill Paul 			else
530ff49530fSBill Paul 				if (size == 0)
531ff49530fSBill Paul 					f_print(fout,
532ff49530fSBill Paul 						"\t\tbuf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
533ff49530fSBill Paul 						sizestr);
534ff49530fSBill Paul 				else
535ff49530fSBill Paul 					f_print(fout,
536ff49530fSBill Paul 						"\t\tbuf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
537ff49530fSBill Paul 						size, sizestr);
538ff49530fSBill Paul 
539ff49530fSBill Paul 			f_print(fout, "\n\t\tif (buf == NULL) {\n");
540ff49530fSBill Paul 			psav = cur;
541ff49530fSBill Paul 			while (cur != NULL){
542ff49530fSBill Paul 				print_stat(indent + 2, &cur->decl);
543ff49530fSBill Paul 				cur = cur->next;
544ff49530fSBill Paul 			}
545ff49530fSBill Paul 			f_print(fout, "\t\t} else {\n");
546ff49530fSBill Paul 
547ff49530fSBill Paul 			cur = psav;
548ff49530fSBill Paul 			while (cur != dl){
549ff49530fSBill Paul 				emit_inline(indent + 2, &cur->decl, flag);
550ff49530fSBill Paul 				cur = cur->next;
551ff49530fSBill Paul 			}
552ff49530fSBill Paul 			f_print(fout, "\t\t}\n");
553ff49530fSBill Paul 		}
554ff49530fSBill Paul }
5554e115012SGarrett Wollman 
5564e115012SGarrett Wollman static
5574e115012SGarrett Wollman emit_struct(def)
5584e115012SGarrett Wollman 	definition *def;
5594e115012SGarrett Wollman {
5604e115012SGarrett Wollman 	decl_list *dl;
561ff49530fSBill Paul 	int i, j, size, flag;
562ff49530fSBill Paul 	bas_type *ptr;
563ff49530fSBill Paul 	int can_inline;
5644e115012SGarrett Wollman 
565ff49530fSBill Paul 	if (inline == 0) {
566ff49530fSBill Paul 		/* No xdr_inlining at all */
567ff49530fSBill Paul 		for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
568ff49530fSBill Paul 			print_stat(1, &dl->decl);
569ff49530fSBill Paul 		return;
5704e115012SGarrett Wollman 	}
5714e115012SGarrett Wollman 
572ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
573ff49530fSBill Paul 		if (dl->decl.rel == REL_VECTOR){
574ff49530fSBill Paul 			f_print(fout, "\tint i;\n");
575ff49530fSBill Paul 			break;
576ff49530fSBill Paul 		}
5774e115012SGarrett Wollman 
578ff49530fSBill Paul 	size = 0;
579ff49530fSBill Paul 	can_inline = 0;
580ff49530fSBill Paul 	/*
581ff49530fSBill Paul 	 * Make a first pass and see if inling is possible.
582ff49530fSBill Paul 	 */
583ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
584ff49530fSBill Paul 		if ((dl->decl.prefix == NULL) &&
585ff49530fSBill Paul 		    ((ptr = find_type(dl->decl.type)) != NULL) &&
586ff49530fSBill Paul 		    ((dl->decl.rel == REL_ALIAS)||
587ff49530fSBill Paul 		     (dl->decl.rel == REL_VECTOR))){
588ff49530fSBill Paul 			if (dl->decl.rel == REL_ALIAS)
589ff49530fSBill Paul 				size += ptr->length;
590ff49530fSBill Paul 			else {
591ff49530fSBill Paul 				can_inline = 1;
592ff49530fSBill Paul 				break; /* can be inlined */
593ff49530fSBill Paul 			}
594ff49530fSBill Paul 		} else {
595ff49530fSBill Paul 			if (size >= inline){
596ff49530fSBill Paul 				can_inline = 1;
597ff49530fSBill Paul 				break; /* can be inlined */
598ff49530fSBill Paul 			}
599ff49530fSBill Paul 			size = 0;
600ff49530fSBill Paul 		}
601ff49530fSBill Paul 	if (size >= inline)
602ff49530fSBill Paul 		can_inline = 1;
6034e115012SGarrett Wollman 
604ff49530fSBill Paul 	if (can_inline == 0){	/* can not inline, drop back to old mode */
605ff49530fSBill Paul 		for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
606ff49530fSBill Paul 			print_stat(1, &dl->decl);
607ff49530fSBill Paul 		return;
608ff49530fSBill Paul 	}
609ff49530fSBill Paul 
610ff49530fSBill Paul 	flag = PUT;
611ff49530fSBill Paul 	for (j = 0; j < 2; j++){
612ff49530fSBill Paul 		inline_struct(def, flag);
613ff49530fSBill Paul 		if (flag == PUT)
614ff49530fSBill Paul 			flag = GET;
615ff49530fSBill Paul 	}
616ff49530fSBill Paul 
617ff49530fSBill Paul 	f_print(fout, "\t\treturn (TRUE);\n\t}\n\n");
618ff49530fSBill Paul 
619ff49530fSBill Paul 	/* now take care of XDR_FREE case */
620ff49530fSBill Paul 
621ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
622ff49530fSBill Paul 		print_stat(1, &dl->decl);
623ff49530fSBill Paul 
624ff49530fSBill Paul }
6254e115012SGarrett Wollman 
6264e115012SGarrett Wollman static
6274e115012SGarrett Wollman emit_typedef(def)
6284e115012SGarrett Wollman 	definition *def;
6294e115012SGarrett Wollman {
6304e115012SGarrett Wollman 	char *prefix = def->def.ty.old_prefix;
6314e115012SGarrett Wollman 	char *type = def->def.ty.old_type;
6324e115012SGarrett Wollman 	char *amax = def->def.ty.array_max;
6334e115012SGarrett Wollman 	relation rel = def->def.ty.rel;
6344e115012SGarrett Wollman 
6354e115012SGarrett Wollman 	print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
6364e115012SGarrett Wollman }
6374e115012SGarrett Wollman 
6384e115012SGarrett Wollman static
639ff49530fSBill Paul print_stat(indent, dec)
640ff49530fSBill Paul 	int indent;
6414e115012SGarrett Wollman 	declaration *dec;
6424e115012SGarrett Wollman {
6434e115012SGarrett Wollman 	char *prefix = dec->prefix;
6444e115012SGarrett Wollman 	char *type = dec->type;
6454e115012SGarrett Wollman 	char *amax = dec->array_max;
6464e115012SGarrett Wollman 	relation rel = dec->rel;
6474e115012SGarrett Wollman 	char name[256];
6484e115012SGarrett Wollman 
6494e115012SGarrett Wollman 	if (isvectordef(type, rel)) {
6504e115012SGarrett Wollman 		s_print(name, "objp->%s", dec->name);
6514e115012SGarrett Wollman 	} else {
6524e115012SGarrett Wollman 		s_print(name, "&objp->%s", dec->name);
6534e115012SGarrett Wollman 	}
654ff49530fSBill Paul 	print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
655ff49530fSBill Paul }
656ff49530fSBill Paul 
657ff49530fSBill Paul 
658ff49530fSBill Paul char *upcase ();
659ff49530fSBill Paul 
660ff49530fSBill Paul emit_inline(indent, decl, flag)
661ff49530fSBill Paul int indent;
662ff49530fSBill Paul declaration *decl;
663ff49530fSBill Paul int flag;
664ff49530fSBill Paul {
665ff49530fSBill Paul 	switch (decl->rel) {
666ff49530fSBill Paul 	case  REL_ALIAS :
667ff49530fSBill Paul 		emit_single_in_line(indent, decl, flag, REL_ALIAS);
668ff49530fSBill Paul 		break;
669ff49530fSBill Paul 	case REL_VECTOR :
670ff49530fSBill Paul 		tabify(fout, indent);
671ff49530fSBill Paul 		f_print(fout, "{\n");
672ff49530fSBill Paul 		tabify(fout, indent + 1);
673ff49530fSBill Paul 		f_print(fout, "register %s *genp;\n\n", decl->type);
674ff49530fSBill Paul 		tabify(fout, indent + 1);
675ff49530fSBill Paul 		f_print(fout,
676ff49530fSBill Paul 			"for (i = 0, genp = objp->%s;\n", decl->name);
677ff49530fSBill Paul 		tabify(fout, indent + 2);
678ff49530fSBill Paul 		f_print(fout, "i < %s; i++) {\n", decl->array_max);
679ff49530fSBill Paul 		emit_single_in_line(indent + 2, decl, flag, REL_VECTOR);
680ff49530fSBill Paul 		tabify(fout, indent + 1);
681ff49530fSBill Paul 		f_print(fout, "}\n");
682ff49530fSBill Paul 		tabify(fout, indent);
683ff49530fSBill Paul 		f_print(fout, "}\n");
684ff49530fSBill Paul 	}
685ff49530fSBill Paul }
686ff49530fSBill Paul 
687ff49530fSBill Paul emit_single_in_line(indent, decl, flag, rel)
688ff49530fSBill Paul int indent;
689ff49530fSBill Paul declaration *decl;
690ff49530fSBill Paul int flag;
691ff49530fSBill Paul relation rel;
692ff49530fSBill Paul {
693ff49530fSBill Paul 	char *upp_case;
694ff49530fSBill Paul 	int freed = 0;
695ff49530fSBill Paul 
696ff49530fSBill Paul 	tabify(fout, indent);
697ff49530fSBill Paul 	if (flag == PUT)
698ff49530fSBill Paul 		f_print(fout, "IXDR_PUT_");
699ff49530fSBill Paul 	else
700ff49530fSBill Paul 		if (rel == REL_ALIAS)
701ff49530fSBill Paul 			f_print(fout, "objp->%s = IXDR_GET_", decl->name);
702ff49530fSBill Paul 		else
703ff49530fSBill Paul 			f_print(fout, "*genp++ = IXDR_GET_");
704ff49530fSBill Paul 
705ff49530fSBill Paul 	upp_case = upcase(decl->type);
706ff49530fSBill Paul 
707ff49530fSBill Paul 	/* hack	 - XX */
708ff49530fSBill Paul 	if (strcmp(upp_case, "INT") == 0)
709ff49530fSBill Paul 	{
710ff49530fSBill Paul 		free(upp_case);
711ff49530fSBill Paul 		freed = 1;
712ff49530fSBill Paul 		upp_case = "LONG";
713ff49530fSBill Paul 	}
714ff49530fSBill Paul 
715ff49530fSBill Paul 	if (strcmp(upp_case, "U_INT") == 0)
716ff49530fSBill Paul 	{
717ff49530fSBill Paul 		free(upp_case);
718ff49530fSBill Paul 		freed = 1;
719ff49530fSBill Paul 		upp_case = "U_LONG";
720ff49530fSBill Paul 	}
721ff49530fSBill Paul 	if (flag == PUT)
722ff49530fSBill Paul 		if (rel == REL_ALIAS)
723ff49530fSBill Paul 			f_print(fout,
724ff49530fSBill Paul 				"%s(buf, objp->%s);\n", upp_case, decl->name);
725ff49530fSBill Paul 		else
726ff49530fSBill Paul 			f_print(fout, "%s(buf, *genp++);\n", upp_case);
727ff49530fSBill Paul 
728ff49530fSBill Paul 	else
729ff49530fSBill Paul 		f_print(fout, "%s(buf);\n", upp_case);
730ff49530fSBill Paul 	if (!freed)
731ff49530fSBill Paul 		free(upp_case);
732ff49530fSBill Paul }
733ff49530fSBill Paul 
734ff49530fSBill Paul char *upcase(str)
735ff49530fSBill Paul char *str;
736ff49530fSBill Paul {
737ff49530fSBill Paul 	char *ptr, *hptr;
738ff49530fSBill Paul 
739ff49530fSBill Paul 	ptr =  (char *)malloc(strlen(str)+1);
740ff49530fSBill Paul 	if (ptr == (char *) NULL)
741ff49530fSBill Paul 	{
742ff49530fSBill Paul 		f_print(stderr, "malloc failed\n");
743ff49530fSBill Paul 		exit(1);
744ff49530fSBill Paul 	};
745ff49530fSBill Paul 
746ff49530fSBill Paul 	hptr = ptr;
747ff49530fSBill Paul 	while (*str != '\0')
748ff49530fSBill Paul 		*ptr++ = toupper(*str++);
749ff49530fSBill Paul 
750ff49530fSBill Paul 	*ptr = '\0';
751ff49530fSBill Paul 	return (hptr);
7524e115012SGarrett Wollman }
753