xref: /freebsd/usr.bin/rpcgen/rpc_cout.c (revision 9ef5c48befdfa36ad5ef5e0ae898e7ca9397abcc)
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[] =
379ef5c48bSBill Fumerola 	"$Id: rpc_cout.c,v 1.5 1997/08/06 06:47:39 charnier Exp $";
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 
51526195adSJordan K. Hubbard static void print_header __P(( definition * ));
52526195adSJordan K. Hubbard static void print_trailer __P(( void ));
53526195adSJordan K. Hubbard static void print_stat __P(( int , declaration * ));
54526195adSJordan K. Hubbard static void emit_enum __P(( definition * ));
55526195adSJordan K. Hubbard static void emit_program __P(( definition * ));
56526195adSJordan K. Hubbard static void emit_union __P(( definition * ));
57526195adSJordan K. Hubbard static void emit_struct __P(( definition * ));
58526195adSJordan K. Hubbard static void emit_typedef __P(( definition * ));
59526195adSJordan K. Hubbard static void emit_inline __P(( int, declaration *, int ));
60526195adSJordan K. Hubbard static void emit_single_in_line __P(( 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:
1024e115012SGarrett Wollman 	}
1034e115012SGarrett Wollman 	print_trailer();
1044e115012SGarrett Wollman }
1054e115012SGarrett Wollman 
106526195adSJordan K. Hubbard static int
1074e115012SGarrett Wollman findtype(def, type)
1084e115012SGarrett Wollman 	definition *def;
1094e115012SGarrett Wollman 	char *type;
1104e115012SGarrett Wollman {
111ff49530fSBill Paul 
1124e115012SGarrett Wollman 	if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
1134e115012SGarrett Wollman 		return (0);
1144e115012SGarrett Wollman 	} else {
1154e115012SGarrett Wollman 		return (streq(def->def_name, type));
1164e115012SGarrett Wollman 	}
1174e115012SGarrett Wollman }
1184e115012SGarrett Wollman 
119526195adSJordan K. Hubbard static int
1204e115012SGarrett Wollman undefined(type)
1214e115012SGarrett Wollman 	char *type;
1224e115012SGarrett Wollman {
1234e115012SGarrett Wollman 	definition *def;
1244e115012SGarrett Wollman 
1254e115012SGarrett Wollman 	def = (definition *) FINDVAL(defined, type, findtype);
1264e115012SGarrett Wollman 	return (def == NULL);
1274e115012SGarrett Wollman }
1284e115012SGarrett Wollman 
1294e115012SGarrett Wollman 
130526195adSJordan K. Hubbard static void
131ff49530fSBill Paul print_generic_header(procname, pointerp)
132ff49530fSBill Paul     char* procname;
133ff49530fSBill Paul     int pointerp;
134ff49530fSBill Paul {
135ff49530fSBill Paul 	f_print(fout, "\n");
136ff49530fSBill Paul 	f_print(fout, "bool_t\n");
137ff49530fSBill Paul 	if (Cflag) {
138ff49530fSBill Paul 	    f_print(fout, "xdr_%s(", procname);
139ff49530fSBill Paul 	    f_print(fout, "register XDR *xdrs, ");
140ff49530fSBill Paul 	    f_print(fout, "%s ", procname);
141ff49530fSBill Paul 	    if (pointerp)
142ff49530fSBill Paul 		    f_print(fout, "*");
143ff49530fSBill Paul 	    f_print(fout, "objp)\n{\n\n");
144ff49530fSBill Paul 	} else {
145ff49530fSBill Paul 	    f_print(fout, "xdr_%s(xdrs, objp)\n", procname);
146ff49530fSBill Paul 	    f_print(fout, "\tregister XDR *xdrs;\n");
147ff49530fSBill Paul 	    f_print(fout, "\t%s ", procname);
148ff49530fSBill Paul 	    if (pointerp)
149ff49530fSBill Paul 		    f_print(fout, "*");
150ff49530fSBill Paul 	    f_print(fout, "objp;\n{\n\n");
151ff49530fSBill Paul 	}
152ff49530fSBill Paul }
153ff49530fSBill Paul 
154526195adSJordan K. Hubbard static void
1554e115012SGarrett Wollman print_header(def)
1564e115012SGarrett Wollman 	definition *def;
1574e115012SGarrett Wollman {
158ff49530fSBill Paul 	print_generic_header(def->def_name,
159ff49530fSBill Paul 			    def->def_kind != DEF_TYPEDEF ||
160ff49530fSBill Paul 			    !isvectordef(def->def.ty.old_type,
161ff49530fSBill Paul 					 def->def.ty.rel));
162ff49530fSBill Paul 	/* Now add Inline support */
163ff49530fSBill Paul 
164ff49530fSBill Paul 	if (inline == 0)
165ff49530fSBill Paul 		return;
166ff49530fSBill Paul 	/* May cause lint to complain. but  ... */
167ff49530fSBill Paul 	f_print(fout, "\tregister long *buf;\n\n");
1684e115012SGarrett Wollman }
169ff49530fSBill Paul 
170526195adSJordan K. Hubbard static void
171ff49530fSBill Paul print_prog_header(plist)
172ff49530fSBill Paul 	proc_list *plist;
173ff49530fSBill Paul {
174ff49530fSBill Paul 	print_generic_header(plist->args.argname, 1);
1754e115012SGarrett Wollman }
1764e115012SGarrett Wollman 
177526195adSJordan K. Hubbard static void
1784e115012SGarrett Wollman print_trailer()
1794e115012SGarrett Wollman {
1804e115012SGarrett Wollman 	f_print(fout, "\treturn (TRUE);\n");
1814e115012SGarrett Wollman 	f_print(fout, "}\n");
1824e115012SGarrett Wollman }
1834e115012SGarrett Wollman 
1844e115012SGarrett Wollman 
185526195adSJordan K. Hubbard static void
1864e115012SGarrett Wollman print_ifopen(indent, name)
1874e115012SGarrett Wollman 	int indent;
1884e115012SGarrett Wollman 	char *name;
1894e115012SGarrett Wollman {
1904e115012SGarrett Wollman 	tabify(fout, indent);
1914e115012SGarrett Wollman 	f_print(fout, "if (!xdr_%s(xdrs", name);
1924e115012SGarrett Wollman }
1934e115012SGarrett Wollman 
194526195adSJordan K. Hubbard static void
1954e115012SGarrett Wollman print_ifarg(arg)
1964e115012SGarrett Wollman 	char *arg;
1974e115012SGarrett Wollman {
1984e115012SGarrett Wollman 	f_print(fout, ", %s", arg);
1994e115012SGarrett Wollman }
2004e115012SGarrett Wollman 
201526195adSJordan K. Hubbard static void
202ff49530fSBill Paul print_ifsizeof(indent, prefix, type)
203ff49530fSBill Paul 	int indent;
2044e115012SGarrett Wollman 	char *prefix;
2054e115012SGarrett Wollman 	char *type;
2064e115012SGarrett Wollman {
207ff49530fSBill Paul 	if (indent) {
208ff49530fSBill Paul 		f_print(fout, ",\n");
209ff49530fSBill Paul 		tabify(fout, indent);
2104e115012SGarrett Wollman 	} else  {
211ff49530fSBill Paul 		f_print(fout, ", ");
212ff49530fSBill Paul 	}
213ff49530fSBill Paul 	if (streq(type, "bool")) {
214ff49530fSBill Paul 		f_print(fout, "sizeof (bool_t), (xdrproc_t) xdr_bool");
215ff49530fSBill Paul 	} else {
216ff49530fSBill Paul 		f_print(fout, "sizeof (");
2174e115012SGarrett Wollman 		if (undefined(type) && prefix) {
2184e115012SGarrett Wollman 			f_print(fout, "%s ", prefix);
2194e115012SGarrett Wollman 		}
220ff49530fSBill Paul 		f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type);
2214e115012SGarrett Wollman 	}
2224e115012SGarrett Wollman }
2234e115012SGarrett Wollman 
224526195adSJordan K. Hubbard static void
2254e115012SGarrett Wollman print_ifclose(indent)
2264e115012SGarrett Wollman 	int indent;
2274e115012SGarrett Wollman {
228ff49530fSBill Paul 	f_print(fout, "))\n");
2294e115012SGarrett Wollman 	tabify(fout, indent);
2304e115012SGarrett Wollman 	f_print(fout, "\treturn (FALSE);\n");
2314e115012SGarrett Wollman }
2324e115012SGarrett Wollman 
233526195adSJordan K. Hubbard static void
2344e115012SGarrett Wollman print_ifstat(indent, prefix, type, rel, amax, objname, name)
2354e115012SGarrett Wollman 	int indent;
2364e115012SGarrett Wollman 	char *prefix;
2374e115012SGarrett Wollman 	char *type;
2384e115012SGarrett Wollman 	relation rel;
2394e115012SGarrett Wollman 	char *amax;
2404e115012SGarrett Wollman 	char *objname;
2414e115012SGarrett Wollman 	char *name;
2424e115012SGarrett Wollman {
2434e115012SGarrett Wollman 	char *alt = NULL;
2444e115012SGarrett Wollman 
2454e115012SGarrett Wollman 	switch (rel) {
2464e115012SGarrett Wollman 	case REL_POINTER:
2474e115012SGarrett Wollman 		print_ifopen(indent, "pointer");
2484e115012SGarrett Wollman 		print_ifarg("(char **)");
2494e115012SGarrett Wollman 		f_print(fout, "%s", objname);
250ff49530fSBill Paul 		print_ifsizeof(0, prefix, type);
2514e115012SGarrett Wollman 		break;
2524e115012SGarrett Wollman 	case REL_VECTOR:
2534e115012SGarrett Wollman 		if (streq(type, "string")) {
2544e115012SGarrett Wollman 			alt = "string";
2554e115012SGarrett Wollman 		} else if (streq(type, "opaque")) {
2564e115012SGarrett Wollman 			alt = "opaque";
2574e115012SGarrett Wollman 		}
2584e115012SGarrett Wollman 		if (alt) {
2594e115012SGarrett Wollman 			print_ifopen(indent, alt);
2604e115012SGarrett Wollman 			print_ifarg(objname);
2614e115012SGarrett Wollman 		} else {
2624e115012SGarrett Wollman 			print_ifopen(indent, "vector");
2634e115012SGarrett Wollman 			print_ifarg("(char *)");
2644e115012SGarrett Wollman 			f_print(fout, "%s", objname);
2654e115012SGarrett Wollman 		}
2664e115012SGarrett Wollman 		print_ifarg(amax);
2674e115012SGarrett Wollman 		if (!alt) {
268ff49530fSBill Paul 			print_ifsizeof(indent + 1, prefix, type);
2694e115012SGarrett Wollman 		}
2704e115012SGarrett Wollman 		break;
2714e115012SGarrett Wollman 	case REL_ARRAY:
2724e115012SGarrett Wollman 		if (streq(type, "string")) {
2734e115012SGarrett Wollman 			alt = "string";
2744e115012SGarrett Wollman 		} else if (streq(type, "opaque")) {
2754e115012SGarrett Wollman 			alt = "bytes";
2764e115012SGarrett Wollman 		}
2774e115012SGarrett Wollman 		if (streq(type, "string")) {
2784e115012SGarrett Wollman 			print_ifopen(indent, alt);
2794e115012SGarrett Wollman 			print_ifarg(objname);
2804e115012SGarrett Wollman 		} else {
2814e115012SGarrett Wollman 			if (alt) {
2824e115012SGarrett Wollman 				print_ifopen(indent, alt);
2834e115012SGarrett Wollman 			} else {
2844e115012SGarrett Wollman 				print_ifopen(indent, "array");
2854e115012SGarrett Wollman 			}
2864e115012SGarrett Wollman 			print_ifarg("(char **)");
2874e115012SGarrett Wollman 			if (*objname == '&') {
2884e115012SGarrett Wollman 				f_print(fout, "%s.%s_val, (u_int *) %s.%s_len",
2894e115012SGarrett Wollman 					objname, name, objname, name);
2904e115012SGarrett Wollman 			} else {
291ff49530fSBill Paul 				f_print(fout,
292ff49530fSBill Paul 					"&%s->%s_val, (u_int *) &%s->%s_len",
2934e115012SGarrett Wollman 					objname, name, objname, name);
2944e115012SGarrett Wollman 			}
2954e115012SGarrett Wollman 		}
2964e115012SGarrett Wollman 		print_ifarg(amax);
2974e115012SGarrett Wollman 		if (!alt) {
298ff49530fSBill Paul 			print_ifsizeof(indent + 1, prefix, type);
2994e115012SGarrett Wollman 		}
3004e115012SGarrett Wollman 		break;
3014e115012SGarrett Wollman 	case REL_ALIAS:
3024e115012SGarrett Wollman 		print_ifopen(indent, type);
3034e115012SGarrett Wollman 		print_ifarg(objname);
3044e115012SGarrett Wollman 		break;
3054e115012SGarrett Wollman 	}
3064e115012SGarrett Wollman 	print_ifclose(indent);
3074e115012SGarrett Wollman }
3084e115012SGarrett Wollman 
3094e115012SGarrett Wollman /* ARGSUSED */
310526195adSJordan K. Hubbard static void
3114e115012SGarrett Wollman emit_enum(def)
3124e115012SGarrett Wollman 	definition *def;
3134e115012SGarrett Wollman {
3144e115012SGarrett Wollman 	print_ifopen(1, "enum");
3154e115012SGarrett Wollman 	print_ifarg("(enum_t *)objp");
3164e115012SGarrett Wollman 	print_ifclose(1);
3174e115012SGarrett Wollman }
3184e115012SGarrett Wollman 
319526195adSJordan K. Hubbard static void
320ff49530fSBill Paul emit_program(def)
321ff49530fSBill Paul 	definition *def;
322ff49530fSBill Paul {
323ff49530fSBill Paul 	decl_list *dl;
324ff49530fSBill Paul 	version_list *vlist;
325ff49530fSBill Paul 	proc_list *plist;
326ff49530fSBill Paul 
327ff49530fSBill Paul 	for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
328ff49530fSBill Paul 		for (plist = vlist->procs; plist != NULL; plist = plist->next) {
329ff49530fSBill Paul 			if (!newstyle || plist->arg_num < 2)
330ff49530fSBill Paul 				continue; /* old style, or single argument */
331ff49530fSBill Paul 			print_prog_header(plist);
332ff49530fSBill Paul 			for (dl = plist->args.decls; dl != NULL;
333ff49530fSBill Paul 			     dl = dl->next)
334ff49530fSBill Paul 				print_stat(1, &dl->decl);
335ff49530fSBill Paul 			print_trailer();
336ff49530fSBill Paul 		}
337ff49530fSBill Paul }
338ff49530fSBill Paul 
3394e115012SGarrett Wollman 
340526195adSJordan K. Hubbard static void
3414e115012SGarrett Wollman emit_union(def)
3424e115012SGarrett Wollman 	definition *def;
3434e115012SGarrett Wollman {
3444e115012SGarrett Wollman 	declaration *dflt;
3454e115012SGarrett Wollman 	case_list *cl;
3464e115012SGarrett Wollman 	declaration *cs;
3474e115012SGarrett Wollman 	char *object;
348ff49530fSBill Paul 	char *vecformat = "objp->%s_u.%s";
3494e115012SGarrett Wollman 	char *format = "&objp->%s_u.%s";
3504e115012SGarrett Wollman 
351ff49530fSBill Paul 	print_stat(1, &def->def.un.enum_decl);
3524e115012SGarrett Wollman 	f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
3534e115012SGarrett Wollman 	for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
354ff49530fSBill Paul 
3554e115012SGarrett Wollman 		f_print(fout, "\tcase %s:\n", cl->case_name);
356ff49530fSBill Paul 		if (cl->contflag == 1) /* a continued case statement */
357ff49530fSBill Paul 			continue;
358ff49530fSBill Paul 		cs = &cl->case_decl;
3594e115012SGarrett Wollman 		if (!streq(cs->type, "void")) {
3604e115012SGarrett Wollman 			object = alloc(strlen(def->def_name) + strlen(format) +
3614e115012SGarrett Wollman 				       strlen(cs->name) + 1);
362ff49530fSBill Paul 			if (isvectordef (cs->type, cs->rel)) {
363ff49530fSBill Paul 				s_print(object, vecformat, def->def_name,
364ff49530fSBill Paul 					cs->name);
365ff49530fSBill Paul 			} else {
366ff49530fSBill Paul 				s_print(object, format, def->def_name,
367ff49530fSBill Paul 					cs->name);
368ff49530fSBill Paul 			}
369ff49530fSBill Paul 			print_ifstat(2, cs->prefix, cs->type, cs->rel,
370ff49530fSBill Paul 				     cs->array_max, object, cs->name);
3714e115012SGarrett Wollman 			free(object);
3724e115012SGarrett Wollman 		}
3734e115012SGarrett Wollman 		f_print(fout, "\t\tbreak;\n");
3744e115012SGarrett Wollman 	}
3754e115012SGarrett Wollman 	dflt = def->def.un.default_decl;
3764e115012SGarrett Wollman 	if (dflt != NULL) {
3774e115012SGarrett Wollman 		if (!streq(dflt->type, "void")) {
3784e115012SGarrett Wollman 			f_print(fout, "\tdefault:\n");
3794e115012SGarrett Wollman 			object = alloc(strlen(def->def_name) + strlen(format) +
3804e115012SGarrett Wollman strlen(dflt->name) + 1);
381ff49530fSBill Paul 			if (isvectordef (dflt->type, dflt->rel)) {
382ff49530fSBill Paul 				s_print(object, vecformat, def->def_name,
383ff49530fSBill Paul 					dflt->name);
384ff49530fSBill Paul 			} else {
385ff49530fSBill Paul 				s_print(object, format, def->def_name,
386ff49530fSBill Paul 					dflt->name);
387ff49530fSBill Paul 			}
388ff49530fSBill Paul 
3894e115012SGarrett Wollman 			print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
3904e115012SGarrett Wollman 				    dflt->array_max, object, dflt->name);
3914e115012SGarrett Wollman 			free(object);
3924e115012SGarrett Wollman 			f_print(fout, "\t\tbreak;\n");
393526195adSJordan K. Hubbard 		} else {
394526195adSJordan K. Hubbard 			f_print(fout, "\tdefault:\n");
395526195adSJordan K. Hubbard 			f_print(fout, "\t\tbreak;\n");
3964e115012SGarrett Wollman 		}
3974e115012SGarrett Wollman 	} else {
3984e115012SGarrett Wollman 		f_print(fout, "\tdefault:\n");
3994e115012SGarrett Wollman 		f_print(fout, "\t\treturn (FALSE);\n");
4004e115012SGarrett Wollman 	}
401ff49530fSBill Paul 
4024e115012SGarrett Wollman 	f_print(fout, "\t}\n");
4034e115012SGarrett Wollman }
4044e115012SGarrett Wollman 
405ff49530fSBill Paul static void
406ff49530fSBill Paul inline_struct(def, flag)
407ff49530fSBill Paul definition *def;
408ff49530fSBill Paul int flag;
409ff49530fSBill Paul {
410ff49530fSBill Paul 	decl_list *dl;
411ff49530fSBill Paul 	int i, size;
412ff49530fSBill Paul 	decl_list *cur, *psav;
413ff49530fSBill Paul 	bas_type *ptr;
414ff49530fSBill Paul 	char *sizestr, *plus;
415ff49530fSBill Paul 	char ptemp[256];
416ff49530fSBill Paul 	int indent = 1;
4174e115012SGarrett Wollman 
418ff49530fSBill Paul 	if (flag == PUT)
419ff49530fSBill Paul 		f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
420ff49530fSBill Paul 	else
421ff49530fSBill Paul 		f_print(fout, "\t\treturn (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
422ff49530fSBill Paul 
423ff49530fSBill Paul 	i = 0;
424ff49530fSBill Paul 	size = 0;
425ff49530fSBill Paul 	sizestr = NULL;
426ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
427ff49530fSBill Paul 		/* now walk down the list and check for basic types */
428ff49530fSBill Paul 		if ((dl->decl.prefix == NULL) &&
429ff49530fSBill Paul 		    ((ptr = find_type(dl->decl.type)) != NULL) &&
430ff49530fSBill Paul 		    ((dl->decl.rel == REL_ALIAS) ||
431ff49530fSBill Paul 		     (dl->decl.rel == REL_VECTOR))){
432ff49530fSBill Paul 			if (i == 0)
433ff49530fSBill Paul 				cur = dl;
434ff49530fSBill Paul 			i++;
435ff49530fSBill Paul 
436ff49530fSBill Paul 			if (dl->decl.rel == REL_ALIAS)
437ff49530fSBill Paul 				size += ptr->length;
438ff49530fSBill Paul 			else {
439ff49530fSBill Paul 				/* this code is required to handle arrays */
440ff49530fSBill Paul 				if (sizestr == NULL)
441ff49530fSBill Paul 					plus = "";
442ff49530fSBill Paul 				else
443ff49530fSBill Paul 					plus = " + ";
444ff49530fSBill Paul 
445ff49530fSBill Paul 				if (ptr->length != 1)
446ff49530fSBill Paul 					s_print(ptemp, "%s%s * %d",
447ff49530fSBill Paul 						plus, dl->decl.array_max,
448ff49530fSBill Paul 						ptr->length);
449ff49530fSBill Paul 				else
450ff49530fSBill Paul 					s_print(ptemp, "%s%s", plus,
451ff49530fSBill Paul 						dl->decl.array_max);
452ff49530fSBill Paul 
453ff49530fSBill Paul 				/* now concatenate to sizestr !!!! */
454ff49530fSBill Paul 				if (sizestr == NULL)
455ff49530fSBill Paul 					sizestr = strdup(ptemp);
456ff49530fSBill Paul 				else{
457ff49530fSBill Paul 					sizestr = realloc(sizestr,
458ff49530fSBill Paul 							  strlen(sizestr)
459ff49530fSBill Paul 							  +strlen(ptemp)+1);
460ff49530fSBill Paul 					if (sizestr == NULL){
4610e76f40dSPhilippe Charnier 						warnx("fatal error: no memory");
462ff49530fSBill Paul 						crash();
463ff49530fSBill Paul 					};
464ff49530fSBill Paul 					sizestr = strcat(sizestr, ptemp);
465ff49530fSBill Paul 					/* build up length of array */
466ff49530fSBill Paul 				}
467ff49530fSBill Paul 			}
468ff49530fSBill Paul 		} else {
4699ef5c48bSBill Fumerola 			if (i > 0) {
470ff49530fSBill Paul 				if (sizestr == NULL && size < inline){
471ff49530fSBill Paul 					/*
472ff49530fSBill Paul 					 * don't expand into inline code
473ff49530fSBill Paul 					 * if size < inline
474ff49530fSBill Paul 					 */
475ff49530fSBill Paul 					while (cur != dl){
476ff49530fSBill Paul 						print_stat(indent + 1, &cur->decl);
477ff49530fSBill Paul 						cur = cur->next;
478ff49530fSBill Paul 					}
479ff49530fSBill Paul 				} else {
480ff49530fSBill Paul 					/* were already looking at a xdr_inlineable structure */
481ff49530fSBill Paul 					tabify(fout, indent + 1);
482ff49530fSBill Paul 					if (sizestr == NULL)
483ff49530fSBill Paul 						f_print(fout, "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
484ff49530fSBill Paul 							size);
4859ef5c48bSBill Fumerola 					else {
486ff49530fSBill Paul 						if (size == 0)
487ff49530fSBill Paul 							f_print(fout,
488ff49530fSBill Paul 								"buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
489ff49530fSBill Paul 								sizestr);
490ff49530fSBill Paul 						else
491ff49530fSBill Paul 							f_print(fout,
492ff49530fSBill Paul 								"buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
493ff49530fSBill Paul 								size, sizestr);
494ff49530fSBill Paul 
4959ef5c48bSBill Fumerola 					}
496ff49530fSBill Paul 					f_print(fout, "\n");
497ff49530fSBill Paul 					tabify(fout, indent + 1);
498ff49530fSBill Paul 					f_print(fout,
499ff49530fSBill Paul 						"if (buf == NULL) {\n");
500ff49530fSBill Paul 
501ff49530fSBill Paul 					psav = cur;
502ff49530fSBill Paul 					while (cur != dl){
503ff49530fSBill Paul 						print_stat(indent + 2, &cur->decl);
504ff49530fSBill Paul 						cur = cur->next;
505ff49530fSBill Paul 					}
506ff49530fSBill Paul 
507ff49530fSBill Paul 					f_print(fout, "\n\t\t} else {\n");
508ff49530fSBill Paul 
509ff49530fSBill Paul 					cur = psav;
510ff49530fSBill Paul 					while (cur != dl){
511ff49530fSBill Paul 						emit_inline(indent + 2, &cur->decl, flag);
512ff49530fSBill Paul 						cur = cur->next;
513ff49530fSBill Paul 					}
514ff49530fSBill Paul 
515ff49530fSBill Paul 					tabify(fout, indent + 1);
516ff49530fSBill Paul 					f_print(fout, "}\n");
517ff49530fSBill Paul 				}
5189ef5c48bSBill Fumerola 			}
519ff49530fSBill Paul 			size = 0;
520ff49530fSBill Paul 			i = 0;
521ff49530fSBill Paul 			sizestr = NULL;
522ff49530fSBill Paul 			print_stat(indent + 1, &dl->decl);
523ff49530fSBill Paul 		}
524ff49530fSBill Paul 	}
525ff49530fSBill Paul 
526ff49530fSBill Paul 	if (i > 0)
527ff49530fSBill Paul 		if (sizestr == NULL && size < inline){
528ff49530fSBill Paul 			/* don't expand into inline code if size < inline */
529ff49530fSBill Paul 			while (cur != dl){
530ff49530fSBill Paul 				print_stat(indent + 1, &cur->decl);
531ff49530fSBill Paul 				cur = cur->next;
532ff49530fSBill Paul 			}
533ff49530fSBill Paul 		} else {
534ff49530fSBill Paul 			/* were already looking at a xdr_inlineable structure */
535ff49530fSBill Paul 			if (sizestr == NULL)
536ff49530fSBill Paul 				f_print(fout, "\t\tbuf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
537ff49530fSBill Paul 					size);
538ff49530fSBill Paul 			else
539ff49530fSBill Paul 				if (size == 0)
540ff49530fSBill Paul 					f_print(fout,
541ff49530fSBill Paul 						"\t\tbuf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
542ff49530fSBill Paul 						sizestr);
543ff49530fSBill Paul 				else
544ff49530fSBill Paul 					f_print(fout,
545ff49530fSBill Paul 						"\t\tbuf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
546ff49530fSBill Paul 						size, sizestr);
547ff49530fSBill Paul 
548ff49530fSBill Paul 			f_print(fout, "\n\t\tif (buf == NULL) {\n");
549ff49530fSBill Paul 			psav = cur;
550ff49530fSBill Paul 			while (cur != NULL){
551ff49530fSBill Paul 				print_stat(indent + 2, &cur->decl);
552ff49530fSBill Paul 				cur = cur->next;
553ff49530fSBill Paul 			}
554ff49530fSBill Paul 			f_print(fout, "\t\t} else {\n");
555ff49530fSBill Paul 
556ff49530fSBill Paul 			cur = psav;
557ff49530fSBill Paul 			while (cur != dl){
558ff49530fSBill Paul 				emit_inline(indent + 2, &cur->decl, flag);
559ff49530fSBill Paul 				cur = cur->next;
560ff49530fSBill Paul 			}
561ff49530fSBill Paul 			f_print(fout, "\t\t}\n");
562ff49530fSBill Paul 		}
563ff49530fSBill Paul }
5644e115012SGarrett Wollman 
565526195adSJordan K. Hubbard static void
5664e115012SGarrett Wollman emit_struct(def)
5674e115012SGarrett Wollman 	definition *def;
5684e115012SGarrett Wollman {
5694e115012SGarrett Wollman 	decl_list *dl;
570526195adSJordan K. Hubbard 	int j, size, flag;
571ff49530fSBill Paul 	bas_type *ptr;
572ff49530fSBill Paul 	int can_inline;
5734e115012SGarrett Wollman 
574ff49530fSBill Paul 	if (inline == 0) {
575ff49530fSBill Paul 		/* No xdr_inlining at all */
576ff49530fSBill Paul 		for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
577ff49530fSBill Paul 			print_stat(1, &dl->decl);
578ff49530fSBill Paul 		return;
5794e115012SGarrett Wollman 	}
5804e115012SGarrett Wollman 
581ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
582ff49530fSBill Paul 		if (dl->decl.rel == REL_VECTOR){
583ff49530fSBill Paul 			f_print(fout, "\tint i;\n");
584ff49530fSBill Paul 			break;
585ff49530fSBill Paul 		}
5864e115012SGarrett Wollman 
587ff49530fSBill Paul 	size = 0;
588ff49530fSBill Paul 	can_inline = 0;
589ff49530fSBill Paul 	/*
590ff49530fSBill Paul 	 * Make a first pass and see if inling is possible.
591ff49530fSBill Paul 	 */
592ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
593ff49530fSBill Paul 		if ((dl->decl.prefix == NULL) &&
594ff49530fSBill Paul 		    ((ptr = find_type(dl->decl.type)) != NULL) &&
595ff49530fSBill Paul 		    ((dl->decl.rel == REL_ALIAS)||
596ff49530fSBill Paul 		     (dl->decl.rel == REL_VECTOR))){
597ff49530fSBill Paul 			if (dl->decl.rel == REL_ALIAS)
598ff49530fSBill Paul 				size += ptr->length;
599ff49530fSBill Paul 			else {
600ff49530fSBill Paul 				can_inline = 1;
601ff49530fSBill Paul 				break; /* can be inlined */
602ff49530fSBill Paul 			}
603ff49530fSBill Paul 		} else {
604ff49530fSBill Paul 			if (size >= inline){
605ff49530fSBill Paul 				can_inline = 1;
606ff49530fSBill Paul 				break; /* can be inlined */
607ff49530fSBill Paul 			}
608ff49530fSBill Paul 			size = 0;
609ff49530fSBill Paul 		}
610ff49530fSBill Paul 	if (size >= inline)
611ff49530fSBill Paul 		can_inline = 1;
6124e115012SGarrett Wollman 
613ff49530fSBill Paul 	if (can_inline == 0){	/* can not inline, drop back to old mode */
614ff49530fSBill Paul 		for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
615ff49530fSBill Paul 			print_stat(1, &dl->decl);
616ff49530fSBill Paul 		return;
617ff49530fSBill Paul 	}
618ff49530fSBill Paul 
619ff49530fSBill Paul 	flag = PUT;
620ff49530fSBill Paul 	for (j = 0; j < 2; j++){
621ff49530fSBill Paul 		inline_struct(def, flag);
622ff49530fSBill Paul 		if (flag == PUT)
623ff49530fSBill Paul 			flag = GET;
624ff49530fSBill Paul 	}
625ff49530fSBill Paul 
626ff49530fSBill Paul 	f_print(fout, "\t\treturn (TRUE);\n\t}\n\n");
627ff49530fSBill Paul 
628ff49530fSBill Paul 	/* now take care of XDR_FREE case */
629ff49530fSBill Paul 
630ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
631ff49530fSBill Paul 		print_stat(1, &dl->decl);
632ff49530fSBill Paul 
633ff49530fSBill Paul }
6344e115012SGarrett Wollman 
635526195adSJordan K. Hubbard static void
6364e115012SGarrett Wollman emit_typedef(def)
6374e115012SGarrett Wollman 	definition *def;
6384e115012SGarrett Wollman {
6394e115012SGarrett Wollman 	char *prefix = def->def.ty.old_prefix;
6404e115012SGarrett Wollman 	char *type = def->def.ty.old_type;
6414e115012SGarrett Wollman 	char *amax = def->def.ty.array_max;
6424e115012SGarrett Wollman 	relation rel = def->def.ty.rel;
6434e115012SGarrett Wollman 
6444e115012SGarrett Wollman 	print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
6454e115012SGarrett Wollman }
6464e115012SGarrett Wollman 
647526195adSJordan K. Hubbard static void
648ff49530fSBill Paul print_stat(indent, dec)
649ff49530fSBill Paul 	int indent;
6504e115012SGarrett Wollman 	declaration *dec;
6514e115012SGarrett Wollman {
6524e115012SGarrett Wollman 	char *prefix = dec->prefix;
6534e115012SGarrett Wollman 	char *type = dec->type;
6544e115012SGarrett Wollman 	char *amax = dec->array_max;
6554e115012SGarrett Wollman 	relation rel = dec->rel;
6564e115012SGarrett Wollman 	char name[256];
6574e115012SGarrett Wollman 
6584e115012SGarrett Wollman 	if (isvectordef(type, rel)) {
6594e115012SGarrett Wollman 		s_print(name, "objp->%s", dec->name);
6604e115012SGarrett Wollman 	} else {
6614e115012SGarrett Wollman 		s_print(name, "&objp->%s", dec->name);
6624e115012SGarrett Wollman 	}
663ff49530fSBill Paul 	print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
664ff49530fSBill Paul }
665ff49530fSBill Paul 
666ff49530fSBill Paul 
667ff49530fSBill Paul char *upcase ();
668ff49530fSBill Paul 
669526195adSJordan K. Hubbard static void
670ff49530fSBill Paul emit_inline(indent, decl, flag)
671ff49530fSBill Paul int indent;
672ff49530fSBill Paul declaration *decl;
673ff49530fSBill Paul int flag;
674ff49530fSBill Paul {
675ff49530fSBill Paul 	switch (decl->rel) {
676ff49530fSBill Paul 	case  REL_ALIAS :
677ff49530fSBill Paul 		emit_single_in_line(indent, decl, flag, REL_ALIAS);
678ff49530fSBill Paul 		break;
679ff49530fSBill Paul 	case REL_VECTOR :
680ff49530fSBill Paul 		tabify(fout, indent);
681ff49530fSBill Paul 		f_print(fout, "{\n");
682ff49530fSBill Paul 		tabify(fout, indent + 1);
683ff49530fSBill Paul 		f_print(fout, "register %s *genp;\n\n", decl->type);
684ff49530fSBill Paul 		tabify(fout, indent + 1);
685ff49530fSBill Paul 		f_print(fout,
686ff49530fSBill Paul 			"for (i = 0, genp = objp->%s;\n", decl->name);
687ff49530fSBill Paul 		tabify(fout, indent + 2);
688ff49530fSBill Paul 		f_print(fout, "i < %s; i++) {\n", decl->array_max);
689ff49530fSBill Paul 		emit_single_in_line(indent + 2, decl, flag, REL_VECTOR);
690ff49530fSBill Paul 		tabify(fout, indent + 1);
691ff49530fSBill Paul 		f_print(fout, "}\n");
692ff49530fSBill Paul 		tabify(fout, indent);
693ff49530fSBill Paul 		f_print(fout, "}\n");
694526195adSJordan K. Hubbard 	default:
695ff49530fSBill Paul 	}
696ff49530fSBill Paul }
697ff49530fSBill Paul 
698526195adSJordan K. Hubbard static void
699ff49530fSBill Paul emit_single_in_line(indent, decl, flag, rel)
700ff49530fSBill Paul int indent;
701ff49530fSBill Paul declaration *decl;
702ff49530fSBill Paul int flag;
703ff49530fSBill Paul relation rel;
704ff49530fSBill Paul {
705ff49530fSBill Paul 	char *upp_case;
706ff49530fSBill Paul 	int freed = 0;
707ff49530fSBill Paul 
708ff49530fSBill Paul 	tabify(fout, indent);
709ff49530fSBill Paul 	if (flag == PUT)
710ff49530fSBill Paul 		f_print(fout, "IXDR_PUT_");
711ff49530fSBill Paul 	else
712ff49530fSBill Paul 		if (rel == REL_ALIAS)
713ff49530fSBill Paul 			f_print(fout, "objp->%s = IXDR_GET_", decl->name);
714ff49530fSBill Paul 		else
715ff49530fSBill Paul 			f_print(fout, "*genp++ = IXDR_GET_");
716ff49530fSBill Paul 
717ff49530fSBill Paul 	upp_case = upcase(decl->type);
718ff49530fSBill Paul 
719ff49530fSBill Paul 	/* hack	 - XX */
720ff49530fSBill Paul 	if (strcmp(upp_case, "INT") == 0)
721ff49530fSBill Paul 	{
722ff49530fSBill Paul 		free(upp_case);
723ff49530fSBill Paul 		freed = 1;
724ff49530fSBill Paul 		upp_case = "LONG";
725ff49530fSBill Paul 	}
726ff49530fSBill Paul 
727ff49530fSBill Paul 	if (strcmp(upp_case, "U_INT") == 0)
728ff49530fSBill Paul 	{
729ff49530fSBill Paul 		free(upp_case);
730ff49530fSBill Paul 		freed = 1;
731ff49530fSBill Paul 		upp_case = "U_LONG";
732ff49530fSBill Paul 	}
733ff49530fSBill Paul 	if (flag == PUT)
734ff49530fSBill Paul 		if (rel == REL_ALIAS)
735ff49530fSBill Paul 			f_print(fout,
736ff49530fSBill Paul 				"%s(buf, objp->%s);\n", upp_case, decl->name);
737ff49530fSBill Paul 		else
738ff49530fSBill Paul 			f_print(fout, "%s(buf, *genp++);\n", upp_case);
739ff49530fSBill Paul 
740ff49530fSBill Paul 	else
741ff49530fSBill Paul 		f_print(fout, "%s(buf);\n", upp_case);
742ff49530fSBill Paul 	if (!freed)
743ff49530fSBill Paul 		free(upp_case);
744ff49530fSBill Paul }
745ff49530fSBill Paul 
746ff49530fSBill Paul char *upcase(str)
747ff49530fSBill Paul char *str;
748ff49530fSBill Paul {
749ff49530fSBill Paul 	char *ptr, *hptr;
750ff49530fSBill Paul 
751ff49530fSBill Paul 	ptr =  (char *)malloc(strlen(str)+1);
752ff49530fSBill Paul 	if (ptr == (char *) NULL)
7530e76f40dSPhilippe Charnier 		errx(1, "malloc failed");
754ff49530fSBill Paul 
755ff49530fSBill Paul 	hptr = ptr;
756ff49530fSBill Paul 	while (*str != '\0')
757ff49530fSBill Paul 		*ptr++ = toupper(*str++);
758ff49530fSBill Paul 
759ff49530fSBill Paul 	*ptr = '\0';
760ff49530fSBill Paul 	return (hptr);
7614e115012SGarrett Wollman }
762