xref: /freebsd/usr.bin/rpcgen/rpc_cout.c (revision 122562cd2bbe429cf1b50b83bcd6582b9c3ebe56)
14e115012SGarrett Wollman /*
24e115012SGarrett Wollman  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
34e115012SGarrett Wollman  * unrestricted use provided that this legend is included on all tape
44e115012SGarrett Wollman  * media and as a part of the software program in whole or part.  Users
54e115012SGarrett Wollman  * may copy or modify Sun RPC without charge, but are not authorized
64e115012SGarrett Wollman  * to license or distribute it to anyone else except as part of a product or
74e115012SGarrett Wollman  * program developed by the user.
84e115012SGarrett Wollman  *
94e115012SGarrett Wollman  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
104e115012SGarrett Wollman  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
114e115012SGarrett Wollman  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
124e115012SGarrett Wollman  *
134e115012SGarrett Wollman  * Sun RPC is provided with no support and without any obligation on the
144e115012SGarrett Wollman  * part of Sun Microsystems, Inc. to assist in its use, correction,
154e115012SGarrett Wollman  * modification or enhancement.
164e115012SGarrett Wollman  *
174e115012SGarrett Wollman  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
184e115012SGarrett Wollman  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
194e115012SGarrett Wollman  * OR ANY PART THEREOF.
204e115012SGarrett Wollman  *
214e115012SGarrett Wollman  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
224e115012SGarrett Wollman  * or profits or other special, indirect and consequential damages, even if
234e115012SGarrett Wollman  * Sun has been advised of the possibility of such damages.
244e115012SGarrett Wollman  *
254e115012SGarrett Wollman  * Sun Microsystems, Inc.
264e115012SGarrett Wollman  * 2550 Garcia Avenue
274e115012SGarrett Wollman  * Mountain View, California  94043
284e115012SGarrett Wollman  */
29ff49530fSBill Paul 
300e76f40dSPhilippe Charnier #if 0
3175863a6dSPhilippe Charnier #ifndef lint
3263f17371SStefan Farfeleder #ident	"@(#)rpc_cout.c	1.14	93/07/05 SMI"
33ff49530fSBill Paul static char sccsid[] = "@(#)rpc_cout.c 1.13 89/02/22 (C) 1987 SMI";
344e115012SGarrett Wollman #endif
350e76f40dSPhilippe Charnier #endif
364e115012SGarrett Wollman 
3775863a6dSPhilippe Charnier #include <sys/cdefs.h>
3875863a6dSPhilippe Charnier __FBSDID("$FreeBSD$");
3975863a6dSPhilippe Charnier 
404e115012SGarrett Wollman /*
414e115012SGarrett Wollman  * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
424e115012SGarrett Wollman  * Copyright (C) 1987, Sun Microsystems, Inc.
434e115012SGarrett Wollman  */
440e76f40dSPhilippe Charnier #include <ctype.h>
454e115012SGarrett Wollman #include <stdio.h>
46ff49530fSBill Paul #include <string.h>
474e115012SGarrett Wollman #include "rpc_parse.h"
48ff49530fSBill Paul #include "rpc_util.h"
494e115012SGarrett Wollman 
50d3cb5dedSWarner Losh static void print_header( definition * );
51d3cb5dedSWarner Losh static void print_trailer( void );
52d3cb5dedSWarner Losh static void print_stat( int , declaration * );
53d3cb5dedSWarner Losh static void emit_enum( definition * );
54d3cb5dedSWarner Losh static void emit_program( definition * );
55d3cb5dedSWarner Losh static void emit_union( definition * );
56d3cb5dedSWarner Losh static void emit_struct( definition * );
57d3cb5dedSWarner Losh static void emit_typedef( definition * );
58d3cb5dedSWarner Losh static void emit_inline( int, declaration *, int );
59d3cb5dedSWarner Losh static void emit_single_in_line( int, declaration *, int, relation );
604e115012SGarrett Wollman 
614e115012SGarrett Wollman /*
624e115012SGarrett Wollman  * Emit the C-routine for the given definition
634e115012SGarrett Wollman  */
644e115012SGarrett Wollman void
654e115012SGarrett Wollman emit(def)
664e115012SGarrett Wollman 	definition *def;
674e115012SGarrett Wollman {
68ff49530fSBill Paul 	if (def->def_kind == DEF_CONST) {
694e115012SGarrett Wollman 		return;
704e115012SGarrett Wollman 	}
71ff49530fSBill Paul 	if (def->def_kind == DEF_PROGRAM) {
72ff49530fSBill Paul 		emit_program(def);
73ff49530fSBill Paul 		return;
74ff49530fSBill Paul 	}
75ff49530fSBill Paul 	if (def->def_kind == DEF_TYPEDEF) {
76ff49530fSBill Paul 		/*
77ff49530fSBill Paul 		 * now we need to handle declarations like
78ff49530fSBill Paul 		 * struct typedef foo foo;
79ff49530fSBill Paul 		 * since we dont want this to be expanded into 2 calls to xdr_foo
80ff49530fSBill Paul 		 */
81ff49530fSBill Paul 
82ff49530fSBill Paul 		if (strcmp(def->def.ty.old_type, def->def_name) == 0)
83ff49530fSBill Paul 			return;
84ff49530fSBill Paul 	};
854e115012SGarrett Wollman 	print_header(def);
864e115012SGarrett Wollman 	switch (def->def_kind) {
874e115012SGarrett Wollman 	case DEF_UNION:
884e115012SGarrett Wollman 		emit_union(def);
894e115012SGarrett Wollman 		break;
904e115012SGarrett Wollman 	case DEF_ENUM:
914e115012SGarrett Wollman 		emit_enum(def);
924e115012SGarrett Wollman 		break;
934e115012SGarrett Wollman 	case DEF_STRUCT:
944e115012SGarrett Wollman 		emit_struct(def);
954e115012SGarrett Wollman 		break;
964e115012SGarrett Wollman 	case DEF_TYPEDEF:
974e115012SGarrett Wollman 		emit_typedef(def);
984e115012SGarrett Wollman 		break;
99526195adSJordan K. Hubbard 		/* DEF_CONST and DEF_PROGRAM have already been handled */
100526195adSJordan K. Hubbard 	default:
10140ad8885SAlfred Perlstein 		break;
1024e115012SGarrett Wollman 	}
1034e115012SGarrett Wollman 	print_trailer();
1044e115012SGarrett Wollman }
1054e115012SGarrett Wollman 
106526195adSJordan K. Hubbard static int
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 
164122562cdSStefan Farfeleder 	if (inline_size == 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")) {
36075863a6dSPhilippe Charnier 			object = xmalloc(strlen(def->def_name) +
36175863a6dSPhilippe Charnier 			                 strlen(format) + 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");
37975863a6dSPhilippe Charnier 			object = xmalloc(strlen(def->def_name) +
38075863a6dSPhilippe Charnier 			                 strlen(format) + 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 
41840ad8885SAlfred Perlstein 	cur = NULL;
419ff49530fSBill Paul 	if (flag == PUT)
420ff49530fSBill Paul 		f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
421ff49530fSBill Paul 	else
422ff49530fSBill Paul 		f_print(fout, "\t\treturn (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
423ff49530fSBill Paul 
424ff49530fSBill Paul 	i = 0;
425ff49530fSBill Paul 	size = 0;
426ff49530fSBill Paul 	sizestr = NULL;
427ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
428ff49530fSBill Paul 		/* now walk down the list and check for basic types */
429ff49530fSBill Paul 		if ((dl->decl.prefix == NULL) &&
430ff49530fSBill Paul 		    ((ptr = find_type(dl->decl.type)) != NULL) &&
431ff49530fSBill Paul 		    ((dl->decl.rel == REL_ALIAS) ||
432ff49530fSBill Paul 		     (dl->decl.rel == REL_VECTOR))){
433ff49530fSBill Paul 			if (i == 0)
434ff49530fSBill Paul 				cur = dl;
435ff49530fSBill Paul 			i++;
436ff49530fSBill Paul 
437ff49530fSBill Paul 			if (dl->decl.rel == REL_ALIAS)
438ff49530fSBill Paul 				size += ptr->length;
439ff49530fSBill Paul 			else {
440ff49530fSBill Paul 				/* this code is required to handle arrays */
441ff49530fSBill Paul 				if (sizestr == NULL)
442ff49530fSBill Paul 					plus = "";
443ff49530fSBill Paul 				else
444ff49530fSBill Paul 					plus = " + ";
445ff49530fSBill Paul 
446ff49530fSBill Paul 				if (ptr->length != 1)
447ff49530fSBill Paul 					s_print(ptemp, "%s%s * %d",
448ff49530fSBill Paul 						plus, dl->decl.array_max,
449ff49530fSBill Paul 						ptr->length);
450ff49530fSBill Paul 				else
451ff49530fSBill Paul 					s_print(ptemp, "%s%s", plus,
452ff49530fSBill Paul 						dl->decl.array_max);
453ff49530fSBill Paul 
454ff49530fSBill Paul 				/* now concatenate to sizestr !!!! */
45575863a6dSPhilippe Charnier 				if (sizestr == NULL) {
45675863a6dSPhilippe Charnier 					sizestr = xstrdup(ptemp);
45775863a6dSPhilippe Charnier 				}
458ff49530fSBill Paul 				else{
45975863a6dSPhilippe Charnier 					sizestr = xrealloc(sizestr,
460ff49530fSBill Paul 							  strlen(sizestr)
461ff49530fSBill Paul 							  +strlen(ptemp)+1);
462ff49530fSBill Paul 					sizestr = strcat(sizestr, ptemp);
463ff49530fSBill Paul 					/* build up length of array */
464ff49530fSBill Paul 				}
465ff49530fSBill Paul 			}
466ff49530fSBill Paul 		} else {
4679ef5c48bSBill Fumerola 			if (i > 0) {
468122562cdSStefan Farfeleder 				if (sizestr == NULL && size < inline_size){
469ff49530fSBill Paul 					/*
470ff49530fSBill Paul 					 * don't expand into inline code
471122562cdSStefan Farfeleder 					 * if size < inline_size
472ff49530fSBill Paul 					 */
473ff49530fSBill Paul 					while (cur != dl){
474ff49530fSBill Paul 						print_stat(indent + 1, &cur->decl);
475ff49530fSBill Paul 						cur = cur->next;
476ff49530fSBill Paul 					}
477ff49530fSBill Paul 				} else {
478ff49530fSBill Paul 					/* were already looking at a xdr_inlineable structure */
479ff49530fSBill Paul 					tabify(fout, indent + 1);
480ff49530fSBill Paul 					if (sizestr == NULL)
481ff49530fSBill Paul 						f_print(fout, "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
482ff49530fSBill Paul 							size);
4839ef5c48bSBill Fumerola 					else {
484ff49530fSBill Paul 						if (size == 0)
485ff49530fSBill Paul 							f_print(fout,
486ff49530fSBill Paul 								"buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
487ff49530fSBill Paul 								sizestr);
488ff49530fSBill Paul 						else
489ff49530fSBill Paul 							f_print(fout,
490ff49530fSBill Paul 								"buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
491ff49530fSBill Paul 								size, sizestr);
492ff49530fSBill Paul 
4939ef5c48bSBill Fumerola 					}
494ff49530fSBill Paul 					f_print(fout, "\n");
495ff49530fSBill Paul 					tabify(fout, indent + 1);
496ff49530fSBill Paul 					f_print(fout,
497ff49530fSBill Paul 						"if (buf == NULL) {\n");
498ff49530fSBill Paul 
499ff49530fSBill Paul 					psav = cur;
500ff49530fSBill Paul 					while (cur != dl){
501ff49530fSBill Paul 						print_stat(indent + 2, &cur->decl);
502ff49530fSBill Paul 						cur = cur->next;
503ff49530fSBill Paul 					}
504ff49530fSBill Paul 
505ff49530fSBill Paul 					f_print(fout, "\n\t\t} else {\n");
506ff49530fSBill Paul 
507ff49530fSBill Paul 					cur = psav;
508ff49530fSBill Paul 					while (cur != dl){
509ff49530fSBill Paul 						emit_inline(indent + 2, &cur->decl, flag);
510ff49530fSBill Paul 						cur = cur->next;
511ff49530fSBill Paul 					}
512ff49530fSBill Paul 
513ff49530fSBill Paul 					tabify(fout, indent + 1);
514ff49530fSBill Paul 					f_print(fout, "}\n");
515ff49530fSBill Paul 				}
5169ef5c48bSBill Fumerola 			}
517ff49530fSBill Paul 			size = 0;
518ff49530fSBill Paul 			i = 0;
519ff49530fSBill Paul 			sizestr = NULL;
520ff49530fSBill Paul 			print_stat(indent + 1, &dl->decl);
521ff49530fSBill Paul 		}
522ff49530fSBill Paul 	}
523ff49530fSBill Paul 
52440ad8885SAlfred Perlstein 	if (i > 0) {
525122562cdSStefan Farfeleder 		if (sizestr == NULL && size < inline_size){
526122562cdSStefan Farfeleder 			/* don't expand into inline code if size < inline_size */
527ff49530fSBill Paul 			while (cur != dl){
528ff49530fSBill Paul 				print_stat(indent + 1, &cur->decl);
529ff49530fSBill Paul 				cur = cur->next;
530ff49530fSBill Paul 			}
531ff49530fSBill Paul 		} else {
532ff49530fSBill Paul 			/* were already looking at a xdr_inlineable structure */
533ff49530fSBill Paul 			if (sizestr == NULL)
534ff49530fSBill Paul 				f_print(fout, "\t\tbuf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
535ff49530fSBill Paul 					size);
536ff49530fSBill Paul 			else
537ff49530fSBill Paul 				if (size == 0)
538ff49530fSBill Paul 					f_print(fout,
539ff49530fSBill Paul 						"\t\tbuf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
540ff49530fSBill Paul 						sizestr);
541ff49530fSBill Paul 				else
542ff49530fSBill Paul 					f_print(fout,
543ff49530fSBill Paul 						"\t\tbuf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
544ff49530fSBill Paul 						size, sizestr);
545ff49530fSBill Paul 
546ff49530fSBill Paul 			f_print(fout, "\n\t\tif (buf == NULL) {\n");
547ff49530fSBill Paul 			psav = cur;
548ff49530fSBill Paul 			while (cur != NULL){
549ff49530fSBill Paul 				print_stat(indent + 2, &cur->decl);
550ff49530fSBill Paul 				cur = cur->next;
551ff49530fSBill Paul 			}
552ff49530fSBill Paul 			f_print(fout, "\t\t} else {\n");
553ff49530fSBill Paul 
554ff49530fSBill Paul 			cur = psav;
555ff49530fSBill Paul 			while (cur != dl){
556ff49530fSBill Paul 				emit_inline(indent + 2, &cur->decl, flag);
557ff49530fSBill Paul 				cur = cur->next;
558ff49530fSBill Paul 			}
559ff49530fSBill Paul 			f_print(fout, "\t\t}\n");
560ff49530fSBill Paul 		}
561ff49530fSBill Paul 	}
56240ad8885SAlfred Perlstein }
5634e115012SGarrett Wollman 
564526195adSJordan K. Hubbard static void
5654e115012SGarrett Wollman emit_struct(def)
5664e115012SGarrett Wollman 	definition *def;
5674e115012SGarrett Wollman {
5684e115012SGarrett Wollman 	decl_list *dl;
569526195adSJordan K. Hubbard 	int j, size, flag;
570ff49530fSBill Paul 	bas_type *ptr;
571ff49530fSBill Paul 	int can_inline;
5724e115012SGarrett Wollman 
573122562cdSStefan Farfeleder 	if (inline_size == 0) {
574ff49530fSBill Paul 		/* No xdr_inlining at all */
575ff49530fSBill Paul 		for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
576ff49530fSBill Paul 			print_stat(1, &dl->decl);
577ff49530fSBill Paul 		return;
5784e115012SGarrett Wollman 	}
5794e115012SGarrett Wollman 
580ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
581ff49530fSBill Paul 		if (dl->decl.rel == REL_VECTOR){
582ff49530fSBill Paul 			f_print(fout, "\tint i;\n");
583ff49530fSBill Paul 			break;
584ff49530fSBill Paul 		}
5854e115012SGarrett Wollman 
586ff49530fSBill Paul 	size = 0;
587ff49530fSBill Paul 	can_inline = 0;
588ff49530fSBill Paul 	/*
589ff49530fSBill Paul 	 * Make a first pass and see if inling is possible.
590ff49530fSBill Paul 	 */
591ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
592ff49530fSBill Paul 		if ((dl->decl.prefix == NULL) &&
593ff49530fSBill Paul 		    ((ptr = find_type(dl->decl.type)) != NULL) &&
594ff49530fSBill Paul 		    ((dl->decl.rel == REL_ALIAS)||
595ff49530fSBill Paul 		     (dl->decl.rel == REL_VECTOR))){
596ff49530fSBill Paul 			if (dl->decl.rel == REL_ALIAS)
597ff49530fSBill Paul 				size += ptr->length;
598ff49530fSBill Paul 			else {
599ff49530fSBill Paul 				can_inline = 1;
600ff49530fSBill Paul 				break; /* can be inlined */
601ff49530fSBill Paul 			}
602ff49530fSBill Paul 		} else {
603122562cdSStefan Farfeleder 			if (size >= inline_size){
604ff49530fSBill Paul 				can_inline = 1;
605ff49530fSBill Paul 				break; /* can be inlined */
606ff49530fSBill Paul 			}
607ff49530fSBill Paul 			size = 0;
608ff49530fSBill Paul 		}
609122562cdSStefan Farfeleder 	if (size >= inline_size)
610ff49530fSBill Paul 		can_inline = 1;
6114e115012SGarrett Wollman 
612ff49530fSBill Paul 	if (can_inline == 0){	/* can not inline, drop back to old mode */
613ff49530fSBill Paul 		for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
614ff49530fSBill Paul 			print_stat(1, &dl->decl);
615ff49530fSBill Paul 		return;
616ff49530fSBill Paul 	}
617ff49530fSBill Paul 
618ff49530fSBill Paul 	flag = PUT;
619ff49530fSBill Paul 	for (j = 0; j < 2; j++){
620ff49530fSBill Paul 		inline_struct(def, flag);
621ff49530fSBill Paul 		if (flag == PUT)
622ff49530fSBill Paul 			flag = GET;
623ff49530fSBill Paul 	}
624ff49530fSBill Paul 
625ff49530fSBill Paul 	f_print(fout, "\t\treturn (TRUE);\n\t}\n\n");
626ff49530fSBill Paul 
627ff49530fSBill Paul 	/* now take care of XDR_FREE case */
628ff49530fSBill Paul 
629ff49530fSBill Paul 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
630ff49530fSBill Paul 		print_stat(1, &dl->decl);
631ff49530fSBill Paul 
632ff49530fSBill Paul }
6334e115012SGarrett Wollman 
634526195adSJordan K. Hubbard static void
6354e115012SGarrett Wollman emit_typedef(def)
6364e115012SGarrett Wollman 	definition *def;
6374e115012SGarrett Wollman {
6384e115012SGarrett Wollman 	char *prefix = def->def.ty.old_prefix;
6394e115012SGarrett Wollman 	char *type = def->def.ty.old_type;
6404e115012SGarrett Wollman 	char *amax = def->def.ty.array_max;
6414e115012SGarrett Wollman 	relation rel = def->def.ty.rel;
6424e115012SGarrett Wollman 
6434e115012SGarrett Wollman 	print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
6444e115012SGarrett Wollman }
6454e115012SGarrett Wollman 
646526195adSJordan K. Hubbard static void
647ff49530fSBill Paul print_stat(indent, dec)
648ff49530fSBill Paul 	int indent;
6494e115012SGarrett Wollman 	declaration *dec;
6504e115012SGarrett Wollman {
6514e115012SGarrett Wollman 	char *prefix = dec->prefix;
6524e115012SGarrett Wollman 	char *type = dec->type;
6534e115012SGarrett Wollman 	char *amax = dec->array_max;
6544e115012SGarrett Wollman 	relation rel = dec->rel;
6554e115012SGarrett Wollman 	char name[256];
6564e115012SGarrett Wollman 
6574e115012SGarrett Wollman 	if (isvectordef(type, rel)) {
6584e115012SGarrett Wollman 		s_print(name, "objp->%s", dec->name);
6594e115012SGarrett Wollman 	} else {
6604e115012SGarrett Wollman 		s_print(name, "&objp->%s", dec->name);
6614e115012SGarrett Wollman 	}
662ff49530fSBill Paul 	print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
663ff49530fSBill Paul }
664ff49530fSBill Paul 
665ff49530fSBill Paul 
666ff49530fSBill Paul char *upcase ();
667ff49530fSBill Paul 
668526195adSJordan K. Hubbard static void
669ff49530fSBill Paul emit_inline(indent, decl, flag)
670ff49530fSBill Paul int indent;
671ff49530fSBill Paul declaration *decl;
672ff49530fSBill Paul int flag;
673ff49530fSBill Paul {
674ff49530fSBill Paul 	switch (decl->rel) {
675ff49530fSBill Paul 	case  REL_ALIAS :
676ff49530fSBill Paul 		emit_single_in_line(indent, decl, flag, REL_ALIAS);
677ff49530fSBill Paul 		break;
678ff49530fSBill Paul 	case REL_VECTOR :
679ff49530fSBill Paul 		tabify(fout, indent);
680ff49530fSBill Paul 		f_print(fout, "{\n");
681ff49530fSBill Paul 		tabify(fout, indent + 1);
682ff49530fSBill Paul 		f_print(fout, "register %s *genp;\n\n", decl->type);
683ff49530fSBill Paul 		tabify(fout, indent + 1);
684ff49530fSBill Paul 		f_print(fout,
685ff49530fSBill Paul 			"for (i = 0, genp = objp->%s;\n", decl->name);
686ff49530fSBill Paul 		tabify(fout, indent + 2);
687ff49530fSBill Paul 		f_print(fout, "i < %s; i++) {\n", decl->array_max);
688ff49530fSBill Paul 		emit_single_in_line(indent + 2, decl, flag, REL_VECTOR);
689ff49530fSBill Paul 		tabify(fout, indent + 1);
690ff49530fSBill Paul 		f_print(fout, "}\n");
691ff49530fSBill Paul 		tabify(fout, indent);
692ff49530fSBill Paul 		f_print(fout, "}\n");
69340ad8885SAlfred Perlstein 		break;
694526195adSJordan K. Hubbard 	default:
69540ad8885SAlfred Perlstein 		break;
696ff49530fSBill Paul 	}
697ff49530fSBill Paul }
698ff49530fSBill Paul 
699526195adSJordan K. Hubbard static void
700ff49530fSBill Paul emit_single_in_line(indent, decl, flag, rel)
701ff49530fSBill Paul int indent;
702ff49530fSBill Paul declaration *decl;
703ff49530fSBill Paul int flag;
704ff49530fSBill Paul relation rel;
705ff49530fSBill Paul {
706ff49530fSBill Paul 	char *upp_case;
707ff49530fSBill Paul 	int freed = 0;
708ff49530fSBill Paul 
709ff49530fSBill Paul 	tabify(fout, indent);
710ff49530fSBill Paul 	if (flag == PUT)
711ff49530fSBill Paul 		f_print(fout, "IXDR_PUT_");
712ff49530fSBill Paul 	else
713ff49530fSBill Paul 		if (rel == REL_ALIAS)
714ff49530fSBill Paul 			f_print(fout, "objp->%s = IXDR_GET_", decl->name);
715ff49530fSBill Paul 		else
716ff49530fSBill Paul 			f_print(fout, "*genp++ = IXDR_GET_");
717ff49530fSBill Paul 
718ff49530fSBill Paul 	upp_case = upcase(decl->type);
719ff49530fSBill Paul 
720ff49530fSBill Paul 	/* hack	 - XX */
721ff49530fSBill Paul 	if (strcmp(upp_case, "INT") == 0)
722ff49530fSBill Paul 	{
723ff49530fSBill Paul 		free(upp_case);
724ff49530fSBill Paul 		freed = 1;
725ff49530fSBill Paul 		upp_case = "LONG";
726ff49530fSBill Paul 	}
727ff49530fSBill Paul 
728ff49530fSBill Paul 	if (strcmp(upp_case, "U_INT") == 0)
729ff49530fSBill Paul 	{
730ff49530fSBill Paul 		free(upp_case);
731ff49530fSBill Paul 		freed = 1;
732ff49530fSBill Paul 		upp_case = "U_LONG";
733ff49530fSBill Paul 	}
734ff49530fSBill Paul 	if (flag == PUT)
735ff49530fSBill Paul 		if (rel == REL_ALIAS)
736ff49530fSBill Paul 			f_print(fout,
737ff49530fSBill Paul 				"%s(buf, objp->%s);\n", upp_case, decl->name);
738ff49530fSBill Paul 		else
739ff49530fSBill Paul 			f_print(fout, "%s(buf, *genp++);\n", upp_case);
740ff49530fSBill Paul 
741ff49530fSBill Paul 	else
742ff49530fSBill Paul 		f_print(fout, "%s(buf);\n", upp_case);
743ff49530fSBill Paul 	if (!freed)
744ff49530fSBill Paul 		free(upp_case);
745ff49530fSBill Paul }
746ff49530fSBill Paul 
747ff49530fSBill Paul char *upcase(str)
748ff49530fSBill Paul char *str;
749ff49530fSBill Paul {
750ff49530fSBill Paul 	char *ptr, *hptr;
751ff49530fSBill Paul 
75275863a6dSPhilippe Charnier 	ptr = (char *)xmalloc(strlen(str)+1);
753ff49530fSBill Paul 
754ff49530fSBill Paul 	hptr = ptr;
755ff49530fSBill Paul 	while (*str != '\0')
756ff49530fSBill Paul 		*ptr++ = toupper(*str++);
757ff49530fSBill Paul 
758ff49530fSBill Paul 	*ptr = '\0';
759ff49530fSBill Paul 	return (hptr);
7604e115012SGarrett Wollman }
761