Home | History | Annotate | Line # | Download | only in rpcgen
rpc_cout.c revision 1.26
      1  1.26       jmc /*	$NetBSD: rpc_cout.c,v 1.26 2004/06/20 22:20:16 jmc Exp $	*/
      2   1.1     glass /*
      3   1.1     glass  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      4   1.1     glass  * unrestricted use provided that this legend is included on all tape
      5   1.1     glass  * media and as a part of the software program in whole or part.  Users
      6   1.1     glass  * may copy or modify Sun RPC without charge, but are not authorized
      7   1.1     glass  * to license or distribute it to anyone else except as part of a product or
      8   1.4        pk  * program developed by the user or with the express written consent of
      9   1.4        pk  * Sun Microsystems, Inc.
     10   1.4        pk  *
     11   1.1     glass  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12   1.1     glass  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13   1.1     glass  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14   1.4        pk  *
     15   1.1     glass  * Sun RPC is provided with no support and without any obligation on the
     16   1.1     glass  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17   1.1     glass  * modification or enhancement.
     18   1.4        pk  *
     19   1.1     glass  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20   1.1     glass  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21   1.1     glass  * OR ANY PART THEREOF.
     22   1.4        pk  *
     23   1.1     glass  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24   1.1     glass  * or profits or other special, indirect and consequential damages, even if
     25   1.1     glass  * Sun has been advised of the possibility of such damages.
     26   1.4        pk  *
     27   1.1     glass  * Sun Microsystems, Inc.
     28   1.1     glass  * 2550 Garcia Avenue
     29   1.1     glass  * Mountain View, California  94043
     30   1.1     glass  */
     31   1.4        pk 
     32  1.26       jmc #if HAVE_NBTOOL_CONFIG_H
     33  1.26       jmc #include "nbtool_config.h"
     34  1.26       jmc #endif
     35  1.26       jmc 
     36  1.11  christos #include <sys/cdefs.h>
     37  1.19        tv #if defined(__RCSID) && !defined(lint)
     38  1.11  christos #if 0
     39   1.4        pk static char sccsid[] = "@(#)rpc_cout.c 1.13 89/02/22 (C) 1987 SMI";
     40  1.11  christos #else
     41  1.26       jmc __RCSID("$NetBSD: rpc_cout.c,v 1.26 2004/06/20 22:20:16 jmc Exp $");
     42  1.11  christos #endif
     43   1.1     glass #endif
     44   1.1     glass 
     45   1.1     glass /*
     46   1.4        pk  * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
     47   1.1     glass  */
     48  1.19        tv #include <ctype.h>
     49  1.19        tv #include <err.h>
     50   1.1     glass #include <stdio.h>
     51   1.5       cgd #include <stdlib.h>
     52   1.4        pk #include <string.h>
     53  1.11  christos #include "rpc_scan.h"
     54   1.4        pk #include "rpc_parse.h"
     55   1.1     glass #include "rpc_util.h"
     56   1.1     glass 
     57  1.11  christos static int findtype __P((definition *, char *));
     58  1.11  christos static int undefined __P((char *));
     59  1.11  christos static void print_generic_header __P((char *, int));
     60  1.11  christos static void print_header __P((definition *));
     61  1.11  christos static void print_prog_header __P((proc_list *));
     62  1.11  christos static void print_trailer __P((void));
     63  1.11  christos static void print_ifopen __P((int, char *));
     64  1.11  christos static void print_ifarg __P((char *));
     65  1.11  christos static void print_ifsizeof __P((char *, char *));
     66  1.11  christos static void print_ifclose __P((int));
     67  1.11  christos static void print_ifstat __P((int, char *, char *, relation, char *, char *, char *));
     68  1.11  christos static void emit_enum __P((definition *));
     69  1.11  christos static void emit_program __P((definition *));
     70  1.11  christos static void emit_union __P((definition *));
     71  1.11  christos static void emit_struct __P((definition *));
     72  1.11  christos static void emit_typedef __P((definition *));
     73  1.11  christos static void print_stat __P((int, declaration *));
     74   1.1     glass 
     75   1.1     glass /*
     76   1.4        pk  * Emit the C-routine for the given definition
     77   1.1     glass  */
     78   1.1     glass void
     79   1.1     glass emit(def)
     80   1.1     glass 	definition *def;
     81   1.1     glass {
     82   1.4        pk 	if (def->def_kind == DEF_CONST) {
     83   1.4        pk 		return;
     84   1.4        pk 	}
     85   1.4        pk 	if (def->def_kind == DEF_PROGRAM) {
     86   1.4        pk 		emit_program(def);
     87   1.1     glass 		return;
     88   1.1     glass 	}
     89   1.4        pk 	if (def->def_kind == DEF_TYPEDEF) {
     90   1.4        pk 		/* now we need to handle declarations like struct typedef foo
     91   1.4        pk 		 * foo; since we dont want this to be expanded into 2 calls to
     92   1.4        pk 		 * xdr_foo */
     93   1.4        pk 
     94   1.4        pk 		if (strcmp(def->def.ty.old_type, def->def_name) == 0)
     95   1.4        pk 			return;
     96   1.4        pk 	};
     97   1.4        pk 
     98   1.1     glass 	print_header(def);
     99   1.7   mycroft 
    100   1.1     glass 	switch (def->def_kind) {
    101   1.1     glass 	case DEF_UNION:
    102   1.1     glass 		emit_union(def);
    103   1.1     glass 		break;
    104   1.1     glass 	case DEF_ENUM:
    105   1.1     glass 		emit_enum(def);
    106   1.1     glass 		break;
    107   1.1     glass 	case DEF_STRUCT:
    108   1.1     glass 		emit_struct(def);
    109   1.1     glass 		break;
    110   1.1     glass 	case DEF_TYPEDEF:
    111   1.1     glass 		emit_typedef(def);
    112   1.1     glass 		break;
    113  1.11  christos 	case DEF_PROGRAM:
    114  1.11  christos 	case DEF_CONST:
    115  1.24    itojun 		errx(1, "Internal error %s, %d: Case %d not handled",
    116  1.11  christos 		    __FILE__, __LINE__, def->def_kind);
    117  1.11  christos 		break;
    118   1.1     glass 	}
    119   1.1     glass 	print_trailer();
    120   1.1     glass }
    121   1.1     glass 
    122  1.11  christos static int
    123   1.1     glass findtype(def, type)
    124   1.1     glass 	definition *def;
    125   1.4        pk 	char   *type;
    126   1.1     glass {
    127   1.4        pk 
    128   1.1     glass 	if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
    129   1.1     glass 		return (0);
    130   1.1     glass 	} else {
    131   1.1     glass 		return (streq(def->def_name, type));
    132   1.1     glass 	}
    133   1.1     glass }
    134   1.1     glass 
    135  1.11  christos static int
    136   1.1     glass undefined(type)
    137   1.4        pk 	char   *type;
    138   1.1     glass {
    139   1.1     glass 	definition *def;
    140   1.1     glass 
    141   1.1     glass 	def = (definition *) FINDVAL(defined, type, findtype);
    142   1.4        pk 
    143   1.4        pk 
    144   1.1     glass 	return (def == NULL);
    145   1.1     glass }
    146   1.1     glass 
    147  1.11  christos static void
    148   1.4        pk print_generic_header(procname, pointerp)
    149   1.4        pk 	char   *procname;
    150   1.4        pk 	int     pointerp;
    151   1.4        pk {
    152   1.4        pk 	f_print(fout, "\n");
    153   1.4        pk 	f_print(fout, "bool_t\n");
    154   1.4        pk 	if (Cflag) {
    155   1.4        pk 		f_print(fout, "xdr_%s(", procname);
    156   1.4        pk 		f_print(fout, "XDR *xdrs, ");
    157   1.4        pk 		f_print(fout, "%s ", procname);
    158   1.4        pk 		if (pointerp)
    159   1.4        pk 			f_print(fout, "*");
    160   1.7   mycroft 		f_print(fout, "objp)\n{\n");
    161   1.4        pk 	} else {
    162   1.4        pk 		f_print(fout, "xdr_%s(xdrs, objp)\n", procname);
    163   1.4        pk 		f_print(fout, "\tXDR *xdrs;\n");
    164   1.4        pk 		f_print(fout, "\t%s ", procname);
    165   1.4        pk 		if (pointerp)
    166   1.4        pk 			f_print(fout, "*");
    167   1.7   mycroft 		f_print(fout, "objp;\n{\n");
    168   1.4        pk 	}
    169   1.4        pk }
    170   1.1     glass 
    171  1.11  christos static void
    172   1.1     glass print_header(def)
    173   1.1     glass 	definition *def;
    174   1.1     glass {
    175   1.4        pk 	print_generic_header(def->def_name,
    176   1.4        pk 	    def->def_kind != DEF_TYPEDEF ||
    177   1.4        pk 	    !isvectordef(def->def.ty.old_type, def->def.ty.rel));
    178   1.4        pk }
    179   1.4        pk 
    180  1.11  christos static void
    181   1.4        pk print_prog_header(plist)
    182   1.4        pk 	proc_list *plist;
    183   1.4        pk {
    184   1.4        pk 	print_generic_header(plist->args.argname, 1);
    185   1.1     glass }
    186   1.1     glass 
    187  1.11  christos static void
    188   1.1     glass print_trailer()
    189   1.1     glass {
    190   1.1     glass 	f_print(fout, "\treturn (TRUE);\n");
    191   1.1     glass 	f_print(fout, "}\n");
    192   1.1     glass }
    193   1.1     glass 
    194   1.1     glass 
    195  1.11  christos static void
    196   1.1     glass print_ifopen(indent, name)
    197   1.4        pk 	int     indent;
    198   1.4        pk 	char   *name;
    199   1.1     glass {
    200  1.16      fvdl 	char _t_kludge[32];
    201  1.16      fvdl 	/*
    202  1.16      fvdl 	 * XXX Solaris seems to strip the _t. No idea why.
    203  1.16      fvdl 	 */
    204  1.16      fvdl 	if (!strcmp(name, "rpcprog_t") || !strcmp(name, "rpcvers_t") ||
    205  1.16      fvdl 	    !strcmp(name, "rpcproc_t") || !strcmp(name, "rpcprot_t") ||
    206  1.16      fvdl 	    !strcmp(name, "rpcport_t") || !strcmp(name, "rpcpinline_t")) {
    207  1.16      fvdl 		strncpy(_t_kludge, name, strlen(name) - 2);
    208  1.16      fvdl 		name = _t_kludge;
    209  1.16      fvdl 	}
    210   1.1     glass 	tabify(fout, indent);
    211   1.7   mycroft 	f_print(fout, "if (!xdr_%s(xdrs", name);
    212   1.1     glass }
    213   1.1     glass 
    214  1.11  christos static void
    215   1.1     glass print_ifarg(arg)
    216   1.4        pk 	char   *arg;
    217   1.1     glass {
    218   1.1     glass 	f_print(fout, ", %s", arg);
    219   1.1     glass }
    220   1.1     glass 
    221  1.11  christos static void
    222   1.1     glass print_ifsizeof(prefix, type)
    223   1.4        pk 	char   *prefix;
    224   1.4        pk 	char   *type;
    225   1.1     glass {
    226   1.1     glass 	if (streq(type, "bool")) {
    227  1.21  christos 		f_print(fout, ", (u_int)sizeof(bool_t), (xdrproc_t)xdr_bool");
    228   1.1     glass 	} else {
    229  1.21  christos 		f_print(fout, ", (u_int)sizeof(");
    230   1.1     glass 		if (undefined(type) && prefix) {
    231   1.1     glass 			f_print(fout, "%s ", prefix);
    232   1.1     glass 		}
    233   1.4        pk 		f_print(fout, "%s), (xdrproc_t)xdr_%s", type, type);
    234   1.1     glass 	}
    235   1.1     glass }
    236   1.1     glass 
    237  1.11  christos static void
    238   1.1     glass print_ifclose(indent)
    239   1.4        pk 	int     indent;
    240   1.1     glass {
    241   1.7   mycroft 	f_print(fout, "))\n");
    242   1.1     glass 	tabify(fout, indent);
    243   1.7   mycroft 	f_print(fout, "\treturn (FALSE);\n");
    244   1.1     glass }
    245   1.1     glass 
    246  1.11  christos static void
    247   1.1     glass print_ifstat(indent, prefix, type, rel, amax, objname, name)
    248   1.4        pk 	int     indent;
    249   1.4        pk 	char   *prefix;
    250   1.4        pk 	char   *type;
    251   1.1     glass 	relation rel;
    252   1.4        pk 	char   *amax;
    253   1.4        pk 	char   *objname;
    254   1.4        pk 	char   *name;
    255   1.1     glass {
    256   1.4        pk 	char   *alt = NULL;
    257   1.1     glass 
    258   1.1     glass 	switch (rel) {
    259   1.1     glass 	case REL_POINTER:
    260   1.1     glass 		print_ifopen(indent, "pointer");
    261   1.1     glass 		print_ifarg("(char **)");
    262   1.1     glass 		f_print(fout, "%s", objname);
    263   1.1     glass 		print_ifsizeof(prefix, type);
    264   1.1     glass 		break;
    265   1.1     glass 	case REL_VECTOR:
    266   1.1     glass 		if (streq(type, "string")) {
    267   1.1     glass 			alt = "string";
    268   1.4        pk 		} else
    269   1.4        pk 			if (streq(type, "opaque")) {
    270   1.4        pk 				alt = "opaque";
    271   1.4        pk 			}
    272   1.1     glass 		if (alt) {
    273   1.1     glass 			print_ifopen(indent, alt);
    274   1.1     glass 			print_ifarg(objname);
    275   1.1     glass 		} else {
    276   1.1     glass 			print_ifopen(indent, "vector");
    277  1.20  christos 			print_ifarg("(char *)(void *)");
    278   1.1     glass 			f_print(fout, "%s", objname);
    279   1.1     glass 		}
    280   1.1     glass 		print_ifarg(amax);
    281   1.1     glass 		if (!alt) {
    282   1.1     glass 			print_ifsizeof(prefix, type);
    283   1.1     glass 		}
    284   1.1     glass 		break;
    285   1.1     glass 	case REL_ARRAY:
    286   1.1     glass 		if (streq(type, "string")) {
    287   1.1     glass 			alt = "string";
    288   1.4        pk 		} else
    289   1.4        pk 			if (streq(type, "opaque")) {
    290   1.4        pk 				alt = "bytes";
    291   1.4        pk 			}
    292   1.1     glass 		if (streq(type, "string")) {
    293   1.1     glass 			print_ifopen(indent, alt);
    294   1.1     glass 			print_ifarg(objname);
    295   1.1     glass 		} else {
    296   1.1     glass 			if (alt) {
    297   1.1     glass 				print_ifopen(indent, alt);
    298   1.1     glass 			} else {
    299   1.1     glass 				print_ifopen(indent, "array");
    300   1.1     glass 			}
    301   1.1     glass 			print_ifarg("(char **)");
    302   1.1     glass 			if (*objname == '&') {
    303   1.1     glass 				f_print(fout, "%s.%s_val, (u_int *)%s.%s_len",
    304   1.4        pk 				    objname, name, objname, name);
    305   1.1     glass 			} else {
    306   1.1     glass 				f_print(fout, "&%s->%s_val, (u_int *)&%s->%s_len",
    307   1.4        pk 				    objname, name, objname, name);
    308   1.1     glass 			}
    309   1.1     glass 		}
    310   1.1     glass 		print_ifarg(amax);
    311   1.1     glass 		if (!alt) {
    312   1.1     glass 			print_ifsizeof(prefix, type);
    313   1.1     glass 		}
    314   1.1     glass 		break;
    315   1.1     glass 	case REL_ALIAS:
    316   1.1     glass 		print_ifopen(indent, type);
    317   1.1     glass 		print_ifarg(objname);
    318   1.1     glass 		break;
    319   1.1     glass 	}
    320   1.1     glass 	print_ifclose(indent);
    321   1.1     glass }
    322   1.1     glass /* ARGSUSED */
    323  1.11  christos static void
    324   1.1     glass emit_enum(def)
    325   1.1     glass 	definition *def;
    326   1.1     glass {
    327  1.20  christos 	tabify(fout, 1);
    328  1.20  christos 	f_print(fout, "{\n");
    329  1.20  christos 	tabify(fout, 2);
    330  1.20  christos 	f_print(fout, "enum_t et = (enum_t)*objp;\n");
    331  1.20  christos 	print_ifopen(2, "enum");
    332  1.20  christos 	print_ifarg("&et");
    333  1.20  christos 	print_ifclose(2);
    334  1.22  christos 	tabify(fout, 2);
    335  1.22  christos 	f_print(fout, "*objp = (%s)et;\n", def->def_name);
    336  1.20  christos 	tabify(fout, 1);
    337  1.20  christos 	f_print(fout, "}\n");
    338   1.1     glass }
    339   1.1     glass 
    340  1.11  christos static void
    341   1.4        pk emit_program(def)
    342   1.4        pk 	definition *def;
    343   1.4        pk {
    344   1.4        pk 	decl_list *dl;
    345   1.4        pk 	version_list *vlist;
    346   1.4        pk 	proc_list *plist;
    347   1.4        pk 
    348   1.4        pk 	for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
    349   1.4        pk 		for (plist = vlist->procs; plist != NULL; plist = plist->next) {
    350   1.4        pk 			if (!newstyle || plist->arg_num < 2)
    351   1.4        pk 				continue;	/* old style, or single
    352   1.4        pk 						 * argument */
    353   1.4        pk 			print_prog_header(plist);
    354   1.4        pk 			for (dl = plist->args.decls; dl != NULL;
    355   1.4        pk 			    dl = dl->next)
    356   1.4        pk 				print_stat(1, &dl->decl);
    357   1.4        pk 			print_trailer();
    358   1.4        pk 		}
    359   1.4        pk }
    360   1.4        pk 
    361   1.1     glass 
    362  1.11  christos static void
    363   1.1     glass emit_union(def)
    364   1.1     glass 	definition *def;
    365   1.1     glass {
    366   1.1     glass 	declaration *dflt;
    367   1.1     glass 	case_list *cl;
    368   1.1     glass 	declaration *cs;
    369   1.4        pk 	char   *object;
    370  1.23      yamt 	static const char vecformat[] = "objp->%s_u.%s";
    371  1.23      yamt 	static const char format[] = "&objp->%s_u.%s";
    372   1.1     glass 
    373   1.7   mycroft 	f_print(fout, "\n");
    374   1.4        pk 	print_stat(1, &def->def.un.enum_decl);
    375   1.1     glass 	f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
    376   1.1     glass 	for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
    377   1.4        pk 		f_print(fout, "\tcase %s:\n", cl->case_name);
    378   1.4        pk 		if (cl->contflag == 1)	/* a continued case statement */
    379   1.4        pk 			continue;
    380   1.1     glass 		cs = &cl->case_decl;
    381   1.1     glass 		if (!streq(cs->type, "void")) {
    382   1.1     glass 			object = alloc(strlen(def->def_name) + strlen(format) +
    383   1.4        pk 			    strlen(cs->name) + 1);
    384   1.4        pk 			if (isvectordef(cs->type, cs->rel)) {
    385   1.4        pk 				s_print(object, vecformat, def->def_name,
    386   1.4        pk 				    cs->name);
    387   1.4        pk 			} else {
    388   1.4        pk 				s_print(object, format, def->def_name,
    389   1.4        pk 				    cs->name);
    390   1.4        pk 			}
    391   1.7   mycroft 			print_ifstat(2, cs->prefix, cs->type, cs->rel,
    392   1.7   mycroft 			    cs->array_max, object, cs->name);
    393   1.1     glass 			free(object);
    394   1.1     glass 		}
    395   1.1     glass 		f_print(fout, "\t\tbreak;\n");
    396   1.1     glass 	}
    397   1.1     glass 	dflt = def->def.un.default_decl;
    398   1.7   mycroft 	f_print(fout, "\tdefault:\n");
    399   1.1     glass 	if (dflt != NULL) {
    400   1.1     glass 		if (!streq(dflt->type, "void")) {
    401   1.1     glass 			object = alloc(strlen(def->def_name) + strlen(format) +
    402   1.4        pk 			    strlen(dflt->name) + 1);
    403   1.4        pk 			if (isvectordef(dflt->type, dflt->rel)) {
    404   1.4        pk 				s_print(object, vecformat, def->def_name,
    405   1.4        pk 				    dflt->name);
    406   1.4        pk 			} else {
    407   1.4        pk 				s_print(object, format, def->def_name,
    408   1.4        pk 				    dflt->name);
    409   1.4        pk 			}
    410   1.1     glass 			print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
    411   1.4        pk 			    dflt->array_max, object, dflt->name);
    412   1.1     glass 			free(object);
    413   1.1     glass 		}
    414   1.7   mycroft 		f_print(fout, "\t\tbreak;\n");
    415   1.1     glass 	} else {
    416   1.1     glass 		f_print(fout, "\t\treturn (FALSE);\n");
    417   1.1     glass 	}
    418   1.4        pk 
    419   1.1     glass 	f_print(fout, "\t}\n");
    420   1.1     glass }
    421   1.1     glass 
    422  1.11  christos static void
    423   1.1     glass emit_struct(def)
    424   1.1     glass 	definition *def;
    425   1.1     glass {
    426   1.1     glass 	decl_list *dl;
    427   1.4        pk 	int     i, j, size, flag;
    428  1.11  christos 	decl_list *cur = NULL, *psav;
    429   1.4        pk 	bas_type *ptr;
    430   1.4        pk 	char   *sizestr, *plus;
    431   1.4        pk 	char    ptemp[256];
    432   1.4        pk 	int     can_inline;
    433   1.1     glass 
    434   1.4        pk 
    435  1.11  christos 	if (doinline == 0) {
    436  1.20  christos 		f_print(fout, "\n");
    437   1.4        pk 		for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
    438   1.4        pk 			print_stat(1, &dl->decl);
    439   1.4        pk 		return;
    440   1.1     glass 	}
    441   1.4        pk 	size = 0;
    442   1.4        pk 	can_inline = 0;
    443   1.4        pk 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
    444   1.4        pk 		if ((dl->decl.prefix == NULL) &&
    445   1.4        pk 		    ((ptr = find_type(dl->decl.type)) != NULL) &&
    446   1.4        pk 		    ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR))) {
    447   1.4        pk 
    448   1.4        pk 			if (dl->decl.rel == REL_ALIAS)
    449   1.4        pk 				size += ptr->length;
    450   1.4        pk 			else {
    451   1.4        pk 				can_inline = 1;
    452   1.4        pk 				break;	/* can be inlined */
    453   1.4        pk 			};
    454   1.4        pk 		} else {
    455  1.11  christos 			if (size >= doinline) {
    456   1.4        pk 				can_inline = 1;
    457   1.4        pk 				break;	/* can be inlined */
    458   1.4        pk 			}
    459   1.4        pk 			size = 0;
    460   1.4        pk 		}
    461  1.11  christos 	if (size > doinline)
    462   1.4        pk 		can_inline = 1;
    463   1.4        pk 
    464   1.4        pk 	if (can_inline == 0) {	/* can not inline, drop back to old mode */
    465  1.20  christos 		f_print(fout, "\n");
    466   1.4        pk 		for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
    467   1.4        pk 			print_stat(1, &dl->decl);
    468   1.4        pk 		return;
    469   1.4        pk 	};
    470   1.4        pk 
    471   1.7   mycroft 	/* May cause lint to complain. but  ... */
    472  1.12     lukem 	f_print(fout, "\tint32_t *buf;\n");
    473   1.4        pk 
    474   1.7   mycroft 	flag = PUT;
    475   1.7   mycroft 	f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
    476   1.4        pk 
    477   1.4        pk 	for (j = 0; j < 2; j++) {
    478   1.4        pk 		i = 0;
    479   1.4        pk 		size = 0;
    480   1.4        pk 		sizestr = NULL;
    481   1.4        pk 		for (dl = def->def.st.decls; dl != NULL; dl = dl->next) {	/* xxx */
    482   1.4        pk 
    483   1.4        pk 			/* now walk down the list and check for basic types */
    484   1.4        pk 			if ((dl->decl.prefix == NULL) && ((ptr = find_type(dl->decl.type)) != NULL) && ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR))) {
    485   1.4        pk 				if (i == 0)
    486   1.4        pk 					cur = dl;
    487   1.4        pk 				i++;
    488   1.4        pk 
    489   1.4        pk 				if (dl->decl.rel == REL_ALIAS)
    490   1.4        pk 					size += ptr->length;
    491   1.4        pk 				else {
    492   1.4        pk 					/* this is required to handle arrays */
    493   1.4        pk 
    494   1.4        pk 					if (sizestr == NULL)
    495  1.10   mycroft 						plus = "";
    496   1.4        pk 					else
    497  1.10   mycroft 						plus = " + ";
    498   1.4        pk 
    499   1.4        pk 					if (ptr->length != 1)
    500  1.10   mycroft 						s_print(ptemp, "%s%s * %d", plus, dl->decl.array_max, ptr->length);
    501   1.4        pk 					else
    502  1.10   mycroft 						s_print(ptemp, "%s%s", plus, dl->decl.array_max);
    503   1.4        pk 
    504   1.4        pk 					/* now concatenate to sizestr !!!! */
    505   1.4        pk 					if (sizestr == NULL)
    506   1.4        pk 						sizestr = strdup(ptemp);
    507   1.4        pk 					else {
    508  1.25    itojun 						char *nsizestr;
    509  1.25    itojun 
    510  1.25    itojun 						nsizestr = (char *) realloc(sizestr, strlen(sizestr) + strlen(ptemp) + 1);
    511  1.25    itojun 						if (nsizestr == NULL) {
    512   1.4        pk 
    513   1.7   mycroft 							f_print(stderr, "Fatal error : no memory\n");
    514   1.4        pk 							crash();
    515  1.25    itojun 						}
    516  1.25    itojun 						sizestr = nsizestr;
    517   1.4        pk 						sizestr = strcat(sizestr, ptemp);	/* build up length of
    518   1.4        pk 											 * array */
    519   1.4        pk 
    520   1.4        pk 					}
    521   1.4        pk 				}
    522   1.4        pk 
    523   1.4        pk 			} else {
    524  1.14      ross 				if (i > 0)  {
    525  1.11  christos 					if (sizestr == NULL && size < doinline) {
    526   1.4        pk 						/* don't expand into inline
    527  1.11  christos 						 * code if size < doinline */
    528   1.4        pk 						while (cur != dl) {
    529   1.7   mycroft 							print_stat(2, &cur->decl);
    530   1.4        pk 							cur = cur->next;
    531   1.4        pk 						}
    532   1.4        pk 					} else {
    533   1.4        pk 
    534   1.4        pk 
    535   1.4        pk 
    536   1.4        pk 						/* were already looking at a
    537   1.4        pk 						 * xdr_inlineable structure */
    538   1.4        pk 						if (sizestr == NULL)
    539   1.7   mycroft 							f_print(fout, "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);\n",
    540   1.4        pk 							    size);
    541   1.4        pk 						else
    542   1.4        pk 							if (size == 0)
    543   1.4        pk 								f_print(fout,
    544   1.7   mycroft 								    "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, %s * BYTES_PER_XDR_UNIT);\n",
    545   1.4        pk 								    sizestr);
    546   1.4        pk 							else
    547   1.4        pk 								f_print(fout,
    548   1.7   mycroft 								    "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, (%d + %s) * BYTES_PER_XDR_UNIT);\n",
    549   1.4        pk 								    size, sizestr);
    550   1.4        pk 
    551   1.7   mycroft 						f_print(fout, "\t\tif (buf == NULL) {\n");
    552   1.4        pk 
    553   1.4        pk 						psav = cur;
    554   1.4        pk 						while (cur != dl) {
    555   1.7   mycroft 							print_stat(3, &cur->decl);
    556   1.4        pk 							cur = cur->next;
    557   1.4        pk 						}
    558   1.4        pk 
    559   1.7   mycroft 						f_print(fout, "\t\t} else {\n");
    560   1.4        pk 
    561   1.4        pk 						cur = psav;
    562   1.4        pk 						while (cur != dl) {
    563   1.4        pk 							emit_inline(&cur->decl, flag);
    564   1.4        pk 							cur = cur->next;
    565   1.4        pk 						}
    566   1.4        pk 
    567   1.7   mycroft 						f_print(fout, "\t\t}\n");
    568   1.4        pk 					}
    569  1.14      ross 				}
    570   1.4        pk 				size = 0;
    571   1.4        pk 				i = 0;
    572   1.4        pk 				sizestr = NULL;
    573   1.7   mycroft 				print_stat(2, &dl->decl);
    574   1.4        pk 			}
    575   1.4        pk 
    576   1.4        pk 		}
    577  1.14      ross 		if (i > 0) {
    578  1.11  christos 			if (sizestr == NULL && size < doinline) {
    579   1.4        pk 				/* don't expand into inline code if size <
    580  1.11  christos 				 * doinline */
    581   1.4        pk 				while (cur != dl) {
    582   1.7   mycroft 					print_stat(2, &cur->decl);
    583   1.4        pk 					cur = cur->next;
    584   1.4        pk 				}
    585   1.4        pk 			} else {
    586   1.4        pk 
    587   1.4        pk 				/* were already looking at a xdr_inlineable
    588   1.4        pk 				 * structure */
    589   1.4        pk 				if (sizestr == NULL)
    590   1.7   mycroft 					f_print(fout, "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);\n",
    591   1.4        pk 					    size);
    592   1.4        pk 				else
    593   1.4        pk 					if (size == 0)
    594   1.4        pk 						f_print(fout,
    595   1.7   mycroft 						    "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, %s * BYTES_PER_XDR_UNIT);\n",
    596   1.4        pk 						    sizestr);
    597   1.4        pk 					else
    598   1.4        pk 						f_print(fout,
    599   1.7   mycroft 						    "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, (%d + %s) * BYTES_PER_XDR_UNIT);\n",
    600   1.4        pk 						    size, sizestr);
    601   1.4        pk 
    602   1.7   mycroft 				f_print(fout, "\t\tif (buf == NULL) {\n");
    603   1.4        pk 
    604   1.4        pk 				psav = cur;
    605   1.4        pk 				while (cur != NULL) {
    606   1.7   mycroft 					print_stat(3, &cur->decl);
    607   1.4        pk 					cur = cur->next;
    608   1.4        pk 				}
    609   1.7   mycroft 				f_print(fout, "\t\t} else {\n");
    610   1.4        pk 
    611   1.4        pk 				cur = psav;
    612   1.4        pk 				while (cur != dl) {
    613   1.4        pk 					emit_inline(&cur->decl, flag);
    614   1.4        pk 					cur = cur->next;
    615   1.4        pk 				}
    616   1.4        pk 
    617   1.7   mycroft 				f_print(fout, "\t\t}\n");
    618   1.1     glass 
    619   1.4        pk 			}
    620  1.14      ross 		}
    621   1.7   mycroft 		if (flag == PUT) {
    622   1.7   mycroft 			flag = GET;
    623   1.7   mycroft 			f_print(fout, "\t} else if (xdrs->x_op == XDR_DECODE) {\n");
    624   1.7   mycroft 		}
    625   1.4        pk 	}
    626   1.7   mycroft 
    627   1.7   mycroft 	f_print(fout, "\t} else {\n");
    628   1.1     glass 
    629   1.4        pk 	/* now take care of XDR_FREE case */
    630   1.1     glass 
    631   1.4        pk 	for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
    632   1.7   mycroft 		print_stat(2, &dl->decl);
    633   1.7   mycroft 
    634   1.7   mycroft 	f_print(fout, "\t}\n");
    635   1.4        pk }
    636   1.1     glass 
    637  1.11  christos static void
    638   1.1     glass emit_typedef(def)
    639   1.1     glass 	definition *def;
    640   1.1     glass {
    641   1.4        pk 	char   *prefix = def->def.ty.old_prefix;
    642   1.4        pk 	char   *type = def->def.ty.old_type;
    643   1.4        pk 	char   *amax = def->def.ty.array_max;
    644   1.1     glass 	relation rel = def->def.ty.rel;
    645   1.1     glass 
    646  1.20  christos 	f_print(fout, "\n");
    647   1.1     glass 	print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
    648   1.1     glass }
    649   1.1     glass 
    650  1.11  christos static void
    651   1.4        pk print_stat(indent, dec)
    652   1.1     glass 	declaration *dec;
    653   1.4        pk 	int     indent;
    654   1.1     glass {
    655   1.4        pk 	char   *prefix = dec->prefix;
    656   1.4        pk 	char   *type = dec->type;
    657   1.4        pk 	char   *amax = dec->array_max;
    658   1.1     glass 	relation rel = dec->rel;
    659   1.4        pk 	char    name[256];
    660   1.1     glass 
    661   1.1     glass 	if (isvectordef(type, rel)) {
    662   1.1     glass 		s_print(name, "objp->%s", dec->name);
    663   1.1     glass 	} else {
    664   1.1     glass 		s_print(name, "&objp->%s", dec->name);
    665   1.1     glass 	}
    666   1.4        pk 	print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
    667   1.4        pk }
    668   1.4        pk 
    669   1.4        pk 
    670  1.11  christos void
    671   1.4        pk emit_inline(decl, flag)
    672   1.4        pk 	declaration *decl;
    673   1.4        pk 	int     flag;
    674   1.4        pk {
    675   1.4        pk 
    676   1.4        pk /*check whether an array or not */
    677   1.4        pk 
    678   1.4        pk 	switch (decl->rel) {
    679   1.4        pk 	case REL_ALIAS:
    680   1.4        pk 		emit_single_in_line(decl, flag, REL_ALIAS);
    681   1.4        pk 		break;
    682   1.4        pk 	case REL_VECTOR:
    683   1.7   mycroft 		f_print(fout, "\t\t\t{\n");
    684  1.12     lukem 		f_print(fout, "\t\t\t\tint i;\n");
    685  1.12     lukem 		f_print(fout, "\t\t\t\t%s *genp;\n", decl->type);
    686   1.9   mycroft 		f_print(fout, "\n");
    687   1.7   mycroft 		f_print(fout, "\t\t\t\tfor (i = 0, genp = objp->%s;\n",
    688   1.7   mycroft 		    decl->name);
    689   1.7   mycroft 		f_print(fout, "\t\t\t\t    i < %s; i++) {\n\t\t",
    690   1.7   mycroft 		    decl->array_max);
    691   1.4        pk 		emit_single_in_line(decl, flag, REL_VECTOR);
    692   1.7   mycroft 		f_print(fout, "\t\t\t\t}\n\t\t\t}\n");
    693  1.11  christos 		break;
    694  1.11  christos 	case REL_ARRAY:
    695  1.11  christos 	case REL_POINTER:
    696  1.24    itojun 		errx(1, "Internal error %s, %d: Case %d not handled",
    697  1.11  christos 		    __FILE__, __LINE__, decl->rel);
    698   1.4        pk 	}
    699   1.4        pk }
    700   1.4        pk 
    701  1.11  christos void
    702   1.4        pk emit_single_in_line(decl, flag, rel)
    703   1.4        pk 	declaration *decl;
    704   1.4        pk 	int     flag;
    705   1.4        pk 	relation rel;
    706   1.4        pk {
    707   1.4        pk 	char   *upp_case;
    708   1.4        pk 	int     freed = 0;
    709   1.4        pk 
    710   1.4        pk 	if (flag == PUT)
    711   1.7   mycroft 		f_print(fout, "\t\t\tIXDR_PUT_");
    712   1.4        pk 	else
    713   1.4        pk 		if (rel == REL_ALIAS)
    714   1.7   mycroft 			f_print(fout, "\t\t\tobjp->%s = IXDR_GET_", decl->name);
    715   1.4        pk 		else
    716   1.7   mycroft 			f_print(fout, "\t\t\t*genp++ = IXDR_GET_");
    717   1.4        pk 
    718   1.4        pk 	upp_case = upcase(decl->type);
    719   1.4        pk 
    720   1.4        pk 	/* hack  - XX */
    721   1.4        pk 	if (strcmp(upp_case, "INT") == 0) {
    722   1.4        pk 		free(upp_case);
    723   1.4        pk 		freed = 1;
    724  1.20  christos 		upp_case = "INT32";
    725   1.4        pk 	}
    726   1.4        pk 	if (strcmp(upp_case, "U_INT") == 0) {
    727   1.4        pk 		free(upp_case);
    728   1.4        pk 		freed = 1;
    729  1.20  christos 		upp_case = "U_INT32";
    730   1.4        pk 	}
    731  1.15  christos 	if (flag == PUT) {
    732   1.4        pk 		if (rel == REL_ALIAS)
    733   1.8   mycroft 			f_print(fout, "%s(buf, objp->%s);\n", upp_case, decl->name);
    734   1.4        pk 		else
    735   1.8   mycroft 			f_print(fout, "%s(buf, *genp++);\n", upp_case);
    736   1.4        pk 
    737  1.15  christos 	} else
    738   1.4        pk 		f_print(fout, "%s(buf);\n", upp_case);
    739   1.4        pk 	if (!freed)
    740   1.4        pk 		free(upp_case);
    741   1.4        pk 
    742   1.4        pk }
    743   1.4        pk 
    744   1.4        pk 
    745  1.13     lukem char   *
    746   1.4        pk upcase(str)
    747   1.4        pk 	char   *str;
    748   1.4        pk {
    749   1.4        pk 	char   *ptr, *hptr;
    750   1.4        pk 
    751   1.4        pk 
    752  1.13     lukem 	ptr = (char *) malloc(strlen(str) + 1);
    753   1.4        pk 	if (ptr == (char *) NULL) {
    754   1.7   mycroft 		f_print(stderr, "malloc failed\n");
    755   1.4        pk 		exit(1);
    756   1.4        pk 	};
    757   1.4        pk 
    758   1.4        pk 	hptr = ptr;
    759   1.4        pk 	while (*str != '\0')
    760   1.4        pk 		*ptr++ = toupper(*str++);
    761   1.4        pk 
    762   1.4        pk 	*ptr = '\0';
    763   1.4        pk 	return (hptr);
    764   1.4        pk 
    765   1.1     glass }
    766